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