Fixed support for multiple module extra params in ModulesLibrary
authorLaurent Bessard
Fri, 26 Apr 2013 23:23:09 +0200
changeset 2138 79dc2d15c580
parent 2137 b65abacdbdf9
child 2139 1565239349a8
Fixed support for multiple module extra params in ModulesLibrary
etherlab/ConfigEditor.py
etherlab/etherlab.py
--- a/etherlab/ConfigEditor.py	Fri Apr 26 13:57:10 2013 +0200
+++ b/etherlab/ConfigEditor.py	Fri Apr 26 23:23:09 2013 +0200
@@ -1042,8 +1042,14 @@
                     wx.TR_COLUMN_LINES |
                     wx.TR_HIDE_ROOT |
                     wx.TR_FULL_ROW_HIGHLIGHT)
-        self.ModulesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DCLICK,
-            self.OnModulesGridLeftDClick)
+        self.ModulesGrid.GetMainWindow().Bind(wx.EVT_LEFT_DOWN,
+            self.OnModulesGridLeftDown)
+        self.ModulesGrid.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT,
+            self.OnModulesGridBeginLabelEdit)
+        self.ModulesGrid.Bind(wx.EVT_TREE_END_LABEL_EDIT,
+            self.OnModulesGridEndLabelEdit)
+        self.ModulesGrid.GetHeaderWindow().Bind(wx.EVT_MOTION, 
+            self.OnModulesGridHeaderMotion)
         self.AddWindow(self.ModulesGrid, border=10, 
             flag=wx.GROW|wx.BOTTOM|wx.LEFT|wx.RIGHT)
         
@@ -1051,10 +1057,15 @@
                 [_("Name")] + [param_infos["column_label"] 
                                for param, param_infos in 
                                self.ModuleLibrary.MODULES_EXTRA_PARAMS],
-                [400] + [150] * len(self.ModuleLibrary.MODULES_EXTRA_PARAMS),
+                [400] + [param_infos["column_size"] 
+                         for param, param_infos in 
+                         self.ModuleLibrary.MODULES_EXTRA_PARAMS],
                 [wx.ALIGN_LEFT] + [wx.ALIGN_RIGHT] * len(self.ModuleLibrary.MODULES_EXTRA_PARAMS)):
-            self.ModulesGrid.AddColumn(_(colname), colsize, colalign)
+            self.ModulesGrid.AddColumn(_(colname), colsize, colalign, edit=True)
         self.ModulesGrid.SetMainColumn(0)
+        
+        self.CurrentSelectedCol = None
+        self.LastToolTipCol = None
     
     def GetPath(self):
         return self.ModuleLibrary.GetPath()
@@ -1088,7 +1099,7 @@
                 item = self.ModulesGrid.AppendItem(root, "")
             self.ModulesGrid.SetItemText(item, module["name"], 0)
             if module["infos"] is not None:
-                for param_idx, (param, params_infos) in enumerate(self.ModuleLibrary.MODULES_EXTRA_PARAMS):
+                for param_idx, (param, param_infos) in enumerate(self.ModuleLibrary.MODULES_EXTRA_PARAMS):
                     self.ModulesGrid.SetItemText(item, 
                                                  str(module["infos"][param]), 
                                                  param_idx + 1)
@@ -1145,37 +1156,67 @@
                 wx.CallAfter(self.RefreshView)
         event.Skip()
     
-    def OnModulesGridLeftDClick(self, event):
+    def OnModulesGridLeftDown(self, event):
         item, flags, col = self.ModulesGrid.HitTest(event.GetPosition())
         if item.IsOk():
             entry_infos = self.ModulesGrid.GetItemPyData(item)
             if entry_infos is not None and col > 0:
-                param, param_infos = self.ModuleLibrary.MODULES_EXTRA_PARAMS[col - 1]
-                column_label = param_infos["column_label"]
-                stripped_column_label = column_label.split('(')[0].strip()
-                dialog = wx.TextEntryDialog(self.ParentWindow, 
-                    _("Set %s:") % column_label,
-                    self.ModulesGrid.GetItemText(item) + " " + stripped_column_label, 
-                    str(entry_infos[param]))
+                self.CurrentSelectedCol = col
+            else:
+                self.CurrentSelectedCol = None
+        else:
+            self.CurrentSelectedCol = None
+        event.Skip()
+
+    def OnModulesGridBeginLabelEdit(self, event):
+        item = event.GetItem()
+        if item.IsOk():
+            entry_infos = self.ModulesGrid.GetItemPyData(item)
+            if entry_infos is not None:
+                event.Skip()
+            else:
+                event.Veto()
+        else:
+            event.Veto()
+
+    def OnModulesGridEndLabelEdit(self, event):
+        item = event.GetItem()
+        if item.IsOk() and self.CurrentSelectedCol is not None:
+            entry_infos = self.ModulesGrid.GetItemPyData(item)
+            if entry_infos is not None and self.CurrentSelectedCol > 0:
+                param, param_infos = self.ModuleLibrary.MODULES_EXTRA_PARAMS[self.CurrentSelectedCol - 1]
+                stripped_column_label = param_infos["column_label"].split('(')[0].strip()
+                try:
+                    self.ModuleLibrary.SetModuleExtraParam(
+                        entry_infos["vendor"],
+                        entry_infos["product_code"],
+                        entry_infos["revision_number"],
+                        param,
+                        int(event.GetLabel()))
+                    wx.CallAfter(self.RefreshModulesGrid)
+                    event.Skip()
+                except ValueError:
+                    message = wx.MessageDialog(self, 
+                        _("Module %s must be an integer!") % stripped_column_label, 
+                        _("Error"), wx.OK|wx.ICON_ERROR)
+                    message.ShowModal()
+                    message.Destroy()
+                    event.Veto()
+            else:
+                event.Veto()
+        else:
+            event.Veto()
                 
