-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisible_Block.cpp
628 lines (553 loc) · 20.7 KB
/
Visible_Block.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
#include "Visible_Block.hpp"
#include "QImage"
#include "QPainter"
#include "QPen"
#include "QInputDialog"
#include "dataStorage.hpp"
#include <typeinfo>
#include "QDebug"
#include "drawpalette.h"
using namespace dataStorage;
/**
* @brief Visible_Block::eventFilter
* @param event
* handle all the event of visible blk:
* 1.drag
* 2.right click show context menu
* 3.check line connection
*/
bool Visible_Block::eventFilter(QObject *, QEvent *event)
{
static QPoint lastPoint;
static bool isHover = false;
if(event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
if(rect().contains(e->pos()) && e->button() == Qt::LeftButton)
{
lastPoint = e->pos();
isHover = true;
if(block_type == USER_FUNC)
{
emit user_func_clicked(name);
}
}
if(rect().contains(e->pos()) && e->button() == Qt::RightButton)
{
emit customContextMenuRequested(e->pos());
}
}
else if(event->type() == QEvent::MouseMove && isHover)
{
QMouseEvent*e = static_cast<QMouseEvent*>(event);
if(movable)
{
int dx = e->pos().x() - lastPoint.x();
int dy = e->pos().y() - lastPoint.y();
int new_x = x() + dx;
int new_y = y() + dy;
move(new_x, new_y);
block_position = this->pos();
}
}
else if(event->type() == QEvent::MouseButtonRelease)
{
if(isHover) isHover = false;
checkAllLineInPool();
}
return false;
}
/**
* @brief Visible_Block::ShowContextMenu
* @param pos
*
* handle right click context menu
* such as delete and show details and setAtrributes
* Very Important!
*/
void Visible_Block::ShowContextMenu(const QPoint& pos)
{
QMenu myMenu("my menu", this);
myMenu.addAction(tr("Delete"), this, SLOT(deleteBlock()));
myMenu.addAction(tr("Details"), this, SLOT(showDetails()));
if(block_type == Visible_Block_type::AND ||
block_type == Visible_Block_type::BIGGERTHAN ||
block_type == Visible_Block_type::DIVISION ||
block_type == Visible_Block_type::EQUALCOMPARE ||
block_type == Visible_Block_type::MINUS ||
block_type == Visible_Block_type::MULTIPLY ||
block_type == Visible_Block_type::OR ||
block_type == Visible_Block_type::PLUS ||
block_type == Visible_Block_type::SMALLERTHAN)
{
myMenu.addAction(tr("Operand1"), this, SLOT(setSource1()));
myMenu.addAction(tr("Operand2"), this, SLOT(setSource2()));
myMenu.addAction(tr("Destination"), this, SLOT(setDestination()));
}
else if(block_type == Visible_Block_type::ASSIGNMENT ||
block_type == Visible_Block_type::NOT)
{
myMenu.addAction(tr("Operand1"), this, SLOT(setSource1()));
myMenu.addAction(tr("Destination"), this, SLOT(setDestination()));
}
else if(block_type == Visible_Block_type::TAKEINDGET ||
block_type == Visible_Block_type::TAKEINDSET)
{
myMenu.addAction(tr("Array name"), this, SLOT(setSource1()));
myMenu.addAction(tr("Array Index"), this, SLOT(setArrInd()));
myMenu.addAction(tr("Destination"), this, SLOT(setDestination()));
}
else if(block_type == Visible_Block_type::IF)
{
myMenu.addAction(tr("Bool Operand"), this, SLOT(setSource1()));
myMenu.addAction(tr("IF true"), this, SLOT(setSubFunc1()));
myMenu.addAction(tr("IF false"), this, SLOT(setSubFunc2()));
}
else if(block_type == Visible_Block_type::WHILE)
{
myMenu.addAction(tr("Bool Operand"), this, SLOT(setSource1()));
myMenu.addAction(tr("Loop Func"),this, SLOT(setSubFunc1()));
}
else if(block_type == Visible_Block_type::USER_FUNC && !movable)
{
myMenu.addAction(tr("Edit Function"), this, SLOT(editFunc()));
}
else if(block_type == Visible_Block_type::USER_FUNC && movable)
{
myMenu.addAction("Operand Modify", this, SLOT(setUserFuncOperands()));
}
myMenu.exec(mapToGlobal(pos));
}
/**
* @brief Visible_Block::showDetails
*
* update std::string details and show it in the pop up window
*/
void Visible_Block::showDetails()
{
currentBlockVerify();
details = "block name: " + name + "\nMother: " + motherFunc->name;
if(block_type == USER_FUNC)
{
details += "\nparam typeInfo: ";
for(std::vector<std::string>::iterator paramTypeIter = funcParamTypeInfo.begin();
paramTypeIter != funcParamTypeInfo.end();
paramTypeIter++)
{
details += *paramTypeIter + " ";
}
}
if(block_type != USER_VAR) details += "\nVerify Info" + verifyInfo;
else
{
std::string typeName;
if(typeid(*motherFunc) == typeid(Var_Block<bool>)) typeName = "bool";
else if(typeid(*motherFunc) == typeid(Var_Block<int>)) typeName = "int";
else if(typeid(*motherFunc) == typeid(Var_Block<double>)) typeName = "double";
else if(typeid(*motherFunc) == typeid(Var_Block<bool*>)) typeName = "bool arr";
else if(typeid(*motherFunc) == typeid(Var_Block<int*>)) typeName = "int arr";
else if(typeid(*motherFunc) == typeid(Var_Block<double*>)) typeName = "double arr";
details += "\nVar type : " + typeName;
}
QMessageBox showDetailsBox(QMessageBox::Information, "showDetails",QString::fromStdString(details));
showDetailsBox.exec();
}
void Visible_Block::deleteBlock()
{
// seems strange...
emit visible_block_delete(name);
this->deleteLater();
checkAllLineInPool();
}
void Visible_Block::setSource1()
{
QString inputText = QInputDialog::getText(this, "Source1", "source1 variable name",
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
operand_source1 = var_pool[inputName];
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\nsource 1 unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setSource2()
{
QString inputText = QInputDialog::getText(this, "Source2", "source2 variable name",
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
operand_source2 = var_pool[inputName];
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\nsource 2 unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setDestination()
{
QString inputText = QInputDialog::getText(this, "Destination", "destination variable name",
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
operand_destination = var_pool[inputName];
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n Destination unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setArrInd()
{
int inputValue = QInputDialog::getInt(this, "Array Index", "array index:",0, 0);
//====================fake a int var=============================
std::string fake_name = getAvailNameFake();
var_pool[fake_name] = new Var_Block<int>(fake_name, inputValue);
indexFakeName = fake_name;
//====================fake a int var=============================
if(operand_source1 != nullptr)
{
if(typeid(*operand_source1) == typeid(Var_Block<bool*>))
{
if(static_cast<unsigned int>(inputValue) < static_cast<Var_Block<bool*>*>(operand_source1)->getSize())
{
arrIndex = inputValue;
}
}
if(typeid(*operand_source1) == typeid(Var_Block<int*>))
{
if(static_cast<unsigned int>(inputValue) < static_cast<Var_Block<int*>*>(operand_source1)->getSize())
{
arrIndex = inputValue;
}
}
if(typeid(*operand_source1) == typeid(Var_Block<double*>))
{
if(static_cast<unsigned int>(inputValue) < static_cast<Var_Block<double*>*>(operand_source1)->getSize())
{
arrIndex = inputValue;
}
}
}
if(arrIndex != inputValue)
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(std::to_string(inputValue) + " is invalid,\narray index unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setSubFunc1()
{
QString inputText = QInputDialog::getText(this, "SubFunction1", "function name",
QLineEdit::Normal, "FUNC2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = func_pool.find(inputName);
if(iter != func_pool.end())
{
subFunction1 = func_pool[inputName];
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n subFunction1 unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setSubFunc2()
{
QString inputText = QInputDialog::getText(this, "SubFunction2", "function name",
QLineEdit::Normal, "FUNC2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = func_pool.find(inputName);
if(iter != func_pool.end())
{
subFunction2 = func_pool[inputName];
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n subFunction2 unchanged"));
warningBox.exec();
}
currentBlockVerify();
}
void Visible_Block::setUserFuncOperands(int pos)
{
//=============================== get postion =======================================
if(pos < 0)
{
pos = QInputDialog::getInt(this, "Set Operands", "Operands position(start from 0):",0, 0);
}
//=============================== get postion =======================================
//=============== fill up at first time, prevent retrieve vector null iter ==========
if(userFuncOperands.empty())
{
for(int i = 0; i < userFuncOperandsNum; i++)
userFuncOperands.push_back(nullptr);
}
//=============== fill up at first time, prevent retrieve vector null iter ==========
std::vector<Block*>::iterator operIter = userFuncOperands.begin() + pos;
QString inputText = QInputDialog::getText(this, "Set Operands", "Setting Operand #" + QString::number(pos),
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
// replace the origin operand with the new operand
std::replace(operIter, operIter+1, *operIter, var_pool[inputName]);
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n" +
"Setting Operand #" + std::to_string(pos) + " unchanged"));
warningBox.exec();
}
userFuncOperands.shrink_to_fit();
currentBlockVerify();
}
void Visible_Block::setUserFuncOperandsT(int pos)
{
//=============================== get postion =======================================
if(pos < 0)
{
pos = QInputDialog::getInt(this, "Set OperandsT", "OperandsT position(start from 0):",0, 0);
}
//=============================== get postion =======================================
//=============== fill up at first time, prevent retrieve vector null iter ==========
if(userFuncOperandsT.empty())
{
for(int i = 0; i < userFuncOperandsNumT; i++)
userFuncOperandsT.push_back(nullptr);
}
//=============== fill up at first time, prevent retrieve vector null iter ==========
std::vector<Block*>::iterator operIter = userFuncOperandsT.begin() + pos;
QString inputText = QInputDialog::getText(this, "Set OperandsT", "Setting Operand #" + QString::number(pos),
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
// replace the origin operand with the new operand
std::replace(operIter, operIter+1, *operIter, var_pool[inputName]);
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n" +
"Setting Operand #" + std::to_string(pos) + " unchanged"));
warningBox.exec();
}
userFuncOperandsT.shrink_to_fit();
currentBlockVerify();
}
void Visible_Block::setUserFuncOperandsF(int pos)
{
//=============================== get postion =======================================
if(pos < 0)
{
pos = QInputDialog::getInt(this, "Set OperandsF", "OperandsT position(start from 0):",0, 0);
}
//=============================== get postion =======================================
//=============== fill up at first time, prevent retrieve vector null iter ==========
if(userFuncOperandsF.empty())
{
for(int i = 0; i < userFuncOperandsNumF; i++)
userFuncOperandsF.push_back(nullptr);
}
//=============== fill up at first time, prevent retrieve vector null iter ==========
std::vector<Block*>::iterator operIter = userFuncOperandsF.begin() + pos;
QString inputText = QInputDialog::getText(this, "Set OperandsF", "Setting Operand #" + QString::number(pos),
QLineEdit::Normal, "VAR2333");
std::string inputName = inputText.toStdString();
std::map<std::string, WriteBackend::Block*>::iterator iter;
iter = var_pool.find(inputName);
if(iter != var_pool.end())
{
// replace the origin operand with the new operand
std::replace(operIter, operIter+1, *operIter, var_pool[inputName]);
}
else
{
QMessageBox warningBox(QMessageBox::Information, "warning",
QString::fromStdString(inputName + " does not exist\n" +
"Setting Operand #" + std::to_string(pos) + " unchanged"));
warningBox.exec();
}
userFuncOperandsF.shrink_to_fit();
currentBlockVerify();
}
void Visible_Block::editFunc()
{
emit user_func_edit(name);
}
bool Visible_Block::currentBlockVerify()
{
std::string info_wrong = "\nlack components:";
std::string info_right = "\nverify ok";
bool status = true;
if(block_type == Visible_Block_type::AND ||
block_type == Visible_Block_type::BIGGERTHAN ||
block_type == Visible_Block_type::DIVISION ||
block_type == Visible_Block_type::EQUALCOMPARE ||
block_type == Visible_Block_type::MINUS ||
block_type == Visible_Block_type::MULTIPLY ||
block_type == Visible_Block_type::OR ||
block_type == Visible_Block_type::PLUS ||
block_type == Visible_Block_type::SMALLERTHAN)
{
if(operand_source1 == nullptr ||
operand_source2 == nullptr ||
operand_destination == nullptr)
status = false;
if(operand_source1 == nullptr) info_wrong += "\n lack Operand 1";
if(operand_source2 == nullptr) info_wrong += "\n lack Operand 2";
if(operand_destination == nullptr) info_wrong += "\n lack Destination";
}
else if(block_type == Visible_Block_type::ASSIGNMENT ||
block_type == Visible_Block_type::NOT)
{
if(operand_source1 == nullptr ||
operand_destination == nullptr)
status = false;
if(operand_source1 == nullptr) info_wrong += "\n lack Operand 1";
if(operand_destination == nullptr) info_wrong += "\n lack Destination";
}
else if(block_type == Visible_Block_type::TAKEINDGET ||
block_type == Visible_Block_type::TAKEINDSET)
{
if(operand_source1 == nullptr ||
arrIndex == -1 ||
operand_destination == nullptr)
status = false;
if(operand_source1 == nullptr) info_wrong += "\n lack Operand 1";
if(operand_destination == nullptr) info_wrong += "\n lack Destination";
if(arrIndex == -1) info_wrong += "\n lack arrIndex";
}
else if(block_type == Visible_Block_type::IF)
{
if(operand_source1 == nullptr || subFunction1 == nullptr || subFunction2 == nullptr)
{
status = false;
if(operand_source1 == nullptr) info_wrong += "\n lack Operand 1";
if(subFunction1 == nullptr) info_wrong += "\n lack subFunction 1";
if(subFunction2 == nullptr) info_wrong += "\n lack subFunction 2";
}
std::vector<Block*>::iterator operIter = userFuncOperandsT.begin();
for(int i=0; operIter != userFuncOperandsT.end(); ++operIter)
{
if(*operIter == nullptr)
{
info_wrong += "\n lack "+std::to_string(i)+"-th operand in subfunction True";
status = false;
}
i++;
}
std::vector<Block*>::iterator operIter2 = userFuncOperandsF.begin();
for(int i=0; operIter2 != userFuncOperandsF.end(); ++operIter2)
{
if(*operIter2 == nullptr)
{
info_wrong += "\n lack "+std::to_string(i)+"-th operand in subfunction False";
status = false;
}
i++;
}
}
else if(block_type == Visible_Block_type::WHILE)
{
if(operand_source1 == nullptr || subFunction1 == nullptr)
{
status = false;
if(operand_source1 == nullptr) info_wrong += "\n lack Operand 1";
if(subFunction1 == nullptr) info_wrong += "\n lack subFunction 1";
}
std::vector<Block*>::iterator operIter = userFuncOperandsT.begin();
for(int i=0; operIter != userFuncOperandsT.end(); ++operIter)
{
if(*operIter == nullptr)
{
info_wrong += "\n lack "+std::to_string(i)+"-th operand in subfunction";
status = false;
}
i++;
}
}
else if(block_type == Visible_Block_type::USER_FUNC && movable)
{
std::vector<Block*>::iterator operIter = userFuncOperands.begin();
for(int i = 0; i < userFuncOperandsNum; i++)
{
if(*operIter == nullptr)
{
info_wrong += "\n lack "+std::to_string(i)+"-th operand";
status = false;
}
operIter++;
}
}
else if(block_type == Visible_Block_type::PRINT)
{
if(operand_source1 == nullptr) status = false;
info_wrong += "\n lack Operand1";
}
if(status == false)
{
verifyInfo = info_wrong;
setStyleSheet("background-color : #e34646");
return false;
}
else
{
verifyInfo = info_right;
setStyleSheet("background-color : white");
return true;
}
}
std::string Visible_Block::getAvailNameFake()
{
int id = 0;
std::string availableName;
do{
availableName = "Ind_" + std::to_string(id);
id++;
}while(var_pool.count(availableName));
return availableName;
}
std::string Visible_Block::getUserFuncITHOperName(int index)
{
return userFuncOperands.at(index)->name;
}
std::string Visible_Block::getUserFuncITHOperNameT(int index)
{
return userFuncOperandsT.at(index)->name;
}
std::string Visible_Block::getUserFuncITHOperNameF(int index)
{
return userFuncOperandsF.at(index)->name;
}