OpenWalnut  1.5.0dev
WPropertyIntWidget.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 
34 #include "../WGuiConsts.h"
35 #include "../WQtGui.h"
36 #include "../guiElements/WQtIntervalEdit.h"
37 #include "core/common/WLogger.h"
38 #include "core/common/WPropertyVariable.h"
39 
40 #include "WPropertyIntWidget.h"
41 
43 
44 WPropertyIntWidget::WPropertyIntWidget( WPropInt property, QGridLayout* propertyGrid, QWidget* parent ):
45  WPropertyWidget( property, propertyGrid, parent ),
46  m_integralProperty( property ),
47  m_slider( Qt::Horizontal, &m_parameterWidgets ),
48  m_edit( &m_parameterWidgets ),
49  m_layout(),
50  m_vLayout( &m_parameterWidgets ),
51  m_asText( &m_informationWidgets ),
52  m_infoLayout( &m_informationWidgets ),
53  m_intervalEdit( &m_parameterWidgets )
54 {
55  m_layout.addWidget( &m_slider );
56 
57  m_layout.addWidget( &m_edit );
58  m_layout.setMargin( WGLOBAL_MARGIN );
59  m_layout.setSpacing( WGLOBAL_SPACING );
60  m_vLayout.setMargin( WGLOBAL_MARGIN );
61  m_vLayout.setSpacing( WGLOBAL_SPACING );
62 
63  m_edit.setMinimumHeight( WMIN_WIDGET_HEIGHT );
64  m_slider.setMinimumHeight( WMIN_WIDGET_HEIGHT );
65 
66  // add the m_layout to the vlayout
67  QWidget* layoutContainer = new QWidget();
68  layoutContainer->setLayout( &m_layout );
69  m_vLayout.addWidget( layoutContainer );
70 
71  // configure the interval edit
72  m_vLayout.addWidget( &m_intervalEdit );
73  if( !WQtGui::getSettings().value( "qtgui/sliderMinMaxEdit", false ).toBool() )
74  {
75  m_intervalEdit.hide();
76  }
77 
78  m_parameterWidgets.setLayout( &m_vLayout );
79 
80  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
81  m_infoLayout.addWidget( &m_asText );
82  m_infoLayout.setMargin( WGLOBAL_MARGIN );
83  m_infoLayout.setSpacing( WGLOBAL_SPACING );
84  m_informationWidgets.setLayout( &m_infoLayout );
85 
86  m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );
87 
88  update();
89 
90  // connect the modification signal of the edit and slider with our callback
91  connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
92  connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
93  connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
94  connect( &m_intervalEdit, SIGNAL( minimumChanged() ), this, SLOT( minMaxUpdated() ) );
95  connect( &m_intervalEdit, SIGNAL( maximumChanged() ), this, SLOT( minMaxUpdated() ) );
96 }
97 
99 {
100  // cleanup
101 }
102 
104 {
105  // // calculate maximum size of the text widget.
106  // // XXX: this is not the optimal way but works for now
107  // NO, it doesn't work on Mac OS X: You won't be able to any digits in it!, So I reset it to default which should work on other platforms too
108  QString valStr = QString::number( m_integralProperty->get() );
109  m_edit.setText( valStr );
110 
111  // get the min constraint
114  bool minMaxConstrained = minC && maxC;
115  if( minMaxConstrained )
116  {
117  // setup the slider
118  m_slider.setMinimum( 0 );
119  m_slider.setMaximum( SliderResolution );
120 
121  // update the interval edit too
122  m_intervalEdit.setAllowedMin( minC->getMin() );
123  m_intervalEdit.setAllowedMax( maxC->getMax() );
126 
127  // updating the interval edit causes the proper values to be set in m_min and m_max
128  m_slider.setHidden( false );
129  m_intervalEdit.setHidden( !WQtGui::getSettings().value( "qtgui/sliderMinMaxEdit", false ).toBool() );
130  m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
131  }
132  else
133  {
134  m_slider.setHidden( true );
135  m_intervalEdit.setHidden( true );
136  }
137 
138  // do not forget to update the label
139  m_asText.setText( valStr );
140 }
141 
143 {
144  int perc = static_cast< double >( SliderResolution ) * ( ( value - m_min ) / ( m_max - m_min ) );
145  return std::min( std::max( perc, 0 ), SliderResolution );
146 }
147 
149 {
150  return ( static_cast< double >( perc ) / static_cast< double >( SliderResolution ) ) * ( m_max - m_min ) + m_min;
151 }
152 
154 {
155  if( !m_slider.isHidden() && toSliderValue( m_integralProperty->get() ) != value )
156  {
157  // set to the property
158  invalidate( !m_integralProperty->set( fromSliderValue( value ) ) ); // NOTE: set automatically checks the validity of the value
159 
160  // set the value in the line edit
161  m_edit.setText( QString::number( m_integralProperty->get() ) );
162  }
163 }
164 
166 {
167  // set the value in the line edit
168  bool valid;
169  int value = m_edit.text().toInt( &valid );
170  if( !valid )
171  {
172  invalidate();
173  return;
174  }
175  // set to the property
176  invalidate( !m_integralProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
177 
178  // update slider
179  m_slider.setValue( toSliderValue( value ) );
180 }
181 
182 void WPropertyIntWidget::textEdited( const QString& text )
183 {
184  // this method does NOT set the property actually, but tries to validate it
185  bool valid;
186  int value = text.toInt( &valid );
187  if( !valid )
188  {
189  invalidate();
190  return;
191  }
192 
193  // simply check validity
194  invalidate( !m_integralProperty->accept( value ) );
195 }
196 
198 {
201 
202  if( m_min > m_integralProperty->get() )
203  {
204  m_integralProperty->set( m_min );
205  }
206  if( m_max < m_integralProperty->get() )
207  {
208  m_integralProperty->set( m_max );
209  }
210 
211  m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
212 }
213 
WPropertyIntWidget(WPropInt property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
void textEdited(const QString &text)
Called when the text in m_edit changes.
virtual void update()
Called whenever the widget should update.
QLineEdit m_edit
The edit field showing the value of the slider.
double m_max
The current maximum value.
void sliderChanged(int value)
Called whenever the slider changes.
WQtIntervalEdit< double, int32_t > m_intervalEdit
The edit for the interval.
WScaleLabel m_asText
Used to show the property as text.
virtual ~WPropertyIntWidget()
Destructor.
QSlider m_slider
The slider allowing modification of the integer value.
int toSliderValue(double value)
Converts a given value to a slider value between m_min and m_max.
void minMaxUpdated()
Called whenever the interval edit changes.
QVBoxLayout m_vLayout
Layout used to combine the property widgets with the WQtIntervalEdit.
void editChanged()
Called whenever the edit field changes.
double fromSliderValue(int perc)
Converts the given slider value to the real double value using m_min and m_max.
double m_min
The current minimum value.
static int SliderResolution
Resolution of the slider.
QHBoxLayout m_layout
Layout used to position the label and the checkbox.
WPropInt m_integralProperty
The property represented by this widget.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
std::shared_ptr< WPropertyConstraintMin< T > > PropertyConstraintMin
Alias for min constraints.
std::shared_ptr< WPropertyConstraintMax< T > > PropertyConstraintMax
Alias for max constraints.
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 QSettings & getSettings()
Returns the settings object.
Definition: WQtGui.cpp:394
void setAllowedMin(DataType min=std::numeric_limits< DataType >::min())
Set the allowed minimum.
const DataType & getMax() const
Get the currently selected maximum.
void setAllowedMax(DataType max=std::numeric_limits< DataType >::max())
Set the allowed maximum.
const DataType & getMin() const
Get the currently selected minimum.
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93