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