plugins/canfestival/canfestival.py
changeset 680 61746934df41
parent 512 36aeab46f27d
child 684 d376a8b0b602
--- a/plugins/canfestival/canfestival.py	Tue Feb 07 19:14:10 2012 +0100
+++ b/plugins/canfestival/canfestival.py	Thu Feb 09 21:23:11 2012 +0100
@@ -13,6 +13,9 @@
 from commondialogs import CreateNodeDialog
 import wx
 
+from SlaveEditor import SlaveEditor
+from NetworkEditor import NetworkEditor
+
 from gnosis.xml.pickle import *
 from gnosis.xml.pickle.util import setParanoia
 setParanoia(0)
@@ -59,9 +62,8 @@
       </xsd:element>
     </xsd:schema>
     """ % DEFAULT_SETTINGS
-
-    def GetSlaveODPath(self):
-        return os.path.join(self.PlugPath(), 'slave.od')
+    
+    EditorType = SlaveEditor
 
     def __init__(self):
         # TODO change netname when name change
@@ -97,34 +99,18 @@
                                    "heartbeat",  # NMT
                                    [])           # options
             dialog.Destroy()
-    _View = None
+            self.OnPlugSave()
+
+    def GetSlaveODPath(self):
+        return os.path.join(self.PlugPath(), 'slave.od')
+
+    def GetCanDevice(self):
+        return self.CanFestivalSlaveNode.getCan_Device()
+
     def _OpenView(self):
-        if not self._View:
-            open_objdictedit = True
-            has_permissions = self.GetPlugRoot().CheckProjectPathPerm()
-            if not has_permissions:
-                dialog = wx.MessageDialog(self.GetPlugRoot().AppFrame,
-                                          _("You don't have write permissions.\nOpen ObjDictEdit anyway ?"),
-                                          _("Open ObjDictEdit"),
-                                          wx.YES_NO|wx.ICON_QUESTION)
-                open_objdictedit = dialog.ShowModal() == wx.ID_YES
-                dialog.Destroy()
-            if open_objdictedit:
-                def _onclose():
-                    self._View = None
-                if has_permissions:
-                    def _onsave():
-                        self.GetPlugRoot().SaveProject()
-                else:
-                    def _onsave():
-                        pass
-            
-                self._View = objdictedit(self.GetPlugRoot().AppFrame, self)
-                # TODO redefine BusId when IEC channel change
-                self._View.SetBusId(self.GetCurrentLocation())
-                self._View._onclose = _onclose
-                self._View._onsave = _onsave
-                self._View.Show()
+        PlugTemplate._OpenView(self)
+        if self._View is not None:
+            self._View.SetBusId(self.GetCurrentLocation())
 
     PluginMethods = [
         {"bitmap" : os.path.join("images", "NetworkEdit"),
@@ -143,6 +129,15 @@
     def OnPlugSave(self):
         return self.SaveCurrentInFile(self.GetSlaveODPath())
 
+    def SetParamsAttribute(self, path, value):
+        result = PlugTemplate.SetParamsAttribute(self, path, value)
+        
+        # Filter IEC_Channel and Name, that have specific behavior
+        if path == "BaseParams.IEC_Channel" and self._View is not None:
+            self._View.SetBusId(self.GetCurrentLocation())
+        
+        return result
+        
     def PlugGenerate_C(self, buildpath, locations):
         """
         Generate C code
@@ -173,10 +168,38 @@
             raise Exception, res
         return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False
 
+    def LoadPrevious(self):
+        self.LoadCurrentPrevious()
+    
+    def LoadNext(self):
+        self.LoadCurrentNext()
+    
+    def GetBufferState(self):
+        return self.GetCurrentBufferState()
+
 #--------------------------------------------------
 #                    MASTER
 #--------------------------------------------------
 
+class MiniNodeManager(NodeManager):
+    
+    def __init__(self, parent, filepath, fullname):
+        NodeManager.__init__(self)
+        
+        self.OpenFileInCurrent(filepath)
+            
+        self.Parent = parent
+        self.Fullname = fullname
+        
+    def OnCloseEditor(self, view):
+        self.Parent.OnCloseEditor(view)
+    
+    def PlugFullName(self):
+        return self.Fullname
+    
+    def GetBufferState(self):
+        return self.GetCurrentBufferState()
+
 class _NodeListPlug(NodeList):
     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
