QGIS API Documentation  2.12.0-Lyon
qgsvaluerelationsearchwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvaluerelationsearchwidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis 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 "qgsfield.h"
19 #include "qgsmaplayerregistry.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsfilterlineedit.h"
23 
24 #include <QStringListModel>
25 #include <QCompleter>
26 
28  : QgsDefaultSearchWidgetWrapper( vl, fieldIdx, parent )
29  , mComboBox( 0 )
30  , mListWidget( 0 )
31  , mLineEdit( 0 )
32  , mLayer( 0 )
33 {
34 }
35 
37 {
38  if ( mLineEdit )
39  {
40  return false;
41  }
42  return true;
43 }
44 
46 {
47  QVariant v;
48 
49  if ( mComboBox )
50  {
51  int cbxIdx = mComboBox->currentIndex();
52  if ( cbxIdx > -1 )
53  {
54  v = mComboBox->itemData( mComboBox->currentIndex() );
55  }
56  }
57 
58  if ( mListWidget )
59  {
60  QStringList selection;
61  for ( int i = 0; i < mListWidget->count(); ++i )
62  {
63  QListWidgetItem* item = mListWidget->item( i );
64  if ( item->checkState() == Qt::Checked )
65  selection << item->data( Qt::UserRole ).toString();
66  }
67 
68  v = selection.join( "," ).prepend( "{" ).append( "}" );
69  }
70 
71  if ( mLineEdit )
72  {
73  Q_FOREACH ( const ValueRelationItem& i , mCache )
74  {
75  if ( i.second == mLineEdit->text() )
76  {
77  v = i.first;
78  break;
79  }
80  }
81  }
82 
83  return v;
84 }
85 
87 {
88  setExpression( value().toString() );
89 }
90 
92 {
93  if ( config( "AllowMulti" ).toBool() )
94  {
95  return new QgsFilterLineEdit( parent );
96  }
97  else if ( config( "UseCompleter" ).toBool() )
98  {
99  return new QgsFilterLineEdit( parent );
100  }
101  {
102  return new QComboBox( parent );
103  }
104 }
105 
107 {
109 
110  mComboBox = qobject_cast<QComboBox*>( editor );
111  mListWidget = qobject_cast<QListWidget*>( editor );
112  mLineEdit = qobject_cast<QLineEdit*>( editor );
113 
114  if ( mComboBox )
115  {
116  mComboBox->addItem( tr( "Please select" ), QVariant( layer()->fields().at( mFieldIdx ).type() ) );
117  if ( config( "AllowNull" ).toBool() )
118  {
119  mComboBox->addItem( tr( "(no selection)" ), QVariant( layer()->fields().at( mFieldIdx ).type() ) );
120  }
121 
122  Q_FOREACH ( const ValueRelationItem& element, mCache )
123  {
124  mComboBox->addItem( element.second, element.first );
125  }
126 
127  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
128  }
129  else if ( mListWidget )
130  {
131  Q_FOREACH ( const ValueRelationItem& element, mCache )
132  {
133  QListWidgetItem *item;
134  item = new QListWidgetItem( element.second );
135  item->setData( Qt::UserRole, element.first );
136 
137  mListWidget->addItem( item );
138  }
139  connect( mListWidget, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( valueChanged() ) );
140  }
141  else if ( mLineEdit )
142  {
143  QStringList values;
144  Q_FOREACH ( const ValueRelationItem& i, mCache )
145  {
146  values << i.second;
147  }
148 
149  QStringListModel* m = new QStringListModel( values, mLineEdit );
150  QCompleter* completer = new QCompleter( m, mLineEdit );
151  completer->setCaseSensitivity( Qt::CaseInsensitive );
152  mLineEdit->setCompleter( completer );
153  connect( mLineEdit, SIGNAL( textChanged( QListWidgetItem* ) ), this, SLOT( valueChanged() ) );
154  }
155 }
156 
157 
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
QString & append(QChar ch)
Qt::CheckState checkState() const
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QString & prepend(QChar ch)
QgsVectorLayer * layer()
Access the QgsVectorLayer, you are working on.
QString join(const QString &separator) const
QString tr(const char *sourceText, const char *disambiguation, int n)
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
void addItem(const QString &text, const QVariant &userData)
const QgsEditorWidgetConfig config()
Returns the whole config.
virtual QVariant data(int role) const
Lineedit with builtin clear button.
QListWidgetItem * item(int row) const
QVariant itemData(int index, int role) const
virtual void setData(int role, const QVariant &value)
static ValueRelationCache createCache(const QgsEditorWidgetConfig &config)
QgsValueRelationSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=0)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString toString() const