Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Add lineedit with autocompleter for ValueRelation edit widget
  • Loading branch information
m-kuhn committed Mar 29, 2015
1 parent 08d4d3b commit 1cce091
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp
Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationwidgetfactory.cpp
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
52 changes: 48 additions & 4 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
{
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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 );
}
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}
}
}
}


Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h
Expand Up @@ -20,6 +20,7 @@

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

class QgsValueRelationWidgetFactory;

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

ValueRelationCache mCache;
QgsVectorLayer* mLayer;
Expand Down
16 changes: 13 additions & 3 deletions src/ui/editorwidgets/qgsvaluerelationconfigdlgbase.ui
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 1cce091

Please sign in to comment.