class NodeTree(QTreeView): def __init__(self): super(NodeTree, self).__init__() self._model = NodeModel(self) self._proxy = QSortFilterProxyModel() self._proxy.setSortRole(Qt.UserRole) # sort order is stored in item in Qt.UserRole self._proxy.setSourceModel(self._model) self.setModel(self._proxy) self.setItemDelegate(NodeDelegate()) self.header().setResizeMode(self.header().ResizeToContents) self.header().setStretchLastSection(False) self.setSortingEnabled(True) self.sortByColumn(self.model().sourceModel().HEADER.index('Node'), Qt.AscendingOrder)
class ModelController(QObject): """ The network controller which basically manages all aspects revolving around a single SBML model (i.e. SBML file). It has references to outside Views (e.g. TreeView). Internally, it has a reference to the custom NetworkView. A Dirty state is used to handle the need for saving/rejecting changes, etc. It inherits from QObject for the use of Signals/Slots. @param filename: Filename of the SBML model that should be opened. If None, an empty model is created. @type filename: str @param views: The views of the main window that are to be associated with the model. @type views: L{QAbstractItemView} @since: 2010-04-12 """ __author__ = "Moritz Wade" __contact__ = "*****@*****.**" __copyright__ = "Zuse Institute Berlin 2010" dirtyChanged = Signal(bool) def GetDirty(self): return self.__Dirty def SetDirty(self, value): self.__Dirty = value # self.emit(SIGNAL("DirtyChanged(bool)"), value) self.dirtyChanged.emit(value) def DelDirty(self): del self.__Dirty Dirty = property( GetDirty, SetDirty, DelDirty, "Defines whether this model has any unsaved changes. This is passed through to SbmlMainModel's Dirty." ) def __init__(self, filename=None, views=[], networkViewController=None): """ Sets up some instance variables. Most importantly, it executes the loading of a SBML file, if a filename is given. """ super(ModelController, self).__init__() self.filename = None # will be set by self._loadFile self.sbmlModel = None self.treeModel = None self.selectionModel = None self.proxyModel = None # we have the proxy model here, so that all Views can use it and the same SelectionModel still works (!) self.views = views self.networkViewController = networkViewController # a special place for the NetworkViewController # self.connect(self, SIGNAL("DirtyChanged(bool)"), self.updateUI) # self.dirtyChanged.connect(self.updateUI) if filename is not None: self._loadFile(filename) def setViews(self, views=None, networkViewController=None): """ Can be used to re-reference the ModelController with Views (either if they haven't been given to the Constructor or if they have changed somehow). This internally calls self._connectViews to set the correct models and a single QSelectionModel on the views. """ if views: self.views = views if networkViewController: self.networkViewController = networkViewController self._connectViews() if self.networkViewController: self.networkViewController._createGraph() def _loadFile(self, filename=None): ''' Loads a SBML file. It invokes the creation of a data model and a networkx graph based on the SBML data. @param filename: Name of the SBML file @type filename: str ''' if filename is None: return self.filename = filename self.sbmlModel = SBMLMainModel(filename) self.treeModel = self.sbmlModel.MainTreeModel # self.connect(self.sbmlModel, SIGNAL("DirtyChanged(bool)"), self.on_dirtyChanged) self.sbmlModel.dirtyChanged.connect(self.on_dirtyChanged) # self.proxyModel = self.treeModel # DEBUGGING: "disabling" the proxy approach self.proxyModel = QSortFilterProxyModel() self.proxyModel.setSortRole(Qt.UserRole) try: self.proxyModel.setSourceModel(self.treeModel) except Exception, e: # try again logging.debug( "ModelController._loadFile(): Could not set source model for proxy model. Trying again. Original Error: %s" % e) self._loadFile(filename) self.selectionModel = QItemSelectionModel(self.proxyModel) # self.selectionModel = self.proxyModel.selectionModel() self._connectViews( ) # probably not needed here, there is no View after __init__ if self.networkViewController: self.networkViewController._createGraph() self.Dirty = False
class ModelController(QObject): """ The network controller which basically manages all aspects revolving around a single SBML model (i.e. SBML file). It has references to outside Views (e.g. TreeView). Internally, it has a reference to the custom NetworkView. A Dirty state is used to handle the need for saving/rejecting changes, etc. It inherits from QObject for the use of Signals/Slots. @param filename: Filename of the SBML model that should be opened. If None, an empty model is created. @type filename: str @param views: The views of the main window that are to be associated with the model. @type views: L{QAbstractItemView} @since: 2010-04-12 """ __author__ = "Moritz Wade" __contact__ = "*****@*****.**" __copyright__ = "Zuse Institute Berlin 2010" dirtyChanged = Signal(bool) def GetDirty(self): return self.__Dirty def SetDirty(self, value): self.__Dirty = value self.dirtyChanged.emit(value) def DelDirty(self): del self.__Dirty Dirty = property(GetDirty, SetDirty, DelDirty, "Defines whether this model has any unsaved changes. This is passed through to SbmlMainModel's Dirty.") def __init__(self, filename=None, views=[], networkViewController=None): """ Sets up some instance variables. Most importantly, it executes the loading of a SBML file, if a filename is given. """ super(ModelController, self).__init__() self.filename = None # will be set by self._loadFile self.sbmlModel = None self.treeModel = None self.selectionModel = None self.proxyModel = None # we have the proxy model here, so that all Views can use it and the same SelectionModel still works (!) self.views = views self.networkViewController = networkViewController # a special place for the NetworkViewController if filename is not None: self._loadFile(filename) def setViews(self, views=None, networkViewController=None): """ Can be used to re-reference the ModelController with Views (either if they haven't been given to the Constructor or if they have changed somehow). This internally calls self._connectViews to set the correct models and a single QSelectionModel on the views. """ if views: self.views = views if networkViewController: self.networkViewController = networkViewController self._connectViews() if self.networkViewController: self.networkViewController._createGraph() def _loadFile(self, filename=None): """ Loads a SBML file. It invokes the creation of a data model and a networkx graph based on the SBML data. @param filename: Name of the SBML file @type filename: str """ if filename is None: return self.filename = filename self.sbmlModel = SBMLMainModel(filename) self.treeModel = self.sbmlModel.MainTreeModel self.sbmlModel.dirtyChanged.connect(self.on_dirtyChanged) # self.proxyModel = self.treeModel # DEBUGGING: "disabling" the proxy approach self.proxyModel = QSortFilterProxyModel() self.proxyModel.setSortRole(Qt.UserRole) try: self.proxyModel.setSourceModel(self.treeModel) except Exception, e: # try again logging.debug("ModelController._loadFile(): Could not set source model for proxy model. Trying again. Original Error: %s" % e) self._loadFile(filename) self.selectionModel = QItemSelectionModel(self.proxyModel) self._connectViews() # probably not needed here, there is no View after __init__ if self.networkViewController: self.networkViewController._createGraph() self.Dirty = False