SemanticGraphEdgeItem.cpp
Go to the documentation of this file.
2 
3 #include <QGraphicsSceneContextMenuEvent>
4 #include <QPainter>
5 
7 
8 namespace armarx
9 {
10  static const QColor SEMANTIC_HIGHLIGHT(0x00, 0x97, 0x81);
11  static const QColor SEMANTIC_DEFAULT = Qt::GlobalColor::black;
12 
13  void
14  SemanticGraphEdgeItem::paint(QPainter* painter,
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 
94 QPainterPath
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 }
armarx::SemanticGraphEdgeItem::selected
bool selected
Definition: SemanticGraphEdgeItem.h:29
armarx::SemanticGraphEdgeItem::size
float size
Definition: SemanticGraphEdgeItem.h:27
armarx::SemanticGraphEdgeItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: SemanticGraphEdgeItem.cpp:14
armarx::SemanticGraphEdgeItem::targetDescriptor
int targetDescriptor
Definition: SemanticGraphEdgeItem.h:24
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::SemanticGraphEdgeItem::onLeftClick
void onLeftClick(SemanticGraphEdgeItem *selected)
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::SemanticGraphEdgeItem::labelPosition
QPointF labelPosition
Definition: SemanticGraphEdgeItem.h:34
armarx::SemanticGraphEdgeItem::label
QString label
Definition: SemanticGraphEdgeItem.h:33
armarx::SemanticGraphEdgeItem::shape
QPainterPath shape() const override
Definition: SemanticGraphEdgeItem.cpp:95
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
option
#define option(type, fn)
SemanticGraphEdgeItem.h
armarx::SemanticGraphEdgeItem::color
QColor color
Definition: SemanticGraphEdgeItem.h:31
armarx::SemanticGraphEdgeItem::openingAngle
float openingAngle
Definition: SemanticGraphEdgeItem.h:26
armarx::SemanticGraphEdgeItem::sourceDescriptor
int sourceDescriptor
Definition: SemanticGraphEdgeItem.h:23
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:109
armarx::SemanticGraphEdgeItem::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Definition: SemanticGraphEdgeItem.cpp:60
Logging.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27