-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
391 lines (305 loc) · 9.83 KB
/
mainwindow.cpp
File metadata and controls
391 lines (305 loc) · 9.83 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSettings>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tabWidget = new EditorTabsWidget(this);
ui->centralWidget->layout()->addWidget(tabWidget);
QApplication::setApplicationName("PlistED");
QApplication::setOrganizationName("Alpex");
undoGroup = new QUndoGroup(this);
// create undo and redo actions
QAction *actionUndo = undoGroup->createUndoAction(this, tr("Undo"));
QAction *actionRedo = undoGroup->createRedoAction(this, tr("Redo"));
// set shortcuts
actionUndo->setShortcuts(QKeySequence::Undo);
actionRedo->setShortcuts(QKeySequence::Redo);
// add actions to menu
ui->menuEdit->addAction(actionUndo);
ui->menuEdit->addAction(actionRedo);
connect(tabWidget, SIGNAL(currentChanged(int)),
this, SLOT(tabWidget_currentChanged(int)));
connect(undoGroup, SIGNAL(cleanChanged(bool)),
this, SLOT(onCleanChanged(bool)));
connect(ui->actionFile1, SIGNAL(triggered()), this, SLOT(openRecentFile()));
connect(ui->actionFile2, SIGNAL(triggered()), this, SLOT(openRecentFile()));
connect(ui->actionFile3, SIGNAL(triggered()), this, SLOT(openRecentFile()));
connect(ui->actionFile4, SIGNAL(triggered()), this, SLOT(openRecentFile()));
connect(ui->actionFile5, SIGNAL(triggered()), this, SLOT(openRecentFile()));
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(onTabCloseRequest(int)));
updateRecentFiles();
}
MainWindow::~MainWindow()
{
delete undoGroup;
delete ui;
}
void MainWindow::on_actionNew_activated()
{
// create new model
DomModel *model = new DomModel();
// add root
DomItem *i = model->getRoot()->addChild();
// set root data
i->setName("plist");
i->setType("dict");
// create tab with model
tabWidget->createTab(model);
}
void MainWindow::on_actionOpen_activated()
{
openFiles();
}
void MainWindow::on_actionClose_activated()
{
if (tabWidget->hasTabs()) onTabCloseRequest();
}
void MainWindow::on_actionClose_all_activated()
{
if (tabWidget->hasTabs()) tabWidget->closeAllTabs();
}
void MainWindow::openFiles(QStringList list)
{
if (list.isEmpty())
{
list = QFileDialog::getOpenFileNames(
this, tr("Select files to open"),
"", "Property list (*.plist)");
}
for (int i = 0; i < list.size(); ++i) openPlist(list[i]);
}
void MainWindow::openPlist(QString filePath)
{
if (!filePath.isEmpty())
{
QFile file(filePath);
if (file.open(QIODevice::ReadOnly))
{
QDomDocument document;
if (document.setContent(&file))
{
qDebug() << QString("File %1 opened").arg(filePath);
DomModel *model = DomParser::fromDom(document);
tabWidget->createTab(model, filePath);
}
file.close();
}
setRecentFiles(filePath);
updateRecentFiles();
}
}
void MainWindow::onTabCloseRequest(int i)
{
if (!undoGroup->isClean())
{
// make tab active
if (i != -1) tabWidget->setCurrentIndex(i);
// messageobox for save
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Question);
msgBox.setText(tr("The document has been modified."));
msgBox.setInformativeText(tr("Do you want to save your changes?"));
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Cancel:
// Cancel was clicked
return;
case QMessageBox::Save:
on_actionSave_activated();
break;
case QMessageBox::Discard:
// Don't Save was clicked
break;
}
}
// get current stack
QUndoStack *stack = tabWidget->getCurentTab()->getUndoStack();
// remove stack from group
undoGroup->removeStack(stack);
// close tab
tabWidget->closeTab();
}
void MainWindow::savePlist(QString filePath)
{
if (tabWidget->hasTabs())
{
EditorTab *tab = tabWidget->getCurentTab();
// get parsed dom doc
QDomDocument doc = DomParser::toDom(tab->getModel());
// create and open file
QFile file(filePath);
file.open(QIODevice::WriteOnly);
// create txt stream with file
QTextStream out(&file);
// write to file
doc.save(out, 4);
// close file
file.close();
// set new name
tab->setPath(filePath);
//get tab index
int index = tabWidget->indexOf(tab);
// get name
QString name = tab->getFileName();
// set text
tabWidget->setTabText(index, name);
// set stack clean
undoGroup->activeStack()->clear();
//undoGroup->activeStack()->setClean();
}
}
void MainWindow::on_actionSave_activated()
{
if (tabWidget->hasTabs())
{
EditorTab *tab = tabWidget->getCurentTab();
QString path = tab->getPath();
if (!path.isEmpty()) savePlist(path);
else on_actionSave_as_activated();
}
}
void MainWindow::on_actionSave_as_activated()
{
if (tabWidget->hasTabs())
{
QString str = QFileDialog::getSaveFileName(
this, tr("Save as"), "", tr("Property list (*.plist)"));
if (!str.isEmpty()) savePlist(str);
}
}
void MainWindow::on_actionAdd_activated()
{
if (tabWidget->hasTabs())
{
EditorTab *tab = tabWidget->getCurentTab();
const QModelIndex index = tab->currentIndex();
//if (index.isValid()) tab->getModel()->addItem(index);
if (index.isValid())
{
QUndoCommand *addCommand = new AddCommand(tab->getModel(), index);
undoGroup->activeStack()->push(addCommand);
}
}
}
void MainWindow::on_actionRemove_activated()
{
if (tabWidget->hasTabs())
{
EditorTab *tab = tabWidget->getCurentTab();
const QModelIndex index = tab->currentIndex();
//if (index.isValid()) tab->getModel()->removeItem(index);
if (index.isValid())
{
DomModel *model = tab->getModel();
if (model->itemNotPlist(index))
{
QUndoCommand *removeCommand = new RemoveCommand(model, index);
undoGroup->activeStack()->push(removeCommand);
}
}
}
}
void MainWindow::on_actionExpand_all_activated()
{
if (tabWidget->hasTabs())
{
EditorTab *tab = tabWidget->getCurentTab();
tab->expand();
setExpandText(tab);
}
}
void MainWindow::on_actionAbout_activated()
{
QMessageBox::about(this, tr("About PlistED"),
"PlistED Copyright 2012 Yaroslav Sushkov");
}
void MainWindow::tabWidget_currentChanged(int index)
{
if (tabWidget->hasTabs())
{
// get tab widget
EditorTab *tab = tabWidget->getCurentTab();
setExpandText(tab);
// set window title to filename
this->setWindowFilePath(tabWidget->tabText(tabWidget->indexOf(tab)));
// get undo stack
QUndoStack *stack = tab->getUndoStack();
// set active stack
if (!undoGroup->stacks().contains(stack)) undoGroup->addStack(stack);
undoGroup->setActiveStack(stack);
}
//?
else this->setWindowFilePath(" ");
}
void MainWindow::onCleanChanged(bool clean)
{
this->setWindowModified(!clean);
}
void MainWindow::setRecentFiles(const QString &fileName)
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
// remove all inclusions
files.removeAll(fileName);
// add to begining
files.prepend(fileName);
while (files.size() > MaxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
}
void MainWindow::updateRecentFiles()
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
for (int i = 0; i < numRecentFiles; ++i)
{
QAction *action = ui->menuRecent_files->actions().at(i);
QString text = files.at(i);
action->setText(text);
action->setVisible(true);
}
}
void MainWindow::openRecentFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action) openPlist(action->text());
}
void MainWindow::setExpandText(EditorTab *tab)
{
QString text = (!tab->isExpanded()) ? tr("Expand all") : tr("Collapse all");
ui->actionExpand_all->setIconText(text);
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData* mimeData = event->mimeData();
if (mimeData->hasFormat("text/uri-list")) event->accept();
// application/xml application/x-plist
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty()) return;
QStringList list;
for (int i = 0; i < urls.size(); ++i)
{
list.append(urls.at(i).toLocalFile());
}
openFiles(list);
}
void MainWindow::on_menu_aboutToShow()
{
bool enabled = tabWidget->hasTabs();
ui->actionClose->setEnabled(enabled);
ui->actionClose_all->setEnabled(enabled);
ui->actionSave->setEnabled(enabled);
ui->actionSave_as->setEnabled(enabled);
}