Skip to content
Permalink
Browse files

Add "filter fields" configuraiton option to relation reference widget

  • Loading branch information
m-kuhn committed Mar 16, 2015
1 parent 5b5f16e commit c5a4ea56b1735873c392150ecb061a50672c96b8
@@ -22,8 +22,10 @@
#include "qgsvectorlayer.h"
#include "qgsexpressionbuilderdialog.h"


QgsRelationReferenceConfigDlg::QgsRelationReferenceConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
, mReferencedLayer( 0 )
{
setupUi( this );
connect( mComboRelation, SIGNAL( currentIndexChanged( int ) ), this, SLOT( relationChanged( int ) ) );
@@ -58,6 +60,7 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
if ( config.contains( "Relation" ) )
{
mComboRelation->setCurrentIndex( mComboRelation->findData( config[ "Relation" ].toString() ) );
relationChanged( mComboRelation->currentIndex() );
}

if ( config.contains( "MapIdentification" ) )
@@ -69,19 +72,47 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
{
mCbxReadOnly->setChecked( config[ "ReadOnly"].toBool() );
}

if ( config.contains( "FilterFields" ) )
{
mFilterGroupBox->setChecked( true );
Q_FOREACH ( const QString& fld, config["FilterFields"].toStringList() )
{
addFilterField( fld );
}
}
}

void QgsRelationReferenceConfigDlg::relationChanged( int idx )
{
QString relName = mComboRelation->itemData( idx ).toString();
QgsRelation rel = QgsProject::instance()->relationManager()->relation( relName );

QgsVectorLayer* referencedLayer = rel.referencedLayer();
mExpressionWidget->setLayer( referencedLayer ); // set even if 0
if ( referencedLayer )
mReferencedLayer = rel.referencedLayer();
mExpressionWidget->setLayer( mReferencedLayer ); // set even if 0
if ( mReferencedLayer )
{
mExpressionWidget->setField( mReferencedLayer->displayExpression() );
mCbxMapIdentification->setEnabled( mReferencedLayer->hasGeometryType() );
}

loadFields();
}

void QgsRelationReferenceConfigDlg::on_mAddFilterButton_clicked()
{
Q_FOREACH ( QListWidgetItem* item, mAvailableFieldsList->selectedItems() )
{
mExpressionWidget->setField( referencedLayer->displayExpression() );
mCbxMapIdentification->setEnabled( referencedLayer->hasGeometryType() );
addFilterField( item );
}
}

void QgsRelationReferenceConfigDlg::on_mRemoveFilterButton_clicked()
{
Q_FOREACH ( QListWidgetItem* item , mFilterFieldsList->selectedItems() )
{
mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
mAvailableFieldsList->addItem( item );
}
}

@@ -95,14 +126,68 @@ QgsEditorWidgetConfig QgsRelationReferenceConfigDlg::config()
myConfig.insert( "ReadOnly", mCbxReadOnly->isChecked() );
myConfig.insert( "Relation", mComboRelation->itemData( mComboRelation->currentIndex() ) );

QString relName = mComboRelation->itemData( mComboRelation->currentIndex() ).toString();
QgsRelation relation = QgsProject::instance()->relationManager()->relation( relName );
if ( mFilterGroupBox->isChecked() )
{
QStringList filterFields;
for ( int i = 0; i < mFilterFieldsList->count(); i++ )
{
filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
}
myConfig.insert( "FilterFields", filterFields );
}

if ( relation.isValid() )
if ( mReferencedLayer )
{
relation.referencedLayer()->setDisplayExpression( mExpressionWidget->currentField() );
mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
}

return myConfig;
}

void QgsRelationReferenceConfigDlg::loadFields()
{
mAvailableFieldsList->clear();
mFilterFieldsList->clear();

if ( mReferencedLayer )
{
QgsVectorLayer* l = mReferencedLayer;
const QgsFields& flds = l->pendingFields();
for ( int i = 0; i < flds.count(); i++ )
{
mAvailableFieldsList->addItem( l->attributeAlias( i ).isEmpty() ? flds.at( i ).name() : l->attributeAlias( i ) );
mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
}
}
}

