-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWire.cpp
98 lines (89 loc) · 2.57 KB
/
Wire.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
#include "Wire.hpp"
#include "GraphicsView.hpp"
Wire::Wire(QPointF start, QGraphicsItem *parent)
: QObject(), QGraphicsLineItem(),
anchor_start(nullptr), anchor_end(nullptr) {
setZValue(1);
setPen(QPen(Qt::black,2));
pointList.append(start);
// end point
pointList.append(start);
setLine(start.x(), start.y(), start.x(), start.y());
}
void Wire::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
if(m_selected) {
painter->setPen(QPen(Qt::red, 2, Qt::DashLine));
}
else {
painter->setPen(QPen(Qt::black, 2));
painter->setBrush(Qt::NoBrush);
}
painter->drawLine(line());
}
QRectF Wire::boundingRect() const{
return QRectF(pointList.front(), pointList.back()).normalized();
}
void Wire::setSelected(bool selected) {
m_selected = selected;
update();
}
void Wire::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
for (auto view : scene()->views()) {
if (GraphicsView* gview = qobject_cast<GraphicsView*>(view)) {
gview->currentWire = this;
setSelected(true);
break;
}
}
}
}
void Wire::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
}
void Wire::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
}
void Wire::updateGeometry() {
if(anchor_start) {
QPointF startPos = anchor_start->getAnchorPos();
pointList.front() = startPos;
}
if(anchor_end) {
QPointF EndPos = anchor_end->getAnchorPos();
pointList.back() = EndPos;
}
setLine(pointList.front().x(), pointList.front().y(), pointList.back().x(), pointList.back().y());
}
void Wire::PressEvent(QPointF pos) {
snapToGrid(pos, 10);
pointList.front() = pos;
updateGeometry();
prepareGeometryChange();
}
void Wire::MoveEvent(QPointF pos) {
snapToGrid(pos, 10);
pointList.back() = pos;
updateGeometry();
prepareGeometryChange();
}
void Wire::ReleaseEvent(QPointF pos) {
snapToGrid(pos, 10);
pointList.back() = pos;
updateGeometry();
prepareGeometryChange();
}
void Wire::setStartAnchor(AnchorPoint* start) {
anchor_start = start;
if(anchor_start) {
connect(anchor_start, &AnchorPoint::positionChanged,
this, &Wire::updateGeometry);
}
updateGeometry();
}
void Wire::setEndAnchor(AnchorPoint* end) {
anchor_end = end;
if(anchor_end) {
connect(anchor_end, &AnchorPoint::positionChanged,
this, &Wire::updateGeometry);
}
updateGeometry();
}