Skip to content
Permalink
Browse files
Merge pull request #3462 from pvalsecc/fix_auto_rangewidget
Fix auto conf of QgsRangeWidgetWrapper
  • Loading branch information
nyalldawson committed Sep 7, 2016
2 parents 03f0b44 + 5eb70d3 commit 845b2a04bfdffdbed4580a12c2a3bf29b82e67b1
@@ -15,6 +15,7 @@

#include "qgsdatetimeeditconfig.h"
#include "qgsdatetimeeditfactory.h"
#include "qgsvectorlayer.h"

QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
@@ -119,9 +120,26 @@ QgsEditorWidgetConfig QgsDateTimeEditConfig::config()
}


QString QgsDateTimeEditConfig::defaultFormat( const QVariant::Type type )
{
switch ( type )
{
case QVariant::DateTime:
return QGSDATETIMEEDIT_DATETIMEFORMAT;
break;
case QVariant::Time:
return QGSDATETIMEEDIT_TIMEFORMAT;
break;
default:
return QGSDATETIMEEDIT_DATEFORMAT;
}
}


void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
{
const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QgsField fieldDef = layer()->fields().at( field() );
const QString fieldFormat = config.value( "field_format", defaultFormat( fieldDef.type() ) ).toString();
mFieldFormatEdit->setText( fieldFormat );

if ( fieldFormat == QGSDATETIMEEDIT_DATEFORMAT )
@@ -133,7 +151,7 @@ void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
else
mFieldFormatComboBox->setCurrentIndex( 3 );

QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
QString displayFormat = config.value( "display_format", defaultFormat( fieldDef.type() ) ).toString();
mDisplayFormatEdit->setText( displayFormat );
if ( displayFormat == mFieldFormatEdit->text() )
{
@@ -41,6 +41,13 @@ class GUI_EXPORT QgsDateTimeEditConfig : public QgsEditorConfigWidget, private U
public:
QgsEditorWidgetConfig config() override;
void setConfig( const QgsEditorWidgetConfig &config ) override;

/**
* Get the default format in fonction of the type
* @param type the field type
* @return the date/time format
*/
static QString defaultFormat( const QVariant::Type type );
};

#endif // QGSDATETIMEEDITCONFIG_H
@@ -81,8 +81,9 @@ QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx
return settings.value( "qgis/nullValue", "NULL" ).toString();
}

const QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QgsField field = vl->fields().at( fieldIdx );
const QString displayFormat = config.value( "display_format", QgsDateTimeEditConfig::defaultFormat( field.type() ) ).toString();
const QString fieldFormat = config.value( "field_format", QgsDateTimeEditConfig::defaultFormat( field.type() ) ).toString();

QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );

@@ -120,7 +121,7 @@ unsigned int QgsDateTimeEditFactory::fieldScore( const QgsVectorLayer* vl, int f
const QgsField field = vl->fields().field( fieldIdx );
const QVariant::Type type = field.type();
const QgsEditorWidgetConfig config = vl->editFormConfig().widgetConfig( field.name() );
if ( type == QVariant::DateTime || config.contains( "field_format" ) )
if ( type == QVariant::DateTime || type == QVariant::Date || type == QVariant::Time || config.contains( "field_format" ) )
{
return 20;
}
@@ -18,6 +18,7 @@
#include "qgsmessagelog.h"
#include "qgslogger.h"
#include "qgsdatetimeedit.h"
#include "qgsdatetimeeditconfig.h"

#include <QDateTimeEdit>
#include <QDateEdit>
@@ -62,7 +63,7 @@ void QgsDateTimeEditWrapper::initWidget( QWidget *editor )
return;
}

const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString displayFormat = config( "display_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
mQDateTimeEdit->setDisplayFormat( displayFormat );

const bool calendar = config( "calendar_popup", false ).toBool();
@@ -110,7 +111,7 @@ void QgsDateTimeEditWrapper::showIndeterminateState()

void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
{
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
emit valueChanged( dateTime.toString( fieldFormat ) );
}

@@ -131,7 +132,7 @@ QVariant QgsDateTimeEditWrapper::value() const
}
}

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();

if ( mQgsDateTimeEdit )
{
@@ -148,7 +149,7 @@ void QgsDateTimeEditWrapper::setValue( const QVariant &value )
if ( !mQDateTimeEdit )
return;

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( field().type() ) ).toString();
const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );

