예제 #1
0
class VariablesEditor(wx.Panel):
    def __init__(self, parent, window, controler):
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)

        main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=4)
        main_sizer.AddGrowableCol(1)
        main_sizer.AddGrowableRow(0)

        controls_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.AddSizer(controls_sizer, border=5, flag=wx.ALL)

        for name, bitmap, help in [
            ("AddVariableButton", "add_element", _("Add variable")),
            ("DeleteVariableButton", "remove_element", _("Remove variable")),
            ("UpVariableButton", "up", _("Move variable up")),
            ("DownVariableButton", "down", _("Move variable 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)
            controls_sizer.AddWindow(button, border=5, flag=wx.BOTTOM)

        self.VariablesGrid = CustomGrid(self, style=wx.VSCROLL)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
                                self.OnVariablesGridCellChange)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
                                self.OnVariablesGridCellLeftClick)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN,
                                self.OnVariablesGridEditorShown)
        main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW)

        self.SetSizer(main_sizer)

        self.ParentWindow = window
        self.Controler = controler

        self.VariablesDefaultValue = {
            "Name": "LocalVar0",
            "Type": DefaultType,
            "Initial": "",
            "Description": "",
            "OnChange": "",
            "Options": ""
        }
        self.Table = VariablesTable(self, [], self.GetVariableTableColnames())
        self.ColAlignements = [wx.ALIGN_RIGHT] +  \
                              [wx.ALIGN_LEFT]*(len(self.VariablesDefaultValue))
        self.ColSizes = [20, 150
                         ] + [130] * (len(self.VariablesDefaultValue) - 1)
        self.VariablesGrid.SetTable(self.Table)
        self.VariablesGrid.SetButtons({
            "Add": self.AddVariableButton,
            "Delete": self.DeleteVariableButton,
            "Up": self.UpVariableButton,
            "Down": self.DownVariableButton
        })

        def _AddVariable(new_row):
            if new_row > 0:
                row_content = self.Table.data[new_row - 1].copy()
                old_name = row_content['Name']
                row_content['Name'] =\
                    self.Controler.GenerateNewName(old_name, old_name+'%d')
            else:
                row_content = self.VariablesDefaultValue.copy()
            self.Table.InsertRow(new_row, row_content)
            self.RefreshModel()
            self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_AddRow", _AddVariable)

        def _DeleteVariable(row):
            self.Table.RemoveRow(row)
            self.RefreshModel()
            self.RefreshView()

        setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)

        def _MoveVariable(row, move):
            new_row = self.Table.MoveRow(row, move)
            if new_row != row:
                self.RefreshModel()
                self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)

        self.VariablesGrid.SetRowLabelSize(0)
        for col in range(self.Table.GetNumberCols()):
            attr = wx.grid.GridCellAttr()
            attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
            self.VariablesGrid.SetColAttr(col, attr)
            self.VariablesGrid.SetColSize(col, self.ColSizes[col])
        self.Table.ResetView(self.VariablesGrid)

    def GetVariableTableColnames(self):
        _ = NoTranslate
        return [
            "#",
            _("Name"),
            _("Type"),
            _("Initial"),
            _("Description"),
            _("OnChange"),
            _("Options")
        ]

    def RefreshModel(self):
        self.Controler.SetVariables(self.Table.GetData())
        self.RefreshBuffer()

    # Buffer the last model state
    def RefreshBuffer(self):
        self.Controler.BufferCodeFile()
        self.ParentWindow.RefreshTitle()
        self.ParentWindow.RefreshFileMenu()
        self.ParentWindow.RefreshEditMenu()
        self.ParentWindow.RefreshPageTitles()

    def RefreshView(self):
        self.Table.SetData(self.Controler.GetVariables())
        self.Table.ResetView(self.VariablesGrid)
        self.VariablesGrid.RefreshButtons()

    def DoGetBestSize(self):
        return self.ParentWindow.GetPanelBestSize()

    def ShowErrorMessage(self, message):
        dialog = wx.MessageDialog(self, message, _("Error"),
                                  wx.OK | wx.ICON_ERROR)
        dialog.ShowModal()
        dialog.Destroy()

    def OnVariablesGridCellChange(self, event):
        row, col = event.GetRow(), event.GetCol()
        colname = self.Table.GetColLabelValue(col, False)
        value = self.Table.GetValue(row, col)
        message = None

        if colname == "Name" and value != "":
            if not TestIdentifier(value):
                message = _("\"%s\" is not a valid identifier!") % value
            elif value.upper() in IEC_KEYWORDS:
                message = _("\"%s\" is a keyword. It can't be used!") % value
            elif value.upper() in [
                    var["Name"].upper()
                    for var_row, var in enumerate(self.Table.data)
                    if var_row != row
            ]:
                message = _(
                    "A variable with \"%s\" as name already exists!") % value
            else:
                self.RefreshModel()
                wx.CallAfter(self.RefreshView)
        else:
            self.RefreshModel()
            wx.CallAfter(self.RefreshView)

        if message is not None:
            event.Veto()
            wx.CallAfter(self.ShowErrorMessage, message)
        else:
            event.Skip()

    def OnVariablesGridEditorShown(self, event):
        row, col = event.GetRow(), event.GetCol()
        if self.Table.GetColLabelValue(col, False) == "Type":
            type_menu = wx.Menu(title='')
            base_menu = wx.Menu(title='')
            for base_type in self.Controler.GetBaseTypes():
                new_id = wx.NewId()
                base_menu.Append(help='',
                                 id=new_id,
                                 kind=wx.ITEM_NORMAL,
                                 text=base_type)
                self.Bind(wx.EVT_MENU,
                          self.GetVariableTypeFunction(base_type),
                          id=new_id)
            type_menu.AppendMenu(wx.NewId(), "Base Types", base_menu)
            datatype_menu = wx.Menu(title='')
            for datatype in self.Controler.GetDataTypes():
                new_id = wx.NewId()
                datatype_menu.Append(help='',
                                     id=new_id,
                                     kind=wx.ITEM_NORMAL,
                                     text=datatype)
                self.Bind(wx.EVT_MENU,
                          self.GetVariableTypeFunction(datatype),
                          id=new_id)
            type_menu.AppendMenu(wx.NewId(), "User Data Types", datatype_menu)
            rect = self.VariablesGrid.BlockToDeviceRect((row, col), (row, col))

            self.VariablesGrid.PopupMenuXY(
                type_menu, rect.x + rect.width,
                rect.y + self.VariablesGrid.GetColLabelSize())
            type_menu.Destroy()
            event.Veto()
        else:
            event.Skip()

    def GetVariableTypeFunction(self, base_type):
        def VariableTypeFunction(event):
            row = self.VariablesGrid.GetGridCursorRow()
            self.Table.SetValueByName(row, "Type", base_type)
            self.Table.ResetView(self.VariablesGrid)
            self.RefreshModel()
            self.RefreshView()
            event.Skip()

        return VariableTypeFunction

    def OnVariablesGridCellLeftClick(self, event):
        if event.GetCol() == 0:
            row = event.GetRow()
            data_type = self.Table.GetValueByName(row, "Type")
            var_name = self.Table.GetValueByName(row, "Name")
            data = wx.TextDataObject(
                str((var_name, "Global", data_type,
                     self.Controler.GetCurrentLocation())))
            dragSource = wx.DropSource(self.VariablesGrid)
            dragSource.SetData(data)
            dragSource.DoDragDrop()
            return
        event.Skip()
