dialogs/ActionBlockDialog.py
changeset 1784 64beb9e9c749
parent 1782 5b6ad7a7fd9d
child 1834 cd42b426028b
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    27 import wx.lib.buttons
    27 import wx.lib.buttons
    28 
    28 
    29 from controls import CustomGrid, CustomTable
    29 from controls import CustomGrid, CustomTable
    30 from util.BitmapLibrary import GetBitmap
    30 from util.BitmapLibrary import GetBitmap
    31 from PLCControler import _ActionInfos
    31 from PLCControler import _ActionInfos
    32 
    32 from util.TranslationCatalogs import NoTranslate
    33 #-------------------------------------------------------------------------------
    33 # -------------------------------------------------------------------------------
    34 #                                  Helpers
    34 #                                  Helpers
    35 #-------------------------------------------------------------------------------
    35 # -------------------------------------------------------------------------------
       
    36 
    36 
    37 
    37 def GetActionTableColnames():
    38 def GetActionTableColnames():
    38     _ = lambda x: x
    39     _ = NoTranslate
    39     return [_("Qualifier"), _("Duration"), _("Type"), _("Value"), _("Indicator")]
    40     return [_("Qualifier"), _("Duration"), _("Type"), _("Value"), _("Indicator")]
    40 
    41 
       
    42 
    41 def GetTypeList():
    43 def GetTypeList():
    42     _ = lambda x: x
    44     _ = NoTranslate
    43     return [_("Action"), _("Variable"), _("Inline")]
    45     return [_("Action"), _("Variable"), _("Inline")]
    44 
    46 
    45 #-------------------------------------------------------------------------------
    47 # -------------------------------------------------------------------------------
    46 #                               Action Table
    48 #                               Action Table
    47 #-------------------------------------------------------------------------------
    49 # -------------------------------------------------------------------------------
       
    50 
    48 
    51 
    49 class ActionTable(CustomTable):
    52 class ActionTable(CustomTable):
    50     
    53 
    51     def GetValue(self, row, col):
    54     def GetValue(self, row, col):
    52         if row < self.GetNumberRows():
    55         if row < self.GetNumberRows():
    53             colname = self.GetColLabelValue(col, False)
    56             colname = self.GetColLabelValue(col, False)
    54             value = getattr(self.data[row], colname.lower())
    57             value = getattr(self.data[row], colname.lower())
    55             if colname == "Type":
    58             if colname == "Type":
    56                 return _(value)
    59                 return _(value)
    57             return value
    60             return value
    58     
    61 
    59     def SetValue(self, row, col, value):
    62     def SetValue(self, row, col, value):
    60         if col < len(self.colnames):
    63         if col < len(self.colnames):
    61             colname = self.GetColLabelValue(col, False)
    64             colname = self.GetColLabelValue(col, False)
    62             if colname == "Type":
    65             if colname == "Type":
    63                 value = self.Parent.TranslateType[value]
    66                 value = self.Parent.TranslateType[value]
    64             elif colname == "Qualifier" and not self.Parent.DurationList[value]:
    67             elif colname == "Qualifier" and not self.Parent.DurationList[value]:
    65                 self.data[row].duration = ""
    68                 self.data[row].duration = ""
    66             setattr(self.data[row], colname.lower(), value)
    69             setattr(self.data[row], colname.lower(), value)
    67         
    70 
    68     def _updateColAttrs(self, grid):
    71     def _updateColAttrs(self, grid):
    69         """
    72         """
    70         wx.Grid -> update the column attributes to add the
    73         wx.Grid -> update the column attributes to add the
    71         appropriate renderer given the column name.
    74         appropriate renderer given the column name.
    72 
    75 
    73         Otherwise default to the default renderer.
    76         Otherwise default to the default renderer.
    74         """
    77         """
    75         
    78 
    76         for row in range(self.GetNumberRows()):
    79         for row in range(self.GetNumberRows()):
    77             for col in range(self.GetNumberCols()):
    80             for col in range(self.GetNumberCols()):
    78                 editor = None
    81                 editor = None
    79                 renderer = None
    82                 renderer = None
    80                 readonly = False
    83                 readonly = False
   101                         editor = wx.grid.GridCellTextEditor()
   104                         editor = wx.grid.GridCellTextEditor()
   102                         renderer = wx.grid.GridCellStringRenderer()
   105                         renderer = wx.grid.GridCellStringRenderer()
   103                 elif colname == "Indicator":
   106                 elif colname == "Indicator":
   104                     editor = wx.grid.GridCellChoiceEditor()
   107                     editor = wx.grid.GridCellChoiceEditor()
   105                     editor.SetParameters(self.Parent.VariableList)
   108                     editor.SetParameters(self.Parent.VariableList)
   106                     
   109 
   107                 grid.SetCellEditor(row, col, editor)
   110                 grid.SetCellEditor(row, col, editor)
   108                 grid.SetCellRenderer(row, col, renderer)
   111                 grid.SetCellRenderer(row, col, renderer)
   109                 grid.SetReadOnly(row, col, readonly)
   112                 grid.SetReadOnly(row, col, readonly)
   110                 
   113 
   111                 grid.SetCellBackgroundColour(row, col, wx.WHITE)
   114                 grid.SetCellBackgroundColour(row, col, wx.WHITE)
   112             self.ResizeRow(grid, row)
   115             self.ResizeRow(grid, row)
   113 
   116 
   114 #-------------------------------------------------------------------------------
   117 # -------------------------------------------------------------------------------
   115 #                            Action Block Dialog
   118 #                            Action Block Dialog
   116 #-------------------------------------------------------------------------------
   119 # -------------------------------------------------------------------------------
       
   120 
   117 
   121 
   118 class ActionBlockDialog(wx.Dialog):
   122 class ActionBlockDialog(wx.Dialog):
   119     
   123 
   120     def __init__(self, parent):
   124     def __init__(self, parent):
   121         wx.Dialog.__init__(self, parent, title=_('Edit action block properties'))
   125         wx.Dialog.__init__(self, parent, title=_('Edit action block properties'))
   122         
   126 
   123         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
   127         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
   124         main_sizer.AddGrowableCol(0)
   128         main_sizer.AddGrowableCol(0)
   125         main_sizer.AddGrowableRow(1)
   129         main_sizer.AddGrowableRow(1)
   126         
   130 
   127         top_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
   131         top_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
   128         top_sizer.AddGrowableCol(0)
   132         top_sizer.AddGrowableCol(0)
   129         top_sizer.AddGrowableRow(0)
   133         top_sizer.AddGrowableRow(0)
   130         main_sizer.AddSizer(top_sizer, border=20,
   134         main_sizer.AddSizer(top_sizer, border=20,
   131               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
   135                             flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
   132         
   136 
   133         actions_label = wx.StaticText(self, label=_('Actions:'))
   137         actions_label = wx.StaticText(self, label=_('Actions:'))
   134         top_sizer.AddWindow(actions_label, flag=wx.ALIGN_BOTTOM)
   138         top_sizer.AddWindow(actions_label, flag=wx.ALIGN_BOTTOM)
   135         
   139 
   136         for name, bitmap, help in [
   140         for name, bitmap, help in [
   137                 ("AddButton", "add_element", _("Add action")),
   141                 ("AddButton", "add_element", _("Add action")),
   138                 ("DeleteButton", "remove_element", _("Remove action")),
   142                 ("DeleteButton", "remove_element", _("Remove action")),
   139                 ("UpButton", "up", _("Move action up")),
   143                 ("UpButton", "up", _("Move action up")),
   140                 ("DownButton", "down", _("Move action down"))]:
   144                 ("DownButton", "down", _("Move action down"))]:
   141             button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), 
   145             button = wx.lib.buttons.GenBitmapButton(
   142                   size=wx.Size(28, 28), style=wx.NO_BORDER)
   146                 self, bitmap=GetBitmap(bitmap),
       
   147                 size=wx.Size(28, 28), style=wx.NO_BORDER)
   143             button.SetToolTipString(help)
   148             button.SetToolTipString(help)
   144             setattr(self, name, button)
   149             setattr(self, name, button)
   145             top_sizer.AddWindow(button)
   150             top_sizer.AddWindow(button)
   146         
   151 
   147         self.ActionsGrid = CustomGrid(self, size=wx.Size(-1, 250), style=wx.VSCROLL)
   152         self.ActionsGrid = CustomGrid(self, size=wx.Size(-1, 250), style=wx.VSCROLL)
   148         self.ActionsGrid.DisableDragGridSize()
   153         self.ActionsGrid.DisableDragGridSize()
   149         self.ActionsGrid.EnableScrolling(False, True)
   154         self.ActionsGrid.EnableScrolling(False, True)
   150         self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, 
   155         self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
   151                               self.OnActionsGridCellChange)
   156                               self.OnActionsGridCellChange)
   152         main_sizer.AddSizer(self.ActionsGrid, border=20,
   157         main_sizer.AddSizer(self.ActionsGrid, border=20,
   153               flag=wx.GROW|wx.LEFT|wx.RIGHT)
   158                             flag=wx.GROW | wx.LEFT | wx.RIGHT)
   154         
   159 
   155         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   160         button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE)
   156         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   161         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   157         main_sizer.AddSizer(button_sizer, border=20, 
   162         main_sizer.AddSizer(button_sizer, border=20,
   158               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
   163                             flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
   159         
   164 
   160         self.SetSizer(main_sizer)
   165         self.SetSizer(main_sizer)
   161         
   166 
   162         self.Table = ActionTable(self, [], GetActionTableColnames())
   167         self.Table = ActionTable(self, [], GetActionTableColnames())
   163         typelist = GetTypeList()       
   168         typelist = GetTypeList()
   164         self.TypeList = ",".join(map(_,typelist))
   169         self.TypeList = ",".join(map(_, typelist))
   165         self.TranslateType = dict([(_(value), value) for value in typelist])
   170         self.TranslateType = dict([(_(value), value) for value in typelist])
   166         self.ColSizes = [60, 90, 130, 200, 50]
   171         self.ColSizes = [60, 90, 130, 200, 50]
   167         self.ColAlignements = [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT]
   172         self.ColAlignements = [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT]
   168         
   173 
   169         self.ActionsGrid.SetTable(self.Table)
   174         self.ActionsGrid.SetTable(self.Table)
   170         self.ActionsGrid.SetDefaultValue(_ActionInfos("N", "Action", "", "", ""))
   175         self.ActionsGrid.SetDefaultValue(_ActionInfos("N", "Action", "", "", ""))
   171         self.ActionsGrid.SetButtons({"Add": self.AddButton,
   176         self.ActionsGrid.SetButtons({"Add": self.AddButton,
   172                                      "Delete": self.DeleteButton,
   177                                      "Delete": self.DeleteButton,
   173                                      "Up": self.UpButton,
   178                                      "Up": self.UpButton,
   174                                      "Down": self.DownButton})
   179                                      "Down": self.DownButton})
   175         self.ActionsGrid.SetRowLabelSize(0)
   180         self.ActionsGrid.SetRowLabelSize(0)
   176         
   181 
   177         for col in range(self.Table.GetNumberCols()):
   182         for col in range(self.Table.GetNumberCols()):
   178             attr = wx.grid.GridCellAttr()
   183             attr = wx.grid.GridCellAttr()
   179             attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
   184             attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
   180             self.ActionsGrid.SetColAttr(col, attr)
   185             self.ActionsGrid.SetColAttr(col, attr)
   181             self.ActionsGrid.SetColMinimalWidth(col, self.ColSizes[col])
   186             self.ActionsGrid.SetColMinimalWidth(col, self.ColSizes[col])
   182             self.ActionsGrid.AutoSizeColumn(col, False)
   187             self.ActionsGrid.AutoSizeColumn(col, False)
   183         
   188 
   184         self.Table.ResetView(self.ActionsGrid)
   189         self.Table.ResetView(self.ActionsGrid)
   185         self.ActionsGrid.SetFocus()
   190         self.ActionsGrid.SetFocus()
   186         self.ActionsGrid.RefreshButtons()
   191         self.ActionsGrid.RefreshButtons()
   187         self.Fit()
   192         self.Fit()
   188     
   193 
   189     def OnOK(self, event):
   194     def OnOK(self, event):
   190         self.ActionsGrid.CloseEditControl()
   195         self.ActionsGrid.CloseEditControl()
   191         self.EndModal(wx.ID_OK)
   196         self.EndModal(wx.ID_OK)
   192 
   197 
   193     def OnActionsGridCellChange(self, event):
   198     def OnActionsGridCellChange(self, event):
   194         wx.CallAfter(self.Table.ResetView, self.ActionsGrid)
   199         wx.CallAfter(self.Table.ResetView, self.ActionsGrid)
   195         event.Skip()
   200         event.Skip()
   196     
   201 
   197     def SetQualifierList(self, list):
   202     def SetQualifierList(self, list):
   198         self.QualifierList = ",".join(list)
   203         self.QualifierList = ",".join(list)
   199         self.DurationList = list
   204         self.DurationList = list
   200 
   205 
   201     def SetVariableList(self, list):
   206     def SetVariableList(self, list):
   202         self.VariableList = "," + ",".join([variable.Name for variable in list])
   207         self.VariableList = "," + ",".join([variable.Name for variable in list])
   203         
   208 
   204     def SetActionList(self, list):
   209     def SetActionList(self, list):
   205         self.ActionList = "," + ",".join(list)
   210         self.ActionList = "," + ",".join(list)
   206 
   211 
   207     def SetValues(self, actions):
   212     def SetValues(self, actions):
   208         for action in actions:
   213         for action in actions:
   216             self.Table.AppendRow(row)
   221             self.Table.AppendRow(row)
   217         self.Table.ResetView(self.ActionsGrid)
   222         self.Table.ResetView(self.ActionsGrid)
   218         if len(actions) > 0:
   223         if len(actions) > 0:
   219             self.ActionsGrid.SetGridCursor(0, 0)
   224             self.ActionsGrid.SetGridCursor(0, 0)
   220         self.ActionsGrid.RefreshButtons()
   225         self.ActionsGrid.RefreshButtons()
   221     
   226 
   222     def GetValues(self):
   227     def GetValues(self):
   223         actions = self.Table.GetData()
   228         actions = self.Table.GetData()
   224         for action in actions:
   229         for action in actions:
   225             if action.type in ["Action", "Variable"]:
   230             if action.type in ["Action", "Variable"]:
   226                 action.type = "reference"
   231                 action.type = "reference"