diff --git a/pykat/finesse.py b/pykat/finesse.py index 4b6b52bcbc33f8947979e9c37b928705b4b8305a..9eb76d45e7d1a8fe9055ffde045bfe0500fb86b5 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -821,7 +821,13 @@ class kat(object): if obj.removed: raise pkex.BasePyKatException("{0} has already been removed".format(obj.name)) + + nodes = None + # store nodes that this componet is attached to as a reference for gui + if isinstance(obj, Component): + nodes = self.nodes.getComponentNodes(obj) + if isinstance(obj, Component): del self.__components[obj.name] self.__del_component(obj) @@ -837,6 +843,11 @@ class kat(object): if obj in self.__blocks[b].contents: self.__blocks[b].contents.remove(obj) + if self.pykatgui != None: + self.pykatgui._onComponentRemoved(obj, nodes) + + del nodes + import gc print gc.get_referrers(obj) diff --git a/pykat/gui/graphics.py b/pykat/gui/graphics.py index 6b445defea3f529c42d8aa2d303878bf1aa0d499..8172e38d1a8f269bf08881e1a2d63049bee5541a 100644 --- a/pykat/gui/graphics.py +++ b/pykat/gui/graphics.py @@ -11,6 +11,7 @@ from PyQt4 import QtSvg from PyQt4.QtSvg import QGraphicsSvgItem import pykat.components import exceptions +import weakref nsize = 10 @@ -143,10 +144,15 @@ class SpaceQGraphicsItem(QGraphicsLineItem): class ComponentQGraphicsItem(QtSvg.QGraphicsSvgItem): + def __on_component_deleted(self, arg): + import gc + print gc.get_referrers(self) + def __init__(self, svgfile, component, nodes): QGraphicsSvgItem.__init__(self,svgfile) self.__nodeGraphics = [] - self.__component = component + self.__component = weakref.ref(component, self.__on_component_deleted) + # this signals the itemChange() method when this item is moved # used for refreshing the spaces between components self.setFlags(QGraphicsItem.ItemSendsGeometryChanges) @@ -170,7 +176,7 @@ class ComponentQGraphicsItem(QtSvg.QGraphicsSvgItem): self.setHandlesChildEvents(True) @property - def component(self): return self.__component + def component(self): return self.__component() def refresh(self): for n in self.__nodeGraphics: @@ -179,10 +185,10 @@ class ComponentQGraphicsItem(QtSvg.QGraphicsSvgItem): def itemChange(self, change, value): # if the item is moved then update any spaces attached to it if change == QGraphicsItem.ItemPositionHasChanged: - nodes = self.__component.nodes + nodes = self.component.nodes for n in nodes: - conn = n.amIConnected(self.__component) + conn = n.amIConnected(self.component) if conn[0] and isinstance(conn[1], pykat.components.space): conn[1].getQGraphicsItem().refresh() diff --git a/pykat/gui/gui.py b/pykat/gui/gui.py index 5532630aafb2716dc882c518ab155680cb853542..4d71109ae493c1dc1f9d4a50cf555ad96163ce12 100644 --- a/pykat/gui/gui.py +++ b/pykat/gui/gui.py @@ -70,6 +70,29 @@ class pyKatGUI(QtGui.QMainWindow, qt_gui.Ui_MainWindow): itm.refresh() itm.setCacheMode(QGraphicsItem.NoCache) self.__scene.addItem(itm) + + def _onComponentRemoved(self, comp, nodes): + """ + When a component has been removed from the kat object this function should update + all gui objects. + comp - object that is removed + nodes - nodes that this comp was attached too, as that information may no longer be accessible + """ + itm = comp.getQGraphicsItem() + + if itm != None: + + itm.refresh() + self.__scene.removeItem(itm) + + for n in nodes: + for cc in self._kat.nodes.getNodeComponents(n): + print "refresh", cc + if cc != None: + ccitm = cc.getQGraphicsItem() + if ccitm != None: + ccitm.refresh() + def exportToSVG(self): self.statusbar.showMessage("Saving to 'output.svg'...")