diff --git a/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.cpp b/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.cpp index bdf766a5e5eb032ade6290794ad18834cd5157ad..88574f40219ff94fee3991b6f27dcc98ce4d3e22 100644 --- a/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.cpp +++ b/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.cpp @@ -25,62 +25,54 @@ #include #include #include +#include #include #include #include "../WGuiConsts.h" #include "../WQt4Gui.h" +#include "../guiElements/WQtIntervalEdit.h" #include "core/common/WLogger.h" #include "core/common/WPropertyVariable.h" #include "WPropertyDoubleWidget.h" #include "WPropertyDoubleWidget.moc" +const int WPropertyDoubleWidget::SliderResolution = 10000; + WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ): WPropertyWidget( property, propertyGrid, parent ), - m_doubleProperty( property ), + m_integralProperty( property ), m_slider( Qt::Horizontal, &m_parameterWidgets ), m_edit( &m_parameterWidgets ), - m_minButton( &m_parameterWidgets ), - m_maxButton( &m_parameterWidgets ), - m_layout( &m_parameterWidgets ), + m_layout(), + m_vLayout( &m_parameterWidgets ), m_asText( &m_informationWidgets ), - m_infoLayout( &m_informationWidgets ) + m_infoLayout( &m_informationWidgets ), + m_intervalEdit( &m_parameterWidgets ) { - // layout both against each other - if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) - { - m_layout.addWidget( &m_minButton ); - m_minButton.setToolTip( "Set minimum of slider.\n Be aware that strange values can cause errors." ); - m_minButton.setMinimumWidth( 5 ); - m_minButton.setMaximumWidth( 5 ); - } - else - { - m_minButton.hide(); - } - m_layout.addWidget( &m_slider ); - if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) - { - m_layout.addWidget( &m_maxButton ); - m_maxButton.setToolTip( "Set maximum of slider.\n Be aware that strange values can cause errors." ); - m_maxButton.setMinimumWidth( 5 ); - m_maxButton.setMaximumWidth( 5 ); - } - else - { - m_maxButton.hide(); - } - m_layout.addWidget( &m_edit ); m_layout.setMargin( WGLOBAL_MARGIN ); m_layout.setSpacing( WGLOBAL_SPACING ); + m_vLayout.setMargin( WGLOBAL_MARGIN ); + m_vLayout.setSpacing( WGLOBAL_SPACING ); + + // add the m_layout to the vlayout + QWidget* layoutContainer = new QWidget(); + layoutContainer->setLayout( &m_layout ); + m_vLayout.addWidget( layoutContainer ); + // configure the interval edit + m_vLayout.addWidget( &m_intervalEdit ); + if( !WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) + { + m_intervalEdit.hide(); + } - m_parameterWidgets.setLayout( &m_layout ); + m_parameterWidgets.setLayout( &m_vLayout ); // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION ) m_infoLayout.addWidget( &m_asText ); @@ -96,8 +88,8 @@ WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) ); connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) ); connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) ); - connect( &m_maxButton, SIGNAL( pressed() ), this, SLOT( maxPressed() ) ); - connect( &m_minButton, SIGNAL( pressed() ), this, SLOT( minPressed() ) ); + connect( &m_intervalEdit, SIGNAL( minimumChanged() ), this, SLOT( minMaxUpdated() ) ); + connect( &m_intervalEdit, SIGNAL( maximumChanged() ), this, SLOT( minMaxUpdated() ) ); } WPropertyDoubleWidget::~WPropertyDoubleWidget() @@ -110,52 +102,60 @@ void WPropertyDoubleWidget::update() // // calculate maximum size of the text widget. // // XXX: this is not the optimal way but works for now // 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 - QString valStr = QString::number( m_doubleProperty->get() ); + QString valStr = QString::number( m_integralProperty->get() ); m_edit.setText( valStr ); // get the min constraint - WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin(); - WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax(); + WPVDouble::PropertyConstraintMin minC = m_integralProperty->getMin(); + WPVDouble::PropertyConstraintMax maxC = m_integralProperty->getMax(); bool minMaxConstrained = minC && maxC; if( minMaxConstrained ) { // setup the slider m_slider.setMinimum( 0 ); - m_slider.setMaximum( 100 ); - m_min = minC->getMin(); - m_max = maxC->getMax(); + m_slider.setMaximum( SliderResolution ); + + // update the interval edit too + m_intervalEdit.setAllowedMin( minC->getMin() ); + m_intervalEdit.setAllowedMax( maxC->getMax() ); + m_min = m_intervalEdit.getMin(); + m_max = m_intervalEdit.getMax(); + // updating the interval edit causes the proper values to be set in m_min and m_max m_slider.setHidden( false ); - m_slider.setValue( toPercent( m_doubleProperty->get() ) ); + m_intervalEdit.setHidden( false ); + m_slider.setValue( toSliderValue( m_integralProperty->get() ) ); } else { m_slider.setHidden( true ); + m_intervalEdit.setHidden( true ); } // do not forget to update the label m_asText.setText( valStr ); } -int WPropertyDoubleWidget::toPercent( double value ) +int WPropertyDoubleWidget::toSliderValue( double value ) { - return 100.0 * ( ( value - m_min ) / ( m_max - m_min ) ); + int perc = static_cast< double >( SliderResolution ) * ( ( value - m_min ) / ( m_max - m_min ) ); + return std::min( std::max( perc, 0 ), SliderResolution ); } -double WPropertyDoubleWidget::fromPercent( int perc ) +double WPropertyDoubleWidget::fromSliderValue( int perc ) { - return ( static_cast< double >( perc ) / 100.0 ) * ( m_max - m_min ) + m_min; + return ( static_cast< double >( perc ) / static_cast< double >( SliderResolution ) ) * ( m_max - m_min ) + m_min; } void WPropertyDoubleWidget::sliderChanged( int value ) { - if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value ) + if( !m_slider.isHidden() && toSliderValue( m_integralProperty->get() ) != value ) { // set to the property - invalidate( !m_doubleProperty->set( fromPercent( value ) ) ); // NOTE: set automatically checks the validity of the value + invalidate( !m_integralProperty->set( fromSliderValue( value ) ) ); // NOTE: set automatically checks the validity of the value // set the value in the line edit - m_edit.setText( QString::number( m_doubleProperty->get() ) ); + m_edit.setText( QString::number( m_integralProperty->get() ) ); } } @@ -171,10 +171,10 @@ void WPropertyDoubleWidget::editChanged() return; } // set to the property - invalidate( !m_doubleProperty->set( value ) ); // NOTE: set automatically checks the validity of the value + invalidate( !m_integralProperty->set( value ) ); // NOTE: set automatically checks the validity of the value // update slider - m_slider.setValue( toPercent( value ) ); + m_slider.setValue( toSliderValue( value ) ); } void WPropertyDoubleWidget::textEdited( const QString& text ) @@ -190,41 +190,23 @@ void WPropertyDoubleWidget::textEdited( const QString& text ) } // simply check validity - invalidate( !m_doubleProperty->accept( value ) ); + invalidate( !m_integralProperty->accept( value ) ); } - -void WPropertyDoubleWidget::maxPressed() +void WPropertyDoubleWidget::minMaxUpdated() { - bool ok; - double newMax = QInputDialog::getDouble( this, "Setting Maximum of Slider", - "Maximum", m_max, m_min, std::numeric_limits< double >::max(), 18, &ok ); - if( ok ) + m_min = m_intervalEdit.getMin(); + m_max = m_intervalEdit.getMax(); + + if( m_min > m_integralProperty->get() ) { - m_max = newMax; - if( m_doubleProperty->get() > newMax ) - { - m_doubleProperty->set( newMax ); - m_slider.setValue( toPercent( newMax ) ); - } - m_doubleProperty->setMax( newMax ); + m_integralProperty->set( m_min ); } -} - -void WPropertyDoubleWidget::minPressed() -{ - bool ok; - double newMin = QInputDialog::getDouble( this, "Setting Minimum of Slider", - "Minimum", m_min, -std::numeric_limits< double >::max(), m_max, 18, &ok ); - if( ok ) + if( m_max < m_integralProperty->get() ) { - m_min = newMin; - if( m_doubleProperty->get() < newMin ) - { - m_doubleProperty->set( newMin ); - m_slider.setValue( toPercent( newMin ) ); - } - m_doubleProperty->setMin( newMin ); + m_integralProperty->set( m_max ); } + + m_slider.setValue( toSliderValue( m_integralProperty->get() ) ); } diff --git a/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.h b/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.h index 6e7ddbfff087666d8331d64b1acd0fe0c271bdef..1a76fab892e3f14cff58306fd5ba1fc29910b3de 100644 --- a/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.h +++ b/src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.h @@ -32,6 +32,7 @@ #include #include +#include "../guiElements/WQtIntervalEdit.h" #include "WPropertyWidget.h" /** @@ -62,9 +63,9 @@ protected: virtual void update(); /** - * The integer property represented by this widget. + * The property represented by this widget. */ - WPropDouble m_doubleProperty; + WPropDouble m_integralProperty; /** * The slider allowing modification of the integer value @@ -77,37 +78,32 @@ protected: QLineEdit m_edit; /** - * The button bringing up the dialog for modifying min of the slider - */ - QToolButton m_minButton; - - /** - * The button bringing up the dialog for modifying max of the slider + * Layout used to position the label and the checkbox */ - QToolButton m_maxButton; + QHBoxLayout m_layout; /** - * Layout used to position the label and the checkbox + * Layout used to combine the property widgets with the WQtIntervalEdit. */ - QHBoxLayout m_layout; + QVBoxLayout m_vLayout; /** - * Converts a given value to a percentage between m_min and m_max. + * Converts a given value to a slider value between m_min and m_max. * * \param value the value. * * \return the percentage. */ - int toPercent( double value ); + int toSliderValue( double value ); /** - * Converts the given percentage to the real double value using m_min and m_max. + * Converts the given slider value to the real double value using m_min and m_max. * * \param perc the percentage. * * \return the real double value. */ - double fromPercent( int perc ); + double fromSliderValue( int perc ); /** * The current minimum value. @@ -129,6 +125,16 @@ protected: */ QHBoxLayout m_infoLayout; + /** + * The edit for the interval. + */ + WQtIntervalEdit< double, double > m_intervalEdit; + + /** + * Resolution of the slider. + */ + const static int SliderResolution; + private: public slots: @@ -152,14 +158,9 @@ public slots: void textEdited( const QString& text ); /** - * Called when the maxMin button is pressed to open maxMinDialog. - */ - void maxPressed(); - - /** - * Called when the maxMin button is pressed to open maxMinDialog. + * Called whenever the interval edit changes. */ - void minPressed(); + void minMaxUpdated(); }; #endif // WPROPERTYDOUBLEWIDGET_H diff --git a/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.cpp b/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.cpp index af88f5745e66b82bdb8180011656cac0a1e7637f..d5b20471ce374fab602f8d065f229f167cf8594c 100644 --- a/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.cpp +++ b/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.cpp @@ -22,64 +22,57 @@ // //--------------------------------------------------------------------------- -#include #include -#include #include +#include +#include #include #include #include "../WGuiConsts.h" #include "../WQt4Gui.h" +#include "../guiElements/WQtIntervalEdit.h" #include "core/common/WLogger.h" #include "core/common/WPropertyVariable.h" #include "WPropertyIntWidget.h" #include "WPropertyIntWidget.moc" +const int WPropertyIntWidget::SliderResolution = 10000; + WPropertyIntWidget::WPropertyIntWidget( WPropInt property, QGridLayout* propertyGrid, QWidget* parent ): WPropertyWidget( property, propertyGrid, parent ), - m_intProperty( property ), + m_integralProperty( property ), m_slider( Qt::Horizontal, &m_parameterWidgets ), m_edit( &m_parameterWidgets ), - m_minButton( &m_parameterWidgets ), - m_maxButton( &m_parameterWidgets ), - m_layout( &m_parameterWidgets ), + m_layout(), + m_vLayout( &m_parameterWidgets ), m_asText( &m_informationWidgets ), - m_infoLayout( &m_informationWidgets ) + m_infoLayout( &m_informationWidgets ), + m_intervalEdit( &m_parameterWidgets ) { - // layout both against each other - if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) - { - m_layout.addWidget( &m_minButton ); - m_minButton.setToolTip( "Set minimum of slider.\n Be aware that strange values can cause errors." ); - m_minButton.setMinimumWidth( 5 ); - m_minButton.setMaximumWidth( 5 ); - } - else - { - m_minButton.hide(); - } - m_layout.addWidget( &m_slider ); - if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) - { - m_layout.addWidget( &m_maxButton ); - m_maxButton.setToolTip( "Set maximum of slider.\n Be aware that strange values can cause errors." ); - m_maxButton.setMinimumWidth( 5 ); - m_maxButton.setMaximumWidth( 5 ); - } - else - { - m_maxButton.hide(); - } - m_layout.addWidget( &m_edit ); m_layout.setMargin( WGLOBAL_MARGIN ); m_layout.setSpacing( WGLOBAL_SPACING ); - m_parameterWidgets.setLayout( &m_layout ); + m_vLayout.setMargin( WGLOBAL_MARGIN ); + m_vLayout.setSpacing( WGLOBAL_SPACING ); + + // add the m_layout to the vlayout + QWidget* layoutContainer = new QWidget(); + layoutContainer->setLayout( &m_layout ); + m_vLayout.addWidget( layoutContainer ); + + // configure the interval edit + m_vLayout.addWidget( &m_intervalEdit ); + if( !WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() ) + { + m_intervalEdit.hide(); + } + + m_parameterWidgets.setLayout( &m_vLayout ); // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION ) m_infoLayout.addWidget( &m_asText ); @@ -95,8 +88,8 @@ WPropertyIntWidget::WPropertyIntWidget( WPropInt property, QGridLayout* property connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) ); connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) ); connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) ); - connect( &m_maxButton, SIGNAL( pressed() ), this, SLOT( maxPressed() ) ); - connect( &m_minButton, SIGNAL( pressed() ), this, SLOT( minPressed() ) ); + connect( &m_intervalEdit, SIGNAL( minimumChanged() ), this, SLOT( minMaxUpdated() ) ); + connect( &m_intervalEdit, SIGNAL( maximumChanged() ), this, SLOT( minMaxUpdated() ) ); } WPropertyIntWidget::~WPropertyIntWidget() @@ -108,43 +101,61 @@ void WPropertyIntWidget::update() { // // calculate maximum size of the text widget. // // XXX: this is not the optimal way but works for now - // Same as in WPropertyDouble.cpp: This does not work as expected on Mac OS X => reset to default - - // set the initial values - QString valStr = QString::number( m_intProperty->get() ); + // 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 + QString valStr = QString::number( m_integralProperty->get() ); m_edit.setText( valStr ); - // get the min constraint - WPVInt::PropertyConstraintMin minC = m_intProperty->getMin(); - WPVInt::PropertyConstraintMax maxC = m_intProperty->getMax(); + // get the min constraint + WPVInt::PropertyConstraintMin minC = m_integralProperty->getMin(); + WPVInt::PropertyConstraintMax maxC = m_integralProperty->getMax(); bool minMaxConstrained = minC && maxC; if( minMaxConstrained ) { - m_slider.setHidden( false ); - m_slider.setMinimum( minC->getMin() ); - m_slider.setMaximum( maxC->getMax() ); - m_slider.setValue( m_intProperty->get() ); + // setup the slider + m_slider.setMinimum( 0 ); + m_slider.setMaximum( SliderResolution ); - m_edit.setMaximumWidth( fontMetrics().width( QString::number( - ( maxC->getMax() - minC->getMin() ) ) ) + 5 ); + // update the interval edit too + m_intervalEdit.setAllowedMin( minC->getMin() ); + m_intervalEdit.setAllowedMax( maxC->getMax() ); + m_min = m_intervalEdit.getMin(); + m_max = m_intervalEdit.getMax(); + + // updating the interval edit causes the proper values to be set in m_min and m_max + m_slider.setHidden( false ); + m_intervalEdit.setHidden( false ); + m_slider.setValue( toSliderValue( m_integralProperty->get() ) ); } else { m_slider.setHidden( true ); + m_intervalEdit.setHidden( true ); } // do not forget to update the label m_asText.setText( valStr ); } +int WPropertyIntWidget::toSliderValue( double value ) +{ + int perc = static_cast< double >( SliderResolution ) * ( ( value - m_min ) / ( m_max - m_min ) ); + return std::min( std::max( perc, 0 ), SliderResolution ); +} + +double WPropertyIntWidget::fromSliderValue( int perc ) +{ + return ( static_cast< double >( perc ) / static_cast< double >( SliderResolution ) ) * ( m_max - m_min ) + m_min; +} + void WPropertyIntWidget::sliderChanged( int value ) { - if( !m_slider.isHidden() ) + if( !m_slider.isHidden() && toSliderValue( m_integralProperty->get() ) != value ) { - // set the value in the line edit - m_edit.setText( QString::number( value ) ); - // set to the property - invalidate( !m_intProperty->set( value ) ); // NOTE: set automatically checks the validity of the value + invalidate( !m_integralProperty->set( fromSliderValue( value ) ) ); // NOTE: set automatically checks the validity of the value + + // set the value in the line edit + m_edit.setText( QString::number( m_integralProperty->get() ) ); } } @@ -158,12 +169,11 @@ void WPropertyIntWidget::editChanged() invalidate(); return; } + // set to the property + invalidate( !m_integralProperty->set( value ) ); // NOTE: set automatically checks the validity of the value // update slider - m_slider.setValue( value ); - - // set to the property - invalidate( !m_intProperty->set( value ) ); // NOTE: set automatically checks the validity of the value + m_slider.setValue( toSliderValue( value ) ); } void WPropertyIntWidget::textEdited( const QString& text ) @@ -178,43 +188,23 @@ void WPropertyIntWidget::textEdited( const QString& text ) } // simply check validity - invalidate( !m_intProperty->accept( value ) ); + invalidate( !m_integralProperty->accept( value ) ); } -void WPropertyIntWidget::maxPressed() +void WPropertyIntWidget::minMaxUpdated() { - bool ok; - int min = m_intProperty->getMin()->getMin(); - int max = m_intProperty->getMax()->getMax(); - int newMax = QInputDialog::getInt( this, "Setting Maximum of Slider", - "Maximum", max, min, std::numeric_limits< int >::max(), 1, &ok ); - if( ok ) + m_min = m_intervalEdit.getMin(); + m_max = m_intervalEdit.getMax(); + + if( m_min > m_integralProperty->get() ) { - if( m_intProperty->get() > newMax ) - { - m_intProperty->set( newMax ); - m_slider.setValue( newMax ); - } - m_slider.setMaximum( newMax ); - m_intProperty->setMax( newMax ); + m_integralProperty->set( m_min ); } -} - -void WPropertyIntWidget::minPressed() -{ - bool ok; - int min = m_intProperty->getMin()->getMin(); - int max = m_intProperty->getMax()->getMax(); - int newMin = QInputDialog::getInt( this, "Setting Minimum of Slider", - "Minimum", min, -std::numeric_limits< int >::max(), max, 1, &ok ); - if( ok ) + if( m_max < m_integralProperty->get() ) { - if( m_intProperty->get() < newMin ) - { - m_intProperty->set( newMin ); - m_slider.setValue( newMin ); - } - m_slider.setMinimum( newMin ); - m_intProperty->setMin( newMin ); + m_integralProperty->set( m_max ); } + + m_slider.setValue( toSliderValue( m_integralProperty->get() ) ); } + diff --git a/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.h b/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.h index d65b650fc11bf4b2fa216595b694e4184cfbd975..acddb903eefefb1778d04767661dddcea3542c10 100644 --- a/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.h +++ b/src/qt4gui/qt4/controlPanel/WPropertyIntWidget.h @@ -27,11 +27,12 @@ #include -#include #include #include +#include #include +#include "../guiElements/WQtIntervalEdit.h" #include "WPropertyWidget.h" /** @@ -62,9 +63,9 @@ protected: virtual void update(); /** - * The integer property represented by this widget. + * The property represented by this widget. */ - WPropInt m_intProperty; + WPropInt m_integralProperty; /** * The slider allowing modification of the integer value @@ -77,19 +78,42 @@ protected: QLineEdit m_edit; /** - * The button bringing up the dialog for modifying min of the slider + * Layout used to position the label and the checkbox */ - QToolButton m_minButton; + QHBoxLayout m_layout; /** - * The button bringing up the dialog for modifying max of the slider + * Layout used to combine the property widgets with the WQtIntervalEdit. */ - QToolButton m_maxButton; + QVBoxLayout m_vLayout; /** - * Layout used to position the label and the checkbox + * Converts a given value to a slider value between m_min and m_max. + * + * \param value the value. + * + * \return the percentage. */ - QHBoxLayout m_layout; + int toSliderValue( double value ); + + /** + * Converts the given slider value to the real double value using m_min and m_max. + * + * \param perc the percentage. + * + * \return the real double value. + */ + double fromSliderValue( int perc ); + + /** + * The current minimum value. + */ + double m_min; + + /** + * The current maximum value. + */ + double m_max; /** * Used to show the property as text. @@ -101,6 +125,18 @@ protected: */ QHBoxLayout m_infoLayout; + /** + * The edit for the interval. + * + * \note we use double here. This is due to the need for discrete values in the sliders. + */ + WQtIntervalEdit< double, int32_t > m_intervalEdit; + + /** + * Resolution of the slider. + */ + const static int SliderResolution; + private: public slots: @@ -124,14 +160,9 @@ public slots: void textEdited( const QString& text ); /** - * Called when the maxMin button is pressed to open maxMinDialog. - */ - void maxPressed(); - - /** - * Called when the maxMin button is pressed to open maxMinDialog. + * Called whenever the interval edit changes. */ - void minPressed(); + void minMaxUpdated(); }; #endif // WPROPERTYINTWIDGET_H diff --git a/src/qt4gui/qt4/guiElements/WQtIntervalEdit.cpp b/src/qt4gui/qt4/guiElements/WQtIntervalEdit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dec98d98ee278e0c126b11e53e64e4162abf2f05 --- /dev/null +++ b/src/qt4gui/qt4/guiElements/WQtIntervalEdit.cpp @@ -0,0 +1,86 @@ +//--------------------------------------------------------------------------- +// +// Project: OpenWalnut ( http://www.openwalnut.org ) +// +// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS +// For more information see http://www.openwalnut.org/copying +// +// This file is part of OpenWalnut. +// +// OpenWalnut is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// OpenWalnut is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with OpenWalnut. If not, see . +// +//--------------------------------------------------------------------------- + +#include "../WGuiConsts.h" + +#include "WQtIntervalEdit.h" +#include "WQtIntervalEdit.moc" + +const int WQtIntervalEditBase::SliderResolution = 10000; + +WQtIntervalEditBase::WQtIntervalEditBase( QWidget* parent ): + QWidget( parent ), + m_minSlider( Qt::Horizontal, this ), + m_maxSlider( Qt::Horizontal, this ), + m_minEdit( this ), + m_maxEdit( this ), + m_minLabel( "Minimum:", this ), + m_maxLabel( "Maximum:", this ), + m_layout( this ) +{ + // initialize members + m_layout.setMargin( WGLOBAL_MARGIN ); + m_layout.setSpacing( WGLOBAL_SPACING ); + + m_layout.addWidget( &m_minLabel, 0, 0 ); + m_layout.addWidget( &m_minSlider, 0, 1 ); + m_layout.addWidget( &m_minEdit, 0, 2 ); + m_layout.addWidget( &m_maxLabel, 1, 0 ); + m_layout.addWidget( &m_maxSlider, 1, 1 ); + m_layout.addWidget( &m_maxEdit, 1, 2 ); + + m_minSlider.setMinimum( 0 ); + m_minSlider.setMaximum( SliderResolution ); + m_maxSlider.setMinimum( 0 ); + m_maxSlider.setMaximum( SliderResolution ); + + setLayout( &m_layout ); + + connect( &m_minSlider, SIGNAL( valueChanged( int ) ), this, SLOT( minSliderChanged() ) ); + connect( &m_maxSlider, SIGNAL( valueChanged( int ) ), this, SLOT( maxSliderChanged() ) ); + connect( &m_minEdit, SIGNAL( editingFinished() ), this, SLOT( minEditChanged() ) ); + connect( &m_maxEdit, SIGNAL( editingFinished() ), this, SLOT( maxEditChanged() ) ); +} + +WQtIntervalEditBase::~WQtIntervalEditBase() +{ + // cleanup +} + +void WQtIntervalEditBase::minSliderChanged() +{ +} + +void WQtIntervalEditBase::maxSliderChanged() +{ +} + +void WQtIntervalEditBase::minEditChanged() +{ +} + +void WQtIntervalEditBase::maxEditChanged() +{ +} + diff --git a/src/qt4gui/qt4/guiElements/WQtIntervalEdit.h b/src/qt4gui/qt4/guiElements/WQtIntervalEdit.h new file mode 100644 index 0000000000000000000000000000000000000000..a0d7a452e5405e2a32d45c71687daeb31fc07ea5 --- /dev/null +++ b/src/qt4gui/qt4/guiElements/WQtIntervalEdit.h @@ -0,0 +1,456 @@ +//--------------------------------------------------------------------------- +// +// Project: OpenWalnut ( http://www.openwalnut.org ) +// +// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS +// For more information see http://www.openwalnut.org/copying +// +// This file is part of OpenWalnut. +// +// OpenWalnut is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// OpenWalnut is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with OpenWalnut. If not, see . +// +//--------------------------------------------------------------------------- + +#ifndef WQTINTERVALEDIT_H +#define WQTINTERVALEDIT_H + +#include +#include +#include + +#include +#include +#include +#include + +template< typename DataType, typename DisplayType > +DisplayType toDisplayType( const DataType& value ) +{ + return static_cast< DisplayType >( round( value ) ); +} + +/** + * Base class for interval editing. You should not use this class. Use the derived \ref WQtIntervalEdit template. + */ +class WQtIntervalEditBase: public QWidget +{ + Q_OBJECT +public: + /** + * Default constructor. + * + * \param parent the parent widget + */ + WQtIntervalEditBase( QWidget* parent ); + + /** + * Destructor. + */ + virtual ~WQtIntervalEditBase(); + + /** + * Update the current state + */ + virtual void update() = 0; + +signals: + /** + * Called whenever the user changes the current minimum OR \ref setMin was called. + */ + void minimumChanged(); + + /** + * Called whenever the user changes the current ,aximum OR \ref setMax was called. + */ + void maximumChanged(); + +protected slots: + + /** + * Slot called when the minimum slider changes. + */ + virtual void minSliderChanged(); + + /** + * Slot called when the maximum slider changes. + */ + virtual void maxSliderChanged(); + + /** + * Slot called when the minimum edit changes. + */ + virtual void minEditChanged(); + + /** + * Slot called when the maximum edit changes. + */ + virtual void maxEditChanged(); + +protected: + /** + * Resolution of the sliders. + */ + const static int SliderResolution; + + /** + * The slider for the minimum. + */ + QSlider m_minSlider; + + /** + * The slider for the maximumum. + */ + QSlider m_maxSlider; + + /** + * The minimum edit. + */ + QLineEdit m_minEdit; + + /** + * The maximum edit. + */ + QLineEdit m_maxEdit; + + /** + * A label for the minimum. + */ + QLabel m_minLabel; + + /** + * A label for the maximum. + */ + QLabel m_maxLabel; + + /** + * The layout containing min and max layout. + */ + QGridLayout m_layout; + +private: +}; + +/** + * Simple widget allowing to define a certain interval. + * + * \tparam DataType interval for which integral type? + */ +template< typename DataType, typename DisplayType > +class WQtIntervalEdit: public WQtIntervalEditBase +{ +public: + /** + * Default constructor. + * + * \param parent the parent widget + */ + WQtIntervalEdit( QWidget* parent ); + + /** + * Destructor. + */ + virtual ~WQtIntervalEdit(); + + /** + * Set the allowed minimum. + * + * \param min the min. + */ + void setAllowedMin( DataType min = std::numeric_limits< DataType >::min() ); + + /** + * Set the allowed maximum. + * + * \param max the max + */ + void setAllowedMax( DataType max = std::numeric_limits< DataType >::max() ); + + /** + * Get the currently allowed min. + * + * \return the min + */ + const DataType& getAllowedMin() const; + + /** + * Get the currently allowed max. + * + * \return the max + */ + const DataType& getAllowedMax() const; + + /** + * Set the currently selected min + * + * \param min the min + */ + void setMin( DataType min = std::numeric_limits< DataType >::min() ); + + /** + * Set the currently selected max + * + * \param max the max + */ + void setMax( DataType max = std::numeric_limits< DataType >::max() ); + + /** + * Get the currently selected minimum. + * + * \return the min + */ + const DataType& getMin() const; + + /** + * Get the currently selected maximum. + * + * \return the max + */ + const DataType& getMax() const; + + /** + * Update the current state + */ + virtual void update(); +protected: + /** + * Slot called when the minimum slider changes. + */ + virtual void minSliderChanged(); + + /** + * Slot called when the maximum slider changes. + */ + virtual void maxSliderChanged(); + + /** + * Slot called when the minimum edit changes. + */ + virtual void minEditChanged(); + + /** + * Slot called when the maximum edit changes. + */ + virtual void maxEditChanged(); + +private: + /** + * The allowed maximum + */ + DataType m_allowedMax; + + /** + * The allowed minimum + */ + DataType m_allowedMin; + + /** + * The current minumum. + */ + DataType m_min; + + /** + * The current maximum + */ + DataType m_max; +}; + + +template< typename DataType, typename DisplayType > +WQtIntervalEdit< DataType, DisplayType >::WQtIntervalEdit( QWidget* parent ): + WQtIntervalEditBase( parent ), + m_allowedMax( std::numeric_limits< DataType >::max() ), + m_allowedMin( std::numeric_limits< DataType >::min() ), + m_min( std::numeric_limits< DataType >::min() ), + m_max( std::numeric_limits< DataType >::max() ) + +{ + // initialize members +} + +template< typename DataType, typename DisplayType > +WQtIntervalEdit< DataType, DisplayType >::~WQtIntervalEdit() +{ + // cleanup +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::setAllowedMin( DataType min ) +{ + if( ( min > m_allowedMax ) || ( m_allowedMin == min ) ) + { + return; + } + + // if the new allowed min is larger than the currently set min -> change m_min too + if( min > m_min ) + { + m_min = min; + emit minimumChanged(); + } + m_allowedMin = min; + update(); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::setAllowedMax( DataType max ) +{ + if( ( max < m_allowedMin ) || ( m_allowedMax == max ) ) + { + return; + } + // if the new allowed max is smaller than the currently set max -> change m_max too + if( max < m_max ) + { + m_max = max; + emit maximumChanged(); + } + m_allowedMax = max; + update(); +} + +template< typename DataType, typename DisplayType > +const DataType& WQtIntervalEdit< DataType, DisplayType >::getAllowedMin() const +{ + return m_allowedMin; +} + +template< typename DataType, typename DisplayType > +const DataType& WQtIntervalEdit< DataType, DisplayType >::getAllowedMax() const +{ + return m_allowedMax; +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::setMin( DataType min ) +{ + DataType newVal = ( min < m_allowedMin ) ? m_allowedMin : min; + if( m_min == newVal ) + { + return; + } + m_min = newVal; + + // ensure the maximum is larger than the minimum + if( m_max < m_min ) + { + m_max = m_min; + emit maximumChanged(); + } + emit minimumChanged(); + update(); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::setMax( DataType max ) +{ + DataType newVal = ( max > m_allowedMax ) ? m_allowedMax : max; + if( m_max == newVal ) + { + return; + } + m_max = newVal; + // ensure the minimum is smaller than the specified max + if( m_max < m_min ) + { + m_min = m_max; + emit minimumChanged(); + } + emit maximumChanged(); + update(); +} + +template< typename DataType, typename DisplayType > +const DataType& WQtIntervalEdit< DataType, DisplayType >::getMin() const +{ + return m_min; +} + +template< typename DataType, typename DisplayType > +const DataType& WQtIntervalEdit< DataType, DisplayType >::getMax() const +{ + return m_max; +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::update() +{ + // set the new slider value + double percMin = static_cast< double >( SliderResolution ) * + ( static_cast< double >( m_min ) - static_cast< double >( m_allowedMin ) ) / + ( static_cast< double >( m_allowedMax ) - static_cast< double >( m_allowedMin ) ); + double percMax = static_cast< double >( SliderResolution ) * + ( static_cast< double >( m_max ) - static_cast< double >( m_allowedMin ) ) / + ( static_cast< double >( m_allowedMax ) - static_cast< double >( m_allowedMin ) ); + + m_minSlider.setValue( percMin ); + m_maxSlider.setValue( percMax ); + + // set the text edits + m_minEdit.setText( QString::number( m_min ) ); + m_maxEdit.setText( QString::number( m_max ) ); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::minSliderChanged() +{ + double perc = static_cast< double >( m_minSlider.value() ) / static_cast< double >( SliderResolution ); + DataType newMin = static_cast< DataType >( + static_cast< double >( m_allowedMin ) + perc * ( static_cast< double >( m_allowedMax ) - + static_cast< double >( m_allowedMin ) ) ); + setMin( newMin ); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::maxSliderChanged() +{ + float perc = static_cast< float >( m_maxSlider.value() ) / static_cast< float >( SliderResolution ); + DataType newMax = static_cast< DataType >( + static_cast< double >( m_allowedMin ) + perc * ( static_cast< double >( m_allowedMax ) - + static_cast< double >( m_allowedMin ) ) ); + + setMax( newMax ); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::minEditChanged() +{ + // try to get the value + bool valid; + double value = m_minEdit.text().toDouble( &valid ); + if( !valid ) + { + m_minEdit.setText( QString::number( static_cast< DataType >( m_min ) ) ); + return; + } + + setMin( value ); +} + +template< typename DataType, typename DisplayType > +void WQtIntervalEdit< DataType, DisplayType >::maxEditChanged() +{ + // try to get the value + bool valid; + double value = m_maxEdit.text().toDouble( &valid ); + if( !valid ) + { + m_maxEdit.setText( QString::number( static_cast< DataType >( m_max ) ) ); + return; + } + + setMax( value ); +} + +#endif // WQTINTERVALEDIT_H +