1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
29
#include <boost/lexical_cast.hpp>
31
#include "core/common/WLogger.h"
32
#include "core/common/WPropertyVariable.h"
33
#include "../WGuiConsts.h"
35
#include "WPropertyDoubleWidget.h"
36
#include "WPropertyDoubleWidget.moc"
38
WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ):
39
WPropertyWidget( property, propertyGrid, parent ),
40
m_doubleProperty( property ),
41
m_slider( Qt::Horizontal, &m_parameterWidgets ),
42
m_edit( &m_parameterWidgets ),
43
m_layout( &m_parameterWidgets ),
44
m_asText( &m_informationWidgets ),
45
m_infoLayout( &m_informationWidgets )
47
// layout both against each other
48
m_layout.addWidget( &m_slider );
49
m_layout.addWidget( &m_edit );
50
m_layout.setMargin( WGLOBAL_MARGIN );
51
m_layout.setSpacing( WGLOBAL_SPACING );
53
m_parameterWidgets.setLayout( &m_layout );
55
// Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
56
m_infoLayout.addWidget( &m_asText );
57
m_infoLayout.setMargin( WGLOBAL_MARGIN );
58
m_infoLayout.setSpacing( WGLOBAL_SPACING );
59
m_informationWidgets.setLayout( &m_infoLayout );
61
m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );
65
// connect the modification signal of the edit and slider with our callback
66
connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
67
connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
68
connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
71
WPropertyDoubleWidget::~WPropertyDoubleWidget()
76
void WPropertyDoubleWidget::update()
78
// // calculate maximum size of the text widget.
79
// // XXX: this is not the optimal way but works for now
80
// 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
81
QString valStr = QString::number( m_doubleProperty->get() );
82
m_edit.setText( valStr );
84
// get the min constraint
85
WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin();
86
WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax();
87
bool minMaxConstrained = minC && maxC;
88
if( minMaxConstrained )
91
m_slider.setMinimum( 0 );
92
m_slider.setMaximum( 100 );
93
m_min = minC->getMin();
94
m_max = maxC->getMax();
96
m_slider.setHidden( false );
97
m_slider.setValue( toPercent( m_doubleProperty->get() ) );
101
m_slider.setHidden( true );
104
// do not forget to update the label
105
m_asText.setText( valStr );
108
int WPropertyDoubleWidget::toPercent( double value )
110
return 100.0 * ( ( value - m_min ) / ( m_max - m_min ) );
113
double WPropertyDoubleWidget::fromPercent( int perc )
115
return ( static_cast< double >( perc ) / 100.0 ) * ( m_max - m_min ) + m_min;
118
void WPropertyDoubleWidget::sliderChanged( int value )
120
if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
122
// set to the property
123
invalidate( !m_doubleProperty->set( fromPercent( value ) ) ); // NOTE: set automatically checks the validity of the value
125
// set the value in the line edit
126
m_edit.setText( QString::number( m_doubleProperty->get() ) );
130
void WPropertyDoubleWidget::editChanged()
132
// set the value in the line edit
134
double value = m_edit.text().toDouble( &valid );
141
// set to the property
142
invalidate( !m_doubleProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
145
m_slider.setValue( toPercent( value ) );
148
void WPropertyDoubleWidget::textEdited( const QString& text )
150
// this method does NOT set the property actually, but tries to validate it
153
double value = text.toDouble( &valid );
160
// simply check validity
161
invalidate( !m_doubleProperty->accept( value ) );