Skip to content

Commit 880cfdf

Browse files
committed
Improve hovering for connections
1 parent 57fa0e3 commit 880cfdf

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ADD_DEFINITIONS(-std=c++11)
1313
find_package(Qt5Widgets)
1414
find_package(Qt5Core)
1515
find_package(Qt5Gui)
16+
find_package(Qt5OpenGL)
1617

1718
add_definitions(${Qt5Widgets_DEFINITIONS})
1819
set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
@@ -25,11 +26,12 @@ add_executable(blocks
2526
)
2627

2728
# Use the Widgets module from Qt 5.
28-
qt5_use_modules(blocks Widgets Gui Core)
29+
qt5_use_modules(blocks Widgets Gui Core OpenGL)
2930

3031

3132
target_link_libraries(blocks
3233
${Qt5Widgets_LIBRARIES}
3334
${Qt5Core_LIBRARIES}
3435
${QtGui_LIBRARIES}
36+
${QtOpenGL_LIBRARIES}
3537
)

‎src/ConnectionGraphicsObject.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ onItemMoved(QUuid id, QPointF const &offset)
6868
}
6969

7070

71-
//QPainterPath
72-
//ConnectionGraphicsObject::
73-
//shape() const
74-
//{
75-
//return _connectionPainter.painterPath();
76-
//}
71+
QPainterPath
72+
ConnectionGraphicsObject::
73+
shape() const
74+
{
75+
return _connectionPainter.cubicPath();
76+
}
7777

7878

7979
void

‎src/ConnectionGraphicsObject.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConnectionGraphicsObject
2727
public slots:
2828
void onItemMoved(QUuid id, QPointF const &offset);
2929

30-
//QPainterPath shape() const override;
30+
QPainterPath shape() const override;
3131

3232
protected:
3333
void paint(QPainter* painter,

‎src/ConnectionPainter.hpp‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class ConnectionPainter
1616

1717
public:
1818

19-
QPainterPath painterPath() const
19+
QPainterPath cubicPath() const
2020
{
21-
QPainterPath path;
22-
2321
QPointF const& source = _connectionGeometry.source();
2422
QPointF const& sink = _connectionGeometry.sink();
2523

@@ -30,7 +28,7 @@ class ConnectionPainter
3028

3129
double const defaultOffset = 200;
3230

33-
double const lineWidth = _connectionGeometry.lineWidth();
31+
double const lineWidth = _connectionGeometry.lineWidth();
3432

3533
double const ratio1 = 0.5;
3634

@@ -44,19 +42,29 @@ class ConnectionPainter
4442
QPointF c1(source.x() + defaultOffset * ratio1, source.y() + 2 * verticalOffset);
4543
QPointF c2(sink.x() - defaultOffset * ratio1, sink.y() + verticalOffset);
4644

47-
QPen p;
48-
49-
p.setWidth(2 * lineWidth);
50-
5145
// cubic spline
5246
QPainterPath cubic(source);
5347
cubic.cubicTo(c1, c2, sink);
5448

55-
path.addPath(cubic);
49+
QPainterPath result(source);
50+
51+
unsigned segments = 20;
52+
53+
for (auto i = 0ul; i < segments; ++i)
54+
{
55+
result.lineTo(cubic.pointAtPercent( double(i + 1) / segments ));
56+
}
5657

57-
return path;
58+
QPainterPathStroker stroker; stroker.setWidth(10.0);
59+
60+
return stroker.createStroke(result);
5861
}
5962

63+
//QPainterPath painterPath() const
64+
//{
65+
//return cubicPath();
66+
//}
67+
6068
inline
6169
void paint(QPainter* painter) const
6270
{
@@ -101,6 +109,10 @@ class ConnectionPainter
101109
painter->drawLine(QLineF(c2, sink));
102110
painter->drawEllipse(c1, 4, 4);
103111
painter->drawEllipse(c2, 4, 4);
112+
113+
painter->setBrush(Qt::NoBrush);
114+
115+
painter->drawPath(cubicPath());
104116
}
105117
#endif
106118

‎src/FlowGraphicsView.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#include <QtGui/QWheelEvent>
88
#include <QtCore/QRectF>
99

10+
11+
#include <QtOpenGL>
12+
#include <QtWidgets>
13+
1014
#include <QDebug>
1115
#include <iostream>
1216

13-
1417
FlowGraphicsView::
1518
FlowGraphicsView(QGraphicsScene *scene)
1619
: QGraphicsView(scene)
@@ -19,7 +22,7 @@ FlowGraphicsView(QGraphicsScene *scene)
1922
setRenderHint(QPainter::Antialiasing);
2023
setBackgroundBrush(QColor(Qt::gray).darker(300));
2124

22-
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
25+
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
2326

2427
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
2528
}

0 commit comments

Comments
 (0)