]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | # Name: panel.py |
2 | # Purpose: XRC editor, Panel class and related | |
3 | # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be> | |
4 | # Created: 02.12.2002 | |
5 | # RCS-ID: $Id$ | |
1fded56b | 6 | |
d14a1e28 RD |
7 | from xxx import * # xxx imports globals and params |
8 | from undo import * | |
9 | from wxPython.html import wxHtmlWindow | |
10 | ||
11 | # Properties panel containing notebook | |
12 | class Panel(wxNotebook): | |
13 | def __init__(self, parent, id = -1): | |
14 | if wxPlatform != '__WXMAC__': # some problems with this style on macs | |
15 | wxNotebook.__init__(self, parent, id, style=wxNB_BOTTOM) | |
16 | else: | |
17 | wxNotebook.__init__(self, parent, id) | |
18 | global panel | |
19 | g.panel = panel = self | |
20 | self.modified = False | |
21 | ||
22 | # Set common button size for parameter buttons | |
23 | bTmp = wxButton(self, -1, '') | |
24 | import params | |
25 | params.buttonSize = (self.DLG_SZE(buttonSize)[0], bTmp.GetSize()[1]) | |
26 | bTmp.Destroy() | |
27 | del bTmp | |
28 | ||
29 | # List of child windows | |
30 | self.pages = [] | |
31 | # Create scrolled windows for pages | |
32 | self.page1 = wxScrolledWindow(self, -1) | |
33 | sizer = wxBoxSizer() | |
34 | sizer.Add(wxBoxSizer()) # dummy sizer | |
35 | self.page1.SetAutoLayout(True) | |
36 | self.page1.SetSizer(sizer) | |
37 | self.AddPage(self.page1, 'Properties') | |
38 | # Second page | |
39 | self.page2 = wxScrolledWindow(self, -1) | |
40 | sizer = wxBoxSizer() | |
41 | sizer.Add(wxBoxSizer()) # dummy sizer | |
42 | self.page2.SetAutoLayout(True) | |
43 | self.page2.SetSizer(sizer) | |
a78714eb | 44 | |
d14a1e28 RD |
45 | # Delete child windows and recreate page sizer |
46 | def ResetPage(self, page): | |
47 | topSizer = page.GetSizer() | |
48 | sizer = topSizer.GetChildren()[0].GetSizer() | |
49 | for w in page.GetChildren(): | |
a78714eb RD |
50 | w.Destroy() |
51 | ||
68ae5821 | 52 | topSizer.Remove(sizer) |
d14a1e28 RD |
53 | # Create new windows |
54 | sizer = wxBoxSizer(wxVERTICAL) | |
55 | # Special case - resize html window | |
56 | if g.conf.panic: | |
57 | topSizer.Add(sizer, 1, wxEXPAND) | |
58 | else: | |
59 | topSizer.Add(sizer, 0, wxALL, 5) | |
60 | return sizer | |
a78714eb | 61 | |
d14a1e28 RD |
62 | def SetData(self, xxx): |
63 | self.pages = [] | |
64 | # First page | |
d14a1e28 RD |
65 | # Remove current objects and sizer |
66 | sizer = self.ResetPage(self.page1) | |
67 | if not xxx or (not xxx.allParams and not xxx.hasName): | |
68 | if g.tree.selection: | |
69 | sizer.Add(wxStaticText(self.page1, -1, 'This item has no properties.')) | |
70 | else: # nothing selected | |
71 | # If first time, show some help | |
72 | if g.conf.panic: | |
73 | html = wxHtmlWindow(self.page1, -1, wxDefaultPosition, | |
74 | wxDefaultSize, wxSUNKEN_BORDER) | |
75 | html.SetPage(g.helpText) | |
76 | sizer.Add(html, 1, wxEXPAND) | |
77 | g.conf.panic = False | |
78 | else: | |
79 | sizer.Add(wxStaticText(self.page1, -1, 'Select a tree item.')) | |
80 | else: | |
81 | g.currentXXX = xxx.treeObject() | |
a4c013b2 RR |
82 | # Normal or SizerItem page |
83 | isGBSizerItem = isinstance(xxx.parent, xxxGridBagSizer) | |
a78714eb | 84 | page = PropPage(self.page1, xxx.panelName(), xxx) |
d14a1e28 RD |
85 | page.SetValues(xxx) |
86 | self.pages.append(page) | |
87 | sizer.Add(page, 1, wxEXPAND) | |
88 | if xxx.hasChild: | |
89 | # Special label for child objects - they may have different GUI | |
a78714eb | 90 | page = PropPage(self.page1, xxx.child.panelName(), xxx.child) |
d14a1e28 RD |
91 | page.SetValues(xxx.child) |
92 | self.pages.append(page) | |
93 | sizer.Add(page, 0, wxEXPAND | wxTOP, 5) | |
94 | self.page1.Layout() | |
95 | size = self.page1.GetSizer().GetMinSize() | |
96 | self.page1.SetScrollbars(1, 1, size.width, size.height, 0, 0, True) | |
97 | ||
98 | # Second page | |
99 | # Create if does not exist | |
100 | if xxx and xxx.treeObject().hasStyle: | |
101 | xxx = xxx.treeObject() | |
102 | # Simplest case: set data if class is the same | |
103 | sizer = self.ResetPage(self.page2) | |
a78714eb | 104 | page = StylePage(self.page2, xxx.className + ' style', xxx) |
d14a1e28 RD |
105 | page.SetValues(xxx) |
106 | self.pages.append(page) | |
107 | sizer.Add(page, 0, wxEXPAND) | |
108 | # Add page if not exists | |
109 | if not self.GetPageCount() == 2: | |
110 | self.AddPage(self.page2, 'Style') | |
111 | self.page2.Layout() | |
112 | size = self.page2.GetSizer().GetMinSize() | |
113 | self.page2.SetScrollbars(1, 1, size.width, size.height, 0, 0, True) | |
114 | else: | |
115 | # Remove page if exists | |
116 | if self.GetPageCount() == 2: | |
117 | self.SetSelection(0) | |
118 | self.page1.Refresh() | |
119 | self.RemovePage(1) | |
120 | self.modified = False | |
121 | def Clear(self): | |
122 | self.SetData(None) | |
123 | self.modified = False | |
124 | # If some parameter has changed | |
125 | def IsModified(self): | |
126 | return self.modified | |
127 | def SetModified(self, value): | |
128 | # Register undo object when modifying first time | |
129 | if not self.modified and value: | |
130 | g.undoMan.RegisterUndo(UndoEdit()) | |
131 | self.modified = value | |
132 | def Apply(self): | |
133 | for p in self.pages: p.Apply() | |
134 | ||
135 | ################################################################################ | |
136 | ||
137 | # General class for notebook pages | |
138 | class ParamPage(wxPanel): | |
139 | def __init__(self, parent, xxx): | |
140 | wxPanel.__init__(self, parent, -1) | |
b09afc93 RD |
141 | self.SetBackgroundColour(parent.GetBackgroundColour()) |
142 | self.SetForegroundColour(parent.GetForegroundColour()) | |
d14a1e28 RD |
143 | self.xxx = xxx |
144 | # Register event handlers | |
145 | for id in paramIDs.values(): | |
146 | EVT_CHECKBOX(self, id, self.OnCheckParams) | |
147 | self.checks = {} | |
148 | self.controls = {} # save python objects | |
149 | self.controlName = None | |
150 | ||
151 | def OnCheckParams(self, evt): | |
152 | xxx = self.xxx | |
153 | param = evt.GetEventObject().GetName() | |
154 | w = self.controls[param] | |
155 | w.Enable(True) | |
156 | objElem = xxx.element | |
157 | if evt.IsChecked(): | |
158 | # Ad new text node in order of allParams | |
159 | w.SetValue('') # set empty (default) value | |
160 | w.SetModified() # mark as changed | |
161 | elem = g.tree.dom.createElement(param) | |
162 | # Some classes are special | |
163 | if param == 'font': | |
164 | xxx.params[param] = xxxParamFont(xxx.element, elem) | |
165 | elif param in xxxObject.bitmapTags: | |
166 | xxx.params[param] = xxxParamBitmap(elem) | |
167 | else: | |
168 | xxx.params[param] = xxxParam(elem) | |
169 | # Find place to put new element: first present element after param | |
170 | found = False | |
171 | paramStyles = xxx.allParams + xxx.styles | |
172 | for p in paramStyles[paramStyles.index(param) + 1:]: | |
173 | # Content params don't have same type | |
174 | if xxx.params.has_key(p) and p != 'content': | |
175 | found = True | |
176 | break | |
177 | if found: | |
178 | nextTextElem = xxx.params[p].node | |
179 | objElem.insertBefore(elem, nextTextElem) | |
180 | else: | |
181 | objElem.appendChild(elem) | |
182 | else: | |
183 | # Remove parameter | |
184 | xxx.params[param].remove() | |
185 | del xxx.params[param] | |
186 | w.SetValue('') | |
187 | w.modified = False # mark as not changed | |
188 | w.Enable(False) | |
189 | # Set modified flag (provokes undo storing is necessary) | |
190 | panel.SetModified(True) | |
191 | def Apply(self): | |
192 | xxx = self.xxx | |
193 | if self.controlName: | |
194 | name = self.controlName.GetValue() | |
195 | if xxx.name != name: | |
196 | xxx.name = name | |
197 | xxx.element.setAttribute('name', name) | |
198 | for param, w in self.controls.items(): | |
199 | if w.modified: | |
200 | paramObj = xxx.params[param] | |
201 | value = w.GetValue() | |
202 | if param in xxx.specials: | |
203 | xxx.setSpecial(param, value) | |
204 | else: | |
205 | paramObj.update(value) | |
206 | # Save current state | |
207 | def SaveState(self): | |
208 | self.origChecks = map(lambda i: (i[0], i[1].GetValue()), self.checks.items()) | |
209 | self.origControls = map(lambda i: (i[0], i[1].GetValue(), i[1].IsEnabled()), | |
210 | self.controls.items()) | |
211 | if self.controlName: | |
212 | self.origName = self.controlName.GetValue() | |
213 | # Return original values | |
214 | def GetState(self): | |
215 | if self.controlName: | |
216 | return (self.origChecks, self.origControls, self.origName) | |
217 | else: | |
218 | return (self.origChecks, self.origControls) | |
219 | # Set values from undo data | |
220 | def SetState(self, state): | |
221 | for k,v in state[0]: | |
222 | self.checks[k].SetValue(v) | |
223 | for k,v,e in state[1]: | |
224 | self.controls[k].SetValue(v) | |
225 | self.controls[k].Enable(e) | |
226 | if e: self.controls[k].modified = True | |
227 | if self.controlName: | |
228 | self.controlName.SetValue(state[2]) | |
229 | ||
230 | ################################################################################ | |
231 | ||
a78714eb RD |
232 | LABEL_WIDTH = 125 |
233 | ||
d14a1e28 RD |
234 | # Panel for displaying properties |
235 | class PropPage(ParamPage): | |
236 | def __init__(self, parent, label, xxx): | |
237 | ParamPage.__init__(self, parent, xxx) | |
2481bf3c | 238 | self.box = wxStaticBox(self, -1, label) |
289128a4 | 239 | self.box.SetFont(g.labelFont()) |
2481bf3c | 240 | topSizer = wxStaticBoxSizer(self.box, wxVERTICAL) |
d14a1e28 RD |
241 | sizer = wxFlexGridSizer(len(xxx.allParams), 2, 1, 1) |
242 | sizer.AddGrowableCol(1) | |
243 | if xxx.hasName: | |
a78714eb | 244 | label = wxStaticText(self, -1, 'XML ID:', size=(LABEL_WIDTH,-1)) |
d14a1e28 RD |
245 | control = ParamText(self, 'XML_name', 200) |
246 | sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL), | |
247 | (control, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM | wxGROW, 5) ]) | |
248 | self.controlName = control | |
249 | for param in xxx.allParams: | |
250 | present = xxx.params.has_key(param) | |
251 | if param in xxx.required: | |
252 | label = wxStaticText(self, paramIDs[param], param + ':', | |
a78714eb | 253 | size = (LABEL_WIDTH,-1), name = param) |
d14a1e28 RD |
254 | else: |
255 | # Notebook has one very loooooong parameter | |
256 | if param == 'usenotebooksizer': sParam = 'usesizer:' | |
257 | else: sParam = param + ':' | |
258 | label = wxCheckBox(self, paramIDs[param], sParam, | |
a78714eb | 259 | size = (LABEL_WIDTH,-1), name = param) |
d14a1e28 RD |
260 | self.checks[param] = label |
261 | try: | |
262 | typeClass = xxx.paramDict[param] | |
263 | except KeyError: | |
264 | try: | |
265 | # Standart type | |
266 | typeClass = paramDict[param] | |
267 | except KeyError: | |
268 | # Default | |
269 | typeClass = ParamText | |
270 | control = typeClass(self, param) | |
271 | control.Enable(present) | |
272 | sizer.AddMany([ (label, 0, wxALIGN_CENTER_VERTICAL), | |
273 | (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ]) | |
274 | self.controls[param] = control | |
275 | topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3) | |
276 | self.SetAutoLayout(True) | |
277 | self.SetSizer(topSizer) | |
278 | topSizer.Fit(self) | |
279 | def SetValues(self, xxx): | |
280 | self.xxx = xxx | |
281 | self.origChecks = [] | |
282 | self.origControls = [] | |
283 | # Set values, checkboxes to False, disable defaults | |
284 | if xxx.hasName: | |
285 | self.controlName.SetValue(xxx.name) | |
286 | self.origName = xxx.name | |
287 | for param in xxx.allParams: | |
288 | w = self.controls[param] | |
289 | w.modified = False | |
290 | try: | |
291 | value = xxx.params[param].value() | |
292 | w.Enable(True) | |
293 | w.SetValue(value) | |
294 | if not param in xxx.required: | |
295 | self.checks[param].SetValue(True) | |
296 | self.origChecks.append((param, True)) | |
297 | self.origControls.append((param, value, True)) | |
298 | except KeyError: | |
299 | self.checks[param].SetValue(False) | |
300 | w.SetValue('') | |
301 | w.Enable(False) | |
302 | self.origChecks.append((param, False)) | |
303 | self.origControls.append((param, '', False)) | |
304 | ||
305 | ################################################################################ | |
306 | ||
307 | # Style notebook page | |
308 | class StylePage(ParamPage): | |
309 | def __init__(self, parent, label, xxx): | |
310 | ParamPage.__init__(self, parent, xxx) | |
311 | box = wxStaticBox(self, -1, label) | |
289128a4 | 312 | box.SetFont(g.labelFont()) |
d14a1e28 RD |
313 | topSizer = wxStaticBoxSizer(box, wxVERTICAL) |
314 | sizer = wxFlexGridSizer(len(xxx.styles), 2, 1, 1) | |
315 | sizer.AddGrowableCol(1) | |
316 | for param in xxx.styles: | |
317 | present = xxx.params.has_key(param) | |
318 | check = wxCheckBox(self, paramIDs[param], | |
a78714eb | 319 | param + ':', size = (LABEL_WIDTH,-1), name = param) |
d14a1e28 RD |
320 | check.SetValue(present) |
321 | control = paramDict[param](self, name = param) | |
322 | control.Enable(present) | |
323 | sizer.AddMany([ (check, 0, wxALIGN_CENTER_VERTICAL), | |
324 | (control, 0, wxALIGN_CENTER_VERTICAL | wxGROW) ]) | |
325 | self.checks[param] = check | |
326 | self.controls[param] = control | |
327 | topSizer.Add(sizer, 1, wxALL | wxEXPAND, 3) | |
328 | self.SetAutoLayout(True) | |
329 | self.SetSizer(topSizer) | |
330 | topSizer.Fit(self) | |
331 | # Set data for a cahced page | |
332 | def SetValues(self, xxx): | |
333 | self.xxx = xxx | |
334 | self.origChecks = [] | |
335 | self.origControls = [] | |
336 | for param in xxx.styles: | |
337 | present = xxx.params.has_key(param) | |
338 | check = self.checks[param] | |
339 | check.SetValue(present) | |
340 | w = self.controls[param] | |
341 | w.modified = False | |
342 | if present: | |
343 | value = xxx.params[param].value() | |
344 | else: | |
345 | value = '' | |
346 | w.SetValue(value) | |
347 | w.Enable(present) | |
348 | self.origChecks.append((param, present)) | |
349 | self.origControls.append((param, value, present)) | |
1fded56b | 350 |