WPropertyDoubleWidget.cpp 6.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

25
#include <algorithm>
26
#include <cmath>
27
#include <limits>
28
#include <sstream>
29
#include <iostream>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
30
#include <string>
31

32
#include <QtGui/QInputDialog>
33

34 35
#include "../WGuiConsts.h"
#include "../WQt4Gui.h"
36
#include "../guiElements/WQtIntervalEdit.h"
37 38
#include "core/common/WLogger.h"
#include "core/common/WPropertyVariable.h"
39

40
#include "WPropertyDoubleWidget.h"
41
#include "WPropertyDoubleWidget.moc"
42

43
int WPropertyDoubleWidget::SliderResolution = 10000;
44

45 46
WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ):
    WPropertyWidget( property, propertyGrid, parent ),
47
    m_integralProperty( property ),
48 49
    m_slider( Qt::Horizontal, &m_parameterWidgets ),
    m_edit( &m_parameterWidgets ),
50 51
    m_layout(),
    m_vLayout( &m_parameterWidgets ),
52
    m_asText( &m_informationWidgets ),
53 54
    m_infoLayout( &m_informationWidgets ),
    m_intervalEdit( &m_parameterWidgets )
55
{
56
    m_layout.addWidget( &m_slider );
57

58
    m_layout.addWidget( &m_edit );
59 60
    m_layout.setMargin( WGLOBAL_MARGIN );
    m_layout.setSpacing( WGLOBAL_SPACING );
61 62 63 64 65 66 67
    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 );
68

69 70 71 72 73 74
    // configure the interval edit
    m_vLayout.addWidget( &m_intervalEdit );
    if( !WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() )
    {
        m_intervalEdit.hide();
    }
75

76
    m_parameterWidgets.setLayout( &m_vLayout );
77

78 79
    // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
    m_infoLayout.addWidget( &m_asText );
80 81
    m_infoLayout.setMargin( WGLOBAL_MARGIN );
    m_infoLayout.setSpacing( WGLOBAL_SPACING );
82 83
    m_informationWidgets.setLayout( &m_infoLayout );

84 85
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );

86 87 88
    update();

    // connect the modification signal of the edit and slider with our callback
89
    connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
90
    connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
91
    connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
92 93
    connect( &m_intervalEdit, SIGNAL( minimumChanged() ), this, SLOT( minMaxUpdated() ) );
    connect( &m_intervalEdit, SIGNAL( maximumChanged() ), this, SLOT( minMaxUpdated() ) );
94 95 96 97 98 99 100 101 102
}

WPropertyDoubleWidget::~WPropertyDoubleWidget()
{
    // cleanup
}

void WPropertyDoubleWidget::update()
{
103 104 105
    // // 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
106
    QString valStr = QString::number( m_integralProperty->get() );
107 108 109
    m_edit.setText( valStr );

       // get the min constraint
110 111
    WPVDouble::PropertyConstraintMin minC = m_integralProperty->getMin();
    WPVDouble::PropertyConstraintMax maxC = m_integralProperty->getMax();
112
    bool minMaxConstrained = minC && maxC;
113
    if( minMaxConstrained )
114
    {
115 116
        // setup the slider
        m_slider.setMinimum( 0 );
117 118 119 120 121 122 123
        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();
124

125
        // updating the interval edit causes the proper values to be set in m_min and m_max
126
        m_slider.setHidden( false );
127
        m_intervalEdit.setHidden( !WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() );
128
        m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
129 130 131
    }
    else
    {
132
        m_slider.setHidden( true );
133
        m_intervalEdit.setHidden( true );
134 135
    }

136 137
    // do not forget to update the label
    m_asText.setText( valStr );
138 139
}

140
int WPropertyDoubleWidget::toSliderValue( double value )
141
{
142 143
    int perc = static_cast< double >( SliderResolution ) * ( ( value - m_min ) / ( m_max - m_min ) );
    return std::min( std::max( perc, 0 ), SliderResolution );
144 145
}

146
double WPropertyDoubleWidget::fromSliderValue( int perc )
147
{
148
    return ( static_cast< double >( perc ) / static_cast< double >( SliderResolution ) ) * ( m_max - m_min ) + m_min;
149 150 151
}

void WPropertyDoubleWidget::sliderChanged( int value )
152
{
153
    if( !m_slider.isHidden() && toSliderValue( m_integralProperty->get() ) != value )
154 155
    {
        // set to the property
156
        invalidate( !m_integralProperty->set( fromSliderValue( value ) ) );    // NOTE: set automatically checks the validity of the value
157

158
        // set the value in the line edit
159
        m_edit.setText( QString::number( m_integralProperty->get() ) );
160
    }
161 162 163 164 165 166 167
}

void WPropertyDoubleWidget::editChanged()
{
    // set the value in the line edit
    bool valid;
    double value = m_edit.text().toDouble( &valid );
168

169
    if( !valid )
170 171 172 173
    {
        invalidate();
        return;
    }
174
    // set to the property
175
    invalidate( !m_integralProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
176 177

    // update slider
178
    m_slider.setValue( toSliderValue( value ) );
179 180 181 182 183 184 185 186
}

void WPropertyDoubleWidget::textEdited( const QString& text )
{
    // this method does NOT set the property actually, but tries to validate it
    bool valid;

    double value = text.toDouble( &valid );
187
    if( !valid )
188 189 190 191 192 193
    {
        invalidate();
        return;
    }

    // simply check validity
194
    invalidate( !m_integralProperty->accept( value ) );
195 196
}

197
void WPropertyDoubleWidget::minMaxUpdated()
198
{
199 200 201 202
    m_min = m_intervalEdit.getMin();
    m_max = m_intervalEdit.getMax();

    if( m_min > m_integralProperty->get() )
203
    {
204
        m_integralProperty->set( m_min );
205
    }
206
    if( m_max < m_integralProperty->get() )
207
    {
208
        m_integralProperty->set( m_max );
209
    }
210 211

    m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
212 213
}