Merged in masterschlumpf/beremiz/generate-button (pull request #38)
authorAndrey Skvortsov <andrej.skvortzov@gmail.com>
Mon, 18 Mar 2019 07:13:04 +0000
changeset 2533 e09e4b4caff1
parent 2522 1378c18402c3 (current diff)
parent 2532 b9d6f5d7e0f1 (diff)
child 2548 82c10dcec29c
Merged in masterschlumpf/beremiz/generate-button (pull request #38)

"Generate Prgram" button

Approved-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
PLCOpenEditor.py
--- a/PLCOpenEditor.py	Mon Mar 11 17:20:15 2019 +0100
+++ b/PLCOpenEditor.py	Mon Mar 18 07:13:04 2019 +0000
@@ -62,7 +62,8 @@
 # Define PLCOpenEditor FileMenu extra items id
 [
     ID_PLCOPENEDITORFILEMENUGENERATE,
-] = [wx.NewId() for _init_coll_FileMenu_Items in range(1)]
+    ID_PLCOPENEDITORFILEMENUGENERATEAS,
+] = [wx.NewId() for _init_coll_FileMenu_Items in range(2)]
 
 
 beremiz_dir = paths.AbsDir(__file__)
@@ -86,6 +87,8 @@
                    kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S')
         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE,
                    kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G')
+        AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATEAS,
+                   kind=wx.ITEM_NORMAL, text=_(u'Generate Program As...') + '\tCTRL+SHIFT+G')
         parent.AppendSeparator()
         AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP,
                    kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P')
@@ -108,6 +111,8 @@
         self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS)
         self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu,
                   id=ID_PLCOPENEDITORFILEMENUGENERATE)
+        self.Bind(wx.EVT_MENU, self.OnGenerateProgramAsMenu,
+                  id=ID_PLCOPENEDITORFILEMENUGENERATEAS)
         self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP)
         self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW)
         self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT)
@@ -118,7 +123,8 @@
                                (wx.ID_OPEN, "open", _(u'Open'), None),
                                (wx.ID_SAVE, "save", _(u'Save'), None),
                                (wx.ID_SAVEAS, "saveas", _(u'Save As...'), None),
-                               (wx.ID_PRINT, "print", _(u'Print'), None)])
+                               (wx.ID_PRINT, "print", _(u'Print'), None),
+                               (ID_PLCOPENEDITORFILEMENUGENERATE, "Build", _(u'Generate Program'), None)])
 
     def _init_coll_HelpMenu_Items(self, parent):
         AppendMenu(parent, help='', id=wx.ID_HELP,
@@ -232,6 +238,8 @@
             self.FileMenu.Enable(wx.ID_SAVEAS, True)
             MenuToolBar.EnableTool(wx.ID_SAVEAS, True)
             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True)
+            MenuToolBar.EnableTool(ID_PLCOPENEDITORFILEMENUGENERATE, True)
+            self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATEAS, True)
         else:
             self.FileMenu.Enable(wx.ID_CLOSE, False)
             self.FileMenu.Enable(wx.ID_PAGE_SETUP, False)
@@ -245,6 +253,8 @@
             self.FileMenu.Enable(wx.ID_SAVEAS, False)
             MenuToolBar.EnableTool(wx.ID_SAVEAS, False)
             self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False)
+            MenuToolBar.EnableTool(ID_PLCOPENEDITORFILEMENUGENERATE, False)
+            self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATEAS, False)
 
     def OnNewProjectMenu(self, event):
         if self.Controler is not None and not self.CheckSaveBeforeClosing():
@@ -305,27 +315,39 @@
         self.SaveProjectAs()
 
     def OnGenerateProgramMenu(self, event):
+        result = self.Controler.GetProgramFilePath()
+        if not result:
+            self.GenerateProgramAs()
+        else:
+            self.GenerateProgram(result)
+
+    def OnGenerateProgramAsMenu(self, event):
+        self.GenerateProgramAs()
+
+    def GenerateProgramAs(self):
         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)
         if dialog.ShowModal() == wx.ID_OK:
-            filepath = dialog.GetPath()
-            message_text = ""
-            header, icon = _("Done"), wx.ICON_INFORMATION
-            if os.path.isdir(os.path.dirname(filepath)):
-                _program, errors, warnings = self.Controler.GenerateProgram(filepath)
-                message_text += "".join([_("warning: %s\n") % warning for warning in warnings])
-                if len(errors) > 0:
-                    message_text += "".join([_("error: %s\n") % error for error in errors])
-                    message_text += _("Can't generate program to file %s!") % filepath
-                    header, icon = _("Error"), wx.ICON_ERROR
-                else:
-                    message_text += _("Program was successfully generated!")
+            self.GenerateProgram(dialog.GetPath())
+        dialog.Destroy()
+
+    def GenerateProgram(self, filepath=None):
+        message_text = ""
+        header, icon = _("Done"), wx.ICON_INFORMATION
+        if os.path.isdir(os.path.dirname(filepath)):
+            _program, errors, warnings = self.Controler.GenerateProgram(filepath)
+            message_text += "".join([_("warning: %s\n") % warning for warning in warnings])
+            if len(errors) > 0:
+                message_text += "".join([_("error: %s\n") % error for error in errors])
+                message_text += _("Can't generate program to file %s!") % filepath
+                header, icon = _("Error"), wx.ICON_ERROR
             else:
-                message_text += _("\"%s\" is not a valid folder!") % os.path.dirname(filepath)
-                header, icon = _("Error"), wx.ICON_ERROR
-            message = wx.MessageDialog(self, message_text, header, wx.OK | icon)
-            message.ShowModal()
-            message.Destroy()
-        dialog.Destroy()
+                message_text += _("Program was successfully generated!")
+        else:
+            message_text += _("\"%s\" is not a valid folder!") % os.path.dirname(filepath)
+            header, icon = _("Error"), wx.ICON_ERROR
+        message = wx.MessageDialog(self, message_text, header, wx.OK | icon)
+        message.ShowModal()
+        message.Destroy()
 
     def OnPLCOpenEditorMenu(self, event):
         wx.MessageBox(_("No documentation available.\nComing soon."))