Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Expression from Settings #49

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions linking_relation_editor/gui/linking_child_manager_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
QgsHighlight,
QgsIdentifyMenu,
QgsMessageBar,
QgsAttributeForm
)
from qgis.PyQt.QtCore import QModelIndex, Qt, QTimer
from qgis.PyQt.QtWidgets import QAction, QDialog, QMessageBox
from qgis.PyQt.uic import loadUiType
from qgis.utils import iface
from unittest.mock import MagicMock

from linking_relation_editor.core.model.attribute_form_delegate import (
AttributeFormDelegate,
Expand All @@ -52,6 +54,7 @@ def __init__(
nmRelation: QgsRelation,
editorContext: QgsAttributeEditorContext,
oneToOne: bool,
filterExpression: str,
linkingChildManagerDialogConfig: dict,
parent=None,
):
Expand All @@ -64,6 +67,7 @@ def __init__(
self._nmRelation = nmRelation
self._editorContext = editorContext
self._oneToOne = oneToOne
self._filterExpression = filterExpression
self._linkingChildManagerDialogConfig = linkingChildManagerDialogConfig

self._mapToolSelect = None
Expand Down Expand Up @@ -177,14 +181,22 @@ def __init__(

self._feature_filter_widget = FeatureFilterWidget(self)
self.mFooterHBoxLayout.insertWidget(0, self._feature_filter_widget)
if iface: # TODO how to use iface in tests?
self._feature_filter_widget.init(
self._layer,
self._editorContext,
self._featuresModelFilterLeft,
iface.messageBar(),
QgsMessageBar.defaultMessageTimeout(),
)

# used for untittest
if not iface:
i = MagicMock()
else:
i = iface
self._feature_filter_widget.init(
self._layer,
self._editorContext,
self._featuresModelFilterLeft,
i.messageBar(),
QgsMessageBar.defaultMessageTimeout(),
)
if self._filterExpression:
self._feature_filter_widget.setFilterExpression(self._filterExpression,QgsAttributeForm.ReplaceFilter, True)
else:
self._feature_filter_widget.filterShowAll()

# Signal slots
Expand Down
3 changes: 3 additions & 0 deletions linking_relation_editor/gui/linking_relation_editor_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def config(self):
return {
"buttons": metaEnumFromValue(QgsRelationEditorWidget.Button.AllButtons).valueToKeys(self.visibleButtons()),
"show_first_feature": self.mShowFirstFeature,
"filter_exression": self.mFilterExpression,
CONFIG_ONE_TO_ONE: self.mOneToOne,
CONFIG_LINKING_CHILD_MANAGER_DIALOG: self.mLinkingChildManagerDialogConfig,
}
Expand All @@ -195,6 +196,7 @@ def setConfig(self, config):
config.get("buttons", metaEnumButtons.valueToKeys(QgsRelationEditorWidget.Button.AllButtons))
)
self.mShowFirstFeature = config.get("show_first_feature", True)
self.mFilterExpression = config.get("filter_expression")
self.mOneToOne = config.get(CONFIG_ONE_TO_ONE)
self.mLinkingChildManagerDialogConfig = config.get(CONFIG_LINKING_CHILD_MANAGER_DIALOG, {})
self.updateButtons()
Expand Down Expand Up @@ -589,6 +591,7 @@ def _execLinkFeatureDialog(self):
self.nmRelation(),
self.editorContext(),
self.mOneToOne,
self.mFilterExpression,
self.mLinkingChildManagerDialogConfig,
self,
)
Expand Down
184 changes: 162 additions & 22 deletions linking_relation_editor/images/LinkingRelationEditor-icon.svg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the icon change? it's still SVG but with a default size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not relevant for this implementation. it was just supertiny with a huge margin and now it's bigger.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions linking_relation_editor/tests/test_linking_child_manager_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def test_Instantiate(self):
QgsRelation(),
QgsAttributeEditorContext(),
False,
None,
{},
None,
)
Expand All @@ -160,6 +161,7 @@ def test_InstantiateRelation1N(self):
QgsRelation(),
QgsAttributeEditorContext(),
False,
None,
{},
None,
)
Expand All @@ -182,6 +184,7 @@ def test_InstantiateRelationNM(self):
self.mRelationNM,
QgsAttributeEditorContext(),
False,
None,
{},
None,
)
Expand All @@ -206,6 +209,7 @@ def test_quickFilter(self):
QgsRelation(),
QgsAttributeEditorContext(),
False,
None,
{},
None,
)
Expand Down Expand Up @@ -306,3 +310,88 @@ def test_quickFilter(self):
dialog._featuresModelFilterLeft.data(dialog._featuresModelFilterLeft.index(1, 0), Qt.DisplayRole),
"Layer1-1: Martina formerly known as Prisca",
)

def test_passed_filter_expression(self):
# get a parent with no childs
parentFeature = QgsFeature()
for feature in self.mLayer2.getFeatures():
if feature.attribute("pk") == 12:
parentFeature = feature
break

self.assertTrue(parentFeature.isValid())

dialog = LinkingChildManagerDialog(
self.mLayer1,
self.mLayer2,
parentFeature,
self.mRelation,
QgsRelation(),
QgsAttributeEditorContext(),
False,
"name LIKE '%formerly known as Prince%'",
{},
None,
)

self.assertEqual(dialog.mLayerNameLabel.text(), self.mLayer1.name())

# one entry
# "Layer1-0: The Artist formerly known as Prince"
self.assertEqual(dialog._featuresModelFilterLeft.rowCount(), 1)
self.assertEqual(
dialog._featuresModelFilterLeft.data(dialog._featuresModelFilterLeft.index(0, 0), Qt.DisplayRole),
"Layer1-0: The Artist formerly known as Prince",
)

dialog = LinkingChildManagerDialog(
self.mLayer1,
self.mLayer2,
parentFeature,
self.mRelation,
QgsRelation(),
QgsAttributeEditorContext(),
False,
"name LIKE '%formerly%'",
{},
None,
)

self.assertEqual(dialog.mLayerNameLabel.text(), self.mLayer1.name())

# all entries with formerly
# "Layer1-0: The Artist formerly known as Prince"
# "Layer1-1: Martina formerly known as Prisca"
self.assertEqual(dialog._featuresModelFilterLeft.rowCount(), 2)
self.assertEqual(
dialog._featuresModelFilterLeft.data(dialog._featuresModelFilterLeft.index(0, 0), Qt.DisplayRole),
"Layer1-0: The Artist formerly known as Prince",
)
self.assertEqual(
dialog._featuresModelFilterLeft.data(dialog._featuresModelFilterLeft.index(1, 0), Qt.DisplayRole),
"Layer1-1: Martina formerly known as Prisca",
)

dialog = LinkingChildManagerDialog(
self.mLayer1,
self.mLayer2,
parentFeature,
self.mRelation,
QgsRelation(),
QgsAttributeEditorContext(),
False,
"name LIKE '%Prisca%'",
{},
None,
)

self.assertEqual(dialog.mLayerNameLabel.text(), self.mLayer1.name())

# one entry
# "Layer1-0: The Artist formerly known as Prince"
self.assertEqual(dialog._featuresModelFilterLeft.rowCount(), 1)
self.assertEqual(
dialog._featuresModelFilterLeft.data(dialog._featuresModelFilterLeft.index(0, 0), Qt.DisplayRole),
"Layer1-1: Martina formerly known as Prisca",
)

Loading