Beispiel #1
0
def getItemFromIndex(model: QAbstractProxyModel,
                     index: QModelIndex) -> QStandardItem:
    '''Returns the item corresponding to the given index.'''
    while isinstance(model, QAbstractProxyModel):
        index = model.mapToSource(index)
        model = model.sourceModel()
    return model.itemFromIndex(index)
Beispiel #2
0
 def setSourceModel(self, model):  # pylint: disable=invalid-name
     """ Reset index/row maps and connect signals """
     QAbstractProxyModel.setSourceModel(self, model)
     self.reset_model()
     self.sourceModel().dataChanged.connect(self.sourceDataChanged)
     self.sourceModel().modelReset.connect(self.reset_model)
     self.sourceModel().layoutChanged.connect(self.reset_model)
     self.sourceModel().rowsRemoved.connect(self.reset_model)
Beispiel #3
0
    def __init__(self, parent=None):
        QAbstractProxyModel.__init__(self, parent)
        self.__sourceColumn = 0
        self.__flatteningMode = 1
        self.__sourceRootIndex = QModelIndex()

        self._source_key = {}
        self._source_offset = {}
Beispiel #4
0
    def data(self, index, role=Qt.DisplayRole):
        """
        Public method to get data from the model.
        
        @param index index of history entry to get data for (QModelIndex)
        @param role data role (integer)
        @return history entry data
        """
        if role in [Qt.DisplayRole, Qt.EditRole]:
            start = index.internalId()
            if start == 0:
                offset = self.__sourceDateRow(index.row())
                if index.column() == 0:
                    idx = self.sourceModel().index(offset, 0)
                    date = idx.data(HistoryModel.DateRole)
                    if date == QDate.currentDate():
                        return self.tr("Earlier Today")
                    return date.toString("yyyy-MM-dd")
                if index.column() == 1:
                    return self.tr(
                        "%n item(s)", "",
                        self.rowCount(index.sibling(index.row(), 0)))

        elif role == Qt.DecorationRole:
            if index.column() == 0 and not index.parent().isValid():
                return UI.PixmapCache.getIcon("history.png")

        elif role == HistoryModel.DateRole:
            if index.column() == 0 and index.internalId() == 0:
                offset = self.__sourceDateRow(index.row())
                idx = self.sourceModel().index(offset, 0)
                return idx.data(HistoryModel.DateRole)

        return QAbstractProxyModel.data(self, index, role)
Beispiel #5
0
 def data(self, index, role=Qt.DisplayRole):
     """
     Public method to get data from the model.
     
     @param index index of history entry to get data for (QModelIndex)
     @param role data role (integer)
     @return history entry data
     """
     if role in [Qt.DisplayRole, Qt.EditRole]:
         start = index.internalId()
         if start == 0:
             offset = self.__sourceDateRow(index.row())
             if index.column() == 0:
                 idx = self.sourceModel().index(offset, 0)
                 date = idx.data(HistoryModel.DateRole)
                 if date == QDate.currentDate():
                     return self.tr("Earlier Today")
                 return date.toString("yyyy-MM-dd")
             if index.column() == 1:
                 return self.tr(
                     "%n item(s)", "",
                     self.rowCount(index.sibling(index.row(), 0)))
     
     elif role == Qt.DecorationRole:
         if index.column() == 0 and not index.parent().isValid():
             return UI.PixmapCache.getIcon("history.png")
     
     elif role == HistoryModel.DateRole:
         if index.column() == 0 and index.internalId() == 0:
             offset = self.__sourceDateRow(index.row())
             idx = self.sourceModel().index(offset, 0)
             return idx.data(HistoryModel.DateRole)
     
     return QAbstractProxyModel.data(self, index, role)
Beispiel #6
0
 def flags(self, index):
     flags = QAbstractProxyModel.flags(self, index)
     if self.__flatteningMode == self.InternalNodesDisabled:
         sourceIndex = self.mapToSource(index)
         sourceModel = self.sourceModel()
         if sourceModel.rowCount(sourceIndex) > 0 and \
                 flags & Qt.ItemIsEnabled:
             # Internal node, enabled in the source model, disable it
             flags ^= Qt.ItemIsEnabled
     return flags
Beispiel #7
0
    def setSourceModel(self, model):
        self.beginResetModel()

        curr_model = self.sourceModel()

        if curr_model is not None:
            curr_model.dataChanged.disconnect(self._sourceDataChanged)
            curr_model.rowsInserted.disconnect(self._sourceRowsInserted)
            curr_model.rowsRemoved.disconnect(self._sourceRowsRemoved)
            curr_model.rowsMoved.disconnect(self._sourceRowsMoved)

        QAbstractProxyModel.setSourceModel(self, model)
        self._updateRowMapping()

        model.dataChanged.connect(self._sourceDataChanged)
        model.rowsInserted.connect(self._sourceRowsInserted)
        model.rowsRemoved.connect(self._sourceRowsRemoved)
        model.rowsMoved.connect(self._sourceRowsMoved)

        self.endResetModel()
Beispiel #8
0
 def data(self, index, role=Qt.DisplayRole):
     """
     Public method to get data from the model.
     
     @param index index of history entry to get data for (QModelIndex)
     @param role data role (integer)
     @return history entry data
     """
     if role == self.FrequencyRole and index.isValid():
         return self.__filteredRows[index.row()].frequency
     
     return QAbstractProxyModel.data(self, index, role)
Beispiel #9
0
def findIndexInModel(
    model: QAbstractProxyModel,
    predicate: ItemPredicate,
    parentIndex: QModelIndex = QModelIndex()
) -> QModelIndex:
    '''Returns the index of the first item in a hierarchical model that satisfies the given predicate.'''
    rowCount = model.rowCount(parentIndex)
    for row in range(rowCount):
        index = model.index(row, 0, parentIndex)

        # Check this item.
        item = getItemFromIndex(model, index)
        if predicate(item):
            return index

        # Recurse into children.
        childIndex = findIndexInModel(model, predicate, index)
        if childIndex.isValid():
            return childIndex

    return QModelIndex()