if ( mQgsDateTimeEdit )
@@ -20,6 +20,7 @@
#include "qgsvectorlayer.h"
#include "qgsdatetimeedit.h"
#include "qcalendarwidget.h"
#include "qgsdatetimeeditconfig.h"

#include <QSettings>

@@ -45,7 +46,7 @@ QVariant QgsDateTimeSearchWidgetWrapper::value() const
if ( ! mDateTimeEdit )
return QDateTime();

const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString fieldFormat = config( "field_format", QgsDateTimeEditConfig::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
return mDateTimeEdit->dateTime().toString( fieldFormat );
}

@@ -151,7 +152,7 @@ void QgsDateTimeSearchWidgetWrapper::initWidget( QWidget* editor )
{
mDateTimeEdit->setAllowNull( false );

const QString displayFormat = config( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
const QString displayFormat = config( "display_format", QgsDateTimeEditConfig::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
mDateTimeEdit->setDisplayFormat( displayFormat );

const bool calendar = config( "calendar_popup", false ).toBool();
@@ -100,12 +100,12 @@ QgsEditorWidgetConfig QgsRangeConfigDlg::config()

void QgsRangeConfigDlg::setConfig( const QgsEditorWidgetConfig& config )
{
minimumDoubleSpinBox->setValue( config.value( "Min", 0.0 ).toDouble() );
maximumDoubleSpinBox->setValue( config.value( "Max", 5.0 ).toDouble() );
minimumDoubleSpinBox->setValue( config.value( "Min", -std::numeric_limits<double>::max() ).toDouble() );
maximumDoubleSpinBox->setValue( config.value( "Max", std::numeric_limits<double>::max() ).toDouble() );
stepDoubleSpinBox->setValue( config.value( "Step", 1.0 ).toDouble() );

minimumSpinBox->setValue( config.value( "Min", 0 ).toInt() );
maximumSpinBox->setValue( config.value( "Max", 5 ).toInt() );
minimumSpinBox->setValue( config.value( "Min", std::numeric_limits<int>::min() ).toInt() );
maximumSpinBox->setValue( config.value( "Max", std::numeric_limits<int>::max() ).toInt() );
stepSpinBox->setValue( config.value( "Step", 1 ).toInt() );

rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( "Style", "SpinBox" ) ) );
@@ -73,7 +73,10 @@ void QgsRangeWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QD

unsigned int QgsRangeWidgetFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
{
return vl->fields().at( fieldIdx ).isNumeric() ? 20 : 0;
const QgsField field = vl->fields().at( fieldIdx );
if ( field.type() == QVariant::Int || field.type() == QVariant::Double ) return 20;
if ( field.isNumeric() ) return 5; // widgets used support only signed 32bits (int) and double
return 0;
}

QMap<const char*, int> QgsRangeWidgetFactory::supportedWidgetTypes()
@@ -65,10 +65,21 @@ QWidget* QgsRangeWidgetWrapper::createWidget( QWidget* parent )
return editor;
}

template<class T>
static void setupIntEditor( const QVariant& min, const QVariant& max, const QVariant& step, T* slider, QgsRangeWidgetWrapper* wrapper )
{
// must use a template function because those methods are overloaded and not inherited by some classes
slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::min() );
slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
QObject::connect( slider, SIGNAL( valueChanged( int ) ), wrapper, SLOT( valueChanged( int ) ) );
}

void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
{
mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor );
mIntSpinBox = qobject_cast<QSpinBox*>( editor );

mDial = qobject_cast<QDial*>( editor );
mSlider = qobject_cast<QSlider*>( editor );
mQgsDial = qobject_cast<QgsDial*>( editor );
@@ -107,90 +118,40 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
mDoubleSpinBox->setValue( minval );
mDoubleSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
}
if ( min.isValid() )
mDoubleSpinBox->setMinimum( min.toDouble() );
if ( max.isValid() )
mDoubleSpinBox->setMaximum( max.toDouble() );
if ( step.isValid() )
mDoubleSpinBox->setSingleStep( step.toDouble() );
mDoubleSpinBox->setMinimum( min.isValid() ? min.toDouble() : std::numeric_limits<double>::min() );
mDoubleSpinBox->setMaximum( max.isValid() ? max.toDouble() : std::numeric_limits<double>::max() );
mDoubleSpinBox->setSingleStep( step.isValid() ? step.toDouble() : 1.0 );
if ( config( "Suffix" ).isValid() )
mDoubleSpinBox->setSuffix( config( "Suffix" ).toString() );

connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) );
}

if ( mIntSpinBox )
else if ( mIntSpinBox )
{
int minval = min.toInt();
int stepval = step.toInt();
QgsSpinBox* qgsWidget = dynamic_cast<QgsSpinBox*>( mIntSpinBox );
if ( qgsWidget )
qgsWidget->setShowClearButton( allowNull );
if ( allowNull )
{
int minval = min.toInt();
int stepval = step.toInt();
minval -= stepval;
mIntSpinBox->setValue( minval );
mIntSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
}
if ( min.isValid() )
mIntSpinBox->setMinimum( min.toInt() );
if ( max.isValid() )
mIntSpinBox->setMaximum( max.toInt() );
if ( step.isValid() )
mIntSpinBox->setSingleStep( step.toInt() );
setupIntEditor( min, max, step, mIntSpinBox, this );
if ( config( "Suffix" ).isValid() )
mIntSpinBox->setSuffix( config( "Suffix" ).toString() );
connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
}


if ( mQgsDial || mQgsSlider )
else
{
field().convertCompatible( min );
field().convertCompatible( max );
field().convertCompatible( step );

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

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

connect( editor, SIGNAL( valueChanged( QVariant ) ), this, SLOT( valueChangedVariant( QVariant ) ) );
}
else if ( mDial )
{
if ( min.isValid() )
mDial->setMinimum( min.toInt() );
if ( max.isValid() )
mDial->setMaximum( max.toInt() );
if ( step.isValid() )
mDial->setSingleStep( step.toInt() );
connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
}
else if ( mSlider )
{
if ( min.isValid() )
mSlider->setMinimum( min.toInt() );
if ( max.isValid() )
mSlider->setMaximum( max.toInt() );
if ( step.isValid() )
mSlider->setSingleStep( step.toInt() );
connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
if ( mQgsDial ) setupIntEditor( min, max, step, mQgsDial, this );
else if ( mQgsSlider ) setupIntEditor( min, max, step, mQgsSlider, this );
else if ( mDial ) setupIntEditor( min, max, step, mDial, this );
else if ( mSlider ) setupIntEditor( min, max, step, mSlider, this );
}
}

@@ -21,6 +21,7 @@
#include <QSpinBox>
#include <QDoubleSpinBox>

class QAbstractSlider;
class QSlider;
class QDial;
class QgsSlider;
@@ -93,10 +93,13 @@
<item row="0" column="1">
<widget class="QSpinBox" name="minimumSpinBox">
<property name="minimum">
<number>-999999999</number>
<number>−2147483648</number>
</property>
<property name="maximum">
<number>999999999</number>
<number>2147483647</number>
</property>
<property name="value">
<number>−2147483648</number>
</property>
</widget>
</item>
@@ -110,13 +113,13 @@
<item row="1" column="1">
<widget class="QSpinBox" name="maximumSpinBox">
<property name="minimum">
<number>-999999999</number>
<number>−2147483648</number>
</property>
<property name="maximum">
<number>999999999</number>
<number>2147483647</number>
</property>
<property name="value">
<number>5</number>
<number>2147483647</number>
</property>
</widget>
</item>
@@ -130,7 +133,7 @@
<item row="2" column="1">
<widget class="QSpinBox" name="stepSpinBox">
<property name="maximum">
<number>999999999</number>
<number>2147483647</number>
</property>
<property name="value">
<number>1</number>
@@ -168,30 +171,33 @@
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="minimumDoubleSpinBox">
<property name="minimum">
<double>-999999999.990000009536743</double>
<double>-1.79769e+308</double>
</property>
<property name="maximum">
<double>999999999.990000009536743</double>
<double>1.79769e+308</double>
</property>
<property name="value">
<double>-1.79769e+308</double>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="maximumDoubleSpinBox">
<property name="minimum">
<double>-999999999.990000009536743</double>
<double>-1.79769e+308</double>
</property>
<property name="maximum">
<double>999999999.990000009536743</double>
<double>1.79769e+308</double>
</property>
<property name="value">
<double>5.000000000000000</double>
<double>1.79769e+308</double>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="stepDoubleSpinBox">
<property name="maximum">
<double>999999999.990000009536743</double>
<double>1.79769e+308</double>
</property>
<property name="value">
<double>1.000000000000000</double>

0 comments on commit 845b2a0

Please sign in to comment.