Skip to content

Commit

Permalink
edit widgets: don't truncate double ranges and also support them in s…
Browse files Browse the repository at this point in the history
…liders and

dials (fixes #12421)

(cherry picked from commit 91e44ea and c6996ac)
  • Loading branch information
jef-n committed Jun 29, 2015
1 parent e64100f commit 1bf1b33
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 36 deletions.
1 change: 1 addition & 0 deletions doc/CONTRIBUTORS
Expand Up @@ -17,6 +17,7 @@ Carlos Dávila
Carson J. Q. Farmer
Christian Ferreira
Cédric Möri
Daniel Vaz
Denis Rouzaud
Diego Moreira
Duarte Carreira
Expand Down
24 changes: 4 additions & 20 deletions src/gui/editorwidgets/qgsrangeconfigdlg.cpp
Expand Up @@ -17,8 +17,8 @@

#include "qgsvectorlayer.h"

QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget *parent ) :
QgsEditorConfigWidget( vl, fieldIdx, parent )
QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
{
setupUi( this );

Expand All @@ -28,28 +28,14 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget
{
case QVariant::Int:
case QVariant::LongLong:
{
rangeStackedWidget->setCurrentIndex( 0 );

rangeWidget->clear();
rangeWidget->addItem( tr( "Editable" ), "SpinBox" );
rangeWidget->addItem( tr( "Slider" ), "Slider" );
rangeWidget->addItem( tr( "Dial" ), "Dial" );

QVariant min = vl->minimumValue( fieldIdx );
QVariant max = vl->maximumValue( fieldIdx );

text = tr( "Current minimum for this value is %1 and current maximum is %2." ).arg( min.toString() ).arg( max.toString() );
break;
}

case QVariant::Double:
{
rangeStackedWidget->setCurrentIndex( 1 );
rangeStackedWidget->setCurrentIndex( vl->pendingFields()[fieldIdx].type() == QVariant::Double ? 1 : 0 );

rangeWidget->clear();
rangeWidget->addItem( tr( "Editable" ), "SpinBox" );
rangeWidget->addItem( tr( "Slider" ), "Slider" );
rangeWidget->addItem( tr( "Dial" ), "Dial" );

QVariant min = vl->minimumValue( fieldIdx );
QVariant max = vl->maximumValue( fieldIdx );
Expand All @@ -59,10 +45,8 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget
}

default:
{
text = tr( "Attribute has no integer or real type, therefore range is not usable." );
break;
}
}

valuesLabel->setText( text );
Expand Down
12 changes: 6 additions & 6 deletions src/gui/editorwidgets/qgsrangewidgetfactory.cpp
Expand Up @@ -41,9 +41,9 @@ QgsEditorWidgetConfig QgsRangeWidgetFactory::readConfig( const QDomElement& conf
QMap<QString, QVariant> cfg;

cfg.insert( "Style", configElement.attribute( "Style" ) );
cfg.insert( "Min", configElement.attribute( "Min" ).toInt() );
cfg.insert( "Max", configElement.attribute( "Max" ).toInt() );
cfg.insert( "Step", configElement.attribute( "Step" ).toInt() );
cfg.insert( "Min", configElement.attribute( "Min" ) );
cfg.insert( "Max", configElement.attribute( "Max" ) );
cfg.insert( "Step", configElement.attribute( "Step" ) );
cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) == "1" );

if ( configElement.hasAttribute( "Suffix" ) )
Expand All @@ -61,9 +61,9 @@ void QgsRangeWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QD
Q_UNUSED( fieldIdx );

configElement.setAttribute( "Style", config["Style"].toString() );
configElement.setAttribute( "Min", config["Min"].toInt() );
configElement.setAttribute( "Max", config["Max"].toInt() );
configElement.setAttribute( "Step", config["Step"].toInt() );
configElement.setAttribute( "Min", config["Min"].toString() );
configElement.setAttribute( "Max", config["Max"].toString() );
configElement.setAttribute( "Step", config["Step"].toString() );
configElement.setAttribute( "AllowNull", config["AllowNull"].toBool() );
if ( config.contains( "Suffix" ) )
{
Expand Down
67 changes: 60 additions & 7 deletions src/gui/editorwidgets/qgsrangewidgetwrapper.cpp
Expand Up @@ -21,11 +21,13 @@
#include "qgsvectorlayer.h"

QgsRangeWidgetWrapper::QgsRangeWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mIntSpinBox( 0 )
, mDoubleSpinBox( 0 )
, mSlider( 0 )
, mDial( 0 )
, mQgsSlider( 0 )
, mQgsDial( 0 )
{
}

Expand Down Expand Up @@ -68,6 +70,8 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
mIntSpinBox = qobject_cast<QSpinBox*>( editor );
mDial = qobject_cast<QDial*>( editor );
mSlider = qobject_cast<QSlider*>( editor );
mQgsDial = qobject_cast<QgsDial*>( editor );
mQgsSlider = qobject_cast<QgsSlider*>( editor );

bool allowNull = config( "AllowNull" ).toBool();

Expand Down Expand Up @@ -131,15 +135,40 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
}

