-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
137 lines (113 loc) · 4.14 KB
/
MainWindow.cpp
File metadata and controls
137 lines (113 loc) · 4.14 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
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QModelIndex>
#include "Simulator.h"
#include "LSystem.h"
#include "Variable.h"
#include "VariableSequence.h"
#include "VariableSequenceModel.h"
#include "VariableSequenceListModel.h"
#include "ProductionTableModel.h"
#include "DialogCreateVariable.h"
#include "DialogCreateAxiom.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow),
_simulator(new Simulator()),
_lSystem(new LSystem(this)),
_createVariableDialog(nullptr),
_createAxiomDialog(nullptr),
_currentSystem(nullptr),
_currentSystemModel(nullptr)
{
_ui->setupUi(this);
_ui->variableListView->setModel(_lSystem->getVariableModel());
_ui->axiomListView->setModel(_lSystem->getAxiomModel());
_ui->actionLSystem_Editor->setChecked(!_ui->lSystemEditor->isHidden());
_simulator->setLSystem(_lSystem);
}
MainWindow::~MainWindow() {
delete _ui;
if(_currentSystemModel) delete _currentSystemModel;
if(_currentSystem) delete _currentSystem;
}
void MainWindow::on_btnAddVariable_clicked() {
if(!_createVariableDialog)
_createVariableDialog = new DialogCreateVariable(_lSystem, this);
_createVariableDialog->setMode(DialogCreateVariable::CREATE);
_createVariableDialog->setVariable(new Variable);
_createVariableDialog->show();
}
void MainWindow::on_btnRemoveVariable_clicked() {
QList<int> indices;
foreach(const QModelIndex& index, _ui->variableListView->selectionModel()->selectedIndexes()) {
indices.push_back(index.row());
}
qSort(indices);
for(int i = indices.length() - 1; i >= 0; i--) {
_ui->variableListView->model()->removeRow(indices.at(i));
}
}
void MainWindow::on_btnEditVariable_clicked() {
QModelIndexList indexes = _ui->variableListView->selectionModel()->selectedIndexes();
if(indexes.length() > 0) {
editVariable((Variable*)_ui->variableListView->model()->data(indexes[0], Qt::EditRole).value<void*>());
}
}
void MainWindow::on_variableListView_doubleClicked(const QModelIndex &index) {
editVariable((Variable*)_ui->variableListView->model()->data(index, Qt::EditRole).value<void*>());
}
void MainWindow::editVariable(Variable* variable) {
if(!_createVariableDialog) {
_createVariableDialog = new DialogCreateVariable(_lSystem, this);
}
_createVariableDialog->setVariable(variable);
_createVariableDialog->setMode(DialogCreateVariable::EDIT);
_createVariableDialog->show();
}
void MainWindow::on_btnAddAxiom_clicked() {
if(!_createAxiomDialog)
_createAxiomDialog = new DialogCreateAxiom(_lSystem, this);
_createAxiomDialog->setAxiom(new VariableSequenceModel(new VariableSequence));
_createAxiomDialog->setMode(DialogCreateAxiom::CREATE);
_createAxiomDialog->show();
}
void MainWindow::on_btnEditAxiom_clicked() {
if(!_createAxiomDialog)
_createAxiomDialog = new DialogCreateAxiom(_lSystem, this);
QModelIndexList selectedIndices = _ui->axiomListView->selectionModel()->selectedIndexes();
if(selectedIndices.length() > 0) {
QModelIndex firstIndex = selectedIndices.at(0);
QModelIndex choosen = _ui->axiomListView->model()->index(firstIndex.row(), 0, QModelIndex());
_createAxiomDialog->setAxiom((VariableSequenceModel*)_ui->axiomListView->model()->data(choosen, Qt::UserRole).value<void*>());
_createAxiomDialog->setMode(DialogCreateAxiom::Mode::EDIT);
_createAxiomDialog->show();
}
}
void MainWindow::on_btnRemoveAxiom_clicked() {
foreach(QModelIndex index, _ui->axiomListView->selectionModel()->selectedRows()) {
_ui->axiomListView->model()->removeRow(index.row());
}
}
void MainWindow::on_btnSimulate_clicked() {
if(_currentSystemModel) {
_ui->simulatedListView->setModel(nullptr);
delete _currentSystemModel;
}
if(_currentSystem) delete _currentSystem;
int recurseLevel = _ui->sbRecurseLevel->value();
VariableSequence* result = _simulator->simulate(recurseLevel);
if(!result) return;
_currentSystem = result;
_currentSystemModel = _currentSystem->getModel();
_ui->simulatedListView->setModel(_currentSystemModel);
}
void MainWindow::on_sbSeed_valueChanged(int value) {
_simulator->setSeed(value);
}
void MainWindow::on_pushButton_clicked() {
int seed = (int)_simulator->generateSeed();
qDebug() << seed;
_ui->sbSeed->setValue(seed);
}