Skip to content

Commit

Permalink
QgsValueRelationSearchW inherits QgsSearchW
Browse files Browse the repository at this point in the history
and not QgsDefaultSearchW
also handles properly the (no selection) case
  • Loading branch information
3nids committed Nov 13, 2015
1 parent f31c0e7 commit 7f556fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void QgsDefaultSearchWidgetWrapper::setExpression( QString exp )
str = QString( "%1 %2 '%3'" )
.arg( QgsExpression::quotedColumnRef( fieldName ),
numeric ? "=" : mCaseString,
numeric
? exp.replace( '\'', "''" )
numeric ?
exp.replace( '\'', "''" )
:
'%' + exp.replace( '\'', "''" ) + '%' ); // escape quotes
}
Expand Down
41 changes: 38 additions & 3 deletions src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#include "qgsvectorlayer.h"
#include "qgsfilterlineedit.h"

#include <QSettings>
#include <QStringListModel>
#include <QCompleter>

QgsValueRelationSearchWidgetWrapper::QgsValueRelationSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
: QgsDefaultSearchWidgetWrapper( vl, fieldIdx, parent )
: QgsSearchWidgetWrapper( vl, fieldIdx, parent )
, mComboBox( 0 )
, mListWidget( 0 )
, mLineEdit( 0 )
Expand All @@ -42,7 +43,12 @@ bool QgsValueRelationSearchWidgetWrapper::applyDirectly()
return true;
}

QVariant QgsValueRelationSearchWidgetWrapper::value()
QString QgsValueRelationSearchWidgetWrapper::expression()
{
return mExpression;
}

QVariant QgsValueRelationSearchWidgetWrapper::value() const
{
QVariant v;

Expand Down Expand Up @@ -83,12 +89,40 @@ QVariant QgsValueRelationSearchWidgetWrapper::value()
return v;
}

bool QgsValueRelationSearchWidgetWrapper::valid()
{
return true;
}

void QgsValueRelationSearchWidgetWrapper::valueChanged()
{
setExpression( value().toString() );
QVariant vl = value();
QSettings settings;
setExpression( vl.isNull() ? settings.value( "qgis/nullValue", "NULL" ).toString() : vl.toString() );
emit expressionChanged( mExpression );
}

void QgsValueRelationSearchWidgetWrapper::setExpression( QString exp )
{
QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
QString fieldName = layer()->fields().at( mFieldIdx ).name();

QString str;
if ( exp == nullValue )
{
str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
}
else
{
str = QString( "%1 = '%3'" )
.arg( QgsExpression::quotedColumnRef( fieldName ),
exp.replace( '\'', "''" )
);
}
mExpression = str;
}

QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
{
if ( config( "AllowMulti" ).toBool() )
Expand All @@ -99,6 +133,7 @@ QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
{
return new QgsFilterLineEdit( parent );
}
else
{
return new QComboBox( parent );
}
Expand Down
30 changes: 10 additions & 20 deletions src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
#define QGSVALUERELATIONSEARCHWIDGETWRAPPER_H

#include "qgsdefaultsearchwidgetwrapper.h"
#include "qgssearchwidgetwrapper.h"
#include "qgsvaluerelationwidgetwrapper.h"

#include <QComboBox>
Expand All @@ -26,26 +26,12 @@
class QgsValueRelationWidgetFactory;

/**
* Wraps a value relation widget. This widget will offer a combobox with values from another layer
* Wraps a value relation search widget. This widget will offer a combobox with values from another layer
* referenced by a foreign key (a constraint may be set but is not required on data level).
* This is useful for having value lists on a separate layer containing codes and their
* translation to human readable names.
*
* Options:
*
* <ul>
* <li><b>Layer</b> <i>The id of the referenced layer.</i></li>
* <li><b>Key</b> <i>The key field on the referenced layer (code).</i></li>
* <li><b>Value</b> <i>The value field on the referenced layer (human readable name).</i></li>
* <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>
* <li><b>AllowNull</b> <i>Will offer NULL as a possible value.</i></li>
* <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>
* <li><b>OrderByValue</b> <i>Will order by value instead of key.</i></li>
* </ul>
*
* It will be used as a search widget and produces expression to look for in the layer.
*/

class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWidgetWrapper
class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsSearchWidgetWrapper
{
Q_OBJECT

Expand All @@ -56,8 +42,9 @@ class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWi
public:
explicit QgsValueRelationSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent = 0 );
bool applyDirectly() override;
QVariant value();

QString expression() override;
bool valid() override;
QVariant value() const;

protected:
QWidget* createWidget( QWidget* parent ) override;
Expand All @@ -66,6 +53,9 @@ class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsDefaultSearchWi
public slots:
void valueChanged();

protected slots:
void setExpression( QString exp ) override;

private:
QComboBox* mComboBox;
QListWidget* mListWidget;
Expand Down

0 comments on commit 7f556fd

Please sign in to comment.