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