laurent@428: #!/usr/bin/env python laurent@428: # -*- coding: utf-8 -*- laurent@428: laurent@428: #This file is part of Beremiz, a Integrated Development Environment for laurent@428: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. laurent@428: # laurent@428: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@428: # laurent@428: #See COPYING file for copyrights details. laurent@428: # laurent@428: #This library is free software; you can redistribute it and/or laurent@428: #modify it under the terms of the GNU General Public laurent@428: #License as published by the Free Software Foundation; either laurent@428: #version 2.1 of the License, or (at your option) any later version. laurent@428: # laurent@428: #This library is distributed in the hope that it will be useful, laurent@428: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@428: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@428: #General Public License for more details. laurent@428: # laurent@428: #You should have received a copy of the GNU General Public laurent@428: #License along with this library; if not, write to the Free Software laurent@428: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@428: Edouard@588: Edouard@588: updateinfo_url = None laurent@428: laurent@428: import os, sys, getopt, wx laurent@650: import __builtin__ Edouard@588: from wx.lib.agw.advancedsplash import AdvancedSplash laurent@428: import tempfile laurent@428: import shutil laurent@428: import random ed@446: import time laurent@675: from types import ListType laurent@428: laurent@428: CWD = os.path.split(os.path.realpath(__file__))[0] laurent@428: laurent@428: def Bpath(*args): laurent@428: return os.path.join(CWD,*args) laurent@428: laurent@428: if __name__ == '__main__': laurent@428: def usage(): laurent@428: print "\nUsage of Beremiz.py :" laurent@428: print "\n %s [Projectpath] [Buildpath]\n"%sys.argv[0] laurent@428: laurent@428: try: Edouard@588: opts, args = getopt.getopt(sys.argv[1:], "hu:", ["help", "updatecheck="]) laurent@428: except getopt.GetoptError: laurent@428: # print help information and exit: laurent@428: usage() laurent@428: sys.exit(2) laurent@428: laurent@428: for o, a in opts: laurent@428: if o in ("-h", "--help"): laurent@428: usage() laurent@428: sys.exit() Edouard@588: if o in ("-u", "--updatecheck"): Edouard@588: updateinfo_url = a laurent@428: laurent@428: if len(args) > 2: laurent@428: usage() laurent@428: sys.exit() laurent@428: elif len(args) == 1: laurent@428: projectOpen = args[0] laurent@428: buildpath = None laurent@428: elif len(args) == 2: laurent@428: projectOpen = args[0] laurent@428: buildpath = args[1] laurent@428: else: laurent@428: projectOpen = None laurent@428: buildpath = None laurent@428: edouard@571: if os.path.exists("BEREMIZ_DEBUG"): laurent@650: __builtin__.__dict__["BMZ_DBG"] = True edouard@571: else : laurent@650: __builtin__.__dict__["BMZ_DBG"] = False edouard@571: edouard@571: app = wx.PySimpleApp(redirect=BMZ_DBG) laurent@428: app.SetAppName('beremiz') laurent@428: wx.InitAllImageHandlers() laurent@428: Edouard@588: # popup splash laurent@428: bmp = wx.Image(Bpath("images","splash.png")).ConvertToBitmap() Edouard@588: #splash=AdvancedSplash(None, bitmap=bmp, style=wx.SPLASH_CENTRE_ON_SCREEN, timeout=4000) Edouard@588: splash=AdvancedSplash(None, bitmap=bmp) laurent@428: wx.Yield() laurent@428: Edouard@588: if updateinfo_url is not None: Edouard@588: updateinfo = "Fetching %s" % updateinfo_url Edouard@588: # warn for possible updates Edouard@588: def updateinfoproc(): Edouard@588: global updateinfo Edouard@588: try : Edouard@588: import urllib2 Edouard@588: updateinfo = urllib2.urlopen(updateinfo_url,None).read() Edouard@588: except : Edouard@588: updateinfo = "update info unavailable." Edouard@588: Edouard@588: from threading import Thread Edouard@588: splash.SetText(text=updateinfo) Edouard@588: wx.Yield() Edouard@588: updateinfoThread = Thread(target=updateinfoproc) Edouard@588: updateinfoThread.start() Edouard@588: updateinfoThread.join(2) Edouard@588: splash.SetText(text=updateinfo) Edouard@588: wx.Yield() Edouard@588: laurent@428: # Import module for internationalization laurent@428: import gettext laurent@428: laurent@428: # Get folder containing translation files laurent@428: localedir = os.path.join(CWD,"locale") laurent@428: # Get the default language laurent@428: langid = wx.LANGUAGE_DEFAULT laurent@428: # Define translation domain (name of translation files) laurent@428: domain = "Beremiz" laurent@428: laurent@428: # Define locale for wx laurent@428: loc = __builtin__.__dict__.get('loc', None) laurent@428: if loc is None: laurent@589: test_loc = wx.Locale(langid) laurent@589: test_loc.AddCatalogLookupPathPrefix(localedir) laurent@589: if test_loc.AddCatalog(domain): laurent@589: loc = wx.Locale(langid) laurent@589: else: laurent@589: loc = wx.Locale(wx.LANGUAGE_ENGLISH) laurent@428: __builtin__.__dict__['loc'] = loc laurent@428: # Define location for searching translation files laurent@428: loc.AddCatalogLookupPathPrefix(localedir) laurent@428: # Define locale domain laurent@428: loc.AddCatalog(domain) laurent@428: laurent@428: def unicode_translation(message): laurent@428: return wx.GetTranslation(message).encode("utf-8") laurent@428: laurent@428: if __name__ == '__main__': laurent@428: __builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation laurent@428: laurent@428: #Quick hack to be able to find Beremiz IEC tools. Should be config params. laurent@428: base_folder = os.path.split(sys.path[0])[0] laurent@428: sys.path.append(base_folder) laurent@428: sys.path.append(os.path.join(base_folder, "plcopeneditor")) laurent@428: laurent@428: import wx.lib.buttons, wx.lib.statbmp laurent@428: import TextCtrlAutoComplete, cPickle laurent@703: from BrowseValuesLibraryDialog import BrowseValuesLibraryDialog laurent@428: import types, time, re, platform, time, traceback, commands Edouard@717: from ConfigTree import ConfigTreeRoot, MiniTextControler, MATIEC_ERROR_MODEL laurent@428: from wxPopen import ProcessLogger laurent@428: laurent@428: from docutils import * laurent@715: from PLCOpenEditor import IDEFrame, AppendMenu, TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, TYPESTREE, INSTANCESTREE, LIBRARYTREE, SCALING, PAGETITLES, USE_AUI laurent@715: from PLCOpenEditor import EditorPanel, Viewer, TextViewer, GraphicViewer, ResourceEditor, ConfigurationEditor, DataTypeEditor Edouard@717: from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY laurent@428: laurent@428: SCROLLBAR_UNIT = 10 laurent@428: WINDOW_COLOUR = wx.Colour(240,240,240) laurent@428: TITLE_COLOUR = wx.Colour(200,200,220) laurent@428: CHANGED_TITLE_COLOUR = wx.Colour(220,200,220) laurent@428: CHANGED_WINDOW_COLOUR = wx.Colour(255,240,240) laurent@428: laurent@428: if wx.Platform == '__WXMSW__': laurent@428: faces = { 'times': 'Times New Roman', laurent@428: 'mono' : 'Courier New', laurent@428: 'helv' : 'Arial', laurent@428: 'other': 'Comic Sans MS', laurent@428: 'size' : 16, laurent@428: } laurent@428: else: laurent@428: faces = { 'times': 'Times', laurent@428: 'mono' : 'Courier', laurent@428: 'helv' : 'Helvetica', laurent@428: 'other': 'new century schoolbook', laurent@428: 'size' : 18, laurent@428: } laurent@428: laurent@675: MAX_RECENT_PROJECTS = 10 laurent@675: laurent@428: # Some helpers to tweak GenBitmapTextButtons laurent@428: # TODO: declare customized classes instead. laurent@428: gen_mini_GetBackgroundBrush = lambda obj:lambda dc: wx.Brush(obj.GetParent().GetBackgroundColour(), wx.SOLID) laurent@428: gen_textbutton_GetLabelSize = lambda obj:lambda:(wx.lib.buttons.GenButton._GetLabelSize(obj)[:-1] + (False,)) laurent@428: laurent@428: def make_genbitmaptogglebutton_flat(button): laurent@428: button.GetBackgroundBrush = gen_mini_GetBackgroundBrush(button) laurent@428: button.labelDelta = 0 laurent@428: button.SetBezelWidth(0) laurent@428: button.SetUseFocusIndicator(False) laurent@428: laurent@428: # Patch wx.lib.imageutils so that gray is supported on alpha images laurent@428: import wx.lib.imageutils laurent@428: from wx.lib.imageutils import grayOut as old_grayOut laurent@428: def grayOut(anImage): laurent@428: if anImage.HasAlpha(): laurent@428: AlphaData = anImage.GetAlphaData() laurent@428: else : laurent@428: AlphaData = None laurent@428: laurent@428: old_grayOut(anImage) laurent@428: laurent@428: if AlphaData is not None: laurent@428: anImage.SetAlphaData(AlphaData) laurent@428: laurent@428: wx.lib.imageutils.grayOut = grayOut laurent@428: laurent@428: class GenBitmapTextButton(wx.lib.buttons.GenBitmapTextButton): laurent@428: def _GetLabelSize(self): laurent@428: """ used internally """ laurent@428: w, h = self.GetTextExtent(self.GetLabel()) laurent@428: if not self.bmpLabel: laurent@428: return w, h, False # if there isn't a bitmap use the size of the text laurent@428: laurent@428: w_bmp = self.bmpLabel.GetWidth()+2 laurent@428: h_bmp = self.bmpLabel.GetHeight()+2 laurent@428: height = h + h_bmp laurent@428: if w_bmp > w: laurent@428: width = w_bmp laurent@428: else: laurent@428: width = w laurent@428: return width, height, False laurent@428: laurent@428: def DrawLabel(self, dc, width, height, dw=0, dy=0): laurent@428: bmp = self.bmpLabel laurent@428: if bmp != None: # if the bitmap is used laurent@428: if self.bmpDisabled and not self.IsEnabled(): laurent@428: bmp = self.bmpDisabled laurent@428: if self.bmpFocus and self.hasFocus: laurent@428: bmp = self.bmpFocus laurent@428: if self.bmpSelected and not self.up: laurent@428: bmp = self.bmpSelected laurent@428: bw,bh = bmp.GetWidth(), bmp.GetHeight() laurent@428: if not self.up: laurent@428: dw = dy = self.labelDelta laurent@428: hasMask = bmp.GetMask() != None laurent@428: else: laurent@428: bw = bh = 0 # no bitmap -> size is zero laurent@428: laurent@428: dc.SetFont(self.GetFont()) laurent@428: if self.IsEnabled(): laurent@428: dc.SetTextForeground(self.GetForegroundColour()) laurent@428: else: laurent@428: dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT)) laurent@428: laurent@428: label = self.GetLabel() laurent@428: tw, th = dc.GetTextExtent(label) # size of text laurent@428: if not self.up: laurent@428: dw = dy = self.labelDelta laurent@428: laurent@428: pos_x = (width-bw)/2+dw # adjust for bitmap and text to centre laurent@428: pos_y = (height-bh-th)/2+dy laurent@428: if bmp !=None: laurent@428: dc.DrawBitmap(bmp, pos_x, pos_y, hasMask) # draw bitmap if available laurent@428: pos_x = (width-tw)/2+dw # adjust for bitmap and text to centre laurent@428: pos_y += bh + 2 laurent@428: laurent@428: dc.DrawText(label, pos_x, pos_y) # draw the text laurent@428: laurent@428: laurent@428: class GenStaticBitmap(wx.lib.statbmp.GenStaticBitmap): laurent@428: """ Customized GenStaticBitmap, fix transparency redraw bug on wx2.8/win32, laurent@428: and accept image name as __init__ parameter, fail silently if file do not exist""" laurent@428: def __init__(self, parent, ID, bitmapname, laurent@428: pos = wx.DefaultPosition, size = wx.DefaultSize, laurent@428: style = 0, laurent@428: name = "genstatbmp"): laurent@428: laurent@428: bitmappath = Bpath( "images", bitmapname) laurent@428: if os.path.isfile(bitmappath): laurent@428: bitmap = wx.Bitmap(bitmappath) laurent@428: else: laurent@428: bitmap = None laurent@428: wx.lib.statbmp.GenStaticBitmap.__init__(self, parent, ID, bitmap, laurent@428: pos, size, laurent@428: style, laurent@428: name) laurent@428: laurent@428: def OnPaint(self, event): laurent@428: dc = wx.PaintDC(self) laurent@428: colour = self.GetParent().GetBackgroundColour() laurent@428: dc.SetPen(wx.Pen(colour)) laurent@428: dc.SetBrush(wx.Brush(colour )) laurent@428: dc.DrawRectangle(0, 0, *dc.GetSizeTuple()) laurent@428: if self._bitmap: laurent@428: dc.DrawBitmap(self._bitmap, 0, 0, True) laurent@428: laurent@428: Edouard@705: from threading import Lock,Timer,currentThread Edouard@705: MainThread = currentThread().ident Edouard@686: REFRESH_PERIOD = 0.1 Edouard@686: from time import time as gettime laurent@428: class LogPseudoFile: laurent@428: """ Base class for file like objects to facilitate StdOut for the Shell.""" edouard@451: def __init__(self, output, risecall): laurent@428: self.red_white = wx.TextAttr("RED", "WHITE") laurent@428: self.red_yellow = wx.TextAttr("RED", "YELLOW") laurent@428: self.black_white = wx.TextAttr("BLACK", "WHITE") laurent@428: self.default_style = None laurent@428: self.output = output edouard@451: self.risecall = risecall ed@446: # to prevent rapid fire on rising log panel ed@446: self.rising_timer = 0 Edouard@686: self.lock = Lock() Edouard@705: self.YieldLock = Lock() Edouard@686: self.RefreshLock = Lock() Edouard@686: self.stack = [] Edouard@686: self.LastRefreshTime = gettime() Edouard@688: self.LastRefreshTimer = None laurent@428: laurent@428: def write(self, s, style = None): Edouard@686: if self.lock.acquire(): Edouard@686: self.stack.append((s,style)) Edouard@686: self.lock.release() Edouard@686: current_time = gettime() Edouard@688: if self.LastRefreshTimer: Edouard@688: self.LastRefreshTimer.cancel() Edouard@688: self.LastRefreshTimer=None Edouard@686: if current_time - self.LastRefreshTime > REFRESH_PERIOD and self.RefreshLock.acquire(False): Edouard@705: self._should_write() Edouard@688: else: Edouard@705: self.LastRefreshTimer = Timer(REFRESH_PERIOD, self._should_write) Edouard@688: self.LastRefreshTimer.start() Edouard@686: Edouard@705: def _should_write(self): Edouard@705: wx.CallAfter(self._write) Edouard@705: if MainThread == currentThread().ident: Edouard@705: app = wx.GetApp() Edouard@705: if app is not None: Edouard@705: if self.YieldLock.acquire(0): Edouard@705: app.Yield() Edouard@705: self.YieldLock.release() Edouard@705: Edouard@686: def _write(self): Edouard@701: if self.output : Edouard@701: self.output.Freeze(); Edouard@701: self.lock.acquire() Edouard@701: for s, style in self.stack: Edouard@701: if style is None : style=self.black_white Edouard@701: if self.default_style != style: Edouard@701: self.output.SetDefaultStyle(style) Edouard@701: self.default_style = style Edouard@701: self.output.AppendText(s) Edouard@701: self.output.ScrollLines(s.count('\n')+1) Edouard@701: self.stack = [] Edouard@701: self.lock.release() Edouard@701: self.output.ShowPosition(self.output.GetLastPosition()) Edouard@701: self.output.Thaw() Edouard@701: self.LastRefreshTime = gettime() Edouard@701: try: Edouard@701: self.RefreshLock.release() Edouard@701: except: Edouard@701: pass Edouard@701: newtime = time.time() Edouard@701: if newtime - self.rising_timer > 1: Edouard@701: self.risecall() Edouard@701: self.rising_timer = newtime laurent@428: laurent@428: def write_warning(self, s): laurent@428: self.write(s,self.red_white) laurent@428: laurent@428: def write_error(self, s): laurent@428: self.write(s,self.red_yellow) laurent@428: edouard@569: def writeyield(self, s): edouard@569: self.write(s) edouard@569: wx.GetApp().Yield() edouard@569: laurent@428: def flush(self): laurent@428: self.output.SetValue("") laurent@428: laurent@428: def isatty(self): laurent@428: return false laurent@428: laurent@428: [ID_BEREMIZ, ID_BEREMIZMAINSPLITTER, laurent@428: ID_BEREMIZPLCCONFIG, ID_BEREMIZLOGCONSOLE, Edouard@623: ID_BEREMIZINSPECTOR] = [wx.NewId() for _init_ctrls in range(5)] laurent@428: laurent@675: [ID_FILEMENURECENTPROJECTS, laurent@675: ] = [wx.NewId() for _init_ctrls in range(1)] laurent@675: Edouard@717: CONFNODEMENU_POSITION = 3 laurent@675: laurent@428: class Beremiz(IDEFrame): laurent@428: laurent@675: def _init_coll_MenuBar_Menus(self, parent): laurent@675: IDEFrame._init_coll_MenuBar_Menus(self, parent) laurent@675: Edouard@717: parent.Insert(pos=CONFNODEMENU_POSITION, Edouard@717: menu=self.ConfNodeMenu, title=_(u'&ConfNode')) laurent@675: laurent@675: def _init_utils(self): Edouard@717: self.ConfNodeMenu = wx.Menu(title='') laurent@675: self.RecentProjectsMenu = wx.Menu(title='') laurent@675: laurent@675: IDEFrame._init_utils(self) laurent@675: laurent@428: def _init_coll_FileMenu_Items(self, parent): laurent@428: AppendMenu(parent, help='', id=wx.ID_NEW, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'New\tCTRL+N')) laurent@428: AppendMenu(parent, help='', id=wx.ID_OPEN, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Open\tCTRL+O')) Edouard@702: parent.AppendMenu(ID_FILEMENURECENTPROJECTS, _("&Recent Projects"), self.RecentProjectsMenu) laurent@675: parent.AppendSeparator() laurent@428: AppendMenu(parent, help='', id=wx.ID_SAVE, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Save\tCTRL+S')) laurent@428: AppendMenu(parent, help='', id=wx.ID_SAVEAS, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Save as\tCTRL+SHIFT+S')) laurent@428: AppendMenu(parent, help='', id=wx.ID_CLOSE, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Close Tab\tCTRL+W')) laurent@428: AppendMenu(parent, help='', id=wx.ID_CLOSE_ALL, Edouard@702: kind=wx.ITEM_NORMAL, text=_(u'Close Project\tCTRL+SHIFT+W')) laurent@428: parent.AppendSeparator() laurent@428: AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, Edouard@702: kind=wx.ITEM_NORMAL, text=_(u'Page Setup\tCTRL+ALT+P')) laurent@428: AppendMenu(parent, help='', id=wx.ID_PREVIEW, Edouard@702: kind=wx.ITEM_NORMAL, text=_(u'Preview\tCTRL+SHIFT+P')) laurent@428: AppendMenu(parent, help='', id=wx.ID_PRINT, Edouard@702: kind=wx.ITEM_NORMAL, text=_(u'Print\tCTRL+P')) laurent@428: parent.AppendSeparator() laurent@428: AppendMenu(parent, help='', id=wx.ID_PROPERTIES, Edouard@702: kind=wx.ITEM_NORMAL, text=_(u'&Properties')) laurent@428: parent.AppendSeparator() laurent@428: AppendMenu(parent, help='', id=wx.ID_EXIT, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Quit\tCTRL+Q')) laurent@428: laurent@428: self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) laurent@428: self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) laurent@428: self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) laurent@428: self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS) laurent@428: self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) laurent@428: self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL) laurent@428: self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) laurent@428: self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) laurent@428: self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) laurent@428: self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) laurent@428: self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) laurent@428: laurent@708: self.AddToMenuToolBar([(wx.ID_NEW, "new.png", _(u'New'), None), laurent@708: (wx.ID_OPEN, "open.png", _(u'Open'), None), laurent@708: (wx.ID_SAVE, "save.png", _(u'Save'), None), laurent@708: (wx.ID_SAVEAS, "saveas.png", _(u'Save As...'), None), laurent@708: (wx.ID_PRINT, "print.png", _(u'Print'), None)]) laurent@706: laurent@428: def _init_coll_HelpMenu_Items(self, parent): laurent@428: parent.Append(help='', id=wx.ID_HELP, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'Beremiz\tF1')) laurent@428: parent.Append(help='', id=wx.ID_ABOUT, laurent@428: kind=wx.ITEM_NORMAL, text=_(u'About')) laurent@428: self.Bind(wx.EVT_MENU, self.OnBeremizMenu, id=wx.ID_HELP) laurent@428: self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) laurent@428: laurent@428: def _init_coll_PLCConfigMainSizer_Items(self, parent): laurent@428: parent.AddSizer(self.PLCParamsSizer, 0, border=10, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) Edouard@717: parent.AddSizer(self.ConfNodeTreeSizer, 0, border=10, flag=wx.BOTTOM|wx.LEFT|wx.RIGHT) laurent@428: laurent@428: def _init_coll_PLCConfigMainSizer_Growables(self, parent): laurent@428: parent.AddGrowableCol(0) laurent@428: parent.AddGrowableRow(1) laurent@428: Edouard@717: def _init_coll_ConfNodeTreeSizer_Growables(self, parent): laurent@428: parent.AddGrowableCol(0) laurent@428: parent.AddGrowableCol(1) laurent@428: laurent@428: def _init_beremiz_sizers(self): laurent@428: self.PLCConfigMainSizer = wx.FlexGridSizer(cols=1, hgap=2, rows=2, vgap=2) laurent@428: self.PLCParamsSizer = wx.BoxSizer(wx.VERTICAL) Edouard@717: #self.ConfNodeTreeSizer = wx.FlexGridSizer(cols=3, hgap=0, rows=0, vgap=2) Edouard@717: self.ConfNodeTreeSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=0, vgap=2) laurent@428: laurent@428: self._init_coll_PLCConfigMainSizer_Items(self.PLCConfigMainSizer) laurent@428: self._init_coll_PLCConfigMainSizer_Growables(self.PLCConfigMainSizer) Edouard@717: self._init_coll_ConfNodeTreeSizer_Growables(self.ConfNodeTreeSizer) laurent@428: laurent@428: self.PLCConfig.SetSizer(self.PLCConfigMainSizer) laurent@428: laurent@428: def _init_ctrls(self, prnt): laurent@428: IDEFrame._init_ctrls(self, prnt) laurent@428: laurent@428: self.Bind(wx.EVT_MENU, self.OnOpenWidgetInspector, id=ID_BEREMIZINSPECTOR) Edouard@623: accels = [wx.AcceleratorEntry(wx.ACCEL_CTRL|wx.ACCEL_ALT, ord('I'), ID_BEREMIZINSPECTOR)] Edouard@623: for method,shortcut in [("Stop", wx.WXK_F4), Edouard@623: ("Run", wx.WXK_F5), Edouard@623: ("Transfer", wx.WXK_F6), Edouard@623: ("Connect", wx.WXK_F7), Edouard@623: ("Build", wx.WXK_F11)]: Edouard@623: def OnMethodGen(obj,meth): Edouard@623: def OnMethod(evt): Edouard@717: if obj.CTR is not None: Edouard@717: obj.CTR.CallMethod('_'+meth) Edouard@623: wx.CallAfter(self.RefreshAll) Edouard@623: return OnMethod Edouard@623: newid = wx.NewId() Edouard@623: self.Bind(wx.EVT_MENU, OnMethodGen(self,method), id=newid) Edouard@623: accels += [wx.AcceleratorEntry(wx.ACCEL_NORMAL, shortcut,newid)] Edouard@623: Edouard@623: self.SetAcceleratorTable(wx.AcceleratorTable(accels)) laurent@428: laurent@428: self.PLCConfig = wx.ScrolledWindow(id=ID_BEREMIZPLCCONFIG, laurent@650: name='PLCConfig', parent=self.BottomNoteBook, pos=wx.Point(0, 0), laurent@428: size=wx.Size(-1, -1), style=wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER|wx.HSCROLL|wx.VSCROLL) laurent@428: self.PLCConfig.SetBackgroundColour(wx.WHITE) laurent@428: self.PLCConfig.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@428: self.PLCConfig.Bind(wx.EVT_SIZE, self.OnMoveWindow) laurent@428: self.PLCConfig.Bind(wx.EVT_MOUSEWHEEL, self.OnPLCConfigScroll) laurent@715: self.MainTabs["PLCConfig"] = (self.PLCConfig, _("Topology")) laurent@428: self.BottomNoteBook.InsertPage(0, self.PLCConfig, _("Topology"), True) laurent@428: laurent@428: self.LogConsole = wx.TextCtrl(id=ID_BEREMIZLOGCONSOLE, value='', laurent@428: name='LogConsole', parent=self.BottomNoteBook, pos=wx.Point(0, 0), laurent@428: size=wx.Size(0, 0), style=wx.TE_MULTILINE|wx.TE_RICH2) laurent@428: self.LogConsole.Bind(wx.EVT_LEFT_DCLICK, self.OnLogConsoleDClick) laurent@715: self.MainTabs["LogConsole"] = (self.LogConsole, _("Log Console")) laurent@715: self.BottomNoteBook.AddPage(*self.MainTabs["LogConsole"]) laurent@650: if USE_AUI: laurent@650: self.BottomNoteBook.Split(self.BottomNoteBook.GetPageIndex(self.LogConsole), wx.RIGHT) laurent@428: laurent@428: self._init_beremiz_sizers() laurent@428: Edouard@717: def __init__(self, parent, projectOpen=None, buildpath=None, ctr=None, debug=True): laurent@428: IDEFrame.__init__(self, parent, debug) edouard@451: self.Log = LogPseudoFile(self.LogConsole,self.RiseLogConsole) laurent@428: laurent@428: self.local_runtime = None laurent@428: self.runtime_port = None laurent@428: self.local_runtime_tmpdir = None laurent@428: laurent@428: self.DisableEvents = False laurent@428: # Variable allowing disabling of PLCConfig scroll when Popup shown laurent@428: self.ScrollingEnabled = True laurent@428: laurent@675: self.LastPanelSelected = None laurent@675: Edouard@717: self.ConfNodeInfos = {} laurent@428: laurent@650: # Define Tree item icon list laurent@650: self.LocationImageList = wx.ImageList(16, 16) laurent@650: self.LocationImageDict = {} laurent@650: laurent@650: # Icons for location items laurent@650: for imgname, itemtype in [ Edouard@717: ("CONFIGURATION", LOCATION_CONFNODE), laurent@650: ("RESOURCE", LOCATION_MODULE), laurent@650: ("PROGRAM", LOCATION_GROUP), laurent@650: ("VAR_INPUT", LOCATION_VAR_INPUT), laurent@650: ("VAR_OUTPUT", LOCATION_VAR_OUTPUT), laurent@650: ("VAR_LOCAL", LOCATION_VAR_MEMORY)]: laurent@650: self.LocationImageDict[itemtype]=self.LocationImageList.Add(wx.Bitmap(os.path.join(base_folder, "plcopeneditor", 'Images', '%s.png'%imgname))) laurent@650: laurent@650: # Add beremiz's icon in top left corner of the frame laurent@650: self.SetIcon(wx.Icon(Bpath( "images", "brz.ico"), wx.BITMAP_TYPE_ICO)) laurent@650: laurent@715: if projectOpen is None and self.Config.HasEntry("currenteditedproject"): laurent@715: projectOpen = str(self.Config.Read("currenteditedproject")) laurent@715: if projectOpen == "": laurent@715: projectOpen = None laurent@715: laurent@428: if projectOpen is not None and os.path.isdir(projectOpen): Edouard@717: self.CTR = ConfigTreeRoot(self, self.Log) Edouard@717: self.Controler = self.CTR Edouard@717: result = self.CTR.LoadProject(projectOpen, buildpath) laurent@679: if not result: laurent@716: self.LibraryPanel.SetControler(self.Controler) laurent@679: self.RefreshConfigRecentProjects(os.path.abspath(projectOpen)) laurent@679: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@679: self.RefreshAll() laurent@679: else: laurent@679: self.ResetView() laurent@679: self.ShowErrorMessage(result) laurent@428: else: Edouard@717: self.CTR = ctr Edouard@717: self.Controler = ctr Edouard@717: if ctr is not None: laurent@716: self.LibraryPanel.SetControler(self.Controler) laurent@428: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@428: self.RefreshAll() laurent@490: if self.EnableDebug: Edouard@717: self.DebugVariablePanel.SetDataProducer(self.CTR) laurent@428: laurent@428: self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) laurent@428: laurent@706: self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU) Edouard@717: self.RefreshConfNodeMenu() Edouard@590: self.LogConsole.SetFocus() laurent@428: ed@446: def RiseLogConsole(self): laurent@513: self.BottomNoteBook.SetSelection(self.BottomNoteBook.GetPageIndex(self.LogConsole)) ed@446: laurent@428: def RefreshTitle(self): laurent@428: name = _("Beremiz") Edouard@717: if self.CTR is not None: Edouard@717: projectname = self.CTR.GetProjectName() Edouard@717: if self.CTR.ProjectTestModified(): laurent@428: projectname = "~%s~" % projectname laurent@428: self.SetTitle("%s - %s" % (name, projectname)) laurent@428: else: laurent@428: self.SetTitle(name) laurent@428: laurent@428: def StartLocalRuntime(self, taskbaricon = True): Edouard@713: if (self.local_runtime is None) or (self.local_runtime.exitcode is not None): laurent@428: # create temporary directory for runtime working directory laurent@428: self.local_runtime_tmpdir = tempfile.mkdtemp() laurent@428: # choose an arbitrary random port for runtime laurent@428: self.runtime_port = int(random.random() * 1000) + 61131 laurent@428: # launch local runtime laurent@428: self.local_runtime = ProcessLogger(self.Log, laurent@428: "\"%s\" \"%s\" -p %s -i localhost %s %s"%(sys.executable, laurent@428: Bpath("Beremiz_service.py"), laurent@428: self.runtime_port, laurent@428: {False : "-x 0", True :"-x 1"}[taskbaricon], laurent@428: self.local_runtime_tmpdir), Edouard@704: no_gui=False, Edouard@704: timeout=500, keyword = "working") Edouard@704: self.local_runtime.spin() laurent@428: return self.runtime_port laurent@428: laurent@428: def KillLocalRuntime(self): laurent@428: if self.local_runtime is not None: laurent@428: # shutdown local runtime laurent@428: self.local_runtime.kill(gently=False) laurent@428: # clear temp dir laurent@428: shutil.rmtree(self.local_runtime_tmpdir) laurent@539: laurent@539: self.local_runtime = None laurent@428: laurent@428: def OnOpenWidgetInspector(self, evt): laurent@428: # Activate the widget inspection tool laurent@428: from wx.lib.inspection import InspectionTool laurent@428: if not InspectionTool().initialized: laurent@428: InspectionTool().Init() laurent@428: laurent@428: # Find a widget to be selected in the tree. Use either the laurent@428: # one under the cursor, if any, or this frame. laurent@428: wnd = wx.FindWindowAtPointer() laurent@428: if not wnd: laurent@428: wnd = self laurent@428: InspectionTool().Show(wnd, True) laurent@428: laurent@428: def OnLogConsoleDClick(self, event): laurent@428: wx.CallAfter(self.SearchLineForError) laurent@428: event.Skip() laurent@428: laurent@428: def SearchLineForError(self): Edouard@717: if self.CTR is not None: laurent@428: text = self.LogConsole.GetRange(0, self.LogConsole.GetInsertionPoint()) laurent@428: line = self.LogConsole.GetLineText(len(text.splitlines()) - 1) laurent@428: result = MATIEC_ERROR_MODEL.match(line) laurent@428: if result is not None: laurent@428: first_line, first_column, last_line, last_column, error = result.groups() Edouard@717: infos = self.CTR.ShowError(self.Log, laurent@428: (int(first_line), int(first_column)), laurent@428: (int(last_line), int(last_column))) laurent@429: laurent@429: ## Function displaying an Error dialog in PLCOpenEditor. laurent@429: # @return False if closing cancelled. laurent@429: def CheckSaveBeforeClosing(self, title=_("Close Project")): Edouard@717: if self.CTR.ProjectTestModified(): laurent@429: dialog = wx.MessageDialog(self, laurent@429: _("There are changes, do you want to save?"), laurent@429: title, laurent@429: wx.YES_NO|wx.CANCEL|wx.ICON_QUESTION) laurent@429: answer = dialog.ShowModal() laurent@429: dialog.Destroy() laurent@429: if answer == wx.ID_YES: Edouard@717: self.CTR.SaveProject() laurent@429: elif answer == wx.ID_CANCEL: laurent@429: return False laurent@429: return True laurent@429: laurent@715: def GetTabInfos(self, tab): laurent@715: if (isinstance(tab, EditorPanel) and laurent@715: not isinstance(tab, (Viewer, laurent@715: TextViewer, laurent@715: GraphicViewer, laurent@715: ResourceEditor, laurent@715: ConfigurationEditor, laurent@715: DataTypeEditor))): Edouard@717: return ("confnode", tab.Controler.PlugFullName()) laurent@716: elif (isinstance(tab, TextViewer) and laurent@716: (tab.Controler is None or isinstance(tab.Controler, MiniTextControler))): Edouard@717: return ("confnode", None, tab.GetInstancePath()) laurent@715: else: laurent@715: return IDEFrame.GetTabInfos(self, tab) laurent@715: laurent@715: def LoadTab(self, notebook, page_infos): Edouard@717: if page_infos[0] == "confnode": laurent@716: if page_infos[1] is None: Edouard@717: confnode = self.CTR laurent@716: else: Edouard@717: confnode = self.CTR.GetChildByName(page_infos[1]) Edouard@717: return notebook.GetPageIndex(confnode._OpenView(*page_infos[2:])) laurent@715: else: laurent@715: return IDEFrame.LoadTab(self, notebook, page_infos) laurent@715: laurent@428: def OnCloseFrame(self, event): Edouard@717: if self.CTR is None or self.CheckSaveBeforeClosing(_("Close Application")): Edouard@717: if self.CTR is not None: Edouard@717: self.CTR.KillDebugThread() laurent@429: self.KillLocalRuntime() laurent@598: laurent@715: self.SaveLastState() laurent@715: Edouard@717: if self.CTR is not None: Edouard@717: project_path = os.path.realpath(self.CTR.GetProjectPath()) laurent@715: else: laurent@715: project_path = "" laurent@715: self.Config.Write("currenteditedproject", project_path) laurent@715: self.Config.Flush() laurent@598: laurent@429: event.Skip() laurent@429: else: laurent@429: event.Veto() laurent@428: laurent@428: def OnMoveWindow(self, event): laurent@428: self.GetBestSize() laurent@428: self.RefreshScrollBars() laurent@428: event.Skip() laurent@428: laurent@428: def EnableScrolling(self, enable): laurent@428: self.ScrollingEnabled = enable laurent@428: laurent@428: def OnPLCConfigScroll(self, event): laurent@428: if self.ScrollingEnabled: laurent@428: event.Skip() laurent@428: laurent@428: def OnPanelLeftDown(self, event): laurent@428: focused = self.FindFocus() laurent@428: if isinstance(focused, TextCtrlAutoComplete.TextCtrlAutoComplete): laurent@428: focused.DismissListBox() laurent@428: event.Skip() laurent@428: laurent@428: def RefreshFileMenu(self): laurent@675: self.RefreshRecentProjectsMenu() laurent@675: laurent@706: MenuToolBar = self.Panes["MenuToolBar"] Edouard@717: if self.CTR is not None: laurent@428: selected = self.TabsOpened.GetSelection() laurent@428: if selected >= 0: laurent@428: graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) laurent@428: else: laurent@428: graphic_viewer = False laurent@428: if self.TabsOpened.GetPageCount() > 0: laurent@428: self.FileMenu.Enable(wx.ID_CLOSE, True) laurent@428: if graphic_viewer: laurent@428: self.FileMenu.Enable(wx.ID_PREVIEW, True) laurent@428: self.FileMenu.Enable(wx.ID_PRINT, True) laurent@706: MenuToolBar.EnableTool(wx.ID_PRINT, True) laurent@428: else: laurent@428: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@428: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@706: MenuToolBar.EnableTool(wx.ID_PRINT, False) laurent@428: else: laurent@428: self.FileMenu.Enable(wx.ID_CLOSE, False) laurent@428: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@428: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@706: MenuToolBar.EnableTool(wx.ID_PRINT, False) laurent@428: self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) Edouard@717: project_modified = self.CTR.ProjectTestModified() laurent@706: self.FileMenu.Enable(wx.ID_SAVE, project_modified) laurent@706: MenuToolBar.EnableTool(wx.ID_SAVE, project_modified) laurent@428: self.FileMenu.Enable(wx.ID_SAVEAS, True) laurent@706: MenuToolBar.EnableTool(wx.ID_SAVEAS, True) laurent@428: self.FileMenu.Enable(wx.ID_PROPERTIES, True) laurent@428: self.FileMenu.Enable(wx.ID_CLOSE_ALL, True) laurent@428: else: laurent@428: self.FileMenu.Enable(wx.ID_CLOSE, False) laurent@428: self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) laurent@428: self.FileMenu.Enable(wx.ID_PREVIEW, False) laurent@428: self.FileMenu.Enable(wx.ID_PRINT, False) laurent@706: MenuToolBar.EnableTool(wx.ID_PRINT, False) laurent@428: self.FileMenu.Enable(wx.ID_SAVE, False) laurent@706: MenuToolBar.EnableTool(wx.ID_SAVE, False) laurent@428: self.FileMenu.Enable(wx.ID_SAVEAS, False) laurent@706: MenuToolBar.EnableTool(wx.ID_SAVEAS, False) laurent@428: self.FileMenu.Enable(wx.ID_PROPERTIES, False) laurent@428: self.FileMenu.Enable(wx.ID_CLOSE_ALL, False) laurent@675: laurent@675: def RefreshRecentProjectsMenu(self): laurent@675: for i in xrange(self.RecentProjectsMenu.GetMenuItemCount()): laurent@675: item = self.RecentProjectsMenu.FindItemByPosition(0) laurent@675: self.RecentProjectsMenu.Delete(item.GetId()) laurent@675: laurent@675: recent_projects = cPickle.loads(str(self.Config.Read("RecentProjects", cPickle.dumps([])))) laurent@675: self.FileMenu.Enable(ID_FILEMENURECENTPROJECTS, len(recent_projects) > 0) laurent@675: for idx, projectpath in enumerate(recent_projects): laurent@675: id = wx.NewId() laurent@675: AppendMenu(self.RecentProjectsMenu, help='', id=id, laurent@675: kind=wx.ITEM_NORMAL, text="%d: %s" % (idx + 1, projectpath)) laurent@675: self.Bind(wx.EVT_MENU, self.GenerateOpenRecentProjectFunction(projectpath), id=id) laurent@675: laurent@675: def GenerateOpenRecentProjectFunction(self, projectpath): laurent@675: def OpenRecentProject(event): Edouard@717: if self.CTR is not None and not self.CheckSaveBeforeClosing(): laurent@675: return laurent@675: laurent@675: self.OpenProject(projectpath) laurent@675: return OpenRecentProject laurent@675: laurent@675: def GenerateMenuRecursive(self, items, menu): laurent@675: for kind, infos in items: laurent@675: if isinstance(kind, ListType): laurent@675: text, id = infos laurent@675: submenu = wx.Menu('') laurent@675: self.GenerateMenuRecursive(kind, submenu) laurent@675: menu.AppendMenu(id, text, submenu) laurent@675: elif kind == wx.ITEM_SEPARATOR: laurent@675: menu.AppendSeparator() laurent@675: else: laurent@675: text, id, help, callback = infos laurent@675: AppendMenu(menu, help='', id=id, kind=kind, text=text) laurent@675: if callback is not None: laurent@675: self.Bind(wx.EVT_MENU, callback, id=id) laurent@675: Edouard@717: def RefreshConfNodeMenu(self): Edouard@717: if self.CTR is not None: laurent@675: selected = self.TabsOpened.GetSelection() laurent@675: if selected >= 0: laurent@675: panel = self.TabsOpened.GetPage(selected) laurent@675: else: laurent@675: panel = None laurent@675: if panel != self.LastPanelSelected: Edouard@717: for i in xrange(self.ConfNodeMenu.GetMenuItemCount()): Edouard@717: item = self.ConfNodeMenu.FindItemByPosition(0) Edouard@717: self.ConfNodeMenu.Delete(item.GetId()) laurent@675: self.LastPanelSelected = panel laurent@675: if panel is not None: Edouard@717: items = panel.GetConfNodeMenuItems() laurent@675: else: laurent@675: items = [] Edouard@717: self.MenuBar.EnableTop(CONFNODEMENU_POSITION, len(items) > 0) Edouard@717: self.GenerateMenuRecursive(items, self.ConfNodeMenu) laurent@675: if panel is not None: Edouard@717: panel.RefreshConfNodeMenu(self.ConfNodeMenu) Edouard@717: else: Edouard@717: self.MenuBar.EnableTop(CONFNODEMENU_POSITION, False) laurent@675: self.MenuBar.UpdateMenus() laurent@675: laurent@428: def RefreshScrollBars(self): laurent@428: xstart, ystart = self.PLCConfig.GetViewStart() laurent@428: window_size = self.PLCConfig.GetClientSize() laurent@428: sizer = self.PLCConfig.GetSizer() laurent@428: if sizer: laurent@428: maxx, maxy = sizer.GetMinSize() laurent@662: posx = max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT)) laurent@662: posy = max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT)) laurent@662: self.PLCConfig.Scroll(posx, posy) laurent@428: self.PLCConfig.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, laurent@662: maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, posx, posy) laurent@428: laurent@428: def RefreshPLCParams(self): laurent@428: self.Freeze() laurent@428: self.ClearSizer(self.PLCParamsSizer) laurent@428: Edouard@717: if self.CTR is not None: laurent@428: plcwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) Edouard@717: if self.CTR.PlugTestModified(): laurent@428: bkgdclr = CHANGED_TITLE_COLOUR laurent@428: else: laurent@428: bkgdclr = TITLE_COLOUR laurent@428: Edouard@717: if self.CTR not in self.ConfNodeInfos: Edouard@717: self.ConfNodeInfos[self.CTR] = {"right_visible" : False} laurent@428: laurent@428: plcwindow.SetBackgroundColour(TITLE_COLOUR) laurent@428: plcwindow.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@428: self.PLCParamsSizer.AddWindow(plcwindow, 0, border=0, flag=wx.GROW) laurent@428: laurent@428: plcwindowsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: plcwindow.SetSizer(plcwindowsizer) laurent@428: laurent@428: st = wx.StaticText(plcwindow, -1) laurent@428: st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) Edouard@717: st.SetLabel(self.CTR.GetProjectName()) laurent@428: plcwindowsizer.AddWindow(st, 0, border=5, flag=wx.ALL|wx.ALIGN_CENTER) laurent@428: laurent@428: addbutton_id = wx.NewId() laurent@428: addbutton = wx.lib.buttons.GenBitmapButton(id=addbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Add.png')), Edouard@717: name='AddConfNodeButton', parent=plcwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(16, 16), style=wx.NO_BORDER) Edouard@717: addbutton.SetToolTipString(_("Add a sub confnode")) Edouard@717: addbutton.Bind(wx.EVT_BUTTON, self.Gen_AddConfNodeMenu(self.CTR), id=addbutton_id) laurent@428: plcwindowsizer.AddWindow(addbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@428: laurent@428: plcwindowmainsizer = wx.BoxSizer(wx.VERTICAL) laurent@428: plcwindowsizer.AddSizer(plcwindowmainsizer, 0, border=5, flag=wx.ALL) laurent@428: laurent@428: plcwindowbuttonsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: plcwindowmainsizer.AddSizer(plcwindowbuttonsizer, 0, border=0, flag=wx.ALIGN_CENTER) laurent@428: Edouard@717: msizer = self.GenerateMethodButtonSizer(self.CTR, plcwindow, not self.ConfNodeInfos[self.CTR]["right_visible"]) laurent@428: plcwindowbuttonsizer.AddSizer(msizer, 0, border=0, flag=wx.GROW) laurent@428: laurent@428: paramswindow = wx.Panel(plcwindow, -1, size=wx.Size(-1, -1), style=wx.TAB_TRAVERSAL) laurent@428: paramswindow.SetBackgroundColour(TITLE_COLOUR) laurent@428: paramswindow.Bind(wx.EVT_LEFT_DOWN, self.OnPanelLeftDown) laurent@428: plcwindowbuttonsizer.AddWindow(paramswindow, 0, border=0, flag=0) laurent@428: laurent@428: psizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: paramswindow.SetSizer(psizer) laurent@428: Edouard@717: confnode_infos = self.CTR.GetParamsAttributes() Edouard@717: self.RefreshSizerElement(paramswindow, psizer, self.CTR, confnode_infos, None, False) Edouard@717: Edouard@717: if not self.ConfNodeInfos[self.CTR]["right_visible"]: laurent@428: paramswindow.Hide() laurent@428: laurent@428: minimizebutton_id = wx.NewId() laurent@428: minimizebutton = wx.lib.buttons.GenBitmapToggleButton(id=minimizebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Maximize.png')), laurent@428: name='MinimizeButton', parent=plcwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(24, 24), style=wx.NO_BORDER) laurent@428: make_genbitmaptogglebutton_flat(minimizebutton) laurent@428: minimizebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Minimize.png'))) Edouard@717: minimizebutton.SetToggle(self.ConfNodeInfos[self.CTR]["right_visible"]) laurent@428: plcwindowbuttonsizer.AddWindow(minimizebutton, 0, border=5, flag=wx.ALL) laurent@428: laurent@428: def togglewindow(event): laurent@428: if minimizebutton.GetToggle(): laurent@428: paramswindow.Show() laurent@428: msizer.SetCols(1) laurent@428: else: laurent@428: paramswindow.Hide() Edouard@717: msizer.SetCols(len(self.CTR.ConfNodeMethods)) Edouard@717: self.ConfNodeInfos[self.CTR]["right_visible"] = minimizebutton.GetToggle() laurent@428: self.PLCConfigMainSizer.Layout() laurent@428: self.RefreshScrollBars() laurent@428: event.Skip() laurent@428: minimizebutton.Bind(wx.EVT_BUTTON, togglewindow, id=minimizebutton_id) laurent@428: Edouard@717: self.ConfNodeInfos[self.CTR]["main"] = plcwindow Edouard@717: self.ConfNodeInfos[self.CTR]["params"] = paramswindow laurent@428: laurent@428: self.PLCConfigMainSizer.Layout() laurent@428: self.RefreshScrollBars() laurent@428: self.Thaw() laurent@428: Edouard@717: def GenerateEnableButton(self, parent, sizer, confnode): Edouard@717: enabled = confnode.PlugEnabled() laurent@675: if enabled is not None: laurent@675: enablebutton_id = wx.NewId() laurent@675: enablebutton = wx.lib.buttons.GenBitmapToggleButton(id=enablebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Disabled.png')), laurent@675: name='EnableButton', parent=parent, size=wx.Size(16, 16), pos=wx.Point(0, 0), style=0)#wx.NO_BORDER) Edouard@717: enablebutton.SetToolTipString(_("Enable/Disable this confnode")) laurent@675: make_genbitmaptogglebutton_flat(enablebutton) laurent@675: enablebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Enabled.png'))) laurent@675: enablebutton.SetToggle(enabled) laurent@675: def toggleenablebutton(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, "BaseParams.Enabled", enablebutton.GetToggle()) laurent@675: enablebutton.SetToggle(res) laurent@675: event.Skip() laurent@675: enablebutton.Bind(wx.EVT_BUTTON, toggleenablebutton, id=enablebutton_id) laurent@675: sizer.AddWindow(enablebutton, 0, border=0, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@675: else: laurent@675: sizer.AddSpacer(wx.Size(16, 16)) laurent@675: Edouard@717: def GenerateMethodButtonSizer(self, confnode, parent, horizontal = True): laurent@428: normal_bt_font=wx.Font(faces["size"] / 3, wx.DEFAULT, wx.NORMAL, wx.NORMAL, faceName = faces["helv"]) laurent@428: mouseover_bt_font=wx.Font(faces["size"] / 3, wx.DEFAULT, wx.NORMAL, wx.NORMAL, underline=True, faceName = faces["helv"]) laurent@428: if horizontal: Edouard@717: msizer = wx.FlexGridSizer(cols=len(confnode.ConfNodeMethods)) laurent@428: else: laurent@428: msizer = wx.FlexGridSizer(cols=1) Edouard@717: for confnode_method in confnode.ConfNodeMethods: Edouard@717: if "method" in confnode_method and confnode_method.get("shown",True): laurent@428: id = wx.NewId() Edouard@717: label = confnode_method["name"] laurent@428: button = GenBitmapTextButton(id=id, parent=parent, Edouard@717: bitmap=wx.Bitmap(Bpath( "%s.png"%confnode_method.get("bitmap", os.path.join("images", "Unknown")))), label=label, laurent@428: name=label, pos=wx.DefaultPosition, style=wx.NO_BORDER) laurent@428: button.SetFont(normal_bt_font) Edouard@717: button.SetToolTipString(confnode_method["tooltip"]) Edouard@717: button.Bind(wx.EVT_BUTTON, self.GetButtonCallBackFunction(confnode, confnode_method["method"]), id=id) laurent@428: # a fancy underline on mouseover laurent@428: def setFontStyle(b, s): laurent@428: def fn(event): laurent@428: b.SetFont(s) laurent@428: b.Refresh() laurent@428: event.Skip() laurent@428: return fn laurent@428: button.Bind(wx.EVT_ENTER_WINDOW, setFontStyle(button, mouseover_bt_font)) laurent@428: button.Bind(wx.EVT_LEAVE_WINDOW, setFontStyle(button, normal_bt_font)) laurent@428: #hack to force size to mini Edouard@717: if not confnode_method.get("enabled",True): laurent@428: button.Disable() laurent@428: msizer.AddWindow(button, 0, border=0, flag=wx.ALIGN_CENTER) laurent@428: return msizer laurent@428: Edouard@717: def GenerateParamsPanel(self, confnode, bkgdclr, top_offset=0): laurent@675: rightwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@675: rightwindow.SetBackgroundColour(bkgdclr) laurent@675: laurent@675: rightwindowmainsizer = wx.BoxSizer(wx.VERTICAL) laurent@675: rightwindow.SetSizer(rightwindowmainsizer) laurent@675: laurent@675: rightwindowsizer = wx.FlexGridSizer(cols=2, rows=1) laurent@675: rightwindowsizer.AddGrowableCol(1) laurent@675: rightwindowsizer.AddGrowableRow(0) laurent@675: rightwindowmainsizer.AddSizer(rightwindowsizer, 0, border=0, flag=wx.GROW) laurent@675: Edouard@717: msizer = self.GenerateMethodButtonSizer(confnode, rightwindow, not self.ConfNodeInfos[confnode]["right_visible"]) laurent@683: rightwindowsizer.AddSizer(msizer, 0, border=top_offset, flag=wx.TOP|wx.GROW) laurent@675: laurent@675: rightparamssizer = wx.BoxSizer(wx.HORIZONTAL) laurent@675: rightwindowsizer.AddSizer(rightparamssizer, 0, border=0, flag=wx.ALIGN_RIGHT) laurent@675: laurent@675: paramswindow = wx.Panel(rightwindow, -1, size=wx.Size(-1, -1)) laurent@675: paramswindow.SetBackgroundColour(bkgdclr) laurent@675: laurent@703: psizer = wx.BoxSizer(wx.VERTICAL) laurent@675: paramswindow.SetSizer(psizer) Edouard@717: self.ConfNodeInfos[confnode]["params"] = paramswindow laurent@675: laurent@675: rightparamssizer.AddWindow(paramswindow, 0, border=5, flag=wx.ALL) laurent@675: Edouard@717: confnode_infos = confnode.GetParamsAttributes() Edouard@717: if len(confnode_infos) > 0: Edouard@717: self.RefreshSizerElement(paramswindow, psizer, confnode, confnode_infos, None, False) Edouard@717: Edouard@717: if not self.ConfNodeInfos[confnode]["right_visible"]: laurent@675: paramswindow.Hide() laurent@675: laurent@675: rightminimizebutton_id = wx.NewId() laurent@675: rightminimizebutton = wx.lib.buttons.GenBitmapToggleButton(id=rightminimizebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Maximize.png')), laurent@675: name='MinimizeButton', parent=rightwindow, pos=wx.Point(0, 0), laurent@675: size=wx.Size(24, 24), style=wx.NO_BORDER) laurent@675: make_genbitmaptogglebutton_flat(rightminimizebutton) laurent@675: rightminimizebutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'Minimize.png'))) Edouard@717: rightminimizebutton.SetToggle(self.ConfNodeInfos[confnode]["right_visible"]) laurent@675: rightparamssizer.AddWindow(rightminimizebutton, 0, border=5, flag=wx.ALL) laurent@675: laurent@675: def togglerightwindow(event): laurent@675: if rightminimizebutton.GetToggle(): laurent@675: rightparamssizer.Show(0) laurent@675: msizer.SetCols(1) laurent@675: else: laurent@675: rightparamssizer.Hide(0) Edouard@717: msizer.SetCols(len(confnode.ConfNodeMethods)) Edouard@717: self.ConfNodeInfos[confnode]["right_visible"] = rightminimizebutton.GetToggle() laurent@675: self.PLCConfigMainSizer.Layout() laurent@675: self.RefreshScrollBars() laurent@675: event.Skip() laurent@675: rightminimizebutton.Bind(wx.EVT_BUTTON, togglerightwindow, id=rightminimizebutton_id) laurent@675: laurent@675: return rightwindow laurent@675: laurent@675: Edouard@717: def RefreshConfNodeTree(self): laurent@428: self.Freeze() Edouard@717: self.ClearSizer(self.ConfNodeTreeSizer) Edouard@717: if self.CTR is not None: Edouard@717: for child in self.CTR.IECSortedChilds(): laurent@428: self.GenerateTreeBranch(child) Edouard@717: if not self.ConfNodeInfos[child]["expanded"]: Edouard@717: self.CollapseConfNode(child) laurent@428: self.PLCConfigMainSizer.Layout() laurent@428: self.RefreshScrollBars() laurent@428: self.Thaw() laurent@428: Edouard@717: def SetConfNodeParamsAttribute(self, confnode, *args, **kwargs): Edouard@717: res, StructChanged = confnode.SetParamsAttribute(*args, **kwargs) laurent@428: if StructChanged: Edouard@717: wx.CallAfter(self.RefreshConfNodeTree) Edouard@717: else: Edouard@717: if confnode == self.CTR: laurent@428: bkgdclr = CHANGED_TITLE_COLOUR laurent@428: items = ["main", "params"] laurent@428: else: laurent@428: bkgdclr = CHANGED_WINDOW_COLOUR laurent@428: items = ["left", "right", "params"] laurent@428: for i in items: Edouard@717: self.ConfNodeInfos[confnode][i].SetBackgroundColour(bkgdclr) Edouard@717: self.ConfNodeInfos[confnode][i].Refresh() laurent@627: self._Refresh(TITLE, FILEMENU) laurent@428: return res laurent@428: Edouard@717: def ExpandConfNode(self, confnode, force = False): Edouard@717: for child in self.ConfNodeInfos[confnode]["children"]: Edouard@717: self.ConfNodeInfos[child]["left"].Show() Edouard@717: self.ConfNodeInfos[child]["right"].Show() Edouard@717: if force or self.ConfNodeInfos[child]["expanded"]: Edouard@717: self.ExpandConfNode(child, force) laurent@428: if force: Edouard@717: self.ConfNodeInfos[child]["expanded"] = True Edouard@717: locations_infos = self.ConfNodeInfos[confnode].get("locations_infos", None) laurent@428: if locations_infos is not None: laurent@428: if force or locations_infos["root"]["expanded"]: laurent@428: self.ExpandLocation(locations_infos, "root", force) laurent@428: if force: laurent@428: locations_infos["root"]["expanded"] = True laurent@428: Edouard@717: def CollapseConfNode(self, confnode, force = False): Edouard@717: for child in self.ConfNodeInfos[confnode]["children"]: Edouard@717: self.ConfNodeInfos[child]["left"].Hide() Edouard@717: self.ConfNodeInfos[child]["right"].Hide() Edouard@717: self.CollapseConfNode(child, force) laurent@428: if force: Edouard@717: self.ConfNodeInfos[child]["expanded"] = False Edouard@717: locations_infos = self.ConfNodeInfos[confnode].get("locations_infos", None) laurent@428: if locations_infos is not None: laurent@428: self.CollapseLocation(locations_infos, "root", force) laurent@428: if force: laurent@428: locations_infos["root"]["expanded"] = False laurent@428: laurent@650: def ExpandLocation(self, locations_infos, group, force = False, refresh_size=True): laurent@650: locations_infos[group]["expanded"] = True laurent@650: if group == "root": laurent@650: if locations_infos[group]["left"] is not None: laurent@650: locations_infos[group]["left"].Show() laurent@650: if locations_infos[group]["right"] is not None: laurent@650: locations_infos[group]["right"].Show() laurent@650: elif locations_infos["root"]["left"] is not None: laurent@650: locations_infos["root"]["left"].Expand(locations_infos[group]["item"]) laurent@428: if force: laurent@650: for child in locations_infos[group]["children"]: laurent@650: self.ExpandLocation(locations_infos, child, force, False) laurent@650: if locations_infos["root"]["left"] is not None and refresh_size: laurent@650: self.RefreshTreeCtrlSize(locations_infos["root"]["left"]) laurent@650: laurent@650: def CollapseLocation(self, locations_infos, group, force = False, refresh_size=True): laurent@650: locations_infos[group]["expanded"] = False laurent@650: if group == "root": laurent@650: if locations_infos[group]["left"] is not None: laurent@650: locations_infos[group]["left"].Hide() laurent@650: if locations_infos[group]["right"] is not None: laurent@650: locations_infos[group]["right"].Hide() laurent@650: elif locations_infos["root"]["left"] is not None: laurent@650: locations_infos["root"]["left"].Collapse(locations_infos[group]["item"]) laurent@650: if force: laurent@650: for child in locations_infos[group]["children"]: laurent@650: self.CollapseLocation(locations_infos, child, force, False) laurent@650: if locations_infos["root"]["left"] is not None and refresh_size: laurent@650: self.RefreshTreeCtrlSize(locations_infos["root"]["left"]) laurent@675: Edouard@717: def GenerateTreeBranch(self, confnode): laurent@428: leftwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) Edouard@717: if confnode.PlugTestModified(): laurent@428: bkgdclr=CHANGED_WINDOW_COLOUR laurent@428: else: laurent@428: bkgdclr=WINDOW_COLOUR laurent@428: laurent@428: leftwindow.SetBackgroundColour(bkgdclr) laurent@428: Edouard@717: if not self.ConfNodeInfos.has_key(confnode): Edouard@717: self.ConfNodeInfos[confnode] = {"expanded" : False, "right_visible" : False} Edouard@717: Edouard@717: self.ConfNodeInfos[confnode]["children"] = confnode.IECSortedChilds() Edouard@717: confnode_locations = [] Edouard@717: if len(self.ConfNodeInfos[confnode]["children"]) == 0: Edouard@717: confnode_locations = confnode.GetVariableLocationTree()["children"] Edouard@717: if not self.ConfNodeInfos[confnode].has_key("locations_infos"): Edouard@717: self.ConfNodeInfos[confnode]["locations_infos"] = {"root": {"expanded" : False}} Edouard@717: Edouard@717: self.ConfNodeInfos[confnode]["locations_infos"]["root"]["left"] = None Edouard@717: self.ConfNodeInfos[confnode]["locations_infos"]["root"]["right"] = None Edouard@717: self.ConfNodeInfos[confnode]["locations_infos"]["root"]["children"] = [] Edouard@717: Edouard@717: self.ConfNodeTreeSizer.AddWindow(leftwindow, 0, border=0, flag=wx.GROW) laurent@428: laurent@428: leftwindowsizer = wx.FlexGridSizer(cols=1, rows=2) laurent@428: leftwindowsizer.AddGrowableCol(0) laurent@428: leftwindow.SetSizer(leftwindowsizer) laurent@428: laurent@428: leftbuttonmainsizer = wx.FlexGridSizer(cols=3, rows=1) laurent@428: leftbuttonmainsizer.AddGrowableCol(0) laurent@428: leftwindowsizer.AddSizer(leftbuttonmainsizer, 0, border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT) #|wx.TOP laurent@428: laurent@428: leftbuttonsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: leftbuttonmainsizer.AddSizer(leftbuttonsizer, 0, border=5, flag=wx.GROW|wx.RIGHT) laurent@428: laurent@428: leftsizer = wx.BoxSizer(wx.VERTICAL) laurent@428: leftbuttonsizer.AddSizer(leftsizer, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@428: laurent@428: rolesizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: leftsizer.AddSizer(rolesizer, 0, border=0, flag=wx.GROW|wx.RIGHT) laurent@675: Edouard@717: #self.GenerateEnableButton(leftwindow, rolesizer, confnode) laurent@428: laurent@428: roletext = wx.StaticText(leftwindow, -1) Edouard@717: roletext.SetLabel(confnode.PlugHelp) laurent@428: rolesizer.AddWindow(roletext, 0, border=5, flag=wx.RIGHT|wx.ALIGN_LEFT) laurent@428: Edouard@717: confnode_IECChannel = confnode.BaseParams.getIEC_Channel() laurent@428: laurent@428: iecsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@428: leftsizer.AddSizer(iecsizer, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@428: laurent@428: st = wx.StaticText(leftwindow, -1) laurent@428: st.SetFont(wx.Font(faces["size"], wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) Edouard@717: st.SetLabel(confnode.GetFullIEC_Channel()) laurent@428: iecsizer.AddWindow(st, 0, border=0, flag=0) laurent@428: laurent@428: updownsizer = wx.BoxSizer(wx.VERTICAL) laurent@428: iecsizer.AddSizer(updownsizer, 0, border=5, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL) laurent@428: Edouard@717: if confnode_IECChannel > 0: laurent@428: ieccdownbutton_id = wx.NewId() laurent@428: ieccdownbutton = wx.lib.buttons.GenBitmapButton(id=ieccdownbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'IECCDown.png')), laurent@428: name='IECCDownButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(16, 16), style=wx.NO_BORDER) Edouard@717: ieccdownbutton.Bind(wx.EVT_BUTTON, self.GetItemChannelChangedFunction(confnode, confnode_IECChannel - 1), id=ieccdownbutton_id) laurent@428: updownsizer.AddWindow(ieccdownbutton, 0, border=0, flag=wx.ALIGN_LEFT) laurent@428: laurent@428: ieccupbutton_id = wx.NewId() laurent@428: ieccupbutton = wx.lib.buttons.GenBitmapTextButton(id=ieccupbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'IECCUp.png')), laurent@428: name='IECCUpButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(16, 16), style=wx.NO_BORDER) Edouard@717: ieccupbutton.Bind(wx.EVT_BUTTON, self.GetItemChannelChangedFunction(confnode, confnode_IECChannel + 1), id=ieccupbutton_id) laurent@428: updownsizer.AddWindow(ieccupbutton, 0, border=0, flag=wx.ALIGN_LEFT) laurent@428: laurent@428: adddeletesizer = wx.BoxSizer(wx.VERTICAL) laurent@428: iecsizer.AddSizer(adddeletesizer, 0, border=5, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL) laurent@428: laurent@428: deletebutton_id = wx.NewId() laurent@428: deletebutton = wx.lib.buttons.GenBitmapButton(id=deletebutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Delete.png')), Edouard@717: name='DeleteConfNodeButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(16, 16), style=wx.NO_BORDER) Edouard@717: deletebutton.SetToolTipString(_("Delete this confnode")) Edouard@717: deletebutton.Bind(wx.EVT_BUTTON, self.GetDeleteButtonFunction(confnode), id=deletebutton_id) laurent@428: adddeletesizer.AddWindow(deletebutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@428: Edouard@717: if len(confnode.PlugChildsTypes) > 0: laurent@428: addbutton_id = wx.NewId() laurent@428: addbutton = wx.lib.buttons.GenBitmapButton(id=addbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'Add.png')), Edouard@717: name='AddConfNodeButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(16, 16), style=wx.NO_BORDER) Edouard@717: addbutton.SetToolTipString(_("Add a sub confnode")) Edouard@717: addbutton.Bind(wx.EVT_BUTTON, self.Gen_AddConfNodeMenu(confnode), id=addbutton_id) laurent@428: adddeletesizer.AddWindow(addbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER) laurent@428: laurent@428: expandbutton_id = wx.NewId() laurent@428: expandbutton = wx.lib.buttons.GenBitmapToggleButton(id=expandbutton_id, bitmap=wx.Bitmap(Bpath( 'images', 'plus.png')), laurent@428: name='ExpandButton', parent=leftwindow, pos=wx.Point(0, 0), laurent@428: size=wx.Size(13, 13), style=wx.NO_BORDER) laurent@428: expandbutton.labelDelta = 0 laurent@428: expandbutton.SetBezelWidth(0) laurent@428: expandbutton.SetUseFocusIndicator(False) laurent@428: expandbutton.SetBitmapSelected(wx.Bitmap(Bpath( 'images', 'minus.png'))) laurent@428: Edouard@717: if len(self.ConfNodeInfos[confnode]["children"]) > 0: Edouard@717: expandbutton.SetToggle(self.ConfNodeInfos[confnode]["expanded"]) laurent@428: def togglebutton(event): laurent@428: if expandbutton.GetToggle(): Edouard@717: self.ExpandConfNode(confnode) laurent@428: else: Edouard@717: self.CollapseConfNode(confnode) Edouard@717: self.ConfNodeInfos[confnode]["expanded"] = expandbutton.GetToggle() laurent@428: self.PLCConfigMainSizer.Layout() laurent@428: self.RefreshScrollBars() laurent@428: event.Skip() laurent@428: expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) Edouard@717: elif len(confnode_locations) > 0: Edouard@717: locations_infos = self.ConfNodeInfos[confnode]["locations_infos"] laurent@428: expandbutton.SetToggle(locations_infos["root"]["expanded"]) laurent@428: def togglebutton(event): laurent@428: if expandbutton.GetToggle(): laurent@428: self.ExpandLocation(locations_infos, "root") laurent@428: else: laurent@428: self.CollapseLocation(locations_infos, "root") Edouard@717: self.ConfNodeInfos[confnode]["expanded"] = expandbutton.GetToggle() laurent@428: locations_infos["root"]["expanded"] = expandbutton.GetToggle() laurent@428: self.PLCConfigMainSizer.Layout() laurent@428: self.RefreshScrollBars() laurent@428: event.Skip() laurent@428: expandbutton.Bind(wx.EVT_BUTTON, togglebutton, id=expandbutton_id) laurent@428: else: laurent@428: expandbutton.Enable(False) laurent@428: iecsizer.AddWindow(expandbutton, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@428: laurent@428: tc_id = wx.NewId() laurent@428: tc = wx.TextCtrl(leftwindow, tc_id, size=wx.Size(150, 25), style=wx.NO_BORDER) laurent@428: tc.SetFont(wx.Font(faces["size"] * 0.75, wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName = faces["helv"])) Edouard@717: tc.ChangeValue(confnode.MandatoryParams[1].getName()) Edouard@717: tc.Bind(wx.EVT_TEXT, self.GetTextCtrlCallBackFunction(tc, confnode, "BaseParams.Name"), id=tc_id) laurent@428: iecsizer.AddWindow(tc, 0, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) laurent@675: Edouard@717: rightwindow = self.GenerateParamsPanel(confnode, bkgdclr, 8) Edouard@717: self.ConfNodeTreeSizer.AddWindow(rightwindow, 0, border=0, flag=wx.GROW) Edouard@717: Edouard@717: self.ConfNodeInfos[confnode]["left"] = leftwindow Edouard@717: self.ConfNodeInfos[confnode]["right"] = rightwindow Edouard@717: for child in self.ConfNodeInfos[confnode]["children"]: laurent@428: self.GenerateTreeBranch(child) Edouard@717: if not self.ConfNodeInfos[child]["expanded"]: Edouard@717: self.CollapseConfNode(child) Edouard@717: Edouard@717: if len(confnode_locations) > 0: Edouard@717: locations_infos = self.ConfNodeInfos[confnode]["locations_infos"] laurent@650: treectrl = wx.TreeCtrl(self.PLCConfig, -1, size=wx.DefaultSize, laurent@650: style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.NO_BORDER|wx.TR_HIDE_ROOT|wx.TR_NO_LINES|wx.TR_LINES_AT_ROOT) laurent@650: treectrl.SetImageList(self.LocationImageList) laurent@650: treectrl.Bind(wx.EVT_TREE_BEGIN_DRAG, self.GenerateLocationBeginDragFunction(locations_infos)) laurent@650: treectrl.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.GenerateLocationExpandCollapseFunction(locations_infos, True)) laurent@650: treectrl.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.GenerateLocationExpandCollapseFunction(locations_infos, False)) laurent@662: treectrl.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheelTreeCtrl) laurent@650: laurent@650: treectrl.AddRoot("") Edouard@717: self.ConfNodeTreeSizer.AddWindow(treectrl, 0, border=0, flag=0) laurent@650: laurent@650: rightwindow = wx.Panel(self.PLCConfig, -1, size=wx.Size(-1, -1)) laurent@650: rightwindow.SetBackgroundColour(wx.WHITE) Edouard@717: self.ConfNodeTreeSizer.AddWindow(rightwindow, 0, border=0, flag=wx.GROW) laurent@650: laurent@650: locations_infos["root"]["left"] = treectrl laurent@650: locations_infos["root"]["right"] = rightwindow Edouard@717: for location in confnode_locations: laurent@428: locations_infos["root"]["children"].append("root.%s" % location["name"]) laurent@650: self.GenerateLocationTreeBranch(treectrl, treectrl.GetRootItem(), locations_infos, "root", location) laurent@650: if locations_infos["root"]["expanded"]: Edouard@717: self.ConfNodeTreeSizer.Layout() laurent@650: self.ExpandLocation(locations_infos, "root") laurent@650: else: laurent@650: self.RefreshTreeCtrlSize(treectrl) laurent@650: laurent@650: def GenerateLocationTreeBranch(self, treectrl, root, locations_infos, parent, location): laurent@428: location_name = "%s.%s" % (parent, location["name"]) laurent@428: if not locations_infos.has_key(location_name): laurent@428: locations_infos[location_name] = {"expanded" : False} laurent@428: laurent@650: if location["type"] in [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]: laurent@650: label = "%(name)s (%(location)s)" % location laurent@650: elif location["location"] != "": laurent@650: label = "%(location)s: %(name)s" % location laurent@428: else: laurent@650: label = location["name"] laurent@650: item = treectrl.AppendItem(root, label) laurent@650: treectrl.SetPyData(item, location_name) laurent@650: treectrl.SetItemImage(item, self.LocationImageDict[location["type"]]) laurent@650: laurent@650: locations_infos[location_name]["item"] = item laurent@428: locations_infos[location_name]["children"] = [] laurent@650: infos = location.copy() laurent@650: infos.pop("children") laurent@650: locations_infos[location_name]["infos"] = infos laurent@428: for child in location["children"]: laurent@428: child_name = "%s.%s" % (location_name, child["name"]) laurent@428: locations_infos[location_name]["children"].append(child_name) laurent@650: self.GenerateLocationTreeBranch(treectrl, item, locations_infos, location_name, child) laurent@650: if locations_infos[location_name]["expanded"]: laurent@650: self.ExpandLocation(locations_infos, location_name) laurent@650: laurent@650: def GenerateLocationBeginDragFunction(self, locations_infos): laurent@650: def OnLocationBeginDragFunction(event): laurent@650: item = event.GetItem() laurent@650: location_name = locations_infos["root"]["left"].GetPyData(item) laurent@650: if location_name is not None: laurent@650: infos = locations_infos[location_name]["infos"] laurent@650: if infos["type"] in [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]: laurent@662: data = wx.TextDataObject(str((infos["location"], "location", infos["IEC_type"], infos["var_name"], infos["description"]))) laurent@650: dragSource = wx.DropSource(self) laurent@650: dragSource.SetData(data) laurent@650: dragSource.DoDragDrop() laurent@650: return OnLocationBeginDragFunction laurent@650: laurent@650: def RefreshTreeCtrlSize(self, treectrl): laurent@650: rect = self.GetTreeCtrlItemRect(treectrl, treectrl.GetRootItem()) laurent@662: treectrl.SetMinSize(wx.Size(max(rect.width, rect.x + rect.width) + 20, max(rect.height, rect.y + rect.height) + 20)) laurent@650: self.PLCConfigMainSizer.Layout() laurent@650: self.PLCConfig.Refresh() laurent@662: wx.CallAfter(self.RefreshScrollBars) laurent@662: laurent@662: def OnMouseWheelTreeCtrl(self, event): laurent@662: x, y = self.PLCConfig.GetViewStart() laurent@662: rotation = - (event.GetWheelRotation() / event.GetWheelDelta()) * 3 laurent@662: if event.ShiftDown(): laurent@662: self.PLCConfig.Scroll(x + rotation, y) laurent@662: else: laurent@662: self.PLCConfig.Scroll(x, y + rotation) laurent@650: laurent@650: def GetTreeCtrlItemRect(self, treectrl, item): laurent@650: item_rect = treectrl.GetBoundingRect(item, True) laurent@650: if item_rect is not None: laurent@650: minx, miny = item_rect.x, item_rect.y laurent@650: maxx, maxy = item_rect.x + item_rect.width, item_rect.y + item_rect.height laurent@650: else: laurent@650: minx = miny = maxx = maxy = 0 laurent@650: laurent@650: if treectrl.ItemHasChildren(item) and (item == treectrl.GetRootItem() or treectrl.IsExpanded(item)): laurent@650: if wx.VERSION >= (2, 6, 0): laurent@650: child, item_cookie = treectrl.GetFirstChild(item) laurent@650: else: laurent@650: child, item_cookie = treectrl.GetFirstChild(item, 0) laurent@650: while child.IsOk(): laurent@650: child_rect = self.GetTreeCtrlItemRect(treectrl, child) laurent@650: minx = min(minx, child_rect.x) laurent@650: miny = min(miny, child_rect.y) laurent@650: maxx = max(maxx, child_rect.x + child_rect.width) laurent@650: maxy = max(maxy, child_rect.y + child_rect.height) laurent@650: child, item_cookie = treectrl.GetNextChild(item, item_cookie) laurent@650: laurent@650: return wx.Rect(minx, miny, maxx - minx, maxy - miny) laurent@650: laurent@650: def GenerateLocationExpandCollapseFunction(self, locations_infos, expand): laurent@650: def OnLocationExpandedFunction(event): laurent@650: item = event.GetItem() laurent@650: location_name = locations_infos["root"]["left"].GetPyData(item) laurent@650: if location_name is not None: laurent@650: locations_infos[location_name]["expanded"] = expand laurent@650: self.RefreshTreeCtrlSize(locations_infos["root"]["left"]) laurent@428: event.Skip() laurent@650: return OnLocationExpandedFunction laurent@428: laurent@428: def RefreshAll(self): laurent@428: self.RefreshPLCParams() Edouard@717: self.RefreshConfNodeTree() Edouard@717: Edouard@717: def GetItemChannelChangedFunction(self, confnode, value): Edouard@717: def OnConfNodeTreeItemChannelChanged(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, "BaseParams.IEC_Channel", value) laurent@428: event.Skip() Edouard@717: return OnConfNodeTreeItemChannelChanged Edouard@717: Edouard@717: def _GetAddConfNodeFunction(self, name, confnode): Edouard@717: def OnConfNodeMenu(event): Edouard@717: wx.CallAfter(self.AddConfNode, name, confnode) Edouard@717: return OnConfNodeMenu Edouard@717: Edouard@717: def Gen_AddConfNodeMenu(self, confnode): Edouard@717: def AddConfNodeMenu(event): laurent@428: main_menu = wx.Menu(title='') Edouard@717: if len(confnode.PlugChildsTypes) > 0: Edouard@717: for name, XSDClass, help in confnode.PlugChildsTypes: laurent@428: new_id = wx.NewId() laurent@428: main_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=_("Append ")+help) Edouard@717: self.Bind(wx.EVT_MENU, self._GetAddConfNodeFunction(name, confnode), id=new_id) laurent@428: self.PopupMenuXY(main_menu) laurent@708: main_menu.Destroy() Edouard@717: return AddConfNodeMenu Edouard@717: Edouard@717: def GetButtonCallBackFunction(self, confnode, method): Edouard@717: """ Generate the callbackfunc for a given confnode method""" laurent@428: def OnButtonClick(event): laurent@428: # Disable button to prevent re-entrant call laurent@428: event.GetEventObject().Disable() laurent@428: # Call Edouard@717: getattr(confnode,method)() laurent@428: # Re-enable button laurent@428: event.GetEventObject().Enable() laurent@428: # Trigger refresh on Idle laurent@428: wx.CallAfter(self.RefreshAll) laurent@428: event.Skip() laurent@428: return OnButtonClick laurent@428: Edouard@717: def GetChoiceCallBackFunction(self, choicectrl, confnode, path): laurent@428: def OnChoiceChanged(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, path, choicectrl.GetStringSelection()) laurent@428: choicectrl.SetStringSelection(res) laurent@428: event.Skip() laurent@428: return OnChoiceChanged laurent@428: Edouard@717: def GetChoiceContentCallBackFunction(self, choicectrl, staticboxsizer, confnode, path): laurent@428: def OnChoiceContentChanged(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, path, choicectrl.GetStringSelection()) laurent@428: if wx.VERSION < (2, 8, 0): laurent@428: self.ParamsPanel.Freeze() laurent@428: choicectrl.SetStringSelection(res) Edouard@717: infos = self.CTR.GetParamsAttributes(path) laurent@428: staticbox = staticboxsizer.GetStaticBox() laurent@428: staticbox.SetLabel("%(name)s - %(value)s"%infos) laurent@428: self.RefreshSizerElement(self.ParamsPanel, staticboxsizer, infos["children"], "%s.%s"%(path, infos["name"]), selected=selected) laurent@428: self.ParamsPanelMainSizer.Layout() laurent@428: self.ParamsPanel.Thaw() laurent@428: self.ParamsPanel.Refresh() laurent@428: else: laurent@428: wx.CallAfter(self.RefreshAll) laurent@428: event.Skip() laurent@428: return OnChoiceContentChanged laurent@428: Edouard@717: def GetTextCtrlCallBackFunction(self, textctrl, confnode, path): laurent@428: def OnTextCtrlChanged(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, path, textctrl.GetValue()) laurent@428: if res != textctrl.GetValue(): laurent@428: textctrl.ChangeValue(res) laurent@428: event.Skip() laurent@428: return OnTextCtrlChanged laurent@428: Edouard@717: def GetCheckBoxCallBackFunction(self, chkbx, confnode, path): laurent@428: def OnCheckBoxChanged(event): Edouard@717: res = self.SetConfNodeParamsAttribute(confnode, path, chkbx.IsChecked()) laurent@428: chkbx.SetValue(res) laurent@428: event.Skip() laurent@428: return OnCheckBoxChanged laurent@428: Edouard@717: def GetBrowseCallBackFunction(self, name, textctrl, library, value_infos, confnode, path): laurent@703: infos = [value_infos] laurent@703: def OnBrowseButton(event): laurent@703: dialog = BrowseValuesLibraryDialog(self, name, library, infos[0]) laurent@703: if dialog.ShowModal() == wx.ID_OK: Edouard@717: value, value_infos = self.SetConfNodeParamsAttribute(confnode, path, dialog.GetValueInfos()) laurent@703: textctrl.ChangeValue(value) laurent@703: infos[0] = value_infos laurent@703: dialog.Destroy() laurent@703: event.Skip() laurent@703: return OnBrowseButton laurent@703: laurent@428: def ClearSizer(self, sizer): laurent@428: staticboxes = [] laurent@428: for item in sizer.GetChildren(): laurent@428: if item.IsSizer(): laurent@428: item_sizer = item.GetSizer() laurent@428: self.ClearSizer(item_sizer) laurent@428: if isinstance(item_sizer, wx.StaticBoxSizer): laurent@428: staticboxes.append(item_sizer.GetStaticBox()) laurent@428: sizer.Clear(True) laurent@428: for staticbox in staticboxes: laurent@428: staticbox.Destroy() laurent@428: Edouard@717: def RefreshSizerElement(self, parent, sizer, confnode, elements, path, clean = True): laurent@428: if clean: laurent@428: if wx.VERSION < (2, 8, 0): laurent@428: self.ClearSizer(sizer) laurent@428: else: laurent@428: sizer.Clear(True) laurent@428: first = True laurent@428: for element_infos in elements: laurent@428: if path: laurent@428: element_path = "%s.%s"%(path, element_infos["name"]) laurent@428: else: laurent@428: element_path = element_infos["name"] laurent@428: if element_infos["type"] == "element": laurent@428: label = element_infos["name"] laurent@428: staticbox = wx.StaticBox(id=-1, label=_(label), laurent@428: name='%s_staticbox'%element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.Size(10, 0), style=0) laurent@428: staticboxsizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL) laurent@428: if first: laurent@428: sizer.AddSizer(staticboxsizer, 0, border=0, flag=wx.GROW|wx.TOP) laurent@428: else: laurent@428: sizer.AddSizer(staticboxsizer, 0, border=0, flag=wx.GROW) Edouard@717: self.RefreshSizerElement(parent, staticboxsizer, confnode, element_infos["children"], element_path) laurent@428: else: laurent@428: boxsizer = wx.FlexGridSizer(cols=3, rows=1) laurent@428: boxsizer.AddGrowableCol(1) laurent@428: if first: laurent@428: sizer.AddSizer(boxsizer, 0, border=5, flag=wx.GROW|wx.ALL) laurent@428: else: laurent@428: sizer.AddSizer(boxsizer, 0, border=5, flag=wx.GROW|wx.LEFT|wx.RIGHT|wx.BOTTOM) laurent@428: staticbitmap = GenStaticBitmap(ID=-1, bitmapname="%s.png"%element_infos["name"], laurent@428: name="%s_bitmap"%element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.Size(24, 24), style=0) laurent@428: boxsizer.AddWindow(staticbitmap, 0, border=5, flag=wx.RIGHT) laurent@428: label = element_infos["name"] laurent@428: statictext = wx.StaticText(id=-1, label="%s:"%_(label), laurent@428: name="%s_label"%element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@428: boxsizer.AddWindow(statictext, 0, border=5, flag=wx.ALIGN_CENTER_VERTICAL|wx.RIGHT) laurent@428: id = wx.NewId() laurent@428: if isinstance(element_infos["type"], types.ListType): laurent@703: if isinstance(element_infos["value"], types.TupleType): laurent@703: browse_boxsizer = wx.BoxSizer(wx.HORIZONTAL) laurent@703: boxsizer.AddSizer(browse_boxsizer, 0, border=0, flag=0) laurent@703: laurent@703: textctrl = wx.TextCtrl(id=id, name=element_infos["name"], parent=parent, laurent@703: pos=wx.Point(0, 0), size=wx.Size(275, 25), style=wx.TE_READONLY) laurent@703: if element_infos["value"] is not None: laurent@703: textctrl.SetValue(element_infos["value"][0]) laurent@703: value_infos = element_infos["value"][1] laurent@703: else: laurent@703: value_infos = None laurent@703: browse_boxsizer.AddWindow(textctrl, 0, border=0, flag=0) laurent@703: button_id = wx.NewId() laurent@703: button = wx.Button(id=button_id, name="browse_%s" % element_infos["name"], parent=parent, laurent@703: label="...", pos=wx.Point(0, 0), size=wx.Size(25, 25)) laurent@703: browse_boxsizer.AddWindow(button, 0, border=0, flag=0) laurent@703: button.Bind(wx.EVT_BUTTON, laurent@703: self.GetBrowseCallBackFunction(element_infos["name"], textctrl, element_infos["type"], Edouard@717: value_infos, confnode, element_path), laurent@703: id=button_id) laurent@428: else: laurent@703: combobox = wx.ComboBox(id=id, name=element_infos["name"], parent=parent, laurent@703: pos=wx.Point(0, 0), size=wx.Size(300, 28), style=wx.CB_READONLY) laurent@703: boxsizer.AddWindow(combobox, 0, border=0, flag=0) laurent@703: if element_infos["use"] == "optional": laurent@703: combobox.Append("") laurent@703: if len(element_infos["type"]) > 0 and isinstance(element_infos["type"][0], types.TupleType): laurent@703: for choice, xsdclass in element_infos["type"]: laurent@703: combobox.Append(choice) laurent@703: name = element_infos["name"] laurent@703: value = element_infos["value"] laurent@703: staticbox = wx.StaticBox(id=-1, label="%s - %s"%(_(name), _(value)), laurent@703: name='%s_staticbox'%element_infos["name"], parent=parent, laurent@703: pos=wx.Point(0, 0), size=wx.Size(10, 0), style=0) laurent@703: staticboxsizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL) laurent@703: sizer.AddSizer(staticboxsizer, 0, border=5, flag=wx.GROW|wx.BOTTOM) Edouard@717: self.RefreshSizerElement(parent, staticboxsizer, confnode, element_infos["children"], element_path) Edouard@717: callback = self.GetChoiceContentCallBackFunction(combobox, staticboxsizer, confnode, element_path) laurent@703: else: laurent@703: for choice in element_infos["type"]: laurent@703: combobox.Append(choice) Edouard@717: callback = self.GetChoiceCallBackFunction(combobox, confnode, element_path) laurent@703: if element_infos["value"] is None: laurent@703: combobox.SetStringSelection("") laurent@703: else: laurent@703: combobox.SetStringSelection(element_infos["value"]) laurent@703: combobox.Bind(wx.EVT_COMBOBOX, callback, id=id) laurent@428: elif isinstance(element_infos["type"], types.DictType): laurent@428: scmin = -(2**31) laurent@428: scmax = 2**31-1 laurent@428: if "min" in element_infos["type"]: laurent@428: scmin = element_infos["type"]["min"] laurent@428: if "max" in element_infos["type"]: laurent@428: scmax = element_infos["type"]["max"] laurent@428: spinctrl = wx.SpinCtrl(id=id, name=element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.Size(300, 25), style=wx.SP_ARROW_KEYS|wx.ALIGN_RIGHT) laurent@428: spinctrl.SetRange(scmin,scmax) laurent@428: boxsizer.AddWindow(spinctrl, 0, border=0, flag=0) laurent@703: if element_infos["value"] is not None: laurent@703: spinctrl.SetValue(element_infos["value"]) Edouard@717: spinctrl.Bind(wx.EVT_SPINCTRL, self.GetTextCtrlCallBackFunction(spinctrl, confnode, element_path), id=id) laurent@428: else: laurent@428: if element_infos["type"] == "boolean": laurent@428: checkbox = wx.CheckBox(id=id, name=element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.Size(17, 25), style=0) laurent@428: boxsizer.AddWindow(checkbox, 0, border=0, flag=0) laurent@703: if element_infos["value"] is not None: laurent@703: checkbox.SetValue(element_infos["value"]) Edouard@717: checkbox.Bind(wx.EVT_CHECKBOX, self.GetCheckBoxCallBackFunction(checkbox, confnode, element_path), id=id) laurent@428: elif element_infos["type"] in ["unsignedLong", "long","integer"]: laurent@428: if element_infos["type"].startswith("unsigned"): laurent@428: scmin = 0 laurent@428: else: laurent@428: scmin = -(2**31) laurent@428: scmax = 2**31-1 laurent@428: spinctrl = wx.SpinCtrl(id=id, name=element_infos["name"], parent=parent, laurent@428: pos=wx.Point(0, 0), size=wx.Size(300, 25), style=wx.SP_ARROW_KEYS|wx.ALIGN_RIGHT) laurent@428: spinctrl.SetRange(scmin, scmax) laurent@428: boxsizer.AddWindow(spinctrl, 0, border=0, flag=0) laurent@703: if element_infos["value"] is not None: laurent@703: spinctrl.SetValue(element_infos["value"]) Edouard@717: spinctrl.Bind(wx.EVT_SPINCTRL, self.GetTextCtrlCallBackFunction(spinctrl, confnode, element_path), id=id) laurent@428: else: laurent@675: choices = cPickle.loads(str(self.Config.Read(element_path, cPickle.dumps([""])))) laurent@428: textctrl = TextCtrlAutoComplete.TextCtrlAutoComplete(id=id, laurent@428: name=element_infos["name"], laurent@428: parent=parent, laurent@428: appframe=self, laurent@428: choices=choices, laurent@428: element_path=element_path, laurent@428: pos=wx.Point(0, 0), laurent@428: size=wx.Size(300, 25), laurent@428: style=0) laurent@428: laurent@428: boxsizer.AddWindow(textctrl, 0, border=0, flag=0) laurent@703: if element_infos["value"] is not None: laurent@703: textctrl.ChangeValue(str(element_infos["value"])) Edouard@717: textctrl.Bind(wx.EVT_TEXT, self.GetTextCtrlCallBackFunction(textctrl, confnode, element_path)) laurent@428: first = False laurent@428: laurent@428: def ResetView(self): laurent@428: IDEFrame.ResetView(self) Edouard@717: self.ConfNodeInfos = {} Edouard@717: if self.CTR is not None: Edouard@717: self.CTR.CloseProject() Edouard@717: self.CTR = None laurent@428: self.Log.flush() laurent@490: if self.EnableDebug: laurent@490: self.DebugVariablePanel.SetDataProducer(None) laurent@428: laurent@679: def RefreshConfigRecentProjects(self, projectpath): laurent@679: recent_projects = cPickle.loads(str(self.Config.Read("RecentProjects", cPickle.dumps([])))) laurent@679: if projectpath in recent_projects: laurent@679: recent_projects.remove(projectpath) laurent@679: recent_projects.insert(0, projectpath) laurent@679: self.Config.Write("RecentProjects", cPickle.dumps(recent_projects[:MAX_RECENT_PROJECTS])) laurent@679: self.Config.Flush() laurent@679: laurent@428: def OnNewProjectMenu(self, event): Edouard@717: if self.CTR is not None and not self.CheckSaveBeforeClosing(): laurent@429: return laurent@429: laurent@428: if not self.Config.HasEntry("lastopenedfolder"): laurent@428: defaultpath = os.path.expanduser("~") laurent@428: else: laurent@428: defaultpath = self.Config.Read("lastopenedfolder") laurent@428: laurent@428: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, wx.DD_NEW_DIR_BUTTON) laurent@428: if dialog.ShowModal() == wx.ID_OK: laurent@428: projectpath = dialog.GetPath() laurent@428: self.Config.Write("lastopenedfolder", os.path.dirname(projectpath)) laurent@428: self.Config.Flush() laurent@428: self.ResetView() Edouard@717: ctr = ConfigTreeRoot(self, self.Log) Edouard@717: result = ctr.NewProject(projectpath) laurent@428: if not result: Edouard@717: self.CTR = ctr Edouard@717: self.Controler = self.CTR laurent@716: self.LibraryPanel.SetControler(self.Controler) laurent@679: self.RefreshConfigRecentProjects(projectpath) laurent@490: if self.EnableDebug: Edouard@717: self.DebugVariablePanel.SetDataProducer(self.CTR) laurent@428: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@428: self.RefreshAll() laurent@428: else: laurent@428: self.ResetView() laurent@428: self.ShowErrorMessage(result) laurent@706: self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) laurent@679: dialog.Destroy() laurent@428: laurent@428: def OnOpenProjectMenu(self, event): Edouard@717: if self.CTR is not None and not self.CheckSaveBeforeClosing(): laurent@429: return laurent@429: laurent@428: if not self.Config.HasEntry("lastopenedfolder"): laurent@428: defaultpath = os.path.expanduser("~") laurent@428: else: laurent@428: defaultpath = self.Config.Read("lastopenedfolder") laurent@428: laurent@428: dialog = wx.DirDialog(self , _("Choose a project"), defaultpath, wx.DD_NEW_DIR_BUTTON) laurent@428: if dialog.ShowModal() == wx.ID_OK: laurent@675: self.OpenProject(dialog.GetPath()) laurent@675: dialog.Destroy() laurent@675: laurent@675: def OpenProject(self, projectpath): laurent@675: if os.path.isdir(projectpath): laurent@675: self.Config.Write("lastopenedfolder", os.path.dirname(projectpath)) laurent@675: self.Config.Flush() laurent@675: self.ResetView() Edouard@717: self.CTR = ConfigTreeRoot(self, self.Log) Edouard@717: self.Controler = self.CTR Edouard@717: result = self.CTR.LoadProject(projectpath) laurent@675: if not result: laurent@716: self.LibraryPanel.SetControler(self.Controler) laurent@679: self.RefreshConfigRecentProjects(projectpath) laurent@675: if self.EnableDebug: Edouard@717: self.DebugVariablePanel.SetDataProducer(self.CTR) laurent@715: self.LoadProjectOrganization() laurent@675: self._Refresh(TYPESTREE, INSTANCESTREE, LIBRARYTREE) laurent@675: self.RefreshAll() laurent@675: else: laurent@428: self.ResetView() laurent@675: self.ShowErrorMessage(result) laurent@675: else: laurent@675: self.ShowErrorMessage(_("\"%s\" folder is not a valid Beremiz project\n") % projectpath) laurent@706: self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) laurent@428: laurent@428: def OnCloseProjectMenu(self, event): Edouard@717: if self.CTR is not None and not self.CheckSaveBeforeClosing(): laurent@429: return laurent@429: laurent@715: self.SaveProjectOrganization() laurent@429: self.ResetView() laurent@706: self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) laurent@429: self.RefreshAll() laurent@428: laurent@428: def OnSaveProjectMenu(self, event): Edouard@717: if self.CTR is not None: Edouard@717: self.CTR.SaveProject() laurent@428: self.RefreshAll() laurent@636: self._Refresh(TITLE, FILEMENU, EDITMENU, PAGETITLES) laurent@428: laurent@428: def OnSaveProjectAsMenu(self, event): Edouard@717: if self.CTR is not None: Edouard@717: self.CTR.SaveProjectAs() laurent@428: self.RefreshAll() laurent@636: self._Refresh(TITLE, FILEMENU, EDITMENU, PAGETITLES) laurent@428: event.Skip() laurent@428: laurent@428: def OnPropertiesMenu(self, event): laurent@428: self.ShowProperties() laurent@428: laurent@428: def OnQuitMenu(self, event): laurent@428: self.Close() laurent@428: laurent@428: def OnBeremizMenu(self, event): laurent@428: open_pdf(Bpath( "doc", "manual_beremiz.pdf")) laurent@428: laurent@428: def OnAboutMenu(self, event): laurent@428: OpenHtmlFrame(self,_("About Beremiz"), Bpath("doc","about.html"), wx.Size(550, 500)) laurent@428: laurent@675: def OnPouSelectedChanged(self, event): Edouard@717: wx.CallAfter(self.RefreshConfNodeMenu) laurent@675: IDEFrame.OnPouSelectedChanged(self, event) laurent@675: laurent@675: def OnPageClose(self, event): Edouard@717: wx.CallAfter(self.RefreshConfNodeMenu) laurent@675: IDEFrame.OnPageClose(self, event) laurent@675: Edouard@717: def GetAddButtonFunction(self, confnode, window): laurent@428: def AddButtonFunction(event): Edouard@717: if confnode and len(confnode.PlugChildsTypes) > 0: Edouard@717: confnode_menu = wx.Menu(title='') Edouard@717: for name, XSDClass, help in confnode.PlugChildsTypes: laurent@428: new_id = wx.NewId() Edouard@717: confnode_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=name) Edouard@717: self.Bind(wx.EVT_MENU, self._GetAddConfNodeFunction(name, confnode), id=new_id) laurent@428: window_pos = window.GetPosition() Edouard@717: wx.CallAfter(self.PLCConfig.PopupMenu, confnode_menu) laurent@428: event.Skip() laurent@428: return AddButtonFunction laurent@428: Edouard@717: def GetDeleteButtonFunction(self, confnode): laurent@428: def DeleteButtonFunction(event): Edouard@717: wx.CallAfter(self.DeleteConfNode, confnode) laurent@428: event.Skip() laurent@428: return DeleteButtonFunction laurent@428: Edouard@717: def AddConfNode(self, ConfNodeType, confnode): Edouard@717: if self.CTR.CheckProjectPathPerm(): Edouard@717: dialog = wx.TextEntryDialog(self, _("Please enter a name for confnode:"), _("Add ConfNode"), "", wx.OK|wx.CANCEL) laurent@428: if dialog.ShowModal() == wx.ID_OK: Edouard@717: ConfNodeName = dialog.GetValue() Edouard@717: confnode.PlugAddChild(ConfNodeName, ConfNodeType) Edouard@717: self.CTR.RefreshConfNodesBlockLists() laurent@627: self._Refresh(TITLE, FILEMENU) Edouard@717: self.RefreshConfNodeTree() laurent@428: dialog.Destroy() laurent@428: Edouard@717: def DeleteConfNode(self, confnode): Edouard@717: if self.CTR.CheckProjectPathPerm(): Edouard@717: dialog = wx.MessageDialog(self, _("Really delete confnode ?"), _("Remove confnode"), wx.YES_NO|wx.NO_DEFAULT) laurent@428: if dialog.ShowModal() == wx.ID_YES: Edouard@717: self.ConfNodeInfos.pop(confnode) Edouard@717: confnode.PlugRemove() Edouard@717: del confnode Edouard@717: self.CTR.RefreshConfNodesBlockLists() laurent@627: self._Refresh(TITLE, FILEMENU) Edouard@717: self.RefreshConfNodeTree() laurent@428: dialog.Destroy() laurent@428: laurent@428: #------------------------------------------------------------------------------- laurent@428: # Exception Handler laurent@428: #------------------------------------------------------------------------------- laurent@428: laurent@428: Max_Traceback_List_Size = 20 laurent@428: laurent@428: def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path): laurent@428: trcbck_lst = [] laurent@428: for i,line in enumerate(traceback.extract_tb(e_tb)): laurent@428: trcbck = " " + str(i+1) + _(". ") laurent@428: if line[0].find(os.getcwd()) == -1: laurent@428: trcbck += _("file : ") + str(line[0]) + _(", ") laurent@428: else: laurent@428: trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") laurent@428: trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) laurent@428: trcbck_lst.append(trcbck) laurent@428: laurent@428: # Allow clicking.... laurent@428: cap = wx.Window_GetCapture() laurent@428: if cap: laurent@428: cap.ReleaseMouse() laurent@428: laurent@428: dlg = wx.SingleChoiceDialog(None, laurent@428: _(""" laurent@428: An unhandled exception (bug) occured. Bug report saved at : laurent@428: (%s) laurent@428: greg@434: Please be kind enough to send this file to: Edouard@618: dev@automforge.net laurent@428: laurent@428: You should now restart Beremiz. laurent@428: laurent@428: Traceback: laurent@428: """) % bug_report_path + laurent@428: str(e_type) + " : " + str(e_value), laurent@428: _("Error"), laurent@428: trcbck_lst) laurent@428: try: laurent@428: res = (dlg.ShowModal() == wx.ID_OK) laurent@428: finally: laurent@428: dlg.Destroy() laurent@428: laurent@428: return res laurent@428: laurent@428: def get_last_traceback(tb): laurent@428: while tb.tb_next: laurent@428: tb = tb.tb_next laurent@428: return tb laurent@428: laurent@428: laurent@428: def format_namespace(d, indent=' '): laurent@428: return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) laurent@428: laurent@428: laurent@428: ignored_exceptions = [] # a problem with a line in a module is only reported once per session laurent@428: laurent@428: def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): laurent@428: laurent@428: def handle_exception(e_type, e_value, e_traceback): laurent@428: 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@428: last_tb = get_last_traceback(e_traceback) laurent@428: ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) Edouard@701: if ex not in ignored_exceptions: laurent@428: date = time.ctime() laurent@428: bug_report_path = path+os.sep+"bug_report_"+date.replace(':','-').replace(' ','_')+".txt" laurent@428: result = Display_Exception_Dialog(e_type,e_value,e_traceback,bug_report_path) laurent@428: if result: laurent@428: ignored_exceptions.append(ex) laurent@428: info = { laurent@428: 'app-title' : wx.GetApp().GetAppName(), # app_title laurent@428: 'app-version' : app_version, laurent@428: 'wx-version' : wx.VERSION_STRING, laurent@428: 'wx-platform' : wx.Platform, laurent@428: 'python-version' : platform.python_version(), #sys.version.split()[0], laurent@428: 'platform' : platform.platform(), laurent@428: 'e-type' : e_type, laurent@428: 'e-value' : e_value, laurent@428: 'date' : date, laurent@428: 'cwd' : os.getcwd(), laurent@428: } laurent@428: if e_traceback: laurent@428: info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) laurent@428: last_tb = get_last_traceback(e_traceback) laurent@428: exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred laurent@428: info['locals'] = format_namespace(exception_locals) laurent@428: if 'self' in exception_locals: laurent@428: info['self'] = format_namespace(exception_locals['self'].__dict__) laurent@428: laurent@428: output = open(bug_report_path,'w') laurent@428: lst = info.keys() laurent@428: lst.sort() laurent@428: for a in lst: laurent@428: output.write(a+":\n"+str(info[a])+"\n\n") laurent@428: laurent@428: #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) laurent@428: sys.excepthook = handle_exception laurent@428: laurent@428: if __name__ == '__main__': laurent@428: # Install a exception handle for bug reports Edouard@588: AddExceptHook(os.getcwd(),updateinfo_url) laurent@428: laurent@428: frame = Beremiz(None, projectOpen, buildpath) Edouard@590: splash.Close() Edouard@713: #wx.Yield() laurent@428: frame.Show() laurent@428: app.MainLoop()