|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from qgis.gui import QgsFilterLineEdit |
| 4 | +from qgis.PyQt.QtWidgets import QSizePolicy, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget |
| 5 | + |
| 6 | + |
| 7 | +class TreeWithSearchWidget(QWidget): |
| 8 | + """A widget combining a QTreeWidget and QgsFilterLineEdit.""" |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + super().__init__() |
| 12 | + self.search = QgsFilterLineEdit(self) |
| 13 | + self.search.setShowClearButton(True) |
| 14 | + self.search.setShowSearchIcon(True) |
| 15 | + self.search.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) |
| 16 | + self.search.valueChanged.connect(self.filter_tree_items) |
| 17 | + |
| 18 | + self.tree = QTreeWidget(self) |
| 19 | + self.tree.setHeaderHidden(True) |
| 20 | + self.tree.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) |
| 21 | + |
| 22 | + layout = QVBoxLayout() |
| 23 | + layout.addWidget(self.search) |
| 24 | + layout.addWidget(self.tree) |
| 25 | + self.setLayout(layout) |
| 26 | + |
| 27 | + self.setContentsMargins(0, 0, 0, 0) |
| 28 | + layout.setContentsMargins(0, 0, 0, 0) |
| 29 | + |
| 30 | + def filter_tree_items(self): |
| 31 | + search_text = self.search.value().lower() |
| 32 | + |
| 33 | + # Iterate over all group (top-level) items in the tree |
| 34 | + for i in range(self.tree.topLevelItemCount()): |
| 35 | + group_item = self.tree.topLevelItem(i) |
| 36 | + group_item.setHidden(True) # Initially hide the top-level item |
| 37 | + |
| 38 | + matches_group = search_text in group_item.text(0).lower() |
| 39 | + if matches_group: |
| 40 | + # If group matches, show it and all its children |
| 41 | + self.show_all_children(group_item) |
| 42 | + group_item.setHidden(False) |
| 43 | + else: |
| 44 | + # Otherwise, recursively filter its children |
| 45 | + matches_child = self.filter_children(group_item, search_text) |
| 46 | + group_item.setHidden(not matches_child) |
| 47 | + |
| 48 | + def filter_children(self, parent_item: QTreeWidgetItem, search_text: str) -> bool: |
| 49 | + """Recursively filter children and return True if any child matches.""" |
| 50 | + has_match = False |
| 51 | + for i in range(parent_item.childCount()): |
| 52 | + child = parent_item.child(i) |
| 53 | + matches = search_text in child.text(0).lower() |
| 54 | + child.setHidden(not matches) |
| 55 | + |
| 56 | + if child.childCount() > 0: |
| 57 | + matches |= self.filter_children(child, search_text) |
| 58 | + |
| 59 | + if matches: |
| 60 | + has_match = True |
| 61 | + |
| 62 | + return has_match |
| 63 | + |
| 64 | + def show_all_children(self, parent_item: QTreeWidgetItem): |
| 65 | + """Show the parent item and all its children recursively.""" |
| 66 | + parent_item.setHidden(False) |
| 67 | + for i in range(parent_item.childCount()): |
| 68 | + child = parent_item.child(i) |
| 69 | + child.setHidden(False) |
| 70 | + if child.childCount() > 0: |
| 71 | + self.show_all_children(child) |
0 commit comments