@@ -190,56 +213,61 @@
       </xsd:element>
     </xsd:schema>
     """ % DEFAULT_SETTINGS
-
+    
+    EditorType = NetworkEditor
+    
     def __init__(self):
         manager = NodeManager()
-        # TODO change netname when name change
-        NodeList.__init__(self, manager, self.BaseParams.getName())
+        NodeList.__init__(self, manager)
         self.LoadProject(self.PlugPath())
-
-    _View = None
+        self.SetNetworkName(self.BaseParams.getName())
+    
+    def GetCanDevice(self):
+        return self.CanFestivalNode.getCan_Device()
+    
+    def SetParamsAttribute(self, path, value):
+        result = PlugTemplate.SetParamsAttribute(self, path, value)
+        
+        # Filter IEC_Channel and Name, that have specific behavior
+        if path == "BaseParams.IEC_Channel" and self._View is not None:
+            self._View.SetBusId(self.GetCurrentLocation())
+        elif path == "BaseParams.Name":
+            self.SetNetworkName(value)
+        
+        return result
+    
     def _OpenView(self):
-        if not self._View:
-            open_networkedit = True
-            has_permissions = self.GetPlugRoot().CheckProjectPathPerm()
-            if not has_permissions:
-                dialog = wx.MessageDialog(self.GetPlugRoot().AppFrame,
-                                          _("You don't have write permissions.\nOpen NetworkEdit anyway ?"),
-                                          _("Open NetworkEdit"),
-                                          wx.YES_NO|wx.ICON_QUESTION)
-                open_networkedit = dialog.ShowModal() == wx.ID_YES
-                dialog.Destroy()
-            if open_networkedit:
-                def _onclose():
-                    self._View = None
-                if has_permissions:
-                    def _onsave():
-                        self.GetPlugRoot().SaveProject()
-                else:
-                    def _onsave():
-                        pass
-                self._View = networkedit(self.GetPlugRoot().AppFrame, self)
-                # TODO redefine BusId when IEC channel change
-                self._View.SetBusId(self.GetCurrentLocation())
-                self._View._onclose = _onclose
-                self._View._onsave = _onsave
-                self._View.Show()
-
+        PlugTemplate._OpenView(self)
+        if self._View is not None:
+            self._View.SetBusId(self.GetCurrentLocation())
+    
+    _GeneratedView = None
     def _ShowMasterGenerated(self):
-        buildpath = self._getBuildPath()
-        # Eventually create build dir
-        if not os.path.exists(buildpath):
-            self.GetPlugRoot().logger.write_error(_("Error: No PLC built\n"))
-            return
-        
-        masterpath = os.path.join(buildpath, "MasterGenerated.od")
-        if not os.path.exists(masterpath):
-            self.GetPlugRoot().logger.write_error(_("Error: No Master generated\n"))
-            return
-        
-        new_dialog = objdictedit(None, filesOpen=[masterpath])
-        new_dialog.Show()
-
+        if self._GeneratedView is None:
+            buildpath = self._getBuildPath()
+            # Eventually create build dir
+            if not os.path.exists(buildpath):
+                self.GetPlugRoot().logger.write_error(_("Error: No PLC built\n"))
+                return
+            
+            masterpath = os.path.join(buildpath, "MasterGenerated.od")
+            if not os.path.exists(masterpath):
+                self.GetPlugRoot().logger.write_error(_("Error: No Master generated\n"))
+                return
+            
+            app_frame = self.GetPlugRoot().AppFrame
+            
+            manager = MiniNodeManager(self, masterpath, self.PlugFullName() + ".generated_master")
+            self._GeneratedView = SlaveEditor(app_frame.TabsOpened, manager, app_frame, False)
+            
+            app_frame.EditProjectElement(self._GeneratedView, "MasterGenerated")
+    
+    def _CloseGenerateView(self):
+        if self._GeneratedView is not None:
+            app_frame = self.GetPlugRoot().AppFrame
+            if app_frame is not None:
+                app_frame.DeletePage(self._GeneratedView)
+    
     PluginMethods = [
         {"bitmap" : os.path.join("images", "NetworkEdit"),
          "name" : _("Edit network"), 
@@ -250,10 +278,16 @@
          "tooltip" : _("Show Master generated by config_utils"),
          "method" : "_ShowMasterGenerated"}
     ]
+    
+    def OnCloseEditor(self, view):
+        PlugTemplate.OnCloseEditor(self, view)
+        if self._GeneratedView == view:
+            self._GeneratedView = None
 
     def OnPlugClose(self):
-        if self._View:
-            self._View.Close()
+        PlugTemplate.OnPlugClose(self)
+        self._CloseGenerateView()
+        return True
 
     def PlugTestModified(self):
         return self.ChangesToSave or self.HasChanged()
@@ -275,6 +309,7 @@
             }, ...]
         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
         """
+        self._CloseGenerateView()
         current_location = self.GetCurrentLocation()
         # define a unique name for the generated C file
         prefix = "_".join(map(str, current_location))
@@ -295,6 +330,15 @@
         
         return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False
     
+    def LoadPrevious(self):
+        self.Manager.LoadCurrentPrevious()
+    
+    def LoadNext(self):
+        self.Manager.LoadCurrentNext()
+    
+    def GetBufferState(self):
+        return self.Manager.GetCurrentBufferState()
+    
 class RootClass:
     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
@@ -317,14 +361,16 @@
                     if child["name"] == "CAN_Driver":
                         DLL_LIST= getattr(local_canfestival_config,"DLL_LIST",None)
                         if DLL_LIST is not None:
-                            child["type"] = DLL_LIST
-                        return infos    
+                            child["type"] = DLL_LIST  
         return infos
-
+    
+    def GetCanDriver(self):
+        return self.CanFestivalInstance.getCAN_Driver()
+    
     def PlugGenerate_C(self, buildpath, locations):
         
         format_dict = {"locstr" : "_".join(map(str,self.GetCurrentLocation())),
-                       "candriver" : self.CanFestivalInstance.getCAN_Driver(),
+                       "candriver" : self.GetCanDriver(),
                        "nodes_includes" : "",
                        "board_decls" : "",
                        "nodes_init" : "",
@@ -409,7 +455,7 @@
             # Declare CAN channels according user filled config
             format_dict["board_decls"] += 'BOARD_DECL(%s, "%s", "%s")\n'%(
                    nodename,
-                   child_data.getCAN_Device(),
+                   child.GetCanDevice(),
                    child_data.getCAN_Baudrate())
             format_dict["nodes_open"] += 'NODE_OPEN(%s)\n    '%(nodename)
             format_dict["nodes_close"] += 'NODE_CLOSE(%s)\n    '%(nodename)