|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 #This file is part of Beremiz, a Integrated Development Environment for |
|
5 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 # |
|
7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 # |
|
9 #See COPYING file for copyrights details. |
|
10 # |
|
11 #This library is free software; you can redistribute it and/or |
|
12 #modify it under the terms of the GNU General Public |
|
13 #License as published by the Free Software Foundation; either |
|
14 #version 2.1 of the License, or (at your option) any later version. |
|
15 # |
|
16 #This library is distributed in the hope that it will be useful, |
|
17 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 #General Public License for more details. |
|
20 # |
|
21 #You should have received a copy of the GNU General Public |
|
22 #License along with this library; if not, write to the Free Software |
|
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
|
25 import wx |
|
26 |
|
27 [ID_BROWSEVALUESLIBRARYDIALOG, ID_BROWSEVALUESLIBRARYDIALOGSTATICTEXT1, |
|
28 ID_BROWSEVALUESLIBRARYDIALOGVALUESLIBRARY |
|
29 ] = [wx.NewId() for _init_ctrls in range(3)] |
|
30 |
|
31 class BrowseValuesLibraryDialog(wx.Dialog): |
|
32 |
|
33 if wx.VERSION < (2, 6, 0): |
|
34 def Bind(self, event, function, id = None): |
|
35 if id is not None: |
|
36 event(self, id, function) |
|
37 else: |
|
38 event(self, function) |
|
39 |
|
40 def _init_coll_flexGridSizer1_Items(self, parent): |
|
41 parent.AddWindow(self.staticText1, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
42 parent.AddWindow(self.ValuesLibrary, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT) |
|
43 parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
44 |
|
45 def _init_coll_flexGridSizer1_Growables(self, parent): |
|
46 parent.AddGrowableCol(0) |
|
47 parent.AddGrowableRow(1) |
|
48 |
|
49 def _init_sizers(self): |
|
50 self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
51 |
|
52 self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) |
|
53 self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) |
|
54 |
|
55 self.SetSizer(self.flexGridSizer1) |
|
56 |
|
57 def _init_ctrls(self, prnt, name): |
|
58 wx.Dialog.__init__(self, id=ID_BROWSEVALUESLIBRARYDIALOG, |
|
59 name='BrowseValueDialog', parent=prnt, |
|
60 size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, |
|
61 title=_('Browse %s library') % name) |
|
62 self.SetClientSize(wx.Size(600, 400)) |
|
63 |
|
64 self.staticText1 = wx.StaticText(id=ID_BROWSEVALUESLIBRARYDIALOGSTATICTEXT1, |
|
65 label=_('Choose a %s:') % name, name='staticText1', parent=self, |
|
66 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
67 |
|
68 self.ValuesLibrary = wx.TreeCtrl(id=ID_BROWSEVALUESLIBRARYDIALOGVALUESLIBRARY, |
|
69 name='ValuesLibrary', parent=self, pos=wx.Point(0, 0), |
|
70 size=wx.Size(0, 0), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT) |
|
71 |
|
72 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
73 if wx.VERSION >= (2, 5, 0): |
|
74 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) |
|
75 else: |
|
76 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
77 |
|
78 self._init_sizers() |
|
79 |
|
80 def __init__(self, parent, name, library, default=None): |
|
81 self._init_ctrls(parent, name) |
|
82 |
|
83 root = self.ValuesLibrary.AddRoot("") |
|
84 self.GenerateValuesLibraryBranch(root, library, default) |
|
85 |
|
86 def GenerateValuesLibraryBranch(self, root, children, default): |
|
87 for infos in children: |
|
88 item = self.ValuesLibrary.AppendItem(root, infos["name"]) |
|
89 self.ValuesLibrary.SetPyData(item, infos["infos"]) |
|
90 if infos["infos"] is not None and infos["infos"] == default: |
|
91 self.ValuesLibrary.SelectItem(item) |
|
92 self.ValuesLibrary.EnsureVisible(item) |
|
93 self.GenerateValuesLibraryBranch(item, infos["children"], default) |
|
94 |
|
95 def GetValueInfos(self): |
|
96 selected = self.ValuesLibrary.GetSelection() |
|
97 return self.ValuesLibrary.GetPyData(selected) |
|
98 |
|
99 def OnOK(self, event): |
|
100 selected = self.ValuesLibrary.GetSelection() |
|
101 if not selected.IsOk() or self.ValuesLibrary.GetPyData(selected) is None: |
|
102 message = wx.MessageDialog(self, _("No valid value selected!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
103 message.ShowModal() |
|
104 message.Destroy() |
|
105 else: |
|
106 self.EndModal(wx.ID_OK) |
|
107 |