def __init__(self, parent): super(EditorView, self).__init__(parent) self.setDragMode(QtGui.QGraphicsView.RubberBandDrag) self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu) self.setViewportUpdateMode(QtGui.QGraphicsView.FullViewportUpdate) self.attribute_panel = AttributePanel(self) self.attribute_panel.setMaximumWidth(self.geometry().width() / 3.0) self._command_key_pressed = False self._model = None self._dataMapper = QtGui.QDataWidgetMapper() self._itemDelegate = EditorViewDelegate(self) self._dataMapper.setItemDelegate(self._itemDelegate)
class EditorView(QtGui.QGraphicsView): '''Subclass of :class:`QGraphicsView` which acts as a viewer for some subset of the model. Automatically loads whatever :class:`ViewModel` is associated with the view's model, as *<Model Name>.view*. If the view file cannot be loaded or found, a default implementation simply creates a :class:`EditorItem` object representing the model. ''' drag_mode_key = QtCore.Qt.Key_Control scroll_mode_key = QtCore.Qt.Key_Control close_aw_key = QtCore.Qt.Key_Escape def __init__(self, parent): super(EditorView, self).__init__(parent) self.setDragMode(QtGui.QGraphicsView.RubberBandDrag) self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu) self.setViewportUpdateMode(QtGui.QGraphicsView.FullViewportUpdate) self.attribute_panel = AttributePanel(self) self.attribute_panel.setMaximumWidth(self.geometry().width() / 3.0) self._command_key_pressed = False self._model = None self._dataMapper = QtGui.QDataWidgetMapper() self._itemDelegate = EditorViewDelegate(self) self._dataMapper.setItemDelegate(self._itemDelegate) def viewModel(self): # the view model is not encapsulated by data/item models # it is directly accessible class objects with attributes etc. return self.view_model def model(self): return self._model def setModel(self, model): self._model = model self._dataMapper.setModel(self.model()) def init_ui(self, index, fname=''): scene = EditorScene(self) self.setScene(scene) self._dataMapper.setModel(index.model()) self._dataMapper.setOrientation(QtCore.Qt.Vertical) self._dataMapper.setRootIndex(index) self._dataMapper.setCurrentIndex(1) self._dataMapper.addMapping(self, 0) # view model is static; will NEVER be edited or viewed, # and will never be used by anything but a scene/view and their # children, so we keep a reference to the view model open here self.view_model = None model = index.model().getModel(index) try: if not fname: fname = model.kind + '.view' self.loadVM(fname) except Exception, e: errStr = 'WARNING: Could not load \'{}\''.format(fname) errStr += ' to generate view for {}:\n\t{}'.format( model['Name'], e ) print errStr # How to initialize self.view_model here? self.view_model = None r = self.buildView(index) scene.addItem(r) self.show()