Vuo  2.3.2
VuoErrorMark.cc
Go to the documentation of this file.
1 
10 #include "VuoErrorMark.hh"
11 #include "VuoRendererCable.hh"
12 #include "VuoRendererFonts.hh"
14 
19 {
20  setZValue(errorMarkZValue);
21  this->errorMarkPath = QPainterPath();
22 }
23 
27 void VuoErrorMark::addMarkedComponents(set<VuoRendererNode *> markedNodes, set<VuoRendererCable *> markedCables)
28 {
29  foreach (VuoRendererNode *node, markedNodes)
30  this->nodes.insert(node);
31 
32  foreach (VuoRendererCable *cable, markedCables)
33  this->cables.insert(cable);
34 
36 }
37 
41 QRectF VuoErrorMark::boundingRect(void) const
42 {
43  QPainterPath errorMarkPath = getErrorMarkPath();
44  QRectF r = errorMarkPath.boundingRect();
45 
46  // Antialiasing bleed
47  r.adjust(-1,-1,1,1);
48 
49  return r.toAlignedRect();
50 }
51 
57 void VuoErrorMark::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
58 {
59  // Workaround to prevent items that have been removed from the scene from being painted on the scene anyway.
60  // https://b33p.net/kosada/node/8267
61  if (!scene())
62  return;
63 
64  drawBoundingRect(painter);
65 
66  QPainterPath path = getErrorMarkPath();
67 
68  VuoRendererColors colors;
69  painter->fillPath(path, colors.errorMark());
70 }
71 
76 {
77  QGraphicsScene *itemScene = scene();
78  if (itemScene)
79  {
80  this->prepareGeometryChange();
81  itemScene->removeItem(this);
82  }
83 }
84 
90 {
91  this->prepareGeometryChange();
92 
93  QPainterPath path;
94  const qreal markWidth = VuoRendererFonts::thickPenWidth / 2.;
95  const qreal cornerRadius = VuoRendererNode::cornerRadius + markWidth;
96 
97  foreach (VuoRendererNode *node, nodes)
98  {
99  QPainterPath nodeSimplifiedOutline;
100 
101  VuoRendererTypecastPort *typecastPort = node->getProxyCollapsedTypecast();
102  if (typecastPort)
103  {
104  QRectF typecastPortRect = typecastPort->boundingRect();
105  if (!typecastPortRect.isNull())
106  {
107  QPainterPath typecastPath;
108  typecastPath.addRoundedRect( typecastPortRect.adjusted(-0.5*markWidth, -0.5*markWidth, 0.5*markWidth, 0.5*markWidth),
109  cornerRadius, cornerRadius );
110  typecastPath.translate( typecastPort->scenePos() );
111  path = path.united( typecastPath );
112  }
113  }
114 
115  else
116  {
117  QRectF nodeOuterFrameRect = node->boundingRect();
118  if (!nodeOuterFrameRect.isNull())
119  {
120  nodeSimplifiedOutline.addRoundedRect( nodeOuterFrameRect.adjusted(-markWidth, -markWidth, markWidth, markWidth),
121  cornerRadius, cornerRadius );
122 
123  nodeSimplifiedOutline.translate( node->getBase()->getX(), node->getBase()->getY() );
124  path = path.united( nodeSimplifiedOutline );
125  }
126  }
127  }
128 
129  foreach (VuoRendererCable *cable, cables)
130  {
131  QPainterPathStroker stroker;
132  stroker.setWidth(markWidth);
133  QPainterPath cableCurve = stroker.createStroke( cable->getCablePath() );
134  path = path.united( cableCurve );
135  }
136 
137  this->errorMarkPath = path;
138 }
139 
143 QPainterPath VuoErrorMark::getErrorMarkPath() const
144 {
145  return this->errorMarkPath;
146 }