dialogs/BrowseLocationsDialog.py
changeset 714 131ea7f237b9
parent 681 c141dad94ff4
child 729 a47f641e50f0
--- a/dialogs/BrowseLocationsDialog.py	Fri Jun 15 18:03:25 2012 +0200
+++ b/dialogs/BrowseLocationsDialog.py	Mon Jun 25 20:03:53 2012 +0200
@@ -24,6 +24,10 @@
 from plcopen.structures import LOCATIONDATATYPES
 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
 
+#-------------------------------------------------------------------------------
+#                                   Helpers
+#-------------------------------------------------------------------------------
+
 CWD = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
 
 def GetDirChoiceOptions():
@@ -40,88 +44,52 @@
     for type in types:
         LOCATION_SIZES[type] = size
 
-[ID_BROWSELOCATIONSDIALOG, ID_BROWSELOCATIONSDIALOGLOCATIONSTREE, 
- ID_BROWSELOCATIONSDIALOGDIRCHOICE, ID_BROWSELOCATIONSDIALOGSTATICTEXT1, 
- ID_BROWSELOCATIONSDIALOGSTATICTEXT2, 
-] = [wx.NewId() for _init_ctrls in range(5)]
+#-------------------------------------------------------------------------------
+#                            Browse Locations Dialog
+#-------------------------------------------------------------------------------
 
 class BrowseLocationsDialog(wx.Dialog):
     
-    if wx.VERSION < (2, 6, 0):
-        def Bind(self, event, function, id = None):
-            if id is not None:
-                event(self, id, function)
-            else:
-                event(self, function)
-    
-    def _init_coll_MainSizer_Items(self, parent):
-        parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
-        parent.AddWindow(self.LocationsTree, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW)
-        parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
+    def __init__(self, parent, var_type, locations):
+        wx.Dialog.__init__(self, parent,  
+              size=wx.Size(600, 400), title=_('Browse Locations'))
         
-    def _init_coll_MainSizer_Growables(self, parent):
-        parent.AddGrowableCol(0)
-        parent.AddGrowableRow(1)
-    
-    def _init_coll_ButtonGridSizer_Items(self, parent):
-        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL)
-        parent.AddWindow(self.DirChoice, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL)
-        parent.AddSizer(self.ButtonSizer, 0, border=0, flag=wx.ALIGN_RIGHT)
+        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
+        main_sizer.AddGrowableCol(0)
+        main_sizer.AddGrowableRow(1)
         
-    def _init_coll_ButtonGridSizer_Growables(self, parent):
-        parent.AddGrowableCol(2)
-        parent.AddGrowableRow(0)
-    
-    def _init_sizers(self):
-        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
-        self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
+        locations_label = wx.StaticText(self, label=_('Locations available:'))
+        main_sizer.AddWindow(locations_label, border=20, 
+              flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
         
-        self._init_coll_MainSizer_Items(self.MainSizer)
-        self._init_coll_MainSizer_Growables(self.MainSizer)
-        self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer)
-        self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer)
+        self.LocationsTree = wx.TreeCtrl(self, 
+              style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
+        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, 
+                  self.LocationsTree)
+        main_sizer.AddWindow(self.LocationsTree, border=20, 
+              flag=wx.LEFT|wx.RIGHT|wx.GROW)
         
-        self.SetSizer(self.MainSizer)
-    
-    def _init_ctrls(self, prnt):
-        wx.Dialog.__init__(self, id=ID_BROWSELOCATIONSDIALOG, 
-              name='BrowseLocationsDialog', parent=prnt,  
-              size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
-              title=_('Browse Locations'))
-    
-        self.staticText1 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT1,
-              label=_('Locations available:'), name='staticText1', parent=self,
-              pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
+        button_gridsizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
+        button_gridsizer.AddGrowableCol(2)
+        button_gridsizer.AddGrowableRow(0)
+        main_sizer.AddSizer(button_gridsizer, border=20, 
+              flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW)
         
-        if wx.Platform == '__WXMSW__':
-            treestyle = wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER
-        else:
-            treestyle = wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_SINGLE|wx.SUNKEN_BORDER
-        self.LocationsTree = wx.TreeCtrl(id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE,
-              name='LocationsTree', parent=self, pos=wx.Point(0, 0),
-              size=wx.Size(0, 0), style=treestyle)
-        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, 
-                  id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE)
+        direction_label = wx.StaticText(self, label=_('Direction:'))
+        button_gridsizer.AddWindow(direction_label,
+              flag=wx.ALIGN_CENTER_VERTICAL)
         
-        self.staticText2 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT2,
-              label=_('Direction:'), name='staticText2', parent=self,
-              pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
+        self.DirChoice = wx.ComboBox(self, style=wx.CB_READONLY)
+        self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, self.DirChoice)
+        button_gridsizer.AddWindow(self.DirChoice,
+              flag=wx.ALIGN_CENTER_VERTICAL)
         
-        self.DirChoice = wx.ComboBox(id=ID_BROWSELOCATIONSDIALOGDIRCHOICE,
-              name='DirChoice', parent=self, pos=wx.Point(0, 0),
-              size=wx.DefaultSize, style=wx.CB_READONLY)
-        self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, id=ID_BROWSELOCATIONSDIALOGDIRCHOICE)
+        button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
+        self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
+        button_gridsizer.AddWindow(button_sizer, flag=wx.ALIGN_RIGHT)
         
-        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
-        if wx.VERSION >= (2, 5, 0):
-            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
-        else:
-            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
+        self.SetSizer(main_sizer)
         
-        self._init_sizers()
-    
-    def __init__(self, parent, var_type, locations):
-        self._init_ctrls(parent)
         self.VarType = var_type
         self.Locations = locations
         
@@ -156,19 +124,13 @@
     def RefreshLocationsTree(self):
         root = self.LocationsTree.GetRootItem()
         if not root.IsOk():
-            if wx.Platform == '__WXMSW__':
-                root = self.LocationsTree.AddRoot(_('ConfNodes'))
-            else:
-                root = self.LocationsTree.AddRoot("")
+            root = self.LocationsTree.AddRoot("")
         self.GenerateLocationsTreeBranch(root, self.Locations)
         self.LocationsTree.Expand(root)
     
     def GenerateLocationsTreeBranch(self, root, locations):
         to_delete = []
-        if wx.VERSION >= (2, 6, 0):
-            item, root_cookie = self.LocationsTree.GetFirstChild(root)
-        else:
-            item, root_cookie = self.LocationsTree.GetFirstChild(root, 0)
+        item, root_cookie = self.LocationsTree.GetFirstChild(root)
         for loc_infos in locations:
             infos = loc_infos.copy()
             if infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP] or\
@@ -211,8 +173,8 @@
         if selected.IsOk():
             var_infos = self.LocationsTree.GetPyData(selected)
         if var_infos is None or var_infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]:
-            message = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
-            message.ShowModal()
-            message.Destroy()
+            dialog = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
+            dialog.ShowModal()
+            dialog.Destroy()
         else:
             self.EndModal(wx.ID_OK)