PLCOpenEditor.py
changeset 2533 e09e4b4caff1
parent 2532 b9d6f5d7e0f1
child 2737 38afed869ff6
equal deleted inserted replaced
2522:1378c18402c3 2533:e09e4b4caff1
    60 # -------------------------------------------------------------------------------
    60 # -------------------------------------------------------------------------------
    61 
    61 
    62 # Define PLCOpenEditor FileMenu extra items id
    62 # Define PLCOpenEditor FileMenu extra items id
    63 [
    63 [
    64     ID_PLCOPENEDITORFILEMENUGENERATE,
    64     ID_PLCOPENEDITORFILEMENUGENERATE,
    65 ] = [wx.NewId() for _init_coll_FileMenu_Items in range(1)]
    65     ID_PLCOPENEDITORFILEMENUGENERATEAS,
       
    66 ] = [wx.NewId() for _init_coll_FileMenu_Items in range(2)]
    66 
    67 
    67 
    68 
    68 beremiz_dir = paths.AbsDir(__file__)
    69 beremiz_dir = paths.AbsDir(__file__)
    69 
    70 
    70 
    71 
    84                    kind=wx.ITEM_NORMAL, text=_(u'Save') + '\tCTRL+S')
    85                    kind=wx.ITEM_NORMAL, text=_(u'Save') + '\tCTRL+S')
    85         AppendMenu(parent, help='', id=wx.ID_SAVEAS,
    86         AppendMenu(parent, help='', id=wx.ID_SAVEAS,
    86                    kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S')
    87                    kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S')
    87         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE,
    88         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE,
    88                    kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G')
    89                    kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G')
       
    90         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATEAS,
       
    91                    kind=wx.ITEM_NORMAL, text=_(u'Generate Program As...') + '\tCTRL+SHIFT+G')
    89         parent.AppendSeparator()
    92         parent.AppendSeparator()
    90         AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP,
    93         AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP,
    91                    kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P')
    94                    kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P')
    92         AppendMenu(parent, help='', id=wx.ID_PREVIEW,
    95         AppendMenu(parent, help='', id=wx.ID_PREVIEW,
    93                    kind=wx.ITEM_NORMAL, text=_(u'Preview') + '\tCTRL+SHIFT+P')
    96                    kind=wx.ITEM_NORMAL, text=_(u'Preview') + '\tCTRL+SHIFT+P')
   106         self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL)
   109         self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL)
   107         self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE)
   110         self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE)
   108         self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS)
   111         self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS)
   109         self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu,
   112         self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu,
   110                   id=ID_PLCOPENEDITORFILEMENUGENERATE)
   113                   id=ID_PLCOPENEDITORFILEMENUGENERATE)
       
   114         self.Bind(wx.EVT_MENU, self.OnGenerateProgramAsMenu,
       
   115                   id=ID_PLCOPENEDITORFILEMENUGENERATEAS)
   111         self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP)
   116         self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP)
   112         self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW)
   117         self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW)
   113         self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT)
   118         self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT)
   114         self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES)
   119         self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES)
   115         self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT)
   120         self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT)
   116 
   121 
   117         self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None),
   122         self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None),
   118                                (wx.ID_OPEN, "open", _(u'Open'), None),
   123                                (wx.ID_OPEN, "open", _(u'Open'), None),
   119                                (wx.ID_SAVE, "save", _(u'Save'), None),
   124                                (wx.ID_SAVE, "save", _(u'Save'), None),
   120                                (wx.ID_SAVEAS, "saveas", _(u'Save As...'), None),
   125                                (wx.ID_SAVEAS, "saveas", _(u'Save As...'), None),
   121                                (wx.ID_PRINT, "print", _(u'Print'), None)])
   126                                (wx.ID_PRINT, "print", _(u'Print'), None),
       
   127                                (ID_PLCOPENEDITORFILEMENUGENERATE, "Build", _(u'Generate Program'), None)])
   122 
   128 
   123     def _init_coll_HelpMenu_Items(self, parent):
   129     def _init_coll_HelpMenu_Items(self, parent):
   124         AppendMenu(parent, help='', id=wx.ID_HELP,
   130         AppendMenu(parent, help='', id=wx.ID_HELP,
   125                    kind=wx.ITEM_NORMAL, text=_(u'PLCOpenEditor') + '\tF1')
   131                    kind=wx.ITEM_NORMAL, text=_(u'PLCOpenEditor') + '\tF1')
   126         # AppendMenu(parent, help='', id=wx.ID_HELP_CONTENTS,
   132         # AppendMenu(parent, help='', id=wx.ID_HELP_CONTENTS,
   230             self.FileMenu.Enable(wx.ID_PROPERTIES, True)
   236             self.FileMenu.Enable(wx.ID_PROPERTIES, True)
   231             self.FileMenu.Enable(wx.ID_CLOSE_ALL, True)
   237             self.FileMenu.Enable(wx.ID_CLOSE_ALL, True)
   232             self.FileMenu.Enable(wx.ID_SAVEAS, True)
   238             self.FileMenu.Enable(wx.ID_SAVEAS, True)
   233             MenuToolBar.EnableTool(wx.ID_SAVEAS, True)
   239             MenuToolBar.EnableTool(wx.ID_SAVEAS, True)
   234             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True)
   240             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True)
       
   241             MenuToolBar.EnableTool(ID_PLCOPENEDITORFILEMENUGENERATE, True)
       
   242             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATEAS, True)
   235         else:
   243         else:
   236             self.FileMenu.Enable(wx.ID_CLOSE, False)
   244             self.FileMenu.Enable(wx.ID_CLOSE, False)
   237             self.FileMenu.Enable(wx.ID_PAGE_SETUP, False)
   245             self.FileMenu.Enable(wx.ID_PAGE_SETUP, False)
   238             self.FileMenu.Enable(wx.ID_PREVIEW, False)
   246             self.FileMenu.Enable(wx.ID_PREVIEW, False)
   239             self.FileMenu.Enable(wx.ID_PRINT, False)
   247             self.FileMenu.Enable(wx.ID_PRINT, False)
   243             self.FileMenu.Enable(wx.ID_PROPERTIES, False)
   251             self.FileMenu.Enable(wx.ID_PROPERTIES, False)
   244             self.FileMenu.Enable(wx.ID_CLOSE_ALL, False)
   252             self.FileMenu.Enable(wx.ID_CLOSE_ALL, False)
   245             self.FileMenu.Enable(wx.ID_SAVEAS, False)
   253             self.FileMenu.Enable(wx.ID_SAVEAS, False)
   246             MenuToolBar.EnableTool(wx.ID_SAVEAS, False)
   254             MenuToolBar.EnableTool(wx.ID_SAVEAS, False)
   247             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False)
   255             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False)
       
   256             MenuToolBar.EnableTool(ID_PLCOPENEDITORFILEMENUGENERATE, False)
       
   257             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATEAS, False)
   248 
   258 
   249     def OnNewProjectMenu(self, event):
   259     def OnNewProjectMenu(self, event):
   250         if self.Controler is not None and not self.CheckSaveBeforeClosing():
   260         if self.Controler is not None and not self.CheckSaveBeforeClosing():
   251             return
   261             return
   252         dialog = ProjectDialog(self)
   262         dialog = ProjectDialog(self)
   303 
   313 
   304     def OnSaveProjectAsMenu(self, event):
   314     def OnSaveProjectAsMenu(self, event):
   305         self.SaveProjectAs()
   315         self.SaveProjectAs()
   306 
   316 
   307     def OnGenerateProgramMenu(self, event):
   317     def OnGenerateProgramMenu(self, event):
       
   318         result = self.Controler.GetProgramFilePath()
       
   319         if not result:
       
   320             self.GenerateProgramAs()
       
   321         else:
       
   322             self.GenerateProgram(result)
       
   323 
       
   324     def OnGenerateProgramAsMenu(self, event):
       
   325         self.GenerateProgramAs()
       
   326 
       
   327     def GenerateProgramAs(self):
   308         dialog = wx.FileDialog(self, _("Choose a file"), os.getcwd(), os.path.basename(self.Controler.GetProgramFilePath()),  _("ST files (*.st)|*.st|All files|*.*"), wx.SAVE | wx.CHANGE_DIR)
   328         dialog = wx.FileDialog(self, _("Choose a file"), os.getcwd(), os.path.basename(self.Controler.GetProgramFilePath()),  _("ST files (*.st)|*.st|All files|*.*"), wx.SAVE | wx.CHANGE_DIR)
   309         if dialog.ShowModal() == wx.ID_OK:
   329         if dialog.ShowModal() == wx.ID_OK:
   310             filepath = dialog.GetPath()
   330             self.GenerateProgram(dialog.GetPath())
   311             message_text = ""
   331         dialog.Destroy()
   312             header, icon = _("Done"), wx.ICON_INFORMATION
   332 
   313             if os.path.isdir(os.path.dirname(filepath)):
   333     def GenerateProgram(self, filepath=None):
   314                 _program, errors, warnings = self.Controler.GenerateProgram(filepath)
   334         message_text = ""
   315                 message_text += "".join([_("warning: %s\n") % warning for warning in warnings])
   335         header, icon = _("Done"), wx.ICON_INFORMATION
   316                 if len(errors) > 0:
   336         if os.path.isdir(os.path.dirname(filepath)):
   317                     message_text += "".join([_("error: %s\n") % error for error in errors])
   337             _program, errors, warnings = self.Controler.GenerateProgram(filepath)
   318                     message_text += _("Can't generate program to file %s!") % filepath
   338             message_text += "".join([_("warning: %s\n") % warning for warning in warnings])
   319                     header, icon = _("Error"), wx.ICON_ERROR
   339             if len(errors) > 0:
   320                 else:
   340                 message_text += "".join([_("error: %s\n") % error for error in errors])
   321                     message_text += _("Program was successfully generated!")
   341                 message_text += _("Can't generate program to file %s!") % filepath
       
   342                 header, icon = _("Error"), wx.ICON_ERROR
   322             else:
   343             else:
   323                 message_text += _("\"%s\" is not a valid folder!") % os.path.dirname(filepath)
   344                 message_text += _("Program was successfully generated!")
   324                 header, icon = _("Error"), wx.ICON_ERROR
   345         else:
   325             message = wx.MessageDialog(self, message_text, header, wx.OK | icon)
   346             message_text += _("\"%s\" is not a valid folder!") % os.path.dirname(filepath)
   326             message.ShowModal()
   347             header, icon = _("Error"), wx.ICON_ERROR
   327             message.Destroy()
   348         message = wx.MessageDialog(self, message_text, header, wx.OK | icon)
   328         dialog.Destroy()
   349         message.ShowModal()
       
   350         message.Destroy()
   329 
   351 
   330     def OnPLCOpenEditorMenu(self, event):
   352     def OnPLCOpenEditorMenu(self, event):
   331         wx.MessageBox(_("No documentation available.\nComing soon."))
   353         wx.MessageBox(_("No documentation available.\nComing soon."))
   332 
   354 
   333     def OnPLCOpenMenu(self, event):
   355     def OnPLCOpenMenu(self, event):