예제 #2
0
class RequestTablePanel(wx.Panel):
    def __init__(self, parent, window, controler):
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
        """ main sizer """
        requestPanelSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=4)
        requestPanelSizer.AddGrowableCol(1)
        requestPanelSizer.AddGrowableRow(0)
        """ sizer with control buttons """
        controls_sizer = wx.BoxSizer(wx.VERTICAL)
        requestPanelSizer.AddSizer(controls_sizer, border=5, flag=wx.ALL)
        for name, bitmap, help in [
            ("AddVariableButton", "add_element", _("Add variable")),
            ("DeleteVariableButton", "remove_element", _("Remove variable")),
            ("UpVariableButton", "up", _("Move variable up")),
            ("DownVariableButton", "down", _("Move variable 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)
            controls_sizer.AddWindow(button, border=5, flag=wx.BOTTOM)
        """ variable grid """
        self.VariablesGrid = CustomGrid(self, style=wx.VSCROLL)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
                                self.OnVariablesGridCellChange)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
                                self.OnVariablesGridCellLeftClick)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN,
                                self.OnVariablesGridEditorShown)
        requestPanelSizer.AddWindow(self.VariablesGrid, flag=wx.GROW)
        """ add header to grid """
        mainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=4)
        mainSizer.AddGrowableRow(1)
        mainSizer.AddGrowableCol(0)

        headerSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.ipAddressDefault = {
            "Name": "ipAddres",
            "Address": "10.10.10.10",
            "Device ID": "",
            "Data type": "",
            "Transfer method": "",
            "Len": "1",
            "Modbus type": "",
            "Device ID": "1",
            "Period (ms)": "100",
            "Description": DESCRIPTION_IP,
        }
        self.portDefault = {
            "Name": "port",
            "Address": "502",
            "Device ID": "",
            "Data type": "",
            "Transfer method": "",
            "Len": "1",
            "Modbus type": "",
            "Device ID": "1",
            "Period (ms)": "100",
            "Description": DESCRIPTION_PORT
        }

        # виджеты для текста IP (label)
        labelIP = wx.StaticText(self, label="       Ip address: ")
        font = wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                       wx.FONTWEIGHT_BOLD)
        labelIP.SetFont(font)
        headerSizer.Add(labelIP)
        """ поле для IP """
        self.ip_address = IpAddrCtrl(self, size=(120, 22))
        wx.EVT_TEXT(self, self.ip_address.GetId(), self.OnChangeIp)
        """ wx.EVT_COMBOBOX(self, self.masterSelectCmbx.cmbbox.GetId(), self.OnChangeMasterOption) """
        headerSizer.Add(self.ip_address, flag=wx.ALIGN_CENTER_VERTICAL)
        """ виджеты для текста порта (label) """
        labelPort = wx.StaticText(self, label="      Port: ")
        font = wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                       wx.FONTWEIGHT_BOLD)
        labelPort.SetFont(font)
        headerSizer.Add(labelPort)
        """ поле для порта """
        self.port = IntCtrl(self,
                            min=0,
                            max=65535,
                            limited=True,
                            size=(70, 21))
        wx.EVT_TEXT(self, self.port.GetId(), self.OnChangePort)
        wx.EVT_TEXT(self, self.port.GetId(), self.OnChangePort)

        headerSizer.Add(self.port, flag=wx.ALIGN_CENTER_VERTICAL)

        mainSizer.AddSizer(headerSizer, flag=wx.GROW)
        mainSizer.AddSizer(requestPanelSizer, flag=wx.GROW)
        self.SetSizer(mainSizer)
        self.ParentWindow = window
        self.Controler = controler

        self.VariablesDefaultValue = {
            "Name": "",
            "Address": "1",
            "Len": "1",
            "Data type": "WORD",
            "Modbus type": HOLDING_REGISTER_READ,
            "Description": DESCRIPTION,
            "Device ID": "1",
            "Transfer method": "Periodic",
            "Period (ms)": "100"
        }
        self.Table = MBRequestDataTable(self)
        self.ColAlignements = [wx.ALIGN_RIGHT] + \
                              [wx.ALIGN_LEFT]*(len(self.VariablesDefaultValue))
        self.ColSizes = [20, 150
                         ] + [100] * (len(self.VariablesDefaultValue) - 1)
        self.VariablesGrid.SetTable(self.Table)
        self.VariablesGrid.SetButtons({
            "Add": self.AddVariableButton,
            "Delete": self.DeleteVariableButton,
            "Up": self.UpVariableButton,
            "Down": self.DownVariableButton
        })
        """ handlers for adding variable """
        def _AddVariable(new_row):
            if new_row > 0:
                row_content = self.Table.data[new_row - 1].copy()
                newAddr = int(row_content["Address"])
                newLen = int(row_content["Len"])
                if row_content["Data type"] in ("REAL", "INT"):
                    newLen = newLen * 2
                newAddr = newAddr + newLen
                row_content["Address"] = str(newAddr)
                result = VARIABLE_NAME_SUFFIX_MODEL.search(row_content["Name"])
                if result is not None:
                    name = row_content["Name"][:result.start(1)]
                    suffix = result.group(1)
                    if suffix != "":
                        start_idx = int(suffix)
                    else:
                        start_idx = 0
                else:
                    name = row_content["Name"]
                    start_idx = 0
                row_content["Name"] = self.Controler.GenerateNewName(
                    name + "%d", start_idx)
            else:
                row_content = self.VariablesDefaultValue.copy()
            self.Table.InsertRow(new_row, row_content)
            self.RefreshModel()
            self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_AddRow", _AddVariable)
        """ handlers for deleting variable """

        def _DeleteVariable(row):
            self.Table.RemoveRow(row)
            self.RefreshModel()
            self.RefreshView()

        setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)
        """ handlers for moving varible """

        def _MoveVariable(row, move):
            new_row = self.Table.MoveRow(row, move)
            if new_row != row:
                self.RefreshModel()
                self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)

        self.VariablesGrid.SetRowLabelSize(0)
        for col in range(self.Table.GetNumberCols()):
            attr = wx.grid.GridCellAttr()
            attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
            self.VariablesGrid.SetColAttr(col, attr)
            self.VariablesGrid.SetColSize(col, self.ColSizes[col])
        self.Table.ResetView(self.VariablesGrid)

        self.Bind(wx.EVT_SHOW, self.OnShow)

    def OnChangeMasterOption(self, event):
        masterOptionOnFrame = self.masterSelectCmbx.cmbbox.GetStringSelection()
        masterOptionInXml = [
            i for i in self.Controler.GetVariables()
            if i["Name"] == MASTER_OPTION
        ]
        if len(masterOptionInXml) > 0:
            if masterOptionOnFrame != masterOptionInXml[0]:
                self.RefreshModel()
        else:
            self.RefreshModel()
        event.Skip()

    def GetMasterOption(self):
        return {
            "Name": "Master option",
            "Address": "",
            "Len": "",
            "Device ID": "",
            "Data type": u"WORD",
            "Transfer method": "",
            "Period (ms)": "",
            "Description": MASTER_OPTION,
            "Modbus type": "0"
        }

    def RefreshModel(self):
        ipAddress = self.ipAddressDefault.copy()
        ipAddress["Address"] = self.ip_address.GetValue()
        controllerVariables = self.Controler.GetVariables()
        controllerVariables = [
            i for i in controllerVariables
            if i["Description"] not in (DESCRIPTION, MASTER_OPTION)
        ]
        controllerVariables += self.Table.GetData()
        controllerVariables.append(self.GetMasterOption())
        controllerVariables.append(ipAddress)
        controllerVariables.append(self.portDefault)
        self.Controler.SetVariables(controllerVariables)
        self.RefreshBuffer()

    def RefreshView(self):
        varForTable = self.Controler.GetVariables()

        varForTable = [
            i for i in varForTable if i["Description"] == DESCRIPTION
        ]
        self.Table.SetData(varForTable)
        self.Table.ResetView(self.VariablesGrid)
        varForTable = self.Controler.GetVariables()
        varForTable = [i for i in varForTable if i["Name"] == MASTER_OPTION]
        #if len(varForTable) > 0:
        #self.masterSelectCmbx.cmbbox.SetStringSelection(varForTable[0]["Modbus type"])
        self.VariablesGrid.RefreshButtons()

        varForTable = self.Controler.GetVariables()
        varForTable = [
            i for i in varForTable if i["Description"] == DESCRIPTION_IP
        ]
        if len(varForTable) == 0:
            varForTable = []
            varForTable.append(self.ipAddressDefault)
        self.ip_address.SetValue(varForTable[0]["Address"])

        varForTable = self.Controler.GetVariables()
        vars = [i for i in varForTable if i["Description"] == DESCRIPTION_PORT]
        if len(vars) == 0:
            vars = []
            vars.append(self.portDefault)
        # if self.port.GetLineText != vars[0]["Address"]:
        #     print vars
        self.port.SetValue(int(vars[0]["Address"]))

    def OnShow(self, event):
        pass
        # print 'kek'

    # используется для событий, когда изменяются поля ip-адреса и порта
    def OnChangeIp(self, event):
        ipAddress = self.ipAddressDefault.copy()
        ipAddress["Address"] = self.ip_address.GetValue().replace(' ', '')
        # port = self.portDefault.copy()
        # port["Address"] = self.port.GetLineText(0)
        controllerVariables = self.Controler.GetVariables()
        # valuesOnFrame = self.GetData()
        valueIPOnWidget = self.ip_address.GetValue()
        controllerVariablesIP = [
            i for i in controllerVariables
            if i["Description"] == (DESCRIPTION_IP)
        ]
        # valuePortOnWidget = self.port.GetLineText()
        controllerVariablesIP = [
            i for i in controllerVariables
            if i["Description"] == (DESCRIPTION_PORT)
        ]
        if not (ipAddress in controllerVariables):
            controllerVariables = [
                i for i in controllerVariables
                if i["Description"] != DESCRIPTION_IP
            ]
            controllerVariables.append(ipAddress)
            # controllerVariables.append(port)
            self.Controler.SetVariables(controllerVariables)
            self.RefreshBuffer()

        event.Skip()

    def OnChangePort(self, event):
        portAddress = self.portDefault.copy()
        portAddress["Address"] = self.port.GetLineText(0)
        controllerVariables = self.Controler.GetVariables()
        if not (portAddress in controllerVariables):
            controllerVariables = [
                i for i in controllerVariables
                if i["Description"] != DESCRIPTION_PORT
            ]
            controllerVariables.append(portAddress)
            self.Controler.SetVariables(controllerVariables)
            self.RefreshBuffer()
        event.Skip()

    # def GetData(self):
    #     """
    #     Считывает с GUI настроенные пользователем параметры и возвращает их в виде списка
    #     словарей
    #     """

    def OnVariablesGridCellChange(self, event):
        row, col = event.GetRow(), event.GetCol()
        colname = self.Table.GetColLabelValue(col, False)
        value = self.Table.GetValue(row, col)
        message = None

        if colname == "Modbus type":
            self.VariablesGrid.SetCellValue(row, col + 1, "WORD")

        if colname == "Len":
            modbusType = self.Table.GetValue(row, 4)  # Modubs type value
            if modbusType in (HOLDING_REGISTER_READ, INPUT_REGISTER_READ):
                if int(value) > MAX_SIMULTANEOUS_REGISTERS_READ:
                    value = MAX_SIMULTANEOUS_REGISTERS_READ
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_REGISTERS_READ))
            if modbusType == HOLDING_REGISTER_WRITE:
                if int(value) > MAX_SIMULTANEOUS_REGISTERS_WRITE:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_REGISTERS_WRITE))
            if modbusType in (COIL_READ, DISCRETE_INPUT_READ):
                if int(value) > MAX_SIMULTANEOUS_COIL_DISC_READ:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_COIL_DISC_READ))
            if modbusType == COIL_WRITE:
                if int(value) > MAX_SIMULTANEOUS_COIL_WRITE:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_COIL_WRITE))

        if colname == "Name" and value != "":
            if not TestIdentifier(value):
                message = _("\"%s\" is not a valid identifier!") % value
            elif value.upper() in IEC_KEYWORDS:
                message = _("\"%s\" is a keyword. It can't be used!") % value
            elif value.upper() in [
                    var["Name"].upper()
                    for var_row, var in enumerate(self.Table.data)
                    if var_row != row
            ]:
                message = _(
                    "A variable with \"%s\" as name already exists!") % value
            else:
                self.RefreshModel()
                wx.CallAfter(self.RefreshView)
        else:
            self.RefreshModel()
            wx.CallAfter(self.RefreshView)

        if message is not None:
            dialog = wx.MessageDialog(self, message, _("Error"),
                                      wx.OK | wx.ICON_ERROR)
            dialog.ShowModal()
            dialog.Destroy()
            event.Veto()
        else:
            event.Skip()

    def DoGetBestSize(self):
        return self.ParentWindow.GetPanelBestSize()

    def OnVariablesGridEditorShown(self, event):
        event.Skip()

    def GetVariableTypeFunction(self, base_type):
        def VariableTypeFunction(event):
            row = self.VariablesGrid.GetGridCursorRow()
            self.Table.SetValueByName(row, "Data type", base_type)
            self.Table.ResetView(self.VariablesGrid)
            self.RefreshModel()
            self.RefreshView()
            event.Skip()

        return VariableTypeFunction

    def OnVariablesGridCellLeftClick(self, event):
        if event.GetCol() == 0:
            row = event.GetRow()
            data_type = self.Table.GetValueByName(row, "Data type")
            var_name = self.Table.GetValueByName(row, "Name")
            data = wx.TextDataObject(
                str((var_name, "Global", data_type,
                     self.Controler.GetCurrentLocation())))
            dragSource = wx.DropSource(self.VariablesGrid)
            dragSource.SetData(data)
            dragSource.DoDragDrop()
            return
        event.Skip()

    def RefreshBuffer(self):
        self.Controler.BufferCodeFile()
        self.ParentWindow.RefreshTitle()
        self.ParentWindow.RefreshFileMenu()
        self.ParentWindow.RefreshEditMenu()
        self.ParentWindow.RefreshPageTitles()
