-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogicline.cpp
132 lines (106 loc) · 3.49 KB
/
logicline.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
#include "logicline.h"
static std::map<std::string, Visible_Block*>* *ptrToVBPool {nullptr};
void setMainWindowVBPool(std::map<std::string, Visible_Block*>* *ppptr) { ptrToVBPool = ppptr; }
LogicLine::LogicLine(QWidget *pa, QPoint stt, QPoint end) : QLabel(pa), start {stt}, end {end}
{
setStyleSheet("border : none");
setStyleSheet("background-color : none");
setAutoFillBackground(false);
setGeometry(QRect(20, 20, 150, 80));
setVisible(true);
installEventFilter(this);
QPoint lefttop;
lefttop.setX(std::max(0, std::min(stt.x(), end.x()) - 10));
lefttop.setY(std::max(0, std::min(stt.y(), end.y()) - 10));
int width = abs(stt.x() - end.x()) + 20;
int height = abs(stt.y() - end.y()) + 20;
setGeometry(QRect(lefttop.x(), lefttop.y(), width, height));
start = stt - lefttop;
this->end = end - lefttop;
checkAllLineInPool();
update();
}
void LogicLine::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
QPixmap image(size());
image.fill(Qt::transparent);
QPainter painter(&image);
QColor color = Qt::green;
color.setAlpha(150);
QColor cl2 {Qt::red};
cl2.setAlpha(150);
QPen peng(color);
QPen penr(cl2);
peng.setWidth(10);
penr.setWidth(10);
painter.setPen(peng);
painter.drawPoint(start); // this way start point green, end point red
painter.setPen(penr);
painter.drawPoint(end);
painter.setPen(peng);
QLineF line(start, end);
peng.setWidth(3);
painter.setPen(peng);
painter.drawLine(line);
setPixmap(image);
setMask(image.mask());
}
bool LogicLine::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 (rect().contains(e->pos()) && e->button() == Qt::RightButton)
{
removeLine(this);
checkAllLineInPool();
}
}
else if (event->type() == QEvent::MouseMove && isHover)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
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);
}
else if (event->type() == QEvent::MouseButtonRelease && isHover)
{
isHover = false;
checkAllLineInPool();
}
return false;
}
void LogicLine::checkConnection()
{
std::map<std::string, Visible_Block*>::iterator *stt {nullptr}, *endd {nullptr};
for (auto i = (**ptrToVBPool).begin(); i != (**ptrToVBPool).end(); ++i)
{
if (i->second->geometry().contains(pos()+start))
{
stt = new std::map<std::string, Visible_Block*>::iterator {i};
}
else if (i->second->geometry().contains(pos()+end))
{
endd = new std::map<std::string, Visible_Block*>::iterator {i};
}
}
if (stt && endd)
{
std::cout << (*stt)->second->getName() << " set " << (*endd)->second->getName() << " as its next" << std::endl;
(*stt)->second->setNextBlock((*endd)->second->getName());
std::cout << (*stt)->second->getName() << " set " << (*endd)->second->getName() << " as its next done" << std::endl;
}
if (stt) delete stt;
if (endd) delete endd;
}