PDA

View Full Version : QAbstractProxyModel that solves Ref Items?



Aki
6th November 2013, 20:09
I have simple find & replace problem with ProxyModel.

My MainTreeModel contains MongoDB documents, witch can contain
DBRefs as fields. (Mongo documents are close relatives of Json objects)



##
#
# One document object in Json format.
# (Just to get the idea of data structure)
#
{
"_id": {
"_id": "5273df1f01d418250767b0a3",
"__type__": "Base.ObjectId"
}
"__type__": "Base.BaseDoc.BaseFlow.Comment",
"name": "Topic",
"timestamp": {
"date": 1383332671257,
"__type__": "date.datetime"
},
"author": null,
"_ns": "MyProject",
"_lock": {
"__type__": "UUID",
"uuid": "e83ad88206bf4e2181d1f848b4708165"
},
"comments": {
"__type__": "Base.RefSet"
"_references": [
{
"_type": "Base.BaseDoc.BaseFlow.Comment",
"_id": {
"_id": "527a6b9f01d418250767b135",
"__type__": "Base.ObjectId"
},
"_ns": "MyProject",
"_db": null,
"_lock": {
"__type__": "UUID",
"uuid": "2470196b220f496590a2adc9645f53b6"
},
"__type__": "Base.Ref"
},
{
"_type": "Base.BaseDoc.BaseFlow.Comment",
"_id": {
"_id": "527a6b2701d418250767b133",
"__type__": "Base.ObjectId"
},
"_ns": "MyProject",
"_db": null,
"_lock": null,
"__type__": "Base.Ref"
},
],
},
"text": "Comment content text ...",
}


Json is deserialized as document object, which is wrapped
inside Item, and for each field in document a wrapping child Item is created recursively.



deserialization note: dicts that have key='__type__' are deserialized as instances of __type__ keys value.



Now the actual problem.

What I`m trying to archive with QAbstractProxyModel is simple
Ref replacement with the actual document that the Ref is referencing.

But good examples / documentation is bit lacking when it comes to QAbstractProxyModel.
So I turn to you for little help on how to implement this.

Simplified code of my current Proxy. (not working)


##
#
# Proxy model that replaces Ref items
# with the actual referenced tree
#
class DocSolvedRefsProxyModel(QAbstractProxyModel):
def __init__(self, parent = None):
super(DocSolvedRefsProxyModel, self).__init__(parent = parent)
self._mappingTo = dict()
self._mappingFrom = dict()

def rowCount(self, index=QModelIndex()):
return self.sourceModel().rowCount(self.mapToSource(index ))

def columnCount(self, index=QModelIndex()):
return self.sourceModel().columnCount(self.mapToSource(in dex))

def index(self, row, column, parentIndex = QModelIndex()):
# to be implemented
return QModelIndex()

def parent(self, childIndex=None):
if childIndex is None:
# Trying to get parent widget.
# Nothing to do with model / view.
return QObject.parent(self)

# to be implemented
return QModelIndex()

def mapToSource(self, proxyIndex=QModelIndex()):
if not proxyIndex.isValid():
return QModelIndex()
return self._mappingTo[proxyIndex]

def mapFromSource(self, sourceIndex=QModelIndex()):
if not sourceIndex.isValid():
return QModelIndex()
if sourceIndex in self._mappingFrom:
return self._mappingFrom[sourceIndex]
else:
# checkin for refs & creating mapping

item = sourceIndex.internalPointer()

# Item.obj() method is used to get the actual object contained inside item.
obj = item.obj()
if isinstance(obj, Ref):

proxyIndex = ???

self._mappingTo[proxyIndex] = sourceIndex
self._mappingFrom[sourceIndex] = proxyIndex


Little nudge to right direction with the mapFromSource method whould be much appreciated.
/:Aki R.