This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Settings: Implementation for search in settings #2699
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4af3b1b
Draft implementation for search in settings
BigRoy 12e7f75
renamed search.py to search_dialog.py
iLLiCiTiT 61b07c3
extended search dialog and handle reset and recreation of dialog model
iLLiCiTiT 7cfbfee
changed prefix 'restart' to 'reset'
iLLiCiTiT 6921e3e
Merge pull request #5 from pypeclub/settings_search_tweaks
BigRoy b1cc2b2
Merge branch 'develop' into settings_search
iLLiCiTiT 24e22c1
Refactor breadcrumbs_widget -> breadcrumbs_bar
BigRoy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
from functools import partial | ||
import re | ||
|
||
from Qt import QtCore, QtWidgets | ||
from openpype.tools.utils.models import TreeModel, Item | ||
from openpype.tools.utils.lib import schedule | ||
|
||
|
||
def get_entity_children(entity): | ||
|
||
if hasattr(entity, "values"): | ||
return entity.values() | ||
return [] | ||
|
||
|
||
class RecursiveSortFilterProxyModel(QtCore.QSortFilterProxyModel): | ||
"""Filters recursively to regex in all columns""" | ||
|
||
def __init__(self): | ||
super(RecursiveSortFilterProxyModel, self).__init__() | ||
|
||
# Note: Recursive filtering was introduced in Qt 5.10. | ||
self.setRecursiveFilteringEnabled(True) | ||
|
||
def filterAcceptsRow(self, row, parent): | ||
|
||
if not parent.isValid(): | ||
return False | ||
|
||
regex = self.filterRegExp() | ||
if not regex.isEmpty() and regex.isValid(): | ||
pattern = regex.pattern() | ||
source_model = self.sourceModel() | ||
|
||
# Check current index itself in all columns | ||
for column in range(source_model.columnCount(parent)): | ||
source_index = source_model.index(row, column, parent) | ||
if not source_index.isValid(): | ||
continue | ||
|
||
key = source_model.data(source_index, self.filterRole()) | ||
if not key: | ||
continue | ||
|
||
if re.search(pattern, key, re.IGNORECASE): | ||
return True | ||
|
||
return False | ||
|
||
return super(RecursiveSortFilterProxyModel, | ||
self).filterAcceptsRow(row, parent) | ||
|
||
|
||
class SearchEntitiesDialog(QtWidgets.QDialog): | ||
|
||
path_clicked = QtCore.Signal(str) | ||
|
||
def __init__(self, entity, parent=None): | ||
super(SearchEntitiesDialog, self).__init__(parent=parent) | ||
|
||
layout = QtWidgets.QVBoxLayout(self) | ||
|
||
filter_edit = QtWidgets.QLineEdit() | ||
filter_edit.setPlaceholderText("Search..") | ||
|
||
model = EntityTreeModel() | ||
proxy = RecursiveSortFilterProxyModel() | ||
proxy.setSourceModel(model) | ||
proxy.setDynamicSortFilter(True) | ||
view = QtWidgets.QTreeView() | ||
view.setModel(proxy) | ||
|
||
layout.addWidget(filter_edit) | ||
layout.addWidget(view) | ||
|
||
filter_edit.textChanged.connect(self._on_filter_changed) | ||
view.selectionModel().selectionChanged.connect(self.on_select) | ||
|
||
view.setAllColumnsShowFocus(True) | ||
view.setSortingEnabled(True) | ||
view.sortByColumn(1, QtCore.Qt.AscendingOrder) | ||
|
||
self._model = model | ||
self._proxy = proxy | ||
self._view = view | ||
|
||
# Refresh to the passed entity | ||
model.set_root(entity) | ||
|
||
view.resizeColumnToContents(0) | ||
|
||
def _on_filter_changed(self, txt): | ||
# Provide slight delay to filtering so user can type quickly | ||
schedule(partial(self.on_filter_changed, txt), 250, channel="search") | ||
|
||
def on_filter_changed(self, txt): | ||
self._proxy.setFilterRegExp(txt) | ||
|
||
# WARNING This expanding and resizing is relatively slow. | ||
self._view.expandAll() | ||
self._view.resizeColumnToContents(0) | ||
|
||
def on_select(self): | ||
current = self._view.currentIndex() | ||
item = current.data(EntityTreeModel.ItemRole) | ||
self.path_clicked.emit(item["path"]) | ||
|
||
|
||
class EntityTreeModel(TreeModel): | ||
|
||
Columns = ["trail", "label", "key", "path"] | ||
|
||
def add_entity(self, entity, parent=None): | ||
|
||
item = Item() | ||
|
||
# Label and key can sometimes be emtpy so we use the trail from path | ||
# in the most left column since it's never empty | ||
item["trail"] = entity.path.rsplit("/", 1)[-1] | ||
item["label"] = entity.label | ||
item["key"] = entity.key | ||
item["path"] = entity.path | ||
|
||
parent.add_child(item) | ||
|
||
for child in get_entity_children(entity): | ||
self.add_entity(child, parent=item) | ||
|
||
def set_root(self, root_entity): | ||
self.clear() | ||
self.beginResetModel() | ||
|
||
# We don't want to see the root entity so we directly add its children | ||
for child in get_entity_children(root_entity): | ||
self.add_entity(child, parent=self._root_item) | ||
self.endResetModel() | ||
|
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
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.
RecursiveSortFilterProxyModel
can be imported fromopenpype.tools.utils
.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.
That's correct. But - this is a changed one. This filters to ALL columns and actually uses Qt 5.10+ feature to enable recursive filtering that Qt allows. Since I assumed the Settings tool would never be used in older Qt (e.g. in hosts with older Python, etc.)
In essence this class name should just be updated to match what it does instead - but I couldn't think of a good name. Maybe
FilterAllColumnsProxyModel
?