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