OpenWalnut  1.5.0dev
WPropertyIntervalWidget.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 <algorithm>
26 #include <cmath>
27 #include <limits>
28 #include <sstream>
29 #include <iostream>
30 #include <string>
31 
32 #include <QInputDialog>
33 #include <QAction>
34 
35 #include "../WGuiConsts.h"
36 #include "../WQtGui.h"
37 #include "../guiElements/WQtIntervalEdit.h"
38 #include "core/common/WLogger.h"
39 #include "core/common/WPropertyVariable.h"
40 
41 #include "WPropertyIntervalWidget.h"
42 
43 WPropertyIntervalWidget::WPropertyIntervalWidget( WPropInterval property, QGridLayout* propertyGrid, QWidget* parent ):
44  WPropertyWidget( property, propertyGrid, parent ),
45  m_intervalProperty( property ),
46  m_layout(),
47  m_vLayout( &m_parameterWidgets ),
48  m_asText( &m_informationWidgets ),
49  m_infoLayout( &m_informationWidgets ),
50  m_minEdit( &m_parameterWidgets ),
51  m_maxEdit( &m_parameterWidgets ),
52  m_resetBtn( &m_parameterWidgets )
53 {
54  // layout all the elements
55  m_layout.addWidget( new QLabel( "[" ) );
56  m_layout.addWidget( &m_minEdit );
57  m_layout.addWidget( new QLabel( "," ) );
58  m_layout.addWidget( &m_maxEdit );
59  m_layout.addWidget( new QLabel( "]" ) );
60 
61  m_layout.addWidget( &m_resetBtn );
62  // WPropertyVartiable does not yet provide a default value mechanism.
63  m_resetBtn.setHidden( true );
64 
65  m_minEdit.setMinimumHeight( WMIN_WIDGET_HEIGHT );
66  m_maxEdit.setMinimumHeight( WMIN_WIDGET_HEIGHT );
67 
68  // create resetButton action
69  QAction* reset = new QAction( WQtGui::getIconManager()->getIcon( "undo" ), "Reset to defaults", &m_resetBtn );
70  m_resetBtn.setDefaultAction( reset );
71 
72  m_layout.setMargin( WGLOBAL_MARGIN );
73  m_layout.setSpacing( WGLOBAL_SPACING );
74 
75  m_vLayout.setMargin( WGLOBAL_MARGIN );
76  m_vLayout.setSpacing( WGLOBAL_SPACING );
77 
78  // add the m_layout to the vlayout
79  QWidget* layoutContainer = new QWidget();
80  layoutContainer->setLayout( &m_layout );
81  m_vLayout.addWidget( layoutContainer );
82 
83  m_parameterWidgets.setLayout( &m_vLayout );
84 
85  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
86  m_infoLayout.addWidget( &m_asText );
87  m_infoLayout.setMargin( WGLOBAL_MARGIN );
88  m_infoLayout.setSpacing( WGLOBAL_SPACING );
89  m_informationWidgets.setLayout( &m_infoLayout );
90 
91  update();
92 
93  // connect the modification signal of the edit and slider with our callback
94  connect( &m_minEdit, SIGNAL( textEdited( const QString& ) ), this, SLOT( minMaxUpdated() ) );
95  connect( &m_maxEdit, SIGNAL( textEdited( const QString& ) ), this, SLOT( minMaxUpdated() ) );
96  connect( reset, SIGNAL( triggered( bool ) ), this, SLOT( reset() ) );
97 }
98 
100 {
101  // cleanup
102 }
103 
105 {
106  QString lowValStr = QString::number( m_intervalProperty->get().getLower() );
107  QString upValStr = QString::number( m_intervalProperty->get().getUpper() );
108  m_minEdit.setText( lowValStr );
109  m_maxEdit.setText( upValStr );
110 
111  // do not forget to update the label
112  m_asText.setText( "[" + lowValStr + ", " + upValStr + "]" );
113 }
114 
116 {
117  bool validMin;
118  double minValue = m_minEdit.text().toDouble( &validMin );
119  bool validMax;
120  double maxValue = m_maxEdit.text().toDouble( &validMax );
121 
122  // mark the prop invalid if the user specifies some strange values
123  if( !( validMin && validMax ) || ( minValue > maxValue ) )
124  {
125  invalidate();
126  return;
127  }
128 
129  // set to the property
130  invalidate( !m_intervalProperty->set( WIntervalDouble( minValue, maxValue ) ) ); // NOTE: set automatically checks the validity of the value
131 }
132 
134 {
135  // WProperties does not really provide a default value mechanism yet.
136 }
137 
WPropInterval m_intervalProperty
The property represented by this widget.
QLineEdit m_minEdit
Minimum Value.
WScaleLabel m_asText
Used to show the property as text.
virtual void update()
Called whenever the widget should update.
QToolButton m_resetBtn
Reset button.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
WPropertyIntervalWidget(WPropInterval property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
QLineEdit m_maxEdit
Maximum Value.
QHBoxLayout m_layout
Layout used to position the label and the checkbox.
void reset()
Reset to default.
virtual ~WPropertyIntervalWidget()
Destructor.
void minMaxUpdated()
Called whenever the interval edit changes.
QVBoxLayout m_vLayout
Layout used to combine the property widgets with the WQtIntervalEdit.
Class building the base for all widgets representing properties.
QWidget m_informationWidgets
The widget containing a layout and provides the widgets for showing information properties.
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.
static WIconManager * getIconManager()
Get the icon manager of this gui instance.
Definition: WQtGui.cpp:93
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93