QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsuniquevaluewidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsuniquevaluewidgetwrapper.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 
17 
18 #include "qgsvectorlayer.h"
19 #include "qgsfilterlineedit.h"
20 
21 #include <QCompleter>
22 #include <QSettings>
23 
24 QgsUniqueValuesWidgetWrapper::QgsUniqueValuesWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 {
27 }
28 
30 {
31  QVariant value;
32 
33  if ( mComboBox )
34  value = mComboBox->itemData( mComboBox->currentIndex() );
35 
36  if ( mLineEdit )
37  {
38  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
39  value = QVariant( field().type() );
40  else
41  value = mLineEdit->text();
42  }
43 
44  return value;
45 }
46 
47 QWidget* QgsUniqueValuesWidgetWrapper::createWidget( QWidget* parent )
48 {
49  if ( config( "Editable" ).toBool() )
50  return new QgsFilterLineEdit( parent );
51  else
52  return new QComboBox( parent );
53 }
54 
56 {
57  mComboBox = qobject_cast<QComboBox*>( editor );
58  mLineEdit = qobject_cast<QLineEdit*>( editor );
59 
60  QStringList sValues;
61 
62  QList<QVariant> values;
63 
64  layer()->uniqueValues( fieldIdx(), values );
65 
66  Q_FOREACH ( QVariant v, values )
67  {
68  if ( mComboBox )
69  {
70  mComboBox->addItem( v.toString(), v );
71  }
72 
73  if ( mLineEdit )
74  {
75  sValues << v.toString();
76  }
77  }
78 
79  if ( mLineEdit )
80  {
81  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
82  if ( fle && !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
83  {
84  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
85  }
86 
87  QCompleter* c = new QCompleter( sValues );
88  c->setCompletionMode( QCompleter::PopupCompletion );
89  mLineEdit->setCompleter( c );
90 
91  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
92  }
93 
94  if ( mComboBox )
95  {
96  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
97  }
98 }
99 
100 void QgsUniqueValuesWidgetWrapper::setValue( const QVariant& value )
101 {
102  if ( mComboBox )
103  {
104  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
105  }
106 
107  if ( mLineEdit )
108  {
109  if ( value.isNull() )
110  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
111  else
112  mLineEdit->setText( value.toString() );
113  }
114 }
void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
QVariant value()
Will be used to access the widget's value.
void valueChanged()
Will call the value() method to determine the emitted value.
void uniqueValues(int index, QList< QVariant > &uniqueValues, int limit=-1)
Returns unique values for column.
Manages an editor widget Widget and wrapper share the same parent.
QgsVectorLayer * layer()
Access the QgsVectorLayer, you are working on.
QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
QgsField field()
Access the field.
const QgsEditorWidgetConfig config()
Returns the whole config.
Lineedit with builtin clear button.
void setNullValue(QString nullValue)
QgsUniqueValuesWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
int fieldIdx()
Access the field index.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:64