-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinknode.cpp
More file actions
135 lines (123 loc) · 4.06 KB
/
linknode.cpp
File metadata and controls
135 lines (123 loc) · 4.06 KB
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
#include <QGraphicsView>
#include "chartscene.h"
#include "linknode.h"
#include "misc.h"
#include "linkline.h"
//Public constructor
LinkNodeItem::LinkNodeItem(int in_x, int in_y, const QColor& normal, const QColor& active, QGraphicsItem* parent):
QGraphicsEllipseItem(0, 0, radius * 2, radius * 2, parent) {
normal_color = normal;
active_color = active;
if (normal_color == Qt::transparent) normal_pen = Qt::NoPen;
setBrush(normal_color);
setPen(normal_pen);
setCenterPos(QPoint(in_x, in_y));
setZValue(1);
ChartScene* chart_scene;
if (scene() && (chart_scene = dynamic_cast<ChartScene*>(scene()))) {
grid_size = chart_scene->gridSize();
} else grid_size = 0;
}
//Public
QPointF LinkNodeItem::gridSnapOffset() const {
if (grid_size) return roundTo(sceneCenterPos(), grid_size) - sceneCenterPos();
else return QPointF();
}
//Public
bool LinkNodeItem::updateConnections() const {
const QGraphicsItem* cur_item;
const LinkNodeItem* cur_node;
bool now_highlighted = false;
connected_nodes.clear();
foreach (cur_item, collidingItems(Qt::IntersectsItemBoundingRect)) {
cur_node = qgraphicsitem_cast<const LinkNodeItem*>(cur_item);
if (cur_node) {
now_highlighted = true;
connected_nodes.append(cur_node);
}
}
bool highlight_changed = (now_highlighted != node_highlighted);
node_highlighted = now_highlighted;
return highlight_changed;
}
//Public virtual
void LinkNodeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
if (updateConnections()) {
setBrush(node_highlighted ? active_color : normal_color);
setPen(node_highlighted ? QPen(): normal_pen);
}
QGraphicsEllipseItem::paint(painter, option, widget);
}
//Protected virtual
void LinkNodeItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
if (event->button() == Qt::LeftButton) {
event->accept();
if (!drawing) {
drawing = true;
grabMouse();
foreach(QGraphicsView* view, scene()->views()) {
view->setMouseTracking(true);
}
last_corner = sceneCenterPos();
} else {
nextCursorLine();
last_corner = roundTo(event->scenePos(), grid_size);
}
} else if (event->button() == Qt::RightButton) {
event->accept();
ungrabMouse();
nextCursorLine();
//TODO: Do something with these pointers!
line_segments.clear();
drawing = false;
}
}
//Protected virtual
void LinkNodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
event->accept();
drawCursorLine(event->scenePos());
}
//Protected virtual
void LinkNodeItem::wheelEvent(QGraphicsSceneWheelEvent* event) {
event->accept();
x_first = !x_first;
drawCursorLine(event->scenePos());
}
//Protected virtual
QVariant LinkNodeItem::itemChange(GraphicsItemChange change, const QVariant& value) {
if (change == ItemSceneChange) {
if (ChartScene* chart_scene = dynamic_cast<ChartScene*>(value.value<QGraphicsScene*>())) {
grid_size = chart_scene->gridSize();
} else grid_size = 0;
}
return QGraphicsItem::itemChange(change, value);
}
//Private
void LinkNodeItem::drawCursorLine(const QPointF& to_point) {
if (line_1) {
delete line_1;
line_1 = NULL;
}
if (line_2) {
delete line_2;
line_2 = NULL;
}
QPointF event_grid_pos = roundTo(to_point, grid_size);
QPointF corner_pos(x_first ? event_grid_pos.x() : last_corner.x(),
x_first ? last_corner.y() : event_grid_pos.y());
if (last_corner != corner_pos) {
line_1 = new LinkLineItem(QLineF(last_corner, corner_pos));
scene()->addItem(line_1);
}
if (corner_pos != event_grid_pos) {
line_2 = new LinkLineItem(QLineF(corner_pos, event_grid_pos));
scene()->addItem(line_2);
}
}
//Private
void LinkNodeItem::nextCursorLine() {
line_segments.append(line_1);
line_1 = NULL;
line_segments.append(line_2);
line_2 = NULL;
}