OpenWalnut  1.5.0dev
WQtNetworkArrow.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WQTNETWORKARROW_H
26 #define WQTNETWORKARROW_H
27 
28 #include <iostream>
29 #include <memory>
30 #include <string>
31 
32 #include <QGraphicsLineItem>
33 #include <QGraphicsPathItem>
34 #include <QGraphicsScene>
35 #include <QGraphicsSceneMouseEvent>
36 #include <QPainter>
37 
38 #include "core/kernel/WModuleConnector.h"
39 
40 // forward declaration
43 
44 /**
45  * This Class is needed for connecting two ports and drawing a line between
46  * them.
47  */
49 {
50 public:
51  /**
52  * This customize the return value of type()
53  */
54  enum
55  {
56  Type = UserType + 4
57  };
58 
59  /**
60  * Constructor
61  *
62  * \param outPort the outputport that provide the data
63  * \param inPort the inputport that recive the data
64  */
65  explicit WQtNetworkArrow( WQtNetworkOutputPort *outPort, WQtNetworkInputPort *inPort );
66 
67  /**
68  * Destructor
69  */
71 
72  /**
73  * Reimplementation from QGraphicsItem
74  * \return the type of the item as int
75  */
76  int type() const;
77 
78  /**
79  * Calculated the new position of the lines endpoints in the scene.
80  * Is called everytime the parentItem is changed or after construction.
81  *
82  * \param deviate pull the line into this direction.
83  */
84  void updatePosition( QPointF deviate = QPointF() );
85 
86  /**
87  * Calculated the new position of the lines endpoints in the scene.
88  * Is called everytime the parentItem is changed or after construction.
89  *
90  * \param deviate pull the line into this direction.
91  * \param targetPoint the point where to point to.
92  */
93  void updatePosition( QPointF targetPoint, QPointF deviate );
94 
95  /**
96  * Returns the WQtNetworkOutputPort where the arrow starts.
97  *\return the WQtNetworkInputPort where the connection starts
98  */
100 
101  /**
102  * Returns the WQtNetworkInputPort where the arrow ends.
103  *\return the WQtNetworkInputPort where the connection ends
104  */
106 
107  /**
108  * Search the next, compatible input port.
109  *
110  * \param pos the current position for which to search
111  * \param maxDistance the maximum distance
112  *
113  * \return the connector, or NULL if none.
114  */
115  WQtNetworkInputPort* findNearestCompatibleInput( QPointF pos, float maxDistance = 100 );
116 
117  /**
118  * Reimplementation form QGraphicsItem, because the arrowhead is added
119  * to the line. Its needed that QGraphicsView knows which area needs to
120  * be redrawn.
121  * \return a QRectF that bounds the drawn object
122  */
123  QRectF boundingRect() const;
124 
125  /**
126  * Reimplementation from QGraphicsItem.
127  * Return the shape of this item as a QPainterPath in local coordinates
128  * \return shape as QPainterPath of this object
129  */
130  QPainterPath shape() const;
131 
132  /**
133  * If the item is changed we want to get notified.
134  *
135  * \param change
136  * \param value
137  * \return
138  */
139  QVariant itemChange( GraphicsItemChange change, const QVariant &value );
140 
141  /**
142  * Start Drag.
143  *
144  * \param pos the position in scene
145  */
146  void startDrag( const QPointF& pos );
147 
148  /**
149  * Update drag position
150  *
151  * \param pos the position in scene
152  */
153  void moveDrag( const QPointF& pos );
154 
155  /**
156  * Called when releasing the mouse.
157  *
158  * \param pos the position in scene
159  */
160  void doneDrag( const QPointF& pos );
161 
162 protected:
163  /**
164  * Draw some customized stuff in the scene.
165  *
166  * \param painter
167  * \param option
168  * \param w
169  */
170  void paint( QPainter* painter, const QStyleOptionGraphicsItem *option, QWidget *w );
171 
172  /**
173  * If the cursor enters the arrow, the arrow becomes geen.
174  *
175  * \param event the hover event
176  */
177  void hoverEnterEvent( QGraphicsSceneHoverEvent *event );
178 
179  /**
180  * If the cursor leaves the arrow, the arrow gets his default color.
181  *
182  * \param event the hover event
183  */
184  void hoverLeaveEvent( QGraphicsSceneHoverEvent *event );
185 
186  /**
187  * Double click on port
188  *
189  * \param mouseEvent the event
190  */
191  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent* mouseEvent );
192 
193  /**
194  * Start drawing an arrow temporary.
195  *
196  * \param mouseEvent the mouse event
197  */
198  void mousePressEvent( QGraphicsSceneMouseEvent *mouseEvent );
199 
200  /**
201  * Updates the temporary arrows endpoint.
202  * Arrow is colored green when connection possible, red if no connection
203  * is possible, or black when cursor doesent covers an WQtNetworkPort.
204  *
205  * \param mouseEvent the mouse event
206  */
207  void mouseMoveEvent( QGraphicsSceneMouseEvent *mouseEvent );
208 
209  /**
210  * Send a connect request to kernel when start- and endport are
211  * connectable
212  *
213  * \param mouseEvent the mouse event
214  */
215  void mouseReleaseEvent( QGraphicsSceneMouseEvent *mouseEvent );
216 private:
217  /**
218  * This method changes the color of the arrow.
219  *
220  * \param color the choosen color
221  */
222  void changeColor( QColor color );
223 
224  /**
225  * Change color and width of the arrow
226  *
227  * \param color the color
228  * \param penWidth the new width.
229  */
230  void changeColor( QColor color, float penWidth );
231 
232  WQtNetworkOutputPort *m_startPort; //!< the start port
233 
234  WQtNetworkInputPort *m_endPort; //!< the end port
235 
236  QColor m_color; //!< the current color
237 
238  QPolygonF m_arrowHead; //!< the arrowhead
239 
240  QLineF m_line; //!< the line representing the arrow
241 
242  QPointF m_clickPoint; //!< position where the click event was created.
243 
244  bool m_snappedOff; //!< gets true once the arrow was pulled far away from original click position.
245 
246  bool m_connectionDisconnect; //!< disconnect if true.
247 
248  WQtNetworkInputPort* m_connectTo; //!< connect to this port after mouse release.
249 };
250 #endif // WQTNETWORKARROW_H
This Class is needed for connecting two ports and drawing a line between them.
WQtNetworkInputPort * getEndPort()
Returns the WQtNetworkInputPort where the arrow ends.
void updatePosition(QPointF deviate=QPointF())
Calculated the new position of the lines endpoints in the scene.
void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
If the cursor enters the arrow, the arrow becomes geen.
WQtNetworkArrow(WQtNetworkOutputPort *outPort, WQtNetworkInputPort *inPort)
Constructor.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
Double click on port.
void changeColor(QColor color)
This method changes the color of the arrow.
bool m_snappedOff
gets true once the arrow was pulled far away from original click position.
WQtNetworkInputPort * m_endPort
the end port
~WQtNetworkArrow()
Destructor.
WQtNetworkOutputPort * getStartPort()
Returns the WQtNetworkOutputPort where the arrow starts.
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
Start drawing an arrow temporary.
QPointF m_clickPoint
position where the click event was created.
QPainterPath shape() const
Reimplementation from QGraphicsItem.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
Send a connect request to kernel when start- and endport are connectable.
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
If the item is changed we want to get notified.
void doneDrag(const QPointF &pos)
Called when releasing the mouse.
QRectF boundingRect() const
Reimplementation form QGraphicsItem, because the arrowhead is added to the line.
int type() const
Reimplementation from QGraphicsItem.
QColor m_color
the current color
WQtNetworkInputPort * m_connectTo
connect to this port after mouse release.
bool m_connectionDisconnect
disconnect if true.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w)
Draw some customized stuff in the scene.
QPolygonF m_arrowHead
the arrowhead
WQtNetworkOutputPort * m_startPort
the start port
QLineF m_line
the line representing the arrow
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
If the cursor leaves the arrow, the arrow gets his default color.
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
Updates the temporary arrows endpoint.
void moveDrag(const QPointF &pos)
Update drag position.
WQtNetworkInputPort * findNearestCompatibleInput(QPointF pos, float maxDistance=100)
Search the next, compatible input port.
void startDrag(const QPointF &pos)
Start Drag.
This class represents the ports a module have.
This class represents the ports a module have.