OpenWalnut  1.5.0dev
WQtNetworkPort.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 WQTNETWORKPORT_H
26 #define WQTNETWORKPORT_H
27 
28 #include <QGraphicsRectItem>
29 #include <QGraphicsPolygonItem>
30 #include <QGraphicsLineItem>
31 
32 #include "WQtNetworkArrow.h"
33 #include "WQtNetworkPort.h"
34 
35 // forward declaration
36 //class WQtNetworkArrow;
37 //class WQtNetworkInputPort;
38 //class WQtNetworkOutputPort;
39 
40 /**
41  * Abstract class to distinguish between input- and output ports from a module.
42  * This class handels if a connection between two ports is possible or not.
43  */
45 {
46 public:
48 
49  ~WQtNetworkPort();
50 
51  /**
52  * Reimplementation from QGraphicsItem
53  * \return the type of the item as int
54  */
55  virtual int type() const = 0;
56 
57  /**
58  * The position of every arrow connected with this port is updating its position in the
59  * scene.
60  */
61  virtual void updateArrows();
62 
63  /**
64  * Removes a specific arrow.
65  * \param arrow an arrow
66  */
67  virtual void removeArrow( WQtNetworkArrow *arrow );
68 
69  /**
70  * Removes all connected arrows.
71  */
72  virtual void removeArrows();
73 
74  /**
75  * Calculates the position inside a item for each port to get a correct
76  * alignment
77  * \param size the total number of ports ( distinguished by in- and
78  * outport
79  * \param portNumber the number of the current port (distinguised by in-
80  * and outport
81  * \param rect the rect of the parent item
82  * \param outPort is it an in- or outport
83  */
84  void alignPosition( int size, int portNumber, QRectF rect, bool outPort );
85 
86  /**
87  * Set the type of the port.
88  * \param type true if out / false if in
89  */
90  virtual void setOutPort( bool type );
91 
92  /**
93  * Returns the porttype - true if outputport, false if inputport
94  * \return is it a outport?
95  */
96  virtual bool isOutPort();
97 
98  /**
99  * Returns the portname.
100  * \return portname
101  */
102  virtual QString getPortName();
103 
104  /**
105  * Set the Name
106  * \param str portname as string
107  */
108  virtual void setPortName( QString str );
109 
110  /**
111  * Return the number of connections
112  *
113  * \return number of connections
114  */
115  virtual int getNumberOfArrows();
116 
117  /**
118  * Adds an arrow to the port
119  *
120  * \param arrow the arrow to add
121  */
122  virtual void addArrow( WQtNetworkArrow* arrow );
123 
124  /**
125  * Get a QList of all arrows connected to this port
126  *
127  * \return a QList of WQtNetworkArrows
128  */
130 
131  /**
132  * Actually paints the port.
133  *
134  * \param painter the painter
135  * \param option style options
136  * \param widget parent widget.
137  */
138  virtual void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget );
139 
140  /**
141  * Calculates the spaced needed for the given amount of ports.
142  *
143  * \param nbPorts width of this amount of ports
144  *
145  * \return the width
146  */
147  static float getMultiplePortWidth( size_t nbPorts );
148 
149 protected:
150  /**
151  * Start drawing an arrow temporary.
152  *
153  * \param mouseEvent the mouse event
154  */
155  void mousePressEvent( QGraphicsSceneMouseEvent *mouseEvent );
156 
157  /**
158  * Double click on port
159  *
160  * \param mouseEvent the event
161  */
162  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent* mouseEvent );
163 
164  /**
165  * Updates the temporary arrows endpoint.
166  * Arrow is colored green when connection possible, red if no connection
167  * is possible, or black when cursor doesent covers an WQtNetworkPort.
168  *
169  * \param mouseEvent the mouse event
170  */
171  void mouseMoveEvent( QGraphicsSceneMouseEvent *mouseEvent );
172 
173  /**
174  * Send a connect request to kernel when start- and endport are
175  * connectable
176  *
177  * \param mouseEvent the mouse event
178  */
179  void mouseReleaseEvent( QGraphicsSceneMouseEvent *mouseEvent );
180 
181  QList< WQtNetworkArrow *> m_arrows; //!< the connected arrows
182 
183  bool m_isOutPort; //!< is the port an outport
184 
185  QString m_name; //!< the portname
186 
187 private:
188  /**
189  * The arrow used to connect items
190  */
192 
193  bool m_brushNotSet; //!< used to indicate that the correct brush was not yet set.
194 };
195 #endif // WQTNETWORKPORT_H
This Class is needed for connecting two ports and drawing a line between them.
Abstract class to distinguish between input- and output ports from a module.
void alignPosition(int size, int portNumber, QRectF rect, bool outPort)
Calculates the position inside a item for each port to get a correct alignment.
static float getMultiplePortWidth(size_t nbPorts)
Calculates the spaced needed for the given amount of ports.
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
Updates the temporary arrows endpoint.
virtual void removeArrow(WQtNetworkArrow *arrow)
Removes a specific arrow.
WQtNetworkArrow * m_arrow
The arrow used to connect items.
QList< WQtNetworkArrow * > m_arrows
the connected arrows
QString m_name
the portname
virtual void updateArrows()
The position of every arrow connected with this port is updating its position in the scene.
virtual void removeArrows()
Removes all connected arrows.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
Send a connect request to kernel when start- and endport are connectable.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
Double click on port.
bool m_isOutPort
is the port an outport
virtual void setOutPort(bool type)
Set the type of the port.
virtual bool isOutPort()
Returns the porttype - true if outputport, false if inputport.
bool m_brushNotSet
used to indicate that the correct brush was not yet set.
virtual QString getPortName()
Returns the portname.
virtual void setPortName(QString str)
Set the Name.
virtual int type() const =0
Reimplementation from QGraphicsItem.
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
Start drawing an arrow temporary.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Actually paints the port.
virtual QList< WQtNetworkArrow * > getArrowList()
Get a QList of all arrows connected to this port.
virtual void addArrow(WQtNetworkArrow *arrow)
Adds an arrow to the port.
virtual int getNumberOfArrows()
Return the number of connections.