Commit 114e16c3 by Sebastian Eichelbaum

[CHANGE #198] min max can only be changed in the property-given interval

parent 3dc6aafc
......@@ -25,62 +25,54 @@
#include <cmath>
#include <limits>
#include <sstream>
#include <iostream>
#include <string>
#include <QtGui/QInputDialog>
#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() ) );
}
......@@ -32,6 +32,7 @@
#include <QtGui/QHBoxLayout>
#include <QtGui/QToolButton>
#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
......
......@@ -22,64 +22,57 @@
//
//---------------------------------------------------------------------------
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <sstream>
#include <iostream>
#include <string>
#include <QtGui/QInputDialog>
#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() ) );
}
......@@ -27,11 +27,12 @@
#include <string>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QSlider>
#include <QtGui/QHBoxLayout>
#include <QtGui/QToolButton>
#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
......
//---------------------------------------------------------------------------
//
// 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 <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "../WGuiConsts.h"
#include "WQtIntervalEdit.h"
#include "WQtIntervalEdit.moc"