if ( mDial )
if ( mQgsDial || mQgsSlider )
{
QVariant min( config( "Min" ) );
QVariant max( config( "Max" ) );
QVariant step( config( "Step" ) );

field().convertCompatible( min );
field().convertCompatible( max );
field().convertCompatible( step );

if ( mQgsSlider )
{
mQgsSlider->setMinimum( min );
mQgsSlider->setMaximum( max );
mQgsSlider->setSingleStep( step );
}

if ( mQgsDial )
{
mQgsDial->setMinimum( min );
mQgsDial->setMaximum( max );
mQgsDial->setSingleStep( step );
}

connect( editor, SIGNAL( valueChanged( QVariant ) ), this, SLOT( valueChanged( QVariant ) ) );
}
else if ( mDial )
{
mDial->setMinimum( config( "Min" ).toInt() );
mDial->setMaximum( config( "Max" ).toInt() );
mDial->setSingleStep( config( "Step" ).toInt() );
connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
}

if ( mSlider )
else if ( mSlider )
{
mSlider->setMinimum( config( "Min" ).toInt() );
mSlider->setMaximum( config( "Max" ).toInt() );
Expand All @@ -148,6 +177,14 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
}
}

void QgsRangeWidgetWrapper::valueChanged( QVariant v )
{
if ( v.type() == QVariant::Int )
valueChanged( v.toInt() );
if ( v.type() == QVariant::Double )
valueChanged( v.toDouble() );
}

QVariant QgsRangeWidgetWrapper::value()
{
QVariant value;
Expand All @@ -168,6 +205,14 @@ QVariant QgsRangeWidgetWrapper::value()
value = QVariant( field().type() );
}
}
else if ( mQgsDial )
{
value = mQgsDial->variantValue();
}
else if ( mQgsSlider )
{
value = mQgsSlider->variantValue();
}
else if ( mDial )
{
value = mDial->value();
Expand Down Expand Up @@ -205,13 +250,21 @@ void QgsRangeWidgetWrapper::setValue( const QVariant& value )
mIntSpinBox->setValue( value.toInt() );
}
}
if ( mDial )

if ( mQgsDial )
{
mQgsDial->setValue( value );
}
else if ( mQgsSlider )
{
mQgsSlider->setValue( value );
}
else if ( mDial )
{
mDial->setValue( value.toInt() );
}
if ( mSlider )
else if ( mSlider )
{
mSlider->setValue( value.toInt() );
}
}

5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsrangewidgetwrapper.h
Expand Up @@ -54,11 +54,16 @@ class GUI_EXPORT QgsRangeWidgetWrapper : public QgsEditorWidgetWrapper
public slots:
virtual void setValue( const QVariant& value ) override;

public slots:
void valueChanged( QVariant );

private:
QSpinBox* mIntSpinBox;
QDoubleSpinBox* mDoubleSpinBox;
QSlider* mSlider;
QDial* mDial;
QgsSlider* mQgsSlider;
QgsDial* mQgsDial;
};

#endif // QGSRANGEWIDGETWRAPPER_H
116 changes: 115 additions & 1 deletion src/gui/qgsdial.cpp
@@ -1,4 +1,22 @@
/***************************************************************************
qgsdial.cpp
-------------------
begin : July 2013
copyright : (C) 2013 by Daniel Vaz
email : danielvaz at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsdial.h"
#include "qgslogger.h"

#include <QRect>

Expand All @@ -14,6 +32,102 @@ void QgsDial::paintEvent( QPaintEvent *event )
QRect rect = geometry();
painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
painter.drawText( QRectF( 0, rect.height() * 0.65, rect.width(), rect.height() ),
Qt::AlignHCenter, QString::number( value() ), 0 );
Qt::AlignHCenter, variantValue().toString(), 0 );
painter.end();
}

void QgsDial::setMinimum( const QVariant &min )
{
mMin = min;
update();
}

void QgsDial::setMaximum( const QVariant &max )
{
mMax = max;
update();
}

void QgsDial::setSingleStep( const QVariant &step )
{
mStep = step;
update();
}

void QgsDial::setValue( const QVariant &value )
{
mValue = value;
update();
}

void QgsDial::update()
{
if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
return;

if ( mValue.isNull() )
mValue = mMin;

if ( mMin.type() == QVariant::Int &&
mMax.type() == QVariant::Int &&
mStep.type() == QVariant::Int &&
mValue.type() == QVariant::Int )
{
QDial::setMinimum( mMin.toInt() );
QDial::setMaximum( mMax.toInt() );
QDial::setSingleStep( mStep.toInt() );
QDial::setValue( mValue.toInt() );
}

if ( mMin.type() == QVariant::Double &&
mMax.type() == QVariant::Double &&
mStep.type() == QVariant::Double &&
mValue.type() == QVariant::Double )
{
if ( minimum() != 0 )
QDial::setMinimum( 0 );

int max = ( int ) ceil(( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
if ( maximum() != max )
QDial::setMaximum( max );

if ( singleStep() != 1 )
QDial::setSingleStep( 1 );

QDial::setValue(( int ) ceil(( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
}

connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
}

QVariant QgsDial::variantValue() const
{
return mValue;
}

void QgsDial::valueChanged( int value )
{
if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
{
mValue = QVariant();
return;
}

if ( mMin.type() == QVariant::Int &&
mMax.type() == QVariant::Int &&
mStep.type() == QVariant::Int &&
mValue.type() == QVariant::Int )
{
mValue = value;
return;
}

if ( mMin.type() == QVariant::Double &&
mMax.type() == QVariant::Double &&
mStep.type() == QVariant::Double &&
mValue.type() == QVariant::Double )
{
mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
return;
}
}

0 comments on commit 1bf1b33

Please sign in to comment.