SemanticGraphEdgeItem.cpp
Go to the documentation of this file.
2
3#include <QGraphicsSceneContextMenuEvent>
4#include <QPainter>
5
7
8namespace armarx
9{
10 static const QColor SEMANTIC_HIGHLIGHT(0x00, 0x97, 0x81);
11 static const QColor SEMANTIC_DEFAULT = Qt::GlobalColor::black;
12
13 void
15 const QStyleOptionGraphicsItem* option,
16 QWidget* widget)
17 {
18 this->setPen(QPen(color, selected ? 3.0 : 2.0));
19 QGraphicsPathItem::paint(painter, option, widget);
20
21 QPainterPath path = this->path();
22 if (path.elementCount() < 2)
23 {
24 return;
25 }
26
27 int elementCount = path.elementCount();
28 QPointF p1 = path.elementAt(elementCount - 1);
29 QPointF p2 = path.elementAt(elementCount - 2);
30 QLineF line(p1, p2);
31
32 QPolygonF arrowHead;
33 {
34 double angle = std::acos(line.dx() / line.length());
35
36 if (line.dy() >= 0)
37 {
38 angle = (M_PI * 2) - angle;
39 }
40
41 float adjustedAngle = M_PI / 2.0 + openingAngle;
42 QPointF arrowP1 = line.p1() + QPointF(sin(angle + adjustedAngle) * size,
43 cos(angle + adjustedAngle) * size);
44 QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - adjustedAngle) * size,
45 cos(angle + M_PI - adjustedAngle) * size);
46
47 arrowHead << line.p1() << arrowP1 << arrowP2;
48 }
49
50 QBrush brush(color);
51 painter->setBrush(brush);
52 painter->drawPolygon(arrowHead);
53 if (label.size() > 0)
54 {
55 painter->drawText(labelPosition, label);
56 }
57 }
58
59 void
60 SemanticGraphEdgeItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
61 {
62 QPointF point = event->pos();
63
64 QPainterPath path = this->path();
65
66 float minDistance2 = 1000.0f;
67 for (int i = 0; i < 101; ++i)
68 {
69 double t = i / 100.0;
70 QPointF pointOnPath = path.pointAtPercent(t);
71 QPointF diff(pointOnPath - point);
72 float length2 = diff.x() * diff.x() + diff.y() * diff.y();
73 if (length2 < minDistance2)
74 {
75 minDistance2 = length2;
76 }
77 }
78 ARMARX_DEBUG << "Mouse: " << sourceDescriptor << " -> " << targetDescriptor << ": "
79 << std::sqrt(minDistance2);
80 const float CLICK_DISTANCE = 7.0f;
81 if (minDistance2 < CLICK_DISTANCE * CLICK_DISTANCE)
82 {
83 event->setAccepted(true);
84 emit onLeftClick(this);
85 }
86 else
87 {
88 event->setAccepted(false);
89 }
90 }
91
92} // namespace armarx
93
94QPainterPath
96{
97 QPainterPath result;
98 QRectF bb = path().boundingRect();
99 double deltaX = 5.0;
100 double deltaY = 5.0;
101 bb.adjust(-deltaX, -deltaY, deltaX, deltaY);
102
103 result.addRect(bb);
104 return result;
105}
#define option(type, fn)
#define M_PI
Definition MathTools.h:17
QPainterPath shape() const override
void onLeftClick(SemanticGraphEdgeItem *selected)
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
This file offers overloads of toIce() and fromIce() functions for STL container types.
double angle(const Point &a, const Point &b, const Point &c)
Definition point.hpp:109