-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
332 lines (272 loc) · 11.1 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright (c) 2015-2016 Sergio Martins <iamsergio@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "mainwindow.h"
#include "syntaxhighlighter.h"
#include <QTimer>
#include <QItemSelection>
#include <QInputDialog>
#include <QFontDatabase>
#include <QApplication>
#include <QFileInfo>
#include <QProcess>
#include <QDebug>
#include <QWindow>
#include <QSyntaxHighlighter>
enum {
FilterUpdateTimeout = 400 // ms
};
static void runCommand(const QString &command)
{
auto proc = new QProcess();
QObject::connect(proc, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
[proc](int ret, QProcess::ExitStatus) {
if (ret != 0) {
qWarning() << "Error running" << proc->program() << proc->arguments();
}
proc->deleteLater();
});
proc->start(command);
}
MainWindow::MainWindow(const QString &initialFilter, QWidget *parent)
: QMainWindow(parent)
, m_snippet(nullptr)
{
qApp->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
setupUi(this);
m_splitter->setSizes({100, 1000});
m_treeView->setModel(m_kernel.topLevelModel());
connect(m_kernel.model(), &SnippetModel::loaded,
[this](int num, const QString &path) { statusBar()->showMessage(QStringLiteral("Loaded %1 snippets from %2").arg(num).arg(path));});
/*connect(m_kernel.filterModel(), &SnippetProxyModel::countChanged,
[this] {
statusBar()->showMessage(QStringLiteral("Showing %1 snippets").arg(m_kernel.filterModel()->rowCount())); // FIXME: Needs a recursive count
});*/
m_tagsLineEdit->setStyleSheet(QStringLiteral("QLineEdit { font-weight: bold; }"));
connect(m_tagsLineEdit, &QLineEdit::textChanged, this, &MainWindow::saveNewTags);
connect(m_textEdit, &QPlainTextEdit::textChanged, this, &MainWindow::saveNewContents);
connect(m_textEdit, &TextEdit::openExternallyRequested,
this, &MainWindow::openCurrentSnippetInEditor);
m_highlighter = new SyntaxHighlighter(m_textEdit->document());
QTimer::singleShot(0, &m_kernel, &Kernel::load);
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::onSelectionChanged);
connect(m_actionReload, &QAction::triggered, m_kernel.model(), &SnippetModel::load);
connect(m_actionQuit, &QAction::triggered, qApp, &QApplication::quit);
connect(m_actionOpenDataFolder, &QAction::triggered, this, &MainWindow::openDataFolder);
m_treeView->setHeaderHidden(true);
setWindowTitle(tr("Snippy"));
m_newFolderAction = m_toolBar->addAction(qApp->style()->standardIcon(QStyle::SP_FileDialogNewFolder), "New Folder");
connect(m_newFolderAction, &QAction::triggered, this, &MainWindow::createFolder);
m_newAction = m_toolBar->addAction(QIcon(":/img/list-add.png"), "New Snippet");
connect(m_newAction, &QAction::triggered, this, &MainWindow::createSnippet);
m_delAction = m_toolBar->addAction(QIcon(":/img/list-remove.png"), "Remove");
connect(m_delAction, &QAction::triggered, this, &MainWindow::deleteSnippet);
m_delAction->setShortcut(QKeySequence(QKeySequence::Delete));
connect(m_actionExpandAll, &QAction::triggered, m_treeView, &QTreeView::expandAll);
m_filterLineEdit->setFocus();
connect(m_filterLineEdit, &QLineEdit::textChanged, this, &MainWindow::scheduleFilter);
m_filterLineEdit->setText(initialFilter);
m_scheduleFilterTimer.setSingleShot(true);
connect(&m_scheduleFilterTimer, &QTimer::timeout, this, &MainWindow::updateFilter);
connect(m_deepSearchCB, &QCheckBox::toggled, this, &MainWindow::updateFilter);
connect(m_kernel.filterModel(), &SnippetProxyModel::filterHasErrorChanged,
this, &MainWindow::updateFilterBackground);
create();
QWindow *window = windowHandle();
connect(window, &QWindow::activeChanged, window, [this, window] {
if (window->isActive()) {
m_filterLineEdit->setFocus();
}
});
// m_treeView->setRootIsDecorated(false); commented out because it's december, the tree should be decorated
setSnippet(nullptr);
}
void MainWindow::setSnippet(Snippet *snippet)
{
//if (m_snippet == snippet)
//return;
m_snippet = snippet;
if (snippet) {
m_textEdit->document()->setPlainText(snippet->contents());
m_tagsLineEdit->setText(snippet->tagsString());
} else {
m_textEdit->document()->setPlainText(QString());
m_tagsLineEdit->setText(QString());
}
m_textEdit->setEnabled(snippet);
m_tagsLineEdit->setEnabled(snippet);
m_delAction->setEnabled(snippet);
}
void MainWindow::onSelectionChanged(const QItemSelection &selection, const QItemSelection &/*deselection*/)
{
const QModelIndexList indexes = selection.indexes();
if (indexes.isEmpty()) {
setSnippet(nullptr);
return;
}
const bool isFolder = indexes.first().data(SnippetModel::IsFolderRole).toBool();
if (isFolder) {
setSnippet(nullptr);
} else {
Snippet *snippet = indexes.first().data(SnippetModel::SnippetRole).value<Snippet*>();
setSnippet(snippet);
}
}
void MainWindow::saveNewTags(const QString &text)
{
if (m_snippet)
m_snippet->setTags(text);
}
void MainWindow::saveNewContents()
{
if (m_snippet)
m_snippet->setContents(m_textEdit->toPlainText());
}
void MainWindow::createFolder()
{
const QString &name = QInputDialog::getText(this, "Snippy", "Enter folder name");
if (!name.isEmpty()) {
QModelIndex selectedProxyIndex = selectedIndex();
QModelIndex selectedIndex = m_kernel.mapToSource(selectedProxyIndex);
QStandardItem *newItem = m_kernel.model()->createFolder(name, selectedIndex);
if (newItem) {
QModelIndex newProxyIndex = m_kernel.mapFromSource(newItem->index());
m_treeView->expand(selectedProxyIndex);
m_treeView->expand(newProxyIndex); // If we create a folder that already exists, expand it, since it has children probably
m_treeView->scrollTo(newProxyIndex, QAbstractItemView::PositionAtCenter);
m_treeView->selectionModel()->select(newProxyIndex, QItemSelectionModel::ClearAndSelect);
} else {
statusBar()->showMessage(QStringLiteral("Failed to create folder: %1").arg(name));
}
}
}
void MainWindow::createSnippet()
{
QModelIndexList indexes = m_treeView->selectionModel()->selectedIndexes();
QModelIndex parentIndex;
if (!indexes.isEmpty()) {
QModelIndex index = indexes.at(0);
if (index.data(SnippetModel::IsFolderRole).toBool()) {
parentIndex = index;
} else {
parentIndex = index.parent();
}
}
QModelIndex newIndex = m_kernel.model()->addSnippet(m_kernel.mapToSource(parentIndex));
if (newIndex.isValid()) {
m_treeView->expand(parentIndex);
QModelIndex newProxyIndex = m_kernel.mapFromSource(newIndex);
m_treeView->scrollTo(newProxyIndex);
m_treeView->selectionModel()->select(newProxyIndex, QItemSelectionModel::ClearAndSelect);
m_treeView->edit(newProxyIndex);
}
}
void MainWindow::deleteSnippet()
{
QModelIndex proxyIndex = selectedIndex();
m_kernel.model()->removeSnippet(m_kernel.mapToSource(proxyIndex));
}
void MainWindow::scheduleFilter()
{
m_scheduleFilterTimer.start(FilterUpdateTimeout);
}
void MainWindow::updateFilter()
{
const QString text = m_filterLineEdit->text();
const bool hasText = !text.isEmpty();
auto filterModel = m_kernel.filterModel();
filterModel->setFilterText(text);
filterModel->setIsDeepSearch(m_deepSearchCB->isChecked());
if (hasText)
m_treeView->expandAll();
if (hasText && !selectedIndex().isValid()) {
QModelIndex index = firstSnippet(QModelIndex());
if (index.isValid()) {
m_treeView->selectionModel()->select(index, QItemSelectionModel::Select);
}
}
m_highlighter->setTokens(m_kernel.filterModel()->searchTokens());
}
QModelIndex MainWindow::firstSnippet(const QModelIndex &index) const
{
if (index.isValid() && !index.data(SnippetModel::IsFolderRole).toBool())
return index;
auto model = m_kernel.topLevelModel();
const int count = model->rowCount(index);
for (int row = 0; row < count; ++row) {
QModelIndex candidate = firstSnippet(model->index(row, 0, index));
if (candidate.isValid())
return candidate;
}
return {};
}
void MainWindow::openCurrentSnippetInEditor()
{
if (!m_snippet) {
qWarning() << "No snippet selected";
return;
}
QString editorCommand = m_kernel.externalEditor();
if (editorCommand.isEmpty()) {
qWarning() << "No external editor specified.<br>";
qWarning() << "Please set the SNIPPY_EDITOR env variable. For example to"
<< "<b>\"kate %1\"</b>";
return;
}
const QString filename = m_snippet->absolutePath();
QString fullCommand = editorCommand;
if (editorCommand.contains(QLatin1String("%1"))) {
fullCommand = fullCommand.arg(filename);
} else {
fullCommand += " " + filename;
}
runCommand(fullCommand);
}
void MainWindow::openFileExplorer(QString path)
{
QFileInfo pathInfo(path);
path = pathInfo.absoluteFilePath(); // Cleanup path, remove double //
QString fileExplorerCommand = m_kernel.externalFileExplorer();
if (fileExplorerCommand.isEmpty()) {
qWarning() << "Please set the SNIPPY_FILE_EXPLORER env variable. For example to"
<< "<b>\"dolphin %1\"</b>";
return;
}
qDebug() << "Opening" << path << "...";
if (fileExplorerCommand.contains(QLatin1String("%1"))) {
fileExplorerCommand = fileExplorerCommand.arg(path);
} else {
fileExplorerCommand += ' ' + path;
}
runCommand(fileExplorerCommand);
}
void MainWindow::openDataFolder()
{
openFileExplorer(m_kernel.model()->snippetDataFolder());
}
void MainWindow::updateFilterBackground(bool isError)
{
if (isError)
m_filterLineEdit->setStyleSheet("QLineEdit { color: red; }");
else
m_filterLineEdit->setStyleSheet(QString());
}
QModelIndex MainWindow::selectedIndex() const
{
const QModelIndexList indexes = m_treeView->selectionModel()->selectedIndexes();
return indexes.isEmpty() ? QModelIndex() : indexes.first();
}