PLCOpenEditor.py
changeset 714 131ea7f237b9
parent 712 c11b54730a7b
child 718 0183a66eddf9
--- a/PLCOpenEditor.py	Fri Jun 15 18:03:25 2012 +0200
+++ b/PLCOpenEditor.py	Mon Jun 25 20:03:53 2012 +0200
@@ -31,6 +31,10 @@
 CWD = os.path.split(os.path.realpath(__file__))[0]
 base_folder = os.path.split(CWD)[0]
 sys.path.append(base_folder)
+
+from utils.BitmapLibrary import AddBitmapFolder, GetBitmap
+AddBitmapFolder(os.path.join(CWD, "Images"))
+
 from docutil import *
 
 from types import TupleType
@@ -165,93 +169,93 @@
 EditorToolBarItems = {
     "FBD" : [(True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARMOTION, "OnMotionTool",
-              "move.png", _("Move the view")),
+              "move", _("Move the view")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARCOMMENT, "OnCommentTool",
-              "add_comment.png", _("Create a new comment")),
+              "add_comment", _("Create a new comment")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARVARIABLE, "OnVariableTool",
-              "add_variable.png", _("Create a new variable")),
+              "add_variable", _("Create a new variable")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARBLOCK, "OnBlockTool",
-              "add_block.png", _("Create a new block")),
+              "add_block", _("Create a new block")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCONNECTION, "OnConnectionTool", 
-              "add_connection.png", _("Create a new connection"))],
+              "add_connection", _("Create a new connection"))],
     "LD"  : [(True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARMOTION, "OnMotionTool",
-              "move.png", _("Move the view")),
+              "move", _("Move the view")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCOMMENT, "OnCommentTool", 
-              "add_comment.png", _("Create a new comment")),
+              "add_comment", _("Create a new comment")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARPOWERRAIL, "OnPowerRailTool", 
-              "add_powerrail.png", _("Create a new power rail")),
+              "add_powerrail", _("Create a new power rail")),
              (False, DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARRUNG, "OnRungTool", 
-              "add_rung.png", _("Create a new rung")),
+              "add_rung", _("Create a new rung")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCOIL, "OnCoilTool", 
-              "add_coil.png", _("Create a new coil")),
+              "add_coil", _("Create a new coil")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCONTACT, "OnContactTool", 
-              "add_contact.png", _("Create a new contact")),
+              "add_contact", _("Create a new contact")),
              (False, DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARBRANCH, "OnBranchTool", 
-              "add_branch.png", _("Create a new branch")),
+              "add_branch", _("Create a new branch")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARVARIABLE, "OnVariableTool", 
-              "add_variable.png", _("Create a new variable")),
+              "add_variable", _("Create a new variable")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARBLOCK, "OnBlockTool", 
-              "add_block.png", _("Create a new block")),
+              "add_block", _("Create a new block")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCONNECTION, "OnConnectionTool", 
-              "add_connection.png", _("Create a new connection"))],
+              "add_connection", _("Create a new connection"))],
     "SFC" : [(True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARMOTION, "OnMotionTool",
-              "move.png", _("Move the view")),
+              "move", _("Move the view")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCOMMENT, "OnCommentTool", 
-              "add_comment.png", _("Create a new comment")),
+              "add_comment", _("Create a new comment")),
              (True, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARINITIALSTEP, "OnInitialStepTool", 
-              "add_initial_step.png", _("Create a new initial step")),
+              "add_initial_step", _("Create a new initial step")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARSTEP, "OnStepTool", 
-              "add_step.png", _("Create a new step")),
+              "add_step", _("Create a new step")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARTRANSITION, "OnTransitionTool", 
-              "add_transition.png", _("Create a new transition")),
+              "add_transition", _("Create a new transition")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARACTIONBLOCK, "OnActionBlockTool", 
-              "add_action.png", _("Create a new action block")),
+              "add_action", _("Create a new action block")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARDIVERGENCE, "OnDivergenceTool", 
-              "add_divergence.png", _("Create a new divergence")),
+              "add_divergence", _("Create a new divergence")),
              (False, FREEDRAWING_MODE|DRIVENDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARJUMP, "OnJumpTool", 
-              "add_jump.png", _("Create a new jump")),
+              "add_jump", _("Create a new jump")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARVARIABLE, "OnVariableTool", 
-              "add_variable.png", _("Create a new variable")),
+              "add_variable", _("Create a new variable")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARBLOCK, "OnBlockTool", 
-              "add_block.png", _("Create a new block")),
+              "add_block", _("Create a new block")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCONNECTION, "OnConnectionTool", 
-              "add_connection.png", _("Create a new connection")),
+              "add_connection", _("Create a new connection")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARPOWERRAIL, "OnPowerRailTool", 
-              "add_powerrail.png", _("Create a new power rail")),
+              "add_powerrail", _("Create a new power rail")),
              (True, FREEDRAWING_MODE, 
               ID_PLCOPENEDITOREDITORTOOLBARCONTACT, "OnContactTool", 
-              "add_contact.png", _("Create a new contact"))],
+              "add_contact", _("Create a new contact"))],
     "ST"  : [],
     "IL"  : [],
     "debug": [(True, FREEDRAWING_MODE|DRIVENDRAWING_MODE,
               ID_PLCOPENEDITOREDITORTOOLBARMOTION, "OnMotionTool",
-              "move.png", _("Move the view"))],
+              "move", _("Move the view"))],
 }
 
 #-------------------------------------------------------------------------------
