~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

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