OpenWalnut  1.5.0dev
WQtNetworkPort.cpp
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 #include <memory>
26 
27 #include <QGraphicsSceneMouseEvent>
28 
29 #include "WQtNetworkColors.h"
30 #include "WQtNetworkEditorGlobals.h"
31 #include "WQtNetworkInputPort.h"
32 #include "WQtNetworkOutputPort.h"
33 #include "WQtNetworkPort.h"
34 #include "core/kernel/combiner/WApplyCombiner.h"
35 
36 WQtNetworkPort::WQtNetworkPort():
38 {
39  setRect( 0.0, 0.0, WNETWORKPORT_SIZEX, WNETWORKPORT_SIZEY );
40  setPen( QPen( Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
41  m_brushNotSet = true;
42 
43  setAcceptHoverEvents( true );
44  m_arrow = NULL;
45 }
46 
47 WQtNetworkPort::~WQtNetworkPort()
48 {
49  removeArrows();
50 }
51 
52 void WQtNetworkPort::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget )
53 {
54  // as the command setBrush forces a re-paint, permanently calling setbrush causes the widget to permanently redraw itself.
55  if( m_brushNotSet )
56  {
57  m_brushNotSet = false;
58  if( isOutPort() )
59  {
60  setBrush( WQtNetworkColors::OutputConnector );
61  }
62  else
63  {
64  setBrush( WQtNetworkColors::InputConnector );
65  }
66  }
67 
68  QGraphicsRectItem::paint( painter, option, widget );
69 
70  // add some nice triangle?
71  // QPolygonF triangle;
72  // triangle.clear();
73  // triangle << QPointF( 0, 0 );
74  // triangle << QPointF( WNETWORKPORT_SIZEX, 0 );
75  // triangle << QPointF( static_cast< float >( WNETWORKPORT_SIZEX ) / 2.0 , WNETWORKPORT_SIZEY / 2.0 );
76  // painter->setBrush( QBrush( Qt::black ) );
77  // painter->drawPolygon( triangle );
78 }
79 
80 void WQtNetworkPort::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* mouseEvent )
81 {
82  QGraphicsItem::mouseDoubleClickEvent( mouseEvent );
83 
84  // ignore all buttons but the left one
85  if( mouseEvent->button() != Qt::LeftButton )
86  {
87  mouseEvent->ignore();
88  return;
89  }
90 
91  QList<QGraphicsItem *> startItem = scene()->items( mouseEvent->scenePos() );
92  if( !startItem.isEmpty() )
93  {
94  mouseEvent->accept();
95  WQtNetworkOutputPort* outP = dynamic_cast< WQtNetworkOutputPort* >( startItem.first() );
96  WQtNetworkInputPort* inP = dynamic_cast< WQtNetworkInputPort* >( startItem.first() );
97  // delete all connections
98  if( inP )
99  {
100  inP->getConnector()->disconnectAll();
101  }
102  if( outP )
103  {
104  outP->getConnector()->disconnectAll();
105  }
106  }
107  else
108  {
109  mouseEvent->ignore();
110  }
111 }
112 
113 void WQtNetworkPort::mousePressEvent( QGraphicsSceneMouseEvent *mouseEvent )
114 {
115  if( mouseEvent->button() != Qt::LeftButton )
116  {
117  mouseEvent->ignore();
118  return;
119  }
120  QList<QGraphicsItem *> startItem = scene()->items( mouseEvent->scenePos() );
121  if( !startItem.isEmpty() )
122  {
123  mouseEvent->accept();
124  if( startItem.first()->type() == WQtNetworkOutputPort::Type &&
125  startItem.first()->parentItem()->isEnabled() == true )
126  {
127  // use might have started to drag
128  m_arrow = new WQtNetworkArrow( qgraphicsitem_cast< WQtNetworkOutputPort* >( startItem.first() ), NULL );
129  m_arrow->startDrag( mouseEvent->scenePos() );
130  scene()->addItem( m_arrow );
131  }
132  }
133  else
134  {
135  mouseEvent->ignore();
136  }
137 }
138 
139 void WQtNetworkPort::mouseMoveEvent( QGraphicsSceneMouseEvent *mouseEvent )
140 {
141  if( m_arrow )
142  {
143  m_arrow->moveDrag( mouseEvent->scenePos() );
144  }
145 }
146 
147 void WQtNetworkPort::mouseReleaseEvent( QGraphicsSceneMouseEvent *mouseEvent )
148 {
149  Q_UNUSED( mouseEvent );
150 
151  if( m_arrow )
152  {
153  m_arrow->doneDrag( mouseEvent->scenePos() );
154  scene()->removeItem( m_arrow );
155  delete m_arrow;
156  }
157 }
158 
159 void WQtNetworkPort::alignPosition( int size, int portNumber, QRectF rect, bool outPort )
160 {
161  if( outPort == false )
162  {
163  setPos( rect.width() / ( size + 1 ) * portNumber - 5.0, -this->rect().height() / 2 + 3 );
164  }
165  else if( outPort == true )
166  {
167  setPos( rect.width() / ( size + 1 ) * portNumber - 5.0, rect.height() - this->rect().height() / 2 - 3 );
168  }
169 }
170 
172 {
173  return ( 5 + WNETWORKPORT_SIZEX ) * nbPorts;
174 }
175 
177 {
178  int index = m_arrows.indexOf( arrow );
179 
180  if( index != -1 )
181  {
182  m_arrows.removeAt( index );
183  }
184 }
185 
187 {
188  foreach( WQtNetworkArrow *arrow, m_arrows )
189  {
190  int index = m_arrows.indexOf( arrow );
191  if( index != -1 )
192  {
193  m_arrows.removeAt( index );
194  }
195  delete arrow;
196  }
197 }
198 
200 {
201  return m_arrows;
202 }
203 
205 {
206  m_arrows.append( arrow );
207 }
208 
210 {
211  foreach( WQtNetworkArrow *arrow, m_arrows )
212  {
213  arrow->updatePosition();
214  }
215 }
216 
218 {
219  return m_arrows.size();
220 }
221 
223 {
224  return m_name;
225 }
226 
227 void WQtNetworkPort::setPortName( QString str )
228 {
229  m_name = str;
230 }
231 
232 void WQtNetworkPort::setOutPort( bool type )
233 {
234  m_isOutPort = type;
235 }
236 
238 {
239  return m_isOutPort;
240 }
241 
This Class is needed for connecting two ports and drawing a line between them.
void updatePosition(QPointF deviate=QPointF())
Calculated the new position of the lines endpoints in the scene.
void doneDrag(const QPointF &pos)
Called when releasing the mouse.
void moveDrag(const QPointF &pos)
Update drag position.
void startDrag(const QPointF &pos)
Start Drag.
This class represents the ports a module have.
std::shared_ptr< WModuleInputConnector > getConnector()
Returns the WModuleInputConnecter that belongs to this object.
This class represents the ports a module have.
std::shared_ptr< WModuleOutputConnector > getConnector()
Returns the WModuleOutputConnecter that belongs to this object.
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.