@@ -460,14 +464,14 @@
         self.Bind(wx.EVT_MENU, self.OnSelectAllMenu, id=wx.ID_SELECTALL)
         self.Bind(wx.EVT_MENU, self.OnDeleteMenu, id=wx.ID_DELETE)
         
-        self.AddToMenuToolBar([(wx.ID_UNDO, "undo.png", _(u'Undo'), None),
-                               (wx.ID_REDO, "redo.png", _(u'Redo'), None),
+        self.AddToMenuToolBar([(wx.ID_UNDO, "undo", _(u'Undo'), None),
+                               (wx.ID_REDO, "redo", _(u'Redo'), None),
                                None,
-                               (wx.ID_CUT, "cut.png", _(u'Cut'), None),
-                               (wx.ID_COPY, "copy.png", _(u'Copy'), None),
-                               (wx.ID_PASTE, "paste.png", _(u'Paste'), None),
+                               (wx.ID_CUT, "cut", _(u'Cut'), None),
+                               (wx.ID_COPY, "copy", _(u'Copy'), None),
+                               (wx.ID_PASTE, "paste", _(u'Paste'), None),
                                None,
-                               (ID_PLCOPENEDITOREDITMENUSEARCHINPROJECT, "find.png", _(u'Search in Project'), None)])
+                               (ID_PLCOPENEDITOREDITMENUSEARCHINPROJECT, "find", _(u'Search in Project'), None)])
 
     def _init_coll_DisplayMenu_Items(self, parent):
         AppendMenu(parent, help='', id=wx.ID_REFRESH,
@@ -586,7 +590,7 @@
                   name='ProjectTree', parent=self.ProjectPanel, 
                   pos=wx.Point(0, 0), size=wx.Size(0, 0),
                   style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_EDIT_LABELS)
