-
Notifications
You must be signed in to change notification settings - Fork 0
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
nmaarnio
merged 1 commit into
main
from
107-suodatus-kaavamääräyksille-ja-kaavamääräysryhmille
Jan 16, 2025
The head ref may contain hidden characters: "107-suodatus-kaavam\u00E4\u00E4r\u00E4yksille-ja-kaavam\u00E4\u00E4r\u00E4ysryhmille"
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
arho_feature_template/gui/components/tree_with_search_widget.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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