Skip to content

Commit

Permalink
Merge pull request #58448 from signedav/releditorfilter
Browse files Browse the repository at this point in the history
Filter in Linking Dialogue of Relation Editor Widget
  • Loading branch information
signedav authored Aug 23, 2024
2 parents cc4a9ae + 5c9890c commit 02c36d9
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ Adds new features with given ``geometry``
Delete a feature with given ``fid``
%End

void linkFeature();
void linkFeature( const QString &filterExpression = QString() );
%Docstring
Links a new feature to the relation

:param filterExpression: to filter the available features in the link dialog since QGIS 3.40
%End

void onLinkFeatureDlgAccepted();
Expand Down
5 changes: 5 additions & 0 deletions python/PyQt6/gui/auto_generated/qgsfeatureselectiondlg.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Gets the selected features
Set the selected features

:param ids: The feature ids to select
%End

void setFilterExpression( const QString &filter, QgsAttributeForm::FilterType type );
%Docstring
Set form filter expression
%End

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ Update the configuration widget to represent the given configuration.
:param config: The configuration which should be represented by this widget
%End

void mEditExpression_clicked();
%Docstring
Opens an expression dialog and sets its value as filter expression for the linking dialog
%End
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ Adds new features with given ``geometry``
Delete a feature with given ``fid``
%End

void linkFeature();
void linkFeature( const QString &filterExpression = QString() );
%Docstring
Links a new feature to the relation

:param filterExpression: to filter the available features in the link dialog since QGIS 3.40
%End

void onLinkFeatureDlgAccepted();
Expand Down
5 changes: 5 additions & 0 deletions python/gui/auto_generated/qgsfeatureselectiondlg.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Gets the selected features
Set the selected features

:param ids: The feature ids to select
%End

void setFilterExpression( const QString &filter, QgsAttributeForm::FilterType type );
%Docstring
Set form filter expression
%End

protected:
Expand Down
4 changes: 4 additions & 0 deletions python/gui/auto_generated/qgsrelationeditorwidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ Update the configuration widget to represent the given configuration.
:param config: The configuration which should be represented by this widget
%End

void mEditExpression_clicked();
%Docstring
Opens an expression dialog and sets its value as filter expression for the linking dialog
%End
};


Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsabstractrelationeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void QgsAbstractRelationEditorWidget::deleteFeatures( const QgsFeatureIds &fids
}
}

