OpenWalnut  1.5.0dev
WQtNetworkItem.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 WQTNETWORKITEM_H
26 #define WQTNETWORKITEM_H
27 
28 #include <memory>
29 #include <string>
30 
31 #include <QColor>
32 #include <QGraphicsItemAnimation>
33 #include <QGraphicsObject>
34 #include <QGraphicsTextItem>
35 #include <QPainter>
36 #include <QtCore/QObject>
37 #include <QtCore/QTimeLine>
38 #include <QtCore/QTimer>
39 
40 #include "WQtNetworkEditorGlobals.h"
41 #include "WQtNetworkInputPort.h"
42 #include "WQtNetworkOutputPort.h"
43 #include "core/kernel/WModule.h"
44 
45 class WQtNetworkEditor;
47 
48 /**
49  * This class represents a WModule as QGraphicsItem and
50  * contains a reference to its in- and outports.
51  */
53 {
54  Q_OBJECT
55 
56 public:
57  /**
58  * Constructs new item in the network scene.
59  *
60  * \param editor the editor containing this item
61  * \param module the module represented by the item
62  */
63  WQtNetworkItem( WQtNetworkEditor* editor, std::shared_ptr< WModule > module );
64 
65  /**
66  * Destructor.
67  */
68  virtual ~WQtNetworkItem();
69 
70  /**
71  * This customize the return value of type()
72  */
73  enum
74  {
75  Type = UserType + 20
76  };
77 
78  /**
79  * Reimplementation from QGraphicsItem
80  * \return the type of the item as int
81  */
82  int type() const;
83 
84  /**
85  * The bounding area of the item.
86  *
87  * \return the bounding rect
88  */
89  virtual QRectF boundingRect() const;
90 
91  /**
92  * Add a port to the item.
93  *
94  * \param inPort the input port
95  */
96  void addInputPort( WQtNetworkInputPort* inPort );
97 
98  /**
99  * Add a port to the item.
100  *
101  * \param outPort the output port
102  */
103  void addOutputPort( WQtNetworkOutputPort* outPort );
104 
105  /**
106  * Returns the item inports.
107  * \return the item inports
108  */
110 
111  /**
112  * Returns the item outports
113  * \return the item outports
114  */
116 
117  /**
118  * This method aligns the in- and outports as well as the modulename
119  * in a regular way.
120  *
121  * \param maximumWidth the maximal width of the item.
122  * \param minimumWidth the minimal width of the item.
123  */
124  void fitLook( float maximumWidth = WNETWORKITEM_MAXIMUM_WIDTH, float minimumWidth = WNETWORKITEM_MINIMUM_WIDTH );
125 
126  /**
127  * Set the QGraphicsTextItem ( the caption ) of the item
128  *
129  * \param text the caption
130  */
131  void setTextItem( QGraphicsTextItem* text );
132 
133  /**
134  * Get the caption as QString
135  *
136  * \return the caption.
137  */
138  QString getText();
139 
140  /**
141  * Get the WModule represented by this object.
142  * \return the related WModule
143  */
144  std::shared_ptr< WModule > getModule();
145 
146  /**
147  * Here the module can be enabled when the WModule is ready.
148  * \param active true if module is ready.
149  */
150  void activate( bool active );
151 
152  /**
153  * Can be used for polling the module states. It is called by a timer.
154  */
155  virtual void updater();
156 
157  /**
158  * Mark this module as crashed.
159  */
160  void setCrashed();
161 
162  /**
163  * Animate removal and finally, instruct the owning network editor to remove the item.
164  *
165  * \return the timer for the removal animation. Use this to start the animation.
166  */
167  QTimeLine* die();
168 
169  /**
170  * Move item to specified position smoothly, via animation.
171  *
172  * \param pos position in world space
173  */
174  void animatedMoveTo( QPointF pos );
175 
176  /**
177  * Move item to specified position smoothly, via animation.
178  *
179  * \param x x coord in world space
180  * \param y y coord in world space
181  */
182  void animatedMoveTo( qreal x, qreal y );
183 
184  /**
185  * Move item to specified position smoothly, no animation.
186  *
187  * \param pos position in world space
188  */
189  void moveTo( QPointF pos );
190 
191  /**
192  * Move item to specified position smoothly, no animation.
193  *
194  * \param x x coord in world space
195  * \param y y coord in world space
196  */
197  void moveTo( qreal x, qreal y );
198 
199  /**
200  * Marks the item as already layed out.
201  *
202  * \param layedOut the layout flag
203  */
204  void setLayedOut( bool layedOut = true );
205 
206  /**
207  * Checks whether the item was layed out earlier.
208  *
209  * \return true if already positioned
210  */
211  bool wasLayedOut() const;
212 
213  /**
214  * Mark item as manually placed. This flag is used by the layouter to avoid re-layouting these kind of items.
215  *
216  * \param manual true if manually placed.
217  */
218  void setManuallyPlaced( bool manual = true );
219 
220  /**
221  * Item was placed by hand. Used as a flag to avoid re-layouting these items.
222  *
223  * \return true if manually placed.
224  */
225  bool wasManuallyPlaced() const;
226 
227 public slots:
228  /**
229  * Called whenever the item moves around.
230  */
231  void positionChanged();
232 
233 signals:
234  /**
235  * The item is now dead. Animation completed and item was removed from scene. Do not use this to delete the item.
236  *
237  * \param item the item.
238  */
239  void dead( WQtNetworkItem* item );
240 protected:
241  /**
242  * If the item is changed we want to get notified.
243  *
244  * \param change
245  * \param value
246  * \return
247  */
248  virtual QVariant itemChange( GraphicsItemChange change, const QVariant& value );
249 
250  /**
251  * If the WQtNetworkItem is moved, then the contained ports have to update
252  * the connected WQtNetworkArrows for correct alignment.
253  *
254  * \param mouseEvent the mouse event
255  */
256  virtual void mouseMoveEvent( QGraphicsSceneMouseEvent* mouseEvent );
257 
258  /**
259  * Reimplemented from QGraphicsRectItem. Used to enable selection of network items
260  * with right-click and left-click.
261  *
262  * \param event the mouse event
263  **/
264  virtual void mousePressEvent( QGraphicsSceneMouseEvent *event );
265 
266  /**
267  * Mouse was released.
268  *
269  * \param event the mouse event
270  */
271  virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent* event );
272 
273  /**
274  * Called upon double click.
275  *
276  * \param event the click event
277  */
278  virtual void mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event );
279 
280  /**
281  * If the cursor enters the item, the item gets a green color.
282  *
283  * \param event the hover event
284  */
285  virtual void hoverEnterEvent( QGraphicsSceneHoverEvent* event );
286 
287  /**
288  * If the cursor leaves the item, the item gets his default color.
289  *
290  * \param event the hover event
291  */
292  virtual void hoverLeaveEvent( QGraphicsSceneHoverEvent* event );
293 
294  /**
295  * Draw some customized stuff in the scene.
296  *
297  * \param painter
298  * \param option
299  * \param w
300  */
301  virtual void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* w );
302 
303  /**
304  * Set the new title text. Forces update.
305  *
306  * \param text the text
307  */
308  void setText( std::string text );
309 
310 private:
311  /**
312  * Denotes the current state this item is in.
313  */
314  enum State
315  {
316  Disabled = 0,
317  Normal,
318  Crashed
319  };
320 
321  /**
322  * Update the visual state of the item. Draw the item according to the state specified.
323  *
324  * \param state the state
325  */
326  void changeState( State state );
327 
328  std::shared_ptr< WModule > m_module; //!< the module
329 
330  QList< WQtNetworkInputPort* > m_inPorts; //!< the input ports of the item
331 
332  QList< WQtNetworkOutputPort* > m_outPorts; //!< the output ports of the item
333 
334  QRectF m_rect; //!< the size of the items rect
335 
336  float m_width; //!< the width of the rect
337 
338  float m_height; //!< the height of the rect
339 
340  std::string m_textFull; //!< always contains the unclipped text of m_text
341  QGraphicsTextItem* m_text; //!< the caption
342  std::string m_subtitleFull; //!< always contains the unclipped text of m_subtitle
343  QGraphicsTextItem* m_subtitle; //!< the caption
344 
345  WQtNetworkEditor* m_networkEditor; //!< the related WQtNetworkEditor
346 
347  QColor m_itemColor; //!< the color of the item. Depends on type (source, sink, ...).
348 
349  State m_currentState; //!< denotes the state the item currently is in
350 
351  WQtNetworkItemActivator *m_hidden; //!< indicator showing if module's graphics are activated (allows to activate it)
352 
353  /**
354  * If true, the item is hovered.
355  */
357 
358  /**
359  * If true, the item is selected.
360  */
362 
363  /**
364  * If true, a percentage is shown. If not, an animated bar walking around.
365  */
367 
368  /**
369  * Counter used for busy indication.
370  */
372 
373  /**
374  * If true, the busy indication is active.
375  */
377 
378  /**
379  * If true, the next call to update() will force a redraw. Avoid setting this to true permanently.
380  */
382 
383  /**
384  * Optimal width for this module. Calculated during construction.
385  */
387 
388  /**
389  * The property toolbox window if any.
390  */
392 
393  /**
394  * Animation for appearance and deletion
395  */
396  QGraphicsItemAnimation* m_animation;
397 
398  /**
399  * Timer for the animation.
400  */
401  QTimeLine* m_animationTimer;
402 
403  /**
404  * Animation for appearance and deletion
405  */
406  QGraphicsItemAnimation* m_removalAnimation;
407 
408  /**
409  * Timer for the animation.
410  */
412 
413  /**
414  * Keep track of the mouse dragging of this item
415  */
417 
418  /**
419  * Dragging?
420  */
422 
423  /**
424  * Start position of dragging
425  */
427 
428  /**
429  * Mark item as already positioned.
430  */
432 
433  /**
434  * Item has been placed manually. This flag is used by the layouter to avoid moving around manually placed items.
435  */
437 
438  /**
439  * If true, the mouse events do not cause a drag and move operation.
440  */
441  bool m_noDrag;
442 
443  /**
444  * Keep track of runtime name changes of this module.
445  */
446  boost::signals2::connection m_runtimeNameConnection;
447 
448  /**
449  * Update name as it has been changed.
450  */
451  void runtimeNameChanged();
452 
453  /**
454  * Update name as it has been changed. Runs in GUI thread.
455  */
457 
458 private slots:
459  /**
460  * Called when the m_removalAnimationTimer finishes.
461  */
462  void removalAnimationDone();
463 
464  /**
465  * Called when the animation timers tick and progress in timeline. Used to blend the item out.
466  *
467  * \param value the value between 0 and 1
468  */
469  void animationBlendOutTick( qreal value );
470 
471  /**
472  * Called when the animation timers tick and progress in timeline. Used to blend the item in.
473  *
474  * \param value the value between 0 and 1
475  */
476  void animationBlendInTick( qreal value );
477 
478  /**
479  * Called whenever a animated move was finished.
480  */
481  void moveFinished();
482 };
483 #endif // WQTNETWORKITEM_H
Container widget to hold the WQtNetworkScene.
This class represents the ports a module have.
Small graphics item that respresents whether a module in the network editor is active or not.
This class represents a WModule as QGraphicsItem and contains a reference to its in- and outports.
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
If the item is changed we want to get notified.
void changeState(State state)
Update the visual state of the item.
QString getText()
Get the caption as QString.
void setLayedOut(bool layedOut=true)
Marks the item as already layed out.
State
Denotes the current state this item is in.
std::string m_subtitleFull
always contains the unclipped text of m_subtitle
void dead(WQtNetworkItem *item)
The item is now dead.
void runtimeNameChangedGT()
Update name as it has been changed.
WQtNetworkItemActivator * m_hidden
indicator showing if module's graphics are activated (allows to activate it)
void addOutputPort(WQtNetworkOutputPort *outPort)
Add a port to the item.
void addInputPort(WQtNetworkInputPort *inPort)
Add a port to the item.
void setManuallyPlaced(bool manual=true)
Mark item as manually placed.
bool m_noDrag
If true, the mouse events do not cause a drag and move operation.
virtual ~WQtNetworkItem()
Destructor.
bool m_busyIsDetermined
If true, a percentage is shown.
void activate(bool active)
Here the module can be enabled when the WModule is ready.
bool m_dragging
Dragging?
void animatedMoveTo(QPointF pos)
Move item to specified position smoothly, via animation.
float m_width
the width of the rect
bool m_isSelected
If true, the item is selected.
bool m_isHovered
If true, the item is hovered.
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
If the cursor leaves the item, the item gets his default color.
bool m_busyIndicatorShow
If true, the busy indication is active.
virtual void updater()
Can be used for polling the module states.
boost::signals2::connection m_runtimeNameConnection
Keep track of runtime name changes of this module.
void moveFinished()
Called whenever a animated move was finished.
QColor m_itemColor
the color of the item. Depends on type (source, sink, ...).
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
If the WQtNetworkItem is moved, then the contained ports have to update the connected WQtNetworkArrow...
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsRectItem.
std::shared_ptr< WModule > getModule()
Get the WModule represented by this object.
void fitLook(float maximumWidth=WNETWORKITEM_MAXIMUM_WIDTH, float minimumWidth=WNETWORKITEM_MINIMUM_WIDTH)
This method aligns the in- and outports as well as the modulename in a regular way.
bool wasLayedOut() const
Checks whether the item was layed out earlier.
QGraphicsItemAnimation * m_animation
Animation for appearance and deletion.
void setTextItem(QGraphicsTextItem *text)
Set the QGraphicsTextItem ( the caption ) of the item.
QList< WQtNetworkInputPort * > getInPorts()
Returns the item inports.
WQtNetworkEditor * m_networkEditor
the related WQtNetworkEditor
void setCrashed()
Mark this module as crashed.
QTimeLine * m_removalAnimationTimer
Timer for the animation.
QList< WQtNetworkOutputPort * > m_outPorts
the output ports of the item
void removalAnimationDone()
Called when the m_removalAnimationTimer finishes.
std::string m_textFull
always contains the unclipped text of m_text
bool m_wasManuallyPlaced
Item has been placed manually.
bool m_wasLayedOut
Mark item as already positioned.
QGraphicsItemAnimation * m_removalAnimation
Animation for appearance and deletion.
void moveTo(QPointF pos)
Move item to specified position smoothly, no animation.
QGraphicsTextItem * m_subtitle
the caption
float m_itemBestWidth
Optimal width for this module.
int type() const
Reimplementation from QGraphicsItem.
void animationBlendOutTick(qreal value)
Called when the animation timers tick and progress in timeline.
bool m_forceUpdate
If true, the next call to update() will force a redraw.
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
Called upon double click.
QGraphicsTextItem * m_text
the caption
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Mouse was released.
void setText(std::string text)
Set the new title text.
State m_currentState
denotes the state the item currently is in
QWidget * m_propertyToolWindow
The property toolbox window if any.
void positionChanged()
Called whenever the item moves around.
WQtNetworkItem(WQtNetworkEditor *editor, std::shared_ptr< WModule > module)
Constructs new item in the network scene.
float m_height
the height of the rect
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
If the cursor enters the item, the item gets a green color.
std::shared_ptr< WModule > m_module
the module
QPointF m_dragStartPosition
Keep track of the mouse dragging of this item.
QTimeLine * die()
Animate removal and finally, instruct the owning network editor to remove the item.
QList< WQtNetworkInputPort * > m_inPorts
the input ports of the item
QRectF m_rect
the size of the items rect
void animationBlendInTick(qreal value)
Called when the animation timers tick and progress in timeline.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w)
Draw some customized stuff in the scene.
QTimeLine * m_animationTimer
Timer for the animation.
bool wasManuallyPlaced() const
Item was placed by hand.
QList< WQtNetworkOutputPort * > getOutPorts()
Returns the item outports.
QPointF m_draggingStart
Start position of dragging.
virtual QRectF boundingRect() const
The bounding area of the item.
void runtimeNameChanged()
Update name as it has been changed.
float m_busyPercent
Counter used for busy indication.
This class represents the ports a module have.