author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 04 Oct 2016 18:01:08 +0300 | |
changeset 1536 | 3518d10487db |
parent 1533 | aca8f43483e9 |
child 1560 | 4ce8492159ab |
permissions | -rwxr-xr-x |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
5 |
#based on the plcopen standard. |
814 | 6 |
# |
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
import os, sys, platform, time, traceback, getopt |
|
27 |
||
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
28 |
beremiz_dir = os.path.dirname(os.path.realpath(__file__)) |
814 | 29 |
|
30 |
__version__ = "$Revision: 1.130 $" |
|
31 |
||
32 |
if __name__ == '__main__': |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
33 |
# Usage message displayed when help request or when error detected in |
814 | 34 |
# command line |
35 |
def usage(): |
|
36 |
print "\nUsage of PLCOpenEditor.py :" |
|
37 |
print "\n %s [Filepath]\n"%sys.argv[0] |
|
38 |
||
39 |
# Parse options given to PLCOpenEditor in command line |
|
40 |
try: |
|
41 |
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) |
|
42 |
except getopt.GetoptError: |
|
43 |
# print help information and exit: |
|
44 |
usage() |
|
45 |
sys.exit(2) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
46 |
|
814 | 47 |
# Extract if help has been requested |
48 |
for o, a in opts: |
|
49 |
if o in ("-h", "--help"): |
|
50 |
usage() |
|
51 |
sys.exit() |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
52 |
|
814 | 53 |
# Extract the optional filename to open |
54 |
fileOpen = None |
|
55 |
if len(args) > 1: |
|
56 |
usage() |
|
57 |
sys.exit() |
|
58 |
elif len(args) == 1: |
|
59 |
fileOpen = args[0] |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
60 |
|
814 | 61 |
# Create wxApp (Need to create App before internationalization because of |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
62 |
# Windows) |
1533
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
63 |
if wx.VERSION >= (3, 0, 0): |
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
64 |
app = wx.App() |
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
65 |
else: |
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
66 |
app = wx.PySimpleApp() |
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
67 |
|
814 | 68 |
|
1388
67c9a9482d24
Factorized bitmap and i18n resources loading in between PLCopenEditor and Beremiz. Now in utils/misc.py
Edouard Tisserant
parents:
1330
diff
changeset
|
69 |
from util.misc import InstallLocalRessources |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
70 |
InstallLocalRessources(beremiz_dir) |
1388
67c9a9482d24
Factorized bitmap and i18n resources loading in between PLCopenEditor and Beremiz. Now in utils/misc.py
Edouard Tisserant
parents:
1330
diff
changeset
|
71 |
|
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
72 |
from docutil import * |
814 | 73 |
from IDEFrame import IDEFrame, AppendMenu |
74 |
from IDEFrame import TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE, PAGETITLES |
|
75 |
from IDEFrame import EncodeFileSystemPath, DecodeFileSystemPath |
|
76 |
from editors.Viewer import Viewer |
|
77 |
from PLCControler import PLCControler |
|
1030
d9e75a5b5f08
Fix bug when creating New project in PLCOpenEditor
Laurent Bessard
parents:
980
diff
changeset
|
78 |
from dialogs import ProjectDialog |
814 | 79 |
|
80 |
#------------------------------------------------------------------------------- |
|
81 |
# PLCOpenEditor Main Class |
|
82 |
#------------------------------------------------------------------------------- |
|
83 |
||
84 |
# Define PLCOpenEditor FileMenu extra items id |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
85 |
[ID_PLCOPENEDITORFILEMENUGENERATE, |
814 | 86 |
] = [wx.NewId() for _init_coll_FileMenu_Items in range(1)] |
87 |
||
88 |
class PLCOpenEditor(IDEFrame): |
|
89 |
||
90 |
# Compatibility function for wx versions < 2.6 |
|
91 |
if wx.VERSION < (2, 6, 0): |
|
92 |
def Bind(self, event, function, id = None): |
|
93 |
if id is not None: |
|
94 |
event(self, id, function) |
|
95 |
else: |
|
96 |
event(self, function) |
|
97 |
||
98 |
def _init_coll_FileMenu_Items(self, parent): |
|
99 |
AppendMenu(parent, help='', id=wx.ID_NEW, |
|
100 |
kind=wx.ITEM_NORMAL, text=_(u'New') +'\tCTRL+N') |
|
101 |
AppendMenu(parent, help='', id=wx.ID_OPEN, |
|
102 |
kind=wx.ITEM_NORMAL, text=_(u'Open') + '\tCTRL+O') |
|
103 |
AppendMenu(parent, help='', id=wx.ID_CLOSE, |
|
104 |
kind=wx.ITEM_NORMAL, text=_(u'Close Tab') + '\tCTRL+W') |
|
105 |
AppendMenu(parent, help='', id=wx.ID_CLOSE_ALL, |
|
106 |
kind=wx.ITEM_NORMAL, text=_(u'Close Project') + '\tCTRL+SHIFT+W') |
|
107 |
parent.AppendSeparator() |
|
108 |
AppendMenu(parent, help='', id=wx.ID_SAVE, |
|
109 |
kind=wx.ITEM_NORMAL, text=_(u'Save') + '\tCTRL+S') |
|
110 |
AppendMenu(parent, help='', id=wx.ID_SAVEAS, |
|
111 |
kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S') |
|
112 |
AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE, |
|
113 |
kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G') |
|
114 |
parent.AppendSeparator() |
|
115 |
AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, |
|
116 |
kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P') |
|
117 |
AppendMenu(parent, help='', id=wx.ID_PREVIEW, |
|
118 |
kind=wx.ITEM_NORMAL, text=_(u'Preview') + '\tCTRL+SHIFT+P') |
|
119 |
AppendMenu(parent, help='', id=wx.ID_PRINT, |
|
120 |
kind=wx.ITEM_NORMAL, text=_(u'Print') + '\tCTRL+P') |
|
121 |
parent.AppendSeparator() |
|
122 |
AppendMenu(parent, help='', id=wx.ID_PROPERTIES, |
|
123 |
kind=wx.ITEM_NORMAL, text=_(u'&Properties')) |
|
124 |
parent.AppendSeparator() |
|
125 |
AppendMenu(parent, help='', id=wx.ID_EXIT, |
|
126 |
kind=wx.ITEM_NORMAL, text=_(u'Quit') + '\tCTRL+Q') |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
127 |
|
814 | 128 |
self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) |
129 |
self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) |
|
130 |
self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) |
|
131 |
self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL) |
|
132 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) |
|
133 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS) |
|
134 |
self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu, |
|
135 |
id=ID_PLCOPENEDITORFILEMENUGENERATE) |
|
136 |
self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) |
|
137 |
self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) |
|
138 |
self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) |
|
139 |
self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) |
|
140 |
self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
141 |
|
814 | 142 |
self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None), |
143 |
(wx.ID_OPEN, "open", _(u'Open'), None), |
|
144 |
(wx.ID_SAVE, "save", _(u'Save'), None), |
|
145 |
(wx.ID_SAVEAS, "saveas", _(u'Save As...'), None), |
|
146 |
(wx.ID_PRINT, "print", _(u'Print'), None)]) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
147 |
|
814 | 148 |
def _init_coll_HelpMenu_Items(self, parent): |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
149 |
AppendMenu(parent, help='', id=wx.ID_HELP, |
814 | 150 |
kind=wx.ITEM_NORMAL, text=_(u'PLCOpenEditor') + '\tF1') |
151 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTENTS, |
|
152 |
# kind=wx.ITEM_NORMAL, text=u'PLCOpen\tF2') |
|
153 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTEXT, |
|
154 |
# kind=wx.ITEM_NORMAL, text=u'IEC 61131-3\tF3') |
|
155 |
AppendMenu(parent, help='', id=wx.ID_ABOUT, |
|
156 |
kind=wx.ITEM_NORMAL, text=_(u'About')) |
|
157 |
self.Bind(wx.EVT_MENU, self.OnPLCOpenEditorMenu, id=wx.ID_HELP) |
|
158 |
#self.Bind(wx.EVT_MENU, self.OnPLCOpenMenu, id=wx.ID_HELP_CONTENTS) |
|
159 |
self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) |
|
160 |
||
161 |
## Constructor of the PLCOpenEditor class. |
|
162 |
# @param parent The parent window. |
|
163 |
# @param controler The controler been used by PLCOpenEditor (default: None). |
|
164 |
# @param fileOpen The filepath to open if no controler defined (default: None). |
|
165 |
# @param debug The filepath to open if no controler defined (default: False). |
|
166 |
def __init__(self, parent, fileOpen = None): |
|
1533
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
167 |
self.icon = wx.Icon(os.path.join(beremiz_dir, "images", "poe.ico"), wx.BITMAP_TYPE_ICO) |
814 | 168 |
IDEFrame.__init__(self, parent) |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
169 |
|
814 | 170 |
result = None |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
171 |
|
814 | 172 |
# Open the filepath if defined |
173 |
if fileOpen is not None: |
|
174 |
fileOpen = DecodeFileSystemPath(fileOpen, False) |
|
175 |
if os.path.isfile(fileOpen): |
|
176 |
# Create a new controller |
|
177 |
controler = PLCControler() |
|
178 |
result = controler.OpenXMLFile(fileOpen) |
|
1330
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
179 |
self.Controler = controler |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
180 |
self.LibraryPanel.SetController(controler) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
181 |
self.ProjectTree.Enable(True) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
182 |
self.PouInstanceVariablesPanel.SetController(controler) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
183 |
self._Refresh(PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE) |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
184 |
|
814 | 185 |
# Define PLCOpenEditor icon |
1533
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
186 |
self.SetIcon(self.icon) |
814 | 187 |
|
188 |
self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
189 |
|
814 | 190 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU) |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
191 |
|
814 | 192 |
if result is not None: |
1330
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
193 |
self.ShowErrorMessage( |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
194 |
_("PLC syntax error at line %d:\n%s") % result) |
814 | 195 |
|
196 |
def OnCloseFrame(self, event): |
|
197 |
if self.Controler is None or self.CheckSaveBeforeClosing(_("Close Application")): |
|
198 |
self.AUIManager.UnInit() |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
199 |
|
814 | 200 |
self.SaveLastState() |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
201 |
|
814 | 202 |
event.Skip() |
203 |
else: |
|
204 |
event.Veto() |
|
205 |
||
206 |
def RefreshTitle(self): |
|
207 |
name = _("PLCOpenEditor") |
|
208 |
if self.Controler is not None: |
|
209 |
self.SetTitle("%s - %s"%(name, self.Controler.GetFilename())) |
|
210 |
else: |
|
211 |
self.SetTitle(name) |
|
212 |
||
213 |
#------------------------------------------------------------------------------- |
|
214 |
# File Menu Functions |
|
215 |
#------------------------------------------------------------------------------- |
|
216 |
||
217 |
def RefreshFileMenu(self): |
|
218 |
MenuToolBar = self.Panes["MenuToolBar"] |
|
219 |
if self.Controler is not None: |
|
220 |
selected = self.TabsOpened.GetSelection() |
|
221 |
if selected >= 0: |
|
222 |
graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) |
|
223 |
else: |
|
224 |
graphic_viewer = False |
|
225 |
if self.TabsOpened.GetPageCount() > 0: |
|
226 |
self.FileMenu.Enable(wx.ID_CLOSE, True) |
|
227 |
if graphic_viewer: |
|
228 |
self.FileMenu.Enable(wx.ID_PREVIEW, True) |
|
229 |
self.FileMenu.Enable(wx.ID_PRINT, True) |
|
230 |
MenuToolBar.EnableTool(wx.ID_PRINT, True) |
|
231 |
else: |
|
232 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
233 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
234 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
235 |
else: |
|
236 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
237 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
238 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
239 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
240 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) |
|
241 |
project_modified = not self.Controler.ProjectIsSaved() |
|
242 |
self.FileMenu.Enable(wx.ID_SAVE, project_modified) |
|
243 |
MenuToolBar.EnableTool(wx.ID_SAVE, project_modified) |
|
244 |
self.FileMenu.Enable(wx.ID_PROPERTIES, True) |
|
245 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, True) |
|
246 |
self.FileMenu.Enable(wx.ID_SAVEAS, True) |
|
247 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, True) |
|
248 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True) |
|
249 |
else: |
|
250 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
251 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) |
|
252 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
253 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
254 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
255 |
self.FileMenu.Enable(wx.ID_SAVE, False) |
|
256 |
MenuToolBar.EnableTool(wx.ID_SAVE, False) |
|
257 |
self.FileMenu.Enable(wx.ID_PROPERTIES, False) |
|
258 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, False) |
|
259 |
self.FileMenu.Enable(wx.ID_SAVEAS, False) |
|
260 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, False) |
|
261 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False) |
|
262 |
||
263 |
def OnNewProjectMenu(self, event): |
|
264 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
265 |
return |
|
266 |
dialog = ProjectDialog(self) |
|
267 |
if dialog.ShowModal() == wx.ID_OK: |
|
268 |
properties = dialog.GetValues() |
|
269 |
self.ResetView() |
|
270 |
self.Controler = PLCControler() |
|
271 |
self.Controler.CreateNewProject(properties) |
|
272 |
self.LibraryPanel.SetController(self.Controler) |
|
1295
2ad168756c5e
Fixed bug no contextual menu displayed in left panel tree when creating new project
Laurent Bessard
parents:
1030
diff
changeset
|
273 |
self.ProjectTree.Enable(True) |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
274 |
self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, |
814 | 275 |
LIBRARYTREE) |
276 |
||
277 |
def OnOpenProjectMenu(self, event): |
|
278 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
279 |
return |
|
280 |
filepath = "" |
|
281 |
if self.Controler is not None: |
|
282 |
filepath = self.Controler.GetFilePath() |
|
283 |
if filepath != "": |
|
284 |
directory = os.path.dirname(filepath) |
|
285 |
else: |
|
286 |
directory = os.getcwd() |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
287 |
|
814 | 288 |
result = None |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
289 |
|
814 | 290 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, "", _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.OPEN) |
291 |
if dialog.ShowModal() == wx.ID_OK: |
|
292 |
filepath = dialog.GetPath() |
|
293 |
if os.path.isfile(filepath): |
|
294 |
self.ResetView() |
|
295 |
controler = PLCControler() |
|
296 |
result = controler.OpenXMLFile(filepath) |
|
1330
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
297 |
self.Controler = controler |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
298 |
self.LibraryPanel.SetController(controler) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
299 |
self.ProjectTree.Enable(True) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
300 |
self.PouInstanceVariablesPanel.SetController(controler) |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
301 |
self._Refresh(PROJECTTREE, LIBRARYTREE) |
814 | 302 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
303 |
dialog.Destroy() |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
304 |
|
814 | 305 |
if result is not None: |
1330
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
306 |
self.ShowErrorMessage( |
96b242e4c59d
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents:
1312
diff
changeset
|
307 |
_("PLC syntax error at line %d:\n%s") % result) |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
308 |
|
814 | 309 |
def OnCloseProjectMenu(self, event): |
310 |
if not self.CheckSaveBeforeClosing(): |
|
311 |
return |
|
312 |
self.ResetView() |
|
313 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
|
314 |
||
315 |
def OnSaveProjectMenu(self, event): |
|
316 |
self.SaveProject() |
|
317 |
||
318 |
def OnSaveProjectAsMenu(self, event): |
|
319 |
self.SaveProjectAs() |
|
320 |
||
321 |
def OnGenerateProgramMenu(self, event): |
|
322 |
dialog = wx.FileDialog(self, _("Choose a file"), os.getcwd(), self.Controler.GetProgramFilePath(), _("ST files (*.st)|*.st|All files|*.*"), wx.SAVE|wx.CHANGE_DIR) |
|
323 |
if dialog.ShowModal() == wx.ID_OK: |
|
324 |
filepath = dialog.GetPath() |
|
325 |
message_text = "" |
|
326 |
header, icon = _("Done"), wx.ICON_INFORMATION |
|
327 |
if os.path.isdir(os.path.dirname(filepath)): |
|
328 |
program, errors, warnings = self.Controler.GenerateProgram(filepath) |
|
1312
250c3ae0787c
Fixed bug when printing program generating errors and warnings in PLCOpenEditor
Laurent Bessard
parents:
1295
diff
changeset
|
329 |
message_text += "".join([_("warning: %s\n") % warning for warning in warnings]) |
814 | 330 |
if len(errors) > 0: |
1312
250c3ae0787c
Fixed bug when printing program generating errors and warnings in PLCOpenEditor
Laurent Bessard
parents:
1295
diff
changeset
|
331 |
message_text += "".join([_("error: %s\n") % error for error in errors]) |
814 | 332 |
message_text += _("Can't generate program to file %s!")%filepath |
333 |
header, icon = _("Error"), wx.ICON_ERROR |
|
334 |
else: |
|
335 |
message_text += _("Program was successfully generated!") |
|
336 |
else: |
|
337 |
message_text += _("\"%s\" is not a valid folder!")%os.path.dirname(filepath) |
|
338 |
header, icon = _("Error"), wx.ICON_ERROR |
|
339 |
message = wx.MessageDialog(self, message_text, header, wx.OK|icon) |
|
340 |
message.ShowModal() |
|
341 |
message.Destroy() |
|
342 |
dialog.Destroy() |
|
343 |
||
344 |
def OnPLCOpenEditorMenu(self, event): |
|
345 |
wx.MessageBox(_("No documentation available.\nComing soon.")) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
346 |
|
814 | 347 |
def OnPLCOpenMenu(self, event): |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
348 |
open_pdf(os.path.join(beremiz_dir, "plcopen", "TC6_XML_V101.pdf")) |
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
349 |
|
814 | 350 |
def OnAboutMenu(self, event): |
1492
14ce1a631f44
add translation into Russian for dialog window "About PLCOpen Editor"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1451
diff
changeset
|
351 |
OpenHtmlFrame(self,_("About PLCOpenEditor"), os.path.join(beremiz_dir, "doc", _("plcopen_about.html")), wx.Size(350, 350)) |
814 | 352 |
|
353 |
def SaveProject(self): |
|
354 |
result = self.Controler.SaveXMLFile() |
|
355 |
if not result: |
|
356 |
self.SaveProjectAs() |
|
357 |
else: |
|
358 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
359 |
|
814 | 360 |
def SaveProjectAs(self): |
361 |
filepath = self.Controler.GetFilePath() |
|
362 |
if filepath != "": |
|
363 |
directory, filename = os.path.split(filepath) |
|
364 |
else: |
|
365 |
directory, filename = os.getcwd(), "%(projectName)s.xml"%self.Controler.GetProjectProperties() |
|
366 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, filename, _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.SAVE|wx.OVERWRITE_PROMPT) |
|
367 |
if dialog.ShowModal() == wx.ID_OK: |
|
368 |
filepath = dialog.GetPath() |
|
369 |
if os.path.isdir(os.path.dirname(filepath)): |
|
370 |
result = self.Controler.SaveXMLFile(filepath) |
|
371 |
if not result: |
|
372 |
self.ShowErrorMessage(_("Can't save project to file %s!")%filepath) |
|
373 |
else: |
|
374 |
self.ShowErrorMessage(_("\"%s\" is not a valid folder!")%os.path.dirname(filepath)) |
|
375 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
376 |
dialog.Destroy() |
|
377 |
||
378 |
#------------------------------------------------------------------------------- |
|
379 |
# Exception Handler |
|
380 |
#------------------------------------------------------------------------------- |
|
381 |
||
382 |
Max_Traceback_List_Size = 20 |
|
383 |
||
384 |
def Display_Exception_Dialog(e_type,e_value,e_tb): |
|
385 |
trcbck_lst = [] |
|
386 |
for i,line in enumerate(traceback.extract_tb(e_tb)): |
|
387 |
trcbck = " " + str(i+1) + _(". ") |
|
388 |
if line[0].find(os.getcwd()) == -1: |
|
389 |
trcbck += _("file : ") + str(line[0]) + _(", ") |
|
390 |
else: |
|
391 |
trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") |
|
392 |
trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) |
|
393 |
trcbck_lst.append(trcbck) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
394 |
|
814 | 395 |
# Allow clicking.... |
396 |
cap = wx.Window_GetCapture() |
|
397 |
if cap: |
|
398 |
cap.ReleaseMouse() |
|
399 |
||
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
400 |
dlg = wx.SingleChoiceDialog(None, |
814 | 401 |
_(""" |
402 |
An error has occurred. |
|
403 |
||
404 |
Click OK to save an error report. |
|
405 |
||
406 |
Please be kind enough to send this file to: |
|
407 |
edouard.tisserant@gmail.com |
|
408 |
||
409 |
Error: |
|
410 |
""") + |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
411 |
str(e_type) + _(" : ") + str(e_value), |
814 | 412 |
_("Error"), |
413 |
trcbck_lst) |
|
414 |
try: |
|
415 |
res = (dlg.ShowModal() == wx.ID_OK) |
|
416 |
finally: |
|
417 |
dlg.Destroy() |
|
418 |
||
419 |
return res |
|
420 |
||
421 |
def Display_Error_Dialog(e_value): |
|
422 |
message = wx.MessageDialog(None, str(e_value), _("Error"), wx.OK|wx.ICON_ERROR) |
|
423 |
message.ShowModal() |
|
424 |
message.Destroy() |
|
425 |
||
426 |
def get_last_traceback(tb): |
|
427 |
while tb.tb_next: |
|
428 |
tb = tb.tb_next |
|
429 |
return tb |
|
430 |
||
431 |
||
432 |
def format_namespace(d, indent=' '): |
|
433 |
return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) |
|
434 |
||
435 |
||
436 |
ignored_exceptions = [] # a problem with a line in a module is only reported once per session |
|
437 |
||
438 |
def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
439 |
|
814 | 440 |
def handle_exception(e_type, e_value, e_traceback): |
441 |
traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func |
|
442 |
last_tb = get_last_traceback(e_traceback) |
|
443 |
ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) |
|
444 |
if str(e_value).startswith("!!!"): |
|
445 |
Display_Error_Dialog(e_value) |
|
446 |
elif ex not in ignored_exceptions: |
|
447 |
result = Display_Exception_Dialog(e_type,e_value,e_traceback) |
|
448 |
if result: |
|
449 |
ignored_exceptions.append(ex) |
|
450 |
info = { |
|
451 |
'app-title' : wx.GetApp().GetAppName(), # app_title |
|
452 |
'app-version' : app_version, |
|
453 |
'wx-version' : wx.VERSION_STRING, |
|
454 |
'wx-platform' : wx.Platform, |
|
455 |
'python-version' : platform.python_version(), #sys.version.split()[0], |
|
456 |
'platform' : platform.platform(), |
|
457 |
'e-type' : e_type, |
|
458 |
'e-value' : e_value, |
|
459 |
'date' : time.ctime(), |
|
460 |
'cwd' : os.getcwd(), |
|
461 |
} |
|
462 |
if e_traceback: |
|
463 |
info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) |
|
464 |
last_tb = get_last_traceback(e_traceback) |
|
465 |
exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred |
|
466 |
info['locals'] = format_namespace(exception_locals) |
|
467 |
if 'self' in exception_locals: |
|
468 |
info['self'] = format_namespace(exception_locals['self'].__dict__) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
469 |
|
814 | 470 |
output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w') |
471 |
lst = info.keys() |
|
472 |
lst.sort() |
|
473 |
for a in lst: |
|
474 |
output.write(a+":\n"+str(info[a])+"\n\n") |
|
475 |
||
476 |
#sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) |
|
477 |
sys.excepthook = handle_exception |
|
478 |
||
479 |
if __name__ == '__main__': |
|
1533
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
480 |
if wx.VERSION < (3, 0, 0): |
aca8f43483e9
fix issues in PLCOpenEditor with moving from wxWidgets 2.8 to 3.0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1492
diff
changeset
|
481 |
wx.InitAllImageHandlers() |
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
482 |
|
814 | 483 |
# Install a exception handle for bug reports |
484 |
AddExceptHook(os.getcwd(),__version__) |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
485 |
|
814 | 486 |
frame = PLCOpenEditor(None, fileOpen=fileOpen) |
487 |
||
488 |
frame.Show() |
|
489 |
app.MainLoop() |
|
1451
94e620cbd9de
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
Edouard Tisserant
parents:
1388
diff
changeset
|
490 |