Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single column QTreeview search filter

I have two questions:

  1. I was wondering if this is the proper way to do a search/filter on a single column treeview. I feel like a lot of my copying/pasting could contain unnecessary stuff. Is all the code in the subclass of QSortFilterProxyModel and the code in the search_text_changed method needed? I don't feel like the regex is needed, since I set the filter-proxy to ignore case-sensitivity.

  2. How can I make it so that when a user double-clicks a treeview item a signal emits a string list containing the string of the item clicked and all of its ancestors recursively? For example, if I double-clicked "Birds", it would return ['Birds','Animals']; and if I double-clicked "Animals", it would just return ['Animals'].

enter image description here

import os, sys
from PySide import QtCore, QtGui

tags = {
    "Animals": [
        "Birds",
        "Various"
    ],
    "Brick": [
        "Blocks",
        "Special"
    ],
    "Manmade": [
        "Air Conditioners",
        "Audio Equipment"
    ],
    "Food": [
        "Fruit",
        "Grains and Seeds"
    ]
}

class SearchProxyModel(QtGui.QSortFilterProxyModel):
    def __init__(self, parent=None):
        super(SearchProxyModel, self).__init__(parent)
        self.text = ''

    # Recursive search
    def _accept_index(self, idx):
        if idx.isValid():
            text = idx.data(role=QtCore.Qt.DisplayRole).lower()
            condition = text.find(self.text) >= 0

            if condition:
                return True
            for childnum in range(idx.model().rowCount(parent=idx)):
                if self._accept_index(idx.model().index(childnum, 0, parent=idx)):
                    return True
        return False

    def filterAcceptsRow(self, sourceRow, sourceParent):
        # Only first column in model for search
        idx = self.sourceModel().index(sourceRow, 0, sourceParent)
        return self._accept_index(idx)

    def lessThan(self, left, right):
        leftData = self.sourceModel().data(left)
        rightData = self.sourceModel().data(right)
        return leftData < rightData


class TagsBrowserWidget(QtGui.QWidget):

    clickedTag = QtCore.Signal(list)

    def __init__(self, parent=None):
        super(TagsBrowserWidget, self).__init__(parent)
        self.resize(300,500)

        # controls
        self.ui_search = QtGui.QLineEdit()
        self.ui_search.setPlaceholderText('Search...')

        self.tags_model = SearchProxyModel()
        self.tags_model.setSourceModel(QtGui.QStandardItemModel())
        self.tags_model.setDynamicSortFilter(True)
        self.tags_model.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)

        self.ui_tags = QtGui.QTreeView()
        self.ui_tags.setSortingEnabled(True)
        self.ui_tags.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.ui_tags.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.ui_tags.setHeaderHidden(True)
        self.ui_tags.setRootIsDecorated(True)
        self.ui_tags.setUniformRowHeights(True)
        self.ui_tags.setModel(self.tags_model)

        # layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(self.ui_search)
        main_layout.addWidget(self.ui_tags)
        self.setLayout(main_layout)

        # signals
        self.ui_tags.doubleClicked.connect(self.tag_double_clicked)
        self.ui_search.textChanged.connect(self.search_text_changed)

        # init
        self.create_model()

    def create_model(self):
        model = self.ui_tags.model().sourceModel()
        self.populate_tree(tags, model.invisibleRootItem())
        self.ui_tags.sortByColumn(0, QtCore.Qt.AscendingOrder)


    def populate_tree(self, children, parent):
        for child in sorted(children):
            node = QtGui.QStandardItem(child)
            parent.appendRow(node)

            if isinstance(children, dict):
                self.populate_tree(children[child], node)


    def tag_double_clicked(self, item):
        text = item.data(role=QtCore.Qt.DisplayRole)
        print [text]
        self.clickedTag.emit([text])


    def search_text_changed(self, text=None):
        regExp = QtCore.QRegExp(self.ui_search.text(), QtCore.Qt.CaseInsensitive, QtCore.QRegExp.FixedString)

        self.tags_model.text = self.ui_search.text().lower()
        self.tags_model.setFilterRegExp(regExp)

        if len(self.ui_search.text()) >= 1 and self.tags_model.rowCount() > 0:
            self.ui_tags.expandAll()
        else:
            self.ui_tags.collapseAll()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = TagsBrowserWidget()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
like image 927
JokerMartini Avatar asked Oct 28 '22 22:10

JokerMartini


1 Answers

There's no point in setting the case-sensivity of the filter-proxy at all, because you are by-passing the built-in filtering by overriding filterAcceptsRow. And even if you weren't doing that, setFilterRegExp ignores the current case sensitiviy settings anyway.

I would simplify the filter-proxy to this:

class SearchProxyModel(QtGui.QSortFilterProxyModel):

    def setFilterRegExp(self, pattern):
        if isinstance(pattern, str):
            pattern = QtCore.QRegExp(
                pattern, QtCore.Qt.CaseInsensitive,
                QtCore.QRegExp.FixedString)
        super(SearchProxyModel, self).setFilterRegExp(pattern)

    def _accept_index(self, idx):
        if idx.isValid():
            text = idx.data(QtCore.Qt.DisplayRole)
            if self.filterRegExp().indexIn(text) >= 0:
                return True
            for row in range(idx.model().rowCount(idx)):
                if self._accept_index(idx.model().index(row, 0, idx)):
                    return True
        return False

    def filterAcceptsRow(self, sourceRow, sourceParent):
        idx = self.sourceModel().index(sourceRow, 0, sourceParent)
        return self._accept_index(idx)

and change the search method to this:

def search_text_changed(self, text=None):
    self.tags_model.setFilterRegExp(self.ui_search.text())

    if len(self.ui_search.text()) >= 1 and self.tags_model.rowCount() > 0:
        self.ui_tags.expandAll()
    else:
        self.ui_tags.collapseAll()

So now the SearchProxyModel has sole responsibilty for deciding how searches are performed via its setFilterRegExp method. The case-sensitivity is handled transparently, so there is no need to pre-process the input.

The method for getting a list of descendants, can be written like this:

def tag_double_clicked(self, idx):
    text = []
    while idx.isValid():
        text.append(idx.data(QtCore.Qt.DisplayRole))
        idx = idx.parent()
    text.reverse()
    self.clickedTag.emit(text)
like image 66
ekhumoro Avatar answered Nov 15 '22 05:11

ekhumoro