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

30
#include <QtGui/QInputDialog>
31

32 33
#include "../WGuiConsts.h"
#include "../WQt4Gui.h"
34 35
#include "core/common/WLogger.h"
#include "core/common/WPropertyVariable.h"
36

37
#include "WPropertyDoubleWidget.h"
38
#include "WPropertyDoubleWidget.moc"
39 40 41 42

WPropertyDoubleWidget::WPropertyDoubleWidget( WPropDouble property, QGridLayout* propertyGrid, QWidget* parent ):
    WPropertyWidget( property, propertyGrid, parent ),
    m_doubleProperty( property ),
43 44
    m_slider( Qt::Horizontal, &m_parameterWidgets ),
    m_edit( &m_parameterWidgets ),
45 46
    m_minButton( &m_parameterWidgets ),
    m_maxButton( &m_parameterWidgets ),
47 48 49
    m_layout( &m_parameterWidgets ),
    m_asText( &m_informationWidgets ),
    m_infoLayout( &m_informationWidgets )
50 51
{
    // layout both against each other
52 53 54 55 56 57 58 59 60 61 62 63
    if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() )
    {
        m_layout.addWidget( &m_minButton );
        m_minButton.setToolTip( "Set minimum of slider.\n Be aware that strange values can cause errors." );
        m_minButton.setMinimumWidth( 5 );
        m_minButton.setMaximumWidth( 5 );
    }
    else
    {
        m_minButton.hide();
    }

64
    m_layout.addWidget( &m_slider );
65 66 67 68 69 70 71 72 73 74 75 76 77

    if( WQt4Gui::getSettings().value( "qt4gui/sliderMinMaxEdit", false ).toBool() )
    {
        m_layout.addWidget( &m_maxButton );
        m_maxButton.setToolTip( "Set maximum of slider.\n Be aware that strange values can cause errors." );
        m_maxButton.setMinimumWidth( 5 );
        m_maxButton.setMaximumWidth( 5 );
    }
    else
    {
        m_maxButton.hide();
    }

78
    m_layout.addWidget( &m_edit );
79 80
    m_layout.setMargin( WGLOBAL_MARGIN );
    m_layout.setSpacing( WGLOBAL_SPACING );
81

82

83
    m_parameterWidgets.setLayout( &m_layout );
84

85 86
    // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
    m_infoLayout.addWidget( &m_asText );
87 88
    m_infoLayout.setMargin( WGLOBAL_MARGIN );
    m_infoLayout.setSpacing( WGLOBAL_SPACING );
89 90
    m_informationWidgets.setLayout( &m_infoLayout );

91 92
    m_slider.setMinimumWidth( WMIN_SLIDER_WIDTH );

93 94 95
    update();

    // connect the modification signal of the edit and slider with our callback
96
    connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
97
    connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
98
    connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
99 100
    connect( &m_maxButton, SIGNAL( pressed() ), this, SLOT( maxPressed() ) );
    connect( &m_minButton, SIGNAL( pressed() ), this, SLOT( minPressed() ) );
101 102 103 104 105 106 107 108 109
}

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

void WPropertyDoubleWidget::update()
{
110 111 112 113 114 115 116
    // // 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
117
    WPVDouble::PropertyConstraintMin minC = m_doubleProperty->getMin();
118 119
    WPVDouble::PropertyConstraintMax maxC = m_doubleProperty->getMax();
    bool minMaxConstrained = minC && maxC;
120
    if( minMaxConstrained )
121
    {
122 123 124
        // setup the slider
        m_slider.setMinimum( 0 );
        m_slider.setMaximum( 100 );
125
        m_min = minC->getMin();
126
        m_max = maxC->getMax();
127

128
        m_slider.setHidden( false );
129
        m_slider.setValue( toPercent( m_doubleProperty->get() ) );
130 131 132
    }
    else
    {
133
        m_slider.setHidden( true );
134 135
    }

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

140 141 142 143 144 145 146 147 148 149 150
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 )
151
{
152
    if( !m_slider.isHidden() && toPercent( m_doubleProperty->get() ) != value )
153 154 155
    {
        // set to the property
        invalidate( !m_doubleProperty->set( fromPercent( value ) ) );    // NOTE: set automatically checks the validity of the value
156

157 158 159
        // set the value in the line edit
        m_edit.setText( QString::number( m_doubleProperty->get() ) );
    }
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 174
    // set to the property
    invalidate( !m_doubleProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
175 176 177 178 179 180 181 182 183 184 185

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

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

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

void WPropertyDoubleWidget::maxPressed()
{
    bool ok;
    double newMax = QInputDialog::getDouble( this, "Setting Maximum of Slider",
                                             "Maximum", m_max, m_min, std::numeric_limits< double >::max(), 18, &ok );
    if( ok )
    {
        m_max = newMax;
        if( m_doubleProperty->get() > newMax )
        {
            m_doubleProperty->set( newMax );
            m_slider.setValue( toPercent( newMax ) );
        }
        m_doubleProperty->setMax( newMax );
    }
}

void WPropertyDoubleWidget::minPressed()
{
    bool ok;
    double newMin = QInputDialog::getDouble( this, "Setting Minimum of Slider",
                                             "Minimum", m_min, -std::numeric_limits< double >::max(), m_max, 18, &ok );
    if( ok )
    {
        m_min = newMin;
        if( m_doubleProperty->get() < newMin )
        {
            m_doubleProperty->set( newMin );
            m_slider.setValue( toPercent( newMin ) );
        }
        m_doubleProperty->setMin( newMin );
    }
}