QGIS API Documentation  2.5.0-Master
 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 "qgsrangewidgetwrapper.h"
17 
18 #include "qgsvectorlayer.h"
19 
20 QgsRangeWidgetWrapper::QgsRangeWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
21  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
22  , mIntSpinBox( 0 )
23  , mDoubleSpinBox( 0 )
24  , mSlider( 0 )
25  , mDial( 0 )
26 {
27 }
28 
29 QWidget* QgsRangeWidgetWrapper::createWidget( QWidget* parent )
30 {
31  QWidget* editor = 0;
32 
33  if ( config( "Style" ).toString() == "Dial" )
34  {
35  editor = new QgsDial( parent );
36  }
37  else if ( config( "Style" ).toString() == "Slider" )
38  {
39  editor = new QgsSlider( Qt::Horizontal, parent );
40  }
41  else
42  {
43  switch ( layer()->pendingFields()[fieldIdx()].type() )
44  {
45  case QVariant::Double:
46  {
47  QDoubleSpinBox* spin = new QDoubleSpinBox( parent );
48  int precision = layer()->pendingFields()[fieldIdx()].precision();
49  if ( precision > 0 )
50  {
51  spin->setDecimals( layer()->pendingFields()[fieldIdx()].precision() );
52  }
53  editor = spin;
54  break;
55  }
56  case QVariant::Int:
57  case QVariant::LongLong:
58  default:
59  editor = new QSpinBox( parent );
60  break;
61 
62 
63  }
64  }
65 
66  return editor;
67 }
68 
69 void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
70 {
71  mDoubleSpinBox = qobject_cast<QDoubleSpinBox*>( editor );
72  mIntSpinBox = qobject_cast<QSpinBox*>( editor );
73  mDial = qobject_cast<QDial*>( editor );
74  mSlider = qobject_cast<QSlider*>( editor );
75 
76  if ( mDoubleSpinBox )
77  {
78  mDoubleSpinBox->setMinimum( config( "Min" ).toDouble() );
79  mDoubleSpinBox->setMaximum( config( "Max" ).toDouble() );
80  mDoubleSpinBox->setSingleStep( config( "Step" ).toDouble() );
81  connect( mDoubleSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( valueChanged( double ) ) );
82  }
83 
84  if ( mIntSpinBox )
85  {
86  mIntSpinBox->setMinimum( config( "Min" ).toInt() );
87  mIntSpinBox->setMaximum( config( "Max" ).toInt() );
88  mIntSpinBox->setSingleStep( config( "Step" ).toInt() );
89  connect( mIntSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
90  }
91 
92  if ( mDial )
93  {
94  mDial->setMinimum( config( "Min" ).toInt() );
95  mDial->setMaximum( config( "Max" ).toInt() );
96  mDial->setSingleStep( config( "Step" ).toInt() );
97  connect( mDial, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
98  }
99 
100  if ( mSlider )
101  {
102  mSlider->setMinimum( config( "Min" ).toInt() );
103  mSlider->setMaximum( config( "Max" ).toInt() );
104  mSlider->setSingleStep( config( "Step" ).toInt() );
105  connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
106  }
107 
108 }
109 
111 {
112  QVariant value;
113 
114  if ( mDoubleSpinBox )
115  {
116  value = mDoubleSpinBox->value();
117  }
118  else if ( mIntSpinBox )
119  {
120  value = mIntSpinBox->value();
121  }
122  else if ( mDial )
123  {
124  value = mDial->value();
125  }
126  else if ( mSlider )
127  {
128  value = mSlider->value();
129  }
130 
131  return value;
132 }
133 
134 void QgsRangeWidgetWrapper::setValue( const QVariant& value )
135 {
136  if ( mDoubleSpinBox )
137  {
138  mDoubleSpinBox->setValue( value.toDouble() );
139  }
140 
141  if ( mIntSpinBox )
142  {
143  mIntSpinBox->setValue( value.toInt() );
144  }
145  if ( mDial )
146  {
147  mDial->setValue( value.toInt() );
148  }
149  if ( mSlider )
150  {
151  mSlider->setValue( value.toInt() );
152  }
153 }
154 
QgsRangeWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent=0)
void valueChanged()
Will call the value() method to determine the emitted value.
virtual QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
Manages an editor widget Widget and wrapper share the same parent.
virtual void setValue(const QVariant &value)
virtual QVariant value()
Will be used to access the widget's value.
QgsVectorLayer * layer()
Access the QgsVectorLayer, you are working on.
const QgsEditorWidgetConfig config()
Returns the whole config.
QDoubleSpinBox * mDoubleSpinBox
virtual void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
Definition: qgsdial.h:6
int fieldIdx()
Access the field index.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
Represents a vector layer which manages a vector based data sets.