QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsrangeconfigdlg.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrangeconfigdlgbase.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsrangeconfigdlg.h"
17 
18 #include "qgsvectorlayer.h"
19 
21  : QgsEditorConfigWidget( vl, fieldIdx, parent )
22 {
23  setupUi( this );
24 
25  QString text;
26 
27  switch ( vl->fields().at( fieldIdx ).type() )
28  {
29  case QVariant::Int:
30  case QVariant::LongLong:
31  case QVariant::Double:
32  {
33  rangeStackedWidget->setCurrentIndex( vl->fields().at( fieldIdx ).type() == QVariant::Double ? 1 : 0 );
34 
35  rangeWidget->clear();
36  rangeWidget->addItem( tr( "Editable" ), "SpinBox" );
37  rangeWidget->addItem( tr( "Slider" ), "Slider" );
38  rangeWidget->addItem( tr( "Dial" ), "Dial" );
39 
40  QVariant min = vl->minimumValue( fieldIdx );
41  QVariant max = vl->maximumValue( fieldIdx );
42 
43  text = tr( "Current minimum for this value is %1 and current maximum is %2." ).arg( min.toString(), max.toString() );
44  break;
45  }
46 
47  default:
48  text = tr( "Attribute has no integer or real type, therefore range is not usable." );
49  break;
50  }
51 
52  valuesLabel->setText( text );
53 
54  connect( rangeWidget, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rangeWidgetChanged( int ) ) );
55 
56  connect( minimumSpinBox, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
57  connect( maximumSpinBox, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
58  connect( stepSpinBox, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
59  connect( minimumDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SIGNAL( changed() ) );
60  connect( maximumDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SIGNAL( changed() ) );
61  connect( stepDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SIGNAL( changed() ) );
62  connect( rangeWidget, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
63  connect( allowNullCheckBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
64  connect( suffixLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
65 }
66 
68 {
70 
71  switch ( layer()->fields().at( field() ).type() )
72  {
73  case QVariant::Int:
74  case QVariant::LongLong:
75  cfg.insert( "Min", minimumSpinBox->value() );
76  cfg.insert( "Max", maximumSpinBox->value() );
77  cfg.insert( "Step", stepSpinBox->value() );
78  break;
79 
80  case QVariant::Double:
81  cfg.insert( "Min", minimumDoubleSpinBox->value() );
82  cfg.insert( "Max", maximumDoubleSpinBox->value() );
83  cfg.insert( "Step", stepDoubleSpinBox->value() );
84  break;
85 
86  default:
87  break;
88  }
89 
90  cfg.insert( "Style", rangeWidget->itemData( rangeWidget->currentIndex() ).toString() );
91  cfg.insert( "AllowNull", allowNullCheckBox->isChecked() );
92 
93  if ( suffixLineEdit->text() != "" )
94  {
95  cfg.insert( "Suffix", suffixLineEdit->text() );
96  }
97 
98  return cfg;
99 }
100 
102 {
103  minimumDoubleSpinBox->setValue( config.value( "Min", 0.0 ).toDouble() );
104  maximumDoubleSpinBox->setValue( config.value( "Max", 5.0 ).toDouble() );
105  stepDoubleSpinBox->setValue( config.value( "Step", 1.0 ).toDouble() );
106 
107  minimumSpinBox->setValue( config.value( "Min", 0 ).toInt() );
108  maximumSpinBox->setValue( config.value( "Max", 5 ).toInt() );
109  stepSpinBox->setValue( config.value( "Step", 1 ).toInt() );
110 
111  rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( "Style", "SpinBox" ) ) );
112 
113  suffixLineEdit->setText( config.value( "Suffix" ).toString() );
114 
115  allowNullCheckBox->setChecked( config.value( "AllowNull", true ).toBool() );
116 }
117 
119 {
120  QString style = rangeWidget->itemData( index ).toString();
121  allowNullCheckBox->setEnabled( style == "SpinBox" );
122 }
QgsVectorLayer * layer()
Returns the layer for which this configuration widget applies.
int field()
Returns the field for which this configuration widget applies.
static unsigned index
void rangeWidgetChanged(int index)
void setupUi(QWidget *widget)
This class should be subclassed for every configurable editor widget type.
QVariant maximumValue(int index)
Returns the maximum value for an attribute column or an invalid variant in case of error...
QStyle * style() const
QVariant minimumValue(int index)
Returns the minimum value for an attribute column or an invalid variant in case of error...
QString tr(const char *sourceText, const char *disambiguation, int n)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
void clear()
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
virtual void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
void changed()
Emitted when the configuration of the widget is changed.
QgsRangeConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
virtual QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.