예제 #3
0
class MBRequestDataPanel(wx.Panel):
    def __init__(self, parent, window, controler):
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
        """ main sizer """
        requestPanelSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=4)
        requestPanelSizer.AddGrowableCol(1)
        requestPanelSizer.AddGrowableRow(0)
        """ sizer with control buttons """
        controls_sizer = wx.BoxSizer(wx.VERTICAL)
        requestPanelSizer.AddSizer(controls_sizer, border=5, flag=wx.ALL)
        for name, bitmap, help in [
            ("AddVariableButton", "add_element", _("Add variable")),
            ("DeleteVariableButton", "remove_element", _("Remove variable")),
            ("UpVariableButton", "up", _("Move variable up")),
            ("DownVariableButton", "down", _("Move variable 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)
            controls_sizer.AddWindow(button, border=5, flag=wx.BOTTOM)
        """ variable grid """
        self.VariablesGrid = CustomGrid(self, style=wx.VSCROLL)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
                                self.OnVariablesGridCellChange)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
                                self.OnVariablesGridCellLeftClick)
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN,
                                self.OnVariablesGridEditorShown)
        requestPanelSizer.AddWindow(self.VariablesGrid, flag=wx.GROW)
        """ add header to grid """
        mainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=4)
        mainSizer.AddGrowableRow(1)
        mainSizer.AddGrowableCol(0)

        headerSizer = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, label="      Request data")
        font = wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                       wx.FONTWEIGHT_BOLD)
        label.SetFont(font)
        headerSizer.Add(label)

        self.masterSelectCmbx = ComboBoxWithLabel(
            self, "", ("Master0", "Master1", "Master2"))
        wx.EVT_COMBOBOX(self, self.masterSelectCmbx.cmbbox.GetId(),
                        self.OnChangeMasterOption)
        headerSizer.Add(self.masterSelectCmbx, flag=wx.ALIGN_CENTER_VERTICAL)

        mainSizer.AddSizer(headerSizer, flag=wx.GROW)
        mainSizer.AddSizer(requestPanelSizer, flag=wx.GROW)
        self.SetSizer(mainSizer)
        self.ParentWindow = window
        self.Controler = controler
        """ request defaule data value """
        self.VariablesDefaultValue = {
            "Name": "",
            "Address": "1",
            "Len": "1",
            "Data type": "WORD",
            "Modbus type": HOLDING_REGISTER_READ,
            "Description": DESCRIPTION,
            "Device ID": "1",
            "Transfer method": "Periodic",
            "Period (ms)": "100",
            "Timeout (ms)": "100",
        }
        self.Table = MBRequestDataTable(self)
        self.ColAlignements = [wx.ALIGN_RIGHT] + \
                              [wx.ALIGN_LEFT]*(len(self.VariablesDefaultValue))
        self.ColSizes = [20, 150
                         ] + [100] * (len(self.VariablesDefaultValue) - 1)
        self.VariablesGrid.SetTable(self.Table)
        self.VariablesGrid.SetButtons({
            "Add": self.AddVariableButton,
            "Delete": self.DeleteVariableButton,
            "Up": self.UpVariableButton,
            "Down": self.DownVariableButton
        })
        """ handlers for adding variable """
        def _AddVariable(new_row):
            if new_row > 0:
                row_content = self.Table.data[new_row - 1].copy()
                newAddr = int(row_content["Address"])
                newLen = int(row_content["Len"])
                if row_content["Data type"] in ("REAL", "INT"):
                    newLen = newLen * 2
                newAddr = newAddr + newLen
                row_content["Address"] = str(newAddr)
                result = VARIABLE_NAME_SUFFIX_MODEL.search(row_content["Name"])
                if result is not None:
                    name = row_content["Name"][:result.start(1)]
                    suffix = result.group(1)
                    if suffix != "":
                        start_idx = int(suffix)
                    else:
                        start_idx = 0
                else:
                    name = row_content["Name"]
                    start_idx = 0
                row_content["Name"] = self.Controler.GenerateNewName(
                    name + "%d", start_idx)
            else:
                row_content = self.VariablesDefaultValue.copy()
            self.Table.InsertRow(new_row, row_content)
            self.RefreshModel()
            self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_AddRow", _AddVariable)
        """ handlers for deleting variable """

        def _DeleteVariable(row):
            self.Table.RemoveRow(row)
            self.RefreshModel()
            self.RefreshView()

        setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)
        """ handlers for moving varible """

        def _MoveVariable(row, move):
            new_row = self.Table.MoveRow(row, move)
            if new_row != row:
                self.RefreshModel()
                self.RefreshView()
            return new_row

        setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)

        self.VariablesGrid.SetRowLabelSize(0)
        for col in range(self.Table.GetNumberCols()):
            attr = wx.grid.GridCellAttr()
            attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
            self.VariablesGrid.SetColAttr(col, attr)
            self.VariablesGrid.SetColSize(col, self.ColSizes[col])
        self.Table.ResetView(self.VariablesGrid)

    def OnChangeMasterOption(self, event):
        masterOptionOnFrame = self.masterSelectCmbx.cmbbox.GetStringSelection()
        masterOptionInXml = [
            i for i in self.Controler.GetVariables()
            if i["Name"] == MASTER_OPTION
        ]
        if len(masterOptionInXml) > 0:
            if masterOptionOnFrame != masterOptionInXml[0]:
                self.RefreshModel()
        else:
            self.RefreshModel()
        event.Skip()

    def GetMasterOption(self):
        return {
            "Name": "Master option",
            "Address": "",
            "Len": "",
            "Device ID": "",
            "Data type": u"WORD",
            "Transfer method": "",
            "Period (ms)": "100",
            "Timeout (ms)": "100",
            "Description": MASTER_OPTION,
            "Modbus type": self.masterSelectCmbx.cmbbox.GetStringSelection()
        }

    def RefreshModel(self):
        controllerVariables = self.Controler.GetVariables()
        controllerVariables = [
            i for i in controllerVariables
            if i["Description"] not in (DESCRIPTION, MASTER_OPTION)
        ]
        controllerVariables += self.Table.GetData()
        controllerVariables.append(self.GetMasterOption())
        self.Controler.SetVariables(controllerVariables)
        self.RefreshBuffer()

    def RefreshView(self):
        varForTable = self.Controler.GetVariables()
        varForTable = [
            i for i in varForTable if i["Description"] == DESCRIPTION
        ]
        self.Table.SetData(varForTable)
        self.Table.ResetView(self.VariablesGrid)
        varForTable = self.Controler.GetVariables()
        varForTable = [i for i in varForTable if i["Name"] == MASTER_OPTION]
        if len(varForTable) > 0:
            self.masterSelectCmbx.cmbbox.SetStringSelection(
                varForTable[0]["Modbus type"])
        self.VariablesGrid.RefreshButtons()

    def OnVariablesGridCellChange(self, event):
        row, col = event.GetRow(), event.GetCol()
        colname = self.Table.GetColLabelValue(col, False)
        value = self.Table.GetValue(row, col)
        message = None

        if colname == "Modbus type":
            self.VariablesGrid.SetCellValue(row, col + 1, "WORD")

        if colname == "Len":
            modbusType = self.Table.GetValue(row, 4)  # Modubs type value
            if modbusType in (HOLDING_REGISTER_READ, INPUT_REGISTER_READ):
                if int(value) > MAX_SIMULTANEOUS_REGISTERS_READ:
                    value = MAX_SIMULTANEOUS_REGISTERS_READ
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_REGISTERS_READ))
            if modbusType == HOLDING_REGISTER_WRITE:
                if int(value) > MAX_SIMULTANEOUS_REGISTERS_WRITE:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_REGISTERS_WRITE))
            if modbusType in (COIL_READ, DISCRETE_INPUT_READ):
                if int(value) > MAX_SIMULTANEOUS_COIL_DISC_READ:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_COIL_DISC_READ))
            if modbusType == COIL_WRITE:
                if int(value) > MAX_SIMULTANEOUS_COIL_WRITE:
                    self.VariablesGrid.SetCellValue(
                        row, col, str(MAX_SIMULTANEOUS_COIL_WRITE))

        if colname == "Name" and value != "":
            if not TestIdentifier(value):
                message = _("\"%s\" is not a valid identifier!") % value
            elif value.upper() in IEC_KEYWORDS:
                message = _("\"%s\" is a keyword. It can't be used!") % value
            elif value.upper() in [
                    var["Name"].upper()
                    for var_row, var in enumerate(self.Table.data)
                    if var_row != row
            ]:
                message = _(
                    "A variable with \"%s\" as name already exists!") % value
            else:
                self.RefreshModel()
                wx.CallAfter(self.RefreshView)
        else:
            self.RefreshModel()
            wx.CallAfter(self.RefreshView)

        if message is not None:
            dialog = wx.MessageDialog(self, message, _("Error"),
                                      wx.OK | wx.ICON_ERROR)
            dialog.ShowModal()
            dialog.Destroy()
            event.Veto()
        else:
            event.Skip()

    def DoGetBestSize(self):
        return self.ParentWindow.GetPanelBestSize()

    def OnVariablesGridEditorShown(self, event):
        event.Skip()

    def GetVariableTypeFunction(self, base_type):
        def VariableTypeFunction(event):
            row = self.VariablesGrid.GetGridCursorRow()
            self.Table.SetValueByName(row, "Data type", base_type)
            self.Table.ResetView(self.VariablesGrid)
            self.RefreshModel()
            self.RefreshView()
            event.Skip()

        return VariableTypeFunction

    def OnVariablesGridCellLeftClick(self, event):
        if event.GetCol() == 0:
            row = event.GetRow()
            data_type = self.Table.GetValueByName(row, "Data type")
            var_name = self.Table.GetValueByName(row, "Name")
            data = wx.TextDataObject(
                str((var_name, "Global", data_type,
                     self.Controler.GetCurrentLocation())))
            dragSource = wx.DropSource(self.VariablesGrid)
            dragSource.SetData(data)
            dragSource.DoDragDrop()
            return
        event.Skip()

    def RefreshBuffer(self):
        self.Controler.BufferCodeFile()
        self.ParentWindow.RefreshTitle()
        self.ParentWindow.RefreshFileMenu()
        self.ParentWindow.RefreshEditMenu()
        self.ParentWindow.RefreshPageTitles()
