WPropertyDoubleWidget.cpp 5.12 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 <sstream>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
27
#include <string>
28 29


30 31
#include "core/common/WLogger.h"
#include "core/common/WPropertyVariable.h"
32
#include "../WGuiConsts.h"
33

34
#include "WPropertyDoubleWidget.h"
35
#include "WPropertyDoubleWidget.moc"
36 37 38 39

WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ):
    WPropertyWidget( property, propertyGrid, parent ),
    m_doubleProperty( property ),
40 41
    m_slider( Qt::Horizontal, &m_parameterWidgets ),
    m_edit( &m_parameterWidgets ),
42 43 44
    m_layout( &m_parameterWidgets ),
    m_asText( &m_informationWidgets ),
    m_infoLayout( &m_informationWidgets )
45 46
{
    // layout both against each other
47 48
    m_layout.addWidget( &m_slider );
    m_layout.addWidget( &m_edit );
49 50
    m_layout.setMargin( WGLOBAL_MARGIN );
    m_layout.setSpacing( WGLOBAL_SPACING );
51 52

    m_parameterWidgets.setLayout( &m_layout );
53

54 55
    // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
    m_infoLayout.addWidget( &m_asText );
56 57
    m_infoLayout.setMargin( WGLOBAL_MARGIN );
    m_infoLayout.setSpacing( WGLOBAL_SPACING );
58 59
    m_informationWidgets.setLayout( &m_infoLayout );

60 61
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );

62 63 64
    update();

    // 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 71 72 73 74 75 76
}

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

void WPropertyDoubleWidget::update()
{
77 78 79 80 81 82 83
    // // 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
    QString valStr = QString::number( m_doubleProperty->get() );
    m_edit.setText( valStr );

       // get the min constraint
84
    WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin();
85 86
    WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax();
    bool minMaxConstrained = minC && maxC;
87
    if( minMaxConstrained )
88
    {
89 90 91
        // setup the slider
        m_slider.setMinimum( 0 );
        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 99
    }
    else
    {
100
        m_slider.setHidden( true );
101 102
    }

103 104
    // do not forget to update the label
    m_asText.setText( valStr );
105 106
}

107 108 109 110 111 112 113 114 115 116 117
int WPropertyDoubleWidget::toPercent( double value )
{
    return 100.0 * ( ( value - m_min ) / ( m_max - m_min ) );
}

double WPropertyDoubleWidget::fromPercent( int perc )
{
    return ( static_cast< double >( perc ) / 100.0 ) * ( m_max - m_min ) + m_min;
}

void WPropertyDoubleWidget::sliderChanged( int value )
118
{
119
    if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
120 121 122
    {
        // set to the property
        invalidate( !m_doubleProperty->set( fromPercent( value ) ) );    // NOTE: set automatically checks the validity of the value
123

124 125 126
        // set the value in the line edit
        m_edit.setText( QString::number( m_doubleProperty->get() ) );
    }
127 128 129 130 131 132 133
}

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

135
    if( !valid )
136 137 138 139
    {
        invalidate();
        return;
    }
140 141
    // set to the property
    invalidate( !m_doubleProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
142 143 144 145 146 147 148 149 150 151 152

    // update slider
    m_slider.setValue( toPercent( value ) );
}

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 );
153
    if( !valid )
154 155 156 157 158 159 160
    {
        invalidate();
        return;
    }

    // simply check validity
    invalidate( !m_doubleProperty->accept( value ) );
161 162
}