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

30 31
#include <boost/lexical_cast.hpp>

32
#include "../../../common/WLogger.h"
33 34
#include "../../../common/WPropertyVariable.h"

35 36
#include "WPropertyIntWidget.h"

37 38 39
WPropertyIntWidget::WPropertyIntWidget( WPropInt property, QGridLayout* propertyGrid, QWidget* parent ):
    WPropertyWidget( property, propertyGrid, parent ),
    m_intProperty( 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 47 48
{
    // layout both against each other
    m_layout.addWidget( &m_slider );
    m_layout.addWidget( &m_edit );
49 50
    m_parameterWidgets.setLayout( &m_layout );

51 52 53 54
    // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
    m_infoLayout.addWidget( &m_asText );
    m_informationWidgets.setLayout( &m_infoLayout );

55 56 57
    update();

    // connect the modification signal of the edit and slider with our callback
58
    connect( &m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderChanged( int ) ) );
59
    connect( &m_edit, SIGNAL( editingFinished() ), this, SLOT( editChanged() ) );
60 61 62 63 64 65 66 67 68 69
    connect( &m_edit, SIGNAL( textEdited( const QString& ) ), this, SLOT( textEdited( const QString& ) ) );
}

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

void WPropertyIntWidget::update()
{
70 71 72 73 74 75 76 77 78
    // get the min constraint
    WPVInt::PropertyConstraintMin minC = m_intProperty->getMin();
    int min = 0;
    if ( minC.get() )
    {
        min = minC->getMin();
    }
    else
    {
79 80
        wlog::warn( "PropertyWidget( " + m_intProperty->getName() + " )" ) << "The property has no minimum constraint. " <<
            "You should define it to avoid unexpected behaviour. Using default (" << min << ").";
81 82 83 84 85 86 87 88 89 90 91
    }

    // get the max constraint
    WPVInt::PropertyConstraintMax maxC = m_intProperty->getMax();
    int max = 100;
    if ( maxC.get() )
    {
        max = maxC->getMax();
    }
    else
    {
92 93
        wlog::warn( "PropertyWidget( " + m_intProperty->getName() + " )" ) << "The property has no maximum constraint. " <<
            "You should define it to avoid unexpected behaviour. Using default (" << max << ").";
94 95 96 97 98 99
    }

    // setup the slider
    m_slider.setMinimum( min );
    m_slider.setMaximum( max );

100 101 102
    // // calculate maximum size of the text widget.
    // // XXX: this is not the optimal way but works for now
    // Same as in WPropertyDouble.cpp: This does not work as expected on Mac OS X => reset to default
103 104

    // set the initial values
105
    QString valStr = QString::number( m_intProperty->get() );
106
    m_edit.setText( valStr );
107
    m_slider.setValue( m_intProperty->get() );
108 109 110

    // do not forget to update the label
    m_asText.setText( valStr );
111 112 113 114 115
}

void WPropertyIntWidget::sliderChanged( int value )
{
    // set the value in the line edit
116
    m_edit.setText( QString::number( value ) );
117 118

    // set to the property
119
    invalidate( !m_intProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

void WPropertyIntWidget::editChanged()
{
    // set the value in the line edit
    bool valid;
    int value = m_edit.text().toInt( &valid );
    if ( !valid )
    {
        invalidate();
        return;
    }

    // update slider
    m_slider.setValue( value );

136 137 138 139 140 141 142 143
    // set to the property
    invalidate( !m_intProperty->set( value ) );    // NOTE: set automatically checks the validity of the value
}

void WPropertyIntWidget::textEdited( const QString& text )
{
    // this method does NOT set the property actually, but tries to validate it
    bool valid;
144
    int value = text.toInt( &valid );
145
    if ( !valid )
146 147
    {
        invalidate();
148
        return;
149
    }
150 151 152

    // simply check validity
    invalidate( !m_intProperty->accept( value ) );
153 154
}