from PyQt4 import QtCore, QtGui
def __init__(self, parent = None):
super(FlatProxyModel, self).__init__(parent)
def sourceDataChanged(self, topLeft, bottomRight):
self.dataChanged.emit(self.mapFromSource(topLeft), self.mapFromSource(bottomRight))
def buildMap
(self, model, parent
= QtCore.
QModelIndex(), row
= 0): if row == 0:
self.m_rowMap = {}
self.m_indexMap = {}
rows = model.rowCount(parent)
for r in range(rows):
index = model.index(r, 0, parent)
print('row', row, 'item', model.data(index,0))
self.m_rowMap[index] = row
self.m_indexMap[row] = index
row = row + 1
if model.hasChildren(index):
row = self.buildMap(model, index, row)
return row
def setSourceModel(self, model):
self.model = model
self.buildMap(model)
model.dataChanged.connect(self.sourceDataChanged)
def mapFromSource(self, index):
if index not in self.
m_rowMap: return QtCore.
QModelIndex() return self.createIndex(self.m_rowMap[index], index.column())
def mapToSource(self, index):
if not index.isValid() or index.row() not in self.m_indexMap:
return self.m_indexMap[index.row()]
def columnCount(self, parent):
.columnCount(self.mapToSource(parent))
def rowCount(self, parent):
return len(self.m_rowMap) if not parent.isValid() else 0
def index(self, row, column, parent):
return self.createIndex(row, column)
def parent(self, index):
from PyQt4 import QtCore, QtGui
class FlatProxyModel(QtGui.QAbstractProxyModel):
def __init__(self, parent = None):
super(FlatProxyModel, self).__init__(parent)
@QtCore.pyqtSlot(QtCore.QModelIndex, QtCore.QModelIndex)
def sourceDataChanged(self, topLeft, bottomRight):
self.dataChanged.emit(self.mapFromSource(topLeft), self.mapFromSource(bottomRight))
def buildMap(self, model, parent = QtCore.QModelIndex(), row = 0):
if row == 0:
self.m_rowMap = {}
self.m_indexMap = {}
rows = model.rowCount(parent)
for r in range(rows):
index = model.index(r, 0, parent)
print('row', row, 'item', model.data(index,0))
self.m_rowMap[index] = row
self.m_indexMap[row] = index
row = row + 1
if model.hasChildren(index):
row = self.buildMap(model, index, row)
return row
def setSourceModel(self, model):
self.model = model
QtGui.QAbstractProxyModel.setSourceModel(self, model)
self.buildMap(model)
model.dataChanged.connect(self.sourceDataChanged)
def mapFromSource(self, index):
if index not in self.m_rowMap: return QtCore.QModelIndex()
return self.createIndex(self.m_rowMap[index], index.column())
def mapToSource(self, index):
if not index.isValid() or index.row() not in self.m_indexMap:
return QtCore.QModelIndex()
return self.m_indexMap[index.row()]
def columnCount(self, parent):
return QtGui.QAbstractProxyModel.sourceModel(self)\
.columnCount(self.mapToSource(parent))
def rowCount(self, parent):
return len(self.m_rowMap) if not parent.isValid() else 0
def index(self, row, column, parent):
if parent.isValid(): return QtCore.QModelIndex()
return self.createIndex(row, column)
def parent(self, index):
return QtCore.QModelIndex()
To copy to clipboard, switch view to plain text mode
QtCore.
QObject.
connect(self._model,QtCore.
SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.
flattenedProxyModel.
update)QtCore.
QObject.
connect(self._model,QtCore.
SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.
filteredProxyModel.
modelReset)
QtCore.QObject.connect(self._model,QtCore.SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.flattenedProxyModel.update)
QtCore.QObject.connect(self._model,QtCore.SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.filteredProxyModel.modelReset)
To copy to clipboard, switch view to plain text mode
Bookmarks