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)

Qt Code:
  1. ##
  2. #
  3. # One document object in Json format.
  4. # (Just to get the idea of data structure)
  5. #
  6. {
  7. "_id": {
  8. "_id": "5273df1f01d418250767b0a3",
  9. "__type__": "Base.ObjectId"
  10. }
  11. "__type__": "Base.BaseDoc.BaseFlow.Comment",
  12. "name": "Topic",
  13. "timestamp": {
  14. "date": 1383332671257,
  15. "__type__": "date.datetime"
  16. },
  17. "author": null,
  18. "_ns": "MyProject",
  19. "_lock": {
  20. "__type__": "UUID",
  21. "uuid": "e83ad88206bf4e2181d1f848b4708165"
  22. },
  23. "comments": {
  24. "__type__": "Base.RefSet"
  25. "_references": [
  26. {
  27. "_type": "Base.BaseDoc.BaseFlow.Comment",
  28. "_id": {
  29. "_id": "527a6b9f01d418250767b135",
  30. "__type__": "Base.ObjectId"
  31. },
  32. "_ns": "MyProject",
  33. "_db": null,
  34. "_lock": {
  35. "__type__": "UUID",
  36. "uuid": "2470196b220f496590a2adc9645f53b6"
  37. },
  38. "__type__": "Base.Ref"
  39. },
  40. {
  41. "_type": "Base.BaseDoc.BaseFlow.Comment",
  42. "_id": {
  43. "_id": "527a6b2701d418250767b133",
  44. "__type__": "Base.ObjectId"
  45. },
  46. "_ns": "MyProject",
  47. "_db": null,
  48. "_lock": null,
  49. "__type__": "Base.Ref"
  50. },
  51. ],
  52. },
  53. "text": "Comment content text ...",
  54. }
To copy to clipboard, switch view to plain text mode 

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)
Qt Code:
  1. ##
  2. #
  3. # Proxy model that replaces Ref items
  4. # with the actual referenced tree
  5. #
  6. class DocSolvedRefsProxyModel(QAbstractProxyModel):
  7. def __init__(self, parent = None):
  8. super(DocSolvedRefsProxyModel, self).__init__(parent = parent)
  9. self._mappingTo = dict()
  10. self._mappingFrom = dict()
  11.  
  12. def rowCount(self, index=QModelIndex()):
  13. return self.sourceModel().rowCount(self.mapToSource(index))
  14.  
  15. def columnCount(self, index=QModelIndex()):
  16. return self.sourceModel().columnCount(self.mapToSource(index))
  17.  
  18. def index(self, row, column, parentIndex = QModelIndex()):
  19. # to be implemented
  20. return QModelIndex()
  21.  
  22. def parent(self, childIndex=None):
  23. if childIndex is None:
  24. # Trying to get parent widget.
  25. # Nothing to do with model / view.
  26. return QObject.parent(self)
  27.  
  28. # to be implemented
  29. return QModelIndex()
  30.  
  31. def mapToSource(self, proxyIndex=QModelIndex()):
  32. if not proxyIndex.isValid():
  33. return QModelIndex()
  34. return self._mappingTo[proxyIndex]
  35.  
  36. def mapFromSource(self, sourceIndex=QModelIndex()):
  37. if not sourceIndex.isValid():
  38. return QModelIndex()
  39. if sourceIndex in self._mappingFrom:
  40. return self._mappingFrom[sourceIndex]
  41. else:
  42. # checkin for refs & creating mapping
  43.  
  44. item = sourceIndex.internalPointer()
  45.  
  46. # Item.obj() method is used to get the actual object contained inside item.
  47. obj = item.obj()
  48. if isinstance(obj, Ref):
  49.  
  50. proxyIndex = ???
  51.  
  52. self._mappingTo[proxyIndex] = sourceIndex
  53. self._mappingFrom[sourceIndex] = proxyIndex
To copy to clipboard, switch view to plain text mode 

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