laurent@426: #!/usr/bin/env python laurent@426: # -*- coding: utf-8 -*- laurent@426: laurent@426: #This file is part of Beremiz, a Integrated Development Environment for laurent@426: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. laurent@426: # laurent@426: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@426: # laurent@426: #See COPYING file for copyrights details. laurent@426: # laurent@426: #This library is free software; you can redistribute it and/or laurent@426: #modify it under the terms of the GNU General Public laurent@426: #License as published by the Free Software Foundation; either laurent@426: #version 2.1 of the License, or (at your option) any later version. laurent@426: # laurent@426: #This library is distributed in the hope that it will be useful, laurent@426: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@426: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@426: #General Public License for more details. laurent@426: # laurent@426: #You should have received a copy of the GNU General Public laurent@426: #License along with this library; if not, write to the Free Software laurent@426: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@426: laurent@426: __version__ = "$Revision$" laurent@426: laurent@426: import os, sys, getopt, wx laurent@426: import tempfile laurent@426: import shutil laurent@426: import random laurent@426: laurent@426: CWD = os.path.split(os.path.realpath(__file__))[0] laurent@426: laurent@426: def Bpath(*args): laurent@426: return os.path.join(CWD,*args) laurent@426: laurent@426: if __name__ == '__main__': laurent@426: def usage(): laurent@426: print "\nUsage of Beremiz.py :" laurent@426: print "\n %s [Projectpath] [Buildpath]\n"%sys.argv[0] laurent@426: laurent@426: try: laurent@426: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) laurent@426: except getopt.GetoptError: laurent@426: # print help information and exit: laurent@426: usage() laurent@426: sys.exit(2) laurent@426: laurent@426: for o, a in opts: laurent@426: if o in ("-h", "--help"): laurent@426: usage() laurent@426: sys.exit() laurent@426: laurent@426: if len(args) > 2: laurent@426: usage() laurent@426: sys.exit() laurent@426: elif len(args) == 1: laurent@426: projectOpen = args[0] laurent@426: buildpath = None laurent@426: elif len(args) == 2: laurent@426: projectOpen = args[0] laurent@426: buildpath = args[1] laurent@426: else: laurent@426: projectOpen = None laurent@426: buildpath = None laurent@426: laurent@426: app = wx.PySimpleApp() laurent@426: app.SetAppName('beremiz') laurent@426: wx.InitAllImageHandlers() laurent@426: laurent@426: bmp = wx.Image(Bpath("images","splash.png")).ConvertToBitmap() laurent@426: splash=wx.SplashScreen(bmp,wx.SPLASH_CENTRE_ON_SCREEN, 1000, None) laurent@426: wx.Yield() laurent@426: laurent@426: # Import module for internationalization laurent@426: import gettext laurent@426: import __builtin__ laurent@426: laurent@426: # Get folder containing translation files laurent@426: localedir = os.path.join(CWD,"locale") laurent@426: # Get the default language laurent@426: langid = wx.LANGUAGE_DEFAULT laurent@426: # Define translation domain (name of translation files) laurent@426: domain = "Beremiz" laurent@426: laurent@426: # Define locale for wx laurent@426: loc = __builtin__.__dict__.get('loc', None) laurent@426: if loc is None: laurent@426: loc = wx.Locale(langid) laurent@426: __builtin__.__dict__['loc'] = loc laurent@426: # Define location for searching translation files laurent@426: loc.AddCatalogLookupPathPrefix(localedir) laurent@426: # Define locale domain laurent@426: loc.AddCatalog(domain) laurent@426: laurent@426: def unicode_translation(message): laurent@426: return wx.GetTranslation(message).encode("utf-8") laurent@426: laurent@426: if __name__ == '__main__': laurent@426: __builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation laurent@426: laurent@426: #Quick hack to be able to find Beremiz IEC tools. Should be config params. laurent@426: base_folder = os.path.split(sys.path[0])[0] laurent@426: sys.path.append(base_folder) laurent@426: sys.path.append(os.path.join(base_folder, "plcopeneditor")) laurent@426: sys.path.append(os.path.join(base_folder, "docutils")) laurent@426: laurent@426: import wx.lib.buttons, wx.lib.statbmp laurent@426: import TextCtrlAutoComplete, cPickle laurent@426: import types, time, re, platform, time, traceback, commands laurent@426: from plugger import PluginsRoot, MATIEC_ERROR_MODEL laurent@426: from wxPopen import ProcessLogger laurent@426: laurent@426: from docutils import * laurent@426: from PLCOpenEditor import IDEFrame, Viewer, AppendMenu, TITLE, TOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, TYPESTREE, INSTANCESTREE, LIBRARYTREE, SCALING laurent@426: from PLCControler import LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY laurent@426: laurent@426: SCROLLBAR_UNIT = 10 laurent@426: WINDOW_COLOUR = wx.Colour(240,240,240) laurent@426: TITLE_COLOUR = wx.Colour(200,200,220) laurent@426: CHANGED_TITLE_COLOUR = wx.Colour(220,200,220) laurent@426: CHANGED_WINDOW_COLOUR = wx.Colour(255,240,240) laurent@426: laurent@426: if wx.Platform == '__WXMSW__': laurent@426: faces = { 'times': 'Times New Roman', laurent@426: 'mono' : 'Courier New', laurent@426: 'helv' : 'Arial', laurent@426: 'other': 'Comic Sans MS', laurent@426: 'size' : 16, laurent@426: } laurent@426: else: laurent@426: faces = { 'times': 'Times', laurent@426: 'mono' : 'Courier', laurent@426: 'helv' : 'Helvetica', laurent@426: 'other': 'new century schoolbook', laurent@426: 'size' : 18, laurent@426: } laurent@426: laurent@426: # Some helpers to tweak GenBitmapTextButtons laurent@426: # TODO: declare customized classes instead. laurent@426: gen_mini_GetBackgroundBrush = lambda obj:lambda dc: wx.Brush(obj.GetParent().GetBackgroundColour(), wx.SOLID) laurent@426: gen_textbutton_GetLabelSize = lambda obj:lambda:(wx.lib.buttons.GenButton._GetLabelSize(obj)[:-1] + (False,)) laurent@426: laurent@426: def make_genbitmaptogglebutton_flat(button): laurent@426: button.GetBackgroundBrush = gen_mini_GetBackgroundBrush(button) laurent@426: button.labelDelta = 0 laurent@426: button.SetBezelWidth(0) laurent@426: button.SetUseFocusIndicator(False) laurent@426: laurent@426: # Patch wx.lib.imageutils so that gray is supported on alpha images laurent@426: import wx.lib.imageutils laurent@426: from wx.lib.imageutils import grayOut as old_grayOut laurent@426: def grayOut(anImage): laurent@426: if anImage.HasAlpha(): laurent@426: AlphaData = anImage.GetAlphaData() laurent@426: else : laurent@426: AlphaData = None laurent@426: laurent@426: old_grayOut(anImage) laurent@426: laurent@426: if AlphaData is not None: laurent@426: anImage.SetAlphaData(AlphaData) laurent@426: laurent@426: wx.lib.imageutils.grayOut = grayOut laurent@426: laurent@426: class GenBitmapTextButton(wx.lib.buttons.GenBitmapTextButton): laurent@426: def _GetLabelSize(self): laurent@426: """ used internally """ laurent@426: w, h = self.GetTextExtent(self.GetLabel()) laurent@426: if not self.bmpLabel: laurent@426: return w, h, False # if there isn't a bitmap use the size of the text laurent@426: laurent@426: w_bmp = self.bmpLabel.GetWidth()+2 laurent@426: h_bmp = self.bmpLabel.GetHeight()+2 laurent@426: height = h + h_bmp laurent@426: if w_bmp > w: laurent@426: width = w_bmp laurent@426: else: laurent@426: width = w laurent@426: return width, height, False laurent@426: laurent@426: def DrawLabel(self, dc, width, height, dw=0, dy=0): laurent@426: bmp = self.bmpLabel laurent@426: if bmp != None: # if the bitmap is used laurent@426: if self.bmpDisabled and not self.IsEnabled(): laurent@426: bmp = self.bmpDisabled laurent@426: if self.bmpFocus and self.hasFocus: laurent@426: bmp = self.bmpFocus laurent@426: if self.bmpSelected and not self.up: laurent@426: bmp = self.bmpSelected laurent@426: bw,bh = bmp.GetWidth(), bmp.GetHeight() laurent@426: if not self.up: laurent@426: dw = dy = self.labelDelta laurent@426: hasMask = bmp.GetMask() != None laurent@426: else: laurent@426: bw = bh = 0 # no bitmap -> size is zero laurent@426: laurent@426: dc.SetFont(self.GetFont()) laurent@426: if self.IsEnabled(): laurent@426: dc.SetTextForeground(self.GetForegroundColour()) laurent@426: else: laurent@426: dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT)) laurent@426: laurent@426: label = self.GetLabel() laurent@426: tw, th = dc.GetTextExtent(label) # size of text laurent@426: if not self.up: laurent@426: dw = dy = self.labelDelta laurent@426: laurent@426: pos_x = (width-bw)/2+dw # adjust for bitmap and text to centre laurent@426: pos_y = (height-bh-th)/2+dy laurent@426: if bmp !=None: laurent@426: dc.DrawBitmap(bmp, pos_x, pos_y, hasMask) # draw bitmap if available laurent@426: pos_x = (width-tw)/2+dw # adjust for bitmap and text to centre laurent@426: pos_y += bh + 2 laurent@426: laurent@426: dc.DrawText(label, pos_x, pos_y) # draw the text laurent@426: laurent@426: laurent@426: class GenStaticBitmap(wx.lib.statbmp.GenStaticBitmap): laurent@426: """ Customized GenStaticBitmap, fix transparency redraw bug on wx2.8/win32, laurent@426: and accept image name as __init__ parameter, fail silently if file do not exist""" laurent@426: def __init__(self, parent, ID, bitmapname, laurent@426: pos = wx.DefaultPosition, size = wx.DefaultSize, laurent@426: style = 0, laurent@426: name = "genstatbmp"): laurent@426: laurent@426: bitmappath = Bpath( "images", bitmapname) laurent@426: if os.path.isfile(bitmappath): laurent@426: bitmap = wx.Bitmap(bitmappath) laurent@426: else: laurent@426: bitmap = None laurent@426: wx.lib.statbmp.GenStaticBitmap.__init__(self, parent, ID, bitmap, laurent@426: pos, size, laurent@426: style, laurent@426: name) laurent@426: laurent@426: def OnPaint(self, event): laurent@426: dc = wx.PaintDC(self) laurent@426: colour = self.GetParent().GetBackgroundColour() laurent@426: dc.SetPen(wx.Pen(colour)) laurent@426: dc.SetBrush(wx.Brush(colour )) laurent@426: dc.DrawRectangle(0, 0, *dc.GetSizeTuple()) laurent@426: if self._bitmap: laurent@426: dc.DrawBitmap(self._bitmap, 0, 0, True) laurent@426: laurent@426: laurent@426: class LogPseudoFile: laurent@426: """ Base class for file like objects to facilitate StdOut for the Shell.""" laurent@426: def __init__(self, output): laurent@426: self.red_white = wx.TextAttr("RED", "WHITE") laurent@426: self.red_yellow = wx.TextAttr("RED", "YELLOW") laurent@426: self.black_white = wx.TextAttr("BLACK", "WHITE") laurent@426: self.default_style = None laurent@426: self.output = output laurent@426: laurent@426: def write(self, s, style = None): laurent@426: if style is None : style=self.black_white laurent@426: self.output.Freeze(); laurent@426: if self.default_style != style: laurent@426: self.output.SetDefaultStyle(style) laurent@426: self.default_style = style laurent@426: self.output.AppendText(s) laurent@426: self.output.ScrollLines(s.count('\n')+1) laurent@426: self.output.ShowPosition(self.output.GetLastPosition()) laurent@426: self.output.Thaw() laurent@426: laurent@426: def write_warning(self, s): laurent@426: self.write(s,self.red_white) laurent@426: laurent@426: def write_error(self, s): laurent@426: self.write(s,self.red_yellow) laurent@426: laurent@426: def flush(self): laurent@426: self.output.SetValue("") laurent@426: laurent@426: def isatty(self): laurent@426: return false laurent@426: laurent@426: [ID_BEREMIZ, ID_BEREMIZMAINSPLITTER, laurent@426: ID_BEREMIZPLCCONFIG, ID_BEREMIZLOGCONSOLE, laurent@426: ID_BEREMIZINSPECTOR] = [wx.NewId() for _init_ctrls in range(5)] laurent@426: laurent@426: [ID_BEREMIZRUNMENUBUILD, ID_BEREMIZRUNMENUSIMULATE, laurent@426: ID_BEREMIZRUNMENURUN, ID_BEREMIZRUNMENUSAVELOG, laurent@426: ] = [wx.NewId() for _init_coll_EditMenu_Items in range(4)] laurent@426: laurent@426: class Beremiz(IDEFrame): laurent@426: laurent@426: def _init_coll_FileMenu_Items(self, parent): laurent@426: AppendMenu(parent, help='', id=wx.ID_NEW, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'New\tCTRL+N')) laurent@426: AppendMenu(parent, help='', id=wx.ID_OPEN, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Open\tCTRL+O')) laurent@426: AppendMenu(parent, help='', id=wx.ID_SAVE, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Save\tCTRL+S')) greg@427: AppendMenu(parent, help='', id=wx.ID_SAVEAS, greg@427: kind=wx.ITEM_NORMAL, text=_(u'Save as\tCTRL+S')) laurent@426: AppendMenu(parent, help='', id=wx.ID_CLOSE, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Close Tab\tCTRL+W')) laurent@426: AppendMenu(parent, help='', id=wx.ID_CLOSE_ALL, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Close Project')) laurent@426: parent.AppendSeparator() laurent@426: AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Page Setup')) laurent@426: AppendMenu(parent, help='', id=wx.ID_PREVIEW, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Preview')) laurent@426: AppendMenu(parent, help='', id=wx.ID_PRINT, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Print')) laurent@426: parent.AppendSeparator() laurent@426: AppendMenu(parent, help='', id=wx.ID_PROPERTIES, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Properties')) laurent@426: parent.AppendSeparator() laurent@426: AppendMenu(parent, help='', id=wx.ID_EXIT, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Quit\tCTRL+Q')) laurent@426: laurent@426: self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) laurent@426: self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) laurent@426: self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) greg@427: self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS) laurent@426: self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) laurent@426: self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL) laurent@426: self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) laurent@426: self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) laurent@426: self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) laurent@426: self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) laurent@426: self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) laurent@426: laurent@426: def _init_coll_HelpMenu_Items(self, parent): laurent@426: parent.Append(help='', id=wx.ID_HELP, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'Beremiz\tF1')) laurent@426: parent.Append(help='', id=wx.ID_ABOUT, laurent@426: kind=wx.ITEM_NORMAL, text=_(u'About')) laurent@426: self.Bind(wx.EVT_MENU, self.OnBeremizMenu, id=wx.ID_HELP) laurent@426: self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) laurent@426: laurent@426: def _init_coll_PLCConfigMainSizer_Items(self, parent): laurent@426: parent.AddSizer(self.PLCParamsSizer, 0, border=10, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) laurent@426: parent.AddSizer(self.PluginTreeSizer, 0, border=10, flag=wx.BOTTOM|wx.LEFT|wx.RIGHT) laurent@426: laurent@426: def _init_coll_PLCConfigMainSizer_Growables(self, parent): laurent@426: parent.AddGrowableCol(0) laurent@426: parent.AddGrowableRow(1) laurent@426: laurent@426: def _init_coll_PluginTreeSizer_Growables(self, parent): laurent@426: parent.AddGrowableCol(0) laurent@426: parent.AddGrowableCol(1) laurent@426: laurent@426: def _init_beremiz_sizers(self): laurent@426: self.PLCConfigMainSizer = wx.FlexGridSizer(cols=1, hgap=2, rows=2, vgap=2) laurent@426: self.PLCParamsSizer = wx.BoxSizer(wx.VERTICAL) laurent@426: #self.PluginTreeSizer = wx.FlexGridSizer(cols=3, hgap=0, rows=0, vgap=2) laurent@426: self.PluginTreeSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=0, vgap=2) laurent@426: laurent@426: self._init_coll_PLCConfigMainSizer_Items(self.PLCConfigMainSizer) laurent@426: self._init_coll_PLCConfigMainSizer_Growables(self.PLCConfigMainSizer) laurent@426: self._init_coll_PluginTreeSizer_Growables(self.PluginTreeSizer) laurent@426: laurent@426: self.PLCConfig.SetSizer(self.PLCConfigMainSizer) laurent@426: laurent@426: def _init_ctrls(self, prnt): laurent@426: IDEFrame._init_ctrls(self, prnt) laurent@426: laurent@426: self.Bind(wx.EVT_MENU, self.OnOpenWidgetInspector, id=ID_BEREMIZINSPECTOR) laurent@426: accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL|wx.ACCEL_ALT, ord('I'), ID_BEREMIZINSPECTOR)]) laurent@426: self.SetAcceleratorTable(accel) laurent@426: laurent@426: self.PLCConfig = wx.ScrolledWindow(id=ID_BEREMIZPLCCONFIG, laurent@426: name='PLCConfig', parent=self.LeftNoteBook, pos=wx.Point(0, 0), laurent@426: size=wx.Size(-1, -1), style=wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER|wx.HSCROLL|wx.VSCROLL) laurent@426: self.PLCConfig.SetBackgroundColour(wx.WHITE) laurent@426: self.PLCConfig.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@426: self.PLCConfig.Bind(wx.EVT_SIZE, self.OnMoveWindow) laurent@426: self.PLCConfig.Bind(wx.EVT_MOUSEWHEEL, self.OnPLCConfigScroll) laurent@426: self.BottomNoteBook.InsertPage(0, self.PLCConfig, _("Topology"), True) laurent@426: laurent@426: self.LogConsole = wx.TextCtrl(id=ID_BEREMIZLOGCONSOLE, value='', laurent@426: name='LogConsole', parent=self.BottomNoteBook, pos=wx.Point(0, 0), laurent@426: size=wx.Size(0, 0), style=wx.TE_MULTILINE|wx.TE_RICH2) laurent@426: self.LogConsole.Bind(wx.EVT_LEFT_DCLICK, self.OnLogConsoleDClick) laurent@426: self.BottomNoteBook.AddPage(self.LogConsole, _("Log Console")) laurent@426: laurent@426: self._init_beremiz_sizers() laurent@426: laurent@426: def __init__(self, parent, projectOpen=None, buildpath=None, plugin_root=None, debug=True): laurent@426: IDEFrame.__init__(self, parent, debug) laurent@426: self.Config = wx.ConfigBase.Get() laurent@426: laurent@426: self.Log = LogPseudoFile(self.LogConsole) laurent@426: laurent@426: self.local_runtime = None laurent@426: self.runtime_port = None laurent@426: self.local_runtime_tmpdir = None laurent@426: laurent@426: self.DisableEvents = False laurent@426: # Variable allowing disabling of PLCConfig scroll when Popup shown laurent@426: self.ScrollingEnabled = True laurent@426: laurent@426: self.PluginInfos = {} laurent@426: laurent@426: if projectOpen is not None and os.path.isdir(projectOpen): laurent@426: self.PluginRoot = PluginsRoot(self, self.Log) laurent@426: self.Controler = self.PluginRoot laurent@426: result = self.PluginRoot.LoadProject(projectOpen, buildpath) laurent@426: if not result: laurent@426: self.DebugVariablePanel.SetDataProducer(self.PluginRoot) laurent@426: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@426: self.RefreshAll() laurent@426: else: laurent@426: self.ResetView() laurent@426: self.ShowErrorMessage(result) laurent@426: else: laurent@426: self.PluginRoot = plugin_root laurent@426: self.Controler = plugin_root laurent@426: if plugin_root is not None: laurent@426: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@426: self.RefreshAll() laurent@426: laurent@426: # Add beremiz's icon in top left corner of the frame laurent@426: self.SetIcon(wx.Icon(Bpath( "images", "brz.ico"), wx.BITMAP_TYPE_ICO)) laurent@426: laurent@426: self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) laurent@426: laurent@426: self._Refresh(TITLE, TOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU) laurent@426: laurent@426: def RefreshTitle(self): laurent@426: name = _("Beremiz") laurent@426: if self.PluginRoot is not None: laurent@426: projectname = self.PluginRoot.GetProjectName() laurent@426: if self.PluginRoot.PlugTestModified(): laurent@426: projectname = "~%s~" % projectname laurent@426: self.SetTitle("%s - %s" % (name, projectname)) laurent@426: else: laurent@426: self.SetTitle(name) laurent@426: laurent@426: def StartLocalRuntime(self, taskbaricon = True): laurent@426: if self.local_runtime is None or self.local_runtime.finished: laurent@426: # create temporary directory for runtime working directory laurent@426: self.local_runtime_tmpdir = tempfile.mkdtemp() laurent@426: # choose an arbitrary random port for runtime laurent@426: self.runtime_port = int(random.random() * 1000) + 61131 laurent@426: # launch local runtime laurent@426: self.local_runtime = ProcessLogger(self.Log, laurent@426: "\"%s\" \"%s\" -p %s -i localhost %s %s"%(sys.executable, laurent@426: Bpath("Beremiz_service.py"), laurent@426: self.runtime_port, laurent@426: {False : "-x 0", True :"-x 1"}[taskbaricon], laurent@426: self.local_runtime_tmpdir), laurent@426: no_gui=False) laurent@426: self.local_runtime.spin(timeout=500, keyword = "working", kill_it = False) laurent@426: return self.runtime_port laurent@426: laurent@426: def KillLocalRuntime(self): laurent@426: if self.local_runtime is not None: laurent@426: # shutdown local runtime laurent@426: self.local_runtime.kill(gently=False) laurent@426: # clear temp dir laurent@426: shutil.rmtree(self.local_runtime_tmpdir) laurent@426: laurent@426: def OnOpenWidgetInspector(self, evt): laurent@426: # Activate the widget inspection tool laurent@426: from wx.lib.inspection import InspectionTool laurent@426: if not InspectionTool().initialized: laurent@426: InspectionTool().Init() laurent@426: laurent@426: # Find a widget to be selected in the tree. Use either the laurent@426: # one under the cursor, if any, or this frame. laurent@426: wnd = wx.FindWindowAtPointer() laurent@426: if not wnd: laurent@426: wnd = self laurent@426: InspectionTool().Show(wnd, True) laurent@426: laurent@426: def OnLogConsoleDClick(self, event): laurent@426: wx.CallAfter(self.SearchLineForError) laurent@426: event.Skip() laurent@426: laurent@426: def SearchLineForError(self): laurent@426: if self.PluginRoot is not None: laurent@426: text = self.LogConsole.GetRange(0, self.LogConsole.GetInsertionPoint()) laurent@426: line = self.LogConsole.GetLineText(len(text.splitlines()) - 1) laurent@426: result = MATIEC_ERROR_MODEL.match(line) laurent@426: if result is not None: laurent@426: first_line, first_column, last_line, last_column, error = result.groups() laurent@426: infos = self.PluginRoot.ShowError(self.Log, laurent@426: (int(first_line), int(first_column)), laurent@426: (int(last_line), int(last_column))) laurent@426: laurent@426: def OnCloseFrame(self, event): laurent@426: if self.PluginRoot is not None: laurent@426: if self.PluginRoot.ProjectTestModified(): laurent@426: dialog = wx.MessageDialog(self, laurent@426: _("Save changes ?"), laurent@426: _("Close Application"), laurent@426: wx.YES_NO|wx.CANCEL|wx.ICON_QUESTION) laurent@426: answer = dialog.ShowModal() laurent@426: dialog.Destroy() laurent@426: if answer == wx.ID_YES: laurent@426: self.PluginRoot.SaveProject() laurent@426: event.Skip() laurent@426: elif answer == wx.ID_NO: laurent@426: event.Skip() laurent@426: return laurent@426: else: laurent@426: event.Veto() laurent@426: return laurent@426: laurent@426: self.KillLocalRuntime() laurent@426: laurent@426: event.Skip() laurent@426: laurent@426: def OnMoveWindow(self, event): laurent@426: self.GetBestSize() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: laurent@426: def EnableScrolling(self, enable): laurent@426: self.ScrollingEnabled = enable laurent@426: laurent@426: def OnPLCConfigScroll(self, event): laurent@426: if self.ScrollingEnabled: laurent@426: event.Skip() laurent@426: laurent@426: def OnPanelLeftDown(self, event): laurent@426: focused = self.FindFocus() laurent@426: if isinstance(focused, TextCtrlAutoComplete.TextCtrlAutoComplete): laurent@426: focused.DismissListBox() laurent@426: event.Skip() laurent@426: laurent@426: def RefreshFileMenu(self): laurent@426: if self.PluginRoot is not None: laurent@426: selected = self.TabsOpened.GetSelection() laurent@426: if selected >= 0: laurent@426: graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) laurent@426: else: laurent@426: graphic_viewer = False laurent@426: if self.TabsOpened.GetPageCount() > 0: laurent@426: self.FileMenu.Enable(wx.ID_CLOSE, True) laurent@426: if graphic_viewer: laurent@426: self.FileMenu.Enable(wx.ID_PREVIEW, True) laurent@426: self.FileMenu.Enable(wx.ID_PRINT, True) laurent@426: else: laurent@426: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@426: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@426: else: laurent@426: self.FileMenu.Enable(wx.ID_CLOSE, False) laurent@426: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@426: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@426: self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) laurent@426: self.FileMenu.Enable(wx.ID_SAVE, True) greg@427: self.FileMenu.Enable(wx.ID_SAVEAS, True) laurent@426: self.FileMenu.Enable(wx.ID_PROPERTIES, True) laurent@426: self.FileMenu.Enable(wx.ID_CLOSE_ALL, True) laurent@426: else: laurent@426: self.FileMenu.Enable(wx.ID_CLOSE, False) laurent@426: self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) laurent@426: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@426: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@426: self.FileMenu.Enable(wx.ID_SAVE, False) greg@427: self.FileMenu.Enable(wx.ID_SAVEAS, False) laurent@426: self.FileMenu.Enable(wx.ID_PROPERTIES, False) laurent@426: self.FileMenu.Enable(wx.ID_CLOSE_ALL, False) laurent@426: laurent@426: def RefreshScrollBars(self): laurent@426: xstart, ystart = self.PLCConfig.GetViewStart() laurent@426: window_size = self.PLCConfig.GetClientSize() laurent@426: sizer = self.PLCConfig.GetSizer() laurent@426: if sizer: laurent@426: maxx, maxy = sizer.GetMinSize() laurent@426: self.PLCConfig.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, laurent@426: maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, laurent@426: max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT)), laurent@426: max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT))) laurent@426: laurent@426: def RefreshPLCParams(self): laurent@426: self.Freeze() laurent@426: self.ClearSizer(self.PLCParamsSizer) laurent@426: laurent@426: if self.PluginRoot is not None: laurent@426: plcwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@426: if self.PluginRoot.PlugTestModified(): laurent@426: bkgdclr = CHANGED_TITLE_COLOUR laurent@426: else: laurent@426: bkgdclr = TITLE_COLOUR laurent@426: laurent@426: if self.PluginRoot not in self.PluginInfos: laurent@426: self.PluginInfos[self.PluginRoot] = {"right_visible" : False} laurent@426: laurent@426: plcwindow.SetBackgroundColour(TITLE_COLOUR) laurent@426: plcwindow.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@426: self.PLCParamsSizer.AddWindow(plcwindow, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: plcwindowsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: plcwindow.SetSizer(plcwindowsizer) laurent@426: laurent@426: st = wx.StaticText(plcwindow, -1) laurent@426: st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) laurent@426: st.SetLabel(self.PluginRoot.GetProjectName()) laurent@426: plcwindowsizer.AddWindow(st, 0, border=5, flag=wx.ALL|wx.ALIGN_CENTER) laurent@426: laurent@426: addbutton_id = wx.NewId() laurent@426: addbutton = wx.lib.buttons.GenBitmapButton(id=addbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Add.png')), laurent@426: name='AddPluginButton', parent=plcwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(16, 16), style=wx.NO_BORDER) laurent@426: addbutton.SetToolTipString(_("Add a sub plugin")) laurent@426: addbutton.Bind(wx.EVT_BUTTON, self.Gen_AddPluginMenu(self.PluginRoot), id=addbutton_id) laurent@426: plcwindowsizer.AddWindow(addbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@426: laurent@426: plcwindowmainsizer = wx.BoxSizer(wx.VERTICAL) laurent@426: plcwindowsizer.AddSizer(plcwindowmainsizer, 0, border=5, flag=wx.ALL) laurent@426: laurent@426: plcwindowbuttonsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: plcwindowmainsizer.AddSizer(plcwindowbuttonsizer, 0, border=0, flag=wx.ALIGN_CENTER) laurent@426: laurent@426: msizer = self.GenerateMethodButtonSizer(self.PluginRoot, plcwindow, not self.PluginInfos[self.PluginRoot]["right_visible"]) laurent@426: plcwindowbuttonsizer.AddSizer(msizer, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: paramswindow = wx.Panel(plcwindow, -1, size=wx.Size(-1, -1), style=wx.TAB_TRAVERSAL) laurent@426: paramswindow.SetBackgroundColour(TITLE_COLOUR) laurent@426: paramswindow.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@426: plcwindowbuttonsizer.AddWindow(paramswindow, 0, border=0, flag=0) laurent@426: laurent@426: psizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: paramswindow.SetSizer(psizer) laurent@426: laurent@426: plugin_infos = self.PluginRoot.GetParamsAttributes() laurent@426: self.RefreshSizerElement(paramswindow, psizer, self.PluginRoot, plugin_infos, None, False) laurent@426: laurent@426: if not self.PluginInfos[self.PluginRoot]["right_visible"]: laurent@426: paramswindow.Hide() laurent@426: laurent@426: minimizebutton_id = wx.NewId() laurent@426: minimizebutton = wx.lib.buttons.GenBitmapToggleButton(id=minimizebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Maximize.png')), laurent@426: name='MinimizeButton', parent=plcwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(24, 24), style=wx.NO_BORDER) laurent@426: make_genbitmaptogglebutton_flat(minimizebutton) laurent@426: minimizebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Minimize.png'))) laurent@426: minimizebutton.SetToggle(self.PluginInfos[self.PluginRoot]["right_visible"]) laurent@426: plcwindowbuttonsizer.AddWindow(minimizebutton, 0, border=5, flag=wx.ALL) laurent@426: laurent@426: def togglewindow(event): laurent@426: if minimizebutton.GetToggle(): laurent@426: paramswindow.Show() laurent@426: msizer.SetCols(1) laurent@426: else: laurent@426: paramswindow.Hide() laurent@426: msizer.SetCols(len(self.PluginRoot.PluginMethods)) laurent@426: self.PluginInfos[self.PluginRoot]["right_visible"] = minimizebutton.GetToggle() laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: minimizebutton.Bind(wx.EVT_BUTTON, togglewindow, id=minimizebutton_id) laurent@426: laurent@426: self.PluginInfos[self.PluginRoot]["main"] = plcwindow laurent@426: self.PluginInfos[self.PluginRoot]["params"] = paramswindow laurent@426: laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: self.Thaw() laurent@426: laurent@426: def GenerateMethodButtonSizer(self, plugin, parent, horizontal = True): laurent@426: normal_bt_font=wx.Font(faces["size"] / 3, wx.DEFAULT, wx.NORMAL, wx.NORMAL, faceName = faces["helv"]) laurent@426: mouseover_bt_font=wx.Font(faces["size"] / 3, wx.DEFAULT, wx.NORMAL, wx.NORMAL, underline=True, faceName = faces["helv"]) laurent@426: if horizontal: laurent@426: msizer = wx.FlexGridSizer(cols=len(plugin.PluginMethods)) laurent@426: else: laurent@426: msizer = wx.FlexGridSizer(cols=1) laurent@426: for plugin_method in plugin.PluginMethods: laurent@426: if "method" in plugin_method and plugin_method.get("shown",True): laurent@426: id = wx.NewId() laurent@426: label = plugin_method["name"] laurent@426: button = GenBitmapTextButton(id=id, parent=parent, laurent@426: bitmap=wx.Bitmap(Bpath( "%s.png"%plugin_method.get("bitmap", os.path.join("images", "Unknown")))), label=label, laurent@426: name=label, pos=wx.DefaultPosition, style=wx.NO_BORDER) laurent@426: button.SetFont(normal_bt_font) laurent@426: button.SetToolTipString(plugin_method["tooltip"]) laurent@426: button.Bind(wx.EVT_BUTTON, self.GetButtonCallBackFunction(plugin, plugin_method["method"]), id=id) laurent@426: # a fancy underline on mouseover laurent@426: def setFontStyle(b, s): laurent@426: def fn(event): laurent@426: b.SetFont(s) laurent@426: b.Refresh() laurent@426: event.Skip() laurent@426: return fn laurent@426: button.Bind(wx.EVT_ENTER_WINDOW, setFontStyle(button, mouseover_bt_font)) laurent@426: button.Bind(wx.EVT_LEAVE_WINDOW, setFontStyle(button, normal_bt_font)) laurent@426: #hack to force size to mini laurent@426: if not plugin_method.get("enabled",True): laurent@426: button.Disable() laurent@426: msizer.AddWindow(button, 0, border=0, flag=wx.ALIGN_CENTER) laurent@426: return msizer laurent@426: laurent@426: def RefreshPluginTree(self): laurent@426: self.Freeze() laurent@426: self.ClearSizer(self.PluginTreeSizer) laurent@426: if self.PluginRoot is not None: laurent@426: for child in self.PluginRoot.IECSortedChilds(): laurent@426: self.GenerateTreeBranch(child) laurent@426: if not self.PluginInfos[child]["expanded"]: laurent@426: self.CollapsePlugin(child) laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: self.Thaw() laurent@426: laurent@426: def SetPluginParamsAttribute(self, plugin, *args, **kwargs): laurent@426: res, StructChanged = plugin.SetParamsAttribute(*args, **kwargs) laurent@426: if StructChanged: laurent@426: wx.CallAfter(self.RefreshPluginTree) laurent@426: else: laurent@426: if plugin == self.PluginRoot: laurent@426: bkgdclr = CHANGED_TITLE_COLOUR laurent@426: items = ["main", "params"] laurent@426: else: laurent@426: bkgdclr = CHANGED_WINDOW_COLOUR laurent@426: items = ["left", "right", "params"] laurent@426: for i in items: laurent@426: self.PluginInfos[plugin][i].SetBackgroundColour(bkgdclr) laurent@426: self.PluginInfos[plugin][i].Refresh() laurent@426: return res laurent@426: laurent@426: def ExpandPlugin(self, plugin, force = False): laurent@426: for child in self.PluginInfos[plugin]["children"]: laurent@426: self.PluginInfos[child]["left"].Show() laurent@426: self.PluginInfos[child]["right"].Show() laurent@426: if force or not self.PluginInfos[child]["expanded"]: laurent@426: self.ExpandPlugin(child, force) laurent@426: if force: laurent@426: self.PluginInfos[child]["expanded"] = True laurent@426: locations_infos = self.PluginInfos[plugin].get("locations_infos", None) laurent@426: if locations_infos is not None: laurent@426: if force or locations_infos["root"]["expanded"]: laurent@426: self.ExpandLocation(locations_infos, "root", force) laurent@426: if force: laurent@426: locations_infos["root"]["expanded"] = True laurent@426: laurent@426: laurent@426: def CollapsePlugin(self, plugin, force = False): laurent@426: for child in self.PluginInfos[plugin]["children"]: laurent@426: self.PluginInfos[child]["left"].Hide() laurent@426: self.PluginInfos[child]["right"].Hide() laurent@426: self.CollapsePlugin(child, force) laurent@426: if force: laurent@426: self.PluginInfos[child]["expanded"] = False laurent@426: locations_infos = self.PluginInfos[plugin].get("locations_infos", None) laurent@426: if locations_infos is not None: laurent@426: self.CollapseLocation(locations_infos, "root", force) laurent@426: if force: laurent@426: locations_infos["root"]["expanded"] = False laurent@426: laurent@426: def ExpandLocation(self, locations_infos, group, force = False): laurent@426: for child in locations_infos[group]["children"]: laurent@426: locations_infos[child]["left"].Show() laurent@426: locations_infos[child]["right"].Show() laurent@426: if force or locations_infos[child]["expanded"]: laurent@426: self.ExpandLocation(locations_infos, child, force) laurent@426: if force: laurent@426: locations_infos[child]["expanded"] = True laurent@426: laurent@426: def CollapseLocation(self, locations_infos, group, force = False): laurent@426: for child in locations_infos[group]["children"]: laurent@426: locations_infos[child]["left"].Hide() laurent@426: locations_infos[child]["right"].Hide() laurent@426: self.CollapseLocation(locations_infos, child, force) laurent@426: if force: laurent@426: locations_infos[child]["expanded"] = False laurent@426: laurent@426: def GenerateTreeBranch(self, plugin): laurent@426: leftwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@426: if plugin.PlugTestModified(): laurent@426: bkgdclr=CHANGED_WINDOW_COLOUR laurent@426: else: laurent@426: bkgdclr=WINDOW_COLOUR laurent@426: laurent@426: leftwindow.SetBackgroundColour(bkgdclr) laurent@426: laurent@426: if not self.PluginInfos.has_key(plugin): laurent@426: self.PluginInfos[plugin] = {"expanded" : False, "right_visible" : False} laurent@426: laurent@426: self.PluginInfos[plugin]["children"] = plugin.IECSortedChilds() laurent@426: plugin_locations = [] laurent@426: if len(self.PluginInfos[plugin]["children"]) == 0: laurent@426: plugin_locations = plugin.GetVariableLocationTree()["children"] laurent@426: if not self.PluginInfos[plugin].has_key("locations_infos"): laurent@426: self.PluginInfos[plugin]["locations_infos"] = {"root": {"expanded" : False}} laurent@426: laurent@426: self.PluginInfos[plugin]["locations_infos"]["root"]["children"] = [] laurent@426: laurent@426: self.PluginTreeSizer.AddWindow(leftwindow, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: leftwindowsizer = wx.FlexGridSizer(cols=1, rows=2) laurent@426: leftwindowsizer.AddGrowableCol(0) laurent@426: leftwindow.SetSizer(leftwindowsizer) laurent@426: laurent@426: leftbuttonmainsizer = wx.FlexGridSizer(cols=3, rows=1) laurent@426: leftbuttonmainsizer.AddGrowableCol(0) laurent@426: leftwindowsizer.AddSizer(leftbuttonmainsizer, 0, border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT) #|wx.TOP laurent@426: laurent@426: leftbuttonsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: leftbuttonmainsizer.AddSizer(leftbuttonsizer, 0, border=5, flag=wx.GROW|wx.RIGHT) laurent@426: laurent@426: leftsizer = wx.BoxSizer(wx.VERTICAL) laurent@426: leftbuttonsizer.AddSizer(leftsizer, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: rolesizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: leftsizer.AddSizer(rolesizer, 0, border=0, flag=wx.GROW|wx.RIGHT) laurent@426: laurent@426: enablebutton_id = wx.NewId() laurent@426: enablebutton = wx.lib.buttons.GenBitmapToggleButton(id=enablebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Disabled.png')), laurent@426: name='EnableButton', parent=leftwindow, size=wx.Size(16, 16), pos=wx.Point(0, 0), style=0)#wx.NO_BORDER) laurent@426: enablebutton.SetToolTipString(_("Enable/Disable this plugin")) laurent@426: make_genbitmaptogglebutton_flat(enablebutton) laurent@426: enablebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Enabled.png'))) laurent@426: enablebutton.SetToggle(plugin.MandatoryParams[1].getEnabled()) laurent@426: def toggleenablebutton(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, "BaseParams.Enabled", enablebutton.GetToggle()) laurent@426: enablebutton.SetToggle(res) laurent@426: event.Skip() laurent@426: enablebutton.Bind(wx.EVT_BUTTON, toggleenablebutton, id=enablebutton_id) laurent@426: rolesizer.AddWindow(enablebutton, 0, border=0, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: roletext = wx.StaticText(leftwindow, -1) laurent@426: roletext.SetLabel(plugin.PlugHelp) laurent@426: rolesizer.AddWindow(roletext, 0, border=5, flag=wx.RIGHT|wx.ALIGN_LEFT) laurent@426: laurent@426: plugin_IECChannel = plugin.BaseParams.getIEC_Channel() laurent@426: laurent@426: iecsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: leftsizer.AddSizer(iecsizer, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: st = wx.StaticText(leftwindow, -1) laurent@426: st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) laurent@426: st.SetLabel(plugin.GetFullIEC_Channel()) laurent@426: iecsizer.AddWindow(st, 0, border=0, flag=0) laurent@426: laurent@426: updownsizer = wx.BoxSizer(wx.VERTICAL) laurent@426: iecsizer.AddSizer(updownsizer, 0, border=5, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: if plugin_IECChannel > 0: laurent@426: ieccdownbutton_id = wx.NewId() laurent@426: ieccdownbutton = wx.lib.buttons.GenBitmapButton(id=ieccdownbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'IECCDown.png')), laurent@426: name='IECCDownButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(16, 16), style=wx.NO_BORDER) laurent@426: ieccdownbutton.Bind(wx.EVT_BUTTON, self.GetItemChannelChangedFunction(plugin, plugin_IECChannel - 1), id=ieccdownbutton_id) laurent@426: updownsizer.AddWindow(ieccdownbutton, 0, border=0, flag=wx.ALIGN_LEFT) laurent@426: laurent@426: ieccupbutton_id = wx.NewId() laurent@426: ieccupbutton = wx.lib.buttons.GenBitmapTextButton(id=ieccupbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'IECCUp.png')), laurent@426: name='IECCUpButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(16, 16), style=wx.NO_BORDER) laurent@426: ieccupbutton.Bind(wx.EVT_BUTTON, self.GetItemChannelChangedFunction(plugin, plugin_IECChannel + 1), id=ieccupbutton_id) laurent@426: updownsizer.AddWindow(ieccupbutton, 0, border=0, flag=wx.ALIGN_LEFT) laurent@426: laurent@426: adddeletesizer = wx.BoxSizer(wx.VERTICAL) laurent@426: iecsizer.AddSizer(adddeletesizer, 0, border=5, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: deletebutton_id = wx.NewId() laurent@426: deletebutton = wx.lib.buttons.GenBitmapButton(id=deletebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Delete.png')), laurent@426: name='DeletePluginButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(16, 16), style=wx.NO_BORDER) laurent@426: deletebutton.SetToolTipString(_("Delete this plugin")) laurent@426: deletebutton.Bind(wx.EVT_BUTTON, self.GetDeleteButtonFunction(plugin), id=deletebutton_id) laurent@426: adddeletesizer.AddWindow(deletebutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@426: laurent@426: if len(plugin.PlugChildsTypes) > 0: laurent@426: addbutton_id = wx.NewId() laurent@426: addbutton = wx.lib.buttons.GenBitmapButton(id=addbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Add.png')), laurent@426: name='AddPluginButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(16, 16), style=wx.NO_BORDER) laurent@426: addbutton.SetToolTipString(_("Add a sub plugin")) laurent@426: addbutton.Bind(wx.EVT_BUTTON, self.Gen_AddPluginMenu(plugin), id=addbutton_id) laurent@426: adddeletesizer.AddWindow(addbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@426: laurent@426: expandbutton_id = wx.NewId() laurent@426: expandbutton = wx.lib.buttons.GenBitmapToggleButton(id=expandbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'plus.png')), laurent@426: name='ExpandButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(13, 13), style=wx.NO_BORDER) laurent@426: expandbutton.labelDelta = 0 laurent@426: expandbutton.SetBezelWidth(0) laurent@426: expandbutton.SetUseFocusIndicator(False) laurent@426: expandbutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'minus.png'))) laurent@426: laurent@426: if len(self.PluginInfos[plugin]["children"]) > 0: laurent@426: expandbutton.SetToggle(self.PluginInfos[plugin]["expanded"]) laurent@426: def togglebutton(event): laurent@426: if expandbutton.GetToggle(): laurent@426: self.ExpandPlugin(plugin) laurent@426: else: laurent@426: self.CollapsePlugin(plugin) laurent@426: self.PluginInfos[plugin]["expanded"] = expandbutton.GetToggle() laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) laurent@426: elif len(plugin_locations) > 0: laurent@426: locations_infos = self.PluginInfos[plugin]["locations_infos"] laurent@426: expandbutton.SetToggle(locations_infos["root"]["expanded"]) laurent@426: def togglebutton(event): laurent@426: if expandbutton.GetToggle(): laurent@426: self.ExpandLocation(locations_infos, "root") laurent@426: else: laurent@426: self.CollapseLocation(locations_infos, "root") laurent@426: self.PluginInfos[plugin]["expanded"] = expandbutton.GetToggle() laurent@426: locations_infos["root"]["expanded"] = expandbutton.GetToggle() laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) laurent@426: else: laurent@426: expandbutton.Enable(False) laurent@426: iecsizer.AddWindow(expandbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: tc_id = wx.NewId() laurent@426: tc = wx.TextCtrl(leftwindow, tc_id, size=wx.Size(150, 25), style=wx.NO_BORDER) laurent@426: tc.SetFont(wx.Font(faces["size"] * 0.75, wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) laurent@426: tc.ChangeValue(plugin.MandatoryParams[1].getName()) laurent@426: tc.Bind(wx.EVT_TEXT, self.GetTextCtrlCallBackFunction(tc, plugin, "BaseParams.Name"), id=tc_id) laurent@426: iecsizer.AddWindow(tc, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: rightwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@426: rightwindow.SetBackgroundColour(bkgdclr) laurent@426: laurent@426: self.PluginTreeSizer.AddWindow(rightwindow, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: rightwindowmainsizer = wx.BoxSizer(wx.VERTICAL) laurent@426: rightwindow.SetSizer(rightwindowmainsizer) laurent@426: laurent@426: rightwindowsizer = wx.FlexGridSizer(cols=2, rows=1) laurent@426: rightwindowsizer.AddGrowableCol(1) laurent@426: rightwindowsizer.AddGrowableRow(0) laurent@426: rightwindowmainsizer.AddSizer(rightwindowsizer, 0, border=8, flag=wx.TOP|wx.GROW) laurent@426: laurent@426: msizer = self.GenerateMethodButtonSizer(plugin, rightwindow, not self.PluginInfos[plugin]["right_visible"]) laurent@426: rightwindowsizer.AddSizer(msizer, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: rightparamssizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: rightwindowsizer.AddSizer(rightparamssizer, 0, border=0, flag=wx.ALIGN_RIGHT) laurent@426: laurent@426: paramswindow = wx.Panel(rightwindow, -1, size=wx.Size(-1, -1)) laurent@426: paramswindow.SetBackgroundColour(bkgdclr) laurent@426: laurent@426: psizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: paramswindow.SetSizer(psizer) laurent@426: self.PluginInfos[plugin]["params"] = paramswindow laurent@426: laurent@426: rightparamssizer.AddWindow(paramswindow, 0, border=5, flag=wx.ALL) laurent@426: laurent@426: plugin_infos = plugin.GetParamsAttributes() laurent@426: self.RefreshSizerElement(paramswindow, psizer, plugin, plugin_infos, None, False) laurent@426: laurent@426: if not self.PluginInfos[plugin]["right_visible"]: laurent@426: paramswindow.Hide() laurent@426: laurent@426: rightminimizebutton_id = wx.NewId() laurent@426: rightminimizebutton = wx.lib.buttons.GenBitmapToggleButton(id=rightminimizebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Maximize.png')), laurent@426: name='MinimizeButton', parent=rightwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(24, 24), style=wx.NO_BORDER) laurent@426: make_genbitmaptogglebutton_flat(rightminimizebutton) laurent@426: rightminimizebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Minimize.png'))) laurent@426: rightminimizebutton.SetToggle(self.PluginInfos[plugin]["right_visible"]) laurent@426: rightparamssizer.AddWindow(rightminimizebutton, 0, border=5, flag=wx.ALL) laurent@426: laurent@426: def togglerightwindow(event): laurent@426: if rightminimizebutton.GetToggle(): laurent@426: rightparamssizer.Show(0) laurent@426: msizer.SetCols(1) laurent@426: else: laurent@426: rightparamssizer.Hide(0) laurent@426: msizer.SetCols(len(plugin.PluginMethods)) laurent@426: self.PluginInfos[plugin]["right_visible"] = rightminimizebutton.GetToggle() laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: rightminimizebutton.Bind(wx.EVT_BUTTON, togglerightwindow, id=rightminimizebutton_id) laurent@426: laurent@426: self.PluginInfos[plugin]["left"] = leftwindow laurent@426: self.PluginInfos[plugin]["right"] = rightwindow laurent@426: for child in self.PluginInfos[plugin]["children"]: laurent@426: self.GenerateTreeBranch(child) laurent@426: if not self.PluginInfos[child]["expanded"]: laurent@426: self.CollapsePlugin(child) laurent@426: if len(plugin_locations) > 0: laurent@426: locations_infos = self.PluginInfos[plugin]["locations_infos"] laurent@426: for location in plugin_locations: laurent@426: locations_infos["root"]["children"].append("root.%s" % location["name"]) laurent@426: self.GenerateLocationTreeBranch(locations_infos, "root", location) laurent@426: if not locations_infos["root"]["expanded"]: laurent@426: self.CollapseLocation(locations_infos, "root") laurent@426: laurent@426: LOCATION_BITMAP = {LOCATION_PLUGIN: "CONFIGURATION", laurent@426: LOCATION_MODULE: "RESOURCE", laurent@426: LOCATION_GROUP: "PROGRAM", laurent@426: LOCATION_VAR_INPUT: "VAR_INPUT", laurent@426: LOCATION_VAR_OUTPUT: "VAR_OUTPUT", laurent@426: LOCATION_VAR_MEMORY: "VAR_LOCAL"} laurent@426: laurent@426: def GenerateLocationTreeBranch(self, locations_infos, parent, location): laurent@426: leftwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@426: self.PluginTreeSizer.AddWindow(leftwindow, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: leftwindowsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@426: leftwindow.SetSizer(leftwindowsizer) laurent@426: laurent@426: rightwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@426: self.PluginTreeSizer.AddWindow(rightwindow, 0, border=0, flag=wx.GROW) laurent@426: laurent@426: location_name = "%s.%s" % (parent, location["name"]) laurent@426: if not locations_infos.has_key(location_name): laurent@426: locations_infos[location_name] = {"expanded" : False} laurent@426: laurent@426: if location["type"] in [LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP]: laurent@426: leftwindow.SetBackgroundColour(WINDOW_COLOUR) laurent@426: rightwindow.SetBackgroundColour(WINDOW_COLOUR) laurent@426: laurent@426: st = wx.StaticText(leftwindow, -1) laurent@426: st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) laurent@426: st.SetLabel(location["location"]) laurent@426: leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.RIGHT) laurent@426: laurent@426: expandbutton_id = wx.NewId() laurent@426: expandbutton = wx.lib.buttons.GenBitmapToggleButton(id=expandbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'plus.png')), laurent@426: name='ExpandButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@426: size=wx.Size(13, 13), style=wx.NO_BORDER) laurent@426: expandbutton.labelDelta = 0 laurent@426: expandbutton.SetBezelWidth(0) laurent@426: expandbutton.SetUseFocusIndicator(False) laurent@426: expandbutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'minus.png'))) laurent@426: expandbutton.SetToggle(locations_infos[location_name]["expanded"]) laurent@426: laurent@426: if len(location["children"]) > 0: laurent@426: def togglebutton(event): laurent@426: if expandbutton.GetToggle(): laurent@426: self.ExpandLocation(locations_infos, location_name) laurent@426: else: laurent@426: self.CollapseLocation(locations_infos, location_name) laurent@426: locations_infos[location_name]["expanded"] = expandbutton.GetToggle() laurent@426: self.PLCConfigMainSizer.Layout() laurent@426: self.RefreshScrollBars() laurent@426: event.Skip() laurent@426: expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) laurent@426: else: laurent@426: expandbutton.Enable(False) laurent@426: leftwindowsizer.AddWindow(expandbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: else: laurent@426: leftwindow.SetBackgroundColour(wx.WHITE) laurent@426: rightwindow.SetBackgroundColour(wx.WHITE) laurent@426: laurent@426: leftwindowsizer.Add(wx.Size(20, 16), 0) laurent@426: laurent@426: sb = wx.StaticBitmap(leftwindow, -1) laurent@426: sb.SetBitmap(wx.Bitmap(os.path.join(base_folder, "plcopeneditor", 'Images', '%s.png' % self.LOCATION_BITMAP[location["type"]]))) laurent@426: leftwindowsizer.AddWindow(sb, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: st_id = wx.NewId() laurent@426: st = wx.StaticText(leftwindow, st_id, size=wx.DefaultSize, style=wx.NO_BORDER) laurent@426: label = location["name"] laurent@426: if location["type"] in [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]: laurent@426: label += " (%s)" % location["location"] laurent@426: infos = location.copy() laurent@426: infos.pop("children") laurent@426: st.SetFont(wx.Font(faces["size"] * 0.5, wx.DEFAULT, wx.NORMAL, wx.NORMAL, faceName = faces["helv"])) laurent@426: st.Bind(wx.EVT_LEFT_DOWN, self.GenerateLocationLeftDownFunction(infos)) laurent@426: else: laurent@426: st.SetFont(wx.Font(faces["size"] * 0.75, wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) laurent@426: st.SetLabel(label) laurent@426: leftwindowsizer.AddWindow(st, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@426: laurent@426: locations_infos[location_name]["left"] = leftwindow laurent@426: locations_infos[location_name]["right"] = rightwindow laurent@426: locations_infos[location_name]["children"] = [] laurent@426: for child in location["children"]: laurent@426: child_name = "%s.%s" % (location_name, child["name"]) laurent@426: locations_infos[location_name]["children"].append(child_name) laurent@426: self.GenerateLocationTreeBranch(locations_infos, location_name, child) laurent@426: if not locations_infos[location_name]["expanded"]: laurent@426: self.CollapseLocation(locations_infos, location_name) laurent@426: laurent@426: def GenerateLocationLeftDownFunction(self, infos): laurent@426: def OnLocationLeftDownFunction(event): laurent@426: data = wx.TextDataObject(str((infos["location"], "location", infos["IEC_type"], infos["name"], infos["description"]))) laurent@426: dragSource = wx.DropSource(self) laurent@426: dragSource.SetData(data) laurent@426: dragSource.DoDragDrop() laurent@426: event.Skip() laurent@426: return OnLocationLeftDownFunction laurent@426: laurent@426: def RefreshAll(self): laurent@426: self.RefreshPLCParams() laurent@426: self.RefreshPluginTree() laurent@426: laurent@426: def GetItemChannelChangedFunction(self, plugin, value): laurent@426: def OnPluginTreeItemChannelChanged(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, "BaseParams.IEC_Channel", value) laurent@426: event.Skip() laurent@426: return OnPluginTreeItemChannelChanged laurent@426: laurent@426: def _GetAddPluginFunction(self, name, plugin): laurent@426: def OnPluginMenu(event): laurent@426: wx.CallAfter(self.AddPlugin, name, plugin) laurent@426: return OnPluginMenu laurent@426: laurent@426: def Gen_AddPluginMenu(self, plugin): laurent@426: def AddPluginMenu(event): laurent@426: main_menu = wx.Menu(title='') laurent@426: if len(plugin.PlugChildsTypes) > 0: laurent@426: for name, XSDClass, help in plugin.PlugChildsTypes: laurent@426: new_id = wx.NewId() laurent@426: main_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=_("Append ")+help) laurent@426: self.Bind(wx.EVT_MENU, self._GetAddPluginFunction(name, plugin), id=new_id) laurent@426: self.PopupMenuXY(main_menu) laurent@426: return AddPluginMenu laurent@426: laurent@426: def GetButtonCallBackFunction(self, plugin, method): laurent@426: """ Generate the callbackfunc for a given plugin method""" laurent@426: def OnButtonClick(event): laurent@426: # Disable button to prevent re-entrant call laurent@426: event.GetEventObject().Disable() laurent@426: # Call laurent@426: getattr(plugin,method)() laurent@426: # Re-enable button laurent@426: event.GetEventObject().Enable() laurent@426: # Trigger refresh on Idle laurent@426: wx.CallAfter(self.RefreshAll) laurent@426: event.Skip() laurent@426: return OnButtonClick laurent@426: laurent@426: def GetChoiceCallBackFunction(self, choicectrl, plugin, path): laurent@426: def OnChoiceChanged(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, path, choicectrl.GetStringSelection()) laurent@426: choicectrl.SetStringSelection(res) laurent@426: event.Skip() laurent@426: return OnChoiceChanged laurent@426: laurent@426: def GetChoiceContentCallBackFunction(self, choicectrl, staticboxsizer, plugin, path): laurent@426: def OnChoiceContentChanged(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, path, choicectrl.GetStringSelection()) laurent@426: if wx.VERSION < (2, 8, 0): laurent@426: self.ParamsPanel.Freeze() laurent@426: choicectrl.SetStringSelection(res) laurent@426: infos = self.PluginRoot.GetParamsAttributes(path) laurent@426: staticbox = staticboxsizer.GetStaticBox() laurent@426: staticbox.SetLabel("%(name)s - %(value)s"%infos) laurent@426: self.RefreshSizerElement(self.ParamsPanel, staticboxsizer, infos["children"], "%s.%s"%(path, infos["name"]), selected=selected) laurent@426: self.ParamsPanelMainSizer.Layout() laurent@426: self.ParamsPanel.Thaw() laurent@426: self.ParamsPanel.Refresh() laurent@426: else: laurent@426: wx.CallAfter(self.RefreshAll) laurent@426: event.Skip() laurent@426: return OnChoiceContentChanged laurent@426: laurent@426: def GetTextCtrlCallBackFunction(self, textctrl, plugin, path): laurent@426: def OnTextCtrlChanged(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, path, textctrl.GetValue()) laurent@426: if res != textctrl.GetValue(): laurent@426: textctrl.ChangeValue(res) laurent@426: event.Skip() laurent@426: return OnTextCtrlChanged laurent@426: laurent@426: def GetCheckBoxCallBackFunction(self, chkbx, plugin, path): laurent@426: def OnCheckBoxChanged(event): laurent@426: res = self.SetPluginParamsAttribute(plugin, path, chkbx.IsChecked()) laurent@426: chkbx.SetValue(res) laurent@426: event.Skip() laurent@426: return OnCheckBoxChanged laurent@426: laurent@426: def ClearSizer(self, sizer): laurent@426: staticboxes = [] laurent@426: for item in sizer.GetChildren(): laurent@426: if item.IsSizer(): laurent@426: item_sizer = item.GetSizer() laurent@426: self.ClearSizer(item_sizer) laurent@426: if isinstance(item_sizer, wx.StaticBoxSizer): laurent@426: staticboxes.append(item_sizer.GetStaticBox()) laurent@426: sizer.Clear(True) laurent@426: for staticbox in staticboxes: laurent@426: staticbox.Destroy() laurent@426: laurent@426: def RefreshSizerElement(self, parent, sizer, plugin, elements, path, clean = True): laurent@426: if clean: laurent@426: if wx.VERSION < (2, 8, 0): laurent@426: self.ClearSizer(sizer) laurent@426: else: laurent@426: sizer.Clear(True) laurent@426: first = True laurent@426: for element_infos in elements: laurent@426: if path: laurent@426: element_path = "%s.%s"%(path, element_infos["name"]) laurent@426: else: laurent@426: element_path = element_infos["name"] laurent@426: if element_infos["type"] == "element": laurent@426: label = element_infos["name"] laurent@426: staticbox = wx.StaticBox(id=-1, label=_(label), laurent@426: name='%s_staticbox'%element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(10, 0), style=0) laurent@426: staticboxsizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL) laurent@426: if first: laurent@426: sizer.AddSizer(staticboxsizer, 0, border=0, flag=wx.GROW|wx.TOP) laurent@426: else: laurent@426: sizer.AddSizer(staticboxsizer, 0, border=0, flag=wx.GROW) laurent@426: self.RefreshSizerElement(parent, staticboxsizer, plugin, element_infos["children"], element_path) laurent@426: else: laurent@426: boxsizer = wx.FlexGridSizer(cols=3, rows=1) laurent@426: boxsizer.AddGrowableCol(1) laurent@426: if first: laurent@426: sizer.AddSizer(boxsizer, 0, border=5, flag=wx.GROW|wx.ALL) laurent@426: else: laurent@426: sizer.AddSizer(boxsizer, 0, border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) laurent@426: staticbitmap = GenStaticBitmap(ID=-1, bitmapname="%s.png"%element_infos["name"], laurent@426: name="%s_bitmap"%element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(24, 24), style=0) laurent@426: boxsizer.AddWindow(staticbitmap, 0, border=5, flag=wx.RIGHT) laurent@426: label = element_infos["name"] laurent@426: statictext = wx.StaticText(id=-1, label="%s:"%_(label), laurent@426: name="%s_label"%element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@426: boxsizer.AddWindow(statictext, 0, border=5, flag=wx.ALIGN_CENTER_VERTICAL|wx.RIGHT) laurent@426: id = wx.NewId() laurent@426: if isinstance(element_infos["type"], types.ListType): laurent@426: combobox = wx.ComboBox(id=id, name=element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(300, 28), style=wx.CB_READONLY) laurent@426: boxsizer.AddWindow(combobox, 0, border=0, flag=0) laurent@426: if element_infos["use"] == "optional": laurent@426: combobox.Append("") laurent@426: if len(element_infos["type"]) > 0 and isinstance(element_infos["type"][0], types.TupleType): laurent@426: for choice, xsdclass in element_infos["type"]: laurent@426: combobox.Append(choice) laurent@426: name = element_infos["name"] laurent@426: value = element_infos["value"] laurent@426: staticbox = wx.StaticBox(id=-1, label="%s - %s"%(_(name), _(value)), laurent@426: name='%s_staticbox'%element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(10, 0), style=0) laurent@426: staticboxsizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL) laurent@426: sizer.AddSizer(staticboxsizer, 0, border=5, flag=wx.GROW|wx.BOTTOM) laurent@426: self.RefreshSizerElement(parent, staticboxsizer, plugin, element_infos["children"], element_path) laurent@426: callback = self.GetChoiceContentCallBackFunction(combobox, staticboxsizer, plugin, element_path) laurent@426: else: laurent@426: for choice in element_infos["type"]: laurent@426: combobox.Append(choice) laurent@426: callback = self.GetChoiceCallBackFunction(combobox, plugin, element_path) laurent@426: if element_infos["value"] is None: laurent@426: combobox.SetStringSelection("") laurent@426: else: laurent@426: combobox.SetStringSelection(element_infos["value"]) laurent@426: combobox.Bind(wx.EVT_COMBOBOX, callback, id=id) laurent@426: elif isinstance(element_infos["type"], types.DictType): laurent@426: scmin = -(2**31) laurent@426: scmax = 2**31-1 laurent@426: if "min" in element_infos["type"]: laurent@426: scmin = element_infos["type"]["min"] laurent@426: if "max" in element_infos["type"]: laurent@426: scmax = element_infos["type"]["max"] laurent@426: spinctrl = wx.SpinCtrl(id=id, name=element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(300, 25), style=wx.SP_ARROW_KEYS|wx.ALIGN_RIGHT) laurent@426: spinctrl.SetRange(scmin,scmax) laurent@426: boxsizer.AddWindow(spinctrl, 0, border=0, flag=0) laurent@426: spinctrl.SetValue(element_infos["value"]) laurent@426: spinctrl.Bind(wx.EVT_SPINCTRL, self.GetTextCtrlCallBackFunction(spinctrl, plugin, element_path), id=id) laurent@426: else: laurent@426: if element_infos["type"] == "boolean": laurent@426: checkbox = wx.CheckBox(id=id, name=element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(17, 25), style=0) laurent@426: boxsizer.AddWindow(checkbox, 0, border=0, flag=0) laurent@426: checkbox.SetValue(element_infos["value"]) laurent@426: checkbox.Bind(wx.EVT_CHECKBOX, self.GetCheckBoxCallBackFunction(checkbox, plugin, element_path), id=id) laurent@426: elif element_infos["type"] in ["unsignedLong", "long","integer"]: laurent@426: if element_infos["type"].startswith("unsigned"): laurent@426: scmin = 0 laurent@426: else: laurent@426: scmin = -(2**31) laurent@426: scmax = 2**31-1 laurent@426: spinctrl = wx.SpinCtrl(id=id, name=element_infos["name"], parent=parent, laurent@426: pos=wx.Point(0, 0), size=wx.Size(300, 25), style=wx.SP_ARROW_KEYS|wx.ALIGN_RIGHT) laurent@426: spinctrl.SetRange(scmin, scmax) laurent@426: boxsizer.AddWindow(spinctrl, 0, border=0, flag=0) laurent@426: spinctrl.SetValue(element_infos["value"]) laurent@426: spinctrl.Bind(wx.EVT_SPINCTRL, self.GetTextCtrlCallBackFunction(spinctrl, plugin, element_path), id=id) laurent@426: else: laurent@426: choices = cPickle.loads(str(self.Config.Read(element_path, cPickle.dumps([""])))) laurent@426: textctrl = TextCtrlAutoComplete.TextCtrlAutoComplete(id=id, laurent@426: name=element_infos["name"], laurent@426: parent=parent, laurent@426: appframe=self, laurent@426: choices=choices, laurent@426: element_path=element_path, laurent@426: pos=wx.Point(0, 0), laurent@426: size=wx.Size(300, 25), laurent@426: style=0) laurent@426: laurent@426: boxsizer.AddWindow(textctrl, 0, border=0, flag=0) laurent@426: textctrl.ChangeValue(str(element_infos["value"])) laurent@426: textctrl.Bind(wx.EVT_TEXT, self.GetTextCtrlCallBackFunction(textctrl, plugin, element_path)) laurent@426: first = False laurent@426: laurent@426: def ResetView(self): laurent@426: IDEFrame.ResetView(self) laurent@426: self.PluginInfos = {} laurent@426: if self.PluginRoot is not None: laurent@426: self.PluginRoot.CloseProject() laurent@426: self.PluginRoot = None laurent@426: self.Log.flush() laurent@426: self.DebugVariablePanel.SetDataProducer(None) laurent@426: laurent@426: def OnNewProjectMenu(self, event): laurent@426: if not self.Config.HasEntry("lastopenedfolder"): laurent@426: defaultpath = os.path.expanduser("~") laurent@426: else: laurent@426: defaultpath = self.Config.Read("lastopenedfolder") laurent@426: laurent@426: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, wx.DD_NEW_DIR_BUTTON) laurent@426: if dialog.ShowModal() == wx.ID_OK: laurent@426: projectpath = dialog.GetPath() laurent@426: dialog.Destroy() laurent@426: self.Config.Write("lastopenedfolder", os.path.dirname(projectpath)) laurent@426: self.Config.Flush() laurent@426: self.ResetView() laurent@426: self.PluginRoot = PluginsRoot(self, self.Log) laurent@426: self.Controler = self.PluginRoot laurent@426: result = self.PluginRoot.NewProject(projectpath) laurent@426: if not result: laurent@426: self.DebugVariablePanel.SetDataProducer(self.PluginRoot) laurent@426: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@426: self.RefreshAll() laurent@426: else: laurent@426: self.ResetView() laurent@426: self.ShowErrorMessage(result) laurent@426: self._Refresh(TITLE, TOOLBAR, FILEMENU, EDITMENU) laurent@426: laurent@426: def OnOpenProjectMenu(self, event): laurent@426: if not self.Config.HasEntry("lastopenedfolder"): laurent@426: defaultpath = os.path.expanduser("~") laurent@426: else: laurent@426: defaultpath = self.Config.Read("lastopenedfolder") laurent@426: laurent@426: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, wx.DD_NEW_DIR_BUTTON) laurent@426: if dialog.ShowModal() == wx.ID_OK: laurent@426: projectpath = dialog.GetPath() laurent@426: if os.path.isdir(projectpath): laurent@426: self.Config.Write("lastopenedfolder", os.path.dirname(projectpath)) laurent@426: self.Config.Flush() laurent@426: self.ResetView() laurent@426: self.PluginRoot = PluginsRoot(self, self.Log) laurent@426: self.Controler = self.PluginRoot laurent@426: result = self.PluginRoot.LoadProject(projectpath) laurent@426: if not result: laurent@426: self.DebugVariablePanel.SetDataProducer(self.PluginRoot) laurent@426: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@426: self.RefreshAll() laurent@426: else: laurent@426: self.ResetView() laurent@426: self.ShowErrorMessage(result) laurent@426: else: laurent@426: self.ShowErrorMessage(_("\"%s\" folder is not a valid Beremiz project\n") % projectpath) laurent@426: self._Refresh(TITLE, TOOLBAR, FILEMENU, EDITMENU) laurent@426: dialog.Destroy() laurent@426: laurent@426: def OnCloseProjectMenu(self, event): laurent@426: if self.PluginRoot is not None: laurent@426: if self.PluginRoot.ProjectTestModified(): laurent@426: dialog = wx.MessageDialog(self, laurent@426: _("Save changes ?"), laurent@426: _("Close Application"), laurent@426: wx.YES_NO|wx.CANCEL|wx.ICON_QUESTION) laurent@426: answer = dialog.ShowModal() laurent@426: dialog.Destroy() laurent@426: if answer == wx.ID_YES: laurent@426: self.PluginRoot.SaveProject() laurent@426: elif answer == wx.ID_CANCEL: laurent@426: return laurent@426: self.ResetView() laurent@426: self._Refresh(TITLE, TOOLBAR, FILEMENU, EDITMENU) laurent@426: self.RefreshAll() laurent@426: laurent@426: def OnSaveProjectMenu(self, event): laurent@426: if self.PluginRoot is not None: laurent@426: self.PluginRoot.SaveProject() laurent@426: self.RefreshAll() laurent@426: self.RefreshTitle() laurent@426: greg@427: def OnSaveProjectAsMenu(self, event): greg@427: if self.PluginRoot is not None: greg@427: self.PluginRoot.SaveProjectAs() greg@427: self.RefreshAll() greg@427: self.RefreshTitle() greg@427: event.Skip() greg@427: laurent@426: def OnPropertiesMenu(self, event): laurent@426: self.ShowProperties() laurent@426: laurent@426: def OnQuitMenu(self, event): laurent@426: self.Close() laurent@426: laurent@426: def OnBeremizMenu(self, event): laurent@426: open_pdf(Bpath( "doc", "manual_beremiz.pdf")) laurent@426: laurent@426: def OnAboutMenu(self, event): laurent@426: OpenHtmlFrame(self,_("About Beremiz"), Bpath("doc","about.html"), wx.Size(550, 500)) laurent@426: laurent@426: def GetAddButtonFunction(self, plugin, window): laurent@426: def AddButtonFunction(event): laurent@426: if plugin and len(plugin.PlugChildsTypes) > 0: laurent@426: plugin_menu = wx.Menu(title='') laurent@426: for name, XSDClass, help in plugin.PlugChildsTypes: laurent@426: new_id = wx.NewId() laurent@426: plugin_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=name) laurent@426: self.Bind(wx.EVT_MENU, self._GetAddPluginFunction(name, plugin), id=new_id) laurent@426: window_pos = window.GetPosition() laurent@426: wx.CallAfter(self.PLCConfig.PopupMenu, plugin_menu) laurent@426: event.Skip() laurent@426: return AddButtonFunction laurent@426: laurent@426: def GetDeleteButtonFunction(self, plugin): laurent@426: def DeleteButtonFunction(event): laurent@426: wx.CallAfter(self.DeletePlugin, plugin) laurent@426: event.Skip() laurent@426: return DeleteButtonFunction laurent@426: laurent@426: def AddPlugin(self, PluginType, plugin): greg@427: if self.PluginRoot.CheckProjectPathPerm(): greg@427: dialog = wx.TextEntryDialog(self, _("Please enter a name for plugin:"), _("Add Plugin"), "", wx.OK|wx.CANCEL) greg@427: if dialog.ShowModal() == wx.ID_OK: greg@427: PluginName = dialog.GetValue() greg@427: plugin.PlugAddChild(PluginName, PluginType) greg@427: self.RefreshPluginTree() greg@427: self.PluginRoot.RefreshPluginsBlockLists() greg@427: dialog.Destroy() laurent@426: laurent@426: def DeletePlugin(self, plugin): greg@427: if self.PluginRoot.CheckProjectPathPerm(): greg@427: dialog = wx.MessageDialog(self, _("Really delete plugin ?"), _("Remove plugin"), wx.YES_NO|wx.NO_DEFAULT) greg@427: if dialog.ShowModal() == wx.ID_YES: greg@427: self.PluginInfos.pop(plugin) greg@427: plugin.PlugRemove() greg@427: del plugin greg@427: self.PluginRoot.RefreshPluginsBlockLists() greg@427: self.RefreshPluginTree() greg@427: dialog.Destroy() greg@427: laurent@426: #------------------------------------------------------------------------------- laurent@426: # Exception Handler laurent@426: #------------------------------------------------------------------------------- laurent@426: laurent@426: Max_Traceback_List_Size = 20 laurent@426: laurent@426: def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path): laurent@426: trcbck_lst = [] laurent@426: for i,line in enumerate(traceback.extract_tb(e_tb)): laurent@426: trcbck = " " + str(i+1) + _(". ") laurent@426: if line[0].find(os.getcwd()) == -1: laurent@426: trcbck += _("file : ") + str(line[0]) + _(", ") laurent@426: else: laurent@426: trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") laurent@426: trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) laurent@426: trcbck_lst.append(trcbck) laurent@426: laurent@426: # Allow clicking.... laurent@426: cap = wx.Window_GetCapture() laurent@426: if cap: laurent@426: cap.ReleaseMouse() laurent@426: laurent@426: dlg = wx.SingleChoiceDialog(None, laurent@426: _(""" laurent@426: An unhandled exception (bug) occured. Bug report saved at : laurent@426: (%s) laurent@426: laurent@426: Please contact LOLITech at: laurent@426: +33 (0)3 29 57 60 42 laurent@426: or please be kind enough to send this file to: laurent@426: bugs_beremiz@lolitech.fr laurent@426: laurent@426: You should now restart Beremiz. laurent@426: laurent@426: Traceback: laurent@426: """) % bug_report_path + laurent@426: str(e_type) + " : " + str(e_value), laurent@426: _("Error"), laurent@426: trcbck_lst) laurent@426: try: laurent@426: res = (dlg.ShowModal() == wx.ID_OK) laurent@426: finally: laurent@426: dlg.Destroy() laurent@426: laurent@426: return res laurent@426: laurent@426: def Display_Error_Dialog(e_value): laurent@426: message = wxMessageDialog(None, str(e_value), _("Error"), wxOK|wxICON_ERROR) laurent@426: message.ShowModal() laurent@426: message.Destroy() laurent@426: laurent@426: def get_last_traceback(tb): laurent@426: while tb.tb_next: laurent@426: tb = tb.tb_next laurent@426: return tb laurent@426: laurent@426: laurent@426: def format_namespace(d, indent=' '): laurent@426: return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) laurent@426: laurent@426: laurent@426: ignored_exceptions = [] # a problem with a line in a module is only reported once per session laurent@426: laurent@426: def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): laurent@426: laurent@426: def handle_exception(e_type, e_value, e_traceback): laurent@426: traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func laurent@426: last_tb = get_last_traceback(e_traceback) laurent@426: ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) laurent@426: if str(e_value).startswith("!!!"): laurent@426: Display_Error_Dialog(e_value) laurent@426: elif ex not in ignored_exceptions: laurent@426: date = time.ctime() laurent@426: bug_report_path = path+os.sep+"bug_report_"+date.replace(':','-').replace(' ','_')+".txt" laurent@426: result = Display_Exception_Dialog(e_type,e_value,e_traceback,bug_report_path) laurent@426: if result: laurent@426: ignored_exceptions.append(ex) laurent@426: info = { laurent@426: 'app-title' : wx.GetApp().GetAppName(), # app_title laurent@426: 'app-version' : app_version, laurent@426: 'wx-version' : wx.VERSION_STRING, laurent@426: 'wx-platform' : wx.Platform, laurent@426: 'python-version' : platform.python_version(), #sys.version.split()[0], laurent@426: 'platform' : platform.platform(), laurent@426: 'e-type' : e_type, laurent@426: 'e-value' : e_value, laurent@426: 'date' : date, laurent@426: 'cwd' : os.getcwd(), laurent@426: } laurent@426: if e_traceback: laurent@426: info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) laurent@426: last_tb = get_last_traceback(e_traceback) laurent@426: exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred laurent@426: info['locals'] = format_namespace(exception_locals) laurent@426: if 'self' in exception_locals: laurent@426: info['self'] = format_namespace(exception_locals['self'].__dict__) laurent@426: laurent@426: output = open(bug_report_path,'w') laurent@426: lst = info.keys() laurent@426: lst.sort() laurent@426: for a in lst: laurent@426: output.write(a+":\n"+str(info[a])+"\n\n") laurent@426: laurent@426: #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) laurent@426: sys.excepthook = handle_exception laurent@426: laurent@426: if __name__ == '__main__': laurent@426: # Install a exception handle for bug reports laurent@426: AddExceptHook(os.getcwd(),__version__) laurent@426: laurent@426: frame = Beremiz(None, projectOpen, buildpath) laurent@426: frame.Show() laurent@426: splash.Close() laurent@426: app.MainLoop()