예제 #4
0
class DebugVariablePanel(wx.Panel, DebugViewer):
    def __init__(self, parent, producer):
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
        DebugViewer.__init__(self, producer, True)

        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
        main_sizer.AddGrowableCol(0)
        main_sizer.AddGrowableRow(1)

        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.AddSizer(button_sizer,
                            border=5,
                            flag=wx.ALIGN_RIGHT | wx.ALL)

        for name, bitmap, help in [
            ("DeleteButton", "remove_element", _("Remove debug variable")),
            ("UpButton", "up", _("Move debug variable up")),
            ("DownButton", "down", _("Move debug variable 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)
            button_sizer.AddWindow(button, border=5, flag=wx.LEFT)

        self.VariablesGrid = CustomGrid(self,
                                        size=wx.Size(0, 150),
                                        style=wx.VSCROLL)
        self.VariablesGrid.SetDropTarget(DebugVariableDropTarget(self))
        self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
                                self.OnVariablesGridCellRightClick)
        main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW)

        self.SetSizer(main_sizer)

        self.HasNewData = False

        self.Table = DebugVariableTable(self, [],
                                        GetDebugVariablesTableColnames())
        self.VariablesGrid.SetTable(self.Table)
        self.VariablesGrid.SetButtons({
            "Delete": self.DeleteButton,
            "Up": self.UpButton,
            "Down": self.DownButton
        })

        def _AddVariable(new_row):
            return self.VariablesGrid.GetGridCursorRow()

        setattr(self.VariablesGrid, "_AddRow", _AddVariable)

        def _DeleteVariable(row):
            item = self.Table.GetItem(row)
            self.RemoveDataConsumer(item)
            self.Table.RemoveItem(row)
            self.RefreshGrid()

        setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)

        def _MoveVariable(row, move):
            new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1))
            if new_row != row:
                self.Table.MoveItem(row, new_row)
                self.RefreshGrid()
            return new_row

        setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)

        self.VariablesGrid.SetRowLabelSize(0)

        for col in range(self.Table.GetNumberCols()):
            attr = wx.grid.GridCellAttr()
            attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER)
            self.VariablesGrid.SetColAttr(col, attr)
            self.VariablesGrid.SetColSize(col, 100)

        self.Table.ResetView(self.VariablesGrid)
        self.VariablesGrid.RefreshButtons()

    def RefreshNewData(self):
        if self.HasNewData:
            self.HasNewData = False
            self.RefreshGrid()
        DebugViewer.RefreshNewData(self)

    def RefreshGrid(self):
        self.Freeze()
        self.Table.ResetView(self.VariablesGrid)
        self.VariablesGrid.RefreshButtons()
        self.Thaw()

    def UnregisterObsoleteData(self):
        items = [(idx, item) for idx, item in enumerate(self.Table.GetData())]
        items.reverse()
        for idx, item in items:
            iec_path = item.GetVariable().upper()
            if self.GetDataType(iec_path) is None:
                self.RemoveDataConsumer(item)
                self.Table.RemoveItem(idx)
            else:
                self.AddDataConsumer(iec_path, item)
        self.Freeze()
        self.Table.ResetView(self.VariablesGrid)
        self.VariablesGrid.RefreshButtons()
        self.Thaw()

    def ResetGrid(self):
        self.DeleteDataConsumers()
        self.Table.Empty()
        self.Freeze()
        self.Table.ResetView(self.VariablesGrid)
        self.VariablesGrid.RefreshButtons()
        self.Thaw()

    def GetForceVariableMenuFunction(self, iec_path, item):
        iec_type = self.GetDataType(iec_path)

        def ForceVariableFunction(event):
            if iec_type is not None:
                dialog = ForceVariableDialog(self, iec_type,
                                             str(item.GetValue()))
                if dialog.ShowModal() == wx.ID_OK:
                    self.ForceDataValue(iec_path, dialog.GetValue())

        return ForceVariableFunction

    def GetReleaseVariableMenuFunction(self, iec_path):
        def ReleaseVariableFunction(event):
            self.ReleaseDataValue(iec_path)

        return ReleaseVariableFunction

    def OnVariablesGridCellRightClick(self, event):
        row, col = event.GetRow(), event.GetCol()
        if self.Table.GetColLabelValue(col, False) == "Value":
            iec_path = self.Table.GetValueByName(row, "Variable").upper()

            menu = wx.Menu(title='')

            new_id = wx.NewId()
            AppendMenu(menu,
                       help='',
                       id=new_id,
                       kind=wx.ITEM_NORMAL,
                       text=_("Force value"))
            self.Bind(wx.EVT_MENU,
                      self.GetForceVariableMenuFunction(
                          iec_path.upper(), self.Table.GetItem(row)),
                      id=new_id)

            new_id = wx.NewId()
            AppendMenu(menu,
                       help='',
                       id=new_id,
                       kind=wx.ITEM_NORMAL,
                       text=_("Release value"))
            self.Bind(wx.EVT_MENU,
                      self.GetReleaseVariableMenuFunction(iec_path.upper()),
                      id=new_id)

            if self.Table.IsForced(row):
                menu.Enable(new_id, True)
            else:
                menu.Enable(new_id, False)

            self.PopupMenu(menu)

            menu.Destroy()
        event.Skip()

    def InsertValue(self, iec_path, idx=None, force=False):
        if idx is None:
            idx = self.Table.GetNumberRows()
        for item in self.Table.GetData():
            if iec_path == item.GetVariable():
                return
        item = VariableTableItem(self, iec_path, "")
        result = self.AddDataConsumer(iec_path.upper(), item)
        if result is not None or force:
            self.Table.InsertItem(idx, item)
            self.RefreshGrid()

    def GetDebugVariables(self):
        return [item.GetVariable() for item in self.Table.GetData()]