-        self.ProjectTree.SetBackgroundBitmap(wx.Bitmap(os.path.join(CWD, 'Images', 'custom_tree_background.png')),
+        self.ProjectTree.SetBackgroundBitmap(GetBitmap("custom_tree_background"),
 											 wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM)
         add_menu = wx.Menu()
         self._init_coll_AddMenu_Items(add_menu)
@@ -639,7 +643,7 @@
                 wx.TB_FLAT | wx.TB_NODIVIDER | wx.NO_BORDER)
         EditorToolBar.SetToolBitmapSize(wx.Size(25, 25))
         EditorToolBar.AddRadioTool(ID_PLCOPENEDITOREDITORTOOLBARSELECTION, 
-              wx.Bitmap(os.path.join(CWD, 'Images', 'select.png')), wx.NullBitmap, _("Select an object"))
+              GetBitmap("select"), wx.NullBitmap, _("Select an object"))
         EditorToolBar.Realize()
         self.Panes["EditorToolBar"] = EditorToolBar
         self.AUIManager.AddPane(EditorToolBar, wx.aui.AuiPaneInfo().
@@ -670,7 +674,7 @@
         self.SetMenuBar(self.MenuBar)
                 
         if self.EnableDebug:
-            self.DebugVariablePanel = DebugVariablePanel(self.RightNoteBook, self, self.Controler)
+            self.DebugVariablePanel = DebugVariablePanel(self.RightNoteBook, self.Controler)
             self.MainTabs["DebugVariablePanel"] = (self.DebugVariablePanel, _("Debugger"))
             self.RightNoteBook.AddPage(*self.MainTabs["DebugVariablePanel"])
         
@@ -694,7 +698,7 @@
         
         # Icons for languages
         for language in LANGUAGES:
-            self.TreeImageDict[language]=self.TreeImageList.Add(wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%language)))
+            self.TreeImageDict[language] = self.TreeImageList.Add(GetBitmap(language))
             
         # Icons for other items
         for imgname, itemtype in [
@@ -724,7 +728,7 @@
             ("CONFIGURATIONS", ITEM_CONFIGURATIONS),
             ("RESOURCES",      ITEM_RESOURCES),
             ("PROPERTIES",     ITEM_PROPERTIES)]:
-            self.TreeImageDict[itemtype]=self.TreeImageList.Add(wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%imgname)))
+            self.TreeImageDict[itemtype] = self.TreeImageList.Add(GetBitmap(imgname))
         
         # Assign icon list to TreeCtrls
         self.ProjectTree.SetImageList(self.TreeImageList)
@@ -1088,42 +1092,6 @@
 #-------------------------------------------------------------------------------
 #                            Notebook Unified Functions
 #-------------------------------------------------------------------------------
-
-    ## Function that generate bitmap for
-    # for wx.aui.AUINotebook.
-    #  @param window Panel to display in tab.
-    #  @param text title for the tab ctrl.
-    def GenerateBitmap(self, icon1_name, icon2_name = None):
-        # Find index of bitmap if already created
-        index = self.TabsImageListIndexes.get((icon1_name, icon2_name), None)
-        # Return index or bitmap if found
-        if index is not None:
-            return self.TabsImageList.GetBitmap(index)
-        if icon2_name is None:
-            # Bitmap with only one icon
-            bitmap = wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%icon1_name))
-        else:
-            # Bitmap with two icon
-            icon1 = wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%icon1_name))
-            icon2 = wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%icon2_name))
-            
-            # Calculate bitmap size
-            width = icon1.GetWidth() + icon2.GetWidth() - 1
-            height = max(icon1.GetHeight(), icon2.GetHeight())
-            # Create bitmap with both icons
-            bitmap = wx.EmptyBitmap(width, height)
-            dc = wx.MemoryDC()
-            dc.SelectObject(bitmap)
-            dc.Clear()
-            dc.DrawBitmap(icon1, 0, 0)
-            dc.DrawBitmap(icon2, icon1.GetWidth() - 1, 0)
-            dc.Destroy()
-            
-            # Store bitmap in ImageList
-            index = self.TabsImageList.Add(bitmap)
-            # Save bitmap index in ImageList in dictionary
-            self.TabsImageListIndexes[(icon1_name, icon2_name)] = index
-        return bitmap
     
     ## Function that add a tab in Notebook, calling refresh for tab DClick event
     # for wx.aui.AUINotebook.
@@ -1196,7 +1164,7 @@
         self.ProjectTree.Enable(False)
         self.PouInstanceVariablesPanel.ResetView()
         self.LibraryPanel.ResetTree()
-        self.LibraryPanel.SetControler(None)
+        self.LibraryPanel.SetController(None)
         self.Controler = None
 
     def OnCloseTabMenu(self, event):
@@ -1603,10 +1571,10 @@
         if infos["type"] == ITEM_POU:
             self.ProjectTree.SetItemImage(root, self.TreeImageDict[self.Controler.GetPouBodyType(infos["name"])])
         elif infos.has_key("icon") and infos["icon"] is not None:
-            icon_path = infos["icon"]
-            if not self.TreeImageDict.has_key(icon_path):
-                self.TreeImageDict[icon_path] = self.TreeImageList.Add(wx.Bitmap(icon_path))
-            self.ProjectTree.SetItemImage(root, self.TreeImageDict[icon_path])
+            icon_name = infos["icon"]
+            if not self.TreeImageDict.has_key(icon_name):
+                self.TreeImageDict[icon_name] = self.TreeImageList.Add(GetBitmap(icon_name))
+            self.ProjectTree.SetItemImage(root, self.TreeImageDict[icon_name])
         elif self.TreeImageDict.has_key(infos["type"]):
             self.ProjectTree.SetItemImage(root, self.TreeImageDict[infos["type"]])      
         
@@ -1853,11 +1821,11 @@
             new_window = None
             if element == ITEM_CONFIGURATION:
                 new_window = ConfigurationEditor(self.TabsOpened, tagname, self, self.Controler)
-                new_window.SetIcon(self.GenerateBitmap("CONFIGURATION"))
+                new_window.SetIcon(GetBitmap("CONFIGURATION"))
                 self.AddPage(new_window, "")
             elif element == ITEM_RESOURCE:
                 new_window = ResourceEditor(self.TabsOpened, tagname, self, self.Controler)
-                new_window.SetIcon(self.GenerateBitmap("RESOURCE"))
+                new_window.SetIcon(GetBitmap("RESOURCE"))
                 self.AddPage(new_window, "")
             elif element in [ITEM_POU, ITEM_TRANSITION, ITEM_ACTION]:
                 bodytype = self.Controler.GetEditedElementBodyType(tagname)
@@ -1879,16 +1847,16 @@
                         new_window.SetKeywords(ST_KEYWORDS)
                 if element == ITEM_POU:
                     pou_type = self.Controler.GetEditedElementType(tagname)[1].upper()
-                    icon = self.GenerateBitmap(pou_type, bodytype)
+                    icon = GetBitmap(pou_type, bodytype)
                 elif element == ITEM_TRANSITION:
-                    icon = self.GenerateBitmap("TRANSITION", bodytype)
+                    icon = GetBitmap("TRANSITION", bodytype)
                 elif element == ITEM_ACTION:
-                    icon = self.GenerateBitmap("ACTION", bodytype)
+                    icon = GetBitmap("ACTION", bodytype)
                 new_window.SetIcon(icon)
                 self.AddPage(new_window, "")
             elif element == ITEM_DATATYPE:
                 new_window = DataTypeEditor(self.TabsOpened, tagname, self, self.Controler)
-                new_window.SetIcon(self.GenerateBitmap("DATATYPE"))
+                new_window.SetIcon(GetBitmap("DATATYPE"))
                 self.AddPage(new_window, "")
             elif isinstance(element, EditorPanel):
                 new_window = element
@@ -2098,11 +2066,11 @@
                 
                 if instance_category in [ITEM_FUNCTIONBLOCK, ITEM_PROGRAM]:
                     pou_type = self.Controler.GetEditedElementType(instance_type, True)[1].upper()
-                    icon = self.GenerateBitmap(pou_type, bodytype)
+                    icon = GetBitmap(pou_type, bodytype)
                 elif instance_category == ITEM_TRANSITION:
-                    icon = self.GenerateBitmap("TRANSITION", bodytype)
+                    icon = GetBitmap("TRANSITION", bodytype)
                 elif instance_category == ITEM_ACTION:
-                    icon = self.GenerateBitmap("ACTION", bodytype)
+                    icon = GetBitmap("ACTION", bodytype)
                 new_window.SetIcon(icon)
                 self.AddPage(new_window, "")
                 new_window.RefreshView()
@@ -2114,6 +2082,7 @@
 
     def OpenGraphicViewer(self, var_path):
         new_window = GraphicViewer(self.TabsOpened, self, self.Controler, var_path)
