Skip to content

Commit ebb5f0d

Browse files
committed
QgsValueRelationSearchW inherits QgsSearchW
and not QgsDefaultSearchW also handles properly the (no selection) case
1 parent dbef766 commit ebb5f0d

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ void QgsDefaultSearchWidgetWrapper::setExpression( QString exp )
6868
str = QString( "%1 %2 '%3'" )
6969
.arg( QgsExpression::quotedColumnRef( fieldName ),
7070
numeric ? "=" : mCaseString,
71-
numeric
72-
? exp.replace( "'", "''" )
71+
numeric ?
72+
exp.replace( '\'', "''" )
7373
:
74-
"%" + exp.replace( "'", "''" ) + "%" ); // escape quotes
74+
'%' + exp.replace( '\'', "''" ) + '%' ); // escape quotes
7575
}
7676
mExpression = str;
7777
}

src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp

+38-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
#include "qgsvectorlayer.h"
2222
#include "qgsfilterlineedit.h"
2323

24+
#include <QSettings>
2425
#include <QStringListModel>
2526
#include <QCompleter>
2627

2728
QgsValueRelationSearchWidgetWrapper::QgsValueRelationSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
28-
: QgsDefaultSearchWidgetWrapper( vl, fieldIdx, parent )
29+
: QgsSearchWidgetWrapper( vl, fieldIdx, parent )
2930
, mComboBox( 0 )
3031
, mListWidget( 0 )
3132
, mLineEdit( 0 )
@@ -42,7 +43,12 @@ bool QgsValueRelationSearchWidgetWrapper::applyDirectly()
4243
return true;
4344
}
4445

45-
QVariant QgsValueRelationSearchWidgetWrapper::value()
46+
QString QgsValueRelationSearchWidgetWrapper::expression()
47+
{
48+
return mExpression;
49+
}
50+
51+
QVariant QgsValueRelationSearchWidgetWrapper::value() const
4652
{
4753
QVariant v;
4854

@@ -83,12 +89,40 @@ QVariant QgsValueRelationSearchWidgetWrapper::value()
8389
return v;
8490
}
8591

92+
bool QgsValueRelationSearchWidgetWrapper::valid()
93+
{
94+
return true;
95+
}
96+
8697
void QgsValueRelationSearchWidgetWrapper::valueChanged()
8798
{
88-
setExpression( value().toString() );
99+
QVariant vl = value();
100+
QSettings settings;
101+
setExpression( vl.isNull() ? settings.value( "qgis/nullValue", "NULL" ).toString() : vl.toString() );
89102
emit expressionChanged( mExpression );
90103
}
91104

105+
void QgsValueRelationSearchWidgetWrapper::setExpression( QString exp )
106+
{
107+
QSettings settings;
108+
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
109+
QString fieldName = layer()->fields().at( mFieldIdx ).name();
110+
111+
QString str;
112+
if ( exp == nullValue )
113+
{
114+
str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
115+
}
116+
else
117+
{
118+
str = QString( "%1 = '%3'" )
119+
.arg( QgsExpression::quotedColumnRef( fieldName ),
120+
exp.replace( '\'', "''" )
121+
);
122+
}
123+
mExpression = str;
124+
}
125+
92126
QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
93127
{
94128
if ( config( "AllowMulti" ).toBool() )
@@ -99,6 +133,7 @@ QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
99133
{
100134
return new QgsFilterLineEdit( parent );
101135
}
136+
else
102137
{
103138
return new QComboBox( parent );
104139
}

src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.h

+10-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
1717
#define QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
1818

19-
#include "qgsdefaultsearchwidgetwrapper.h"
19+
#include "qgssearchwidgetwrapper.h"
2020
#include "qgsvaluerelationwidgetwrapper.h"
2121

2222
#include <QComboBox>
@@ -26,26 +26,12 @@
2626
class QgsValueRelationWidgetFactory;
2727

2828
/**
29-
* Wraps a value relation widget. This widget will offer a combobox with values from another layer
29+
* Wraps a value relation search widget. This widget will offer a combobox with values from another layer
3030
* referenced by a foreign key (a constraint may be set but is not required on data level).
31-
* This is useful for having value lists on a separate layer containing codes and their
32-
* translation to human readable names.
33-
*
34-
* Options:
35-
*
36-
* <ul>
37-
* <li><b>Layer</b> <i>The id of the referenced layer.</i></li>
38-
* <li><b>Key</b> <i>The key field on the referenced layer (code).</i></li>
39-
* <li><b>Value</b> <i>The value field on the referenced layer (human readable name).</i></li>
40-
* <li><b>AllowMulti</b> <i>If set to True, will allow multiple selections. This requires the data type to be a string. This does NOT work with normalized database structures.</i></li>
41-
* <li><b>AllowNull</b> <i>Will offer NULL as a possible value.</i></li>
42-
* <li><b>FilterExpression</b> <i>If not empty, will be used as expression. Only if this evaluates to True, the value will be shown.</i></li>
43-
* <li><b>OrderByValue</b> <i>Will order by value instead of key.</i></li>
44-
* </ul>
45-
*
31+
* It will be used as a search widget and produces expression to look for in the layer.
4632
*/
4733

48-
class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWidgetWrapper
34+
class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsSearchWidgetWrapper
4935
{
5036
Q_OBJECT
5137

@@ -56,8 +42,9 @@ class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWi
5642
public:
5743
explicit QgsValueRelationSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent = 0 );
5844
bool applyDirectly() override;
59-
QVariant value();
60-
45+
QString expression() override;
46+
bool valid() override;
47+
QVariant value() const;
6148

6249
protected:
6350
QWidget* createWidget( QWidget* parent ) override;
@@ -66,6 +53,9 @@ class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWi
6653
public slots:
6754
void valueChanged();
6855

56+
protected slots:
57+
void setExpression( QString exp ) override;
58+
6959
private:
7060
QComboBox* mComboBox;
7161
QListWidget* mListWidget;

0 commit comments

Comments
 (0)