-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawpalette.cpp
97 lines (84 loc) · 2.39 KB
/
drawpalette.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
#include "drawpalette.h"
#include "logicline.h"
static std::vector<LogicLine*> *linePool {nullptr}; // current pool correspond to the current funciton name in canvas
static std::vector<LogicLine*> llll;
void setCurrentLinePool(std::vector<LogicLine*> *ptr) { linePool = ptr; }
void checkAllLineInPool()
{
for (auto i : *linePool)
{
i->checkConnection();
}
}
bool removeLine(LogicLine *l)
{
auto i {std::find(linePool->begin(), linePool->end(), l)};
if (i != linePool->end())
{
linePool->erase(i);
(*i)->deleteLater();
return true;
}
return false;
}
DrawPalette::DrawPalette(QWidget *pa, QPushButton *myBtn): QLabel(pa), myToggleButton {myBtn}, isPainting {false}
{
setAutoFillBackground(false);
setStyleSheet("QLabel { background-color : none; }");
setVisible(false);
setMouseTracking(true);
installEventFilter(this);
}
bool DrawPalette::eventFilter(QObject *, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
if (rect().contains(e->pos())
&& (e->button() == Qt::LeftButton)
&& (e->pos().x() > 24 && (e->pos().y() > 24)))
{
if (isPainting)
{
isPainting = false;
LogicLine *l {new LogicLine {parentWidget(), startPoint, currentPoint}};
linePool->push_back(l);
myToggleButton->toggle();
setVisible(false);
update();
}
else
{
startPoint = currentPoint = e->pos();
isPainting = true;
update();
}
}
}
else if (event->type() == QEvent::MouseMove && isPainting)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
currentPoint = e->pos();
update();
}
return false;
}
void DrawPalette::paintEvent(QPaintEvent *event)
{
setGeometry(parentWidget()->geometry());
QLabel::paintEvent(event);
if (isPainting)
{
QPainter painter(this);
QColor color = Qt::green;
color.setAlpha(150);
QPen pen(color);
pen.setWidth(10);
painter.setPen(pen);
painter.drawPoint(startPoint);
QLineF line(startPoint, currentPoint);
pen.setWidth(3);
painter.setPen(pen);
painter.drawLine(line);
}
}