WPropertyDoubleWidget.cpp 5.13 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 32

#include <boost/lexical_cast.hpp>

#include "../../../common/WLogger.h"
#include "../../../common/WPropertyVariable.h"
33
#include "../WGuiConsts.h"
34 35 36 37 38
#include "WPropertyDoubleWidget.h"

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

    m_parameterWidgets.setLayout( &m_layout );
52

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

59 60
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );

61 62 63
    update();

    // connect the modification signal of the edit and slider with our callback
64
    connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
65
    connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
66
    connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
67 68 69 70 71 72 73 74 75
}

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

void WPropertyDoubleWidget::update()
{
76 77 78 79 80 81 82
    // // 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
83
    WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin();
84 85 86
    WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax();
    bool minMaxConstrained = minC && maxC;
    if ( minMaxConstrained )
87
    {
88 89 90
        // setup the slider
        m_slider.setMinimum( 0 );
        m_slider.setMaximum( 100 );
91
        m_min = minC->getMin();
92
        m_max = maxC->getMax();
93

94
        m_slider.setHidden( false );
95
        m_slider.setValue( toPercent( m_doubleProperty->get() ) );
96 97 98
    }
    else
    {
99
        m_slider.setHidden( true );
100 101
    }

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

106 107 108 109 110 111 112 113 114 115 116
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 )
117
{
118
    if ( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
119 120 121
    {
        // set to the property
        invalidate( !m_doubleProperty->set( fromPercent( value ) ) );    // NOTE: set automatically checks the validity of the value
122

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

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

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

    // 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 );
    if ( !valid )
    {
        invalidate();
        return;
    }

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