QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrangewidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrangewidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias dot kuhn at gmx 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 <QSettings>
17 
18 #include "qgsrangewidgetwrapper.h"
19 #include "qgsspinbox.h"
20 #include "qgsdoublespinbox.h"
21 #include "qgsvectorlayer.h"
22 
24  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
25  , mIntSpinBox( 0 )
26  , mDoubleSpinBox( 0 )
27  , mSlider( 0 )
28  , mDial( 0 )
29  , mQgsSlider( 0 )
30  , mQgsDial( 0 )
31 {
32 }
33 
35 {
36  QWidget* editor = 0;
37 
38  if ( config( "Style" ).toString() == "Dial" )
39  {
40  editor = new QgsDial( parent );
41  }
42  else if ( config( "Style" ).toString() == "Slider" )
43  {
44  editor = new QgsSlider( Qt::Horizontal, parent );
45  }
46  else
47  {
48  QgsDebugMsg( QString( "%1" ).arg(( int )layer()->pendingFields()[fieldIdx()].type() ) );
49  switch ( layer()->pendingFields()[fieldIdx()].type() )
50  {
51  case QVariant::Double:
52  {
53  editor = new QgsDoubleSpinBox( parent );
54  break;
55  }
56  case QVariant::Int:
57  case QVariant::LongLong:
58  default:
59  editor = new QgsSpinBox( parent );
60  break;
61  }
62  }
63 
64  return editor;
65 }
66 
68 {
69  mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor );
70  mIntSpinBox = qobject_cast<QSpinBox*>( editor );
71  mDial = qobject_cast<QDial*>( editor );
72  mSlider = qobject_cast<QSlider*>( editor );
73  mQgsDial = qobject_cast<QgsDial*>( editor );
74  mQgsSlider = qobject_cast<QgsSlider*>( editor );
75 
76  bool allowNull = config( "AllowNull" ).toBool();
77 
78  if ( mDoubleSpinBox )
79  {
80  // set the precision if field is integer
81  int precision = layer()->pendingFields()[fieldIdx()].precision();
82  if ( precision > 0 )
83  {
84  mDoubleSpinBox->setDecimals( layer()->pendingFields()[fieldIdx()].precision() );
85  }
86 
87  double min = config( "Min" ).toDouble();
88  double step = config( "Step" ).toDouble();
89  QgsDoubleSpinBox* qgsWidget = dynamic_cast<QgsDoubleSpinBox*>( mDoubleSpinBox );
90  if ( qgsWidget )
91  qgsWidget->setShowClearButton( allowNull );
92  if ( allowNull )
93  {
94  if ( precision > 0 )
95  {
96  min -= 10 ^ -precision;
97  }
98  else
99  {
100  min -= step;
101  }
102  mDoubleSpinBox->setValue( min );
103  mDoubleSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
104  }
105  mDoubleSpinBox->setMinimum( min );
106  mDoubleSpinBox->setMaximum( config( "Max" ).toDouble() );
107  mDoubleSpinBox->setSingleStep( step );
108  if ( config( "Suffix" ).isValid() )
109  {
110  mDoubleSpinBox->setSuffix( config( "Suffix" ).toString() );
111  }
112  connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) );
113  }
114 
115  if ( mIntSpinBox )
116  {
117  int min = config( "Min" ).toInt();
118  int step = config( "Step" ).toInt();
119  QgsSpinBox* qgsWidget = dynamic_cast<QgsSpinBox*>( mIntSpinBox );
120  if ( qgsWidget )
121  qgsWidget->setShowClearButton( allowNull );
122  if ( allowNull )
123  {
124  min -= step;
125  mIntSpinBox->setValue( min );
126  mIntSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
127  }
128  mIntSpinBox->setMinimum( min );
129  mIntSpinBox->setMaximum( config( "Max" ).toInt() );
130  mIntSpinBox->setSingleStep( step );
131  if ( config( "Suffix" ).isValid() )
132  {
133  mIntSpinBox->setSuffix( config( "Suffix" ).toString() );
134  }
135  connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
136  }
137 
138  if ( mQgsDial || mQgsSlider )
139  {
140  QVariant min( config( "Min" ) );
141  QVariant max( config( "Max" ) );
142  QVariant step( config( "Step" ) );
143 
144  field().convertCompatible( min );
145  field().convertCompatible( max );
146  field().convertCompatible( step );
147 
148  if ( mQgsSlider )
149  {
150  mQgsSlider->setMinimum( min );
151  mQgsSlider->setMaximum( max );
152  mQgsSlider->setSingleStep( step );
153  }
154 
155  if ( mQgsDial )
156  {
157  mQgsDial->setMinimum( min );
158  mQgsDial->setMaximum( max );
159  mQgsDial->setSingleStep( step );
160  }
161 
162  connect( editor, SIGNAL( valueChanged( QVariant ) ), this, SLOT( valueChanged( QVariant ) ) );
163  }
164  else if ( mDial )
165  {
166  mDial->setMinimum( config( "Min" ).toInt() );
167  mDial->setMaximum( config( "Max" ).toInt() );
168  mDial->setSingleStep( config( "Step" ).toInt() );
169  connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
170  }
171  else if ( mSlider )
172  {
173  mSlider->setMinimum( config( "Min" ).toInt() );
174  mSlider->setMaximum( config( "Max" ).toInt() );
175  mSlider->setSingleStep( config( "Step" ).toInt() );
176  connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
177  }
178 }
179 
181 {
182  if ( v.type() == QVariant::Int )
183  valueChanged( v.toInt() );
184  if ( v.type() == QVariant::Double )
185  valueChanged( v.toDouble() );
186 }
187 
189 {
190  QVariant value;
191 
192  if ( mDoubleSpinBox )
193  {
194  value = mDoubleSpinBox->value();
195  if ( value == mDoubleSpinBox->minimum() && config( "AllowNull" ).toBool() )
196  {
197  value = QVariant( field().type() );
198  }
199  }
200  else if ( mIntSpinBox )
201  {
202  value = mIntSpinBox->value();
203  if ( value == mIntSpinBox->minimum() && config( "AllowNull" ).toBool() )
204  {
205  value = QVariant( field().type() );
206  }
207  }
208  else if ( mQgsDial )
209  {
210  value = mQgsDial->variantValue();
211  }
212  else if ( mQgsSlider )
213  {
214  value = mQgsSlider->variantValue();
215  }
216  else if ( mDial )
217  {
218  value = mDial->value();
219  }
220  else if ( mSlider )
221  {
222  value = mSlider->value();
223  }
224 
225  return value;
226 }
227 
229 {
230  if ( mDoubleSpinBox )
231  {
232  if ( value.isNull() && config( "AllowNull" ).toBool() )
233  {
234  mDoubleSpinBox->setValue( mDoubleSpinBox->minimum() );
235  }
236  else
237  {
238  mDoubleSpinBox->setValue( value.toDouble() );
239  }
240  }
241 
242  if ( mIntSpinBox )
243  {
244  if ( value.isNull() && config( "AllowNull" ).toBool() )
245  {
246  mIntSpinBox->setValue( mIntSpinBox->minimum() );
247  }
248  else
249  {
250  mIntSpinBox->setValue( value.toInt() );
251  }
252  }
253 
254  if ( mQgsDial )
255  {
256  mQgsDial->setValue( value );
257  }
258  else if ( mQgsSlider )
259  {
260  mQgsSlider->setValue( value );
261  }
262  else if ( mDial )
263  {
264  mDial->setValue( value.toInt() );
265  }
266  else if ( mSlider )
267  {
268  mSlider->setValue( value.toInt() );
269  }
270 }
void setValue(const QVariant &value)
Definition: qgsslider.cpp:65
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
virtual QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsRangeWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=0)
virtual void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void valueChanged()
Will call the value() method to determine the emitted value.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
Manages an editor widget Widget and wrapper share the same parent.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
Definition: qgsspinbox.h:27
QgsVectorLayer * layer()
Access the QgsVectorLayer, you are working on.
QgsField field()
Access the field.
void setShowClearButton(const bool showClearButton)
determines if the widget will show a clear button
void setSingleStep(double val)
void setShowClearButton(const bool showClearButton)
determines if the widget will show a clear button
Definition: qgsspinbox.cpp:49
int toInt(bool *ok) const
bool isNull() const
const QgsEditorWidgetConfig config()
Returns the whole config.
QVariant variantValue() const
Definition: qgsdial.cpp:106
void setDecimals(int prec)
QVariant variantValue() const
Definition: qgsslider.cpp:111
void setSuffix(const QString &suffix)
void setValue(double val)
virtual QVariant value() override
Will be used to access the widget's value.
void setValue(const QVariant &value)
Definition: qgsdial.cpp:60
int min(int a, int b)
Definition: util.h:93
virtual void setValue(const QVariant &value) override
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:142
void setSpecialValueText(const QString &txt)
int fieldIdx()
Access the field index.
double toDouble(bool *ok) const
void setMinimum(double min)
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
Type type() const
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.
int max(int a, int b)
Definition: util.h:87
void setMaximum(double max)