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

107 suodatus kaavamääräyksille ja kaavamääräysryhmille #108

Merged
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
4 changes: 1 addition & 3 deletions arho_feature_template/core/plan_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ def _plan_geom_digitized(self, feature: QgsFeature):
if not plan_layer:
return

attribute_form = PlanAttributeForm(
[*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
)
attribute_form = PlanAttributeForm(self.regulation_group_libraries)
if attribute_form.exec_():
plan_attributes = attribute_form.get_plan_attributes()
plan_attributes.geom = feature.geometry()
Expand Down
88 changes: 88 additions & 0 deletions arho_feature_template/gui/components/tree_with_search_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import annotations

from typing import Any

from qgis.gui import QgsFilterLineEdit
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QSizePolicy, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget


class TreeWithSearchWidget(QWidget):
Copy link
Contributor

Choose a reason for hiding this comment

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

Tää on tosiaan aika simppeli widget eikä niin väliä, mutta oliko joku syy miksi tätä ei tehty Designerilla ui-tiedostoksi?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eipä juuri. Taisin vaan ajatella, että kivempi laittaa yhteen tiedostoon kun mahtui hyvin / oli sen verran vähän alustuskoodia

"""A widget combining a QTreeWidget and QgsFilterLineEdit."""

def __init__(self):
super().__init__()
self.search = QgsFilterLineEdit(self)
self.search.setShowClearButton(True)
self.search.setShowSearchIcon(True)
self.search.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
self.search.valueChanged.connect(self.filter_tree_items)

self.tree = QTreeWidget(self)
self.tree.setHeaderHidden(True)
self.tree.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)

layout = QVBoxLayout()
layout.addWidget(self.search)
layout.addWidget(self.tree)
self.setLayout(layout)

self.setContentsMargins(0, 0, 0, 0)
layout.setContentsMargins(0, 0, 0, 0)

def add_item_to_tree(
self, text: str | None, model: Any | None = None, parent: QTreeWidgetItem | None = None
) -> QTreeWidgetItem:
item = QTreeWidgetItem(parent)
if text:
item.setText(0, text)
item.setToolTip(0, text) # Set text as tooltip in case the tree width is not enough to show item text
if model:
item.setData(0, Qt.UserRole, model)
if not parent:
self.tree.addTopLevelItem(item)

return item

def filter_tree_items(self):
search_text = self.search.value().lower()

# Iterate over all group (top-level) items in the tree
for i in range(self.tree.topLevelItemCount()):
group_item = self.tree.topLevelItem(i)
group_item.setHidden(True) # Initially hide the top-level item

matches_group = search_text in group_item.text(0).lower()
if matches_group:
# If group matches, show it and all its children
self.show_all_children(group_item)
group_item.setHidden(False)
else:
# Otherwise, recursively filter its children
matches_child = self.filter_children(group_item, search_text)
group_item.setHidden(not matches_child)

def filter_children(self, parent_item: QTreeWidgetItem, search_text: str) -> bool:
"""Recursively filter children and return True if any child matches."""
has_match = False
for i in range(parent_item.childCount()):
child = parent_item.child(i)
matches = search_text in child.text(0).lower()
child.setHidden(not matches)

if child.childCount() > 0:
matches |= self.filter_children(child, search_text)

if matches:
has_match = True

return has_match

def show_all_children(self, parent_item: QTreeWidgetItem):
"""Show the parent item and all its children recursively."""
parent_item.setHidden(False)
for i in range(parent_item.childCount()):
child = parent_item.child(i)
child.setHidden(False)
if child.childCount() > 0:
self.show_all_children(child)
28 changes: 12 additions & 16 deletions arho_feature_template/gui/dialogs/new_plan_regulation_group_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QTextBrowser, QTreeWidget, QTreeWidgetItem
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QTextBrowser, QTreeWidgetItem, QVBoxLayout

