QGIS API Documentation  2.14.0-Essen
qgsvaluemapsearchwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdefaultsearchwidgettwrapper.cpp
3  --------------------------------------
4  Date : 31.5.2015
5  Copyright : (C) 2015 Karolina Alexiou (carolinux)
6  Email : carolinegr at gmail dot com
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 #include "qgstexteditconfigdlg.h"
18 
19 #include "qgsfield.h"
20 #include "qgsfieldvalidator.h"
21 
22 #include <QSettings>
23 #include <QSizePolicy>
24 
26  : QgsSearchWidgetWrapper( vl, fieldIdx, parent ),
27  mComboBox( nullptr )
28 {
29 }
30 
32 {
33  return new QComboBox( parent );
34 }
35 
36 void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
37 {
38  if ( mComboBox )
39  {
40  if ( idx == 0 )
41  {
43  }
44  else
45  {
46  setExpression( mComboBox->itemData( idx ).toString() );
47  }
49  }
50 }
51 
53 {
54  return true;
55 }
56 
58 {
59  return mExpression;
60 }
61 
63 {
64  return true;
65 }
66 
68 {
69  mComboBox = qobject_cast<QComboBox*>( editor );
70 
71  if ( mComboBox )
72  {
73  const QgsEditorWidgetConfig cfg = config();
75  mComboBox->addItem( tr( "Please select" ), "" );
76 
77  while ( it != cfg.constEnd() )
78  {
79  mComboBox->addItem( it.key(), it.value() );
80  ++it;
81  }
82  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( comboBoxIndexChanged( int ) ) );
83  }
84 }
85 
87 {
88  QString fieldName = layer()->fields().at( mFieldIdx ).name();
89  QString str;
90 
91  str = QString( "%1 = '%2'" )
92  .arg( QgsExpression::quotedColumnRef( fieldName ),
93  exp.replace( '\'', "''" ) );
94 
95  mExpression = str;
96 }
97 
Manages an editor widget Widget and wrapper share the same parent.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
bool valid() const override
Return true if the widget has been properly initialized.
QString expression() override
Will be used to access the widget&#39;s value.
QgsFields fields() const
Returns the list of fields of this layer.
const_iterator constBegin() const
void clearExpression()
clears the expression to search for all features
QString tr(const char *sourceText, const char *disambiguation, int n)
void addItem(const QString &text, const QVariant &userData)
QString name() const
Gets the name of the field.
Definition: qgsfield.cpp:84
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
const_iterator constEnd() const
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:385
QVariant itemData(int index, int role) const
QString & replace(int position, int n, QChar after)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsValueMapSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
QgsEditorWidgetConfig config() const
Returns the whole config.
typedef ConstIterator
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.