Developer Documentation
connectionPoint.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 //== INCLUDES =================================================================
45 #include <QPolygonF>
46 #include <QGraphicsItem>
47 #include <QFontMetrics>
48 #include <QPainter>
49 
50 #include "connectionPoint.hh"
51 #include "connection.hh"
52 #include "elementInOut.hh"
53 #include "graphicsScene.hh"
54 #include "elementArea.hh"
55 #include "text.hh"
56 #include "../parser/inout.hh"
57 
58 #define SIZE 20
59 
60 //== NAMESPACES ===============================================================
61 namespace VSI {
62 
63 //=============================================================================
64 //
65 // CLASS VSI::ConnectionPoint - IMPLEMENTATION
66 //
67 //=============================================================================
68 
69 
70 QColor colors_[] = { QColor(255, 0, 0),
71  QColor(255, 0, 255),
72  QColor(255, 128, 0),
73  QColor(224, 224, 0),
74  QColor(0, 192, 0),
75  QColor(0, 0, 255) };
76 
77 //------------------------------------------------------------------------------
78 
80 ConnectionPoint::ConnectionPoint (ElementInOut *_e, QGraphicsItem *_parent) :
81  QGraphicsEllipseItem (_parent),
82  inout_ (_e),
83  width_ (5)
84 {
85  setRect (0, 0, width_, width_);
86 
87  setBrush (QColor (64,64,64));
88 
89  setAcceptHoverEvents (true);
90 }
91 
92 //------------------------------------------------------------------------------
93 
96 {
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 void ConnectionPoint::setGeometry (const QRectF &_rect)
103 {
104  QGraphicsEllipseItem::setPos (_rect.topLeft ());
105  QGraphicsLayoutItem::setGeometry (_rect);
106  setRect (0, 0, width_, width_);
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 // size informations for layouting
112 QSizeF ConnectionPoint::sizeHint (Qt::SizeHint _which, const QSizeF &/*_constraint*/) const
113 {
114  QSizeF sh;
115  switch (_which) {
116  case Qt::MinimumSize:
117  case Qt::PreferredSize:
118  case Qt::MaximumSize:
119  sh = QSizeF(width_, width_);
120  break;
121  default:
122  break;
123  }
124 
125  return sh;
126 }
127 
128 //------------------------------------------------------------------------------
129 
130 // start a new connection on press
131 void ConnectionPoint::mousePressEvent (QGraphicsSceneMouseEvent *_event)
132 {
133  Connection *c = new Connection (this, scene ());
134  static_cast<GraphicsScene *>(scene ())->setActiveConnection (c);
135  QGraphicsEllipseItem::mousePressEvent(_event);
136 }
137 
138 //------------------------------------------------------------------------------
139 
142 {
143  QGraphicsItem *elementArea = dynamic_cast<GraphicsScene *> (scene ())->elementArea ();
144 
145  if (inout_->type () == ElementInOut::TypeInput)
146  return elementArea->mapFromScene (mapToScene (QPointF (0, width_ / 2)));
147  return elementArea->mapFromScene (mapToScene (QPointF (width_, width_ / 2)));
148 }
149 
150 //------------------------------------------------------------------------------
151 
154 {
155  setBrush (colors_[_state]);
156 
157  QString stateStr;
158 
159  switch (_state)
160  {
161  case NoValue:
162  stateStr = "[NOT CONNECTED]";
163  break;
164  case Optional:
165  stateStr = "[OPTIONAL]";
166  break;
167  case RuntimeInput:
168  stateStr = "[ASK DURING EXECUTION]";
169  break;
170  case UserInput:
171  stateStr = "[CONSTANT SET]";
172  break;
173  case Connected:
174  stateStr = "[CONNECTED]";
175  break;
176  case OutputNotConnected:
177  stateStr = "[NOT CONNECTED]";
178  break;
179  }
180 
181  stateStr += " " + inout_->inOut ()->longDescription () + " (" + inout_->inOut ()->typeString () + ")";
182  setToolTip (stateStr);
183  inout_->typeTextItem ()->setToolTip (stateStr);
184  inout_->descriptionTextItem ()->setToolTip (stateStr);
185 }
186 
187 //------------------------------------------------------------------------------
188 
189 // highlight connection if mouse over
190 void ConnectionPoint::hoverEnterEvent (QGraphicsSceneHoverEvent * /*_event*/)
191 {
192  foreach (Connection *c, inout_->connections ())
193  {
194  QPen p = c->pen ();
195 
196  if (inOut ()->inOut ()->typeString () == "data")
197  p.setWidth (6);
198  else
199  p.setWidth (4);
200 
201  p.setColor (QColor (0, 0, 255));
202  c->setPen (p);
203  }
204 }
205 
206 //------------------------------------------------------------------------------
207 
208 // remove highlight
209 void ConnectionPoint::hoverLeaveEvent (QGraphicsSceneHoverEvent * /*_event*/)
210 {
211  foreach (Connection *c, inout_->connections ())
212  {
213  QPen p = c->pen ();
214 
215  if (inOut ()->inOut ()->typeString () == "data")
216  p.setWidth (4);
217  else
218  p.setWidth (2);
219 
220  p.setColor (QColor (0, 0, 0));
221  c->setPen (p);
222  }
223 }
224 
225 //------------------------------------------------------------------------------
226 
229 {
230  width_ = _width;
231  updateGeometry ();
232 }
233 
234 //------------------------------------------------------------------------------
235 
236 // painting
237 void ConnectionPoint::paint(QPainter *_painter, const QStyleOptionGraphicsItem * _option, QWidget *_widget)
238 {
239  QGraphicsEllipseItem::paint (_painter, _option, _widget);
240 
241  float wH = width_ / 2.0;
242  float wT = width_ / 3.0;
243  float wS = width_ / 6.0;
244 
245  QPainterPath path;
246  path.moveTo (0, 0);
247  path.arcTo (0, 0, width_, width_, 0, 360);
248 
249  QRadialGradient rG (QPointF(width_ * 0.8, width_), width_ * 0.8);
250  rG.setColorAt(0, QColor (255, 255, 255, 128));
251  rG.setColorAt(1, Qt::transparent);
252  _painter->setBrush (rG);
253  _painter->setPen (Qt::NoPen);
254  _painter->drawPath (path);
255 
256  path = QPainterPath ();
257  path.moveTo (1, wH);
258  path.arcTo (1, 1, width_ - 2, width_ - 2, 180, - 165);
259  path.cubicTo (QPointF (wH + wT, wH + wS), QPointF (wH + wS, wH - wS), QPointF (wH, wH));
260  path.cubicTo (QPointF (wT, wH + wS), QPointF (wS, wH + wS), QPointF (1, wH));
261 
262  rG.setCenter (wS, 0);
263  rG.setFocalPoint(wS, 0);
264  rG.setRadius (width_);
265  rG.setColorAt(0, Qt::white);
266  rG.setColorAt(1, Qt::transparent);
267  _painter->setBrush (rG);
268  _painter->setPen (Qt::NoPen);
269  _painter->drawPath(path);
270 }
271 
272 //------------------------------------------------------------------------------
273 }
void setState(State _state)
sets the state
ElementInOut * inOut() const
Input/output element.
QList< Connection * > connections() const
Connections.
Definition: elementInOut.hh:98
virtual void setGeometry(const QRectF &_rect)
Sets the geometry.
State
State of the connection point.
const QString & longDescription() const
Long description.
Definition: inout.hh:76
QString typeString() const
Type.
Definition: inout.cc:65
Text * descriptionTextItem() const
Short description widget.
Definition: elementInOut.hh:95
Text * typeTextItem() const
Type text widget.
Definition: elementInOut.hh:92
void setWidth(int _width)
Sets the diameter.
ConnectionPoint(ElementInOut *_e, QGraphicsItem *_parent)
Constructor.
InOut * inOut() const
InOut context object.
~ConnectionPoint()
Destructor.
virtual Type type() const =0
Type.
QPointF connectPos()
Position for connections.