void QgsAbstractRelationEditorWidget::linkFeature()
void QgsAbstractRelationEditorWidget::linkFeature( const QString &filterExpression )
{
QgsVectorLayer *layer = nullptr;

Expand All @@ -474,6 +474,7 @@ void QgsAbstractRelationEditorWidget::linkFeature()

const QString displayString = QgsVectorLayerUtils::getFeatureDisplayString( mRelation.referencedLayer(), mFeatureList.first() );
selectionDlg->setWindowTitle( tr( "Link existing child features for parent %1 \"%2\"" ).arg( mRelation.referencedLayer()->name(), displayString ) );
selectionDlg->setFilterExpression( filterExpression, QgsAttributeForm::ReplaceFilter );

connect( selectionDlg, &QDialog::accepted, this, &QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted );
selectionDlg->show();
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsabstractrelationeditorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ class GUI_EXPORT QgsAbstractRelationEditorWidget : public QWidget

/**
* Links a new feature to the relation
* \param filterExpression to filter the available features in the link dialog since QGIS 3.40
*/
void linkFeature();
void linkFeature( const QString &filterExpression = QString() );

/**
* Called when the link feature dialog is confirmed by the user
Expand Down
10 changes: 5 additions & 5 deletions src/gui/qgsfeatureselectiondlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class GUI_EXPORT QgsFeatureSelectionDlg : public QDialog, private Ui::QgsFeature
*/
void setSelectedFeatures( const QgsFeatureIds &ids );

/**
* Set form filter expression
*/
void setFilterExpression( const QString &filter, QgsAttributeForm::FilterType type );

protected:

void keyPressEvent( QKeyEvent *evt ) override;
Expand Down Expand Up @@ -105,11 +110,6 @@ class GUI_EXPORT QgsFeatureSelectionDlg : public QDialog, private Ui::QgsFeature
*/
void mActionExpressionSelect_triggered();

/**
* Set form filter expression
*/
void setFilterExpression( const QString &filter, QgsAttributeForm::FilterType type );

/**
* View mode has changed
*/
Expand Down
53 changes: 51 additions & 2 deletions src/gui/qgsrelationeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "qgsmessagebar.h"
#include "qgsmessagebaritem.h"
#include "qgsactionmenu.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"

#include <QHBoxLayout>
#include <QLabel>
Expand Down Expand Up @@ -93,6 +95,7 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( const QVariantMap &config, QWi
, mButtonsVisibility( qgsFlagKeysToValue( config.value( QStringLiteral( "buttons" ) ).toString(), QgsRelationEditorWidget::Button::AllButtons ) )
, mShowFirstFeature( config.value( QStringLiteral( "show_first_feature" ), true ).toBool() )
, mAllowAddChildFeatureWithNoGeometry( config.value( QStringLiteral( "allow_add_child_feature_with_no_geometry" ), false ).toBool() )
, mFilterExpression( config.value( QStringLiteral( "filter_expression" ) ).toString() )
{
QVBoxLayout *rootLayout = new QVBoxLayout( this );
rootLayout->setContentsMargins( 0, 9, 0, 0 );
Expand Down Expand Up @@ -494,6 +497,11 @@ void QgsRelationEditorWidget::multiEditItemSelectionChanged()
updateButtons();
}

void QgsRelationEditorWidget::linkFeature()
{
QgsAbstractRelationEditorWidget::linkFeature( mFilterExpression );
}

void QgsRelationEditorWidget::toggleEditing( bool state )
{
QgsAbstractRelationEditorWidget::toggleEditing( state );
Expand Down Expand Up @@ -819,7 +827,8 @@ QVariantMap QgsRelationEditorWidget::config() const
{
return QVariantMap( {{"buttons", qgsFlagValueToKeys( visibleButtons() )},
{"show_first_feature", mShowFirstFeature},
{"allow_add_child_feature_with_no_geometry", mAllowAddChildFeatureWithNoGeometry }
{"allow_add_child_feature_with_no_geometry", mAllowAddChildFeatureWithNoGeometry },
{"filter_expression", mFilterExpression }
} );
}

Expand All @@ -828,6 +837,7 @@ void QgsRelationEditorWidget::setConfig( const QVariantMap &config )
mButtonsVisibility = qgsFlagKeysToValue( config.value( QStringLiteral( "buttons" ) ).toString(), QgsRelationEditorWidget::Button::AllButtons );
mShowFirstFeature = config.value( QStringLiteral( "show_first_feature" ), true ).toBool();
mAllowAddChildFeatureWithNoGeometry = config.value( QStringLiteral( "allow_add_child_feature_with_no_geometry" ), false ).toBool();
mFilterExpression = config.value( QStringLiteral( "filter_expression" ) ).toString();
updateButtons();
}

Expand Down Expand Up @@ -942,6 +952,43 @@ QgsRelationEditorConfigWidget::QgsRelationEditorConfigWidget( const QgsRelation
: QgsAbstractRelationEditorConfigWidget( relation, parent )
{
setupUi( this );
connect( mEditExpression, &QAbstractButton::clicked, this, &QgsRelationEditorConfigWidget::mEditExpression_clicked );

// Make filter depending on link button
filterExpressionLabel->setEnabled( mRelationShowLinkCheckBox->isChecked() );
mEditExpression->setEnabled( mRelationShowLinkCheckBox->isChecked() );
mFilterExpression->setEnabled( mRelationShowLinkCheckBox->isChecked() );
connect( mRelationShowLinkCheckBox, &QCheckBox::toggled, filterExpressionLabel, &QLabel::setEnabled );
connect( mRelationShowLinkCheckBox, &QCheckBox::toggled, mEditExpression, &QToolButton::setEnabled );
connect( mRelationShowLinkCheckBox, &QCheckBox::toggled, mFilterExpression, &QTextEdit::setEnabled );

// Make add feature with no geometry depending on add button
mAllowAddChildFeatureWithNoGeometry->setEnabled( mRelationShowAddChildCheckBox->isChecked() );
connect( mRelationShowAddChildCheckBox, &QCheckBox::toggled, mAllowAddChildFeatureWithNoGeometry, &QCheckBox::setEnabled );
}

void QgsRelationEditorConfigWidget::mEditExpression_clicked()
{
QgsVectorLayer *vl = nullptr;

if ( nmRelation().isValid() )
{
vl = nmRelation().referencedLayer();
}
else
{
vl = relation().referencingLayer();
}

// Show expression builder
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vl ) );
QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
dlg.setWindowTitle( tr( "Edit Filter Expression of Target Layer" ) );

if ( dlg.exec() == QDialog::Accepted )
{
mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
}
}

QVariantMap QgsRelationEditorConfigWidget::config()
Expand All @@ -959,7 +1006,8 @@ QVariantMap QgsRelationEditorConfigWidget::config()
{
{"buttons", qgsFlagValueToKeys( buttons )},
{"show_first_feature", mShowFirstFeature->isChecked()},
{"allow_add_child_feature_with_no_geometry", mAllowAddChildFeatureWithNoGeometry->isChecked()}
{"allow_add_child_feature_with_no_geometry", mAllowAddChildFeatureWithNoGeometry->isChecked()},
{"filter_expression", mFilterExpression->toPlainText()}
} );
}