-                if dialog.ShowModal() == wx.ID_OK:
-                    try:
-                        self.ModuleLibrary.SetModuleExtraParam(
-                            entry_infos["vendor"],
-                            entry_infos["product_code"],
-                            entry_infos["revision_number"],
-                            param,
-                            int(dialog.GetValue()))
-                        wx.CallAfter(self.RefreshModulesGrid)
-                    except ValueError:
-                        message = wx.MessageDialog(self, 
-                            _("Module %s must be an integer!") % stripped_column_label, 
-                            _("Error"), wx.OK|wx.ICON_ERROR)
-                        message.ShowModal()
-                        message.Destroy()
-                    
-                dialog.Destroy()
-        
+    def OnModulesGridHeaderMotion(self, event):
+        item, flags, col = self.ModulesGrid.HitTest(event.GetPosition())
+        if col != self.LastToolTipCol and self.LastToolTipCol is not None:
+            self.ModulesGrid.SetToolTip(None)
+            self.LastToolTipCol = None
+        if col > 0:
+            self.LastToolTipCol = col
+            param, param_infos = self.ModuleLibrary.MODULES_EXTRA_PARAMS[col - 1]
+            wx.CallAfter(self.ModulesGrid.SetToolTip,
+                         wx.ToolTip(param_infos["description"]))
         event.Skip()
 
 class DatabaseManagementDialog(wx.Dialog):
--- a/etherlab/etherlab.py	Fri Apr 26 13:57:10 2013 +0200
+++ b/etherlab/etherlab.py	Fri Apr 26 23:23:09 2013 +0200
@@ -236,14 +236,27 @@
 
     MODULES_EXTRA_PARAMS = [
         ("pdo_alignment", {
-            "column_label": _("PDO alignment (bits)"), 
-            "default": 8}),
+            "column_label": _("PDO alignment"), 
+            "column_size": 150,
+            "default": 8,
+            "description": _(
+"Minimal size in bits between 2 pdo entries")}),
         ("max_pdo_size", {
-            "column_label": _("Max entries by PDO (-1=unbounded)"), 
-            "default": -1}),
+            "column_label": _("Max entries by PDO"),
+            "column_size": 150,
+            "default": -1,
+            "description": _(
+"""Maximal number of entries mapped in a PDO
+including empty entries used for PDO alignment
+(-1=unbounded)""")}),
         ("add_pdo", {
-            "column_label": _("Creating new PDO (1=possible)"), 
-            "default": 0})
+            "column_label": _("Creating new PDO"), 
+            "column_size": 150,
+            "default": 0,
+            "description": _(
+"""Adding a PDO not defined in default configuration
+for mapping needed location variables
+(1 if possible)""")})
     ]
     
     def __init__(self, path, parent_library=None):
@@ -388,12 +401,13 @@
                 if has_header:
                     has_header = False
                 else:
-                    try:
-                        self.ModulesExtraParams[tuple(map(int, row[:3]))] = dict(
-                            zip([param for param, params_infos in self.MODULES_EXTRA_PARAMS], 
-                                int(row[3])))
-                    except:
-                        pass
+                    params_values = {}
+                    for (param, param_infos), value in zip(
+                        self.MODULES_EXTRA_PARAMS, row[3:]):
+                        if value != "":
+                            params_values[param] = int(value)
+                    self.ModulesExtraParams[
+                        tuple(map(int, row[:3]))] = params_values
             csvfile.close()
     
     def SaveModulesExtraParams(self):
@@ -412,7 +426,10 @@
         product_code = ExtractHexDecValue(product_code)
         revision_number = ExtractHexDecValue(revision_number)
         
-        self.ModulesExtraParams[tuple([vendor, product_code, revision_number])][param] = value
+        module_infos = (vendor, product_code, revision_number)
+        self.ModulesExtraParams.setdefault(module_infos, {})
+        self.ModulesExtraParams[module_infos][param] = value
+        
         self.SaveModulesExtraParams()
     
     def GetModuleExtraParams(self, vendor, product_code, revision_number):
@@ -425,7 +442,7 @@
         else:
             extra_params = {}
         
-        extra_params.update(self.ModulesExtraParams.get(tuple([vendor, product_code, revision_number]), {}))
+        extra_params.update(self.ModulesExtraParams.get((vendor, product_code, revision_number), {}))
         
         for param, param_infos in self.MODULES_EXTRA_PARAMS:
             extra_params.setdefault(param, param_infos["default"])