QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
20 #include <QCompleter>
21 
22 QgsUniqueValuesWidgetWrapper::QgsUniqueValuesWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
23  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
24 {
25 }
26 
28 {
29  QVariant value;
30 
31  if ( mComboBox )
32  value = mComboBox->itemData( mComboBox->currentIndex() );
33 
34  if ( mLineEdit )
35  value = mLineEdit->text();
36 
37  return value;
38 }
39 
40 QWidget* QgsUniqueValuesWidgetWrapper::createWidget( QWidget* parent )
41 {
42  if ( config( "Editable" ).toBool() )
43  return new QLineEdit( parent );
44  else
45  return new QComboBox( parent );
46 }
47 
49 {
50  mComboBox = qobject_cast<QComboBox*>( editor );
51  mLineEdit = qobject_cast<QLineEdit*>( editor );
52 
53  QStringList sValues;
54 
55  QList<QVariant> values;
56 
57  layer()->uniqueValues( fieldIdx(), values );
58 
59  Q_FOREACH ( QVariant v, values )
60  {
61  if ( mComboBox )
62  {
63  mComboBox->addItem( v.toString(), v );
64  }
65 
66  sValues << v.toString();
67  }
68 
69  if ( mLineEdit )
70  {
71  QCompleter* c = new QCompleter( sValues );
72  c->setCompletionMode( QCompleter::PopupCompletion );
73  mLineEdit->setCompleter( c );
74 
75  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
76  }
77 
78  if ( mComboBox )
79  {
80  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
81  }
82 }
83 
84 void QgsUniqueValuesWidgetWrapper::setValue( const QVariant& value )
85 {
86  if ( mComboBox )
87  {
88  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
89  }
90 
91  if ( mLineEdit )
92  {
93  mLineEdit->setText( value.toString() );
94  }
95 }
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 setValue(const QVariant &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.
const QgsEditorWidgetConfig config()
Returns the whole config.
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.