lbessard@205: #!/usr/bin/env python lbessard@205: # -*- coding: utf-8 -*- lbessard@205: lbessard@205: #This file is part of CanFestival, a library implementing CanOpen Stack. lbessard@205: # lbessard@205: #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD lbessard@205: # lbessard@205: #See COPYING file for copyrights details. lbessard@205: # lbessard@205: #This library is free software; you can redistribute it and/or lbessard@205: #modify it under the terms of the GNU Lesser General Public lbessard@205: #License as published by the Free Software Foundation; either lbessard@205: #version 2.1 of the License, or (at your option) any later version. lbessard@205: # lbessard@205: #This library is distributed in the hope that it will be useful, lbessard@205: #but WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@205: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@205: #Lesser General Public License for more details. lbessard@205: # lbessard@205: #You should have received a copy of the GNU Lesser General Public lbessard@205: #License along with this library; if not, write to the Free Software lbessard@205: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA lbessard@205: lbessard@205: import wx lbessard@205: lbessard@205: from types import * lbessard@205: import os, re, platform, sys, time, traceback, getopt lbessard@205: laurent@580: __version__ = "$Revision: 1.27 $" laurent@580: laurent@580: if __name__ == '__main__': laurent@580: def usage(): laurent@580: print _("\nUsage of networkedit.py :") laurent@580: print "\n %s [Projectpath]\n"%sys.argv[0] laurent@580: laurent@580: try: laurent@580: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) laurent@580: except getopt.GetoptError: laurent@580: # print help information and exit: laurent@580: usage() laurent@580: sys.exit(2) laurent@580: laurent@580: for o, a in opts: laurent@580: if o in ("-h", "--help"): laurent@580: usage() laurent@580: sys.exit() laurent@580: laurent@580: if len(args) == 0: laurent@580: projectOpen = None laurent@580: elif len(args) == 1: laurent@580: projectOpen = args[0] laurent@580: else: laurent@580: usage() laurent@580: sys.exit(2) laurent@580: laurent@580: app = wx.PySimpleApp() laurent@580: laurent@580: ScriptDirectory = os.path.split(os.path.realpath(__file__))[0] laurent@580: laurent@580: # Import module for internationalization laurent@580: import gettext laurent@580: import __builtin__ laurent@580: laurent@580: # Get folder containing translation files laurent@580: localedir = os.path.join(ScriptDirectory,"locale") laurent@580: # Get the default language laurent@580: langid = wx.LANGUAGE_DEFAULT laurent@580: # Define translation domain (name of translation files) laurent@580: domain = "objdictgen" laurent@580: laurent@580: # Define locale for wx laurent@580: loc = __builtin__.__dict__.get('loc', None) laurent@580: if loc is None: laurent@580: loc = wx.Locale(langid) laurent@580: __builtin__.__dict__['loc'] = loc laurent@580: # Define location for searching translation files laurent@580: loc.AddCatalogLookupPathPrefix(localedir) laurent@580: # Define locale domain laurent@580: loc.AddCatalog(domain) laurent@580: laurent@580: if __name__ == '__main__': laurent@580: __builtin__.__dict__['_'] = wx.GetTranslation lbessard@205: lbessard@205: from nodelist import * lbessard@205: from nodemanager import * Laurent@778: from networkeditortemplate import NetworkEditorTemplate lbessard@205: from doc_index.DS301_index import * lbessard@205: lbessard@205: try: lbessard@254: import wx.html lbessard@254: lbessard@254: EVT_HTML_URL_CLICK = wx.NewId() lbessard@254: lbessard@254: class HtmlWindowUrlClick(wx.PyEvent): lbessard@205: def __init__(self, linkinfo): lbessard@254: wx.PyEvent.__init__(self) lbessard@254: self.SetEventType(EVT_HTML_URL_CLICK) lbessard@205: self.linkinfo = (linkinfo.GetHref(), linkinfo.GetTarget()) lbessard@254: lbessard@254: class UrlClickHtmlWindow(wx.html.HtmlWindow): lbessard@205: """ HTML window that generates and OnLinkClicked event. lbessard@205: lbessard@205: Use this to avoid having to override HTMLWindow lbessard@205: """ lbessard@205: def OnLinkClicked(self, linkinfo): lbessard@254: wx.PostEvent(self, HtmlWindowUrlClick(linkinfo)) lbessard@254: lbessard@254: def Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY): lbessard@254: if event == HtmlWindowUrlClick: lbessard@254: self.Connect(-1, -1, EVT_HTML_URL_CLICK, handler) lbessard@254: else: lbessard@254: wx.html.HtmlWindow.Bind(event, handler, source=source, id=id, id2=id2) lbessard@205: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Html Frame lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@254: [ID_HTMLFRAME, ID_HTMLFRAMEHTMLCONTENT] = [wx.NewId() for _init_ctrls in range(2)] lbessard@205: lbessard@205: class HtmlFrame(wx.Frame): lbessard@205: def _init_ctrls(self, prnt): lbessard@205: # generated method, don't edit lbessard@254: wx.Frame.__init__(self, id=ID_HTMLFRAME, name='HtmlFrame', lbessard@205: parent=prnt, pos=wx.Point(320, 231), size=wx.Size(853, 616), lbessard@205: style=wx.DEFAULT_FRAME_STYLE, title='') lbessard@273: self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) lbessard@205: lbessard@254: self.HtmlContent = UrlClickHtmlWindow(id=ID_HTMLFRAMEHTMLCONTENT, lbessard@205: name='HtmlContent', parent=self, pos=wx.Point(0, 0), lbessard@254: size=wx.Size(-1, -1), style=wx.html.HW_SCROLLBAR_AUTO|wx.html.HW_NO_SELECTION) lbessard@254: self.HtmlContent.Bind(HtmlWindowUrlClick, self.OnLinkClick) lbessard@205: lbessard@205: def __init__(self, parent, opened): lbessard@205: self._init_ctrls(parent) lbessard@205: self.HtmlFrameOpened = opened lbessard@205: lbessard@205: def SetHtmlCode(self, htmlcode): lbessard@205: self.HtmlContent.SetPage(htmlcode) lbessard@205: lbessard@205: def SetHtmlPage(self, htmlpage): lbessard@205: self.HtmlContent.LoadPage(htmlpage) lbessard@205: lbessard@205: def OnCloseFrame(self, event): lbessard@205: self.HtmlFrameOpened.remove(self.GetTitle()) lbessard@205: event.Skip() lbessard@205: lbessard@205: def OnLinkClick(self, event): lbessard@205: url = event.linkinfo[0] lbessard@205: try: lbessard@205: import webbrowser lbessard@205: except ImportError: laurent@580: wx.MessageBox(_('Please point your browser at: %s') % url) lbessard@205: else: lbessard@205: webbrowser.open(url) lbessard@205: lbessard@205: Html_Window = True lbessard@205: except: lbessard@205: Html_Window = False lbessard@205: lbessard@254: [ID_NETWORKEDIT, ID_NETWORKEDITNETWORKNODES, lbessard@254: ID_NETWORKEDITHELPBAR, lbessard@205: ] = [wx.NewId() for _init_ctrls in range(3)] lbessard@205: lbessard@418: [ID_NETWORKEDITNETWORKMENUBUILDMASTER, lbessard@418: ] = [wx.NewId() for _init_coll_AddMenu_Items in range(1)] lbessard@418: lbessard@418: [ID_NETWORKEDITEDITMENUNODEINFOS, ID_NETWORKEDITEDITMENUDS301PROFILE, lbessard@418: ID_NETWORKEDITEDITMENUDS302PROFILE, ID_NETWORKEDITEDITMENUOTHERPROFILE, lbessard@418: ] = [wx.NewId() for _init_coll_EditMenu_Items in range(4)] lbessard@418: lbessard@418: [ID_NETWORKEDITADDMENUSDOSERVER, ID_NETWORKEDITADDMENUSDOCLIENT, lbessard@418: ID_NETWORKEDITADDMENUPDOTRANSMIT, ID_NETWORKEDITADDMENUPDORECEIVE, lbessard@418: ID_NETWORKEDITADDMENUMAPVARIABLE, ID_NETWORKEDITADDMENUUSERTYPE, lbessard@205: ] = [wx.NewId() for _init_coll_AddMenu_Items in range(6)] lbessard@205: smarteh-dev@715: class networkedit(wx.Frame, NetworkEditorTemplate): smarteh-dev@715: smarteh-dev@715: EDITMENU_ID = ID_NETWORKEDITEDITMENUOTHERPROFILE smarteh-dev@715: lbessard@418: def _init_coll_MenuBar_Menus(self, parent): lbessard@242: if self.ModeSolo: laurent@580: parent.Append(menu=self.FileMenu, title=_('File')) laurent@580: parent.Append(menu=self.NetworkMenu, title=_('Network')) laurent@580: parent.Append(menu=self.EditMenu, title=_('Edit')) laurent@580: parent.Append(menu=self.AddMenu, title=_('Add')) laurent@580: parent.Append(menu=self.HelpMenu, title=_('Help')) lbessard@205: lbessard@418: def _init_coll_FileMenu_Items(self, parent): lbessard@418: parent.Append(help='', id=wx.ID_NEW, laurent@580: kind=wx.ITEM_NORMAL, text=_('New\tCTRL+N')) lbessard@418: parent.Append(help='', id=wx.ID_OPEN, laurent@580: kind=wx.ITEM_NORMAL, text=_('Open\tCTRL+O')) lbessard@418: parent.Append(help='', id=wx.ID_CLOSE, laurent@580: kind=wx.ITEM_NORMAL, text=_('Close\tCTRL+W')) lbessard@418: parent.AppendSeparator() lbessard@418: parent.Append(help='', id=wx.ID_SAVE, laurent@580: kind=wx.ITEM_NORMAL, text=_('Save\tCTRL+S')) lbessard@418: parent.AppendSeparator() lbessard@418: parent.Append(help='', id=wx.ID_EXIT, laurent@580: kind=wx.ITEM_NORMAL, text=_('Exit')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) lbessard@418: self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) lbessard@418: self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE) lbessard@418: self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) lbessard@418: self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) lbessard@418: lbessard@418: def _init_coll_NetworkMenu_Items(self, parent): lbessard@418: parent.Append(help='', id=wx.ID_ADD, laurent@580: kind=wx.ITEM_NORMAL, text=_('Add Slave Node')) lbessard@418: parent.Append(help='', id=wx.ID_DELETE, laurent@580: kind=wx.ITEM_NORMAL, text=_('Remove Slave Node')) lbessard@418: parent.AppendSeparator() lbessard@418: parent.Append(help='', id=ID_NETWORKEDITNETWORKMENUBUILDMASTER, laurent@580: kind=wx.ITEM_NORMAL, text=_('Build Master Dictionary')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnAddSlaveMenu, id=wx.ID_ADD) lbessard@418: self.Bind(wx.EVT_MENU, self.OnRemoveSlaveMenu, id=wx.ID_DELETE) lbessard@418: ## self.Bind(wx.EVT_MENU, self.OnBuildMasterMenu, lbessard@418: ## id=ID_NETWORKEDITNETWORKMENUBUILDMASTER) lbessard@418: lbessard@205: def _init_coll_EditMenu_Items(self, parent): lbessard@418: parent.Append(help='', id=wx.ID_REFRESH, laurent@580: kind=wx.ITEM_NORMAL, text=_('Refresh\tCTRL+R')) lbessard@205: parent.AppendSeparator() lbessard@418: parent.Append(help='', id=wx.ID_UNDO, laurent@580: kind=wx.ITEM_NORMAL, text=_('Undo\tCTRL+Z')) lbessard@418: parent.Append(help='', id=wx.ID_REDO, laurent@580: kind=wx.ITEM_NORMAL, text=_('Redo\tCTRL+Y')) lbessard@205: parent.AppendSeparator() lbessard@418: parent.Append(help='', id=ID_NETWORKEDITEDITMENUNODEINFOS, laurent@580: kind=wx.ITEM_NORMAL, text=_('Node infos')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITEDITMENUDS301PROFILE, laurent@580: kind=wx.ITEM_NORMAL, text=_('DS-301 Profile')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITEDITMENUDS302PROFILE, laurent@580: kind=wx.ITEM_NORMAL, text=_('DS-302 Profile')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITEDITMENUOTHERPROFILE, laurent@580: kind=wx.ITEM_NORMAL, text=_('Other Profile')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnRefreshMenu, id=wx.ID_REFRESH) lbessard@418: self.Bind(wx.EVT_MENU, self.OnUndoMenu, id=wx.ID_UNDO) lbessard@418: self.Bind(wx.EVT_MENU, self.OnRedoMenu, id=wx.ID_REDO) lbessard@418: self.Bind(wx.EVT_MENU, self.OnNodeInfosMenu, lbessard@418: id=ID_NETWORKEDITEDITMENUNODEINFOS) lbessard@205: self.Bind(wx.EVT_MENU, self.OnCommunicationMenu, lbessard@418: id=ID_NETWORKEDITEDITMENUDS301PROFILE) lbessard@418: self.Bind(wx.EVT_MENU, self.OnOtherCommunicationMenu, lbessard@418: id=ID_NETWORKEDITEDITMENUDS302PROFILE) lbessard@205: self.Bind(wx.EVT_MENU, self.OnEditProfileMenu, lbessard@418: id=ID_NETWORKEDITEDITMENUOTHERPROFILE) lbessard@418: lbessard@205: def _init_coll_AddMenu_Items(self, parent): lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUSDOSERVER, laurent@580: kind=wx.ITEM_NORMAL, text=_('SDO Server')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUSDOCLIENT, laurent@580: kind=wx.ITEM_NORMAL, text=_('SDO Client')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUPDOTRANSMIT, laurent@580: kind=wx.ITEM_NORMAL, text=_('PDO Transmit')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUPDORECEIVE, laurent@580: kind=wx.ITEM_NORMAL, text=_('PDO Receive')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUMAPVARIABLE, laurent@580: kind=wx.ITEM_NORMAL, text=_('Map Variable')) lbessard@418: parent.Append(help='', id=ID_NETWORKEDITADDMENUUSERTYPE, laurent@580: kind=wx.ITEM_NORMAL, text=_('User Type')) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddSDOServerMenu, lbessard@418: id=ID_NETWORKEDITADDMENUSDOSERVER) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddSDOClientMenu, lbessard@418: id=ID_NETWORKEDITADDMENUSDOCLIENT) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddPDOTransmitMenu, lbessard@418: id=ID_NETWORKEDITADDMENUPDOTRANSMIT) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddPDOReceiveMenu, lbessard@418: id=ID_NETWORKEDITADDMENUPDORECEIVE) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddMapVariableMenu, lbessard@418: id=ID_NETWORKEDITADDMENUMAPVARIABLE) lbessard@205: self.Bind(wx.EVT_MENU, self.OnAddUserTypeMenu, lbessard@418: id=ID_NETWORKEDITADDMENUUSERTYPE) lbessard@418: lbessard@418: def _init_coll_HelpMenu_Items(self, parent): lbessard@418: parent.Append(help='', id=wx.ID_HELP, laurent@580: kind=wx.ITEM_NORMAL, text=_('DS-301 Standard\tF1')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnHelpDS301Menu, id=wx.ID_HELP) lbessard@418: parent.Append(help='', id=wx.ID_HELP_CONTEXT, laurent@580: kind=wx.ITEM_NORMAL, text=_('CAN Festival Docs\tF2')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnHelpCANFestivalMenu, id=wx.ID_HELP_CONTEXT) lbessard@418: if Html_Window and self.ModeSolo: lbessard@418: parent.Append(help='', id=wx.ID_ABOUT, laurent@580: kind=wx.ITEM_NORMAL, text=_('About')) lbessard@418: self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) lbessard@205: lbessard@205: def _init_coll_HelpBar_Fields(self, parent): lbessard@205: parent.SetFieldsCount(3) lbessard@205: lbessard@205: parent.SetStatusText(number=0, text='') lbessard@205: parent.SetStatusText(number=1, text='') lbessard@205: parent.SetStatusText(number=2, text='') lbessard@205: lbessard@205: parent.SetStatusWidths([100, 110, -1]) lbessard@205: lbessard@205: def _init_utils(self): lbessard@418: self.MenuBar = wx.MenuBar() lbessard@418: self.MenuBar.SetEvtHandlerEnabled(True) lbessard@205: lbessard@242: if self.ModeSolo: lbessard@205: self.FileMenu = wx.Menu(title='') lbessard@205: self.NetworkMenu = wx.Menu(title='') lbessard@205: self.EditMenu = wx.Menu(title='') lbessard@205: self.AddMenu = wx.Menu(title='') lbessard@205: self.HelpMenu = wx.Menu(title='') lbessard@205: lbessard@418: self._init_coll_MenuBar_Menus(self.MenuBar) lbessard@242: if self.ModeSolo: lbessard@205: self._init_coll_FileMenu_Items(self.FileMenu) lbessard@205: self._init_coll_NetworkMenu_Items(self.NetworkMenu) lbessard@205: self._init_coll_EditMenu_Items(self.EditMenu) lbessard@205: self._init_coll_AddMenu_Items(self.AddMenu) lbessard@205: self._init_coll_HelpMenu_Items(self.HelpMenu) lbessard@205: lbessard@205: def _init_ctrls(self, prnt): lbessard@254: wx.Frame.__init__(self, id=ID_NETWORKEDIT, name='networkedit', lbessard@205: parent=prnt, pos=wx.Point(149, 178), size=wx.Size(1000, 700), laurent@580: style=wx.DEFAULT_FRAME_STYLE, title=_('Networkedit')) lbessard@205: self._init_utils() lbessard@205: self.SetClientSize(wx.Size(1000, 700)) lbessard@418: self.SetMenuBar(self.MenuBar) lbessard@273: self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) lbessard@271: if not self.ModeSolo: lbessard@418: self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) lbessard@418: accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL, 83, wx.ID_SAVE)]) lbessard@271: self.SetAcceleratorTable(accel) lbessard@254: smarteh-dev@715: NetworkEditorTemplate._init_ctrls(self, self) lbessard@254: lbessard@254: self.HelpBar = wx.StatusBar(id=ID_NETWORKEDITHELPBAR, name='HelpBar', lbessard@254: parent=self, style=wx.ST_SIZEGRIP) lbessard@205: self._init_coll_HelpBar_Fields(self.HelpBar) lbessard@205: self.SetStatusBar(self.HelpBar) lbessard@205: lbessard@276: def __init__(self, parent, nodelist = None, projectOpen = None): smarteh-dev@715: if nodelist is None: smarteh-dev@715: NetworkEditorTemplate.__init__(self, NodeList(NodeManager()), self, True) smarteh-dev@715: else: smarteh-dev@715: NetworkEditorTemplate.__init__(self, nodelist, self, False) lbessard@205: self._init_ctrls(parent) lbessard@205: self.HtmlFrameOpened = [] lbessard@205: etisserant@410: icon = wx.Icon(os.path.join(ScriptDirectory,"networkedit.ico"),wx.BITMAP_TYPE_ICO) greg@409: self.SetIcon(icon) etisserant@410: lbessard@242: if self.ModeSolo: lbessard@205: if projectOpen: lbessard@205: result = self.NodeList.LoadProject(projectOpen) lbessard@205: if not result: smarteh-dev@715: self.NodeList.SetCurrentSelected(0) lbessard@205: self.RefreshNetworkNodes() smarteh-dev@715: self.RefreshProfileMenu() lbessard@205: else: lbessard@205: self.NodeList = None lbessard@205: else: lbessard@242: self.NodeList.SetCurrentSelected(0) lbessard@242: self.RefreshNetworkNodes() lbessard@242: self.RefreshProfileMenu() smarteh-dev@715: self.NetworkNodes.SetFocus() lbessard@205: lbessard@205: self.RefreshBufferState() lbessard@205: self.RefreshTitle() lbessard@205: self.RefreshMainMenu() lbessard@205: lbessard@242: def OnCloseFrame(self, event): lbessard@491: self.Closing = True lbessard@271: if not self.ModeSolo and getattr(self, "_onclose", None) != None: lbessard@271: self._onclose() lbessard@271: event.Skip() lbessard@271: lbessard@271: def OnChar(self, event): lbessard@271: if event.ControlDown() and event.GetKeyCode() == 83 and getattr(self, "_onsave", None) != None: lbessard@271: self._onsave() greg@612: #event.Skip() lbessard@242: lbessard@205: def OnQuitMenu(self, event): lbessard@205: self.Close() lbessard@205: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Load and Save Funtions lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@205: def OnNewProjectMenu(self, event): lbessard@205: if self.NodeList: lbessard@205: defaultpath = os.path.dirname(self.NodeList.GetRoot()) lbessard@205: else: lbessard@205: defaultpath = os.getcwd() laurent@580: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, wx.DD_NEW_DIR_BUTTON) lbessard@254: if dialog.ShowModal() == wx.ID_OK: lbessard@205: projectpath = dialog.GetPath() greg@206: if os.path.isdir(projectpath) and len(os.listdir(projectpath)) == 0: lbessard@258: manager = NodeManager() lbessard@205: nodelist = NodeList(manager) lbessard@205: result = nodelist.LoadProject(projectpath) lbessard@205: if not result: lbessard@205: self.Manager = manager lbessard@205: self.NodeList = nodelist lbessard@205: self.NodeList.SetCurrentSelected(0) lbessard@205: lbessard@205: self.RefreshNetworkNodes() lbessard@205: self.RefreshBufferState() lbessard@205: self.RefreshTitle() lbessard@205: self.RefreshProfileMenu() lbessard@205: self.RefreshMainMenu() lbessard@205: else: laurent@580: message = wx.MessageDialog(self, result, _("ERROR"), wx.OK|wx.ICON_ERROR) lbessard@205: message.ShowModal() lbessard@205: message.Destroy() laurent@608: lbessard@205: def OnOpenProjectMenu(self, event): lbessard@205: if self.NodeList: lbessard@205: defaultpath = os.path.dirname(self.NodeList.GetRoot()) lbessard@205: else: lbessard@205: defaultpath = os.getcwd() laurent@580: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, 0) lbessard@254: if dialog.ShowModal() == wx.ID_OK: lbessard@205: projectpath = dialog.GetPath() lbessard@205: if os.path.isdir(projectpath): lbessard@258: manager = NodeManager() lbessard@205: nodelist = NodeList(manager) lbessard@205: result = nodelist.LoadProject(projectpath) lbessard@205: if not result: lbessard@205: self.Manager = manager lbessard@205: self.NodeList = nodelist lbessard@205: self.NodeList.SetCurrentSelected(0) lbessard@205: lbessard@205: self.RefreshNetworkNodes() lbessard@205: self.RefreshBufferState() lbessard@205: self.RefreshTitle() lbessard@205: self.RefreshProfileMenu() lbessard@205: self.RefreshMainMenu() lbessard@205: else: laurent@580: message = wx.MessageDialog(self, result, _("Error"), wx.OK|wx.ICON_ERROR) lbessard@205: message.ShowModal() lbessard@205: message.Destroy() lbessard@205: dialog.Destroy() laurent@608: lbessard@205: def OnSaveProjectMenu(self, event): lbessard@271: if not self.ModeSolo and getattr(self, "_onsave", None) != None: lbessard@271: self._onsave() lbessard@271: else: lbessard@271: result = self.NodeList.SaveProject() lbessard@271: if result: laurent@580: message = wx.MessageDialog(self, result, _("Error"), wx.OK|wx.ICON_ERROR) lbessard@271: message.ShowModal() lbessard@271: message.Destroy() laurent@608: lbessard@242: def OnCloseProjectMenu(self, event): greg@238: if self.NodeList: lbessard@242: if self.NodeList.HasChanged(): laurent@580: dialog = wx.MessageDialog(self, _("There are changes, do you want to save?"), _("Close Project"), wx.YES_NO|wx.CANCEL|wx.ICON_QUESTION) lbessard@242: answer = dialog.ShowModal() greg@238: dialog.Destroy() lbessard@254: if answer == wx.ID_YES: lbessard@242: result = self.NodeList.SaveProject() lbessard@242: if result: laurent@580: message = wx.MessageDialog(self, result, _("Error"), wx.OK|wx.ICON_ERROR) lbessard@242: message.ShowModal() lbessard@242: message.Destroy() lbessard@254: elif answer == wx.ID_NO: lbessard@242: self.NodeList.ForceChanged(False) lbessard@242: if not self.NodeList.HasChanged(): lbessard@242: self.Manager = None lbessard@242: self.NodeList = None lbessard@242: self.RefreshNetworkNodes() lbessard@242: self.RefreshTitle() lbessard@242: self.RefreshMainMenu() laurent@608: laurent@608: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Refresh Functions lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@205: def RefreshTitle(self): lbessard@205: if self.NodeList != None: smarteh-dev@715: self.SetTitle(_("Networkedit - %s") % self.NodeList.GetNetworkName()) lbessard@205: else: laurent@580: self.SetTitle(_("Networkedit")) smarteh-dev@715: lbessard@205: def RefreshStatusBar(self): lbessard@491: selected = self.NetworkNodes.GetSelection() lbessard@491: if self.HelpBar and selected >= 0: lbessard@491: window = self.NetworkNodes.GetPage(selected) smarteh-dev@715: self.SetStatusBarText(window.GetSelection(), self.NodeList) smarteh-dev@715: lbessard@205: def RefreshMainMenu(self): lbessard@491: self.NetworkMenu.Enable(ID_NETWORKEDITNETWORKMENUBUILDMASTER, False) lbessard@491: if self.NodeList == None: lbessard@491: if self.ModeSolo: lbessard@491: self.MenuBar.EnableTop(1, False) lbessard@491: self.MenuBar.EnableTop(2, False) lbessard@491: self.MenuBar.EnableTop(3, False) lbessard@491: if self.FileMenu: lbessard@491: self.FileMenu.Enable(wx.ID_CLOSE, False) lbessard@491: self.FileMenu.Enable(wx.ID_SAVE, False) lbessard@491: else: lbessard@491: self.MenuBar.EnableTop(0, False) lbessard@491: self.MenuBar.EnableTop(1, False) lbessard@491: self.MenuBar.EnableTop(2, False) lbessard@491: else: lbessard@491: if self.ModeSolo: lbessard@491: self.MenuBar.EnableTop(1, True) lbessard@491: if self.FileMenu: lbessard@491: self.FileMenu.Enable(wx.ID_CLOSE, True) lbessard@491: self.FileMenu.Enable(wx.ID_SAVE, True) lbessard@491: if self.NetworkNodes.GetSelection() == 0: lbessard@491: self.MenuBar.EnableTop(2, True) lbessard@491: self.MenuBar.EnableTop(3, True) lbessard@491: else: lbessard@491: self.MenuBar.EnableTop(2, False) lbessard@491: self.MenuBar.EnableTop(3, False) lbessard@491: else: lbessard@491: self.MenuBar.EnableTop(0, True) lbessard@491: if self.NetworkNodes.GetSelection() == 0: lbessard@491: self.MenuBar.EnableTop(1, True) lbessard@491: self.MenuBar.EnableTop(2, True) lbessard@491: else: lbessard@491: self.MenuBar.EnableTop(1, False) lbessard@418: self.MenuBar.EnableTop(2, False) lbessard@205: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Buffer Functions lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@205: def RefreshBufferState(self): smarteh-dev@715: NetworkEditorTemplate.RefreshBufferState(self) lbessard@491: if self.NodeList is not None: lbessard@205: self.RefreshTitle() lbessard@205: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Help Method lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@205: def OnHelpDS301Menu(self, event): lbessard@205: find_index = False lbessard@205: selected = self.NetworkNodes.GetSelection() lbessard@205: if selected >= 0: lbessard@205: window = self.NetworkNodes.GetPage(selected) lbessard@205: result = window.GetSelection() lbessard@205: if result: lbessard@205: find_index = True lbessard@205: index, subIndex = result lbessard@205: result = OpenPDFDocIndex(index, ScriptDirectory) lbessard@512: if isinstance(result, (StringType, UnicodeType)): laurent@580: message = wx.MessageDialog(self, result, _("ERROR"), wx.OK|wx.ICON_ERROR) lbessard@205: message.ShowModal() lbessard@205: message.Destroy() lbessard@205: if not find_index: lbessard@205: result = OpenPDFDocIndex(None, ScriptDirectory) lbessard@512: if isinstance(result, (StringType, UnicodeType)): laurent@580: message = wx.MessageDialog(self, result, _("ERROR"), wx.OK|wx.ICON_ERROR) lbessard@205: message.ShowModal() lbessard@205: message.Destroy() lbessard@205: lbessard@205: def OnHelpCANFestivalMenu(self, event): lbessard@205: #self.OpenHtmlFrame("CAN Festival Reference", os.path.join(ScriptDirectory, "doc/canfestival.html"), wx.Size(1000, 600)) etisserant@386: if wx.Platform == '__WXMSW__': etisserant@386: readerpath = get_acroversion() etisserant@386: readerexepath = os.path.join(readerpath,"AcroRd32.exe") etisserant@386: if(os.path.isfile(readerexepath)): greg@408: os.spawnl(os.P_DETACH, readerexepath, "AcroRd32.exe", '"%s"'%os.path.join(ScriptDirectory, "doc","manual_en.pdf")) etisserant@386: else: etisserant@386: os.system("xpdf -remote CANFESTIVAL %s %d &"%(os.path.join(ScriptDirectory, "doc/manual_en.pdf"),16)) lbessard@205: lbessard@205: def OnAboutMenu(self, event): laurent@580: self.OpenHtmlFrame(_("About CAN Festival"), os.path.join(ScriptDirectory, "doc/about.html"), wx.Size(500, 450)) lbessard@205: lbessard@205: def OpenHtmlFrame(self, title, file, size): lbessard@205: if title not in self.HtmlFrameOpened: lbessard@205: self.HtmlFrameOpened.append(title) lbessard@205: window = HtmlFrame(self, self.HtmlFrameOpened) lbessard@205: window.SetTitle(title) lbessard@205: window.SetHtmlPage(file) lbessard@205: window.SetClientSize(size) lbessard@205: window.Show() lbessard@205: lbessard@205: #------------------------------------------------------------------------------- lbessard@205: # Exception Handler lbessard@205: #------------------------------------------------------------------------------- lbessard@205: lbessard@205: Max_Traceback_List_Size = 20 lbessard@205: lbessard@205: def Display_Exception_Dialog(e_type,e_value,e_tb): lbessard@205: trcbck_lst = [] lbessard@205: for i,line in enumerate(traceback.extract_tb(e_tb)): laurent@580: trcbck = " " + str(i+1) + _(". ") lbessard@205: if line[0].find(os.getcwd()) == -1: laurent@580: trcbck += _("file : ") + str(line[0]) + _(", ") lbessard@205: else: laurent@580: trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") laurent@580: trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) lbessard@205: trcbck_lst.append(trcbck) lbessard@205: lbessard@205: # Allow clicking.... lbessard@205: cap = wx.Window_GetCapture() lbessard@205: if cap: lbessard@205: cap.ReleaseMouse() lbessard@205: lbessard@205: dlg = wx.SingleChoiceDialog(None, laurent@580: _(""" lbessard@205: An error happens. lbessard@205: lbessard@205: Click on OK for saving an error report. lbessard@205: greg@614: Please be kind enough to send this file to: greg@614: edouard.tisserant@gmail.com lbessard@205: lbessard@205: lbessard@205: Error: laurent@580: """) + laurent@580: str(e_type) + _(" : ") + str(e_value), laurent@580: _("Error"), lbessard@205: trcbck_lst) lbessard@205: try: lbessard@205: res = (dlg.ShowModal() == wx.ID_OK) lbessard@205: finally: lbessard@205: dlg.Destroy() lbessard@205: lbessard@205: return res lbessard@205: lbessard@205: def Display_Error_Dialog(e_value): laurent@580: message = wx.MessageDialog(None, str(e_value), _("Error"), wx.OK|wx.ICON_ERROR) lbessard@205: message.ShowModal() lbessard@205: message.Destroy() lbessard@205: lbessard@205: def get_last_traceback(tb): lbessard@205: while tb.tb_next: lbessard@205: tb = tb.tb_next lbessard@205: return tb lbessard@205: lbessard@205: lbessard@205: def format_namespace(d, indent=' '): lbessard@205: return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) lbessard@205: lbessard@205: lbessard@205: ignored_exceptions = [] # a problem with a line in a module is only reported once per session lbessard@205: lbessard@254: def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): lbessard@205: lbessard@205: def handle_exception(e_type, e_value, e_traceback): lbessard@205: traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func lbessard@205: last_tb = get_last_traceback(e_traceback) lbessard@205: ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) lbessard@205: if str(e_value).startswith("!!!"): lbessard@205: Display_Error_Dialog(e_value) lbessard@205: elif ex not in ignored_exceptions: lbessard@205: ignored_exceptions.append(ex) lbessard@205: result = Display_Exception_Dialog(e_type,e_value,e_traceback) lbessard@205: if result: lbessard@205: info = { lbessard@205: 'app-title' : wx.GetApp().GetAppName(), # app_title lbessard@205: 'app-version' : app_version, lbessard@205: 'wx-version' : wx.VERSION_STRING, lbessard@205: 'wx-platform' : wx.Platform, lbessard@205: 'python-version' : platform.python_version(), #sys.version.split()[0], lbessard@205: 'platform' : platform.platform(), lbessard@205: 'e-type' : e_type, lbessard@205: 'e-value' : e_value, lbessard@205: 'date' : time.ctime(), lbessard@205: 'cwd' : os.getcwd(), lbessard@205: } lbessard@205: if e_traceback: lbessard@205: info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) lbessard@205: last_tb = get_last_traceback(e_traceback) lbessard@205: exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred lbessard@205: info['locals'] = format_namespace(exception_locals) lbessard@205: if 'self' in exception_locals: lbessard@205: info['self'] = format_namespace(exception_locals['self'].__dict__) lbessard@205: lbessard@205: output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w') lbessard@205: lst = info.keys() lbessard@205: lst.sort() lbessard@205: for a in lst: lbessard@205: output.write(a+":\n"+str(info[a])+"\n\n") lbessard@205: lbessard@205: #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) lbessard@205: sys.excepthook = handle_exception lbessard@205: lbessard@205: if __name__ == '__main__': lbessard@254: wx.InitAllImageHandlers() lbessard@205: lbessard@205: # Install a exception handle for bug reports lbessard@254: AddExceptHook(os.getcwd(),__version__) lbessard@205: lbessard@276: frame = networkedit(None, projectOpen=projectOpen) lbessard@205: lbessard@205: frame.Show() lbessard@205: app.MainLoop()