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 dbef766 commit ebb5f0d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
Expand Up @@ -68,10 +68,10 @@ void QgsDefaultSearchWidgetWrapper::setExpression( QString exp )
str = QString( "%1 %2 '%3'" ) str = QString( "%1 %2 '%3'" )
.arg( QgsExpression::quotedColumnRef( fieldName ), .arg( QgsExpression::quotedColumnRef( fieldName ),
numeric ? "=" : mCaseString, numeric ? "=" : mCaseString,
numeric numeric ?
? exp.replace( "'", "''" ) exp.replace( '\'', "''" )
: :
"%" + exp.replace( "'", "''" ) + "%" ); // escape quotes '%' + exp.replace( '\'', "''" ) + '%' ); // escape quotes
} }
mExpression = str; mExpression = str;
} }
Expand Down
41 changes: 38 additions & 3 deletions src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp
Expand Up @@ -21,11 +21,12 @@
#include "qgsvectorlayer.h" #include "qgsvectorlayer.h"
#include "qgsfilterlineedit.h" #include "qgsfilterlineedit.h"


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


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


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

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


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


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

void QgsValueRelationSearchWidgetWrapper::valueChanged() 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 ); 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 ) QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
{ {
if ( config( "AllowMulti" ).toBool() ) if ( config( "AllowMulti" ).toBool() )
Expand All @@ -99,6 +133,7 @@ QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
{ {
return new QgsFilterLineEdit( parent ); return new QgsFilterLineEdit( parent );
} }
else
{ {
return new QComboBox( parent ); return new QComboBox( parent );
} }
Expand Down
30 changes: 10 additions & 20 deletions src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.h
Expand Up @@ -16,7 +16,7 @@
#ifndef QGSVALUERELATIONSEARCHWIDGETWRAPPER_H #ifndef QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
#define QGSVALUERELATIONSEARCHWIDGETWRAPPER_H #define QGSVALUERELATIONSEARCHWIDGETWRAPPER_H


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


#include <QComboBox> #include <QComboBox>
Expand All @@ -26,26 +26,12 @@
class QgsValueRelationWidgetFactory; 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). * 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 * It will be used as a search widget and produces expression to look for in the layer.
* 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>
*
*/ */


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


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

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


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


protected slots:
void setExpression( QString exp ) override;

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

0 comments on commit ebb5f0d

Please sign in to comment.