QGIS API Documentation  2.8.2-Wien
 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 
23 QgsRangeWidgetWrapper::QgsRangeWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
24  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
25  , mIntSpinBox( 0 )
26  , mDoubleSpinBox( 0 )
27  , mSlider( 0 )
28  , mDial( 0 )
29 {
30 }
31 
32 QWidget* QgsRangeWidgetWrapper::createWidget( QWidget* parent )
33 {
34  QWidget* editor = 0;
35 
36  if ( config( "Style" ).toString() == "Dial" )
37  {
38  editor = new QgsDial( parent );
39  }
40  else if ( config( "Style" ).toString() == "Slider" )
41  {
42  editor = new QgsSlider( Qt::Horizontal, parent );
43  }
44  else
45  {
46  QgsDebugMsg( QString( "%1" ).arg(( int )layer()->pendingFields()[fieldIdx()].type() ) );
47  switch ( layer()->pendingFields()[fieldIdx()].type() )
48  {
49  case QVariant::Double:
50  {
51  editor = new QgsDoubleSpinBox( parent );
52  break;
53  }
54  case QVariant::Int:
55  case QVariant::LongLong:
56  default:
57  editor = new QgsSpinBox( parent );
58  break;
59  }
60  }
61 
62  return editor;
63 }
64 
65 void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
66 {
67  mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor );
68  mIntSpinBox = qobject_cast<QSpinBox*>( editor );
69  mDial = qobject_cast<QDial*>( editor );
70  mSlider = qobject_cast<QSlider*>( editor );
71 
72  bool allowNull = config( "AllowNull" ).toBool();
73 
74  if ( mDoubleSpinBox )
75  {
76  // set the precision if field is integer
77  int precision = layer()->pendingFields()[fieldIdx()].precision();
78  if ( precision > 0 )
79  {
80  mDoubleSpinBox->setDecimals( layer()->pendingFields()[fieldIdx()].precision() );
81  }
82 
83  double min = config( "Min" ).toDouble();
84  double step = config( "Step" ).toDouble();
85  QgsDoubleSpinBox* qgsWidget = dynamic_cast<QgsDoubleSpinBox*>( mDoubleSpinBox );
86  if ( qgsWidget )
87  qgsWidget->setShowClearButton( allowNull );
88  if ( allowNull )
89  {
90  if ( precision > 0 )
91  {
92  min -= 10 ^ -precision;
93  }
94  else
95  {
96  min -= step;
97  }
98  mDoubleSpinBox->setValue( min );
99  mDoubleSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
100  }
101  mDoubleSpinBox->setMinimum( min );
102  mDoubleSpinBox->setMaximum( config( "Max" ).toDouble() );
103  mDoubleSpinBox->setSingleStep( step );
104  if ( config( "Suffix" ).isValid() )
105  {
106  mDoubleSpinBox->setSuffix( config( "Suffix" ).toString() );
107  }
108  connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) );
109  }
110 
111  if ( mIntSpinBox )
112  {
113  int min = config( "Min" ).toInt();
114  int step = config( "Step" ).toInt();
115  QgsSpinBox* qgsWidget = dynamic_cast<QgsSpinBox*>( mIntSpinBox );
116  if ( qgsWidget )
117  qgsWidget->setShowClearButton( allowNull );
118  if ( allowNull )
119  {
120  min -= step;
121  mIntSpinBox->setValue( min );
122  mIntSpinBox->setSpecialValueText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
123  }
124  mIntSpinBox->setMinimum( min );
125  mIntSpinBox->setMaximum( config( "Max" ).toInt() );
126  mIntSpinBox->setSingleStep( step );
127  if ( config( "Suffix" ).isValid() )
128  {
129  mIntSpinBox->setSuffix( config( "Suffix" ).toString() );
130  }
131  connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
132  }
133 
134  if ( mDial )
135  {
136  mDial->setMinimum( config( "Min" ).toInt() );
137  mDial->setMaximum( config( "Max" ).toInt() );
138  mDial->setSingleStep( config( "Step" ).toInt() );
139  connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
140  }
141 
142  if ( mSlider )
143  {
144  mSlider->setMinimum( config( "Min" ).toInt() );
145  mSlider->setMaximum( config( "Max" ).toInt() );
146  mSlider->setSingleStep( config( "Step" ).toInt() );
147  connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
148  }
149 }
150 
152 {
153  QVariant value;
154 
155  if ( mDoubleSpinBox )
156  {
157  value = mDoubleSpinBox->value();
158  if ( value == mDoubleSpinBox->minimum() && config( "AllowNull" ).toBool() )
159  {
160  value = QVariant( field().type() );
161  }
162  }
163  else if ( mIntSpinBox )
164  {
165  value = mIntSpinBox->value();
166  if ( value == mIntSpinBox->minimum() && config( "AllowNull" ).toBool() )
167  {
168  value = QVariant( field().type() );
169  }
170  }
171  else if ( mDial )
172  {
173  value = mDial->value();
174  }
175  else if ( mSlider )
176  {
177  value = mSlider->value();
178  }
179 
180  return value;
181 }
182 
183 void QgsRangeWidgetWrapper::setValue( const QVariant& value )
184 {
185  if ( mDoubleSpinBox )
186  {
187  if ( value.isNull() && config( "AllowNull" ).toBool() )
188  {
189  mDoubleSpinBox->setValue( mDoubleSpinBox->minimum() );
190  }
191  else
192  {
193  mDoubleSpinBox->setValue( value.toDouble() );
194  }
195  }
196 
197  if ( mIntSpinBox )
198  {
199  if ( value.isNull() && config( "AllowNull" ).toBool() )
200  {
201  mIntSpinBox->setValue( mIntSpinBox->minimum() );
202  }
203  else
204  {
205  mIntSpinBox->setValue( value.toInt() );
206  }
207  }
208  if ( mDial )
209  {
210  mDial->setValue( value.toInt() );
211  }
212  if ( mSlider )
213  {
214  mSlider->setValue( value.toInt() );
215  }
216 }
217