from arho_feature_template.core.models import Regulation, RegulationConfig, RegulationGroup, RegulationLibrary
from arho_feature_template.gui.components.plan_regulation_widget import RegulationWidget
from arho_feature_template.gui.components.tree_with_search_widget import TreeWithSearchWidget
from arho_feature_template.project.layers.code_layers import PlanRegulationGroupTypeLayer

if TYPE_CHECKING:
Expand All @@ -35,39 +36,34 @@ def __init__(self):
self.color_code: QLineEdit
self.type_of_regulation_group: CodeComboBox

self.regulations_view: QTreeWidget
self.regulations_tree_layout: QVBoxLayout
self.regulations_scroll_area_contents: QWidget
self.regulations_layout: QBoxLayout
self.regulation_info: QTextBrowser

self.button_box: QDialogButtonBox

# INIT
self.initialize_regulations()
self.regulations_selection_widget = TreeWithSearchWidget()
self.regulations_tree_layout.insertWidget(1, self.regulations_selection_widget)
self.regulations_selection_widget.tree.itemDoubleClicked.connect(self.add_selected_regulation)
self.regulations_selection_widget.tree.itemClicked.connect(self.update_selected_regulation)
for config in RegulationLibrary.get_regulations():
self._initalize_regulation_from_config(config)

self.type_of_regulation_group.populate_from_code_layer(PlanRegulationGroupTypeLayer)
self.type_of_regulation_group.removeItem(0) # Remove NULL from combobox as underground data is required
self.regulations_view.itemDoubleClicked.connect(self.add_selected_regulation)
self.regulations_view.itemClicked.connect(self.update_selected_regulation)
self.button_box.accepted.connect(self._on_ok_clicked)
self.regulation_widgets: list[RegulationWidget] = []
self.save_as_config = False

def _initalize_regulation_from_config(self, config: RegulationConfig, parent: QTreeWidgetItem | None = None):
tree_item = QTreeWidgetItem(parent)
tree_item.setText(0, config.name)
tree_item.setData(0, Qt.UserRole, config)

if parent is None:
self.regulations_view.addTopLevelItem(tree_item)
item = self.regulations_selection_widget.add_item_to_tree(config.name, config, parent)

# Initialize plan regulations recursively
if config.child_regulations:
for child_config in config.child_regulations:
self._initalize_regulation_from_config(child_config, tree_item)

def initialize_regulations(self):
for config in RegulationLibrary.get_regulations():
self._initalize_regulation_from_config(config)
self._initalize_regulation_from_config(child_config, item)

def update_selected_regulation(self, item: QTreeWidgetItem, column: int):
config: RegulationConfig = item.data(column, Qt.UserRole) # Retrieve the associated config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,54 +111,14 @@
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="regulations_tree_layout">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Kaavamääräykset</string>
</property>
</widget>
</item>
<item>
<widget class="QgsFilterLineEdit" name="mLineEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="placeholderText">
<string>Suodata kaavamääräyksiä</string>
</property>
<property name="showSearchIcon">
<bool>true</bool>
</property>
<property name="qgisRelation" stdset="0">
<string notr="true"/>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="regulations_view">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
Expand Down Expand Up @@ -256,11 +216,6 @@ p, li { white-space: pre-wrap; }
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsFilterLineEdit</class>
<extends>QLineEdit</extends>
<header>qgsfilterlineedit.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
Expand Down
24 changes: 13 additions & 11 deletions arho_feature_template/gui/dialogs/plan_attribute_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
QSizePolicy,
QSpacerItem,
QTextEdit,
QTreeWidget,
QTreeWidgetItem,
)

from arho_feature_template.core.models import Plan, RegulationGroup, RegulationGroupLibrary
from arho_feature_template.gui.components.plan_regulation_group_widget import RegulationGroupWidget
from arho_feature_template.gui.components.tree_with_search_widget import TreeWithSearchWidget
from arho_feature_template.project.layers.code_layers import (
LifeCycleStatusLayer,
OrganisationLayer,
PlanTypeLayer,
)