void QgsRelationReferenceConfigDlg::addFilterField( const QString& field )
{
for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
{
if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
{
addFilterField( mAvailableFieldsList->item( i ) );
break;
}
}
}

void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem* item )
{
mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
mFilterFieldsList->addItem( item );
}

int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem* item )
{
QListWidget* lw = item->listWidget();

for ( int i = 0; i < lw->count(); i++ )
{
if ( lw->item( i ) == item )
return i;
}

return -1;
}
@@ -16,6 +16,8 @@
#ifndef QGSRELATIONREFERENCECONFIGDLGBASE_H
#define QGSRELATIONREFERENCECONFIGDLGBASE_H

#include <QListWidget>

#include "ui_qgsrelationreferenceconfigdlgbase.h"
#include "qgseditorconfigwidget.h"

@@ -28,8 +30,18 @@ class GUI_EXPORT QgsRelationReferenceConfigDlg : public QgsEditorConfigWidget, p
virtual QgsEditorWidgetConfig config() override;
virtual void setConfig( const QgsEditorWidgetConfig& config ) override;

private:
void loadFields();
void addFilterField( const QString& field );
void addFilterField( QListWidgetItem* item );
int indexFromListWidgetItem( QListWidgetItem* item );

QgsVectorLayer* mReferencedLayer;

private slots:
void relationChanged( int idx );
void on_mAddFilterButton_clicked();
void on_mRemoveFilterButton_clicked();
};

#endif // QGSRELATIONREFERENCECONFIGDLGBASE_H
@@ -48,6 +48,18 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
cfg.insert( "MapIdentification", configElement.attribute( "MapIdentification" ) == "1" );
cfg.insert( "ReadOnly", configElement.attribute( "ReadOnly" ) == "1" );

QDomNode filterNode = configElement.elementsByTagName( "FilterFields" ).at( 0 );
if ( !filterNode.isNull() )
{
QStringList filterFields;
QDomNodeList fieldNodes = filterNode.toElement().elementsByTagName( "field" );
for ( int i = 0; i < fieldNodes.size(); i++ )
{
QDomElement fieldElement = fieldNodes.at( i ).toElement();
filterFields << fieldElement.attribute( "name" );
}
cfg.insert( "FilterFields", filterFields );
}
return cfg;
}

@@ -63,4 +75,17 @@ void QgsRelationReferenceFactory::writeConfig( const QgsEditorWidgetConfig& conf
configElement.setAttribute( "Relation", config["Relation"].toString() );
configElement.setAttribute( "MapIdentification", config["MapIdentification"].toBool() );
configElement.setAttribute( "ReadOnly", config["ReadOnly"].toBool() );

if ( config.contains( "FilterFields" ) )
{
QDomElement filterFields = doc.createElement( "FilterFields" );

Q_FOREACH ( const QString& field, config["FilterFields"].toStringList() )
{
QDomElement fieldElem = doc.createElement( "field" );
fieldElem.setAttribute( "name", field );
filterFields.appendChild( fieldElem );
}
configElement.appendChild( filterFields );
}
}
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>470</width>
<height>240</height>
<height>481</height>
</rect>
</property>
<property name="windowTitle">
@@ -72,6 +72,75 @@
</property>
</widget>
</item>
<item row="11" column="0" colspan="2">
<widget class="QgsCollapsibleGroupBox" name="mFilterGroupBox">
<property name="title">
<string>Filters</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="mAddFilterButton">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSignPlus.png</normaloff>:/images/themes/default/mActionSignPlus.png</iconset>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QToolButton" name="mRemoveFilterButton">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSignMinus.png</normaloff>:/images/themes/default/mActionSignMinus.png</iconset>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2" rowspan="4">
<widget class="QListWidget" name="mFilterFieldsList"/>
</item>
<item row="1" column="0" rowspan="4">
<widget class="QListWidget" name="mAvailableFieldsList"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
@@ -81,7 +150,15 @@
<header location="global">qgsfieldexpressionwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit c5a4ea5

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