QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19#include "qgsvectorlayer.h"
20
21#include "moc_qgsrangeconfigdlg.cpp"
22
23QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
24 : QgsEditorConfigWidget( vl, fieldIdx, parent )
25{
26 setupUi( this );
27 precisionSpinBox->setClearValue( 4 );
28 setPrecision( precisionSpinBox->value() );
29
30 minimumSpinBox->setMinimum( std::numeric_limits<int>::lowest() );
31 minimumSpinBox->setMaximum( std::numeric_limits<int>::max() );
32 minimumSpinBox->setValue( std::numeric_limits<int>::lowest() );
33
34 maximumSpinBox->setMinimum( std::numeric_limits<int>::lowest() );
35 maximumSpinBox->setMaximum( std::numeric_limits<int>::max() );
36 maximumSpinBox->setValue( std::numeric_limits<int>::max() );
37
38 stepSpinBox->setMaximum( std::numeric_limits<int>::max() );
39 stepSpinBox->setValue( 1 );
40 stepSpinBox->setClearValue( 1 );
41
42 minimumDoubleSpinBox->setMinimum( std::numeric_limits<double>::lowest() );
43 minimumDoubleSpinBox->setMaximum( std::numeric_limits<double>::max() );
44 minimumDoubleSpinBox->setValue( std::numeric_limits<double>::min() );
45
46 maximumDoubleSpinBox->setMinimum( std::numeric_limits<double>::lowest() );
47 maximumDoubleSpinBox->setMaximum( std::numeric_limits<double>::max() );
48 maximumDoubleSpinBox->setValue( std::numeric_limits<double>::max() );
49
50 // Use integer here:
51 stepDoubleSpinBox->setMaximum( std::numeric_limits<int>::max() );
52 stepDoubleSpinBox->setValue( 1 );
53 stepDoubleSpinBox->setClearValue( 1 );
54
55
56 QString text;
57
58 const QMetaType::Type fieldType( vl->fields().at( fieldIdx ).type() );
59
60 switch ( fieldType )
61 {
62 case QMetaType::Type::Int:
63 case QMetaType::Type::LongLong:
64 case QMetaType::Type::Double:
65 {
66 // we use the double spin boxes for double OR long long field types, as QSpinBox does not have sufficient
67 // available range for long long values
68 rangeStackedWidget->setCurrentIndex( fieldType == QMetaType::Type::Int ? 0 : 1 );
69 if ( fieldType == QMetaType::Type::LongLong )
70 {
71 minimumDoubleSpinBox->setDecimals( 0 );
72 maximumDoubleSpinBox->setDecimals( 0 );
73 stepDoubleSpinBox->setDecimals( 0 );
74 }
75
76 rangeWidget->clear();
77 rangeWidget->addItem( tr( "Editable" ), QStringLiteral( "SpinBox" ) );
78 rangeWidget->addItem( tr( "Slider" ), QStringLiteral( "Slider" ) );
79 rangeWidget->addItem( tr( "Dial" ), QStringLiteral( "Dial" ) );
80
81 QVariant min;
82 QVariant max;
83 vl->minimumAndMaximumValue( fieldIdx, min, max );
84
85 text = tr( "Current minimum for this value is %1 and current maximum is %2." ).arg( min.toString(), max.toString() );
86 break;
87 }
88
89 default:
90 text = tr( "Attribute has no integer or real type, therefore range is not usable." );
91 break;
92 }
93
94 // Hide precision for integer types
95 if ( fieldType != QMetaType::Type::Double )
96 {
97 precisionSpinBox->hide();
98 precisionLabel->hide();
99 }
100
101 valuesLabel->setText( text );
102
103 connect( rangeWidget, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRangeConfigDlg::rangeWidgetChanged );
104 connect( minimumSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
105 connect( maximumSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
106 connect( stepSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
107 connect( minimumDoubleSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
108 connect( maximumDoubleSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
109 connect( stepDoubleSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
110 connect( rangeWidget, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
111 connect( allowNullCheckBox, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
112 connect( suffixLineEdit, &QLineEdit::textChanged, this, &QgsEditorConfigWidget::changed );
113 connect( precisionSpinBox, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsRangeConfigDlg::setPrecision );
114}
115
117{
118 QVariantMap cfg;
119
120 switch ( layer()->fields().at( field() ).type() )
121 {
122 case QMetaType::Type::Int:
123 cfg.insert( QStringLiteral( "Min" ), minimumSpinBox->value() );
124 cfg.insert( QStringLiteral( "Max" ), maximumSpinBox->value() );
125 cfg.insert( QStringLiteral( "Step" ), stepSpinBox->value() );
126 break;
127
128 // we use the double spin boxes for double OR long long field types, as QSpinBox does not have sufficient
129 // available range for long long values
130 case QMetaType::Type::Double:
131 case QMetaType::Type::LongLong:
132 cfg.insert( QStringLiteral( "Min" ), minimumDoubleSpinBox->value() );
133 cfg.insert( QStringLiteral( "Max" ), maximumDoubleSpinBox->value() );
134 cfg.insert( QStringLiteral( "Step" ), stepDoubleSpinBox->value() );
135 break;
136
137 default:
138 break;
139 }
140
141 cfg.insert( QStringLiteral( "Style" ), rangeWidget->currentData().toString() );
142 cfg.insert( QStringLiteral( "AllowNull" ), allowNullCheckBox->isChecked() );
143 cfg.insert( QStringLiteral( "Precision" ), precisionSpinBox->value() );
144
145 if ( !suffixLineEdit->text().isEmpty() )
146 {
147 cfg.insert( QStringLiteral( "Suffix" ), suffixLineEdit->text() );
148 }
149
150 return cfg;
151}
152
153void QgsRangeConfigDlg::setConfig( const QVariantMap &config )
154{
155 minimumDoubleSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<double>::lowest() ).toDouble() );
156 maximumDoubleSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<double>::max() ).toDouble() );
157 stepDoubleSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1.0 ).toDouble() );
158 minimumSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<int>::lowest() ).toInt() );
159 maximumSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<int>::max() ).toInt() );
160 stepSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1 ).toInt() );
161 rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( QStringLiteral( "Style" ), "SpinBox" ) ) );
162 suffixLineEdit->setText( config.value( QStringLiteral( "Suffix" ) ).toString() );
163 allowNullCheckBox->setChecked( config.value( QStringLiteral( "AllowNull" ), true ).toBool() );
164
165 const QgsField layerField = layer()->fields().at( field() );
166 const int fieldPrecision = QgsRangeWidgetWrapper::defaultFieldPrecision( layerField );
167 precisionSpinBox->setValue( config.value( QStringLiteral( "Precision" ), fieldPrecision ).toInt() );
168}
169
171{
172 const QString style = rangeWidget->itemData( index ).toString();
173 allowNullCheckBox->setEnabled( style == QLatin1String( "SpinBox" ) );
174}
175
177{
178 minimumDoubleSpinBox->setDecimals( precision );
179 maximumDoubleSpinBox->setDecimals( precision );
180 stepDoubleSpinBox->setDecimals( precision );
181}
QgsEditorConfigWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
Create a new configuration widget.
int field()
Returns the field for which this configuration widget applies.
QgsVectorLayer * layer()
Returns the layer for which this configuration widget applies.
void changed()
Emitted when the configuration of the widget is changed.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QMetaType::Type type
Definition qgsfield.h:61
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QVariantMap config() override
Create a configuration from the current GUI state.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QgsRangeConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
void rangeWidgetChanged(int index)
void setPrecision(int precision)
Sets the precision of minimum value, maximum value, step size UI elements.
static int defaultFieldPrecision(const QgsField &field)
Returns the default field precision to use for a field.
Represents a vector layer which manages a vector based dataset.
void minimumAndMaximumValue(int index, QVariant &minimum, QVariant &maximum) const
Calculates both the minimum and maximum value for an attribute column.