示例#1
0
 def OnDoDrop(self, event):
     try:
         values = eval(event.GetDragText())
     except Exception:
         values = event.GetDragText()
     if isinstance(values, tuple):
         message = None
         if values[1] in ["program", "debug"]:
             event.SetDragText("")
         elif values[1] in ["functionBlock", "function"]:
             blocktype = values[0]
             blockname = values[2]
             if len(values) > 3:
                 blockinputs = values[3]
             else:
                 blockinputs = None
             if values[1] != "function":
                 if blockname == "":
                     dialog = wx.TextEntryDialog(self.ParentWindow, _("Block name"), _("Please enter a block name"), "", wx.OK | wx.CANCEL | wx.CENTRE)
                     if dialog.ShowModal() == wx.ID_OK:
                         blockname = dialog.GetValue()
                     else:
                         event.SetDragText("")
                         return
                     dialog.Destroy()
                 if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!") % blockname
                 elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     message = _("\"%s\" element for this pou already exists!") % blockname
                 else:
                     self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname)
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
             blockinfo = self.Controler.GetBlockType(blocktype, blockinputs, self.Debug)
             hint = ',\n    '.join(
                         [" " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] +
                         [" " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]])
             if values[1] == "function":
                 event.SetDragText(blocktype+"(\n    "+hint+")")
             else:
                 event.SetDragText(blockname+"(\n    "+hint+")")
         elif values[1] == "location":
             pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
             if len(values) > 2 and pou_type == "program":
                 var_name = values[3]
                 dlg = wx.TextEntryDialog(
                     self.ParentWindow,
                     _("Confirm or change variable name"),
                     _('Variable Drop'), var_name)
                 dlg.SetValue(var_name)
                 var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
                 dlg.Destroy()
                 if var_name is None:
                     return
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!") % var_name
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     message = _("\"%s\" element for this pou already exists!") % var_name
                 else:
                     location = values[0]
                     if not location.startswith("%"):
                         dialog = wx.SingleChoiceDialog(
                             self.ParentWindow,
                             _("Select a variable class:"),
                             _("Variable class"),
                             [_("Input"), _("Output"), _("Memory")],
                             wx.DEFAULT_DIALOG_STYLE | wx.OK | wx.CANCEL)
                         if dialog.ShowModal() == wx.ID_OK:
                             selected = dialog.GetSelection()
                         else:
                             selected = None
                         dialog.Destroy()
                         if selected is None:
                             event.SetDragText("")
                             return
                         if selected == 0:
                             location = "%I" + location
                         elif selected == 1:
                             location = "%Q" + location
                         else:
                             location = "%M" + location
                     if values[2] is not None:
                         var_type = values[2]
                     else:
                         var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0]
                     self.Controler.AddEditedElementPouVar(
                         self.TagName,
                         var_type, var_name,
                         location=location, description=values[4])
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
                     event.SetDragText(var_name)
             else:
                 event.SetDragText("")
         elif values[1] == "NamedConstant":
             pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
             if pou_type == "program":
                 initval = values[0]
                 var_name = values[3]
                 dlg = wx.TextEntryDialog(
                     self.ParentWindow,
                     _("Confirm or change variable name"),
                     _('Variable Drop'), var_name)
                 dlg.SetValue(var_name)
                 var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
                 dlg.Destroy()
                 if var_name is None:
                     return
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!") % var_name
                 else:
                     var_type = values[2]
                     if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                         self.Controler.AddEditedElementPouVar(self.TagName,
                                                               var_type,
                                                               var_name,
                                                               description=values[4], initval=initval)
                         self.RefreshVariablePanel()
                         self.RefreshVariableTree()
                     event.SetDragText(var_name)
         elif values[1] == "Global":
             var_name = values[0]
             dlg = wx.TextEntryDialog(
                 self.ParentWindow,
                 _("Confirm or change variable name"),
                 _('Variable Drop'), var_name)
             dlg.SetValue(var_name)
             var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
             dlg.Destroy()
             if var_name is None:
                 return
             elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                 message = _("\"%s\" pou already exists!") % var_name
             else:
                 if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     self.Controler.AddEditedElementPouExternalVar(self.TagName, values[2], var_name)
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
                 event.SetDragText(var_name)
         elif values[1] == "Constant":
             event.SetDragText(values[0])
         elif values[3] == self.TagName:
             self.ResetBuffer()
             event.SetDragText(values[0])
             wx.CallAfter(self.RefreshModel)
         else:
             message = _("Variable don't belong to this POU!")
         if message is not None:
             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR)
             dialog.ShowModal()
             dialog.Destroy()
             event.SetDragText("")
     event.Skip()
