WPropertyIntWidget.cpp 6.82 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 <cmath>
26
#include <limits>
27 28
#include <sstream>
#include <iostream>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
29
#include <string>
30

31
#include <QtGui/QInputDialog>
32

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

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

42 43
const int WPropertyIntWidget::SliderResolution = 10000;

44 45
WPropertyIntWidget::WPropertyIntWidget( WPropInt 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 67 68 69 70 71 72 73 74 75
    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 );

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

    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
}

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

void WPropertyIntWidget::update()
{
102 103
    // // calculate maximum size of the text widget.
    // // XXX: this is not the optimal way but works for now
104 105
    // 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_integralProperty->get() );
106 107
    m_edit.setText( valStr );

108 109 110
       // get the min constraint
    WPVInt::PropertyConstraintMin minC = m_integralProperty->getMin();
    WPVInt::PropertyConstraintMax maxC = m_integralProperty->getMax();
111
    bool minMaxConstrained = minC && maxC;
112
    if( minMaxConstrained )
113
    {
114 115 116
        // setup the slider
        m_slider.setMinimum( 0 );
        m_slider.setMaximum( SliderResolution );
117

118 119 120 121 122 123 124 125 126 127
        // 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();

        // updating the interval edit causes the proper values to be set in m_min and m_max
        m_slider.setHidden( false );
        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 140 141 142 143 144 145 146 147 148 149
int WPropertyIntWidget::toSliderValue( double value )
{
    int perc = static_cast< double >( SliderResolution ) * ( ( value - m_min ) / ( m_max - m_min ) );
    return std::min( std::max( perc, 0 ), SliderResolution );
}

double WPropertyIntWidget::fromSliderValue( int perc )
{
    return ( static_cast< double >( perc ) / static_cast< double >( SliderResolution ) ) * ( m_max - m_min ) + m_min;
}

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

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

void WPropertyIntWidget::editChanged()
{
    // set the value in the line edit
    bool valid;
    int value = m_edit.text().toInt( &valid );
167
    if( !valid )
168 169 170 171
    {
        invalidate();
        return;
    }
172 173
    // set to the property
    invalidate( !m_integralProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
174 175

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

void WPropertyIntWidget::textEdited( const QString& text )
{
    // this method does NOT set the property actually, but tries to validate it
    bool valid;
183
    int value = text.toInt( &valid );
184
    if( !valid )
185 186
    {
        invalidate();
187
        return;
188
    }
189 190

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

194
void WPropertyIntWidget::minMaxUpdated()
195
{
196 197 198 199
    m_min = m_intervalEdit.getMin();
    m_max = m_intervalEdit.getMax();

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

    m_slider.setValue( toSliderValue( m_integralProperty->get() ) );
209
}
210