-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
459 lines (372 loc) · 14.5 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "mainwindow.h"
MainWindow::MainWindow() {
mainWidget = new MainWidget(this);
setCentralWidget(mainWidget);
optionsWidget = new OptionsWidget(this, new QSettings());
optionsWidget->hide();
usbConnection = new UsbConnection();
codeHighlighter = new CodeHighlighter(this);
searchwidget = new SearchWidget(this);
modelingWidget = new ModelingWidget();
createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();
setCurrentFile("");
setUnifiedTitleAndToolBarOnMac(true);
connect(mainWidget,
SIGNAL(wantToClose(QTextDocument*, QString)),
this, SLOT(closeFile(QTextDocument*, QString)));
connect(mainWidget,
SIGNAL(currentChanged(int)),
this, SLOT(bindCurrentText(int)));
connect(mainWidget,
SIGNAL(disconnectActionsFrom(CustomEditText*)),
this,
SLOT(disconnectWidget(CustomEditText*)));
connect(searchwidget, SIGNAL(searchString(QString)),
this, SLOT(findInCurrent(QString)) );
connect(searchwidget, SIGNAL(replaceString(QString, QString)),
this, SLOT(replaceInCurrent(QString, QString)));
connect(searchwidget, SIGNAL(replaceAllString(QString, QString)),
this, SLOT(replaceAllInCurrent(QString, QString)));
}
void MainWindow::closeEvent(QCloseEvent *event) {
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
void MainWindow::closeFile(QTextDocument * doc, const QString path) {
statusBar()->showMessage(tr("Пытаюьсь закрыть вкладку"), 2000);
if(maybeSave(doc)) {
mainWidget->closeTabSlot(path);
} else {
statusBar()->showMessage(tr("Не могу закрыть эту вкладку"), 2000);
}
}
void MainWindow::newFile() {
setCurrentFile("");
}
void MainWindow::open() {
if (maybeSave()) {
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::save() {
if (curFile.isEmpty()) {
return saveAs();
} else {
return saveFile(curFile);
}
}
bool MainWindow::saveAs() {
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::about() {
QMessageBox::about(this, tr("About Application"),
tr("The <b>Application</b> is a student programm "
"for work with machine "
"write modern GUI applications using Qt, with a menu bar, "
"toolbars, and a status bar."));
}
void MainWindow::documentWasModified() {
setWindowModified(mainWidget->getTextEdit()->document()->isModified());
}
void MainWindow::createActions() {
newAct = new QAction(tr("&Новый"), this);
newAct->setShortcuts(QKeySequence::New);
//newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(tr("&Открыть..."), this);
openAct->setShortcuts(QKeySequence::Open);
//openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(tr("&Сохранить"), this);
saveAct->setShortcuts(QKeySequence::Save);
//saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Сохранить &Как..."), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
//saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAct = new QAction(tr("В&ыход"), this);
exitAct->setShortcuts(QKeySequence::Quit);
//exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
cutAct = new QAction(tr("Выре&зать"), this);
cutAct->setShortcuts(QKeySequence::Cut);
//cutAction->setStatusTip(tr("Cut the current selection's contents to the "
// "clipboard"));
cutAct->setEnabled(false);
copyAct = new QAction(tr("&Копировать"), this);
copyAct->setShortcuts(QKeySequence::Copy);
//copyAction->setStatusTip(tr("Copy the current selection's contents to the "
// "clipboard"));
copyAct->setEnabled(false);
pasteAct = new QAction(tr("&Вставить"), this);
pasteAct->setShortcuts(QKeySequence::Paste);
//pasteAction->setStatusTip(tr("Paste the clipboard's contents into the current "
// "selection"));
optionsAct = new QAction(tr("&Настройки"), this);
connect(optionsAct, SIGNAL(triggered()), this, SLOT(showOptions()));
findAct = new QAction(tr("&Поиск"), this);
connect(findAct, SIGNAL(triggered()), this, SLOT(find()));
replaceAct = new QAction(tr("&Замена"), this);
connect(replaceAct, SIGNAL(triggered()), this, SLOT(replace()));
compileAction = new QAction(tr("&Компилировать"), this);
connect(compileAction, SIGNAL(triggered()), this, SLOT(compile()));
runAction = new QAction(tr("&Запустить"), this);
connect(runAction, SIGNAL(triggered()), this, SLOT(runCode()));
modelAction = new QAction(tr("&Моделировать"), this);
connect(modelAction, SIGNAL(triggered()), this, SLOT(onModeling()));
manualCtrlAction = new QAction(tr("Ручсное &управление"), this);
connect(manualCtrlAction, SIGNAL(triggered()), this, SLOT(manualCtrl()));
machineOffAction = new QAction(tr("&Выключить машину"), this);
connect(machineOffAction, SIGNAL(triggered()), this, SLOT(machineOff()));
}
void MainWindow::createMenus() {
QMenuBar * bar = menuBar();
fileMenu = bar->addMenu(tr("&Файл"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = bar->addMenu(tr("&Правка"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(findAct);
editMenu->addAction(replaceAct);
compilationMenu = bar->addMenu(tr("&Компиляция"));
compilationMenu->addAction(compileAction);
compilationMenu->addAction(runAction);
compilationMenu->addAction(modelAction);
optionsMenu = bar->addMenu(tr("&Настройки"));
optionsMenu->addAction(optionsAct);
toolsMenu = bar->addMenu(tr("&Оборудование"));
toolsMenu->addAction(manualCtrlAction);
toolsMenu->addAction(machineOffAction);
bar->addSeparator();
helpMenu = bar->addMenu(tr("&Помощь"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createToolBars() {
fileToolBar = addToolBar(tr("Файл"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Правка"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
}
void MainWindow::createStatusBar() {
statusBar()->showMessage(tr("Готов"));
}
void MainWindow::readSettings() {
QSettings settings("QtProject", "Application Example");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
resize(size);
move(pos);
}
void MainWindow::writeSettings() {
QSettings settings("QtProject", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());
}
bool MainWindow::maybeSave() {
return maybeSave(mainWidget->getTextEdit()->document());
}
bool MainWindow::maybeSave(QTextDocument * doc) {
if (doc->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Application"),
tr("Документ был изменён.\n"
"Вы хотите сохранить изменения?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return save();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}
void MainWindow::loadFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Не могу открыть файл %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QTextStream in(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
setCurrentFile(fileName, in.readAll());
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
statusBar()->showMessage(tr("Файл загружен"), 2000);
}
bool MainWindow::saveFile(const QString &fileName) {
return saveDocument(mainWidget->getTextEdit()->document(),
&fileName);
}
bool MainWindow::saveDocument(QTextDocument *doc, const QString * path){
QFile file(*path);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Не могу открыть файл %1:\n%2.")
.arg(*path)
.arg(file.errorString()));
return false;
}
QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << doc->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
doc->setModified(false);
//setCurrentFile(*path);
statusBar()->showMessage(tr("Файл сохранен в ") + *path, 2000);
return true;
}
void MainWindow::bindCurrentText(int i){
bindTextActions();
}
void MainWindow::bindTextActions(){
CustomEditText* current = mainWidget->getTextEdit();
QAction* cut = current->getCutAction();
QAction* copy = current->getCopyAction();
QAction* paste = current->getPasteAction();
connect(cutAct, SIGNAL(triggered()), cut, SIGNAL(triggered()));
connect(copyAct, SIGNAL(triggered()), copy, SIGNAL(triggered()));
connect(pasteAct, SIGNAL(triggered()), paste, SIGNAL(triggered()));
connect(current, SIGNAL(copyAvailable(bool)),
cutAct, SLOT(setEnabled(bool)));
connect(current, SIGNAL(copyAvailable(bool)),
copyAct, SLOT(setEnabled(bool)));
cutAct->setEnabled(cut->isEnabled());
copyAct->setEnabled(copy->isEnabled());
connect(current->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));
codeHighlighter->setDocument(current->document());
}
void MainWindow::setCurrentFile(const QString &fileName) {
QString text("");
setCurrentFile(fileName, text);
}
void MainWindow::setCurrentFile(const QString &fileName, const QString &text) {
curFile = fileName;
QString shownName = curFile;
if (curFile.isEmpty())
shownName = "untitled.txt";
mainWidget->newTab(shownName, text)->document()->setModified(false);
setWindowModified(false);
setWindowFilePath(shownName);
bindTextActions();
}
QString MainWindow::strippedName(const QString &fullFileName) {
return QFileInfo(fullFileName).fileName();
}
void MainWindow::disconnectWidget(CustomEditText* textEdit) {
QAction* cut = textEdit->getCutAction();
QAction* copy = textEdit->getCopyAction();
QAction* paste = textEdit->getPasteAction();
disconnect(cutAct, SIGNAL(triggered()), cut, SIGNAL(triggered()));
disconnect(copyAct, SIGNAL(triggered()), copy, SIGNAL(triggered()));
disconnect(pasteAct, SIGNAL(triggered()), paste, SIGNAL(triggered()));
disconnect(textEdit, SIGNAL(copyAvailable(bool)),
cutAct, SLOT(setEnabled(bool)));
disconnect(textEdit, SIGNAL(copyAvailable(bool)),
copyAct, SLOT(setEnabled(bool)));
disconnect(textEdit->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));
}
void MainWindow::showOptions(){
optionsWidget->show();
}
void MainWindow::find(){
this->searchwidget->show();
}
void MainWindow::replace(){
this->searchwidget->show();
}
void MainWindow::replaceAll(){
this->searchwidget->show();
}
void MainWindow::manualCtrl(){
}
void MainWindow::machineOff(){
}
void MainWindow::compile() {
compilator = new Compilator(this->mainWidget->getCurrentPath().toStdString().c_str());
compilator->compile();
//this->thread()->currentThread()->sleep(5);
statusBar()->showMessage(tr("Компилирую"), 2000);
}
void MainWindow::runCode() {
compile();
onFinishCompilation();
}
void MainWindow::onFinishCompilation() {
mainWidget->clearTables();
lines_t *lines = compilator->getLines();
foreach(line_t* i, *lines){
mainWidget->addLine(i);
}
statusBar()->showMessage(tr("Компилированы %1").arg(compilator->getLines()->size()), 2000);
}
void MainWindow::onModeling() {
compile();
lines_t *lines = compilator->getLines();
statusBar()->showMessage(tr("Компилированы %1").arg(lines->size()), 2000);
usbConnection->print_info();
if (lines->size() != 0) {
modelingWidget->plotLine(lines);
modelingWidget->show();
}
}
void MainWindow::findInCurrent(QString regexp){
CustomEditText* textEdit = mainWidget->getTextEdit();
textEdit->find(regexp);
textEdit->show();
}
void MainWindow::replaceInCurrent(QString regexp, QString text){
CustomEditText* textEdit = mainWidget->getTextEdit();
textEdit->insertPlainText(text);
textEdit->find(regexp);
textEdit->show();
}
void MainWindow::replaceAllInCurrent(QString regexp, QString text){
CustomEditText* textEdit = mainWidget->getTextEdit();
while(textEdit->find(regexp)) {
textEdit->insertPlainText(text);
};
textEdit->show();
}