2 # -*- coding: utf-8 -*-
4 #This file is part of CanFestival, a library implementing CanOpen Stack.
6 #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
8 #See COPYING file for copyrights details.
10 #This library is free software; you can redistribute it and/or
11 #modify it under the terms of the GNU Lesser General Public
12 #License as published by the Free Software Foundation; either
13 #version 2.1 of the License, or (at your option) any later version.
15 #This library is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 #Lesser General Public License for more details.
20 #You should have received a copy of the GNU Lesser General Public
21 #License along with this library; if not, write to the Free Software
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 import os, re, platform, sys, time, traceback, getopt
30 __version__ = "$Revision: 1.20 $"
32 from nodelist import *
33 from nodemanager import *
34 from subindextable import *
35 from commondialogs import *
36 from doc_index.DS301_index import *
38 ScriptDirectory = os.path.split(os.path.realpath(__file__))[0]
43 EVT_HTML_URL_CLICK = wx.NewId()
45 class HtmlWindowUrlClick(wx.PyEvent):
46 def __init__(self, linkinfo):
47 wx.PyEvent.__init__(self)
48 self.SetEventType(EVT_HTML_URL_CLICK)
49 self.linkinfo = (linkinfo.GetHref(), linkinfo.GetTarget())
51 class UrlClickHtmlWindow(wx.html.HtmlWindow):
52 """ HTML window that generates and OnLinkClicked event.
54 Use this to avoid having to override HTMLWindow
56 def OnLinkClicked(self, linkinfo):
57 wx.PostEvent(self, HtmlWindowUrlClick(linkinfo))
59 def Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY):
60 if event == HtmlWindowUrlClick:
61 self.Connect(-1, -1, EVT_HTML_URL_CLICK, handler)
63 wx.html.HtmlWindow.Bind(event, handler, source=source, id=id, id2=id2)
65 #-------------------------------------------------------------------------------
67 #-------------------------------------------------------------------------------
69 [ID_HTMLFRAME, ID_HTMLFRAMEHTMLCONTENT] = [wx.NewId() for _init_ctrls in range(2)]
71 class HtmlFrame(wx.Frame):
72 def _init_ctrls(self, prnt):
73 # generated method, don't edit
74 wx.Frame.__init__(self, id=ID_HTMLFRAME, name='HtmlFrame',
75 parent=prnt, pos=wx.Point(320, 231), size=wx.Size(853, 616),
76 style=wx.DEFAULT_FRAME_STYLE, title='')
77 self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
79 self.HtmlContent = UrlClickHtmlWindow(id=ID_HTMLFRAMEHTMLCONTENT,
80 name='HtmlContent', parent=self, pos=wx.Point(0, 0),
81 size=wx.Size(-1, -1), style=wx.html.HW_SCROLLBAR_AUTO|wx.html.HW_NO_SELECTION)
82 self.HtmlContent.Bind(HtmlWindowUrlClick, self.OnLinkClick)
84 def __init__(self, parent, opened):
85 self._init_ctrls(parent)
86 self.HtmlFrameOpened = opened
88 def SetHtmlCode(self, htmlcode):
89 self.HtmlContent.SetPage(htmlcode)
91 def SetHtmlPage(self, htmlpage):
92 self.HtmlContent.LoadPage(htmlpage)
94 def OnCloseFrame(self, event):
95 self.HtmlFrameOpened.remove(self.GetTitle())
98 def OnLinkClick(self, event):
99 url = event.linkinfo[0]
103 wx.MessageBox('Please point your browser at: %s' % url)
112 [ID_NETWORKEDIT, ID_NETWORKEDITNETWORKNODES,
113 ID_NETWORKEDITHELPBAR,
114 ] = [wx.NewId() for _init_ctrls in range(3)]
116 [ID_NETWORKEDITADDMENUITEMS0, ID_NETWORKEDITADDMENUITEMS1,
117 ID_NETWORKEDITADDMENUITEMS2, ID_NETWORKEDITADDMENUITEMS3,
118 ID_NETWORKEDITADDMENUITEMS4, ID_NETWORKEDITADDMENUITEMS5,
119 ] = [wx.NewId() for _init_coll_AddMenu_Items in range(6)]
121 [ID_NETWORKEDITFILEMENUITEMS0, ID_NETWORKEDITFILEMENUITEMS1,
122 ID_NETWORKEDITFILEMENUITEMS2, ID_NETWORKEDITFILEMENUITEMS4,
123 ID_NETWORKEDITFILEMENUITEMS5, ID_NETWORKEDITFILEMENUITEMS6,
124 ] = [wx.NewId() for _init_coll_FileMenu_Items in range(6)]
126 [ID_NETWORKEDITNETWORKMENUITEMS0, ID_NETWORKEDITNETWORKMENUITEMS1,
127 ID_NETWORKEDITNETWORKMENUITEMS3,
128 ] = [wx.NewId() for _init_coll_AddMenu_Items in range(3)]
131 [ID_NETWORKEDITEDITMENUITEMS0, ID_NETWORKEDITEDITMENUITEMS1,
132 ID_NETWORKEDITEDITMENUITEMS2, ID_NETWORKEDITEDITMENUITEMS4,
133 ID_NETWORKEDITEDITMENUITEMS6, ID_NETWORKEDITEDITMENUITEMS7,
134 ID_NETWORKEDITEDITMENUITEMS8,
135 ] = [wx.NewId() for _init_coll_EditMenu_Items in range(7)]
137 [ID_NETWORKEDITHELPMENUITEMS0, ID_NETWORKEDITHELPMENUITEMS1,
138 ID_NETWORKEDITHELPMENUITEMS2,
139 ] = [wx.NewId() for _init_coll_HelpMenu_Items in range(3)]
141 class networkedit(wx.Frame):
142 def _init_coll_menuBar1_Menus(self, parent):
144 parent.Append(menu=self.FileMenu, title='File')
145 parent.Append(menu=self.NetworkMenu, title='Network')
146 parent.Append(menu=self.EditMenu, title='Edit')
147 parent.Append(menu=self.AddMenu, title='Add')
148 parent.Append(menu=self.HelpMenu, title='Help')
150 def _init_coll_EditMenu_Items(self, parent):
151 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS4,
152 kind=wx.ITEM_NORMAL, text='Refresh\tCTRL+R')
153 parent.AppendSeparator()
154 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS1,
155 kind=wx.ITEM_NORMAL, text='Undo\tCTRL+Z')
156 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS0,
157 kind=wx.ITEM_NORMAL, text='Redo\tCTRL+Y')
158 parent.AppendSeparator()
159 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS6,
160 kind=wx.ITEM_NORMAL, text='Node infos')
161 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS2,
162 kind=wx.ITEM_NORMAL, text='DS-301 Profile')
163 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS8,
164 kind=wx.ITEM_NORMAL, text='DS-302 Profile')
165 parent.Append(help='', id=ID_NETWORKEDITEDITMENUITEMS7,
166 kind=wx.ITEM_NORMAL, text='Other Profile')
167 self.Bind(wx.EVT_MENU, self.OnUndoMenu,
168 id=ID_NETWORKEDITEDITMENUITEMS1)
169 self.Bind(wx.EVT_MENU, self.OnRedoMenu,
170 id=ID_NETWORKEDITEDITMENUITEMS0)
171 self.Bind(wx.EVT_MENU, self.OnCommunicationMenu,
172 id=ID_NETWORKEDITEDITMENUITEMS2)
173 self.Bind(wx.EVT_MENU, self.OnRefreshMenu,
174 id=ID_NETWORKEDITEDITMENUITEMS4)
175 self.Bind(wx.EVT_MENU, self.OnNodeInfosMenu,
176 id=ID_NETWORKEDITEDITMENUITEMS6)
177 self.Bind(wx.EVT_MENU, self.OnEditProfileMenu,
178 id=ID_NETWORKEDITEDITMENUITEMS7)
179 self.Bind(wx.EVT_MENU, self.OnOtherCommunicationMenu,
180 id=ID_NETWORKEDITEDITMENUITEMS8)
182 def _init_coll_HelpMenu_Items(self, parent):
183 parent.Append(help='', id=ID_NETWORKEDITHELPMENUITEMS0,
184 kind=wx.ITEM_NORMAL, text='DS-301 Standard\tF1')
185 self.Bind(wx.EVT_MENU, self.OnHelpDS301Menu,
186 id=ID_NETWORKEDITHELPMENUITEMS0)
187 parent.Append(help='', id=ID_NETWORKEDITHELPMENUITEMS1,
188 kind=wx.ITEM_NORMAL, text='CAN Festival Docs\tF2')
189 self.Bind(wx.EVT_MENU, self.OnHelpCANFestivalMenu,
190 id=ID_NETWORKEDITHELPMENUITEMS1)
191 if Html_Window and self.ModeSolo:
192 parent.Append(help='', id=ID_NETWORKEDITHELPMENUITEMS2,
193 kind=wx.ITEM_NORMAL, text='About')
194 self.Bind(wx.EVT_MENU, self.OnAboutMenu,
195 id=ID_NETWORKEDITHELPMENUITEMS2)
197 def _init_coll_FileMenu_Items(self, parent):
198 parent.Append(help='', id=ID_NETWORKEDITFILEMENUITEMS5,
199 kind=wx.ITEM_NORMAL, text='New\tCTRL+N')
200 parent.Append(help='', id=ID_NETWORKEDITFILEMENUITEMS0,
201 kind=wx.ITEM_NORMAL, text='Open\tCTRL+O')
202 parent.Append(help='', id=ID_NETWORKEDITFILEMENUITEMS1,
203 kind=wx.ITEM_NORMAL, text='Save\tCTRL+S')
204 parent.Append(help='', id=ID_NETWORKEDITFILEMENUITEMS2,
205 kind=wx.ITEM_NORMAL, text='Close\tCTRL+W')
206 parent.AppendSeparator()
207 parent.Append(help='', id=ID_NETWORKEDITFILEMENUITEMS4,
208 kind=wx.ITEM_NORMAL, text='Exit')
209 self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu,
210 id=ID_NETWORKEDITFILEMENUITEMS0)
211 self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu,
212 id=ID_NETWORKEDITFILEMENUITEMS1)
213 self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu,
214 id=ID_NETWORKEDITFILEMENUITEMS2)
215 self.Bind(wx.EVT_MENU, self.OnQuitMenu,
216 id=ID_NETWORKEDITFILEMENUITEMS4)
217 self.Bind(wx.EVT_MENU, self.OnNewProjectMenu,
218 id=ID_NETWORKEDITFILEMENUITEMS5)
220 def _init_coll_NetworkMenu_Items(self, parent):
221 parent.Append(help='', id=ID_NETWORKEDITNETWORKMENUITEMS0,
222 kind=wx.ITEM_NORMAL, text='Add Slave Node')
223 parent.Append(help='', id=ID_NETWORKEDITNETWORKMENUITEMS1,
224 kind=wx.ITEM_NORMAL, text='Remove Slave Node')
225 parent.AppendSeparator()
226 parent.Append(help='', id=ID_NETWORKEDITNETWORKMENUITEMS3,
227 kind=wx.ITEM_NORMAL, text='Build Master Dictionary')
228 self.Bind(wx.EVT_MENU, self.OnAddSlaveMenu,
229 id=ID_NETWORKEDITNETWORKMENUITEMS0)
230 self.Bind(wx.EVT_MENU, self.OnRemoveSlaveMenu,
231 id=ID_NETWORKEDITNETWORKMENUITEMS1)
232 ## self.Bind(wx.EVT_MENU, self.OnBuildMasterMenu,
233 ## id=ID_NETWORKEDITNETWORKMENUITEMS3)
235 def _init_coll_AddMenu_Items(self, parent):
236 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS0,
237 kind=wx.ITEM_NORMAL, text='SDO Server')
238 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS1,
239 kind=wx.ITEM_NORMAL, text='SDO Client')
240 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS2,
241 kind=wx.ITEM_NORMAL, text='PDO Transmit')
242 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS3,
243 kind=wx.ITEM_NORMAL, text='PDO Receive')
244 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS4,
245 kind=wx.ITEM_NORMAL, text='Map Variable')
246 parent.Append(help='', id=ID_NETWORKEDITADDMENUITEMS5,
247 kind=wx.ITEM_NORMAL, text='User Type')
248 self.Bind(wx.EVT_MENU, self.OnAddSDOServerMenu,
249 id=ID_NETWORKEDITADDMENUITEMS0)
250 self.Bind(wx.EVT_MENU, self.OnAddSDOClientMenu,
251 id=ID_NETWORKEDITADDMENUITEMS1)
252 self.Bind(wx.EVT_MENU, self.OnAddPDOTransmitMenu,
253 id=ID_NETWORKEDITADDMENUITEMS2)
254 self.Bind(wx.EVT_MENU, self.OnAddPDOReceiveMenu,
255 id=ID_NETWORKEDITADDMENUITEMS3)
256 self.Bind(wx.EVT_MENU, self.OnAddMapVariableMenu,
257 id=ID_NETWORKEDITADDMENUITEMS4)
258 self.Bind(wx.EVT_MENU, self.OnAddUserTypeMenu,
259 id=ID_NETWORKEDITADDMENUITEMS5)
261 def _init_coll_HelpBar_Fields(self, parent):
262 parent.SetFieldsCount(3)
264 parent.SetStatusText(number=0, text='')
265 parent.SetStatusText(number=1, text='')
266 parent.SetStatusText(number=2, text='')
268 parent.SetStatusWidths([100, 110, -1])
270 def _init_utils(self):
271 self.menuBar1 = wx.MenuBar()
272 self.menuBar1.SetEvtHandlerEnabled(True)
275 self.FileMenu = wx.Menu(title='')
276 self.NetworkMenu = wx.Menu(title='')
277 self.EditMenu = wx.Menu(title='')
278 self.AddMenu = wx.Menu(title='')
279 self.HelpMenu = wx.Menu(title='')
281 self._init_coll_menuBar1_Menus(self.menuBar1)
283 self._init_coll_FileMenu_Items(self.FileMenu)
284 self._init_coll_NetworkMenu_Items(self.NetworkMenu)
285 self._init_coll_EditMenu_Items(self.EditMenu)
286 self._init_coll_AddMenu_Items(self.AddMenu)
287 self._init_coll_HelpMenu_Items(self.HelpMenu)
289 def _init_ctrls(self, prnt):
290 wx.Frame.__init__(self, id=ID_NETWORKEDIT, name='networkedit',
291 parent=prnt, pos=wx.Point(149, 178), size=wx.Size(1000, 700),
292 style=wx.DEFAULT_FRAME_STYLE, title='Networkedit')
294 self.SetClientSize(wx.Size(1000, 700))
295 self.SetMenuBar(self.menuBar1)
296 self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
297 if not self.ModeSolo:
298 self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=ID_NETWORKEDITFILEMENUITEMS1)
299 accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL, 83, ID_NETWORKEDITFILEMENUITEMS1)])
300 self.SetAcceleratorTable(accel)
302 self.NetworkNodes = wx.Notebook(id=ID_NETWORKEDITNETWORKNODES,
303 name='NetworkNodes', parent=self, pos=wx.Point(0, 0),
304 size=wx.Size(0, 0), style=wx.NB_LEFT)
305 self.NetworkNodes.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED,
306 self.OnNodeSelectedChanged, id=ID_NETWORKEDITNETWORKNODES)
308 self.HelpBar = wx.StatusBar(id=ID_NETWORKEDITHELPBAR, name='HelpBar',
309 parent=self, style=wx.ST_SIZEGRIP)
310 self._init_coll_HelpBar_Fields(self.HelpBar)
311 self.SetStatusBar(self.HelpBar)
313 def __init__(self, parent, nodelist = None, projectOpen = None):
314 self.ModeSolo = nodelist == None
315 self._init_ctrls(parent)
316 self.HtmlFrameOpened = []
319 # Add beremiz's icon in top left corner of the frame
320 if wx.Platform == '__WXMSW__':
321 icon = wx.Icon(os.path.join(ScriptDirectory,"networkedit.ico"),wx.BITMAP_TYPE_ICO)
323 icon = wx.Icon(os.path.join(ScriptDirectory,"networkedit.png"),wx.BITMAP_TYPE_PNG)
327 self.Manager = NodeManager()
329 self.NodeList = NodeList(self.Manager)
330 result = self.NodeList.LoadProject(projectOpen)
332 self.RefreshNetworkNodes()
336 self.NodeList = nodelist
337 self.Manager = self.NodeList.GetManager()
338 self.NodeList.SetCurrentSelected(0)
339 self.RefreshNetworkNodes()
340 self.RefreshProfileMenu()
341 self.NetworkNodes.SetFocus()
343 self.RefreshBufferState()
345 self.RefreshMainMenu()
347 def SetBusId(self, bus_id):
353 def GetCurrentNodeId(self):
354 selected = self.NetworkNodes.GetSelection()
355 # At init selected = -1
357 window = self.NetworkNodes.GetPage(selected)
358 return window.GetIndex()
362 def OnCloseFrame(self, event):
363 if not self.ModeSolo and getattr(self, "_onclose", None) != None:
367 def OnChar(self, event):
368 if event.ControlDown() and event.GetKeyCode() == 83 and getattr(self, "_onsave", None) != None:
372 def OnQuitMenu(self, event):
376 def OnAddSDOServerMenu(self, event):
377 self.Manager.AddSDOServerToCurrent()
378 self.RefreshBufferState()
379 self.RefreshCurrentIndexList()
382 def OnAddSDOClientMenu(self, event):
383 self.Manager.AddSDOClientToCurrent()
384 self.RefreshBufferState()
385 self.RefreshCurrentIndexList()
388 def OnAddPDOTransmitMenu(self, event):
389 self.Manager.AddPDOTransmitToCurrent()
390 self.RefreshBufferState()
391 self.RefreshCurrentIndexList()
394 def OnAddPDOReceiveMenu(self, event):
395 self.Manager.AddPDOReceiveToCurrent()
396 self.RefreshBufferState()
397 self.RefreshCurrentIndexList()
400 def OnAddMapVariableMenu(self, event):
401 self.AddMapVariable()
404 def OnAddUserTypeMenu(self, event):
408 def OnNodeSelectedChanged(self, event):
409 selected = event.GetSelection()
410 # At init selected = -1
412 window = self.NetworkNodes.GetPage(selected)
413 self.NodeList.SetCurrentSelected(window.GetIndex())
414 wx.CallAfter(self.RefreshMainMenu)
415 wx.CallAfter(self.RefreshStatusBar)
418 #-------------------------------------------------------------------------------
419 # Load and Save Funtions
420 #-------------------------------------------------------------------------------
422 def OnNewProjectMenu(self, event):
424 defaultpath = os.path.dirname(self.NodeList.GetRoot())
426 defaultpath = os.getcwd()
427 dialog = wx.DirDialog(self , "Choose a project", defaultpath, wx.DD_NEW_DIR_BUTTON)
428 if dialog.ShowModal() == wx.ID_OK:
429 projectpath = dialog.GetPath()
430 if os.path.isdir(projectpath) and len(os.listdir(projectpath)) == 0:
431 manager = NodeManager()
432 nodelist = NodeList(manager)
433 result = nodelist.LoadProject(projectpath)
435 self.Manager = manager
436 self.NodeList = nodelist
437 self.NodeList.SetCurrentSelected(0)
439 self.RefreshNetworkNodes()
440 self.RefreshBufferState()
442 self.RefreshProfileMenu()
443 self.RefreshMainMenu()
445 message = wx.MessageDialog(self, result, "ERROR", wx.OK|wx.ICON_ERROR)
450 def OnOpenProjectMenu(self, event):
452 defaultpath = os.path.dirname(self.NodeList.GetRoot())
454 defaultpath = os.getcwd()
455 dialog = wx.DirDialog(self , "Choose a project", defaultpath, 0)
456 if dialog.ShowModal() == wx.ID_OK:
457 projectpath = dialog.GetPath()
458 if os.path.isdir(projectpath):
459 manager = NodeManager()
460 nodelist = NodeList(manager)
461 result = nodelist.LoadProject(projectpath)
463 self.Manager = manager
464 self.NodeList = nodelist
465 self.NodeList.SetCurrentSelected(0)
467 self.RefreshNetworkNodes()
468 self.RefreshBufferState()
470 self.RefreshProfileMenu()
471 self.RefreshMainMenu()
473 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
479 def OnSaveProjectMenu(self, event):
480 if not self.ModeSolo and getattr(self, "_onsave", None) != None:
483 result = self.NodeList.SaveProject()
485 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
490 def OnCloseProjectMenu(self, event):
492 if self.NodeList.HasChanged():
493 dialog = wx.MessageDialog(self, "There are changes, do you want to save?", "Close Project", wx.YES_NO|wx.CANCEL|wx.ICON_QUESTION)
494 answer = dialog.ShowModal()
496 if answer == wx.ID_YES:
497 result = self.NodeList.SaveProject()
499 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
502 elif answer == wx.ID_NO:
503 self.NodeList.ForceChanged(False)
504 if not self.NodeList.HasChanged():
507 self.RefreshNetworkNodes()
509 self.RefreshMainMenu()
512 #-------------------------------------------------------------------------------
513 # Slave Nodes Management
514 #-------------------------------------------------------------------------------
516 def OnAddSlaveMenu(self, event):
517 dialog = AddSlaveDialog(self)
518 dialog.SetNodeList(self.NodeList)
519 if dialog.ShowModal() == wx.ID_OK:
520 values = dialog.GetValues()
521 result = self.NodeList.AddSlaveNode(values["slaveName"], values["slaveNodeID"], values["edsFile"])
523 new_editingpanel = EditingPanel(self.NetworkNodes, self, self.NodeList, False)
524 new_editingpanel.SetIndex(values["slaveNodeID"])
525 idx = self.NodeList.GetOrderNumber(values["slaveNodeID"])
526 self.NetworkNodes.InsertPage(idx, new_editingpanel, "")
527 self.NodeList.SetCurrentSelected(idx)
528 self.NetworkNodes.SetSelection(idx)
529 self.RefreshBufferState()
531 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
537 def OnRemoveSlaveMenu(self, event):
538 slavenames = self.NodeList.GetSlaveNames()
539 slaveids = self.NodeList.GetSlaveIDs()
540 dialog = wx.SingleChoiceDialog(self, "Choose a slave to remove", "Remove slave", slavenames)
541 if dialog.ShowModal() == wx.ID_OK:
542 choice = dialog.GetSelection()
543 result = self.NodeList.RemoveSlaveNode(slaveids[choice])
546 current = self.NetworkNodes.GetSelection()
547 self.NetworkNodes.DeletePage(choice + 1)
548 if self.NetworkNodes.GetPageCount() > 0:
549 new_selection = min(current, self.NetworkNodes.GetPageCount() - 1)
550 self.NetworkNodes.SetSelection(new_selection)
551 if new_selection > 0:
552 self.NodeList.SetCurrentSelected(slaveids[new_selection - 1])
553 self.RefreshBufferState()
555 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
560 #-------------------------------------------------------------------------------
562 #-------------------------------------------------------------------------------
564 def RefreshTitle(self):
565 if self.NodeList != None:
566 self.SetTitle("Networkedit - %s"%self.NodeList.GetNetworkName())
568 self.SetTitle("Networkedit")
570 def OnRefreshMenu(self, event):
571 self.RefreshCurrentIndexList()
574 def RefreshCurrentIndexList(self):
575 selected = self.NetworkNodes.GetSelection()
577 window = self.NetworkNodes.GetPage(selected)
578 window.RefreshIndexList()
582 def RefreshNetworkNodes(self):
583 if self.NetworkNodes.GetPageCount() > 0:
584 self.NetworkNodes.DeleteAllPages()
586 new_editingpanel = EditingPanel(self.NetworkNodes, self, self.Manager)
587 new_editingpanel.SetIndex(0)
588 self.NetworkNodes.AddPage(new_editingpanel, "")
589 for idx in self.NodeList.GetSlaveIDs():
590 new_editingpanel = EditingPanel(self.NetworkNodes, self, self.NodeList, False)
591 new_editingpanel.SetIndex(idx)
592 self.NetworkNodes.AddPage(new_editingpanel, "")
594 def RefreshStatusBar(self):
596 selected = self.NetworkNodes.GetSelection()
597 if self.HelpBar and selected >= 0:
598 window = self.NetworkNodes.GetPage(selected)
599 selection = window.GetSelection()
601 index, subIndex = selection
602 if self.NodeList.IsCurrentEntry(index):
603 self.HelpBar.SetStatusText("Index: 0x%04X"%index, 0)
604 self.HelpBar.SetStatusText("Subindex: 0x%02X"%subIndex, 1)
605 entryinfos = self.NodeList.GetEntryInfos(index)
606 name = entryinfos["name"]
607 category = "Optional"
608 if entryinfos["need"]:
609 category = "Mandatory"
612 if entryinfos["struct"] & OD_IdenticalIndexes:
613 number = " possibly defined %d times"%entryinfos["nbmax"]
614 if entryinfos["struct"] & OD_IdenticalSubindexes:
616 elif entryinfos["struct"] & OD_MultipleSubindexes:
618 text = "%s: %s entry of struct %s%s."%(name,category,struct,number)
619 self.HelpBar.SetStatusText(text, 2)
622 self.HelpBar.SetStatusText("", i)
625 self.HelpBar.SetStatusText("", i)
627 def RefreshMainMenu(self):
629 self.NetworkMenu.Enable(ID_NETWORKEDITNETWORKMENUITEMS3, False)
630 if self.NodeList == None:
632 self.menuBar1.EnableTop(1, False)
633 self.menuBar1.EnableTop(2, False)
634 self.menuBar1.EnableTop(3, False)
636 self.FileMenu.Enable(ID_NETWORKEDITFILEMENUITEMS1, False)
637 self.FileMenu.Enable(ID_NETWORKEDITFILEMENUITEMS2, False)
639 self.menuBar1.EnableTop(0, False)
640 self.menuBar1.EnableTop(1, False)
641 self.menuBar1.EnableTop(2, False)
644 self.menuBar1.EnableTop(1, True)
646 self.FileMenu.Enable(ID_NETWORKEDITFILEMENUITEMS1, True)
647 self.FileMenu.Enable(ID_NETWORKEDITFILEMENUITEMS2, True)
648 if self.NetworkNodes.GetSelection() == 0:
649 self.menuBar1.EnableTop(2, True)
650 self.menuBar1.EnableTop(3, True)
652 self.menuBar1.EnableTop(2, False)
653 self.menuBar1.EnableTop(3, False)
655 self.menuBar1.EnableTop(0, True)
656 if self.NetworkNodes.GetSelection() == 0:
657 self.menuBar1.EnableTop(1, True)
658 self.menuBar1.EnableTop(2, True)
660 self.menuBar1.EnableTop(1, False)
661 self.menuBar1.EnableTop(2, False)
663 def RefreshProfileMenu(self):
665 profile = self.Manager.GetCurrentProfileName()
666 edititem = self.EditMenu.FindItemById(ID_NETWORKEDITEDITMENUITEMS7)
668 length = self.AddMenu.GetMenuItemCount()
669 for i in xrange(length-6):
670 additem = self.AddMenu.FindItemByPosition(6)
671 self.AddMenu.Delete(additem.GetId())
672 if profile not in ("None", "DS-301"):
673 edititem.SetText("%s Profile"%profile)
674 edititem.Enable(True)
675 self.AddMenu.AppendSeparator()
676 for text, indexes in self.Manager.GetCurrentSpecificMenu():
678 self.AddMenu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=text)
679 self.Bind(wx.EVT_MENU, self.GetProfileCallBack(text), id=new_id)
681 edititem.SetText("Other Profile")
682 edititem.Enable(False)
684 def GetProfileCallBack(self, text):
685 def ProfileCallBack(event):
686 self.Manager.AddSpecificEntryToCurrent(text)
687 self.RefreshBufferState()
688 self.RefreshCurrentIndexList()
690 return ProfileCallBack
692 #-------------------------------------------------------------------------------
694 #-------------------------------------------------------------------------------
696 def RefreshBufferState(self):
698 nodeID = self.Manager.GetCurrentNodeID()
700 nodename = "0x%2.2X %s"%(nodeID, self.Manager.GetCurrentNodeName())
702 nodename = self.Manager.GetCurrentNodeName()
703 self.NetworkNodes.SetPageText(0, nodename)
704 for idx, name in enumerate(self.NodeList.GetSlaveNames()):
705 self.NetworkNodes.SetPageText(idx + 1, name)
708 def OnUndoMenu(self, event):
709 self.Manager.LoadCurrentPrevious()
710 self.RefreshCurrentIndexList()
711 self.RefreshBufferState()
714 def OnRedoMenu(self, event):
715 self.Manager.LoadCurrentNext()
716 self.RefreshCurrentIndexList()
717 self.RefreshBufferState()
720 #-------------------------------------------------------------------------------
722 #-------------------------------------------------------------------------------
724 def OnHelpDS301Menu(self, event):
726 selected = self.NetworkNodes.GetSelection()
728 window = self.NetworkNodes.GetPage(selected)
729 result = window.GetSelection()
732 index, subIndex = result
733 result = OpenPDFDocIndex(index, ScriptDirectory)
734 if type(result) == StringType:
735 message = wx.MessageDialog(self, result, "ERROR", wx.OK|wx.ICON_ERROR)
739 result = OpenPDFDocIndex(None, ScriptDirectory)
740 if type(result) == StringType:
741 message = wx.MessageDialog(self, result, "ERROR", wx.OK|wx.ICON_ERROR)
746 def OnHelpCANFestivalMenu(self, event):
747 #self.OpenHtmlFrame("CAN Festival Reference", os.path.join(ScriptDirectory, "doc/canfestival.html"), wx.Size(1000, 600))
748 if wx.Platform == '__WXMSW__':
749 readerpath = get_acroversion()
750 readerexepath = os.path.join(readerpath,"AcroRd32.exe")
751 if(os.path.isfile(readerexepath)):
752 os.spawnl(os.P_DETACH, readerexepath, "AcroRd32.exe", '"%s"'%os.path.join(ScriptDirectory, "doc","manual_en.pdf"))
754 os.system("xpdf -remote CANFESTIVAL %s %d &"%(os.path.join(ScriptDirectory, "doc/manual_en.pdf"),16))
757 def OnAboutMenu(self, event):
758 self.OpenHtmlFrame("About CAN Festival", os.path.join(ScriptDirectory, "doc/about.html"), wx.Size(500, 450))
761 def OpenHtmlFrame(self, title, file, size):
762 if title not in self.HtmlFrameOpened:
763 self.HtmlFrameOpened.append(title)
764 window = HtmlFrame(self, self.HtmlFrameOpened)
765 window.SetTitle(title)
766 window.SetHtmlPage(file)
767 window.SetClientSize(size)
770 #-------------------------------------------------------------------------------
771 # Editing Profiles functions
772 #-------------------------------------------------------------------------------
774 def OnCommunicationMenu(self, event):
775 dictionary,current = self.Manager.GetCurrentCommunicationLists()
776 self.EditProfile("Edit DS-301 Profile", dictionary, current)
779 def OnOtherCommunicationMenu(self, event):
780 dictionary,current = self.Manager.GetCurrentDS302Lists()
781 self.EditProfile("Edit DS-301 Profile", dictionary, current)
784 def OnEditProfileMenu(self, event):
785 title = "Edit %s Profile"%self.Manager.GetCurrentProfileName()
786 dictionary,current = self.Manager.GetCurrentProfileLists()
787 self.EditProfile(title, dictionary, current)
790 def EditProfile(self, title, dictionary, current):
791 dialog = CommunicationDialog(self)
792 dialog.SetTitle(title)
793 dialog.SetIndexDictionary(dictionary)
794 dialog.SetCurrentList(current)
795 dialog.RefreshLists()
796 if dialog.ShowModal() == wx.ID_OK:
797 new_profile = dialog.GetCurrentList()
800 for index in new_profile:
801 if index not in current:
802 addinglist.append(index)
803 for index in current:
804 if index not in new_profile:
805 removinglist.append(index)
806 self.Manager.ManageEntriesOfCurrent(addinglist, removinglist)
807 self.Manager.BufferCurrentNode()
808 self.RefreshBufferState()
809 self.RefreshCurrentIndexList()
812 #-------------------------------------------------------------------------------
813 # Edit Node informations function
814 #-------------------------------------------------------------------------------
816 def OnNodeInfosMenu(self, event):
817 dialog = NodeInfosDialog(self)
818 name, id, type, description = self.Manager.GetCurrentNodeInfos()
819 dialog.SetValues(name, id, type, description)
820 if dialog.ShowModal() == wx.ID_OK:
821 name, id, type, description = dialog.GetValues()
822 self.Manager.SetCurrentNodeInfos(name, id, type, description)
823 self.RefreshBufferState()
824 self.RefreshProfileMenu()
825 selected = self.NetworkNodes.GetSelection()
827 window = self.NetworkNodes.GetPage(selected)
828 window.RefreshTable()
832 #-------------------------------------------------------------------------------
833 # Add User Types and Variables
834 #-------------------------------------------------------------------------------
836 def AddMapVariable(self):
837 index = self.Manager.GetCurrentNextMapIndex()
839 dialog = MapVariableDialog(self)
840 dialog.SetIndex(index)
841 if dialog.ShowModal() == wx.ID_OK:
842 index, name, struct, number = dialog.GetValues()
843 result = self.Manager.AddMapVariableToCurrent(index, name, struct, number)
844 if type(result) != StringType:
845 self.RefreshBufferState()
846 self.RefreshCurrentIndexList()
848 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
853 message = wx.MessageDialog(self, result, "No map variable index left!", wx.OK|wx.ICON_ERROR)
857 def AddUserType(self):
858 dialog = UserTypeDialog(self)
859 dialog.SetTypeList(self.Manager.GetCustomisableTypes())
860 if dialog.ShowModal() == wx.ID_OK:
861 type, min, max, length = dialog.GetValues()
862 result = self.Manager.AddUserTypeToCurrent(type, min, max, length)
863 if not IsOfType(result, StringType):
864 self.RefreshBufferState()
865 self.RefreshCurrentIndexList()
867 message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
872 def OpenMasterDCFDialog(self, node_id):
873 self.NetworkNodes.SetSelection(0)
874 self.NetworkNodes.GetPage(0).OpenDCFDialog(node_id)
876 #-------------------------------------------------------------------------------
878 #-------------------------------------------------------------------------------
880 Max_Traceback_List_Size = 20
882 def Display_Exception_Dialog(e_type,e_value,e_tb):
884 for i,line in enumerate(traceback.extract_tb(e_tb)):
885 trcbck = " " + str(i+1) + ". "
886 if line[0].find(os.getcwd()) == -1:
887 trcbck += "file : " + str(line[0]) + ", "
889 trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ", "
890 trcbck += "line : " + str(line[1]) + ", " + "function : " + str(line[2])
891 trcbck_lst.append(trcbck)
894 cap = wx.Window_GetCapture()
898 dlg = wx.SingleChoiceDialog(None,
902 Click on OK for saving an error report.
904 Please contact LOLITech at:
906 bugs_networkedit@lolitech.fr
911 str(e_type) + " : " + str(e_value),
915 res = (dlg.ShowModal() == wx.ID_OK)
921 def Display_Error_Dialog(e_value):
922 message = wx.MessageDialog(None, str(e_value), "Error", wx.OK|wx.ICON_ERROR)
926 def get_last_traceback(tb):
932 def format_namespace(d, indent=' '):
933 return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
936 ignored_exceptions = [] # a problem with a line in a module is only reported once per session
938 def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]):
940 def handle_exception(e_type, e_value, e_traceback):
941 traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func
942 last_tb = get_last_traceback(e_traceback)
943 ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
944 if str(e_value).startswith("!!!"):
945 Display_Error_Dialog(e_value)
946 elif ex not in ignored_exceptions:
947 ignored_exceptions.append(ex)
948 result = Display_Exception_Dialog(e_type,e_value,e_traceback)
951 'app-title' : wx.GetApp().GetAppName(), # app_title
952 'app-version' : app_version,
953 'wx-version' : wx.VERSION_STRING,
954 'wx-platform' : wx.Platform,
955 'python-version' : platform.python_version(), #sys.version.split()[0],
956 'platform' : platform.platform(),
959 'date' : time.ctime(),
963 info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
964 last_tb = get_last_traceback(e_traceback)
965 exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred
966 info['locals'] = format_namespace(exception_locals)
967 if 'self' in exception_locals:
968 info['self'] = format_namespace(exception_locals['self'].__dict__)
970 output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w')
974 output.write(a+":\n"+str(info[a])+"\n\n")
976 #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
977 sys.excepthook = handle_exception
979 if __name__ == '__main__':
981 print "\nUsage of networkedit.py :"
982 print "\n %s [Projectpath]\n"%sys.argv[0]
985 opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
986 except getopt.GetoptError:
987 # print help information and exit:
992 if o in ("-h", "--help"):
999 projectOpen = args[0]
1004 app = wx.PySimpleApp()
1005 wx.InitAllImageHandlers()
1007 # Install a exception handle for bug reports
1008 AddExceptHook(os.getcwd(),__version__)
1010 frame = networkedit(None, projectOpen=projectOpen)