-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
266 lines (201 loc) · 7.4 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
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
qDebug() << "BUG QSortFilterProxyModel::rowCount() is bugged so subclassing QAbstractProxyModel is needed...\n"
"FIX QFileDialog doesn't allow files ann dirs to be selected at the same time...";
createActions();
_tagList = loadPersistance();
_tagListModel = new TagListModelDropCheckable(_tagList);
// Creating the main view
// _tagListWidget displays the tags
_tagListWidget = new TagListWidget;
_tagListWidget->setModel(_tagListModel);
connect(_tagListWidget, SIGNAL(viewFileList(QModelIndexList *)),
this, SLOT(showFileList(QModelIndexList *)));
// _fileListWidget displays tagged files
_fileListWidget = new FileListWidget;
connect(_fileListWidget, SIGNAL(viewTagList()),
this, SLOT(showTagList()));
_stackedWidget->addWidget(_tagListWidget);
_stackedWidget->addWidget(_fileListWidget);
setCentralWidget(_stackedWidget);
// When opening the app the tags are shown
showTagList();
}
void MainWindow::createActions() {
// Creating all actions used across the app
// Storing the actions in a HasMap for convenience
// General Actions (not specific)
QAction *addAction = new QAction(QIcon(":icons/add@2x"), tr("Add"), this);
addAction->setShortcut(QKeySequence::New);
_actions->insert("add", addAction);
QAction *copyAction = new QAction(tr("Copy"), this);
copyAction->setShortcut(QKeySequence::Copy);
_actions->insert("copy", copyAction);
QAction *pasteAction = new QAction(tr("Paste"), this);
pasteAction->setShortcut(QKeySequence::Paste);
_actions->insert("paste", pasteAction);
QAction *removeAction = new QAction(QIcon(":icons/remove@2x"), tr("Remove"), this);
#ifdef Q_OS_MAC
removeAction->setShortcut(QKeySequence(Qt::Key_Backspace));
#else
removeAction->setShortcut(QKeySequence::Delete);
#endif /* Q_OS_MAC */
_actions->insert("remove", removeAction);
QAction *findAction = new QAction(QIcon(":icons/search@2x"), tr("Search"), this);
findAction->setShortcut(QKeySequence::Find);
_actions->insert("find", findAction);
// TagListWidget specific actions
QAction *renameAction = new QAction(tr("Rename"), this);
_actions->insert("rename", renameAction);
QAction *filterAction = new QAction(tr("Filter"), this);
_actions->insert("filter", filterAction);
// FileListWidget specific actions
QAction *previousAction = new QAction(tr("Previous"), this);
previousAction->setShortcut(QKeySequence::Back);
_actions->insert("previous", previousAction);
QAction *openInExplorerAction = new QAction(tr("Open in Finder"), this);
_actions->insert("openInExplorer", openInExplorerAction);
QAction *openAction = new QAction(tr("Open"), this);
openAction->setShortcut(QKeySequence::Open);
_actions->insert("open", openAction);
//Initializing toolbar
setUnifiedTitleAndToolBarOnMac(true);
_toolbar->setMovable(false);
_toolbar->setFloatable(false);
_toolbar->setIconSize(QSize(15,15));
//Adding general actions to the toolbar
_toolbar->addAction(addAction);
_toolbar->addAction(removeAction);
_toolbar->addAction(findAction);
this->addToolBar(_toolbar);
//Initializing menu
#ifdef Q_OS_MAC
_menuBar = new QMenuBar(0);
#else
_menuBar = menuBar();
#endif /* Q_OS_MAC */
//Adding general actions to the menu
_menu->addAction(addAction);
_menu->addAction(copyAction);
_menu->addAction(pasteAction);
_menu->addAction(removeAction);
_menu->addAction(findAction);
_menuBar->addMenu(_menu);
}
TagList *MainWindow::loadPersistance() {
// Load data if available
TagList *tagList = new TagList;
QFile file(_path + "/" + _fileName);
QString data;
if (file.open(QIODevice::ReadOnly))
{
QXmlStreamReader *reader = new QXmlStreamReader(&file);
tagList->fromXML(reader);
file.close();
} else {
tagList->init(); //Load mockup data
qDebug() << "Unable to load the data.";
}
return tagList;
}
void MainWindow::savePersistance(TagList *tagList) {
// Save data
QDir d;
d.mkpath(_path);
QFile file(_path + "/" + _fileName);
if (file.open(QIODevice::WriteOnly))
{
QXmlStreamWriter *writer = new QXmlStreamWriter(&file);
tagList->toXML(writer);
file.close();
delete writer;
} else {
qDebug() << "Unable to save the data.";
}
}
void MainWindow::hideWidget() {
// Disconnecting all actions
QMapIterator<QString, QAction *> i(*_actions);
while (i.hasNext()) {
i.next();
disconnect(i.value(), SIGNAL(triggered()), 0, 0);
}
/* When changing between tags and files
* - removing toolbar actions that are not general
* - removing menu actions that are not general
*/
_toolbar->removeAction(getAction("previous"));
_toolbar->removeAction(getAction("openInExplorer"));
_toolbar->removeAction(getAction("open"));
_toolbar->removeAction(getAction("rename"));
_toolbar->removeAction(getAction("filter"));
_menu->removeAction(getAction("previous"));
_menu->removeAction(getAction("openInExplorer"));
_menu->removeAction(getAction("open"));
_menu->removeAction(getAction("rename"));
_menu->removeAction(getAction("filter"));
}
void MainWindow::showTagList() {
hideWidget();
// Adding TagList specific actions
_menu->addAction(getAction("rename"));
_menu->addAction(getAction("filter"));
_toolbar->addAction(getAction("rename"));
_toolbar->addAction(getAction("filter"));
setWindowTitle("Tagulous");
// Showing tags
_tagListWidget->connectActions(_actions);
_stackedWidget->setCurrentWidget(_tagListWidget);
}
void MainWindow::showFileList(QModelIndexList *indexes) {
hideWidget();
// Adding FileList specific actions
_menu->addAction(getAction("previous"));
_menu->addAction(getAction("open"));
_menu->addAction(getAction("openInExplorer"));
_toolbar->addAction(getAction("previous"));
_toolbar->addAction(getAction("open"));
_toolbar->addAction(getAction("openInExplorer"));
// Creating a model with all the tagged file that we want to show
QString windowTitle;
QList<Tag *> *lst = new QList<Tag *>;
for (QModelIndex index : *indexes) {
lst->append(_tagList->at(index.row()));
if (windowTitle == "") {
windowTitle += _tagList->at(index.row())->getName();
} else {
windowTitle += " - " + _tagList->at(index.row())->getName();
}
}
setWindowTitle(windowTitle);
delete indexes;
FileListModel *m = new FileListModel(lst);
_fileListWidget->setModel(m);
// Showing tagged files
_fileListWidget->connectActions(_actions);
_stackedWidget->setCurrentWidget(_fileListWidget);
}
QAction *MainWindow::getAction(QString key) {
return _actions->value(key);
}
MainWindow::~MainWindow() {
// When quitting the app we save the data to the disk
savePersistance(_tagList);
// Deleting what was allocated
QMapIterator<QString, QAction *> i(*_actions);
while (i.hasNext()) {
i.next();
delete i.value();
}
delete _actions;
delete _toolbar;
delete _menu;
delete _menuBar;
delete _tagListWidget;
delete _fileListWidget;
delete _stackedWidget;
delete _tagListModel;
delete _tagList;
}