-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinepicker.cpp
242 lines (201 loc) · 6.04 KB
/
linepicker.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
#include "linepicker.h"
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include "lineelement.h"
#include "basespace.h"
#include "commondef.h"
LinePicker::LinePicker(LineElement *element):
BasePicker(element),
m_currentArchor(-1),
m_lineElement(element)
{
}
LinePicker::~LinePicker()
{
QVector<LinePickerArchor *>::iterator it = m_archors.begin();
for(;it!=m_archors.end();++it){
if (*it){
delete (*it);
}
}
}
void LinePicker::setByElement()
{
if (m_lineElement->m_points.size()<m_archors.size()){
QVector<LinePickerArchor *> archorsNew;
for(int i = 0;i<m_archors.size();++i){
LinePickerArchor * archor = m_archors.at(i);
if (m_lineElement->m_points.indexOf(archor->point())<0){
delete archor;
}
else {
archorsNew.append(archor);
}
}
m_archors = archorsNew;
}
QPointF pickerPoint;
for(int i = 0; i<m_lineElement->m_points.size(); ++i){
const QPointF & elementPoint = m_lineElement->m_points.at(i);
LinePickerArchor* archor = 0;
if (m_archors.size()<i+1){
archor = new LinePickerArchor(this);
m_archors.append(archor);
}
else{
archor = m_archors.at(i);
}
pickerPoint = elementToPicker(elementPoint);
archor->setPoint(elementPoint);
archor->setPos(pickerPoint);
}
Q_ASSERT(m_lineElement->m_points.size()==m_archors.size());
this->prepareGeometryChange();
}
void LinePicker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(painter)
Q_UNUSED(option)
Q_UNUSED(widget)
#ifdef DRAW_PICKER_BOUNDING
painter->setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::gray);
pen.setWidth(0);
pen.setCapStyle(Qt::SquareCap);
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
QRectF bounding = this->boundingRect();
painter->drawRect(bounding.x(),bounding.y(),bounding.width(),bounding.height());
#endif
}
QRectF LinePicker::boundingRect() const
{
QRectF r;
QVector<LinePickerArchor *>::const_iterator it = m_archors.begin();
for(;it!=m_archors.end();++it){
const LinePickerArchor * archor = *it;
if (*it){
r = r.united(this->mapFromItem(archor,archor->boundingRect()).boundingRect());
}
}
return r;
}
void LinePicker::setCurrentArchor(LinePickerArchor *archor)
{
int i = m_archors.indexOf(archor);
m_currentArchor = i;
}
void LinePicker::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (m_currentArchor>-1){
QPointF eventPos = event->pos();
QPointF elementPos = pickerToElement(eventPos);
LinePickerArchor * archor = m_archors.at(m_currentArchor);
m_lineElement->replacePoint(archor->point(),elementPos);
archor->setPoint(elementPos);
archor->setPos(this->mapToParent(eventPos));
//qDebug()<<this->mapToParent(event->pos());
//this->setByElement();
this->prepareGeometryChange();
}
}
void LinePicker::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (m_currentArchor>-1){
space()->setPicking(true);
element()->beginAdjust();
event->accept();
}
}
void LinePicker::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (m_currentArchor>-1){
space()->setPicking(false);
element()->endAdjust();
event->accept();
}
}
LinePickerArchor::LinePickerArchor(LinePicker *parent):QGraphicsItem(parent),
m_boundingRect(-DEFAULT_ARCHOR_WIDTH,-DEFAULT_ARCHOR_WIDTH,DEFAULT_ARCHOR_WIDTH*2,DEFAULT_ARCHOR_WIDTH*2),
m_hovered(false),
m_picker(parent)
{
this->setAcceptHoverEvents(true);
}
void LinePickerArchor::setPicked(bool arg1)
{
m_picked = arg1;
}
void LinePickerArchor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::gray);
pen.setWidth(0);
pen.setCapStyle(Qt::SquareCap);
painter->setPen(pen);
QBrush brush(m_hovered?Qt::blue:Qt::white);
painter->setBrush(brush);
painter->setPen(pen);
qreal w2 = DEFAULT_ARCHOR_WIDTH * 2;
QRectF rect(-DEFAULT_ARCHOR_WIDTH+0.5,
-DEFAULT_ARCHOR_WIDTH+0.5,
w2-1,
w2-1);
painter->drawEllipse(rect);
#ifdef DRAW_PICKER_ARCHOR_BOUNDING
painter->setRenderHint(QPainter::Antialiasing,true);
pen.setColor(Qt::gray);
pen.setWidth(0);
pen.setCapStyle(Qt::SquareCap);
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
QRectF bounding = this->boundingRect();
painter->drawRect(bounding.x(),bounding.y(),bounding.width(),bounding.height());
#endif
}
QRectF LinePickerArchor::boundingRect() const
{
return m_boundingRect;
}
void LinePickerArchor::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button()==Qt::LeftButton){
m_picked = true;
m_picker->setCurrentArchor(this);
}
event->ignore();
}
void LinePickerArchor::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button()==Qt::LeftButton){
m_picked=false;
m_picker->setCurrentArchor(-1);
}
event->ignore();
}
void LinePickerArchor::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
m_hovered = false;
qDebug()<<"hover leave";
this->update();
event->ignore();
}
void LinePickerArchor::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
m_hovered = true;
this->update();
event->ignore();
}
void LinePickerArchor::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
Q_UNUSED(event)
this->update();
event->ignore();
}