Viewer.py
changeset 218 1b8e9bb83f25
parent 216 93af9ac5aeaf
child 222 8ce5c2635976
equal deleted inserted replaced
217:ddb5b2e499e2 218:1b8e9bb83f25
    27 from plcopen.structures import *
    27 from plcopen.structures import *
    28 from PLCControler import ITEM_POU
    28 from PLCControler import ITEM_POU
    29 
    29 
    30 from Dialogs import *
    30 from Dialogs import *
    31 
    31 
       
    32 from types import TupleType
       
    33 
    32 SCROLLBAR_UNIT = 10
    34 SCROLLBAR_UNIT = 10
    33 WINDOW_BORDER = 10
    35 WINDOW_BORDER = 10
    34 SCROLL_ZONE = 10
    36 SCROLL_ZONE = 10
    35 
    37 
    36 def AppendMenu(parent, help, id, kind, text):
    38 def AppendMenu(parent, help, id, kind, text):
    82         self.ParentWindow = parent
    84         self.ParentWindow = parent
    83     
    85     
    84     def OnDropText(self, x, y, data):
    86     def OnDropText(self, x, y, data):
    85         x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
    87         x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
    86         scaling = self.ParentWindow.Scaling
    88         scaling = self.ParentWindow.Scaling
    87         values = eval(data)
    89         message = None
    88         if values[1] == "program":
    90         try:
    89             message = wx.MessageDialog(self.ParentWindow, "Programs can't be used by other POUs!", "Error", wx.OK|wx.ICON_ERROR)
    91             values = eval(data)
    90             message.ShowModal()
    92         except:
    91             message.Destroy()
    93             message = "Invalid value \"%s\" for viewer block"%data
    92         elif values[1] in ["function", "functionBlock", "program"]:
    94             values = None
    93             name, type = self.ParentWindow.Controler.GetEditedElementType(self.ParentWindow.GetTagName())
    95         if not isinstance(values, TupleType):
    94             if name == values[0]:
    96             message = "Invalid value \"%s\" for viewer block"%data
    95                 message = wx.MessageDialog(self.ParentWindow, "\"%s\" can't use itself!"%name, "Error", wx.OK|wx.ICON_ERROR)
    97             values = None
    96                 message.ShowModal()
    98         if values is not None:
    97                 message.Destroy()
    99             if values[1] == "program":
    98             elif type == "function" and values[1] != "function":
   100                 message = "Programs can't be used by other POUs!"
    99                 message = wx.MessageDialog(self.ParentWindow, "Function Blocks can't be used by Functions!", "Error", wx.OK|wx.ICON_ERROR)
   101             elif values[1] in ["function", "functionBlock", "program"]:
   100                 message.ShowModal()
   102                 name, type = self.ParentWindow.Controler.GetEditedElementType(self.ParentWindow.GetTagName())
   101                 message.Destroy()
   103                 if name == values[0]:
   102             elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0]):
   104                     message = "\"%s\" can't use itself!"%name
   103                 message = wx.MessageDialog(self.ParentWindow, "\"%s\" is already used by \"%s\"!"%(name, values[0]), "Error", wx.OK|wx.ICON_ERROR)
   105                 elif type == "function" and values[1] != "function":
   104                 message.ShowModal()
   106                     message = "Function Blocks can't be used by Functions!"
   105                 message.Destroy()
   107                 elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0]):
   106             else:
   108                     message = "\"%s\" is already used by \"%s\"!"%(name, values[0])
   107                 blockname = values[2]
       
   108                 if values[1] != "function" and blockname == "":
       
   109                     dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
       
   110                     if dialog.ShowModal():
       
   111                         blockname = dialog.GetValue()
       
   112                     dialog.Destroy()
       
   113                 if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames()]:
       
   114                     message = wx.MessageDialog(self.ParentWindow, "\"%s\" pou already exists!"%blockname, "Error", wx.OK|wx.ICON_ERROR)
       
   115                     message.ShowModal()
       
   116                     message.Destroy()
       
   117                 elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(self.ParentWindow.GetTagName())]:
       
   118                     message = wx.MessageDialog(self.ParentWindow, "\"%s\" element for this pou already exists!"%blockname, "Error", wx.OK|wx.ICON_ERROR)
       
   119                     message.ShowModal()
       
   120                     message.Destroy()
       
   121                 else:
   109                 else:
       
   110                     blockname = values[2]
       
   111                     if values[1] != "function" and blockname == "":
       
   112                         dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
       
   113                         if dialog.ShowModal():
       
   114                             blockname = dialog.GetValue()
       
   115                         dialog.Destroy()
       
   116                     if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames()]:
       
   117                         message = "\"%s\" pou already exists!"%blockname
       
   118                     elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(self.ParentWindow.GetTagName())]:
       
   119                         message = "\"%s\" element for this pou already exists!"%blockname
       
   120                     else:
       
   121                         id = self.ParentWindow.GetNewId()
       
   122                         block = FBD_Block(self.ParentWindow, values[0], blockname, id)
       
   123                         width, height = block.GetMinSize()
       
   124                         if scaling is not None:
       
   125                             x = round(float(x) / float(scaling[0])) * scaling[0]
       
   126                             y = round(float(y) / float(scaling[1])) * scaling[1]
       
   127                             width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
       
   128                             height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
       
   129                         block.SetPosition(x, y)
       
   130                         block.SetSize(width, height)
       
   131                         self.ParentWindow.AddBlock(block)
       
   132                         self.ParentWindow.Controler.AddEditedElementBlock(self.ParentWindow.GetTagName(), id, values[0], blockname)
       
   133                         self.ParentWindow.RefreshBlockModel(block)
       
   134                         self.ParentWindow.RefreshBuffer()
       
   135                         self.ParentWindow.RefreshScrollBars()
       
   136                         self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
       
   137                         self.ParentWindow.Refresh(False)
       
   138             elif values[1] != "location":
       
   139                 if values[3] == self.ParentWindow.GetTagName():
   122                     id = self.ParentWindow.GetNewId()
   140                     id = self.ParentWindow.GetNewId()
   123                     block = FBD_Block(self.ParentWindow, values[0], blockname, id)
   141                     if values[1] == "Output":
   124                     width, height = block.GetMinSize()
   142                         var_type = OUTPUT
       
   143                     elif values[1] == "InOut":
       
   144                         var_type = INPUT
       
   145                     else:
       
   146                         var_type = INPUT
       
   147                     variable = FBD_Variable(self.ParentWindow, var_type, values[0], values[2], id)
       
   148                     width, height = variable.GetMinSize()
   125                     if scaling is not None:
   149                     if scaling is not None:
   126                         x = round(float(x) / float(scaling[0])) * scaling[0]
   150                         x = round(float(x) / float(scaling[0])) * scaling[0]
   127                         y = round(float(y) / float(scaling[1])) * scaling[1]
   151                         y = round(float(y) / float(scaling[1])) * scaling[1]
   128                         width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
   152                         width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
   129                         height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
   153                         height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
   130                     block.SetPosition(x, y)
   154                     variable.SetPosition(x, y)
   131                     block.SetSize(width, height)
   155                     variable.SetSize(width, height)
   132                     self.ParentWindow.AddBlock(block)
   156                     self.ParentWindow.AddBlock(variable)
   133                     self.ParentWindow.Controler.AddEditedElementBlock(self.ParentWindow.GetTagName(), id, values[0], blockname)
   157                     self.ParentWindow.Controler.AddEditedElementVariable(self.ParentWindow.GetTagName(), id, var_type)
   134                     self.ParentWindow.RefreshBlockModel(block)
   158                     self.ParentWindow.RefreshVariableModel(variable)
   135                     self.ParentWindow.RefreshBuffer()
   159                     self.ParentWindow.RefreshBuffer()
   136                     self.ParentWindow.RefreshScrollBars()
   160                     self.ParentWindow.RefreshScrollBars()
   137                     self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
       
   138                     self.ParentWindow.Refresh(False)
   161                     self.ParentWindow.Refresh(False)
   139         elif values[1] != "location":
       
   140             if values[3] == self.ParentWindow.GetTagName():
       
   141                 id = self.ParentWindow.GetNewId()
       
   142                 if values[1] == "Output":
       
   143                     var_type = OUTPUT
       
   144                 elif values[1] == "InOut":
       
   145                     var_type = INPUT
       
   146                 else:
   162                 else:
   147                     var_type = INPUT
   163                     message = "Variable don't belong to this POU!"
   148                 variable = FBD_Variable(self.ParentWindow, var_type, values[0], values[2], id)
   164         wx.CallAfter(self.ShowMessage, message)
   149                 width, height = variable.GetMinSize()
   165 
   150                 if scaling is not None:
   166     def ShowMessage(self, message):
   151                     x = round(float(x) / float(scaling[0])) * scaling[0]
   167         if message is not None:
   152                     y = round(float(y) / float(scaling[1])) * scaling[1]
   168             message = wx.MessageDialog(self.ParentWindow, message, "Error", wx.OK|wx.ICON_ERROR)
   153                     width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
   169             message.ShowModal()
   154                     height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
   170             message.Destroy()
   155                 variable.SetPosition(x, y)
   171    
   156                 variable.SetSize(width, height)
       
   157                 self.ParentWindow.AddBlock(variable)
       
   158                 self.ParentWindow.Controler.AddEditedElementVariable(self.ParentWindow.GetTagName(), id, var_type)
       
   159                 self.ParentWindow.RefreshVariableModel(variable)
       
   160                 self.ParentWindow.RefreshBuffer()
       
   161                 self.ParentWindow.RefreshScrollBars()
       
   162                 self.ParentWindow.Refresh(False)
       
   163             else:
       
   164                 message = wx.MessageDialog(self.ParentWindow, "Variable don't belong to this POU!", "Error", wx.OK|wx.ICON_ERROR)
       
   165                 message.ShowModal()
       
   166                 message.Destroy()
       
   167             
       
   168 if wx.VERSION >= (2, 8, 0):
   172 if wx.VERSION >= (2, 8, 0):
   169     import wx.aui
   173     import wx.aui
   170 
   174 
   171     class MDIViewer(wx.aui.AuiMDIChildFrame):
   175     class MDIViewer(wx.aui.AuiMDIChildFrame):
   172         def __init__(self, parent, tagname, window, controler):
   176         def __init__(self, parent, tagname, window, controler):