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