~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/qt4gui/qt4/controlPanel/WPropertyDoubleWidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <cmath>
 
26
#include <sstream>
 
27
#include <string>
 
28
 
 
29
#include <boost/lexical_cast.hpp>
 
30
 
 
31
#include "core/common/WLogger.h"
 
32
#include "core/common/WPropertyVariable.h"
 
33
#include "../WGuiConsts.h"
 
34
 
 
35
#include "WPropertyDoubleWidget.h"
 
36
#include "WPropertyDoubleWidget.moc"
 
37
 
 
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 )
 
46
{
 
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 );
 
52
 
 
53
    m_parameterWidgets.setLayout( &m_layout );
 
54
 
 
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 );
 
60
 
 
61
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );
 
62
 
 
63
    update();
 
64
 
 
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& ) ) );
 
69
}
 
70
 
 
71
WPropertyDoubleWidget::~WPropertyDoubleWidget()
 
72
{
 
73
    // cleanup
 
74
}
 
75
 
 
76
void WPropertyDoubleWidget::update()
 
77
{
 
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 );
 
83
 
 
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 )
 
89
    {
 
90
        // setup the slider
 
91
        m_slider.setMinimum( 0 );
 
92
        m_slider.setMaximum( 100 );
 
93
        m_min = minC->getMin();
 
94
        m_max = maxC->getMax();
 
95
 
 
96
        m_slider.setHidden( false );
 
97
        m_slider.setValue( toPercent( m_doubleProperty->get() ) );
 
98
    }
 
99
    else
 
100
    {
 
101
        m_slider.setHidden( true );
 
102
    }
 
103
 
 
104
    // do not forget to update the label
 
105
    m_asText.setText( valStr );
 
106
}
 
107
 
 
108
int WPropertyDoubleWidget::toPercent( double value )
 
109
{
 
110
    return 100.0 * ( ( value - m_min ) / ( m_max - m_min ) );
 
111
}
 
112
 
 
113
double WPropertyDoubleWidget::fromPercent( int perc )
 
114
{
 
115
    return ( static_cast< double >( perc ) / 100.0 ) * ( m_max - m_min ) + m_min;
 
116
}
 
117
 
 
118
void WPropertyDoubleWidget::sliderChanged( int value )
 
119
{
 
120
    if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
 
121
    {
 
122
        // set to the property
 
123
        invalidate( !m_doubleProperty->set( fromPercent( value ) ) );    // NOTE: set automatically checks the validity of the value
 
124
 
 
125
        // set the value in the line edit
 
126
        m_edit.setText( QString::number( m_doubleProperty->get() ) );
 
127
    }
 
128
}
 
129
 
 
130
void WPropertyDoubleWidget::editChanged()
 
131
{
 
132
    // set the value in the line edit
 
133
    bool valid;
 
134
    double value = m_edit.text().toDouble( &valid );
 
135
 
 
136
    if( !valid )
 
137
    {
 
138
        invalidate();
 
139
        return;
 
140
    }
 
141
    // set to the property
 
142
    invalidate( !m_doubleProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
 
143
 
 
144
    // update slider
 
145
    m_slider.setValue( toPercent( value ) );
 
146
}
 
147
 
 
148
void WPropertyDoubleWidget::textEdited( const QString& text )
 
149
{
 
150
    // this method does NOT set the property actually, but tries to validate it
 
151
    bool valid;
 
152
 
 
153
    double value = text.toDouble( &valid );
 
154
    if( !valid )
 
155
    {
 
156
        invalidate();
 
157
        return;
 
158
    }
 
159
 
 
160
    // simply check validity
 
161
    invalidate( !m_doubleProperty->accept( value ) );
 
162
}
 
163