-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
342 lines (282 loc) · 10.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include "widgetcontrol.h"
#include "logiccontrol.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_contentChanged(false)
{
// setup ui
ui->setupUi(this);
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveFile()));
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveFileAs()));
connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(closeFile()));
connect(ui->actionExport, SIGNAL(triggered()), this, SLOT(exportScene()));
connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(quit()));
//zoom in zoom out
connect(ui->actionZoom_In, SIGNAL(triggered()), this, SLOT(ZoomIn()));
connect(ui->actionZoom_Out, SIGNAL(triggered()), this, SLOT(ZoomOut()));
//help information
connect(ui->actionHelp_Information, SIGNAL(triggered()), this, SLOT(help()));
// graphwidget is hided by def, new/open file will show it
m_graphicsView = new WidgetControl(this);
setCentralWidget(m_graphicsView);
m_graphicsView->hide();
// Connect controller to detect content change
connect(m_graphicsView->logicControl(), SIGNAL(contentChanged(const bool&)),
this, SLOT(contentChanged(const bool&)));
// Show notification message on Status Bar
connect(m_graphicsView, SIGNAL(notification(QString)),
this, SLOT(statusBarMsg(QString)));
connect(m_graphicsView->logicControl(), SIGNAL(notification(QString)),
this, SLOT(statusBarMsg(QString)));
// setup toolbars, don't show them
setupEditToolbar();
ui->undoToolBar->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::statusBarMsg(const QString &msg) // msg for readonly file and two of toolbars
{
ui->statusBar->showMessage(msg, 5000);
}
void MainWindow::contentChanged(const bool& changed)
//changed,true是改变动作发生了,m_contentchanged此时还没有变化。然后改变m_。并+*给文件名。
//另一种情况是m_changed完成后,changed被设定为false,此时消除*并改变文件状态
{
// only care about the transitions
if (m_contentChanged == false && changed == true)
{
setWindowTitle(windowTitle().prepend("* "));
m_contentChanged = true;
QFileInfo fileInfo(m_filename);
if (m_filename != tr("untitled") && fileInfo.isWritable())
ui->actionSave->setEnabled(true);
}
else if (m_contentChanged == true && changed == false)
{
setWindowTitle(windowTitle().replace("*",NULL));
m_contentChanged = false;
ui->actionSave->setEnabled(false);
}
}
void MainWindow::newFile() //open a new widget, set the options' states. give a initial name
{
if (!closeFile())
return;
m_graphicsView->newScene();
ui->actionSave->setEnabled(false);
ui->actionSave_as->setEnabled(true);
ui->actionClose->setEnabled(true);
ui->actionExport->setEnabled(true);
contentChanged(false);
m_filename = tr("untitled");
setTitle(m_filename);
m_graphicsView->setFocus();
}
void MainWindow::openFile(const QString &fileName) //use QfileDialog to get the path, , acceptmode to open.
{
if (!closeFile())
return;
QString currFilename(m_filename);
if (fileName.isEmpty())
{
QFileDialog *filedialog = new QFileDialog(this);
filedialog->setWindowTitle(tr("Open BrainLine"));
filedialog->setDirectory("");
filedialog->setNameFilter(tr("BrainLine (*bl)"));
filedialog->setAcceptMode(QFileDialog::AcceptOpen);
filedialog->setDefaultSuffix("bl");
if (!filedialog->exec())
return;
m_filename = filedialog->selectedFiles().first();
}
else
{
m_filename = fileName;
}
QFileInfo fileInfo(m_filename);
if (!fileInfo.isWritable())
statusBarMsg(tr("Read-only file!"));
if (!m_graphicsView->logicControl()->readContentFromXmlFile(m_filename)) //this is to read the content in the file
{
m_filename = currFilename;
return;
}
ui->actionSave_as->setEnabled(true); //set its states, mainly for save option and contentChanged()
ui->actionClose->setEnabled(true);
ui->actionExport->setEnabled(true);
ui->actionSave->setEnabled(false);
ui->actionSave->setEnabled(false);
contentChanged(false);
fileInfo.isWritable() ?
setTitle(m_filename) :
setTitle(tr("readonly ").append(m_filename));
}
void MainWindow::saveFile(const bool &checkIfReadonly) //use bool input and writable to decide save or not
{
QFileInfo fileInfo(m_filename);
if (checkIfReadonly && !fileInfo.isWritable())
{
statusBarMsg(tr("Read-only file!"));
return;
}
m_graphicsView->logicControl()->writeContentToXmlFile(m_filename); //the path to save
contentChanged(false);
m_undoStack->clear();
}
bool MainWindow::saveFileAs() //the same logic to find, different in acceptmode
{
QFileDialog *filedialog = new QFileDialog(this);
filedialog->setWindowTitle(tr("Save BrainLine"));
filedialog->setDirectory("");
filedialog->setNameFilter(tr("BrainLine (*.bl)"));
filedialog->setAcceptMode(QFileDialog::AcceptSave);
filedialog->setDefaultSuffix("bl");
if (!filedialog->exec())
return false;
m_filename = filedialog->selectedFiles().first();
saveFile(false);
setTitle(m_filename);
return true;
}
bool MainWindow::closeFile() //give a msgbox to choose, switch cases to go on
{
if (m_contentChanged)
{
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Save brainline - BrainLine"));
msgBox.setText(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::Save:
{
if (m_filename == tr("untitled"))
{
if (!saveFileAs())
return false;
}
else
{
saveFile();
}
break;
}
case QMessageBox::Discard:
break;
case QMessageBox::Cancel:
return false;
default:
break;
}
}
ui->actionSave->setEnabled(false);
ui->actionSave_as->setEnabled(false);
ui->actionClose->setEnabled(false);
ui->actionExport->setEnabled(false);
m_contentChanged = false;
setTitle("");
m_graphicsView->closeScene();
m_undoStack->clear();
showUndoToolbar(false);
return true;
}
void MainWindow::exportScene() // get path
{
QFileDialog *picdialog = new QFileDialog(this);
picdialog->setWindowTitle(tr("Export as a picture"));
picdialog->setDirectory("");
picdialog->setNameFilter(tr("Picture (*.png)"));
picdialog->setAcceptMode(QFileDialog::AcceptSave);
picdialog->setDefaultSuffix("png");
if (picdialog->exec())
{
m_graphicsView->logicControl()->writeContentToPngFile(
picdialog->selectedFiles().first());
}
}
void MainWindow::ZoomIn(){
m_graphicsView->zoomIn();
// std::cout << "Zoom In" << std::endl;
}
void MainWindow::ZoomOut(){
m_graphicsView->zoomOut();
// std::cout << "Zoom out" << std::endl;
}
void MainWindow::help(){
/* New a QMessageBox to display the help information */
QMessageBox *msgBox = new QMessageBox();
// msgBox = new QMessageBox();
msgBox->setWindowTitle(tr("About BrainLine"));
msgBox->setText(tr("BrainLIne software written in Qt.<br><br>").
append(tr("Report bugs to:<br>")).
append(" <a href=\"mailto:zixuanyao@link.cuhk.edu.cn\">" "zixuanyao@link.cuhk.edu.cn</a>"));
QPixmap pixmap(":/image/brainline.svg");
msgBox->setIconPixmap(pixmap.scaled(85,85));
// msgBox->setIcon(QMessageBox::NoIcon);
msgBox->exec();
}
//void MainWindow::help() // give msgbox for help information
//{
// QMessageBox msgBox;
// msgBox.setWindowTitle(tr("About BrainLine"));
// msgBox.setText(tr("BrainLine software written in Qt."));
// msgBox.setTextFormat(Qt::RichText);
// msgBox.setInformativeText(tr("Report bugs to:").append(" <a href=\"mailto:zixuanyao@link.cuhk.edu.cn\">" "zixuanyao@link.cuhk.edu.cn</a>"));
// QPixmap pixMap(":/brainline.svg");
// msgBox.setIconPixmap(pixMap.scaled(50,50));
// msgBox.exec();
//}
void MainWindow::showUndoToolbar(const bool &show)
{
ui->undoToolBar->setVisible(show ? !ui->undoToolBar->isVisible() : false);
}
void MainWindow::quit() //quit program without request
{
// if (m_contentChanged && !closeFile())
// return;
QApplication::instance()->quit();
}
void MainWindow::closeEvent(QCloseEvent * event) //press the x 关闭button and will jump out the closefile function
{
m_contentChanged && !closeFile() ?
event->ignore() :
event->accept();
}
void MainWindow::setupEditToolbar()
{
m_undoStack = new QUndoStack(this);
m_undoView = new QUndoView(m_undoStack,this);
ui->undoToolBar->addWidget(m_undoView);
m_undo = m_undoStack->createUndoAction(this, tr("&Undo"));
m_undo->setShortcuts(QKeySequence::Undo);
m_redo = m_undoStack->createRedoAction(this, tr("&Redo"));
m_redo->setShortcuts(QKeySequence::Redo);
ui->menuTool->addAction(m_undo);
ui->menuTool->addAction(m_redo);
ui->menuTool->addSeparator();
m_undoToolbar = new QAction(tr("undo toolbar"), this);
m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
connect(m_undoToolbar, SIGNAL(triggered()),
this, SLOT (showUndoToolbar()));
ui->menuTool->addAction(m_undoToolbar);
m_graphicsView->logicControl()->setUndoStack(m_undoStack);
}
void MainWindow::setTitle(const QString &title) //clear the old path if there is no title
{
title.isEmpty() ?
setWindowTitle("BrainLine") :
setWindowTitle(QString(title).append(" - BrainLine"));
}