示例#2
0
 def OnDoDrop(self, event):
     try:
         values = eval(event.GetDragText())
     except:
         values = event.GetDragText()
     if isinstance(values, tuple):
         message = None
         if values[1] in ["program", "debug"]:
             event.SetDragText("")
         elif values[1] in ["functionBlock", "function"]:
             blocktype = values[0]
             blockname = values[2]
             if len(values) > 3:
                 blockinputs = values[3]
             else:
                 blockinputs = None
             if values[1] != "function":
                 if blockname == "":
                     dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
                     if dialog.ShowModal() == wx.ID_OK:
                         blockname = dialog.GetValue()
                     else:
                         return
                     dialog.Destroy()
                 if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!")%blockname
                 elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     message = _("\"%s\" element for this pou already exists!")%blockname
                 else:
                     self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname)
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
             blockinfo = self.Controler.GetBlockType(blocktype, blockinputs, self.Debug)
             hint = ',\n    '.join(
                         [ " " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] +
                         [ " " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]])
             if values[1] == "function":
                 event.SetDragText(blocktype+"(\n    "+hint+")")
             else:
                 event.SetDragText(blockname+"(\n    "+hint+")")
         elif values[1] == "location":
             pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
             if len(values) > 2 and pou_type == "program":
                 var_name = values[3]
                 dlg = wx.TextEntryDialog(
                     self.ParentWindow,
                     _("Confirm or change variable name"),
                     'Variable Drop', var_name)
                 dlg.SetValue(var_name)
                 var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
                 dlg.Destroy()
                 if var_name is None:
                     return
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!")%var_name
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     message = _("\"%s\" element for this pou already exists!")%var_name
                 else:
                     location = values[0]
                     if not location.startswith("%"):
                         dialog = wx.SingleChoiceDialog(self.ParentWindow,
                               _("Select a variable class:"), _("Variable class"),
                               ["Input", "Output", "Memory"],
                               wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
                         if dialog.ShowModal() == wx.ID_OK:
                             selected = dialog.GetSelection()
                         else:
                             selected = None
                         dialog.Destroy()
                         if selected is None:
                             event.SetDragText("")
                             return
                         if selected == 0:
                             location = "%I" + location
                         elif selected == 1:
                             location = "%Q" + location
                         else:
                             location = "%M" + location
                     if values[2] is not None:
                         var_type = values[2]
                     else:
                         var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0]
                     self.Controler.AddEditedElementPouVar(self.TagName,
                         var_type, var_name,
                         location=location, description=values[4])
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
                     event.SetDragText(var_name)
             else:
                 event.SetDragText("")
         elif values[1] == "NamedConstant":
             pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
             if pou_type == "program":
                 initval = values[0]
                 var_name = values[3]
                 dlg = wx.TextEntryDialog(
                     self.ParentWindow,
                     _("Confirm or change variable name"),
                     'Variable Drop', var_name)
                 dlg.SetValue(var_name)
                 var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
                 dlg.Destroy()
                 if var_name is None:
                     return
                 elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                     message = _("\"%s\" pou already exists!")%var_name
                 else:
                     var_type = values[2]
                     if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                         self.Controler.AddEditedElementPouVar(self.TagName,
                                                               var_type,
                                                               var_name,
                                                               description=values[4], initval=initval)
                         self.RefreshVariablePanel()
                         self.RefreshVariableTree()
                     event.SetDragText(var_name)
         elif values[1] == "Global":
             var_name = values[0]
             dlg = wx.TextEntryDialog(
                 self.ParentWindow,
                 _("Confirm or change variable name"),
                 'Variable Drop', var_name)
             dlg.SetValue(var_name)
             var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
             dlg.Destroy()
             if var_name is None:
                 return
             elif var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
                 message = _("\"%s\" pou already exists!")%var_name
             else:
                 if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
                     self.Controler.AddEditedElementPouExternalVar(self.TagName, values[2], var_name)
                     self.RefreshVariablePanel()
                     self.RefreshVariableTree()
                 event.SetDragText(var_name)
         elif values[1] == "Constant":
             event.SetDragText(values[0])
         elif values[3] == self.TagName:
             self.ResetBuffer()
             event.SetDragText(values[0])
             wx.CallAfter(self.RefreshModel)
         else:
             message = _("Variable don't belong to this POU!")
         if message is not None:
             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
             dialog.ShowModal()
             dialog.Destroy()
             event.SetDragText("")
     event.Skip()
    _ = NoTranslate
    return [(_("All"),
             [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]),
            (_("Input"), [LOCATION_VAR_INPUT]),
            (_("Output"), [LOCATION_VAR_OUTPUT]),
            (_("Memory"), [LOCATION_VAR_MEMORY])]


def GetTypeFilterChoiceOptions():
    _ = NoTranslate
    return [_("All"), _("Type and derivated"), _("Type strict")]


# turn LOCATIONDATATYPES inside-out
LOCATION_SIZES = {}
for size, types in LOCATIONDATATYPES.iteritems():
    for type in types:
        LOCATION_SIZES[type] = size

# -------------------------------------------------------------------------------
#                            Browse Locations Dialog
# -------------------------------------------------------------------------------


class BrowseLocationsDialog(wx.Dialog):
    def __init__(self, parent, var_type, controller):
        wx.Dialog.__init__(self,
                           parent,
                           title=_('Browse Locations'),
                           style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
    _ = lambda x : x
    return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), 
            (_("Input"), [LOCATION_VAR_INPUT]), 
            (_("Output"), [LOCATION_VAR_OUTPUT]), 
            (_("Memory"), [LOCATION_VAR_MEMORY])]
DIRFILTERCHOICE_OPTIONS = dict([(_(option), filter) for option, filter in GetDirFilterChoiceOptions()])

def GetTypeFilterChoiceOptions():
    _ = lambda x : x
    return [_("All"), 
            _("Type and derivated"), 
            _("Type strict")]

# turn LOCATIONDATATYPES inside-out
LOCATION_SIZES = {}
for size, types in LOCATIONDATATYPES.iteritems():
    for type in types:
        LOCATION_SIZES[type] = size

#-------------------------------------------------------------------------------
#                            Browse Locations Dialog
#-------------------------------------------------------------------------------

class BrowseLocationsDialog(wx.Dialog):
    
    def __init__(self, parent, var_type, controller):
        wx.Dialog.__init__(self, parent,  
              size=wx.Size(600, 400), title=_('Browse Locations'),
              style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
        
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)