Expand All @@ -976,6 +1024,7 @@ void QgsRelationEditorConfigWidget::setConfig( const QVariantMap &config )
mRelationShowSaveChildEditsCheckBox->setChecked( buttons.testFlag( QgsRelationEditorWidget::Button::SaveChildEdits ) );
mShowFirstFeature->setChecked( config.value( QStringLiteral( "show_first_feature" ), true ).toBool() );
mAllowAddChildFeatureWithNoGeometry->setChecked( config.value( QStringLiteral( "allow_add_child_feature_with_no_geometry" ), false ).toBool() );
mFilterExpression->setPlainText( config.value( QStringLiteral( "filter_expression" ) ).toString() );
}


Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsrelationeditorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
void onDigitizingCompleted( const QgsFeature &feature );
void onDigitizingCanceled( );
void multiEditItemSelectionChanged();
void linkFeature();

private:

Expand Down Expand Up @@ -261,6 +262,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
Buttons mButtonsVisibility = Button::AllButtons;
bool mShowFirstFeature = true;
bool mAllowAddChildFeatureWithNoGeometry = true;
QString mFilterExpression;

QList<QTreeWidgetItem *> mMultiEditPreviousSelectedItems;
QgsFeatureIds mMultiEdit1NJustAddedIds;
Expand Down Expand Up @@ -303,6 +305,10 @@ class GUI_EXPORT QgsRelationEditorConfigWidget : public QgsAbstractRelationEdito
*/
void setConfig( const QVariantMap &config ) override;

/**
* Opens an expression dialog and sets its value as filter expression for the linking dialog
*/
void mEditExpression_clicked();
};


Expand Down
53 changes: 47 additions & 6 deletions src/ui/qgsrelationeditorconfigwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<rect>
<x>0</x>
<y>0</y>
<width>422</width>
<height>272</height>
<width>486</width>
<height>494</height>
</rect>
</property>
<property name="windowTitle">
<string>Attribute Widget Relation Edit Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QCheckBox" name="mShowFirstFeature">
<property name="toolTip">
<string>Unchecking this can lead to faster loading time of attribute forms and avoid unnecessary queries.</string>
Expand All @@ -24,7 +24,7 @@
</property>
</widget>
</item>
<item>
<item row="1" column="0">
<widget class="QGroupBox" name="mButtonsVisibility">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
Expand Down Expand Up @@ -115,11 +115,52 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="filterExpressionLabel">
<property name="text">
<string>Filter expression</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mEditExpression">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mIconExpression.svg</normaloff>:/images/themes/default/mIconExpression.svg</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QTextEdit" name="mFilterExpression"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 02c36d9

Please sign in to comment.