From ad82f79259ee7f2398ae31b2c6a011bacb99f4e7 Mon Sep 17 00:00:00 2001
From: Daniel Brown <ddb@star.sr.bham.ac.uk>
Date: Tue, 4 Mar 2014 13:09:14 +0000
Subject: [PATCH] updating gui on object removal

---
 pykat/finesse.py      | 11 +++++++++++
 pykat/gui/graphics.py | 14 ++++++++++----
 pykat/gui/gui.py      | 23 +++++++++++++++++++++++
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/pykat/finesse.py b/pykat/finesse.py
index 4b6b52b..9eb76d4 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 6b445de..8172e38 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 5532630..4d71109 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'...")
-- 
GitLab