File indexing completed on 2025-01-26 04:11:38

0001 # SPDX-License-Identifier: CC0-1.0
0002 
0003 from PyQt5.QtCore import QAbstractItemModel, QModelIndex, Qt
0004 from PyQt5.QtGui import QPixmap
0005 from . import filtermanagertreeitem
0006 
0007 
0008 class FilterManagerTreeModel(QAbstractItemModel):
0009 
0010     TYPE_COLUMN = 1
0011     NODE_COLUMN = 3
0012     DOCUMENT_COLUMN = 4
0013 
0014     def __init__(self, uiFilterManager, parent=None):
0015         super(FilterManagerTreeModel, self).__init__(parent)
0016 
0017         self.rootItem = filtermanagertreeitem.FilterManagerTreeItem(
0018             (i18n("Name"), i18n("Type"), i18n("Thumbnail")))
0019         self.uiFilterManager = uiFilterManager
0020         self._loadTreeModel(self.rootItem)
0021 
0022     def index(self, row, column, parent):
0023         if not self.hasIndex(row, column, parent):
0024             return QModelIndex()
0025 
0026         if parent.isValid():
0027             parentItem = parent.internalPointer()
0028         else:
0029             parentItem = self.rootItem
0030 
0031         # It's a FilterManagerTreeItem
0032         childItem = parentItem.child(row)
0033         if childItem:
0034             return self.createIndex(row, column, childItem)
0035         else:
0036             return QModelIndex()
0037 
0038     def parent(self, index):
0039         if not index.isValid():
0040             return QModelIndex()
0041 
0042         childItem = index.internalPointer()
0043         parentItem = childItem.parent()
0044 
0045         if parentItem == self.rootItem:
0046             return QModelIndex()
0047 
0048         return self.createIndex(parentItem.row(), 0, parentItem)
0049 
0050     def rowCount(self, parent):
0051         if parent.column() > 0:
0052             return 0
0053 
0054         if not parent.isValid():
0055             parentItem = self.rootItem
0056         else:
0057             parentItem = parent.internalPointer()
0058         return parentItem.childCount()
0059 
0060     def columnCount(self, parent):
0061         if parent.isValid():
0062             return parent.internalPointer().columnCount()
0063         else:
0064             return self.rootItem.columnCount()
0065 
0066     def data(self, index, role):
0067         if not index.isValid():
0068             return None
0069 
0070         item = index.internalPointer()
0071 
0072         if role == Qt.UserRole + 1:
0073             return item.data(self.NODE_COLUMN)
0074 
0075         if role == Qt.UserRole + 2:
0076             return item.data(self.DOCUMENT_COLUMN)
0077 
0078         if role == Qt.UserRole + 3:
0079             return item.data(self.TYPE_COLUMN)
0080 
0081         if role != Qt.DisplayRole and role != Qt.DecorationRole:
0082             return None
0083 
0084         return item.data(index.column())
0085 
0086     def flags(self, index):
0087         if not index.isValid():
0088             return Qt.NoItemFlags
0089 
0090         return Qt.ItemIsEnabled | Qt.ItemIsSelectable
0091 
0092     def headerData(self, section, orientation, role):
0093         if orientation == Qt.Horizontal and role == Qt.DisplayRole:
0094             return self.rootItem.data(section)
0095 
0096         return None
0097 
0098     def _loadTreeModel(self, parent):
0099         for index, document in enumerate(self.uiFilterManager.documents):
0100             rootNode = document.rootNode()
0101             columnData = (document.fileName(),
0102                           "Document",
0103                           QPixmap.fromImage(document.thumbnail(30, 30)),
0104                           rootNode, index)
0105             item = filtermanagertreeitem.FilterManagerTreeItem(
0106                 columnData, parent)
0107             parent.appendChild(item)
0108 
0109             childNodes = rootNode.childNodes()
0110             if len(childNodes):
0111                 self._addSubNodes(childNodes[::-1], item, index)
0112 
0113     def _addSubNodes(self, nodes, parent, documentIndex):
0114         for node in nodes:
0115             nodeName = node.name()
0116             nodeType = node.type()
0117             columnData = ("Unnamed" if nodeName == '' else nodeName,
0118                           "Untyped" if nodeType == '' else nodeType,
0119                           QPixmap.fromImage(node.thumbnail(30, 30)),
0120                           node, documentIndex)
0121             item = filtermanagertreeitem.FilterManagerTreeItem(
0122                 columnData, parent)
0123             parent.appendChild(item)
0124 
0125             childNodes = node.childNodes()
0126             if len(childNodes):
0127                 self._addSubNodes(childNodes[::-1], item, documentIndex)