WPropertyDoubleWidget.cpp 6.88 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 25
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

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

31
#include <QtGui/QInputDialog>
32

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

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

42 43
const int WPropertyDoubleWidget::SliderResolution = 10000;

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

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

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

75
    m_parameterWidgets.setLayout( &m_vLayout );
76

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

83 84
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );

85 86 87
    update();

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

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

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

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

124
        // updating the interval edit causes the proper values to be set in m_min and m_max
125
        m_slider.setHidden( false );
126 127
        m_intervalEdit.setHidden( false );
        m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
128 129 130
    }
    else
    {
131
        m_slider.setHidden( true );
132
        m_intervalEdit.setHidden( true );
133 134
    }

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

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

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

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

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

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

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

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

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 );
186
    if( !valid )
187 188 189 190 191 192
    {
        invalidate();
        return;
    }

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

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

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

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