OpenWalnut  1.5.0dev
WPropertyWidget.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 #include <string>
27 
28 #include <QApplication>
29 
30 #include "../WGuiConsts.h"
31 #include "../events/WEventTypes.h"
32 #include "../events/WPropertyChangedEvent.h"
33 #include "WPropertyBoolWidget.h"
34 #include "WPropertyColorWidget.h"
35 #include "WPropertyDoubleWidget.h"
36 #include "WPropertyFilenameWidget.h"
37 #include "WPropertyIntWidget.h"
38 #include "WPropertyIntervalWidget.h"
39 #include "WPropertyMatrix4X4Widget.h"
40 #include "WPropertyPositionWidget.h"
41 #include "WPropertySelectionWidget.h"
42 #include "WPropertyStringWidget.h"
43 #include "WPropertyStructWidget.h"
44 #include "WPropertyTransferFunctionWidget.h"
45 #include "WPropertyTriggerWidget.h"
46 #include "WPropertyWidget.h"
47 #include "WQtPropertyGroupWidget.h"
48 #include "core/common/WLogger.h"
49 
50 WPropertyWidget::WPropertyWidget( std::shared_ptr< WPropertyBase > property, QGridLayout* propertyGrid, QWidget* parent ):
51  QStackedWidget( parent ),
52  m_property( property ),
53  m_propertyGrid( propertyGrid ),
54  m_label( this ),
55  m_separator( this ),
56  m_useLabel( m_propertyGrid ),
57  m_parameterWidgets(), // parent gets set by the QStackWidget
58  m_informationWidgets(), // parent gets set by the QStackWidget
59  m_invalid( false )
60 {
61  setObjectName( "ControlPanelPropertyWidget" );
62 
63  // define some colors
64  QPalette palette;
65  QColor defaultCol = palette.window().color();
66 
67  // label color
68  m_labelCol = defaultCol.darker( 115 );
69  // property color
70  m_propertyCol = defaultCol;
71  // separator color
72  m_sepCol = defaultCol.darker( 130 );
73 
74  m_errorCol = QColor( "#ff3543" );
75 
76  if( m_useLabel )
77  {
78  // initialize members
79  m_label.setMargin( 0 );
80  m_label.addAdditionalWidth( 8 ); // a 4 px margin around the label
81  m_label.setText( property->getName().c_str() );
82 
83  // set tooltips
84  m_label.setToolTip( getTooltip().c_str() );
85  setToolTip( m_label.toolTip() );
86 
87  // setup grid layout
88  int row = m_propertyGrid->rowCount();
89  m_propertyGrid->addWidget( &m_label, row, 0 );
90  m_propertyGrid->addWidget( this, row, 1 );
91  m_propertyGrid->setColumnStretch( 0, 0.0 );
92  m_propertyGrid->setColumnStretch( 1, 10000.0 );
93 
94  // ONLY style if in label mode
95  // set spearator style
96  m_separator.setFixedHeight( 1 );
97  // If you use QFrame for m_separator
98  // m_separator.setFrameShape( QFrame::HLine );
99  // m_separator.setFrameShadow( QFrame::Plain );
100  m_propertyGrid->addWidget( &m_separator, row + 1, 0, 1, 2 );
101  m_separator.setStyleSheet( "QWidget{ background-color:" + m_sepCol.name() + ";}" );
102 
103  // set style of label
104  m_label.setObjectName( "ControlPanelPropertyLabelWidget" );
105  // increase size of label to be the whole layout cell
106  m_label.setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Expanding ) );
107  m_label.setStyleSheet( " padding-left:1px; background-color:" + m_labelCol.name() + ";" );
108 
109  // set style of this property widget
110  setStyleSheet( "QStackedWidget#ControlPanelPropertyWidget{ background-color:" + m_propertyCol.name() +
111  "; margin-left:1px; margin-right:1px; }" );
112  }
113 
114  // add both widgets to the stacked widget, it then uses the first as default.
115  addWidget( &m_parameterWidgets );
116  addWidget( &m_informationWidgets );
117 
118  // if the purpose of the property is INFORMTION -> activate the information widget
119  if( m_property->getPurpose() == PV_PURPOSE_INFORMATION )
120  {
121  setCurrentIndex( 1 );
122  }
123 
124  // if the property is hidden initially, hide widget too
125  setHidden( m_property->isHidden() );
126  m_label.setHidden( m_property->isHidden() );
127  m_separator.setHidden( m_property->isHidden() );
128 
129  // setup the update callback
130  m_connection = m_property->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyWidget::requestUpdate, this ) );
131 }
132 
134 {
135  // cleanup
136  m_connection.disconnect();
137 }
138 
140 {
141  if( !force && ( m_property->getPurpose() != PV_PURPOSE_INFORMATION ) )
142  {
143  setCurrentIndex( 0 );
144  return;
145  }
146 
147  setCurrentIndex( 1 );
148 }
149 
151 {
152  QCoreApplication::postEvent( this, new WPropertyChangedEvent() );
153 }
154 
156 {
157  // a property changed
158  if( event->type() == WQT_PROPERTY_CHANGED_EVENT )
159  {
160  setHidden( m_property->isHidden() );
161  m_label.setHidden( m_property->isHidden() );
162  m_separator.setHidden( m_property->isHidden() );
163  update();
164  return true;
165  }
166 
167  return QWidget::event( event );
168 }
169 
170 std::string WPropertyWidget::getTooltip() const
171 {
172  std::string tip = "<b>Property: </b>" + m_property->getName() + "<br/>";
173  tip += "<b>Status: </b>";
174  tip += m_invalid ? "<font color=#FF0000><b>invalid</b></font>" : "valid";
175  tip += "<br/><br/>";
176  return tip + m_property->getDescription();
177 }
178 
179 std::shared_ptr< WPropertyBase > WPropertyWidget::getProperty()
180 {
181  return m_property;
182 }
183 
184 void WPropertyWidget::invalidate( bool invalid )
185 {
186  m_invalid = invalid;
187 
188  if( m_useLabel )
189  {
190  // update tooltip
191  m_label.setToolTip( getTooltip().c_str() );
192  setToolTip( m_label.toolTip() );
193 
194  if( invalid )
195  {
196  m_label.setStyleSheet( " padding-left:1px; background-color:" + m_errorCol.name() + "; font-weight: bold;" );
197  }
198  else
199  {
200  m_label.setStyleSheet( " padding-left:1px; background-color:" + m_labelCol.name() + ";" );
201  }
202  }
203 }
204 
206 {
207  return &m_parameterWidgets;
208 }
209 
211 {
212  return &m_informationWidgets;
213 }
214 
215 WPropertyWidget* WPropertyWidget::construct( WPropertyBase::SPtr property, QGridLayout* propertyGrid, QWidget* parent )
216 {
217  switch( property->getType() )
218  {
219  case PV_BOOL:
220  return new WPropertyBoolWidget( property->toPropBool(), propertyGrid, parent );
221  break;
222  case PV_INT:
223  return new WPropertyIntWidget( property->toPropInt(), propertyGrid, parent );
224  break;
225  case PV_DOUBLE:
226  return new WPropertyDoubleWidget( property->toPropDouble(), propertyGrid, parent );
227  break;
228  case PV_STRING:
229  return new WPropertyStringWidget( property->toPropString(), propertyGrid, parent );
230  break;
231  case PV_PATH:
232  return new WPropertyFilenameWidget( property->toPropFilename(), propertyGrid, parent );
233  break;
234  case PV_SELECTION:
235  return new WPropertySelectionWidget( property->toPropSelection(), propertyGrid, parent );
236  break;
237  case PV_COLOR:
238  return new WPropertyColorWidget( property->toPropColor(), propertyGrid, parent );
239  break;
240  case PV_POSITION:
241  return new WPropertyPositionWidget( property->toPropPosition(), propertyGrid, parent );
242  break;
243  case PV_TRIGGER:
244  return new WPropertyTriggerWidget( property->toPropTrigger(), propertyGrid, parent );
245  break;
246  case PV_STRUCT:
247  return new WPropertyStructWidget( property->toPropGroupBase(), propertyGrid, parent );
248  break;
249  case PV_MATRIX4X4:
250  return new WPropertyMatrix4X4Widget( property->toPropMatrix4X4(), propertyGrid, parent );
251  break;
252  case PV_TRANSFERFUNCTION:
253  return new WPropertyTransferFunctionWidget( property->toPropTransferFunction(), propertyGrid, parent );
254  break;
255  case PV_INTERVAL:
256  return new WPropertyIntervalWidget( property->toPropInterval(), propertyGrid, parent );
257  break;
258  default: // NOTE:: WPropGroup will be handled in WQtConrolPanel::buildPropWidget
259  WLogger::getLogger()->addLogMessage( "This property type is not yet supported.", "WPropertyWidget", LL_WARNING );
260  break;
261  }
262  return NULL;
263 }
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
Implements a property widget for WPropBool.
Event signalling a new module has been associated with the root container in the kernel.
Implements a property widget for WPropColor.
Implements a property widget for WPropDouble.
Implements a property widget for WPropColor.
Implements a property widget for WPropInt.
Implements a property widget for WPropInterval.
Implements a property widget for MATRIX4X4.
Implements a property widget for WPropDouble.
Implements a property widget for WPropSelection.
Implements a property widget for WPropString.
Implements a property widget for arbitrary WPropStruct.
Implements a property widget for WPropDouble.
Implements a property widget for WPropTrigger.
Class building the base for all widgets representing properties.
std::shared_ptr< WPropertyBase > m_property
The property handled by the widget.
QGridLayout * m_propertyGrid
The grid used to layout label and widget.
bool m_useLabel
If set to true, the widgets uses the control layout to combine the widget with a label.
QWidget m_informationWidgets
The widget containing a layout and provides the widgets for showing information properties.
QWidget m_separator
Separator after each property.
QWidget * getInformationWidgets()
Returns the info widget for this property.
static WPropertyWidget * construct(WPropertyBase::SPtr property, QGridLayout *propertyGrid=NULL, QWidget *parent=NULL)
Constructs a proper widget for the specified property.
WScaleLabel m_label
The label used to name the property.
WPropertyWidget(std::shared_ptr< WPropertyBase > property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
void forceInformationMode(bool force=true)
Force the widget to use the information widgets.
QColor m_errorCol
Color used for indicating errors.
virtual void requestUpdate()
Request an update of the property widget.
QWidget * getParameterWidgets()
Returns the parameter widget for this property.
QColor m_labelCol
The color to use for the property labels.
QColor m_propertyCol
The color to use for property widgets.
bool m_invalid
Flag denoting whether the widget is set to an invalid value.
boost::signals2::connection m_connection
The connection for propertyChangeNotifier().
QWidget m_parameterWidgets
The widget containing a layout and provides the edit widgets for the property.
virtual void invalidate(bool invalid=true)
This method marks this widget as invalid.
std::shared_ptr< WPropertyBase > getProperty()
Returns the handled property.
virtual ~WPropertyWidget()
Destructor.
QColor m_sepCol
The color to use for separators.
virtual void update()=0
Called whenever the widget should update itself.
virtual bool event(QEvent *event)
Custom event dispatcher.
virtual std::string getTooltip() const
Gets the tooltip that should be used for this widget.
virtual void addAdditionalWidth(int margin)
Set this to reserve extra space for a margin.
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93