Skip to content

Commit 0d264db

Browse files
Miguel Ferreirapaceholder
authored andcommitted
Implement node selecting and deleting
1 parent e2ff293 commit 0d264db

File tree

8 files changed

+98
-10
lines changed

8 files changed

+98
-10
lines changed

src/FlowGraphicsView.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,44 @@ wheelEvent(QWheelEvent *event)
109109
scale(factor, factor);
110110
}
111111

112+
void
113+
FlowGraphicsView::
114+
keyPressEvent(QKeyEvent *event)
115+
{
116+
switch (event->key())
117+
{
118+
case Qt::Key_Escape:
119+
_scene->clearSelection();
120+
break;
121+
case Qt::Key_Delete:
122+
foreach(QGraphicsItem* item, _scene->selectedItems()){
123+
_scene->removeNode(item);
124+
}
125+
break;
126+
case Qt::Key_Shift:
127+
setDragMode(QGraphicsView::RubberBandDrag);
128+
break;
129+
default:
130+
break;
131+
}
132+
QGraphicsView::keyPressEvent(event);
133+
}
134+
135+
void
136+
FlowGraphicsView::
137+
keyReleaseEvent(QKeyEvent *event)
138+
{
139+
switch (event->key())
140+
{
141+
case Qt::Key_Shift:
142+
setDragMode(QGraphicsView::ScrollHandDrag);
143+
break;
144+
default:
145+
break;
146+
}
147+
QGraphicsView::keyReleaseEvent(event);
148+
}
149+
112150

113151
void
114152
FlowGraphicsView::

src/FlowGraphicsView.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class NODE_EDITOR_PUBLIC FlowGraphicsView
1919

2020
void wheelEvent(QWheelEvent *event) override;
2121

22+
void keyPressEvent(QKeyEvent *event) override;
23+
24+
void keyReleaseEvent(QKeyEvent *event) override;
25+
2226
void drawBackground(QPainter* painter, const QRectF& r) override;
2327

2428
private:

src/FlowScene.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ createNode(std::unique_ptr<NodeDataModel> && dataModel)
5757
return node;
5858
}
5959

60+
void
61+
FlowScene::
62+
removeNode(QGraphicsItem* item)
63+
{
64+
auto ngo = dynamic_cast<NodeGraphicsObject*>(item);
65+
std::shared_ptr<Node> const& node = ngo->node().lock();
66+
67+
auto nodeState = node->nodeState();
68+
for (std::weak_ptr<Connection> conn : nodeState.getEntries(PortType::In)) {
69+
if(!conn.expired())
70+
this->deleteConnection(conn.lock());
71+
}
72+
for (std::weak_ptr<Connection> conn : nodeState.getEntries(PortType::Out)) {
73+
if (!conn.expired())
74+
this->deleteConnection(conn.lock());
75+
}
76+
77+
_nodes.erase(node->id());
78+
}
79+
6080

6181
FlowScene::
6282
FlowScene()

src/FlowScene.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class NODE_EDITOR_PUBLIC FlowScene
4949
std::shared_ptr<Node>
5050
createNode(std::unique_ptr<NodeDataModel> &&dataModel);
5151

52+
void
53+
removeNode(QGraphicsItem* item);
54+
5255
private:
5356

5457
using SharedConnection = std::shared_ptr<Connection>;

src/NodeGraphicsObject.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ NodeGraphicsObject(FlowScene &scene,
2828
setFlag(QGraphicsItem::ItemDoesntPropagateOpacityToChildren, true);
2929
setFlag(QGraphicsItem::ItemIsMovable, true);
3030
setFlag(QGraphicsItem::ItemIsFocusable, true);
31+
setFlag(QGraphicsItem::ItemIsSelectable, true);
32+
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
33+
3134

3235
setCacheMode( QGraphicsItem::DeviceCoordinateCache );
3336

@@ -166,11 +169,24 @@ paint(QPainter * painter,
166169
NodePainter::paint(painter, node);
167170
}
168171

172+
QVariant
173+
NodeGraphicsObject::
174+
itemChange(GraphicsItemChange change, const QVariant &value) {
175+
if (change == ItemPositionChange && scene()) {
176+
moveConnections();
177+
}
178+
return QGraphicsItem::itemChange(change, value);
179+
}
169180

170181
void
171182
NodeGraphicsObject::
172183
mousePressEvent(QGraphicsSceneMouseEvent * event)
173184
{
185+
186+
if (!this->isSelected() && !(event->modifiers() & Qt::ControlModifier)) {
187+
_scene.clearSelection();
188+
}
189+
174190
auto clickPort =
175191
[&](PortType portToCheck)
176192
{
@@ -216,11 +232,7 @@ mousePressEvent(QGraphicsSceneMouseEvent * event)
216232

217233
clickPort(PortType::In);
218234
clickPort(PortType::Out);
219-
220-
event->accept();
221-
222-
//------
223-
235+
224236
auto pos = event->pos();
225237
auto node = _node.lock();
226238
auto & geom = node->nodeGeometry();

src/NodeGraphicsObject.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class NodeGraphicsObject : public QGraphicsObject
4646
QStyleOptionGraphicsItem const* option,
4747
QWidget* widget = 0) override;
4848

49+
QVariant
50+
itemChange(GraphicsItemChange change, const QVariant &value) override;
51+
4952
void
5053
mousePressEvent(QGraphicsSceneMouseEvent* event) override;
5154

src/NodePainter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <QtCore/QMargins>
44

5+
#include "NodeGraphicsObject.hpp"
56
#include "NodeGeometry.hpp"
67
#include "NodeState.hpp"
78
#include "NodeDataModel.hpp"
@@ -16,11 +17,13 @@ paint(QPainter* painter,
1617

1718
NodeState const& state = node->nodeState();
1819

20+
std::unique_ptr<NodeGraphicsObject> const& graphicsObject = node->nodeGraphicsObject();
21+
1922
geom.recalculateSize(painter->fontMetrics());
2023

2124
//--------------------------------------------
2225

23-
drawNodeRect(painter, geom);
26+
drawNodeRect(painter, geom, graphicsObject);
2427

2528
drawConnectionPoints(painter, geom, state);
2629

@@ -39,16 +42,19 @@ paint(QPainter* painter,
3942
void
4043
NodePainter::
4144
drawNodeRect(QPainter* painter,
42-
NodeGeometry const& geom)
45+
NodeGeometry const& geom,
46+
std::unique_ptr<NodeGraphicsObject> const& graphicsObject)
4347
{
48+
auto color = graphicsObject->isSelected() ? QColor(255, 150, 0) : Qt::white;
49+
4450
if (geom.hovered())
4551
{
46-
QPen p(Qt::white, 2.0);
52+
QPen p(color, 2.0);
4753
painter->setPen(p);
4854
}
4955
else
5056
{
51-
QPen p(Qt::white, 1.5);
57+
QPen p(color, 1.5);
5258
painter->setPen(p);
5359
}
5460

src/NodePainter.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "PortType.hpp"
88

9+
class NodeGraphicsObject;
910
class NodeGeometry;
1011
class NodeState;
1112
class Node;
@@ -27,7 +28,8 @@ class NodePainter
2728

2829
static
2930
void
30-
drawNodeRect(QPainter* painter, NodeGeometry const& geom);
31+
drawNodeRect(QPainter* painter, NodeGeometry const& geom,
32+
std::unique_ptr<NodeGraphicsObject> const& graphicsObject);
3133

3234
static
3335
void

0 commit comments

Comments
 (0)