-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnavigationview.cpp
More file actions
250 lines (210 loc) · 6.6 KB
/
Copy pathnavigationview.cpp
File metadata and controls
250 lines (210 loc) · 6.6 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
#include "navigationview.h"
NavigationView::NavigationView(QWidget *parent, bool editable):QTreeView(parent)
{
this->setColumnHidden(1,true);
this->setHeaderHidden(true);
this->editable = editable;
this->setContextMenuPolicy(Qt::CustomContextMenu);
addFileAction.setText("Add File");
addFolderAction.setText("Add Folder");
renameFileAction.setText("Rename");
deleteFileAction.setText("Delete");
openLocationAction.setText("Open Location");
copyPath.setText("Copy Path");
SetVault.setText("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);
}
void NavigationView::setRowsEditable(bool enable)
{
this->editable = enable;
}
void NavigationView::expandEveryItems(QModelIndex index)
{
if(!expandTimer.isActive())
expandTimer.start(TIME_PERIOD_FOR_EXPANSION);
}
void NavigationView::keyPressEvent(QKeyEvent *event)
{
QTreeView::keyPressEvent(event);
QModelIndex index = this->currentIndex();
if(!isPersistentEditorOpen(index))
{
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);
}
}
}
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);
}
void NavigationView::rowsInserted(const QModelIndex &parent, int start, int end)
{
QTreeView::rowsInserted(parent,start,end);
if(!newEntryName.isEmpty())
folderChangedHandler();
}
void NavigationView::mouseDoubleClickEvent(QMouseEvent *event)
{
if(editable)
renameFile();
}
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));
}
void NavigationView::addFile()
{
auto 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;
}
void NavigationView::addFolder()
{
auto 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;
}
void NavigationView::renameFile()
{
auto index = this->currentIndex();
editingFilename = index.data().toString();
this->edit(index);
}
void NavigationView::deleteFile()
{
auto indexes = this->selectedIndexes();
for(auto index: indexes){
emit deleteFileFolder(index);
}
}
void NavigationView::openFileFolder()
{
auto index = lastClickedIndex;
emit openLocation(index);
}
void NavigationView::copyFileFolderPath()
{
auto index = lastClickedIndex;
emit copyFolderFilePath(index);
}
void NavigationView::setVaultHandler()
{
emit setVaultPath();
}
void NavigationView::folderChangedHandler()
{
QModelIndex index =lastClickedIndex;
if(!index.isValid()){
index = rootIndex();
}
int totalFiles = model()->rowCount(index);
for( int i = 0 ; i < totalFiles; i++){
QModelIndex fileIndex = model()->index(i,0,index);
if(!fileIndex.isValid())
continue;
QString name = model()->data(fileIndex, Qt::DisplayRole).toString();
if(name == newEntryName){
this->setCurrentIndex(fileIndex);
editingFilename = name;
emit newFileCreated(fileIndex);
this->edit(fileIndex);
newEntryName = "";
return;
}
}
}
void NavigationView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
{
QModelIndex selected = this->currentIndex();
QString finishedEditingFilename = selected.data(Qt::DisplayRole).toString();
QTreeView::closeEditor(editor,hint);
emit newFileCreated(selected);
if(editingFilename != finishedEditingFilename){
emit fileRenamed(finishedEditingFilename, editingFilename, selected);
}
}
void NavigationView::expandTimerHandler()
{
expandTimer.stop();
emit expansionComplete();
}
void NavigationView::rowClicked(const QModelIndex &index)
{
if(this->isExpanded(index))
{
this->collapse(index);
}
else
{
this->expand(index);
}
}