forked from AngryFender/Remini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigationview.cpp
More file actions
344 lines (290 loc) · 11.3 KB
/
Copy pathnavigationview.cpp
File metadata and controls
344 lines (290 loc) · 11.3 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
#include "navigationview.h"
#include <QApplication>
#include <QFileSystemModel>
#include <QLineEdit>
#include <QMenu>
#include <QTimer>
// ---------------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------------
NavigationView::NavigationView(QWidget *parent, bool editable)
: QTreeView(parent), editable(editable)
{
this->setColumnHidden(1, true);
this->setHeaderHidden(true);
this->setContextMenuPolicy(Qt::CustomContextMenu);
addFileAction.setText(QStringLiteral("Add File"));
addFolderAction.setText(QStringLiteral("Add Folder"));
renameFileAction.setText(QStringLiteral("Rename"));
deleteFileAction.setText(QStringLiteral("Delete"));
openLocationAction.setText(QStringLiteral("Open Location"));
copyPath.setText(QStringLiteral("Copy Path"));
SetVault.setText(QStringLiteral("Set Vault Path"));
connect(&addFileAction, &QAction::triggered, this,
&NavigationView::addFile);
connect(&addFolderAction, &QAction::triggered, this,
&NavigationView::addFolder);
connect(&renameFileAction, &QAction::triggered, this,
&NavigationView::renameFile);
connect(&deleteFileAction, &QAction::triggered, this,
&NavigationView::deleteFile);
connect(&openLocationAction, &QAction::triggered, this,
&NavigationView::openFileFolder);
connect(©Path, &QAction::triggered, this,
&NavigationView::copyFileFolderPath);
connect(&SetVault, &QAction::triggered, this,
&NavigationView::setVaultHandler);
connect(this, &NavigationView::customContextMenuRequested, this,
&NavigationView::ContextMenuHandler);
connect(&expandTimer, &QTimer::timeout, this,
&NavigationView::expandTimerHandler);
connect(this, &NavigationView::clicked, this, &NavigationView::rowClicked);
}
// ---------------------------------------------------------------------------
// Enable or disable inline editing
// ---------------------------------------------------------------------------
void NavigationView::setRowsEditable(bool enable) { editable = enable; }
// ---------------------------------------------------------------------------
// Start the delayed expansion timer (used after bulk model changes)
// ---------------------------------------------------------------------------
void NavigationView::expandEveryItems(QModelIndex index)
{
Q_UNUSED(index);
if (!expandTimer.isActive())
expandTimer.start(TIME_PERIOD_FOR_EXPANSION);
}
// ---------------------------------------------------------------------------
// Key handling
// ---------------------------------------------------------------------------
void NavigationView::keyPressEvent(QKeyEvent *event)
{
QTreeView::keyPressEvent(event);
QModelIndex index = this->currentIndex();
if (isPersistentEditorOpen(index))
return;
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
{
emit pressed(index);
if (this->isExpanded(index))
this->collapse(index);
else
this->expand(index);
}
else if (event->key() == Qt::Key_Backspace)
{
emit sendFocusToSearch(this);
}
}
// ---------------------------------------------------------------------------
// Font
// ---------------------------------------------------------------------------
void NavigationView::setFont(const QFont &font)
{
addFileAction.setFont(font);
addFolderAction.setFont(font);
renameFileAction.setFont(font);
deleteFileAction.setFont(font);
openLocationAction.setFont(font);
copyPath.setFont(font);
SetVault.setFont(font);
QTreeView::setFont(font);
}
// ---------------------------------------------------------------------------
// Called after new rows are inserted into the model
// ---------------------------------------------------------------------------
void NavigationView::rowsInserted(const QModelIndex &parent, int start, int end)
{
QTreeView::rowsInserted(parent, start, end);
if (!newEntryName.isEmpty())
folderChangedHandler();
}
// ---------------------------------------------------------------------------
// Mouse double click
// ---------------------------------------------------------------------------
void NavigationView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (editable)
{
renameFile();
event->accept();
return;
}
// Fall back to the default expand/collapse behaviour when not editable.
QTreeView::mouseDoubleClickEvent(event);
}
// ---------------------------------------------------------------------------
// Context menu
// ---------------------------------------------------------------------------
void NavigationView::ContextMenuHandler(QPoint pos)
{
QMenu menu(this);
QModelIndex index = indexAt(pos);
lastClickedIndex = index;
if (!index.isValid())
{
menu.addAction(&addFileAction);
menu.addAction(&addFolderAction);
menu.addAction(&openLocationAction);
menu.addAction(©Path);
menu.addAction(&SetVault);
menu.exec(viewport()->mapToGlobal(pos));
return;
}
NavigationProxyModel *fileModel =
qobject_cast<NavigationProxyModel *>(this->model());
if (!fileModel)
return;
QFileInfo fileInfo = fileModel->getFileInfoMappedToSource(index);
if (fileInfo.isDir())
{
menu.addAction(&addFileAction);
menu.addAction(&addFolderAction);
}
menu.addAction(&renameFileAction);
menu.addAction(&deleteFileAction);
menu.addAction(&openLocationAction);
menu.addAction(©Path);
menu.exec(viewport()->mapToGlobal(pos));
}
// ---------------------------------------------------------------------------
// Add file
// ---------------------------------------------------------------------------
void NavigationView::addFile()
{
QModelIndex index = lastClickedIndex;
this->expand(index);
emit emptySearch();
this->scrollTo(index);
const QList<QModelIndex> selections = this->selectedIndexes();
if (!selections.empty() && lastClickedIndex.isValid())
index = selections.first();
QString fileName;
emit createFile(index, fileName);
lastClickedIndex = index;
newEntryName = fileName;
}
// ---------------------------------------------------------------------------
// Add folder
// ---------------------------------------------------------------------------
void NavigationView::addFolder()
{
QModelIndex index = lastClickedIndex;
this->expand(index);
emit emptySearch();
this->scrollTo(index);
const QList<QModelIndex> selections = this->selectedIndexes();
if (!selections.isEmpty() && lastClickedIndex.isValid())
index = selections.first();
QString folderName;
emit createFolder(index, folderName);
lastClickedIndex = index;
newEntryName = folderName;
}
// ---------------------------------------------------------------------------
// Rename
// ---------------------------------------------------------------------------
void NavigationView::renameFile()
{
const QModelIndex index = this->currentIndex();
if (!index.isValid())
return;
editingFilename = index.data(Qt::DisplayRole).toString();
this->edit(index);
}
// ---------------------------------------------------------------------------
// Delete
// ---------------------------------------------------------------------------
void NavigationView::deleteFile()
{
const QList<QModelIndex> indexes = this->selectedIndexes();
for (const QModelIndex &index : indexes)
emit deleteFileFolder(const_cast<QModelIndex &>(index));
}
// ---------------------------------------------------------------------------
// Open containing location
// ---------------------------------------------------------------------------
void NavigationView::openFileFolder()
{
QModelIndex index = lastClickedIndex;
emit openLocation(index);
}
// ---------------------------------------------------------------------------
// Copy path to clipboard
// ---------------------------------------------------------------------------
void NavigationView::copyFileFolderPath()
{
QModelIndex index = lastClickedIndex;
emit copyFolderFilePath(index);
}
// ---------------------------------------------------------------------------
// Set vault path
// ---------------------------------------------------------------------------
void NavigationView::setVaultHandler() { emit setVaultPath(); }
// ---------------------------------------------------------------------------
// After the model has been updated, look for the newly created entry and
// start inline editing on it. Delayed via QTimer::singleShot(0) so that the
// view has regained focus before edit() is called (important on Wayland).
// ---------------------------------------------------------------------------
void NavigationView::folderChangedHandler()
{
QModelIndex index = lastClickedIndex;
if (!index.isValid())
index = rootIndex();
const int totalFiles = model()->rowCount(index);
for (int i = 0; i < totalFiles; ++i)
{
const QModelIndex fileIndex = model()->index(i, 0, index);
if (!fileIndex.isValid())
continue;
const QString name =
model()->data(fileIndex, Qt::DisplayRole).toString();
if (name != newEntryName)
continue;
this->setCurrentIndex(fileIndex);
this->setFocus();
editingFilename = name;
newEntryName.clear();
emit newFileCreated(fileIndex);
// Defer edit() to the next event-loop iteration so that focus is
// guaranteed to be back on this view. This avoids silent failures
// on Wayland where focus may still be pending.
QTimer::singleShot(0, this,
[this, fileIndex]() { this->edit(fileIndex); });
return;
}
}
// ---------------------------------------------------------------------------
// Close inline editor.
// Read the new value from the editor widget, not from the model (the model
// still holds the old value at this point).
// ---------------------------------------------------------------------------
void NavigationView::closeEditor(QWidget *editor,
QAbstractItemDelegate::EndEditHint hint)
{
QString newName;
if (auto *lineEdit = qobject_cast<QLineEdit *>(editor))
newName = lineEdit->text();
QTreeView::closeEditor(editor, hint);
const QModelIndex selected = this->currentIndex();
emit newFileCreated(selected);
if (!newName.isEmpty() && editingFilename != newName)
emit fileRenamed(newName, editingFilename, selected);
}
// ---------------------------------------------------------------------------
// Expansion timer handler
// ---------------------------------------------------------------------------
void NavigationView::expandTimerHandler()
{
expandTimer.stop();
emit expansionComplete();
}
// ---------------------------------------------------------------------------
// Row clicked -> toggle expand/collapse
// ---------------------------------------------------------------------------
void NavigationView::rowClicked(const QModelIndex &index)
{
if (this->isExpanded(index))
this->collapse(index);
else
this->expand(index);
}