QGIS API Documentation  2.8.2-Wien
 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 #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  , mComboBox( NULL )
27  , mLineEdit( NULL )
28 {
29 }
30 
32 {
33  QVariant value;
34 
35  if ( mComboBox )
36  value = mComboBox->itemData( mComboBox->currentIndex() );
37 
38  if ( mLineEdit )
39  {
40  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
41  value = QVariant( field().type() );
42  else
43  value = mLineEdit->text();
44  }
45 
46  return value;
47 }
48 
49 QWidget* QgsUniqueValuesWidgetWrapper::createWidget( QWidget* parent )
50 {
51  if ( config( "Editable" ).toBool() )
52  return new QgsFilterLineEdit( parent );
53  else
54  return new QComboBox( parent );
55 }
56 
58 {
59  mComboBox = qobject_cast<QComboBox*>( editor );
60  mLineEdit = qobject_cast<QLineEdit*>( editor );
61 
62  QStringList sValues;
63 
64  QList<QVariant> values;
65 
66  layer()->uniqueValues( fieldIdx(), values );
67 
68  Q_FOREACH ( QVariant v, values )
69  {
70  if ( mComboBox )
71  {
72  mComboBox->addItem( v.toString(), v );
73  }
74 
75  if ( mLineEdit )
76  {
77  sValues << v.toString();
78  }
79  }
80 
81  if ( mLineEdit )
82  {
83  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
84  if ( fle && !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
85  {
86  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
87  }
88 
89  QCompleter* c = new QCompleter( sValues );
90  c->setCompletionMode( QCompleter::PopupCompletion );
91  mLineEdit->setCompleter( c );
92 
93  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
94  }
95 
96  if ( mComboBox )
97  {
98  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
99  }
100 }
101 
102 void QgsUniqueValuesWidgetWrapper::setValue( const QVariant& value )
103 {
104  if ( mComboBox )
105  {
106  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
107  }
108 
109  if ( mLineEdit )
110  {
111  if ( value.isNull() )
112  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
113  else
114  mLineEdit->setText( value.toString() );
115  }
116 }