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
//---------------------------------------------------------------------------
30
#include "core/common/WLogger.h"
31
#include "core/common/WPropertyVariable.h"
32
#include "../WGuiConsts.h"
34
#include "WPropertyDoubleWidget.h"
35
#include "WPropertyDoubleWidget.moc"
37
WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ):
38
WPropertyWidget( property, propertyGrid, parent ),
39
m_doubleProperty( property ),
40
m_slider( Qt::Horizontal, &m_parameterWidgets ),
41
m_edit( &m_parameterWidgets ),
42
m_layout( &m_parameterWidgets ),
43
m_asText( &m_informationWidgets ),
44
m_infoLayout( &m_informationWidgets )
46
// layout both against each other
47
m_layout.addWidget( &m_slider );
48
m_layout.addWidget( &m_edit );
49
m_layout.setMargin( WGLOBAL_MARGIN );
50
m_layout.setSpacing( WGLOBAL_SPACING );
52
m_parameterWidgets.setLayout( &m_layout );
54
// Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
55
m_infoLayout.addWidget( &m_asText );
56
m_infoLayout.setMargin( WGLOBAL_MARGIN );
57
m_infoLayout.setSpacing( WGLOBAL_SPACING );
58
m_informationWidgets.setLayout( &m_infoLayout );
60
m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );
64
// connect the modification signal of the edit and slider with our callback
65
connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
66
connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
67
connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
70
WPropertyDoubleWidget::~WPropertyDoubleWidget()
75
void WPropertyDoubleWidget::update()
77
// // calculate maximum size of the text widget.
78
// // XXX: this is not the optimal way but works for now
79
// 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
80
QString valStr = QString::number( m_doubleProperty->get() );
81
m_edit.setText( valStr );
83
// get the min constraint
84
WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin();
85
WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax();
86
bool minMaxConstrained = minC && maxC;
87
if( minMaxConstrained )
90
m_slider.setMinimum( 0 );
91
m_slider.setMaximum( 100 );
92
m_min = minC->getMin();
93
m_max = maxC->getMax();
95
m_slider.setHidden( false );
96
m_slider.setValue( toPercent( m_doubleProperty->get() ) );
100
m_slider.setHidden( true );
103
// do not forget to update the label
104
m_asText.setText( valStr );
107
int WPropertyDoubleWidget::toPercent( double value )
109
return 100.0 * ( ( value - m_min ) / ( m_max - m_min ) );
112
double WPropertyDoubleWidget::fromPercent( int perc )
114
return ( static_cast< double >( perc ) / 100.0 ) * ( m_max - m_min ) + m_min;
117
void WPropertyDoubleWidget::sliderChanged( int value )
119
if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
121
// set to the property
122
invalidate( !m_doubleProperty->set( fromPercent( value ) ) ); // NOTE: set automatically checks the validity of the value
124
// set the value in the line edit
125
m_edit.setText( QString::number( m_doubleProperty->get() ) );
129
void WPropertyDoubleWidget::editChanged()
131
// set the value in the line edit
133
double value = m_edit.text().toDouble( &valid );
140
// set to the property
141
invalidate( !m_doubleProperty->set( value ) ); // NOTE: set automatically checks the validity of the value
144
m_slider.setValue( toPercent( value ) );
147
void WPropertyDoubleWidget::textEdited( const QString& text )
149
// this method does NOT set the property actually, but tries to validate it
152
double value = text.toDouble( &valid );
159
// simply check validity
160
invalidate( !m_doubleProperty->accept( value ) );