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