Various cleanup and optimization
authorEdouard Tisserant
Fri, 02 Aug 2013 08:55:45 +0900
changeset 1281 47131e3388f4
parent 1279 0eb9f8af479f
child 1282 f427352f9727
Various cleanup and optimization
- Deleted shameful dead code here and here...
- Accelerated creation of GenBitmap*Button adding some caching for bitmap grayOut
- Replaced lists inside type use dictionary by sets
- Prevented useless type usage checking when unnecessary
Beremiz.py
controls/PouInstanceVariablesPanel.py
editors/ConfTreeNodeEditor.py
plcopen/plcopen.py
--- a/Beremiz.py	Fri Jul 26 14:04:29 2013 +0900
+++ b/Beremiz.py	Fri Aug 02 08:55:45 2013 +0900
@@ -155,29 +155,6 @@
 
 MAX_RECENT_PROJECTS = 10
 
-class GenStaticBitmap(wx.lib.statbmp.GenStaticBitmap):
-    """ Customized GenStaticBitmap, fix transparency redraw bug on wx2.8/win32, 
-    and accept image name as __init__ parameter, fail silently if file do not exist"""
-    def __init__(self, parent, ID, bitmapname,
-                 pos = wx.DefaultPosition, size = wx.DefaultSize,
-                 style = 0,
-                 name = "genstatbmp"):
-        
-        wx.lib.statbmp.GenStaticBitmap.__init__(self, parent, ID, 
-                 GetBitmap(bitmapname),
-                 pos, size,
-                 style,
-                 name)
-        
-    def OnPaint(self, event):
-        dc = wx.PaintDC(self)
-        colour = self.GetParent().GetBackgroundColour()
-        dc.SetPen(wx.Pen(colour))
-        dc.SetBrush(wx.Brush(colour ))
-        dc.DrawRectangle(0, 0, *dc.GetSizeTuple())
-        if self._bitmap:
-            dc.DrawBitmap(self._bitmap, 0, 0, True)
-
 if wx.Platform == '__WXMSW__':
     faces = {
         'mono' : 'Courier New',
@@ -300,14 +277,7 @@
     def isatty(self):
         return False
 
-[ID_BEREMIZ, ID_BEREMIZMAINSPLITTER, 
- ID_BEREMIZPLCCONFIG, ID_BEREMIZLOGCONSOLE, 
- ID_BEREMIZINSPECTOR] = [wx.NewId() for _init_ctrls in range(5)]
-
-[ID_FILEMENURECENTPROJECTS,
-] = [wx.NewId() for _init_ctrls in range(1)]
-
-CONFNODEMENU_POSITION = 3
+ID_FILEMENURECENTPROJECTS = wx.NewId()
 
 class Beremiz(IDEFrame):
     
@@ -394,8 +364,9 @@
         
         self.EditMenuSize = self.EditMenu.GetMenuItemCount()
         
-        self.Bind(wx.EVT_MENU, self.OnOpenWidgetInspector, id=ID_BEREMIZINSPECTOR)
-        accels = [wx.AcceleratorEntry(wx.ACCEL_CTRL|wx.ACCEL_ALT, ord('I'), ID_BEREMIZINSPECTOR)]
+        inspectorID = wx.NewId()
+        self.Bind(wx.EVT_MENU, self.OnOpenWidgetInspector, id=inspectorID)
+        accels = [wx.AcceleratorEntry(wx.ACCEL_CTRL|wx.ACCEL_ALT, ord('I'), inspectorID)]
         for method,shortcut in [("Stop",     wx.WXK_F4),
                                 ("Run",      wx.WXK_F5),
                                 ("Transfer", wx.WXK_F6),
@@ -413,7 +384,7 @@
         
         self.SetAcceleratorTable(wx.AcceleratorTable(accels))
         
-        self.LogConsole = CustomStyledTextCtrl(id=ID_BEREMIZLOGCONSOLE,
+        self.LogConsole = CustomStyledTextCtrl(
                   name='LogConsole', parent=self.BottomNoteBook, pos=wx.Point(0, 0),
                   size=wx.Size(0, 0))
         self.LogConsole.Bind(wx.EVT_SET_FOCUS, self.OnLogConsoleFocusChanged)
--- a/controls/PouInstanceVariablesPanel.py	Fri Jul 26 14:04:29 2013 +0900
+++ b/controls/PouInstanceVariablesPanel.py	Fri Aug 02 08:55:45 2013 +0900
@@ -23,8 +23,29 @@
 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import wx
+import wx.lib.agw.customtreectrl as CT
+
+import wx.lib.imageutils
 import wx.lib.buttons
-import wx.lib.agw.customtreectrl as CT
+GrayedBitmapCache = {}
+def SetBitmapLabel(self, bitmap, createOthers=True):
+    """
+    Set the bitmap to display normally.
+    This is the only one that is required. If
+    createOthers is True, then the other bitmaps
+    will be generated on the fly.  Currently,
+    only the disabled bitmap is generated.
+    """
+    self.bmpLabel = bitmap
+    if bitmap is not None and createOthers:
+        GrayBitmap=GrayedBitmapCache.get(bitmap,None)
+        if GrayBitmap is None:
+            image = wx.ImageFromBitmap(bitmap)
+            wx.lib.imageutils.grayOut(image)
+            GrayBitmap = wx.BitmapFromImage(image)
+            GrayedBitmapCache[bitmap] = GrayBitmap
+        self.SetBitmapDisabled(GrayBitmap)
+wx.lib.buttons.GenBitmapButton.SetBitmapLabel = SetBitmapLabel
 
 try:
     import matplotlib
--- a/editors/ConfTreeNodeEditor.py	Fri Jul 26 14:04:29 2013 +0900
+++ b/editors/ConfTreeNodeEditor.py	Fri Aug 02 08:55:45 2013 +0900
@@ -3,7 +3,6 @@
 import types
 
 import wx
-import wx.lib.buttons
 
 from EditorPanel import EditorPanel
 
@@ -35,22 +34,6 @@
 def Bpath(*args):
     return os.path.join(CWD,*args)
 
-# Patch wx.lib.imageutils so that gray is supported on alpha images
-import wx.lib.imageutils
-from wx.lib.imageutils import grayOut as old_grayOut
-def grayOut(anImage):
-    if anImage.HasAlpha():
-        AlphaData = anImage.GetAlphaData()
-    else :
-        AlphaData = None
-
-    old_grayOut(anImage)
-
-    if AlphaData is not None:
-        anImage.SetAlphaData(AlphaData)
-
-wx.lib.imageutils.grayOut = grayOut
-
 class GenBitmapTextButton(wx.lib.buttons.GenBitmapTextButton):
     def _GetLabelSize(self):
         """ used internally """
--- a/plcopen/plcopen.py	Fri Jul 26 14:04:29 2013 +0900
+++ b/plcopen/plcopen.py	Fri Aug 02 08:55:45 2013 +0900
@@ -502,10 +502,8 @@
 
     def AddElementUsingTreeInstance(self, name, type_infos):
         typename = type_infos.getname()
-        if not self.ElementUsingTree.has_key(typename):
-            self.ElementUsingTree[typename] = [name]
-        elif name not in self.ElementUsingTree[typename]:
-            self.ElementUsingTree[typename].append(name)
+        elements = self.ElementUsingTree.setdefault(typename, set())
+        elements.add(name)
     setattr(cls, "AddElementUsingTreeInstance", AddElementUsingTreeInstance)
     
     def RefreshElementUsingTree(self):
@@ -518,9 +516,8 @@
             name = datatype.getname()
             basetype_content = datatype.baseType.getcontent()
             if basetype_content["name"] == "derived":
-                typename = basetype_content["value"].getname()
-                if name in self.ElementUsingTree[typename]:
-                    self.ElementUsingTree[typename].append(name)
+                self.AddElementUsingTreeInstance(name,
+                                                 basetype_content["value"])
             elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]:
                 base_type = basetype_content["value"].baseType.getcontent()
                 if base_type["name"] == "derived":
@@ -540,9 +537,6 @@
                         vartype_content = var.gettype().getcontent()
                         if vartype_content["name"] == "derived":
                             self.AddElementUsingTreeInstance(name, vartype_content["value"])
-            for typename in self.ElementUsingTree.iterkeys():
-                if typename != name and pou.hasblock(block_type=typename) and name not in self.ElementUsingTree[typename]:
-                    self.ElementUsingTree[typename].append(name)
         
     setattr(cls, "RefreshElementUsingTree", RefreshElementUsingTree)
 
@@ -591,9 +585,8 @@
 
     # Return if pou given by name is used by another pou
     def ElementIsUsed(self, name):
-        if self.ElementUsingTree.has_key(name):
-            return len(self.ElementUsingTree[name]) > 0
-        return False
+        elements = self.ElementUsingTree.get(name, None)
+        return elements is not None
     setattr(cls, "ElementIsUsed", ElementIsUsed)
 
     def DataTypeIsDerived(self, name):
@@ -602,18 +595,17 @@
 
     # Return if pou given by name is directly or undirectly used by the reference pou
     def ElementIsUsedBy(self, name, reference):
-        if self.ElementUsingTree.has_key(name):
-            list = self.ElementUsingTree[name]
-            # Test if pou is directly used by reference
-            if reference in list:
-                return True
-            else:
-                # Test if pou is undirectly used by reference, by testing if pous 
-                # that directly use pou is directly or undirectly used by reference
-                used = False
-                for element in list:
-                    used |= self.ElementIsUsedBy(element, reference)
-                return used
+        elements = self.ElementUsingTree.get(name, set())
+        # Test if pou is directly used by reference
+        if reference in elements:
+            return True
+        else:
+            # Test if pou is undirectly used by reference, by testing if pous
+            # that directly use pou is directly or undirectly used by reference
+            selffn = self.ElementIsUsedBy
+            for element in elements:
+                if selffn(element, reference):
+                    return True
         return False
     setattr(cls, "ElementIsUsedBy", ElementIsUsedBy)
 
@@ -654,17 +646,16 @@
     setattr(cls, "GetCustomBlockType", GetCustomBlockType)
 
     # Return Block types checking for recursion
-    def GetCustomBlockTypes(self, exclude = "", onlyfunctions = False):
-        type = None
-        if exclude != "":
-            pou = self.getpou(exclude)
-            if pou is not None:
-                type = pou.getpouType()
-        customblocktypes = []
-        for customblocktype in self.CustomBlockTypes:
-            if customblocktype["type"] != "program" and customblocktype["name"] != exclude and not self.ElementIsUsedBy(exclude, customblocktype["name"]) and not (onlyfunctions and customblocktype["type"] != "function"):
-                customblocktypes.append(customblocktype)
-        return customblocktypes
+    def GetCustomBlockTypes(self, exclude = None, onlyfunctions = False):
+        if exclude is not None:
+            return [customblocktype for customblocktype in self.CustomBlockTypes
+                if (customblocktype["type"] != "program"
+                    and customblocktype["name"] != exclude
+                    and not self.ElementIsUsedBy(exclude, customblocktype["name"])
+                    and not (onlyfunctions and customblocktype["type"] != "function"))]
+        return [customblocktype for customblocktype in self.CustomBlockTypes
+            if (customblocktype["type"] != "program"
+                and not (onlyfunctions and customblocktype["type"] != "function"))]
     setattr(cls, "GetCustomBlockTypes", GetCustomBlockTypes)
 
     # Return Function Block types checking for recursion