2 # Purpose: XRC editor, Panel class and related
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
7 from xxx
import * # xxx imports globals and params
9 from wxPython
.html
import wxHtmlWindow
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
)
17 wxNotebook
.__init
__(self
, parent
, id)
19 g
.panel
= panel
= self
22 # Set common button size for parameter buttons
23 bTmp
= wxButton(self
, -1, '')
25 params
.buttonSize
= (self
.DLG_SZE(buttonSize
)[0], bTmp
.GetSize()[1])
29 # List of child windows
31 # Create scrolled windows for pages
32 self
.page1
= wxScrolledWindow(self
, -1)
34 sizer
.Add(wxBoxSizer()) # dummy sizer
35 self
.page1
.SetAutoLayout(True)
36 self
.page1
.SetSizer(sizer
)
37 self
.AddPage(self
.page1
, 'Properties')
39 self
.page2
= wxScrolledWindow(self
, -1)
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
48 # Delete child windows and recreate page sizer
49 def ResetPage(self
, page
):
50 topSizer
= page
.GetSizer()
51 sizer
= topSizer
.GetChildren()[0].GetSizer()
52 for w
in page
.GetChildren():
54 if isinstance(w
, ParamPage
):
59 topSizer
.Remove(sizer
)
61 sizer
= wxBoxSizer(wxVERTICAL
)
62 # Special case - resize html window
64 topSizer
.Add(sizer
, 1, wxEXPAND
)
66 topSizer
.Add(sizer
, 0, wxALL
, 5)
69 def SetData(self
, xxx
):
72 # Remove current objects and sizer
73 sizer
= self
.ResetPage(self
.page1
)
74 if not xxx
or (not xxx
.allParams
and not xxx
.hasName
):
76 sizer
.Add(wxStaticText(self
.page1
, -1, 'This item has no properties.'))
77 else: # nothing selected
78 # If first time, show some help
80 html
= wxHtmlWindow(self
.page1
, -1, wxDefaultPosition
,
81 wxDefaultSize
, wxSUNKEN_BORDER
)
82 html
.SetPage(g
.helpText
)
83 sizer
.Add(html
, 1, wxEXPAND
)
86 sizer
.Add(wxStaticText(self
.page1
, -1, 'Select a tree item.'))
88 g
.currentXXX
= xxx
.treeObject()
89 # Normal or SizerItem page
90 isGBSizerItem
= isinstance(xxx
.parent
, xxxGridBagSizer
)
91 cacheID
= (xxx
.__class
__, isGBSizerItem
)
93 page
= self
.pageCache
[cacheID
]
94 page
.box
.SetLabel(xxx
.panelName())
97 page
= PropPage(self
.page1
, xxx
.panelName(), xxx
)
98 self
.pageCache
[cacheID
] = page
100 self
.pages
.append(page
)
101 sizer
.Add(page
, 1, wxEXPAND
)
103 # Special label for child objects - they may have different GUI
104 cacheID
= (xxx
.child
.__class
__, xxx
.__class
__)
106 page
= self
.pageCache
[cacheID
]
107 page
.box
.SetLabel(xxx
.child
.panelName())
110 page
= PropPage(self
.page1
, xxx
.child
.panelName(), xxx
.child
)
111 self
.pageCache
[cacheID
] = page
112 page
.SetValues(xxx
.child
)
113 self
.pages
.append(page
)
114 sizer
.Add(page
, 0, wxEXPAND | wxTOP
, 5)
116 size
= self
.page1
.GetSizer().GetMinSize()
117 self
.page1
.SetScrollbars(1, 1, size
.width
, size
.height
, 0, 0, True)
120 # Create if does not exist
121 if xxx
and xxx
.treeObject().hasStyle
:
122 xxx
= xxx
.treeObject()
123 # Simplest case: set data if class is the same
124 sizer
= self
.ResetPage(self
.page2
)
126 page
= self
.stylePageCache
[xxx
.__class
__]
129 page
= StylePage(self
.page2
, xxx
.className
+ ' style', xxx
)
130 self
.stylePageCache
[xxx
.__class
__] = page
132 self
.pages
.append(page
)
133 sizer
.Add(page
, 0, wxEXPAND
)
134 # Add page if not exists
135 if not self
.GetPageCount() == 2:
136 self
.AddPage(self
.page2
, 'Style')
138 size
= self
.page2
.GetSizer().GetMinSize()
139 self
.page2
.SetScrollbars(1, 1, size
.width
, size
.height
, 0, 0, True)
141 # Remove page if exists
142 if self
.GetPageCount() == 2:
146 self
.modified
= False
150 self
.modified
= False
152 # If some parameter has changed
153 def IsModified(self
):
156 def SetModified(self
, value
):
157 # Register undo object when modifying first time
158 if not self
.modified
and value
:
159 g
.undoMan
.RegisterUndo(UndoEdit())
160 self
.modified
= value
163 for p
in self
.pages
: p
.Apply()
165 ################################################################################
167 # General class for notebook pages
168 class ParamPage(wxPanel
):
169 def __init__(self
, parent
, xxx
):
170 wxPanel
.__init
__(self
, parent
, -1)
171 self
.SetBackgroundColour(parent
.GetBackgroundColour())
172 self
.SetForegroundColour(parent
.GetForegroundColour())
174 # Register event handlers
175 for id in paramIDs
.values():
176 EVT_CHECKBOX(self
, id, self
.OnCheckParams
)
178 self
.controls
= {} # save python objects
179 self
.controlName
= None
181 def OnCheckParams(self
, evt
):
183 param
= evt
.GetEventObject().GetName()
184 w
= self
.controls
[param
]
186 objElem
= xxx
.element
188 # Ad new text node in order of allParams
189 w
.SetValue('') # set empty (default) value
190 w
.SetModified() # mark as changed
191 elem
= g
.tree
.dom
.createElement(param
)
192 # Some classes are special
194 xxx
.params
[param
] = xxxParamFont(xxx
.element
, elem
)
195 elif param
in xxxObject
.bitmapTags
:
196 xxx
.params
[param
] = xxxParamBitmap(elem
)
198 xxx
.params
[param
] = xxxParam(elem
)
199 # Find place to put new element: first present element after param
201 paramStyles
= xxx
.allParams
+ xxx
.styles
202 for p
in paramStyles
[paramStyles
.index(param
) + 1:]:
203 # Content params don't have same type
204 if xxx
.params
.has_key(p
) and p
!= 'content':
208 nextTextElem
= xxx
.params
[p
].node
209 objElem
.insertBefore(elem
, nextTextElem
)
211 objElem
.appendChild(elem
)
214 xxx
.params
[param
].remove()
215 del xxx
.params
[param
]
217 w
.modified
= False # mark as not changed
219 # Set modified flag (provokes undo storing is necessary)
220 panel
.SetModified(True)
224 name
= self
.controlName
.GetValue()
227 xxx
.element
.setAttribute('name', name
)
228 for param
, w
in self
.controls
.items():
230 paramObj
= xxx
.params
[param
]
232 if param
in xxx
.specials
:
233 xxx
.setSpecial(param
, value
)
235 paramObj
.update(value
)
238 self
.origChecks
= map(lambda i
: (i
[0], i
[1].GetValue()), self
.checks
.items())
239 self
.origControls
= map(lambda i
: (i
[0], i
[1].GetValue(), i
[1].IsEnabled()),
240 self
.controls
.items())
242 self
.origName
= self
.controlName
.GetValue()
243 # Return original values
246 return (self
.origChecks
, self
.origControls
, self
.origName
)
248 return (self
.origChecks
, self
.origControls
)
249 # Set values from undo data
250 def SetState(self
, state
):
252 self
.checks
[k
].SetValue(v
)
253 for k
,v
,e
in state
[1]:
254 self
.controls
[k
].SetValue(v
)
255 self
.controls
[k
].Enable(e
)
256 if e
: self
.controls
[k
].modified
= True
258 self
.controlName
.SetValue(state
[2])
260 ################################################################################
264 # Panel for displaying properties
265 class PropPage(ParamPage
):
266 def __init__(self
, parent
, label
, xxx
):
267 ParamPage
.__init
__(self
, parent
, xxx
)
268 self
.box
= wxStaticBox(self
, -1, label
)
269 self
.box
.SetFont(g
.labelFont())
270 topSizer
= wxStaticBoxSizer(self
.box
, wxVERTICAL
)
271 sizer
= wxFlexGridSizer(len(xxx
.allParams
), 2, 1, 1)
272 sizer
.AddGrowableCol(1)
274 label
= wxStaticText(self
, -1, 'XML ID:', size
=(LABEL_WIDTH
,-1))
275 control
= ParamText(self
, 'XML_name', 200)
276 sizer
.AddMany([ (label
, 0, wxALIGN_CENTER_VERTICAL
),
277 (control
, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM | wxGROW
, 5) ])
278 self
.controlName
= control
279 for param
in xxx
.allParams
:
280 present
= xxx
.params
.has_key(param
)
281 if param
in xxx
.required
:
282 label
= wxStaticText(self
, paramIDs
[param
], param
+ ':',
283 size
= (LABEL_WIDTH
,-1), name
= param
)
285 # Notebook has one very loooooong parameter
286 if param
== 'usenotebooksizer': sParam
= 'usesizer:'
287 else: sParam
= param
+ ':'
288 label
= wxCheckBox(self
, paramIDs
[param
], sParam
,
289 size
= (LABEL_WIDTH
,-1), name
= param
)
290 self
.checks
[param
] = label
292 typeClass
= xxx
.paramDict
[param
]
296 typeClass
= paramDict
[param
]
299 typeClass
= ParamText
300 control
= typeClass(self
, param
)
301 control
.Enable(present
)
302 sizer
.AddMany([ (label
, 0, wxALIGN_CENTER_VERTICAL
),
303 (control
, 0, wxALIGN_CENTER_VERTICAL | wxGROW
) ])
304 self
.controls
[param
] = control
305 topSizer
.Add(sizer
, 1, wxALL | wxEXPAND
, 3)
306 self
.SetAutoLayout(True)
307 self
.SetSizer(topSizer
)
309 def SetValues(self
, xxx
):
312 self
.origControls
= []
313 # Set values, checkboxes to False, disable defaults
315 self
.controlName
.SetValue(xxx
.name
)
316 self
.origName
= xxx
.name
317 for param
in xxx
.allParams
:
318 w
= self
.controls
[param
]
321 value
= xxx
.params
[param
].value()
324 if not param
in xxx
.required
:
325 self
.checks
[param
].SetValue(True)
326 self
.origChecks
.append((param
, True))
327 self
.origControls
.append((param
, value
, True))
329 self
.checks
[param
].SetValue(False)
332 self
.origChecks
.append((param
, False))
333 self
.origControls
.append((param
, '', False))
335 ################################################################################
337 # Style notebook page
338 class StylePage(ParamPage
):
339 def __init__(self
, parent
, label
, xxx
):
340 ParamPage
.__init
__(self
, parent
, xxx
)
341 box
= wxStaticBox(self
, -1, label
)
342 box
.SetFont(g
.labelFont())
343 topSizer
= wxStaticBoxSizer(box
, wxVERTICAL
)
344 sizer
= wxFlexGridSizer(len(xxx
.styles
), 2, 1, 1)
345 sizer
.AddGrowableCol(1)
346 for param
in xxx
.styles
:
347 present
= xxx
.params
.has_key(param
)
348 check
= wxCheckBox(self
, paramIDs
[param
],
349 param
+ ':', size
= (LABEL_WIDTH
,-1), name
= param
)
350 check
.SetValue(present
)
351 control
= paramDict
[param
](self
, name
= param
)
352 control
.Enable(present
)
353 sizer
.AddMany([ (check
, 0, wxALIGN_CENTER_VERTICAL
),
354 (control
, 0, wxALIGN_CENTER_VERTICAL | wxGROW
) ])
355 self
.checks
[param
] = check
356 self
.controls
[param
] = control
357 topSizer
.Add(sizer
, 1, wxALL | wxEXPAND
, 3)
358 self
.SetAutoLayout(True)
359 self
.SetSizer(topSizer
)
361 # Set data for a cahced page
362 def SetValues(self
, xxx
):
365 self
.origControls
= []
366 for param
in xxx
.styles
:
367 present
= xxx
.params
.has_key(param
)
368 check
= self
.checks
[param
]
369 check
.SetValue(present
)
370 w
= self
.controls
[param
]
373 value
= xxx
.params
[param
].value()
378 self
.origChecks
.append((param
, present
))
379 self
.origControls
.append((param
, value
, present
))