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