QGIS API Documentation  2.12.0-Lyon
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 
21  : QgsEditorConfigWidget( vl, fieldIdx, parent )
22 {
23  setupUi( this );
24 
25  QString text;
26 
27  switch ( vl->fields().at( fieldIdx ).type() )
28  {
29  case QVariant::Int:
30  case QVariant::LongLong:
31  case QVariant::Double:
32  {
33  rangeStackedWidget->setCurrentIndex( vl->fields().at( fieldIdx ).type() == QVariant::Double ? 1 : 0 );
34 
35  rangeWidget->clear();
36  rangeWidget->addItem( tr( "Editable" ), "SpinBox" );
37  rangeWidget->addItem( tr( "Slider" ), "Slider" );
38  rangeWidget->addItem( tr( "Dial" ), "Dial" );
39 
40  QVariant min = vl->minimumValue( fieldIdx );
41  QVariant max = vl->maximumValue( fieldIdx );
42 
43  text = tr( "Current minimum for this value is %1 and current maximum is %2." ).arg( min.toString(), max.toString() );
44  break;
45  }
46 
47  default:
48  text = tr( "Attribute has no integer or real type, therefore range is not usable." );
49  break;
50  }
51 
52  valuesLabel->setText( text );
53 
54  connect( rangeWidget, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rangeWidgetChanged( int ) ) );
55 }
56 
58 {
60 
61  switch ( layer()->fields().at( field() ).type() )
62  {
63  case QVariant::Int:
64  case QVariant::LongLong:
65  cfg.insert( "Min", minimumSpinBox->value() );
66  cfg.insert( "Max", maximumSpinBox->value() );
67  cfg.insert( "Step", stepSpinBox->value() );
68  break;
69 
70  case QVariant::Double:
71  cfg.insert( "Min", minimumDoubleSpinBox->value() );
72  cfg.insert( "Max", maximumDoubleSpinBox->value() );
73  cfg.insert( "Step", stepDoubleSpinBox->value() );
74  break;
75 
76  default:
77  break;
78  }
79 
80  cfg.insert( "Style", rangeWidget->itemData( rangeWidget->currentIndex() ).toString() );
81  cfg.insert( "AllowNull", allowNullCheckBox->isChecked() );
82 
83  if ( suffixLineEdit->text() != "" )
84  {
85  cfg.insert( "Suffix", suffixLineEdit->text() );
86  }
87 
88  return cfg;
89 }
90 
92 {
93  minimumDoubleSpinBox->setValue( config.value( "Min", 0.0 ).toDouble() );
94  maximumDoubleSpinBox->setValue( config.value( "Max", 5.0 ).toDouble() );
95  stepDoubleSpinBox->setValue( config.value( "Step", 1.0 ).toDouble() );
96 
97  minimumSpinBox->setValue( config.value( "Min", 0 ).toInt() );
98  maximumSpinBox->setValue( config.value( "Max", 5 ).toInt() );
99  stepSpinBox->setValue( config.value( "Step", 1 ).toInt() );
100 
101  rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( "Style", "SpinBox" ) ) );
102 
103  suffixLineEdit->setText( config.value( "Suffix" ).toString() );
104 
105  allowNullCheckBox->setChecked( config.value( "AllowNull", true ).toBool() );
106 }
107 
109 {
110  QString style = rangeWidget->itemData( index ).toString();
111  allowNullCheckBox->setEnabled( style == "SpinBox" );
112 }
QgsVectorLayer * layer()
Returns the layer for which this configuration widget applies.
int field()
Returns the field for which this configuration widget applies.
static unsigned index
void rangeWidgetChanged(int index)
void setupUi(QWidget *widget)
This class should be subclassed for every configurable editor widget type.
QVariant maximumValue(int index)
Returns maximum value for an attribute column or invalid variant in case of error.
QgsFields fields() const
Returns the list of fields of this layer.
QStyle * style() const
QVariant minimumValue(int index)
Returns minimum value for an attribute column or invalid variant in case of error.
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
virtual void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:331
QgsRangeConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
iterator insert(const Key &key, const T &value)
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
virtual QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:77
const T value(const Key &key) const