FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodeeditor/src/GraphicsView.cpp at bt · BehaviorTree/nodeeditor · GitHub
BehaviorTree
/
nodeeditor
Public
forked from
l3enQ/nodeeditor
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodeeditor
/
src
/
GraphicsView.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
425 lines (331 loc) · 11.1 KB
Breadcrumbs
nodeeditor
/
src
/
GraphicsView.cpp
Copy path
File metadata and controls
425 lines (331 loc) · 11.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#
include
"
GraphicsView.hpp
"
#
include
"
BasicGraphicsScene.hpp
"
#
include
"
ConnectionGraphicsObject.hpp
"
#
include
"
NodeGraphicsObject.hpp
"
#
include
"
StyleCollection.hpp
"
#
include
"
UndoCommands.hpp
"
#
include
<
QtWidgets/QGraphicsScene
>
#
include
<
QtGui/QBrush
>
#
include
<
QtGui/QPen
>
#
include
<
QtWidgets/QMenu
>
#
include
<
QtCore/QDebug
>
#
include
<
QtCore/QPointF
>
#
include
<
QtCore/QRectF
>
#
include
<
QtOpenGL
>
#
include
<
QtWidgets
>
#
include
<
cmath
>
#
include
<
iostream
>
using
QtNodes::BasicGraphicsScene;
using
QtNodes::GraphicsView;
GraphicsView::GraphicsView
(QWidget *parent)
: QGraphicsView(parent)
, _clearSelectionAction(
Q_NULLPTR
)
, _deleteSelectionAction(
Q_NULLPTR
)
, _duplicateSelectionAction(
Q_NULLPTR
)
, _copySelectionAction(
Q_NULLPTR
)
, _pasteAction(
Q_NULLPTR
)
{
setDragMode
(QGraphicsView::ScrollHandDrag);
setRenderHint
(QPainter::Antialiasing);
setHorizontalScrollBarPolicy
(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy
(Qt::ScrollBarAlwaysOff);
setTransformationAnchor
(QGraphicsView::AnchorUnderMouse);
setCacheMode
(QGraphicsView::CacheBackground);
setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
auto
const
&flowViewStyle =
StyleCollection::flowViewStyle
();
setBackgroundBrush
(flowViewStyle.
BackgroundColor
);
setScaleRange
(
0.3
,
2
);
//
Sets the scene rect to its maximum possible ranges to avoid autu scene range
//
re-calculation when expanding the all QGraphicsItems common rect.
int
maxSize =
32767
;
setSceneRect
(-maxSize, -maxSize, (maxSize *
2
), (maxSize *
2
));
}
GraphicsView::GraphicsView
(BasicGraphicsScene *scene, QWidget *parent)
: GraphicsView(parent)
{
setScene
(scene);
}
QAction *
GraphicsView::clearSelectionAction
()
const
{
return
_clearSelectionAction;
}
QAction *
GraphicsView::deleteSelectionAction
()
const
{
return
_deleteSelectionAction;
}
void
GraphicsView::setScene
(BasicGraphicsScene *scene)
{
QGraphicsView::setScene
(scene);
{
//
setup actions
delete
_clearSelectionAction;
_clearSelectionAction =
new
QAction
(
QStringLiteral
(
"
Clear Selection
"
),
this
);
_clearSelectionAction->
setShortcutContext
(Qt::ShortcutContext::WidgetShortcut);
_clearSelectionAction->
setShortcut
(Qt::Key_Escape);
connect
(_clearSelectionAction, &QAction::triggered, scene, &QGraphicsScene::clearSelection);
addAction
(_clearSelectionAction);
}
{
delete
_deleteSelectionAction;
_deleteSelectionAction =
new
QAction
(
QStringLiteral
(
"
Delete Selection
"
),
this
);
_deleteSelectionAction->
setShortcutContext
(Qt::ShortcutContext::WidgetShortcut);
_deleteSelectionAction->
setShortcut
(
QKeySequence
(QKeySequence::Delete));
connect
(_deleteSelectionAction,
&QAction::triggered,
this
,
&GraphicsView::onDeleteSelectedObjects);
addAction
(_deleteSelectionAction);
}
//
as we handle manually
return
;
{
delete
_duplicateSelectionAction;
_duplicateSelectionAction =
new
QAction
(
QStringLiteral
(
"
Duplicate Selection
"
),
this
);
_duplicateSelectionAction->
setShortcutContext
(Qt::ShortcutContext::WidgetShortcut);
_duplicateSelectionAction->
setShortcut
(
QKeySequence
(Qt::
CTRL
| Qt::Key_D));
connect
(_duplicateSelectionAction,
&QAction::triggered,
this
,
&GraphicsView::onDuplicateSelectedObjects);
addAction
(_duplicateSelectionAction);
}
{
delete
_copySelectionAction;
_copySelectionAction =
new
QAction
(
QStringLiteral
(
"
Copy Selection
"
),
this
);
_copySelectionAction->
setShortcutContext
(Qt::ShortcutContext::WidgetShortcut);
_copySelectionAction->
setShortcut
(
QKeySequence
(QKeySequence::Copy));
connect
(_copySelectionAction,
&QAction::triggered,
this
,
&GraphicsView::onCopySelectedObjects);
addAction
(_copySelectionAction);
}
{
delete
_pasteAction;
_pasteAction =
new
QAction
(
QStringLiteral
(
"
Copy Selection
"
),
this
);
_pasteAction->
setShortcutContext
(Qt::ShortcutContext::WidgetShortcut);
_pasteAction->
setShortcut
(
QKeySequence
(QKeySequence::Paste));
connect
(_pasteAction, &QAction::triggered,
this
, &GraphicsView::onPasteObjects);
addAction
(_pasteAction);
}
auto
undoAction = scene->
undoStack
().
createUndoAction
(
this
,
tr
(
"
&Undo
"
));
undoAction->
setShortcuts
(QKeySequence::Undo);
addAction
(undoAction);
auto
redoAction = scene->
undoStack
().
createRedoAction
(
this
,
tr
(
"
&Redo
"
));
redoAction->
setShortcuts
(QKeySequence::Redo);
addAction
(redoAction);
}
void
GraphicsView::centerScene
()
{
if
(
scene
()) {
scene
()->
setSceneRect
(
QRectF
());
QRectF sceneRect =
scene
()->
sceneRect
();
if
(sceneRect.
width
() >
this
->
rect
().
width
() || sceneRect.
height
() >
this
->
rect
().
height
()) {
fitInView
(sceneRect, Qt::KeepAspectRatio);
}
centerOn
(sceneRect.
center
());
}
}
void
GraphicsView::contextMenuEvent
(QContextMenuEvent *event)
{
if
(
itemAt
(event->
pos
())) {
QGraphicsView::contextMenuEvent
(event);
return
;
}
auto
const
scenePos =
mapToScene
(event->
pos
());
QMenu *menu =
nodeScene
()->
createSceneMenu
(scenePos);
if
(menu) {
menu->
exec
(event->
globalPos
());
}
nodeScene
()->
cleanupSceneMenu
(menu);
}
void
GraphicsView::wheelEvent
(QWheelEvent *event)
{
QPoint delta = event->
angleDelta
();
if
(delta.
y
() ==
0
) {
event->
ignore
();
return
;
}
double
const
d = delta.
y
() /
std::abs
(delta.
y
());
if
(d >
0.0
)
scaleUp
();
else
scaleDown
();
}
double
GraphicsView::getScale
()
const
{
return
transform
().
m11
();
}
void
GraphicsView::setScaleRange
(
double
minimum,
double
maximum)
{
if
(maximum < minimum)
std::swap
(minimum, maximum);
minimum =
std::max
(
0.0
, minimum);
maximum =
std::max
(
0.0
, maximum);
_scaleRange = {minimum, maximum};
setupScale
(
transform
().
m11
());
}
void
GraphicsView::getScaleRange
(
double
&minimum,
double
&maximum)
{
minimum = _scaleRange.
minimum
;
maximum = _scaleRange.
maximum
;
}
void
GraphicsView::setScaleRange
(ScaleRange range)
{
setScaleRange
(range.
minimum
, range.
maximum
);
}
void
GraphicsView::scaleUp
()
{
double
const
step =
1.2
;
double
const
factor =
std::pow
(step,
1.0
);
if
(_scaleRange.
maximum
>
0
) {
QTransform t =
transform
();
t.
scale
(factor, factor);
if
(t.
m11
() >= _scaleRange.
maximum
) {
setupScale
(t.
m11
());
return
;
}
}
scale
(factor, factor);
Q_EMIT
scaleChanged
(
transform
().
m11
());
}
void
GraphicsView::scaleDown
()
{
double
const
step =
1.2
;
double
const
factor =
std::pow
(step, -
1.0
);
if
(_scaleRange.
minimum
>
0
) {
QTransform t =
transform
();
t.
scale
(factor, factor);
if
(t.
m11
() <= _scaleRange.
minimum
) {
setupScale
(t.
m11
());
return
;
}
}
scale
(factor, factor);
Q_EMIT
scaleChanged
(
transform
().
m11
());
}
void
GraphicsView::setupScale
(
double
scale)
{
scale =
std::max
(_scaleRange.
minimum
,
std::min
(_scaleRange.
maximum
, scale));
if
(scale <=
0
)
return
;
if
(scale ==
transform
().
m11
())
return
;
QTransform matrix;
matrix.
scale
(scale, scale);
setTransform
(matrix,
false
);
Q_EMIT
scaleChanged
(scale);
}
void
GraphicsView::onDeleteSelectedObjects
()
{
nodeScene
()->
undoStack
().
push
(
new
DeleteCommand
(
nodeScene
()));
}
void
GraphicsView::onDuplicateSelectedObjects
()
{
QPointF
const
pastePosition =
scenePastePosition
();
nodeScene
()->
undoStack
().
push
(
new
CopyCommand
(
nodeScene
()));
nodeScene
()->
undoStack
().
push
(
new
PasteCommand
(
nodeScene
(), pastePosition));
}
void
GraphicsView::onCopySelectedObjects
()
{
nodeScene
()->
undoStack
().
push
(
new
CopyCommand
(
nodeScene
()));
}
void
GraphicsView::onPasteObjects
()
{
QPointF
const
pastePosition =
scenePastePosition
();
nodeScene
()->
undoStack
().
push
(
new
PasteCommand
(
nodeScene
(), pastePosition));
}
void
GraphicsView::keyPressEvent
(QKeyEvent *event)
{
switch
(event->
key
()) {
case
Qt::Key_Shift:
setDragMode
(QGraphicsView::RubberBandDrag);
break
;
default
:
break
;
}
QGraphicsView::keyPressEvent
(event);
}
void
GraphicsView::keyReleaseEvent
(QKeyEvent *event)
{
switch
(event->
key
()) {
case
Qt::Key_Shift:
setDragMode
(QGraphicsView::ScrollHandDrag);
break
;
default
:
break
;
}
QGraphicsView::keyReleaseEvent
(event);
}
void
GraphicsView::mousePressEvent
(QMouseEvent *event)
{
if
(!
itemAt
(event->
pos
())) {
Q_EMIT
voidPressed
();
}
QGraphicsView::mousePressEvent
(event);
if
(event->
button
() == Qt::LeftButton) {
_clickPos =
mapToScene
(event->
pos
());
}
}
void
GraphicsView::mouseMoveEvent
(QMouseEvent *event)
{
QGraphicsView::mouseMoveEvent
(event);
if
(
scene
()->
mouseGrabberItem
() ==
nullptr
&& event->
buttons
() == Qt::LeftButton) {
//
Make sure shift is not being pressed
if
((event->
modifiers
() & Qt::ShiftModifier) ==
0
) {
QPointF difference = _clickPos -
mapToScene
(event->
pos
());
setSceneRect
(
sceneRect
().
translated
(difference.
x
(), difference.
y
()));
}
}
}
void
GraphicsView::drawBackground
(QPainter *painter,
const
QRectF &r)
{
auto
const
&flowViewStyle =
StyleCollection::flowViewStyle
();
if
(
backgroundBrush
().
color
() != flowViewStyle.
BackgroundColor
) {
//
might be needed if style changed at runtime
setBackgroundBrush
(flowViewStyle.
BackgroundColor
);
}
QGraphicsView::drawBackground
(painter, r);
auto
drawGrid = [&](
double
gridStep) {
QRect windowRect =
rect
();
QPointF tl =
mapToScene
(windowRect.
topLeft
());
QPointF br =
mapToScene
(windowRect.
bottomRight
());
double
left =
std::floor
(tl.
x
() / gridStep -
0.5
);
double
right =
std::floor
(br.
x
() / gridStep +
1.0
);
double
bottom =
std::floor
(tl.
y
() / gridStep -
0.5
);
double
top =
std::floor
(br.
y
() / gridStep +
1.0
);
//
vertical lines
for
(
int
xi =
int
(left); xi <=
int
(right); ++xi) {
QLineF
line
(xi * gridStep, bottom * gridStep, xi * gridStep, top * gridStep);
painter->
drawLine
(line);
}
//
horizontal lines
for
(
int
yi =
int
(bottom); yi <=
int
(top); ++yi) {
QLineF
line
(left * gridStep, yi * gridStep, right * gridStep, yi * gridStep);
painter->
drawLine
(line);
}
};
QPen
pfine
(flowViewStyle.
FineGridColor
,
1.0
);
painter->
setPen
(pfine);
drawGrid
(
15
);
QPen
p
(flowViewStyle.
CoarseGridColor
,
1.0
);
painter->
setPen
(p);
drawGrid
(
150
);
}
void
GraphicsView::showEvent
(QShowEvent *event)
{
QGraphicsView::showEvent
(event);
centerScene
();
}
BasicGraphicsScene *
GraphicsView::nodeScene
()
{
return
dynamic_cast
<BasicGraphicsScene *>(
scene
());
}
QPointF
GraphicsView::scenePastePosition
()
{
QPoint origin =
mapFromGlobal
(
QCursor::pos
());
QRect
const
viewRect =
rect
();
if
(!viewRect.
contains
(origin))
origin = viewRect.
center
();
return
mapToScene
(origin);
}
Back
|
FazBrowse Home
|
New Git URL