+        new_window.SetIcon(GetBitmap("GRAPH"))
         self.AddPage(new_window, "")
         new_window.RefreshView()
         new_window.SetFocus()
@@ -2167,7 +2136,7 @@
                 MenuToolBar.AddSeparator()
             else:
                 id, bitmap, help, callback = toolbar_item
-                MenuToolBar.AddSimpleTool(id=id, shortHelpString=help, bitmap=wx.Bitmap(os.path.join(CWD, 'Images', bitmap)))
+                MenuToolBar.AddSimpleTool(id=id, shortHelpString=help, bitmap=GetBitmap(bitmap))
                 if callback is not None:
                     self.Bind(wx.EVT_TOOL, callback, id=id)
         MenuToolBar.Realize()
@@ -2210,9 +2179,9 @@
                 for radio, modes, id, method, picture, help in EditorToolBarItems[menu]:
                     if modes & self.DrawingMode:
                         if radio or self.DrawingMode == FREEDRAWING_MODE:
-                            EditorToolBar.AddRadioTool(id, wx.Bitmap(os.path.join(CWD, "Images", picture)), wx.NullBitmap, help)
+                            EditorToolBar.AddRadioTool(id, GetBitmap(picture), wx.NullBitmap, help)
                         else:
-                            EditorToolBar.AddSimpleTool(id, wx.Bitmap(os.path.join(CWD, "Images", picture)), help)
+                            EditorToolBar.AddSimpleTool(id, GetBitmap(picture), help)
                         self.Bind(wx.EVT_MENU, getattr(self, method), id=id)
                         self.CurrentEditorToolBar.append(id)
                 EditorToolBar.Realize()
@@ -2667,11 +2636,11 @@
         self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES)
         self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT)
         
-        self.AddToMenuToolBar([(wx.ID_NEW, "new.png", _(u'New'), None),
-                               (wx.ID_OPEN, "open.png", _(u'Open'), None),
-                               (wx.ID_SAVE, "save.png", _(u'Save'), None),
-                               (wx.ID_SAVEAS, "saveas.png", _(u'Save As...'), None),
-                               (wx.ID_PRINT, "print.png", _(u'Print'), None)])
+        self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None),
+                               (wx.ID_OPEN, "open", _(u'Open'), None),
+                               (wx.ID_SAVE, "save", _(u'Save'), None),
+                               (wx.ID_SAVEAS, "saveas", _(u'Save As...'), None),
+                               (wx.ID_PRINT, "print", _(u'Print'), None)])
             
     def _init_coll_HelpMenu_Items(self, parent):
         AppendMenu(parent, help='', id=wx.ID_HELP, 
@@ -2703,13 +2672,13 @@
             result = controler.OpenXMLFile(fileOpen)
             if result is None:
                 self.Controler = controler
-            	self.LibraryPanel.SetControler(controler)
+            	self.LibraryPanel.SetController(controler)
                 self.ProjectTree.Enable(True)
                 self.PouInstanceVariablesPanel.SetController(controler)
                 self._Refresh(PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE)
         
         # Define PLCOpenEditor icon
-        self.SetIcon(wx.Icon(os.path.join(CWD,"Images", "poe.ico"),wx.BITMAP_TYPE_ICO))
+        self.SetIcon(wx.Icon(os.path.join(CWD, "Images", "poe.ico"),wx.BITMAP_TYPE_ICO))
 
         self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
         
@@ -2794,7 +2763,7 @@
             self.ResetView()
             self.Controler = PLCControler()
             self.Controler.CreateNewProject(properties)
-            self.LibraryPanel.SetControler(self.Controler)
+            self.LibraryPanel.SetController(self.Controler)
             self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, 
                           LIBRARYTREE)
 
@@ -2820,7 +2789,7 @@
                 result = controler.OpenXMLFile(filepath)
                 if result is None:
                     self.Controler = controler
-                    self.LibraryPanel.SetControler(controler)
+                    self.LibraryPanel.SetController(controler)
                     self.ProjectTree.Enable(True)
                     self.PouInstanceVariablesPanel.SetController(controler)
                     self.LoadProjectLayout()