class ActionBlockDialog(wx.Dialog): def __init__(self, parent): wx.Dialog.__init__(self, parent, title=_('Edit action block properties')) main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) main_sizer.AddGrowableCol(0) main_sizer.AddGrowableRow(1) top_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) top_sizer.AddGrowableCol(0) top_sizer.AddGrowableRow(0) main_sizer.AddSizer(top_sizer, border=20, flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT) actions_label = wx.StaticText(self, label=_('Actions:')) top_sizer.AddWindow(actions_label, flag=wx.ALIGN_BOTTOM) for name, bitmap, help in [ ("AddButton", "add_element", _("Add action")), ("DeleteButton", "remove_element", _("Remove action")), ("UpButton", "up", _("Move action up")), ("DownButton", "down", _("Move action down")) ]: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), size=wx.Size(28, 28), style=wx.NO_BORDER) button.SetToolTipString(help) setattr(self, name, button) top_sizer.AddWindow(button) self.ActionsGrid = CustomGrid(self, size=wx.Size(-1, 250), style=wx.VSCROLL) self.ActionsGrid.DisableDragGridSize() self.ActionsGrid.EnableScrolling(False, True) self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnActionsGridCellChange) main_sizer.AddSizer(self.ActionsGrid, border=20, flag=wx.GROW | wx.LEFT | wx.RIGHT) button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) main_sizer.AddSizer(button_sizer, border=20, flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) self.SetSizer(main_sizer) self.Table = ActionTable(self, [], GetActionTableColnames()) typelist = GetTypeList() self.TypeList = ",".join(map(_, typelist)) self.TranslateType = dict([(_(value), value) for value in typelist]) self.ColSizes = [60, 90, 130, 200, 50] self.ColAlignements = [ wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT ] self.ActionsGrid.SetTable(self.Table) self.ActionsGrid.SetDefaultValue( _ActionInfos("N", "Action", "", "", "")) self.ActionsGrid.SetButtons({ "Add": self.AddButton, "Delete": self.DeleteButton, "Up": self.UpButton, "Down": self.DownButton }) self.ActionsGrid.SetRowLabelSize(0) for col in range(self.Table.GetNumberCols()): attr = wx.grid.GridCellAttr() attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE) self.ActionsGrid.SetColAttr(col, attr) self.ActionsGrid.SetColMinimalWidth(col, self.ColSizes[col]) self.ActionsGrid.AutoSizeColumn(col, False) self.Table.ResetView(self.ActionsGrid) self.ActionsGrid.SetFocus() self.ActionsGrid.RefreshButtons() self.Fit() def OnOK(self, event): self.ActionsGrid.CloseEditControl() self.EndModal(wx.ID_OK) def OnActionsGridCellChange(self, event): wx.CallAfter(self.Table.ResetView, self.ActionsGrid) event.Skip() def SetQualifierList(self, list): self.QualifierList = ",".join(list) self.DurationList = list def SetVariableList(self, list): self.VariableList = "," + ",".join( [variable.Name for variable in list]) def SetActionList(self, list): self.ActionList = "," + ",".join(list) def SetValues(self, actions): for action in actions: row = action.copy() if row.type == "reference" and row.value in self.ActionList: row.type = "Action" elif row.type == "reference" and row.value in self.VariableList: row.type = "Variable" else: row.type = "Inline" self.Table.AppendRow(row) self.Table.ResetView(self.ActionsGrid) if len(actions) > 0: self.ActionsGrid.SetGridCursor(0, 0) self.ActionsGrid.RefreshButtons() def GetValues(self): actions = self.Table.GetData() for action in actions: if action.type in ["Action", "Variable"]: action.type = "reference" else: action.type = "inline" return actions
class ResourceEditor(EditorPanel): VARIABLE_PANEL_TYPE = "resource" def _init_Editor(self, parent): self.Editor = wx.Panel(parent, style=wx.SUNKEN_BORDER | wx.TAB_TRAVERSAL) main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) main_sizer.AddGrowableCol(0) main_sizer.AddGrowableRow(0) main_sizer.AddGrowableRow(1) tasks_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) tasks_sizer.AddGrowableCol(0) tasks_sizer.AddGrowableRow(1) main_sizer.AddSizer(tasks_sizer, border=5, flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT) tasks_buttons_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) tasks_buttons_sizer.AddGrowableCol(0) tasks_buttons_sizer.AddGrowableRow(0) tasks_sizer.AddSizer(tasks_buttons_sizer, flag=wx.GROW) tasks_label = wx.StaticText(self.Editor, label=_(u'Tasks:')) tasks_buttons_sizer.AddWindow(tasks_label, flag=wx.ALIGN_BOTTOM) for name, bitmap, help in [ ("AddTaskButton", "add_element", _("Add task")), ("DeleteTaskButton", "remove_element", _("Remove task")), ("UpTaskButton", "up", _("Move task up")), ("DownTaskButton", "down", _("Move task down")) ]: button = wx.lib.buttons.GenBitmapButton(self.Editor, bitmap=GetBitmap(bitmap), size=wx.Size(28, 28), style=wx.NO_BORDER) button.SetToolTipString(help) setattr(self, name, button) tasks_buttons_sizer.AddWindow(button) self.TasksGrid = CustomGrid(self.Editor, style=wx.VSCROLL) self.TasksGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnTasksGridCellChange) tasks_sizer.AddWindow(self.TasksGrid, flag=wx.GROW) instances_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) instances_sizer.AddGrowableCol(0) instances_sizer.AddGrowableRow(1) main_sizer.AddSizer(instances_sizer, border=5, flag=wx.GROW | wx.BOTTOM | wx.LEFT | wx.RIGHT) instances_buttons_sizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) instances_buttons_sizer.AddGrowableCol(0) instances_buttons_sizer.AddGrowableRow(0) instances_sizer.AddSizer(instances_buttons_sizer, flag=wx.GROW) instances_label = wx.StaticText(self.Editor, label=_(u'Instances:')) instances_buttons_sizer.AddWindow(instances_label, flag=wx.ALIGN_BOTTOM) for name, bitmap, help in [ ("AddInstanceButton", "add_element", _("Add instance")), ("DeleteInstanceButton", "remove_element", _("Remove instance")), ("UpInstanceButton", "up", _("Move instance up")), ("DownInstanceButton", "down", _("Move instance down")) ]: button = wx.lib.buttons.GenBitmapButton(self.Editor, bitmap=GetBitmap(bitmap), size=wx.Size(28, 28), style=wx.NO_BORDER) button.SetToolTipString(help) setattr(self, name, button) instances_buttons_sizer.AddWindow(button) self.InstancesGrid = CustomGrid(self.Editor, style=wx.VSCROLL) self.InstancesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnInstancesGridCellChange) instances_sizer.AddWindow(self.InstancesGrid, flag=wx.GROW) self.Editor.SetSizer(main_sizer) def __init__(self, parent, tagname, window, controler): EditorPanel.__init__(self, parent, tagname, window, controler) self.RefreshHighlightsTimer = wx.Timer(self, -1) self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer) self.TasksDefaultValue = { "Name": "", "Triggering": "", "Single": "", "Interval": "", "Priority": 0 } self.TasksTable = ResourceTable(self, [], GetTasksTableColnames()) self.TasksTable.SetColAlignements([ wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_RIGHT, wx.ALIGN_RIGHT ]) self.TasksTable.SetColSizes([200, 100, 100, 150, 100]) self.TasksGrid.SetTable(self.TasksTable) self.TasksGrid.SetButtons({ "Add": self.AddTaskButton, "Delete": self.DeleteTaskButton, "Up": self.UpTaskButton, "Down": self.DownTaskButton }) def _AddTask(new_row): self.TasksTable.InsertRow(new_row, self.TasksDefaultValue.copy()) self.RefreshModel() self.RefreshView() return new_row setattr(self.TasksGrid, "_AddRow", _AddTask) def _DeleteTask(row): self.TasksTable.RemoveRow(row) self.RefreshModel() self.RefreshView() setattr(self.TasksGrid, "_DeleteRow", _DeleteTask) def _MoveTask(row, move): new_row = self.TasksTable.MoveRow(row, move) if new_row != row: self.RefreshModel() self.RefreshView() return new_row setattr(self.TasksGrid, "_MoveRow", _MoveTask) self.TasksGrid.SetRowLabelSize(0) self.TasksTable.ResetView(self.TasksGrid) self.TasksGrid.RefreshButtons() self.InstancesDefaultValue = {"Name": "", "Type": "", "Task": ""} self.InstancesTable = ResourceTable(self, [], GetInstancesTableColnames()) self.InstancesTable.SetColAlignements( [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT]) self.InstancesTable.SetColSizes([200, 150, 150]) self.InstancesGrid.SetTable(self.InstancesTable) self.InstancesGrid.SetButtons({ "Add": self.AddInstanceButton, "Delete": self.DeleteInstanceButton, "Up": self.UpInstanceButton, "Down": self.DownInstanceButton }) def _AddInstance(new_row): self.InstancesTable.InsertRow(new_row, self.InstancesDefaultValue.copy()) self.RefreshModel() self.RefreshView() return new_row setattr(self.InstancesGrid, "_AddRow", _AddInstance) def _DeleteInstance(row): self.InstancesTable.RemoveRow(row) self.RefreshModel() self.RefreshView() setattr(self.InstancesGrid, "_DeleteRow", _DeleteInstance) def _MoveInstance(row, move): new_row = max( 0, min(row + move, self.InstancesTable.GetNumberRows() - 1)) if new_row != row: if self.InstancesTable.GetValueByName( row, "Task") != self.InstancesTable.GetValueByName( new_row, "Task"): return row self.InstancesTable.MoveRow(row, move) self.RefreshModel() self.RefreshView() return new_row setattr(self.InstancesGrid, "_MoveRow", _MoveInstance) def _RefreshInstanceButtons(): if self: rows = self.InstancesTable.GetNumberRows() row = self.InstancesGrid.GetGridCursorRow() self.DeleteInstanceButton.Enable(rows > 0) self.UpInstanceButton.Enable( row > 0 and self.InstancesTable.GetValueByName(row, "Task") == self.InstancesTable.GetValueByName(row - 1, "Task")) self.DownInstanceButton.Enable( 0 <= row < rows - 1 and self.InstancesTable.GetValueByName(row, "Task") == self.InstancesTable.GetValueByName(row + 1, "Task")) setattr(self.InstancesGrid, "RefreshButtons", _RefreshInstanceButtons) self.InstancesGrid.SetRowLabelSize(0) self.InstancesTable.ResetView(self.InstancesGrid) self.InstancesGrid.RefreshButtons() self.TasksGrid.SetFocus() def __del__(self): self.RefreshHighlightsTimer.Stop() def RefreshTypeList(self): self.TypeList = "" blocktypes = self.Controler.GetBlockResource() for blocktype in blocktypes: self.TypeList += ",%s" % blocktype def RefreshTaskList(self): self.TaskList = "" for row in xrange(self.TasksTable.GetNumberRows()): self.TaskList += ",%s" % self.TasksTable.GetValueByName( row, "Name") def RefreshVariableList(self): self.VariableList = "" for variable in self.Controler.GetEditedResourceVariables( self.TagName): self.VariableList += ",%s" % variable def RefreshModel(self): self.Controler.SetEditedResourceInfos(self.TagName, self.TasksTable.GetData(), self.InstancesTable.GetData()) self.RefreshBuffer() # Buffer the last model state def RefreshBuffer(self): self.Controler.BufferProject() self.ParentWindow.RefreshTitle() self.ParentWindow.RefreshFileMenu() self.ParentWindow.RefreshEditMenu() def GetBufferState(self): return self.Controler.GetBufferState() def Undo(self): self.Controler.LoadPrevious() self.ParentWindow.CloseTabsWithoutModel() def Redo(self): self.Controler.LoadNext() self.ParentWindow.CloseTabsWithoutModel() def HasNoModel(self): return self.Controler.GetEditedElement(self.TagName) is None def RefreshView(self, variablepanel=True): EditorPanel.RefreshView(self, variablepanel) tasks, instances = self.Controler.GetEditedResourceInfos(self.TagName) self.TasksTable.SetData(tasks) self.InstancesTable.SetData(instances) self.RefreshTypeList() self.RefreshTaskList() self.RefreshVariableList() self.TasksTable.ResetView(self.TasksGrid) self.InstancesTable.ResetView(self.InstancesGrid) self.TasksGrid.RefreshButtons() self.InstancesGrid.RefreshButtons() def OnTasksGridCellChange(self, event): row, col = event.GetRow(), event.GetCol() if self.TasksTable.GetColLabelValue(col) == "Name": tasklist = [ name for name in self.TaskList.split(",") if name != "" ] for i in xrange(self.TasksTable.GetNumberRows()): task = self.TasksTable.GetValueByName(i, "Name") if task in tasklist: tasklist.remove(task) if len(tasklist) > 0: old_name = tasklist[0] new_name = self.TasksTable.GetValue(row, col) for i in xrange(self.InstancesTable.GetNumberRows()): if self.InstancesTable.GetValueByName(i, "Task") == old_name: self.InstancesTable.SetValueByName(i, "Task", new_name) self.RefreshModel() colname = self.TasksTable.GetColLabelValue(col, False) if colname in ["Triggering", "Name", "Single", "Interval"]: wx.CallAfter(self.RefreshView, False) event.Skip() def OnInstancesGridCellChange(self, event): self.RefreshModel() self.ParentWindow.RefreshPouInstanceVariablesPanel() self.InstancesGrid.RefreshButtons() event.Skip() #------------------------------------------------------------------------------- # Highlights showing functions #------------------------------------------------------------------------------- def OnRefreshHighlightsTimer(self, event): self.RefreshView() event.Skip() def AddHighlight(self, infos, start, end, highlight_type): EditorPanel.AddHighlight(self, infos, start, end, highlight_type) if infos[0] == "task": self.TasksTable.AddHighlight(infos[1:], highlight_type) elif infos[0] == "instance": self.InstancesTable.AddHighlight(infos[1:], highlight_type) self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) def ClearHighlights(self, highlight_type=None): EditorPanel.ClearHighlights(self, highlight_type) self.TasksTable.ClearHighlights(highlight_type) self.InstancesTable.ClearHighlights(highlight_type) self.TasksTable.ResetView(self.TasksGrid) self.InstancesTable.ResetView(self.InstancesGrid)