OpenWalnut  1.5.0dev
WQtNetworkItemActivator.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 <iostream>
26 #include <memory>
27 
28 #include <QApplication>
29 #include <QGraphicsSceneMouseEvent>
30 #include <QPaintEvent>
31 #include <QPolygonF>
32 
33 #include "WQtNetworkColors.h"
34 #include "WQtNetworkEditorGlobals.h"
35 #include "WQtNetworkInputPort.h"
36 #include "WQtNetworkItemActivator.h"
37 #include "WQtNetworkOutputPort.h"
38 
39 WQtNetworkItemActivator::WQtNetworkItemActivator( std::shared_ptr< WModule > module )
40  : m_module( module ), m_activeColor( WQtNetworkColors::ActivatorActive ),
41  m_inactiveColor( WQtNetworkColors::ActivatorInactive )
42 {
43  float dist = 2.0;
44  // create the shape using a polygon
45  QPolygonF poly;
46  poly << QPointF( dist, dist ) << QPointF( dist + WNETWORKPORT_SIZEX, dist ) << QPointF( dist, dist + WNETWORKPORT_SIZEY );
47  setPolygon( poly );
48 
49  setPen( QPen( Qt::white, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
50  setAcceptHoverEvents( true );
51 
52  // to avoid polling during paint, subscribe to the change signal
53  m_notifierConnection = m_module->getProperties()->getProperty( "active" )->toPropBool()->getValueChangeCondition()->subscribeSignal(
55  );
56  m_needStateUpdate = true;
57 
58  if( m_module->getProperties()->getProperty( "active" )->toPropBool()->get() )
59  {
60  setToolTip( "<b>Active</b><br> Click to deactivate" );
61  }
62  else
63  {
64  setToolTip( "<b>Not</b> active.<br> Click to activate." );
65  }
66 }
67 
69 {
70 }
71 
73 {
74  return Type;
75 }
76 
78 {
79  // simply set a bool
80  m_needStateUpdate = true;
81  // we somehow need to force an update here. QCoreApplication::postEvent does not work here since the graphics classes are not derived from
82  // qobject.
83 }
84 
85 void WQtNetworkItemActivator::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget )
86 {
87  if( m_needStateUpdate )
88  {
89  m_needStateUpdate = false;
91  }
92  QGraphicsPolygonItem::paint( painter, option, widget );
93 }
94 
95 void WQtNetworkItemActivator::mousePressEvent( QGraphicsSceneMouseEvent *mouseEvent )
96 {
97  // if module is crashed -> do not de-activate module
98  if( m_module->isCrashed() )
99  {
100  mouseEvent->accept();
101  return;
102  }
103 
104  QList<QGraphicsItem *> startItem = scene()->items( mouseEvent->scenePos() );
105  if( !startItem.isEmpty() )
106  {
107  WPropBool active = m_module->getProperties()->getProperty( "active" )->toPropBool();
108  active->set( !active->get() );
109 
110  // update graphics
112  mouseEvent->accept();
113  }
114  else
115  {
116  mouseEvent->ignore();
117  }
118 }
119 
121 {
122  // if module is crashed -> do not de-activate module
123  if( m_module->isCrashed() )
124  {
125  return;
126  }
127 
128  if( m_module->getProperties()->getProperty( "active" )->toPropBool()->get() )
129  {
130  setBrush( QBrush( m_activeColor ) );
131  setToolTip( "<b>Active</b><br> Click to deactivate." );
132  }
133  else
134  {
135  setBrush( QBrush( m_inactiveColor ) );
136  setToolTip( "<b>Not</b> active<br> Click to activate." );
137  }
138 }
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
Start drawing an arrow temporary.
WQtNetworkItemActivator(std::shared_ptr< WModule > module)
Constructor for indicator showing if module is active.
std::shared_ptr< WModule > m_module
the module
bool m_needStateUpdate
If true, the module state changed.
void activeChangeNotifier()
Callback getting called by the module's active property to know about state changes.
boost::signals2::connection m_notifierConnection
The connection used for notification.
void handleActiveState()
Setups tooltip and brush acccording to state.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Actually paints the port.
QColor m_inactiveColor
color used for inactive indicator
virtual int type() const
Reimplementation from QGraphicsItem.
QColor m_activeColor
color used for active indicator