QgsValueRelationSearchW inherits QgsSearchW

and not QgsDefaultSearchW
also handles properly the (no selection) case
This commit is contained in:
Denis Rouzaud 2015-11-12 15:14:54 +01:00
parent f31c0e7bf0
commit 7f556fd99d
3 changed files with 50 additions and 25 deletions

View File

@ -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
}

View File

@ -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 )
@ -42,7 +43,12 @@ bool QgsValueRelationSearchWidgetWrapper::applyDirectly()
return true;
}
QVariant QgsValueRelationSearchWidgetWrapper::value()
QString QgsValueRelationSearchWidgetWrapper::expression()
{
return mExpression;
}
QVariant QgsValueRelationSearchWidgetWrapper::value() const
{
QVariant v;
@ -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() )
@ -99,6 +133,7 @@ QWidget* QgsValueRelationSearchWidgetWrapper::createWidget( QWidget* parent )
{
return new QgsFilterLineEdit( parent );
}
else
{
return new QComboBox( parent );
}

View File

@ -16,7 +16,7 @@
#ifndef QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
#define QGSVALUERELATIONSEARCHWIDGETWRAPPER_H
#include "qgsdefaultsearchwidgetwrapper.h"
#include "qgssearchwidgetwrapper.h"
#include "qgsvaluerelationwidgetwrapper.h"
#include <QComboBox>
@ -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
@ -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;
@ -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;