jkrieger

for Toster

Dec 31st, 2013
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class NewItemProxyModel(QtGui.QAbstractProxyModel):
  2.     def __init__(self, new, parent=None, *args):
  3.         super(NewItemProxyModel, self).__init__(parent, *args)
  4.         self.__element__ = new
  5.  
  6.  
  7.     def setSourceModel(self, model):
  8.  
  9.         self.beginResetModel()
  10.  
  11.         QtGui.QAbstractProxyModel.setSourceModel(self, model)
  12.  
  13.         model.dataChanged.connect(self.sourceDataChanged)
  14.         model.modelReset.connect(self.sourceModelReset)
  15.  
  16.         # required for filtering/sorting
  17.         model.rowsInserted.connect(self.sourceRowsInserted)
  18.         model.rowsRemoved.connect(self.sourceRowsRemoved)
  19.         model.rowsAboutToBeInserted.connect(self.sourceRowsAboutToBeInserted)
  20.         model.rowsAboutToBeRemoved.connect(self.sourceRowsAboutToBeRemoved)
  21.         model.layoutAboutToBeChanged.connect(self.sourceLayoutAboutToBeChanged)
  22.         model.layoutChanged.connect(self.sourceLayoutChanged)
  23.  
  24.         self.endResetModel()
  25.  
  26. # ...
  27.  
  28.     def mapToSource(self, proxy_index):
  29.  
  30.         if not proxy_index.isValid():
  31.             return QtCore.QModelIndex()
  32.  
  33.         if proxy_index.row() == 0:
  34.             # return self.createIndex(proxy_index.row(), proxy_index.column(), proxy_index.parent())
  35.             return self.createIndex(proxy_index.row(), proxy_index.column())
  36.  
  37.         else:
  38.             return self.sourceModel().index(proxy_index.row() - 1, proxy_index.column())
  39.             # return self.sourceModel().createIndex(proxy_index.row() - 1, proxy_index.column(), proxy_index.internalPointer())
  40.  
  41.  
  42.     def mapFromSource(self, source_index):
  43.  
  44.         if not source_index.isValid():
  45.             return QtCore.QModelIndex()
  46.  
  47.         return self.index(source_index.row() + 1, source_index.column())
  48.         # return self.createIndex(source_index.row() + 1, source_index.column(), source_index.internalPointer())
  49.  
  50.  
  51.     def rowCount(self, parent=QtCore.QModelIndex()):
  52.  
  53.         return self.sourceModel().rowCount() + 1
  54.  
  55.  
  56.     def data(self, index, role=QtCore.Qt.DisplayRole):
  57.  
  58.         if index.isValid():
  59.             if index.row() == 0:
  60.                 if role == QtCore.Qt.DisplayRole: # or role == QtCore.Qt.EditRole:
  61.                     value = '<new %s>' % (self.__element__.name, )
  62.                     return QtCore.QVariant(value)
  63.  
  64.             else:
  65.                 if role == QtCore.Qt.DisplayRole: # or role == QtCore.Qt.EditRole:
  66.                     value = self.mapToSource(index).data(role)
  67.                     return QtCore.QVariant(value)
  68.  
  69.         return QtCore.QVariant()
RAW Paste Data