if TYPE_CHECKING:
from qgis.PyQt.QtWidgets import QComboBox, QLineEdit, QTextEdit, QTreeWidget, QWidget
from qgis.PyQt.QtWidgets import QComboBox, QLineEdit, QTextEdit, QVBoxLayout, QWidget

from arho_feature_template.gui.components.code_combobox import CodeComboBox, HierarchicalCodeComboBox

Expand All @@ -47,7 +47,7 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore

plan_regulation_group_scrollarea_contents: QWidget
plan_regulation_group_libraries_combobox: QComboBox
plan_regulation_groups_tree: QTreeWidget
regulation_groups_tree_layout: QVBoxLayout

button_box: QDialogButtonBox

Expand All @@ -66,9 +66,13 @@ def __init__(self, regulation_group_libraries: list[RegulationGroupLibrary], par
self.lifecycle_status_combo_box.currentIndexChanged.connect(self._check_required_fields)

self.scroll_area_spacer = None
self.regulation_groups_selection_widget = TreeWithSearchWidget()
self.regulation_groups_tree_layout.insertWidget(2, self.regulation_groups_selection_widget)
self.regulation_group_widgets: list[RegulationGroupWidget] = []
[self.init_plan_regulation_group_library(library) for library in regulation_group_libraries]
self.plan_regulation_groups_tree.itemDoubleClicked.connect(self.add_selected_plan_regulation_group)
for library in regulation_group_libraries:
self.init_plan_regulation_group_library(library)

self.regulation_groups_selection_widget.tree.itemDoubleClicked.connect(self.add_selected_plan_regulation_group)

self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)

Expand Down Expand Up @@ -117,13 +121,11 @@ def remove_plan_regulation_group(self, regulation_group_widget: RegulationGroupW
def init_plan_regulation_group_library(self, library: RegulationGroupLibrary):
self.plan_regulation_group_libraries_combobox.addItem(library.name)
for category in library.regulation_group_categories:
category_item = QTreeWidgetItem()
category_item.setText(0, category.name)
self.plan_regulation_groups_tree.addTopLevelItem(category_item)
category_item = self.regulation_groups_selection_widget.add_item_to_tree(category.name)
for group_definition in category.regulation_groups:
regulation_group_item = QTreeWidgetItem(category_item)
regulation_group_item.setText(0, group_definition.name)
regulation_group_item.setData(0, Qt.UserRole, group_definition)
self.regulation_groups_selection_widget.add_item_to_tree(
group_definition.name, group_definition, category_item
)

# ---

Expand Down
67 changes: 2 additions & 65 deletions arho_feature_template/gui/dialogs/plan_attribute_form.ui
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_5">
<layout class="QVBoxLayout" name="regulation_groups_tree_layout">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
Expand All @@ -254,64 +254,6 @@
<item>
<widget class="QComboBox" name="plan_regulation_group_libraries_combobox"/>
</item>
<item>
<widget class="QgsFilterLineEdit" name="mLineEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="placeholderText">
<string>Suodata kaavamääräysryhmiä</string>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
<property name="showSearchIcon">
<bool>true</bool>
</property>
<property name="qgisRelation" stdset="0">
<string notr="true"/>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="plan_regulation_groups_tree">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::NoDragDrop</enum>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string/>
</property>
</column>
</widget>
</item>
</layout>
</item>
<item>
Expand All @@ -333,7 +275,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>541</width>
<width>601</width>
<height>493</height>
</rect>
</property>
Expand Down Expand Up @@ -369,11 +311,6 @@
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFilterLineEdit</class>
<extends>QLineEdit</extends>
<header>qgsfilterlineedit.h</header>
</customwidget>
<customwidget>
<class>CodeComboBox</class>
<extends>QComboBox</extends>
Expand Down
Loading
Loading