forked from FLAME-HPC/flame_visualiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
1673 lines (1516 loc) · 60.6 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* \file mainwindow.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of main window
*/
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QColorDialog>
#include <QtGui/QMouseEvent>
#include <QTextEdit>
#include <QDir>
#include <QDesktopServices>
#include <QUrl>
#include <math.h>
#include "./mainwindow.h"
#include "./ui_mainwindow.h"
#include "./zeroxmlreader.h"
#include "./visualsettingsmodel.h"
#include "./visualsettingsitem.h"
#include "./configxmlreader.h"
#include "./agenttypedelegate.h"
#include "./shapedelegate.h"
#include "./colourdelegate.h"
#include "./positiondelegate.h"
#include "./conditiondelegate.h"
#include "./graphsettingsitem.h"
#include "./graphsettingsmodel.h"
#include "./graphwidget.h"
#include "./enableddelegate.h"
#include "./graphdelegate.h"
/*! \brief Setup the main window.
* \param *parent
*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
/* Setup User Interface */
ui->setupUi(this);
this->setWindowTitle("FLAME Visualiser - ");
enableInterface(false); /* Disable UI to start */
/* Set the application icon (for linux) as mac and win
* have platform-dependent techniques (see .pro file).
*/
#ifdef Q_WS_X11
setWindowIcon(QIcon("flame-v.png"));
#endif
/* Initialise variables */
itLocked = false;
fileOpen = false;
images_dialog_open = false;
time_dialog_open = false;
restrict_dimension_open = false;
iterationInfo_dialog_open = false;
timeString = "";
iteration = 0;
openedValidIteration = false;
delayTime = 0;
configPath = "";
configName = "";
timeScale = new TimeScale();
visual_settings_model = new VisualSettingsModel();
connect(visual_settings_model, SIGNAL(ruleUpdated(int)),
this, SLOT(ruleUpdated(int)));
graph_settings_model = new GraphSettingsModel(&agents);
connect(graph_settings_model,
SIGNAL(plotGraphChanged(GraphSettingsItem*, QString, QString)),
this, SLOT(
plotGraphChanged(GraphSettingsItem*, QString, QString)));
/* Visual window camera variables */
visualBackground = Qt::white;
xrotate = 0.0;
yrotate = 0.0;
xmove = 0.0;
ymove = 0.0;
zmove = -3.0;
xoffset = 0.0;
yoffset = 0.0;
zoffset = 0.0;
orthoZoom = 1.0;
restrictDimension = new Dimension();
agentDimension = new Dimension();
restrictAgentDimension = new Dimension();
on_actionLines_triggered();
/* Setup 3D OpenGL visual window */
// ui->pushButton_OpenCloseVisual->setText("Open visual");
opengl_window_open = false;
/* Set update viewpoint button to be false */
ui->pushButton_updateViewpoint->setEnabled(false);
on_actionPerspective_triggered();
/* Setup tables in UI */
/* Set tableViewVisual to stretch columns to table size */
QHeaderView *headerView = ui->tableViewVisual->horizontalHeader();
headerView->setResizeMode(QHeaderView::Stretch);
headerView->setResizeMode(1, QHeaderView::Interactive);
ui->tableViewVisual->verticalHeader()->hide();
ui->tableViewVisual->setModel(visual_settings_model);
ui->tableViewVisual->setSelectionBehavior(QAbstractItemView::SelectRows);
/* Set tableViewVisual delegates for each column below */
ui->tableViewVisual->setItemDelegateForColumn(0,
new AgentTypeDelegate(&agentTypes));
ui->tableViewVisual->setItemDelegateForColumn(1,
new ConditionDelegate(&agentTypes, visual_settings_model));
ui->tableViewVisual->setItemDelegateForColumn(2,
new PositionDelegate(&agentTypes, visual_settings_model));
ui->tableViewVisual->setItemDelegateForColumn(3,
new PositionDelegate(&agentTypes, visual_settings_model));
ui->tableViewVisual->setItemDelegateForColumn(4,
new PositionDelegate(&agentTypes, visual_settings_model));
ui->tableViewVisual->setItemDelegateForColumn(5,
new ShapeDelegate(&agentTypes, visual_settings_model));
ui->tableViewVisual->setItemDelegateForColumn(6,
new ColourDelegate); /* Only to draw the cell */
/* Connect signals that affect tableViewVisual */
connect(ui->tableViewVisual, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(getColourVisual(QModelIndex)));
/* Handles the enabling of visual rules */
connect(ui->tableViewVisual, SIGNAL(clicked(QModelIndex)),
this, SLOT(enabledRule(QModelIndex)));
connect(ui->pushButton_AddAgentType, SIGNAL(clicked()),
this, SLOT(addRule()));
connect(ui->pushButton_DeleteAgentType, SIGNAL(clicked()),
this, SLOT(deleteRule()));
/* Set tableViewGraph to stretch columns to table size */
headerView = ui->tableViewGraph->horizontalHeader();
headerView->setResizeMode(QHeaderView::Stretch);
headerView->setResizeMode(1, QHeaderView::Interactive);
ui->tableViewGraph->verticalHeader()->hide();
ui->tableViewGraph->setModel(graph_settings_model);
ui->tableViewGraph->setSelectionBehavior(QAbstractItemView::SelectRows);
/* Set tableViewGraph delegates for each column below */
ui->tableViewGraph->setItemDelegateForColumn(0,
new GraphDelegate(graph_settings_model, 0));
ui->tableViewGraph->setItemDelegateForColumn(1,
new GraphDelegate(graph_settings_model, 1));
ui->tableViewGraph->setItemDelegateForColumn(2,
new AgentTypeDelegate(&agentTypes));
ui->tableViewGraph->setItemDelegateForColumn(3,
new ConditionDelegate(&agentTypes, graph_settings_model));
ui->tableViewGraph->setItemDelegateForColumn(4,
new ColourDelegate); /* Only to draw the cell */
/* Connect signals that affect tableViewGraph */
/* Handles the color choosing */
connect(ui->tableViewGraph, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(getColourGraph(QModelIndex)));
connect(ui->pushButton_AddPlot, SIGNAL(clicked()),
this, SLOT(addPlot()));
connect(ui->pushButton_DeletePlot, SIGNAL(clicked()),
this, SLOT(deletePlot()));
/* Handles the enabling of graphs */
connect(ui->tableViewGraph, SIGNAL(clicked(QModelIndex)),
this, SLOT(enabledGraph(QModelIndex)));
/* Connect signals of time scale */
connect(ui->checkBox_timeScale, SIGNAL(clicked(bool)),
this, SLOT(enableTimeScale(bool)));
/* Connect signals of iteration buttons */
connect(ui->pushButton_ForwardIteration, SIGNAL(clicked()),
this, SLOT(increment_iteration()));
connect(ui->pushButton_BackIteration, SIGNAL(clicked()),
this, SLOT(decrement_iteration()));
/* Connect signals of the menu items */
connect(ui->actionNew, SIGNAL(triggered()),
this, SLOT(new_config_file()));
connect(ui->actionOpen, SIGNAL(triggered()),
this, SLOT(open_config_file()));
connect(ui->actionSave, SIGNAL(triggered()),
this, SLOT(save_config_file()));
connect(ui->actionSave_As, SIGNAL(triggered()),
this, SLOT(save_as_config_file()));
connect(ui->actionClose, SIGNAL(triggered()),
this, SLOT(close_config_file()));
/* Try and find .flamevisualisersettings and load
* last known model and iteration number */
findLoadSettings();
}
/*! \brief Destroy the main window.
*/
MainWindow::~MainWindow() {
delete ui;
/* Output settings */
QFile file(".flamevisualisersettings");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
if (fileOpen) {
QString configFile;
configFile.append(configPath);
configFile.append("/");
configFile.append(configName);
QTextStream out(&file);
out << "true|" << configFile << "|" << iteration << "\n";
} else {
QTextStream out(&file);
out << "false|\n";
}
file.close();
}
void MainWindow::closeEvent(QCloseEvent */*event*/) {
on_actionQuit_triggered();
}
/*! \brief Try and find .flamevisualisersettings and load last known model and iteration number.
*/
void MainWindow::findLoadSettings() {
/* Try and open file, return if fail */
QFile file(".flamevisualisersettings");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
/* Read in each line */
QTextStream in(&file);
if (!in.atEnd()) {
QString line = in.readLine();
/* Split using | into a string list */
QStringList list = line.split("|");
/* If the first value is true */
if (QString::compare(list.at(0), "true") == 0) {
/* Try and read in the config file */
readConfigFile(list.at(1), list.at(2).toInt());
}
}
}
/*! \brief When the visual window is closed, disconnect all signal/slots and set variables.
*/
void MainWindow::visual_window_closed() {
disconnect(this, SIGNAL(updateVisual()),
visual_window, SLOT(updateGL()));
disconnect(visual_window, SIGNAL(increase_iteration()),
this, SLOT(increment_iteration()));
disconnect(visual_window, SIGNAL(decrease_iteration()),
this, SLOT(decrement_iteration()));
disconnect(visual_window, SIGNAL(visual_window_closed()),
this, SLOT(visual_window_closed()));
disconnect(this, SIGNAL(iterationLoaded()),
visual_window, SLOT(iterationLoaded()));
disconnect(visual_window, SIGNAL(signal_toggleAnimation()),
this, SLOT(slot_toggleAnimation()));
disconnect(this, SIGNAL(takeSnapshotSignal()),
visual_window, SLOT(takeSnapshot()));
disconnect(visual_window, SIGNAL(imageStatus(QString)),
this, SLOT(imageStatusSlot(QString)));
disconnect(this, SIGNAL(takeAnimationSignal(bool)),
visual_window, SIGNAL(takeAnimation(bool)));
disconnect(this, SIGNAL(updateImagesLocationSignal(QString)),
visual_window, SLOT(updateImagesLocation(QString)));
disconnect(this, SIGNAL(restrictAxes(bool)),
visual_window, SLOT(restrictAxes(bool)));
disconnect(this, SIGNAL(updateDelayTime(int)),
visual_window, SIGNAL(updateDelayTime(int)));
opengl_window_open = false;
ui->pushButton_OpenCloseVisual->setText("Open Visual Window");
animation = false;
ui->pushButton_Animate->setText("Start Animation - A");
ui->pushButton_Animate->setEnabled(false);
/* Set update viewpoint button to be false */
ui->pushButton_updateViewpoint->setEnabled(false);
}
/*! \brief When the image dialog is closed, disconnect all signal/slots and set variables.
*/
void MainWindow::image_dialog_closed() {
disconnect(images_dialog, SIGNAL(image_dialog_closed()),
this, SLOT(image_dialog_closed()));
disconnect(images_dialog, SIGNAL(take_snapshot()),
this, SLOT(takeSnapshotSlot()));
disconnect(this, SIGNAL(imageStatusSignal(QString)),
images_dialog, SLOT(imageStatus(QString)));
disconnect(images_dialog, SIGNAL(takeAnimationSignal(bool)),
this, SLOT(takeAnimationSlot(bool)));
disconnect(images_dialog, SIGNAL(updateImagesLocation(QString)),
this, SLOT(updateImagesLocationSlot(QString)));
images_dialog_open = false;
ui->pushButton_ImageSettings->setText("Open Image Settings");
}
/*! \brief When the time dialog is closed, disconnect all signal/slots and set variables.
*/
void MainWindow::time_dialog_closed() {
disconnect(time_dialog, SIGNAL(time_dialog_closed()),
this, SLOT(time_dialog_closed()));
time_dialog_open = false;
ui->pushButton_timeScale->setText("Open Time Settings");
calcTimeScale();
}
/*! \brief When the iteration info dialog is closed, disconnect all signal/slots
* and set variables.
*/
void MainWindow::iterationInfoDialog_closed() {
disconnect(this, SIGNAL(updateIterationInfoDialog()),
iterationInfo_dialog, SLOT(update_info()));
disconnect(iterationInfo_dialog, SIGNAL(iterationInfoDialog_closed()),
this, SLOT(iterationInfoDialog_closed()));
iterationInfo_dialog_open = false;
}
void MainWindow::restrict_axes_closed() {
restrict_dimension_open = false;
disconnect(restrictAxesDialog, SIGNAL(closed()),
this, SLOT(restrict_axes_closed()));
disconnect(this, SIGNAL(updatedAgentDimension()),
restrictAxesDialog, SLOT(updatedAgentDimensions()));
emit(restrictAxes(false));
}
/*! \brief When a graph window is closed, close all windows with the same graph name.
* \param graphName The graph name.
*/
void MainWindow::graph_window_closed(QString graphName) {
closeGraphWindows(graphName);
}
/*! \brief Enable and disable the user interface when a file is open or closed.
* \param enable True for enabled, false for disabled.
*/
void MainWindow::enableInterface(bool enable) {
/* Enable/Disable UI */
emit(ui->groupBox->setEnabled(enable));
emit(ui->groupBox_2->setEnabled(enable));
emit(ui->groupBox_3->setEnabled(enable));
emit(ui->groupBox_4->setEnabled(enable));
/* Enable/Disable menu items */
emit(ui->actionClose->setEnabled(enable));
emit(ui->actionSave->setEnabled(enable));
emit(ui->actionSave_As->setEnabled(enable));
}
/*! \brief Add a new plot to the graph model.
*/
void MainWindow::addPlot() {
graph_settings_model->addPlot();
}
/*! \brief Create a new graph window, setup.
*/
void MainWindow::createGraphWindow(GraphWidget *graph_window) {
graphs.append(graph_window);
graph_window->updateData(iteration);
graph_window->resize(720, 380);
graph_window->show();
connect(graph_window, SIGNAL(increase_iteration()),
this, SLOT(increment_iteration()));
connect(graph_window, SIGNAL(decrease_iteration()),
this, SLOT(decrement_iteration()));
connect(graph_window, SIGNAL(signal_toggleAnimation()),
this, SLOT(slot_toggleAnimation()));
connect(graph_window, SIGNAL(graph_window_closed(QString)),
this, SLOT(graph_window_closed(QString)));
}
/*! \brief Set all rules with the graph name as disabled and close and
* remove all associated graph windows.
* \param graphName The graph name
*/
void MainWindow::closeGraphWindows(QString graphName) {
graph_settings_model->setDisabled(graphName);
/* Check open graphs */
for (int i = 0; i < graphs.count(); i++) {
if (QString::compare(graphs[i]->getGraph(), graphName) == 0) {
graphs[i]->close();
graphs.removeAt(i);
i--;
}
}
}
/*! \brief Enable or disable a visual rule when the enabled cell
* of the visual rule table is clicked.
* \param index The index of the cell clicked.
*/
void MainWindow::enabledRule(QModelIndex index) {
/* If the enabled column */
if (index.column() == 7) {
/* Switch the enabled value */
visual_settings_model->switchEnabled(index);
/* Populate rule agents */
visual_settings_model->getRule(index.row())->populate(&agents);
visual_settings_model->getRule(index.row())->
copyAgentDrawDataToRuleAgentDrawData(agentDimension);
visual_settings_model->getRule(index.row())->
applyOffset(xoffset, yoffset, zoffset);
visual_settings_model->getRule(index.row())->applyRatio(ratio);
}
}
/*! \brief Show or hide a graph window when the enabled cell of the graph table is clicked.
* \param index The index of the cell clicked.
*/
void MainWindow::enabledGraph(QModelIndex index) {
bool enabled;
if (index.column() == 5) {
enabled = !(graph_settings_model->getPlot(index.row())->getEnable());
graph_settings_model->switchEnabled(index);
if (enabled) {
GraphWidget * gw = new GraphWidget(
&agents, &graph_style, timeScale);
gw->setGraph(graph_settings_model->
getPlot(index.row())->getGraph());
QList<GraphSettingsItem *> subplots =
graph_settings_model->getPlotsInSameGraph(index);
for (int i = 0; i < subplots.count(); i++)
gw->addPlot(subplots.at(i));
createGraphWindow(gw);
} else {
closeGraphWindows(graph_settings_model->
getPlot(index.row())->getGraph());
}
}
}
/*! \brief Handle a graph name change of a plot rule.
* \param gsi The GraphSettingsItem.
* \param oldGraph The old graph name
* \param newGraph The new graph name
*/
void MainWindow::plotGraphChanged(GraphSettingsItem * gsi, QString oldGraph,
QString newGraph) {
for (int i = 0; i < graphs.count(); i++) {
qDebug() << i << graphs[i]->getGraph() << newGraph;
if (QString::compare(graphs[i]->getGraph(), oldGraph) == 0) {
int rc = graphs[i]->removePlot(gsi);
if (rc == 0) {
closeGraphWindows(oldGraph);
} else {
graphs[i]->repaint();
}
}
if (QString::compare(graphs[i]->getGraph(), newGraph) == 0) {
graphs[i]->addPlot(gsi);
graphs[i]->updateData(iteration);
graphs[i]->repaint();
}
}
}
/*! \brief Show the colour dialog when the colour cell of the visual table is clicked.
* \param index The index of the cell clicked.
*/
void MainWindow::getColourVisual(QModelIndex index) {
if (index.column() == 6) {
colourIndex = index;
colour = qVariantValue<QColor>(index.data());
QColor colourSave = colour;
QColorDialog *colourDialog = new QColorDialog(this);
colourDialog->setOption(QColorDialog::ShowAlphaChannel);
colourDialog->setCurrentColor(colour);
connect(colourDialog, SIGNAL(currentColorChanged(QColor)),
this, SLOT(colourChanged(QColor)));
int rc = colourDialog->exec();
if (rc == QDialog::Rejected) {
visual_settings_model->setData(index, qVariantFromValue(colourSave));
}
delete colourDialog;
}
}
/*! \brief Update the visual settings model with any change in colour.
* \param c The new colour.
*/
void MainWindow::colourChanged(QColor c) {
visual_settings_model->setData(colourIndex, qVariantFromValue(c));
}
/*! \brief Show the colour dialog when the
* colour cell of the graph table is clicked.
* \param index The index of the cell clicked.
*/
void MainWindow::getColourGraph(QModelIndex index) {
if (index.column() == 4) {
QColor colour = QColorDialog::getColor(
qVariantValue<QColor>(index.data()));
if (colour.isValid()) {
graph_settings_model->setData(index, qVariantFromValue(colour));
}
}
}
/*! \brief Connect the add rule button with the data model.
*/
void MainWindow::addRule() {
visual_settings_model->addRule();
}
/*! \brief Connect the delete rule button with the data model and
* keep deleting all selected rows.
*/
void MainWindow::deleteRule() {
QModelIndexList indexList =
ui->tableViewVisual->selectionModel()->selectedRows();
while (indexList.count() > 0) {
visual_settings_model->deleteRule(indexList.at(0));
indexList = ui->tableViewVisual->selectionModel()->selectedRows();
}
}
/*! \brief Connect the delete plot button with the data model and
* keep deleting all selected rows.
*/
void MainWindow::deletePlot() {
QModelIndexList indexList =
ui->tableViewGraph->selectionModel()->selectedRows();
while (indexList.count() > 0) {
graph_settings_model->deletePlot(indexList.at(0));
indexList = ui->tableViewGraph->selectionModel()->selectedRows();
}
}
/** \brief Provide a dialog to select the folder location of the 0.xml
* and make the path relative to the config xml location.
*/
void MainWindow::on_pushButton_LocationFind_clicked() {
QString s = configPath;
s.append("/");
s.append(ui->lineEdit_ResultsLocation->text());
/* Provide dialog to select folder */
QString filepath =
QFileDialog::getExistingDirectory(this,
tr("Select results data location..."),
s, QFileDialog::ShowDirsOnly);
if (filepath.isEmpty())
return;
/* Return relative path from currentPath to location */
QDir dir(configPath);
QString s1 = dir.canonicalPath();
QDir dir2(s1);
s = dir2.relativeFilePath(filepath);
ui->lineEdit_ResultsLocation->setText(s);
// tryAndReadInAgentTypes();
readZeroXML(); /* Read in new agent data */
}
/*! \brief Switch between the visual window being shown or hidden.
*/
void MainWindow::on_pushButton_OpenCloseVisual_clicked() {
if (opengl_window_open == false) {
/* Calculate viewpoint */
resetVisualViewpoint();
visual_window = new GLWidget(&xrotate, &yrotate, &xmove, &ymove, &zmove,
restrictDimension, &orthoZoom, &animation);
// Make the window destroy on close rather than hide
visual_window->setAttribute(Qt::WA_DeleteOnClose);
visual_window->resize(800, 600);
visual_window->update_agents(&agents);
visual_window->set_rules(visual_settings_model);
visual_window->setIteration(&iteration);
visual_window->setConfigPath(&configPath);
visual_window->setTimeScale(timeScale);
visual_window->setTimeString(&timeString);
visual_window->setDimension(visual_dimension);
visual_window->setBackgroundColour(visualBackground);
/* Connect signals between MainWindow and visual_window */
connect(this, SIGNAL(updateVisual()),
visual_window, SLOT(updateGL()));
connect(visual_window, SIGNAL(increase_iteration()),
this, SLOT(increment_iteration()));
connect(visual_window, SIGNAL(decrease_iteration()),
this, SLOT(decrement_iteration()));
connect(visual_window, SIGNAL(visual_window_closed()),
this, SLOT(visual_window_closed()));
connect(this, SIGNAL(iterationLoaded()),
visual_window, SLOT(iterationLoaded()));
connect(visual_window, SIGNAL(signal_toggleAnimation()),
this, SLOT(slot_toggleAnimation()));
connect(this, SIGNAL(takeSnapshotSignal()),
visual_window, SLOT(takeSnapshot()));
connect(visual_window, SIGNAL(imageStatus(QString)),
this, SLOT(imageStatusSlot(QString)));
connect(this, SIGNAL(takeAnimationSignal(bool)),
visual_window, SLOT(takeAnimation(bool)));
connect(this, SIGNAL(updateImagesLocationSignal(QString)),
visual_window, SLOT(updateImagesLocation(QString)));
connect(this, SIGNAL(restrictAxes(bool)),
visual_window, SLOT(restrictAxes(bool)));
connect(this, SIGNAL(updateDelayTime(int)),
visual_window, SLOT(updateDelayTime(int)));
if (restrict_dimension_open) emit( restrictAxes(true) );
visual_window->show();
visual_window->setFocus();
ui->pushButton_OpenCloseVisual->setText("Close Visual Window");
opengl_window_open = true;
/* Set update viewpoint button to be false */
ui->pushButton_updateViewpoint->setEnabled(true);
ui->pushButton_Animate->setEnabled(true);
} else {
visual_window->close();
}
}
/*! \brief Read the 0 xml defined by the current iteration.
* \return False if the file could not be found.
*/
int MainWindow::readZeroXML() {
if (itLocked) return 3;
itLocked = true;
QString fileName;
fileName.append(configPath);
fileName.append("/");
fileName.append(ui->lineEdit_ResultsLocation->text());
fileName.append("/");
fileName.append(QString().number(iteration));
fileName.append(".xml");
// qDebug() << "Opening file: " << fileName;
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
// ui->spinBox->setValue(iteration);
ui->label_5->setText(QString("! Error opening %1.xml").
arg(QString().number(iteration)));
itLocked = false;
return 1;
}
// used by visual
/* for (int i = 0; i < visual_settings_model->rowCount(); i++) {
for (int j = 0; j < visual_settings_model->getRule(i)->agents.size(); j++)
// Free memory of ruleagent data
delete visual_settings_model->getRule(i)->agents.at(j);
visual_settings_model->getRule(i)->agents.clear();
}*/
// used by graphs
for (int j = 0; j < agents.size(); j++)
// Free memory of agent tag data
delete agents.at(j);
agents.clear();
// used by iteration info dialog
QHash<QString, int>::iterator i;
for (i = agentTypeCounts.begin(); i != agentTypeCounts.end(); ++i)
i.value() = 0;
ZeroXMLReader reader(&agents, &agentTypes, visual_settings_model, ratio,
agentDimension, &stringAgentTypes, &agentTypeCounts,
xoffset, yoffset, zoffset);
if (!reader.read(&file)) {
// ui->spinBox->setValue(iteration);
ui->label_5->setText(
QString("! Error reading %1.xml").
arg(QString().number(iteration)));
/* Make the path to the file look pretty */
QDir dir(fileName);
QString filePath = dir.canonicalPath();
QString error = tr(
"Cannot parse iteration file %1 at line %2, column %3:\n%4").arg(
filePath).arg(reader.lineNumber()).arg(
reader.columnNumber()).arg(reader.errorString());
#ifdef TESTBUILD
qDebug() << error;
#else
QMessageBox::warning(this, "FLAME Visualiser", error);
#endif
itLocked = false;
return 2;
} else {
itLocked = false;
if (opengl_window_open) emit(iterationLoaded());
ui->label_5->setText(
QString("Read %1.xml").arg(QString().number(iteration)));
}
if (iterationInfo_dialog_open) {
emit(updateIterationInfoDialog());
}
if (openedValidIteration == false && opengl_window_open == true)
resetVisualViewpoint();
openedValidIteration = true;
return 0;
}
/*! \brief Change the iteration number to the number of the spin box.
* \param arg1 The value of the spin box
*/
void MainWindow::on_spinBox_valueChanged(int arg1) {
// qDebug() << "on_spinBox_valueChanged" << arg1;
if (iteration != arg1) {
iteration = arg1;
int rc = readZeroXML(); /* Read in new agent data */
if (rc == 0) if (ui->checkBox_timeScale->isChecked()) calcTimeScale();
}
}
/*! \brief Increment the iteration number, read in new agent data, set the spin box value, update all graphs.
*/
void MainWindow::increment_iteration() {
int rc;
/* increase iteration number */
iteration++;
/* try and read the iteration file
* the parameter 1 means try and read in the agent data */
rc = readZeroXML();
/* return codes
3 - itLocked is true
1 - error opening file
2 - error reading file
0 - success
*/
if (rc == 1) { // Can't open file
/* search forward '0' for next file */
if (checkDirectoryForNextIteration(iteration, 0)) {
// successful
rc = readZeroXML();
}
}
if (rc != 0) { // unsuccessful open and read
iteration--;
if (animation) {
slot_toggleAnimation();
}
} else {
if (ui->checkBox_timeScale->isChecked()) calcTimeScale();
for (int i = 0; i < graphs.count(); i++) {
graphs.at(i)->updateData(iteration);
}
if (restrict_dimension_open) emit(updatedAgentDimension());
}
ui->spinBox->setValue(iteration);
}
/*! \brief Decrement the iteration number, set the spin box value, read in new agent data.
*/
void MainWindow::decrement_iteration() {
int rc;
if (iteration > 0) iteration--;
rc = readZeroXML();
if (rc == 1) { // Can't open file
if (checkDirectoryForNextIteration(iteration, 1)) {
rc = readZeroXML();
}
}
ui->spinBox->setValue(iteration);
if (rc == 0) {
ui->spinBox->setValue(iteration);
if (ui->checkBox_timeScale->isChecked()) calcTimeScale();
if (restrict_dimension_open) emit(updatedAgentDimension());
}
}
/*! \brief Provide a dialog to select a config file to open.
*/
void MainWindow::open_config_file() {
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open config file..."),
"", tr("XML Files (*.xml)"));
if (fileName.isEmpty())
return;
close_config_file();
readConfigFile(fileName, 0);
}
bool MainWindow::checkDirectoryForNextIteration(int it, int flag) {
QString fileName;
fileName.append(configPath);
fileName.append("/");
fileName.append(ui->lineEdit_ResultsLocation->text());
fileName.append("/");
QDir dir(fileName);
QStringList filters;
filters << "*.xml";
dir.setNameFilters(filters);
QStringList list = dir.entryList();
QList<int> ilist;
for (int i = 0; i < list.size(); i++) {
QString f = list.at(i);
f.chop(4);
bool s;
int j = f.toInt(&s);
if (s) ilist.append(j);
}
qSort(ilist);
for (int i = 0; i < ilist.size(); i++) {
if ((ilist.at(i) > it && flag == 0) ||
(ilist.at(ilist.size()-1-i) < it && flag == 1)) {
if (flag == 0) iteration = ilist.at(i);
if (flag == 1) iteration = ilist.at(ilist.size()-1-i);
return true;
}
}
return false;
}
/*! \brief Read in a config file.
* \param fileName The file name
* \param it The iteration number to be set
*/
int MainWindow::readConfigFile(QString fileName, int it) {
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QString error = tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString());
#ifdef TESTBUILD
qDebug() << error;
#else
QMessageBox::warning(this, tr("FLAME Visualiser"), error);
#endif
return 1;
}
ConfigXMLReader reader(visual_settings_model, graph_settings_model,
&resultsData, timeScale, &ratio, &xrotate, &yrotate,
&xmove, &ymove, &zmove, &delayTime, &orthoZoom, &visual_dimension,
&visualBackground);
if (!reader.read(&file)) {
QString error = tr("Parse error in file %1 at line %2, column %3:\n%4").
arg(fileName).
arg(reader.lineNumber()).
arg(reader.columnNumber()).
arg(reader.errorString());
#ifdef TESTBUILD
qDebug() << error;
#else
QMessageBox::warning(this, tr("FLAME Visualiser"), error);
#endif
/* Clear anything read in */
close_config_file();
/* Close file */
file.close();
return 2;
}
if (visual_dimension == 2) on_actionOrthogonal_triggered();
if (visual_dimension == 3) on_actionPerspective_triggered();
/* Setup time scale */
timeScale->calcTotalSeconds();
enableTimeScale(timeScale->enabled);
QFileInfo fileInfo(file.fileName());
configPath = fileInfo.absolutePath();
configName = fileInfo.fileName();
QString wtitle;
wtitle.append("FLAME Visualiser - ");
wtitle.append(file.fileName());
this->setWindowTitle(wtitle);
iteration = it;
ui->spinBox->setValue(iteration);
ui->horizontalSlider_delay->setValue(
static_cast<int>((1000-delayTime)/1000.0 * 99));
// qDebug() << "slider value: " << ui->horizontalSlider_delay->value();
if (ui->checkBox_timeScale->isChecked()) calcTimeScale();
ui->lineEdit_ResultsLocation->setText(resultsData);
// tryAndReadInAgentTypes();
openedValidIteration = false;
readZeroXML();
/* For each new plot create a graph window */
for (int i = 0; i < graph_settings_model->rowCount(); i++) {
}
enableInterface(true);
fileOpen = true;
file.close();
return 0;
}
/*! \brief Enable or disable the time scale UI.
* \param b The enable or disable flag
*/
void MainWindow::enableTimeScale(bool b) {
timeScale->enabled = b;
ui->checkBox_timeScale->setChecked(b);
ui->lineEdit_timeScale->setEnabled(b);
ui->pushButton_timeScale->setEnabled(b);
/*QPalette palet;
if (b) palet.setColor(ui->checkBox_timeScale->foregroundRole(),
QColor(0, 0, 0));
else
palet.setColor(ui->checkBox_timeScale->foregroundRole(),
QColor(120, 120, 120));
ui->checkBox_timeScale->setPalette(palet);*/
if (b) calcTimeScale();
else
emit(ui->lineEdit_timeScale->setText(""));
}
int MainWindow::create_new_config_file(QString fileName) {
if (fileName.isEmpty())
return 1;
/* Close any current config */
close_config_file();
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QString error = tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString());
#ifdef TESTBUILD
qDebug() << error;
#else
QMessageBox::warning(this, tr("FLAME Visualiser"), error);
#endif
return 2;
}
enableInterface(true);
writeConfigXML(&file);
return 0;
}
/*! \brief Open a new config file.
*/
void MainWindow::new_config_file() {
QString fileName =
QFileDialog::getSaveFileName(this, tr("New config file..."),
"",
tr("XML Files (*.xml)"));
create_new_config_file(fileName);
}
int MainWindow::save_config_file_internal(QString fileName) {
if (fileName.isEmpty())
return 1;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QString error = tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString());
#ifdef TESTBUILD
qDebug() << error;
#else
QMessageBox::warning(this, tr("FLAME Visualiser"), error);
#endif
return 2;
}
writeConfigXML(&file);
return 0;
}
/*! \brief Provide a dialog to save a config file.
*/
void MainWindow::save_as_config_file() {
QString configFile = "";
if (fileOpen) {
configFile.append(configPath);
configFile.append("/");
configFile.append(configName);
}
QString fileName =
QFileDialog::getSaveFileName(this, tr("Save config file..."),
configFile,
tr("XML Files (*.xml)"));
save_config_file_internal(fileName);
}
/*! \brief Save a config file.
*/
void MainWindow::save_config_file() {
save_as_config_file();