-
Notifications
You must be signed in to change notification settings - Fork 0
/
EssenseCreateWindow.cpp
433 lines (350 loc) · 10.3 KB
/
EssenseCreateWindow.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
#include "EssenseCreateWindow.h"
EssenceCreateWindow::EssenceCreateWindow(QString id, int type, QWidget *parent) : QWidget(parent)
{
old_id = id;
current_type = type;
regExp.setPattern("[A-Z,a-z,А-Я,а-я]{1,15}");
validator.setRegExp(regExp);
createWindow();
if(id == "")
{
editMode = false;
ConsoleOutput::getInstance()->printStartActionInfo("Создаётся новая сущность");
}
else
{
ConsoleOutput::getInstance()->printStartActionInfo("Редактируется существующая сущность");
editMode = true;
loadData(id);
}
}
bool EssenceCreateWindow::loadData(QString id)
{
keys.clear();
attributes.clear();
// qDebug() << "load\n";
// EssenceDataController::instance()->getInfoAboutEssence(id);
EREssenceData * p_data = DataController::getInstance()->search(id);
old_data = std::make_tuple(p_data->getId(), p_data->getType(), p_data->getKeys(), p_data->getAttributes());
old_attrs = p_data->getAttributes();
old_keys = p_data->getKeys();
this->name.setText(std::get<0>(old_data));
// for(int i = 0; i < std::get<2>(old_data).length(); i++)
// {
// if(std::get<2>(old_data).at(i).contains("::") == true || (std::get<2>(old_data).at(i).contains("_id") == true))
// {
//// QTableWidgetItem * it = new QTableWidgetItem(std::get<2>(old_data).at(i));
// QLineEdit * it = new QLineEdit(std::get<2>(old_data).at(i));
// it->setDisabled(true);
//// it->setFlags(it->flags() & ~Qt::ItemIsEditable);
//// keys.setItem(i,0, it);
// it->setValidator(&validator);
// keys.setCellWidget(i, 0, it);
// it = nullptr;
// }
// else
// {
// QLineEdit * it = new QLineEdit(std::get<2>(old_data).at(i));
// it->setValidator(&validator);
// keys.setCellWidget(i,0, it);
// }
// }
// for(int i = 0; i < std::get<3>(old_data).length(); i++)
// {
// attributes.setItem(i,0, new QTableWidgetItem(std::get<3>(old_data).at(i)));
// }
//keys col creation
for(int i = 0; i < keys.rowCount(); i++)
{
if(i < std::get<2>(old_data).length())
{
if(std::get<2>(old_data).at(i).contains("::") == true || (std::get<2>(old_data).at(i).contains("_id") == true))
{
QLineEdit * it = new QLineEdit(std::get<2>(old_data).at(i));
it->setDisabled(true);
it->setValidator(&validator);
keys.setCellWidget(i, 0, it);
it = nullptr;
}
else
{
QLineEdit * it = new QLineEdit(std::get<2>(old_data).at(i));
it->setValidator(&validator);
keys.setCellWidget(i, 0, it);
it = nullptr;
}
}
else
{
QLineEdit * it = new QLineEdit();
it->setValidator(&validator);
keys.setCellWidget(i, 0, it);
it = nullptr;
}
}
//attrs col creation
for(int i = 0; i < attributes.rowCount(); i++)
{
if(i < std::get<3>(old_data).length())
{
if(std::get<3>(old_data).at(i).contains("::") == true || (std::get<3>(old_data).at(i).contains("_id") == true))
{
QLineEdit * it = new QLineEdit(std::get<3>(old_data).at(i));
it->setDisabled(true);
it->setValidator(&validator);
attributes.setCellWidget(i, 0, it);
it = nullptr;
}
else
{
QLineEdit * it = new QLineEdit(std::get<3>(old_data).at(i));
it->setValidator(&validator);
attributes.setCellWidget(i, 0, it);
it = nullptr;
}
}
else
{
QLineEdit * it = new QLineEdit();
it->setValidator(&validator);
attributes.setCellWidget(i, 0, it);
it = nullptr;
}
}
}
void EssenceCreateWindow::createWindow()
{
setWindowFlags( ( (this->windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint));
attributes.setRowCount(200);
attributes.setColumnCount(1);
attributes.setColumnWidth(0, 400);
QHeaderView * headKeysAttribut = new QHeaderView(Qt::Horizontal);
headKeysAttribut->setSectionResizeMode(QHeaderView::Stretch);
attributes.setHorizontalHeader(headKeysAttribut);
QTableWidgetItem* item1 = new QTableWidgetItem("Название");
item1->setText("Название");
attributes.setHorizontalHeaderItem(0, item1);
keys.setRowCount(50);
keys.setColumnCount(1);
QHeaderView * headKeys = new QHeaderView(Qt::Horizontal);
headKeys->setSectionResizeMode(QHeaderView::Stretch);
keys.setHorizontalHeader(headKeys);
QTableWidgetItem* item0 = new QTableWidgetItem("Название");
item0->setText("Название");
keys.setHorizontalHeaderItem(0, item0);
submit.setText("Готово");
cancel.setText("Отмена");
remove.setText("Удалить");
this->setWindowTitle("Создание сущности");
lay.addWidget(new QLabel("Имя"), 0,0);
name.setValidator(&validator);
lay.addWidget(&name, 1,0);
lay.addWidget(&remove, 1,1);
lay.addWidget(new QLabel("Ключи"), 2,0);
lay.addWidget(&keys, 3,0);
lay.addWidget(new QLabel("Атрибуты"), 2,1);
lay.addWidget(&attributes, 3,1);
lay.addWidget(&submit, 4,1);
lay.addWidget(&cancel, 4,0);
QObject::connect(&submit, SIGNAL(clicked(bool)), this, SLOT(submitCreation(bool)));
QObject::connect(&cancel, SIGNAL(clicked(bool)), this, SLOT(cancelCreation(bool)));
QObject::connect(&remove, SIGNAL(clicked()), this, SLOT(removeEssence()));
keys.clearContents();
attributes.clearContents();
for(int i = 0; i < keys.rowCount(); i++)
{
QLineEdit * it = new QLineEdit();
// it->setDisabled(true);
// it->setFlags(it->flags() & ~Qt::ItemIsEditable);
// keys.setItem(i,0, it);
it->setValidator(&validator);
keys.setCellWidget(i, 0, it);
}
for(int i = 0; i < attributes.rowCount(); i++)
{
QLineEdit * it = new QLineEdit();
// it->setDisabled(true);
// it->setFlags(it->flags() & ~Qt::ItemIsEditable);
// keys.setItem(i,0, it);
it->setValidator(&validator);
attributes.setCellWidget(i, 0, it);
}
this->setLayout(&lay);
}
void EssenceCreateWindow::submitCreation(bool)
{
// old_attrs.clear();
// old_keys.clear();
ConsoleOutput::getInstance()->printStartActionInfo("Проверка введенных данных");
QString new_id = this->name.text();
QList<QString> new_keys;
QList<QString> new_attributes;
for(int i = 0; i < this->keys.rowCount(); i++)
{
if(this->keys.cellWidget(i,0) == nullptr)
{
continue;
}
else
{
QLineEdit * data = (QLineEdit*)keys.cellWidget(i,0);
if(data->text() == "")
{
continue;
}
new_keys.append(data->text());
}
}
for(int i = 0; i < this->attributes.rowCount(); i++)
{
if(this->attributes.cellWidget(i,0) == nullptr)
{
continue;
}
else
{
QLineEdit * data = (QLineEdit*)attributes.cellWidget(i,0);
if(data->text() == "")
{
continue;
}
new_attributes.append(data->text());
}
}
if(editMode == true)
{
ConsoleOutput::getInstance()->printStartActionInfo("\nПроверка при редактировании сущности");
// qDebug() <<"Old_keys: "<<old_keys;
// qDebug() <<"old_attrs: "<<old_attrs;
// qDebug() <<"old_id: "<<old_id;
// qDebug() <<"new_keys: " << new_keys;
// qDebug() <<"new_attrs: "<< new_attributes;
// qDebug() <<"new_id: " << new_id;
int error_dubl_key = 0;
int error_dubl_attr = 0;
int error_add_key = 0;
int error_add_attr = 0;
int error_remove_key = 0;
int error_remove_attr = 0;
//add new keys
foreach (QString key, new_keys)
{
if(old_keys.contains(key) == false)
{
error_add_key = DataController::getInstance()->addKey(old_id, key);
if(error_add_key != 0)
{
error_add_key = 1;
break;
}
}
if(new_keys.count(key) > 1)
{
ConsoleOutput::getInstance()->printUserError("Ключ: " + key + "Дуюлируется. Проверьте ошибки");
error_dubl_key = 1;
break;
}
}
//add new attrs
foreach (QString attr, new_attributes)
{
if(old_attrs.contains(attr) == false)
{
error_add_attr = DataController::getInstance()->addAttribute(old_id, attr);
if(error_add_attr != 0)
{
error_add_attr = 1;
break;
}
}
if(new_attributes.count(attr) > 1)
{
ConsoleOutput::getInstance()->printUserError("Атрибут: " + attr + "Дуюлируется. Проверьте ошибки");
error_dubl_attr = 1; break;
}
}
//remove key
foreach (QString key, old_keys)
{
if(new_keys.contains(key) == false)
{
error_remove_key = DataController::getInstance()->removeKey(old_id, key);
if(error_remove_key != 0)
{
error_remove_key = 1;
break;
}
}
}
//remove attrs
foreach (QString attr, old_attrs)
{
if(new_attributes.contains(attr) == false)
{
error_remove_attr = DataController::getInstance()->removeAttribute(old_id, attr);
if(error_remove_attr != 0)
{
error_remove_attr = 1;
break;
}
}
}
int error_rename = 0;
if(old_id != new_id)
{
error_rename = DataController::getInstance()->renameEssence(old_id, new_id);
if(error_rename != 0)
{
error_rename = 1;
}
}
if(error_rename == 1 || error_add_attr == 1 ||
error_add_key == 1 || error_dubl_attr == 1 ||
error_dubl_key == 1 || error_remove_attr == 1 ||
error_remove_key == 1)
{
loadData(old_id);
// qDebug() << "error_dubl_key " << error_dubl_key ;
// qDebug() << "error_dubl_attr " << error_dubl_attr ;
// qDebug() << "error_add_key " << error_add_key ;
// qDebug() << "error_add_attr " << error_add_attr ;
// qDebug() << "error_remove_key " << error_remove_key ;
// qDebug() << "error_remove_attr" << error_remove_attr ;
ConsoleOutput::getInstance()->printUserError("Проверка не пройдена");
}
else
{
ConsoleOutput::getInstance()->printInfo("Проверка пройдена");
emit endSuccessEditation(new_id);
this->close();
}
}
if(editMode == false)
{
ConsoleOutput::getInstance()->printStartActionInfo("Проверка при создании сущности");
int error = DataController::getInstance()->createEssence(new_id, current_type, new_keys, new_attributes);
// DataController::getInstance()->printAllEssence();
if(error == 0)
{
ConsoleOutput::getInstance()->printInfo("Проверка пройдена");
emit endSuccessCreation(new_id);
this->close();
}
}
}
void EssenceCreateWindow::cancelCreation(bool)
{
ConsoleOutput::getInstance()->printSystemMassage("Работа с обьектом прервана");
emit exitFromCreateWindow();
this->close();
}
void EssenceCreateWindow::removeEssence()
{
int error = 0;
error = DataController::getInstance()->removeEssence(old_id);
if(error == 0)
{
emit endDeletetion(old_id);
this->close();
}
}