Skip to content
Permalink
Browse files

[FEATURE] Add lineedit with autocompleter for ValueRelation edit widget

  • Loading branch information
m-kuhn committed Mar 29, 2015
1 parent 08d4d3b commit 1cce091e177bcf413a63e372e44aef312836abb9
@@ -39,6 +39,7 @@ QgsEditorWidgetConfig QgsValueRelationConfigDlg::config()
cfg.insert( "AllowNull", mAllowNull->isChecked() );
cfg.insert( "OrderByValue", mOrderByValue->isChecked() );
cfg.insert( "FilterExpression", mFilterExpression->toPlainText() );
cfg.insert( "UseCompleter", mUseCompleter->isChecked() );

return cfg;
}
@@ -53,6 +54,7 @@ void QgsValueRelationConfigDlg::setConfig( const QgsEditorWidgetConfig& config )
mAllowNull->setChecked( config.value( "AllowNull" ).toBool() );
mOrderByValue->setChecked( config.value( "OrderByValue" ).toBool() );
mFilterExpression->setPlainText( config.value( "FilterExpression" ).toString() );
mUseCompleter->setChecked( config.value( "UseCompleter" ).toBool() );
}

void QgsValueRelationConfigDlg::editExpression()
@@ -53,6 +53,7 @@ QgsEditorWidgetConfig QgsValueRelationWidgetFactory::readConfig( const QDomEleme
cfg.insert( "OrderByValue", configElement.attribute( "OrderByValue" ) );
cfg.insert( "AllowMulti", configElement.attribute( "AllowMulti" ) );
cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) );
cfg.insert( "UseCompleter", configElement.attribute( "UseCompleter" ) );

return cfg;
}
@@ -70,6 +71,7 @@ void QgsValueRelationWidgetFactory::writeConfig( const QgsEditorWidgetConfig& co
configElement.setAttribute( "OrderByValue", config.value( "OrderByValue" ).toBool() );
configElement.setAttribute( "AllowMulti", config.value( "AllowMulti" ).toBool() );
configElement.setAttribute( "AllowNull", config.value( "AllowNull" ).toBool() );
configElement.setAttribute( "UseCompleter", config.value( "UseCompleter" ).toBool() );
}

QString QgsValueRelationWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
@@ -19,6 +19,10 @@
#include "qgsmaplayerregistry.h"
#include "qgsvaluerelationwidgetfactory.h"
#include "qgsvectorlayer.h"
#include "qgsfilterlineedit.h"

#include <QStringListModel>
#include <QCompleter>

bool orderByKeyLessThan( const QgsValueRelationWidgetWrapper::ValueRelationItem& p1
, const QgsValueRelationWidgetWrapper::ValueRelationItem& p2 )
@@ -47,9 +51,10 @@ bool orderByValueLessThan( const QgsValueRelationWidgetWrapper::ValueRelationIte

QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mComboBox( NULL )
, mListWidget( NULL )
, mLayer( NULL )
, mComboBox( 0 )
, mListWidget( 0 )
, mLineEdit( 0 )
, mLayer( 0 )
{
}

@@ -80,6 +85,18 @@ QVariant QgsValueRelationWidgetWrapper::value()
v = selection.join( "," ).prepend( "{" ).append( "}" );
}

if ( mLineEdit )
{
Q_FOREACH( const ValueRelationItem& i , mCache )
{
if ( i.second == mLineEdit->text() )
{
v = i.first;
break;
}
}
}

return v;
}

@@ -89,7 +106,10 @@ QWidget* QgsValueRelationWidgetWrapper::createWidget( QWidget* parent )
{
return new QListWidget( parent );
}
else
else if ( config( "UseCompleter" ).toBool() )
{
return new QgsFilterLineEdit( parent );
}
{
return new QComboBox( parent );
}
@@ -101,6 +121,7 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget* editor )

mComboBox = qobject_cast<QComboBox*>( editor );
mListWidget = qobject_cast<QListWidget*>( editor );
mLineEdit= qobject_cast<QLineEdit*>( editor );

if ( mComboBox )
{
@@ -128,6 +149,18 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget* editor )
}
connect( mListWidget, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( valueChanged() ) );
}
else if ( mLineEdit )
{
QStringList values;
Q_FOREACH( const ValueRelationItem& i, mCache )
{
values << i.second;
}

QStringListModel* m = new QStringListModel( values, mLineEdit );
QCompleter* completer = new QCompleter( m, mLineEdit );
mLineEdit->setCompleter( completer );
}
}

void QgsValueRelationWidgetWrapper::setValue( const QVariant& value )
@@ -153,6 +186,17 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant& value )
{
mComboBox->setCurrentIndex( mComboBox->findData( value ) );
}
else if ( mLineEdit )
{
Q_FOREACH( ValueRelationItem i, mCache )
{
if ( i.first == value )
{
mLineEdit->setText( i.second );
break;
}
}
}
}


@@ -20,6 +20,7 @@

#include <QComboBox>
#include <QListWidget>
#include <QLineEdit>

class QgsValueRelationWidgetFactory;

@@ -70,6 +71,7 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
private:
QComboBox* mComboBox;
QListWidget* mListWidget;
QLineEdit* mLineEdit;

ValueRelationCache mCache;
QgsVectorLayer* mLayer;
@@ -93,7 +93,7 @@
</property>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_19">
<property name="text">
<string>Filter expression</string>
@@ -103,7 +103,7 @@
</property>
</widget>
</item>
<item row="7" column="1">
<item row="8" column="1">
<widget class="QToolButton" name="mEditExpression">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
@@ -113,9 +113,19 @@
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<widget class="QTextEdit" name="mFilterExpression"/>
</item>
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="mUseCompleter">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Use Completer</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>

0 comments on commit 1cce091

Please sign in to comment.
You can’t perform that action at this time.