-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogCreateVariable.cpp
More file actions
162 lines (131 loc) · 5.35 KB
/
DialogCreateVariable.cpp
File metadata and controls
162 lines (131 loc) · 5.35 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
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
#include "DialogCreateVariable.h"
#include "ui_DialogCreateVariable.h"
#include "Variable.h"
#include "ProductionTableModel.h"
#include "Production.h"
#include "MainWindow.h"
#include "LSystem.h"
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QDebug>
DialogCreateVariable::DialogCreateVariable(LSystem* lSystem, QWidget* parent) :
QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint),
_ui(new Ui::DialogCreateVariable),
_lSystem(lSystem),
_variable(nullptr),
_variableModel(lSystem->getVariableModel()),
_itemDelegate(new SpinBoxDelegate(this)),
_currentMode(CREATE),
_currentProductionModel(nullptr)
{
_ui->setupUi(this);
_ui->variableListView->setModel(_variableModel);
_ui->productionsView->setItemDelegateForColumn(1, _itemDelegate);
connect(this, SIGNAL(done()), this, SLOT(accept()));
connect(this, SIGNAL(cancel()), this, SLOT(reject()));
}
DialogCreateVariable::~DialogCreateVariable() {
delete _ui;
}
void DialogCreateVariable::setVariable(Variable* variable) {
_variable = variable;
_originalName = variable->getName();
_ui->txtVarName->setText(variable->getName());
_ui->productionsView->setModel((QAbstractItemModel*)variable->getProductionModel());
_currentProductionModel = nullptr;
_currentProductionIndex = QModelIndex();
_ui->producitionVariableView->setModel(nullptr);
}
Variable* DialogCreateVariable::getVariable() {
return _variable;
}
void DialogCreateVariable::setMode(DialogCreateVariable::Mode mode) {
_currentMode = mode;
}
DialogCreateVariable::Mode DialogCreateVariable::getMode() const {
return _currentMode;
}
void DialogCreateVariable::on_btnAddProduction_clicked() {
// Create a new production and make it the one being edited
_ui->productionsView->model()->insertRow(0);
Production* production = new Production(new VariableSequence(), 1, this);
_currentProductionIndex = _ui->productionsView->model()->index(0, 0);
_ui->productionsView->model()->setData(_currentProductionIndex, QVariant::fromValue<void*>((void*)production), Qt::UserRole);
_ui->producitionVariableView->setModel(((Production*)_ui->productionsView->model()->data(_currentProductionIndex, Qt::UserRole).value<void*>())->getVariableSequenceModel());
//_ui->txtProductionName->setText(production->getName());
}
void DialogCreateVariable::on_btnRemoveProduction_clicked() {
ProductionTableModel* model = (ProductionTableModel*)_ui->productionsView->model();
foreach(QModelIndex index, _ui->productionsView->selectionModel()->selectedIndexes()) {
model->removeRow(index.row());
}
}
// Add the currently selected variable to the end of the production
void DialogCreateVariable::on_btnAddToProduction_clicked() {
if(!_currentProductionModel) return;
foreach(QModelIndex index, _ui->variableListView->selectionModel()->selectedIndexes()) {
if(!index.isValid()) continue;
Variable* var = (Variable*)_ui->variableListView->model()->data(index, Qt::EditRole).value<void*>();
int row = _currentProductionModel->rowCount(QModelIndex());
_currentProductionModel->insertRow(row);
_currentProductionModel->setData(_currentProductionModel->index(row, 0), QVariant::fromValue((void*)var), Qt::UserRole);
}
}
void DialogCreateVariable::on_btnRemoveFromProduction_clicked() {
if(!_currentProductionModel) return;
foreach(QModelIndex index, _ui->producitionVariableView->selectionModel()->selectedIndexes()) {
_currentProductionModel->removeRow(index.row());
}
}
// Set up the ability for the production with the specified index to be used.
void DialogCreateVariable::on_productionsView_clicked(const QModelIndex& index) {
_currentProductionIndex = index;
Production* production = (Production*)_ui->productionsView->model()->data(index, Qt::UserRole).value<void*>();
_ui->producitionVariableView->setModel(production->getVariableSequenceModel());
_currentProductionModel = production->getVariableSequenceModel();
_ui->txtProductionName->setText(production->getName());
}
void DialogCreateVariable::on_btnBoxOkCancel_accepted() {
//_ui->lblErorMessage->setText("Accepting!");
if(_currentMode == CREATE && _lSystem->variableExists(_variable->getName())) {
_ui->lblErorMessage->setText("Variable name is not unique.");
return;
}
ProductionTableModel* model = (ProductionTableModel*)_ui->productionsView->model();
for(int i = 0; i < model->rowCount(QModelIndex()); i++) {
Production* production = (Production*)model->data(model->index(i, 0), Qt::UserRole).value<void*>();
if(production->getSequence()->length() == 0) {
_ui->lblErorMessage->setText("Production " + production->getName() + " does not contain any variables.");
return;
}
}
_ui->lblErorMessage->setText("Accepting!");
if(_currentMode == CREATE) {
_lSystem->addVariable(_variable);
}
emit done();
}
void DialogCreateVariable::on_btnBoxOkCancel_rejected() {
delete _variable;
emit cancel();
}
void DialogCreateVariable::on_txtProductionName_textEdited(const QString& newName) {
_ui->productionsView->model()->setData(_currentProductionIndex, newName, Qt::DisplayRole);
}
void DialogCreateVariable::on_txtVarName_textEdited(const QString& name) {
switch(_currentMode) {
case CREATE:
_variable->setName(name);
break;
case EDIT:
if(name != _originalName) {
if(_lSystem->variableExists(name)) {
_ui->txtVarName->setText(_originalName);
} else {
_lSystem->changeVariableName(_originalName, name);
_originalName = name;
}
}
break;
}
}