This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
executable file
·384 lines (314 loc) · 9.51 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
/*****************************************************************************
* Copyright © 2015 Oakware - All Rights Reserved
*
* Github : <https://github.com/Oakware>
* Authors: Ivan Kondratuk <i.o.kondratuk@gmail.com>
*
* This file is part of Graph Builder.
*
* Graph Builder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graph Builder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graph Builder. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "GraphAlgorithms/graph_algorithms.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
MW_MAIN_DATA_INIT
// Setup ui
ui->setupUi(this);
MainLayot = new QHBoxLayout;
ui->centralWidget->setLayout(MainLayot);
GraphV = new GraphView;
GraphV->SetGraph(&Graph);
MainLayot->addWidget(GraphV);
// Connections
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(Open()));
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(Save()));
connect(ui->actionSave_As, SIGNAL(triggered()), this, SLOT(SaveAs()));
connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
// Graph changes
connect(GraphV, SIGNAL(VertexSelected()), this, SLOT(UpdateVerInfo()));
connect(GraphV, SIGNAL(RibSelected()), this, SLOT(UpdateRibInfo()));
connect(&Graph, SIGNAL(Changed()), this, SLOT(GraphChanged()));
// Options load
Options.Load();
UpdateOptions();
restoreGeometry(Options.Geometry);
if(!Options.LastFile.isEmpty()){
if(!Open(Options.LastFile))
{SetSaveFile("");}
}
// Dock widgets init
UpdateVerInfo();
UpdateRibInfo();
}
MainWindow::~MainWindow(){delete ui;}
void MainWindow::closeEvent(QCloseEvent *ev){
if(GrChanged){
int s = QMessageBox::information(this, tr("Close"),
tr("Do you want to save graph?"),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
if(s == QMessageBox::Yes)
{Save();}
else if(s == QMessageBox::Cancel)
{ev->ignore(); return;}
}
Options.Geometry = saveGeometry();
Options.Save();
Options.GrAlgoDelay = 0; // For fast algo stop
}
void MainWindow::changeEvent(QEvent *ev){
if(ev->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
ResetTitle();
}else
{QMainWindow::changeEvent(ev);}
}
void MainWindow::UpdateOptions(){
GraphV->SetEditable(Options.Editable);
GraphV->SetStdVertex(TvertexInfo(Options.VertexRadius,
Options.VertexColor));
GraphV->SetStdRib(TribInfo(Options.RibWeight));
ui->actionDock_visible->setChecked(Options.VDWVisible);
on_actionDock_visible_toggled(Options.VDWVisible);
ui->actionRib_info->setChecked(Options.RDWVisible);
on_actionRib_info_toggled(Options.RDWVisible);
SetAlgoSelected(Options.GrAlgoSelected);
ResetTitle();
}
void MainWindow::SetSaveFile(const QString &FileName){
Options.LastFile = FileName;
Options.Save();
ResetTitle();
}
void MainWindow::ResetTitle(){
QString Title;
if(Options.LastFile.isEmpty()){
Title = tr("Graph builder");
}else{
QFileInfo FI(Options.LastFile);
Title = FI.fileName();
}
if(GrChanged){Title += '*';}
setWindowTitle(Title);
}
void MainWindow::GraphChanged(){
GrChanged = true;
ResetTitle();
}
void MainWindow::UpdateVerInfo(){
int v = GraphV->GetSelectedVertex();
if(!v){
ui->dockWidgetContents->setEnabled(false);
ui->VNumberLineEdit->setText("");
ui->VXLineEdit->setText("");
ui->VYLineEdit->setText("");
ui->VDegreeLineEdit->setText("");
ui->VRadiusSpinBox->setValue(0);
ui->VColorLineEdit->setText("");
}else{
ui->dockWidgetContents->setEnabled(true);
TvertexInfo vi = Graph.GetVertexInfo(v);
ui->VNumberLineEdit->setText(QString::number(vi.number));
ui->VXLineEdit->setText(QString::number(vi.x));
ui->VYLineEdit->setText(QString::number(vi.y));
ui->VDegreeLineEdit->setText(QString::number(vi.degree));
ui->VRadiusSpinBox->setValue(vi.radius);
ui->VColorLineEdit->setText(vi.color.name());
}
}
void MainWindow::on_actionDock_visible_toggled(bool arg1){
ui->VertexDockWidget->setVisible(arg1);
Options.VDWVisible = arg1;
Options.Save();
}
void MainWindow::on_VRadiusSpinBox_valueChanged(int arg1){
int v = GraphV->GetSelectedVertex();
if(!v){return;}
TvertexInfo vi = Graph.GetVertexInfo(v);
if(vi.radius == arg1){return;}
int oldR = vi.radius;
vi.radius = arg1;
if(!Graph.UpdateVertex(v, vi)){
ui->VRadiusSpinBox->setValue(oldR);
}
}
void MainWindow::on_VColorToolButton_clicked(){
int v = GraphV->GetSelectedVertex();
if(!v){return;}
TvertexInfo vi = Graph.GetVertexInfo(v);
QColor col = QColorDialog::getColor(vi.color, this, tr("Vertex color"));
if(!col.isValid()){return;}
vi.color = col; Graph.UpdateVertex(v, vi);
ui->VColorLineEdit->setText(vi.color.name());
}
void MainWindow::UpdateRibInfo(){
ABrib r = GraphV->GetSelectedRib();
if(!r.from){
ui->dockWidgetContents_2->setEnabled(false);
ui->RFromLineEdit->setText("");
ui->RToLineEdit->setText("");
ui->RWeightSpinBox->setValue(0);
ui->RColorLineEdit->setText("");
}else{
ui->dockWidgetContents_2->setEnabled(true);
TribInfo ri = Graph.GetRibInfo(r);
ui->RFromLineEdit->setText(QString::number(ri.from));
ui->RToLineEdit->setText(QString::number(ri.to));
ui->RWeightSpinBox->setValue(ri.weight);
ui->RColorLineEdit->setText(ri.color.name());
}
}
void MainWindow::on_actionRib_info_toggled(bool arg1){
ui->RibDockWidget->setVisible(arg1);
Options.RDWVisible = arg1;
Options.Save();
}
void MainWindow::on_RWeightSpinBox_valueChanged(int arg1){
ABrib r = GraphV->GetSelectedRib();
if(!r.from){return;}
TribInfo ri = Graph.GetRibInfo(r);
if(ri.weight == arg1){return;}
ri.weight = arg1;
Graph.UpdateRib(r, ri);
}
void MainWindow::on_RColorToolButton_clicked(){
ABrib r = GraphV->GetSelectedRib();
if(!r.from){return;}
TribInfo ri = Graph.GetRibInfo(r);
QColor col = QColorDialog::getColor(ri.color, this, tr("Rib color"));
if(!col.isValid()){return;}
ri.color = col;
Graph.UpdateRib(r, ri);
ui->RColorLineEdit->setText(ri.color.name());
}
void MainWindow::on_actionRun_algorithm_triggered(){
GraphV->SetEditable(false); // Set disabled
ui->menuBar->setEnabled(false);
AbstractGraph *NG = new AbstractGraph;
NG->SetGraphModel(Graph.GetGraphModel());
GraphV->SetGraph(NG);
RunGraphAlgorithm(Options.GrAlgoSelected, NG, Options.GrAlgoDelay);
GraphV->SetGraph(&Graph); // Set enabled
delete NG;
GraphV->SetEditable(Options.Editable);
ui->menuBar->setEnabled(true);
}
#define X(A) \
void MainWindow::on_action##A##_toggled(bool arg1){\
if(arg1 && Options.GrAlgoSelected != A##GA){\
SetAlgoSelected(A##GA);\
}\
}
GR_ALGO_LIST
#undef X
void MainWindow::SetAlgoSelected(int algo){
Options.GrAlgoSelected = algo;
Options.Save();
ResetSelected();
#define X(A) if(algo == A##GA){ui->action##A->setChecked(true);}
GR_ALGO_LIST
#undef X
}
void MainWindow::ResetSelected(){
#define X(A) ui->action##A->setChecked(false);
GR_ALGO_LIST
#undef X
}
bool MainWindow::Open(QString FileName){
// Get open name
if(FileName.isEmpty()){
FileName = QFileDialog::getOpenFileName(this, tr("Open graph"),
QDir::currentPath(), "Graph file (*.graph)");
if(FileName.isEmpty()){return false;}
}
// If cant open file
QFile file(FileName);
if(!file.open(QFile::ReadOnly)){
QMessageBox::warning(this, tr("Error"), tr("Cant open file %1:\n%2")
.arg(FileName, file.errorString()));
return false;
}
// Read
GraphModel gr;
QDataStream io(&file);
io >> gr;
Graph.SetGraphModel(gr);
file.close();
GrChanged = false;
SetSaveFile(FileName);
UpdateVerInfo(); UpdateRibInfo();
ui->actionOriented->setChecked(Graph.IsOriented());
return true;
}
bool MainWindow::Save(){
// Get save name
if(Options.LastFile.isEmpty()){
QString FileName;
FileName = QFileDialog::getSaveFileName(this, tr("Save graph"),
QDir::currentPath(), "Graph file (*.graph)");
if(FileName.isEmpty()){return false;}
// If type not set
QFileInfo FI(FileName);
if(FI.suffix() != "graph"){FileName += ".graph";}
SetSaveFile(FileName);
}
// If cant open file
QSaveFile file(Options.LastFile);
if(!file.open(QFile::WriteOnly)){
QMessageBox::warning(this, tr("Error"), tr("Cant save file %1:\n%2")
.arg(Options.LastFile, file.errorString()));
return false;
}
// Write
GraphModel gr = Graph.GetGraphModel();
QDataStream io(&file);
io << gr;
if(!file.commit()){
QMessageBox::warning(this, tr("Error"), tr("Cant save file %1:\n%2")
.arg(Options.LastFile, file.errorString()));
return false;
}
GrChanged = false;
ResetTitle();
return true;
}
bool MainWindow::SaveAs(QString FileName){
// Save last file name
QString TmpFileName = Options.LastFile;
Options.LastFile = FileName;
// Try to save
if(!Save()){
SetSaveFile(TmpFileName);
return false;
}
return true;
}
void MainWindow::on_actionOriented_toggled(bool arg1){
Graph.SetOriented(arg1);
}
void MainWindow::on_actionClear_triggered(){
Graph.Clear();
}
void MainWindow::on_actionOptions_triggered(){
OptionDialog opts(&Options, this);
opts.exec();
UpdateOptions();
}
void MainWindow::on_actionAbout_triggered(){
AboutDialog about(this);
about.exec();
}