1 # Copyright (C) 2010 Massimo Sandal <devicerandom@gmail.com>
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 # Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke. If not, see
18 # <http://www.gnu.org/licenses/>.
20 """Property editor panel for Hooke.
27 import wx.propgrid as wxpg
29 # There are many comments and code fragments in here from the demo app.
30 # They should come in handy to expand the functionality in the future.
32 class Display (object):
33 property_descriptor = []
37 class ValueObject (object):
42 class IntProperty2 (wxpg.PyProperty):
43 """This is a simple re-implementation of wxIntProperty.
45 def __init__(self, label, name = wxpg.LABEL_AS_NAME, value=0):
46 wxpg.PyProperty.__init__(self, label, name)
49 def GetClassName(self):
55 def GetValueAsString(self, flags):
56 return str(self.GetValue())
58 def PyStringToValue(self, s, flags):
61 if self.GetValue() != v:
64 if flags & wxpg.PG_REPORT_ERROR:
65 wx.MessageBox("Cannot convert '%s' into a number."%s, "Error")
68 def PyIntToValue(self, v, flags):
69 if (self.GetValue() != v):
73 class PyFilesProperty(wxpg.PyArrayStringProperty):
74 def __init__(self, label, name = wxpg.LABEL_AS_NAME, value=[]):
75 wxpg.PyArrayStringProperty.__init__(self, label, name, value)
78 def OnSetValue(self, v):
80 self.display = ', '.join(self.value)
82 def GetValueAsString(self, argFlags):
85 def PyStringToValue(self, s, flags):
86 return [a.strip() for a in s.split(',')]
88 def OnEvent(self, propgrid, ctrl, event):
89 if event.GetEventType() == wx.wxEVT_COMMAND_BUTTON_CLICKED:
90 # Show dialog to select a string, call DoSetValue and
91 # return True, if value changed.
97 class PyObjectPropertyValue:
99 Value type of our sample PyObjectProperty. We keep a simple dash-delimited
100 list of string given as argument to constructor.
102 def __init__(self, s=None):
104 self.ls = [a.strip() for a in s.split('-')]
109 return ' - '.join(self.ls)
112 class PyObjectProperty(wxpg.PyProperty):
114 Another simple example. This time our value is a PyObject (NOTE: we can't
115 return an arbitrary python object in DoGetValue. It cannot be a simple
116 type such as int, bool, double, or string, nor an array or wxObject based.
117 Dictionary, None, or any user-specified Python object is allowed).
119 def __init__(self, label, name = wxpg.LABEL_AS_NAME, value=None):
120 wxpg.PyProperty.__init__(self, label, name)
123 def GetClassName(self):
124 return self.__class__.__name__
129 def GetValueAsString(self, flags):
130 return repr(self.GetValue())
132 def PyStringToValue(self, s, flags):
133 return PyObjectPropertyValue(s)
136 class ShapeProperty(wxpg.PyEnumProperty):
138 Demonstrates use of OnCustomPaint method.
140 def __init__(self, label, name = wxpg.LABEL_AS_NAME, value=-1):
141 wxpg.PyEnumProperty.__init__(self, label, name, ['Line','Circle','Rectangle'], [0,1,2], value)
143 def OnMeasureImage(self, index):
144 return wxpg.DEFAULT_IMAGE_SIZE
146 def OnCustomPaint(self, dc, rect, paint_data):
148 paint_data.m_choiceItem is -1 if we are painting the control,
149 in which case we need to get the drawn item using DoGetValue.
151 item = paint_data.m_choiceItem
153 item = self.DoGetValue()
155 dc.SetPen(wx.Pen(wx.BLACK))
156 dc.SetBrush(wx.Brush(wx.BLACK))
159 dc.DrawLine(rect.x,rect.y,rect.x+rect.width,rect.y+rect.height)
161 half_width = rect.width / 2
162 dc.DrawCircle(rect.x+half_width,rect.y+half_width,half_width-3)
164 dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
167 class LargeImagePickerCtrl(wx.Window):
169 Control created and used by LargeImageEditor.
175 def Create(self, parent, id_, pos, size, style = 0):
176 wx.Window.Create(self, parent, id_, pos, size, style | wx.BORDER_SIMPLE)
178 self.tc = wx.TextCtrl(self, -1, "", (img_spc,0), (2048,size[1]), wx.BORDER_NONE)
179 self.SetBackgroundColour(wx.WHITE)
180 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
183 self.Bind(wx.EVT_PAINT, self.OnPaint)
185 def OnPaint(self, event):
186 dc = wx.BufferedPaintDC(self)
188 whiteBrush = wx.Brush(wx.WHITE)
189 dc.SetBackground(whiteBrush)
194 dc.DrawBitmap(bmp, 2, 2)
196 dc.SetPen(wx.Pen(wx.BLACK))
197 dc.SetBrush(whiteBrush)
198 dc.DrawRectangle(2, 2, 64, 64)
200 def RefreshThumbnail(self):
202 We use here very simple image scaling code.
204 if not self.property:
208 path = self.property.DoGetValue()
210 if not os.path.isfile(path):
214 image = wx.Image(path)
215 image.Rescale(64, 64)
216 self.bmp = wx.BitmapFromImage(image)
218 def SetProperty(self, property):
219 self.property = property
220 self.tc.SetValue(property.GetDisplayedString())
221 self.RefreshThumbnail()
223 def SetValue(self, s):
224 self.RefreshThumbnail()
227 def GetLastPosition(self):
228 return self.tc.GetLastPosition()
231 class LargeImageEditor(wxpg.PyEditor):
233 Double-height text-editor with image in front.
236 wxpg.PyEditor.__init__(self)
238 def CreateControls(self, propgrid, property, pos, sz):
241 x = propgrid.GetSplitterPosition()
242 x2 = propgrid.GetClientSize().x
243 bw = propgrid.GetRowHeight()
244 lipc = LargeImagePickerCtrl()
245 if sys.platform == 'win32':
247 lipc.Create(propgrid, wxpg.PG_SUBID1, (x,pos[1]), (x2-x-bw,h))
248 lipc.SetProperty(property)
249 # Hmmm.. how to have two-stage creation without subclassing?
250 #btn = wx.PreButton()
251 #pre = wx.PreWindow()
252 #self.PostCreate(pre)
253 #if sys.platform == 'win32':
255 #btn.Create(propgrid, wxpg.PG_SUBID2, '...', (x2-bw,pos[1]), (bw,h), wx.WANTS_CHARS)
256 btn = wx.Button(propgrid, wxpg.PG_SUBID2, '...', (x2-bw,pos[1]), (bw,h), wx.WANTS_CHARS)
260 print traceback.print_exc()
262 def UpdateControl(self, property, ctrl):
263 ctrl.SetValue(property.GetDisplayedString())
265 def DrawValue(self, dc, property, rect):
266 if not (property.GetFlags() & wxpg.PG_PROP_AUTO_UNSPECIFIED):
267 dc.DrawText( property.GetDisplayedString(), rect.x+5, rect.y );
269 def OnEvent(self, propgrid, ctrl, event):
273 evtType = event.GetEventType()
275 if evtType == wx.wxEVT_COMMAND_TEXT_ENTER:
276 if propgrid.IsEditorsValueModified():
279 elif evtType == wx.wxEVT_COMMAND_TEXT_UPDATED:
280 if not property.HasFlag(wxpg.PG_PROP_AUTO_UNSPECIFIED) or not ctrl or \
281 ctrl.GetLastPosition() > 0:
283 # We must check this since an 'empty' text event
284 # may be triggered when creating the property.
285 PG_FL_IN_SELECT_PROPERTY = 0x00100000
286 if not (propgrid.GetInternalFlags() & PG_FL_IN_SELECT_PROPERTY):
288 event.SetId(propgrid.GetId());
290 propgrid.EditorsValueWasModified();
295 def CopyValueFromControl(self, property, ctrl):
297 res = property.SetValueFromString(tc.GetValue(),0)
298 # Changing unspecified always causes event (returning
299 # true here should be enough to trigger it).
300 if not res and property.IsFlagSet(wxpg.PG_PROP_AUTO_UNSPECIFIED):
305 def SetValueToUnspecified(self, ctrl):
306 ctrl.tc.Remove(0,len(ctrl.tc.GetValue()));
308 def SetControlStringValue(self, ctrl, txt):
311 def OnFocus(self, property, ctrl):
312 ctrl.tc.SetSelection(-1,-1)
316 class PropertyEditor(wx.Panel):
318 def __init__(self, parent):
319 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
320 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS, size=(160, 200))
322 sizer = wx.BoxSizer(wx.VERTICAL)
324 self.pg = wxpg.PropertyGrid(self, style=wxpg.PG_SPLITTER_AUTO_CENTER|wxpg.PG_AUTO_SORT)
326 # Show help as tooltips
327 self.pg.SetExtraStyle(wxpg.PG_EX_HELP_AS_TOOLTIPS)
329 #pg.Bind(wxpg.EVT_PG_CHANGED, self.OnPropGridChange)
330 #pg.Bind(wxpg.EVT_PG_SELECTED, self.OnPropGridSelect)
331 #self.pg.Bind(wxpg.EVT_PG_RIGHT_CLICK, self.OnPropGridRightClick)
333 # Needed by custom image editor
334 wx.InitAllImageHandlers()
337 # Let's create a simple custom editor
339 # NOTE: Editor must be registered *before* adding a property that uses it.
340 self.pg.RegisterEditor(LargeImageEditor)
347 pg.Append( wxpg.PropertyCategory("1 - Basic Properties") )
348 pg.Append( wxpg.StringProperty("String",value="Some Text") )
349 pg.Append( wxpg.IntProperty("Int",value=100) )
350 pg.Append( wxpg.FloatProperty("Float",value=100.0) )
351 pg.Append( wxpg.BoolProperty("Bool",value=True) )
352 pg.Append( wxpg.BoolProperty("Bool_with_Checkbox",value=True) )
353 pg.SetPropertyAttribute("Bool_with_Checkbox", "UseCheckbox", True)
355 pg.Append( wxpg.PropertyCategory("2 - More Properties") )
356 pg.Append( wxpg.LongStringProperty("LongString",value="This is a\\nmulti-line string\\nwith\\ttabs\\nmixed\\tin.") )
357 pg.Append( wxpg.DirProperty("Dir",value="C:\\Windows") )
358 pg.Append( wxpg.FileProperty("File",value="C:\\Windows\\system.ini") )
359 pg.Append( wxpg.ArrayStringProperty("ArrayString",value=['A','B','C']) )
361 pg.Append( wxpg.EnumProperty("Enum","Enum",
362 ['wxPython Rules','wxPython Rocks','wxPython Is The Best'],
364 pg.Append( wxpg.EditEnumProperty("EditEnum","EditEnumProperty",['A','B','C'],[0,1,2],"Text Not in List") )
366 pg.Append( wxpg.PropertyCategory("3 - Advanced Properties") )
367 pg.Append( wxpg.DateProperty("Date",value=wx.DateTime_Now()) )
368 pg.Append( wxpg.FontProperty("Font",value=self.GetFont()) )
369 pg.Append( wxpg.ColourProperty("Colour",value=self.GetBackgroundColour()) )
370 pg.Append( wxpg.SystemColourProperty("SystemColour") )
371 pg.Append( wxpg.ImageFileProperty("ImageFile") )
372 pg.Append( wxpg.MultiChoiceProperty("MultiChoice",choices=['wxWidgets','QT','GTK+']) )
374 pg.Append( wxpg.PropertyCategory("4 - Additional Properties") )
375 pg.Append( wxpg.PointProperty("Point",value=self.GetPosition()) )
376 pg.Append( wxpg.SizeProperty("Size",value=self.GetSize()) )
377 pg.Append( wxpg.FontDataProperty("FontData") )
378 pg.Append( wxpg.IntProperty("IntWithSpin",value=256) )
379 pg.SetPropertyEditor("IntWithSpin","SpinCtrl")
380 pg.Append( wxpg.DirsProperty("Dirs",value=['C:/Lib','C:/Bin']) )
381 pg.SetPropertyHelpString( "String", "String Property help string!" )
382 pg.SetPropertyHelpString( "Dirs", "Dirs Property help string!" )
384 pg.SetPropertyAttribute( "File", wxpg.PG_FILE_SHOW_FULL_PATH, 0 )
385 pg.SetPropertyAttribute( "File", wxpg.PG_FILE_INITIAL_PATH, "C:\\Program Files\\Internet Explorer" )
386 pg.SetPropertyAttribute( "Date", wxpg.PG_DATE_PICKER_STYLE, wx.DP_DROPDOWN|wx.DP_SHOWCENTURY )
388 pg.Append( wxpg.PropertyCategory("5 - Custom Properties") )
389 pg.Append( IntProperty2("IntProperty2", value=1024) )
391 pg.Append( ShapeProperty("ShapeProperty", value=0) )
392 pg.Append( PyObjectProperty("PyObjectProperty") )
394 pg.Append( wxpg.ImageFileProperty("ImageFileWithLargeEditor") )
395 pg.SetPropertyEditor("ImageFileWithLargeEditor", "LargeImageEditor")
398 pg.SetPropertyClientData( "Point", 1234 )
399 if pg.GetPropertyClientData( "Point" ) != 1234:
400 raise ValueError("Set/GetPropertyClientData() failed")
402 # Test setting unicode string
403 pg.GetPropertyByName("String").SetValue(u"Some Unicode Text")
406 # Test some code that *should* fail (but not crash)
408 #a_ = pg.GetPropertyValue( "NotARealProperty" )
409 #pg.EnableProperty( "NotAtAllRealProperty", False )
410 #pg.SetPropertyHelpString( "AgaintNotARealProperty", "Dummy Help String" )
416 sizer.Add(self.pg, 1, wx.EXPAND)
418 sizer.SetSizeHints(self)
420 self.SelectedTreeItem = None
422 def GetPropertyValues(self):
423 return self.pg.GetPropertyValues()
425 def Initialize(self, properties):
430 for element in properties:
431 if element[1]['type'] == 'arraystring':
432 elements = element[1]['elements']
433 if 'value' in element[1]:
434 property_value = element[1]['value']
436 property_value = element[1]['default']
437 #retrieve individual strings
438 property_value = split(property_value, ' ')
440 values = [value.strip('"') for value in property_value]
441 pg.Append(wxpg.ArrayStringProperty(element[0], value=values))
443 if element[1]['type'] == 'boolean':
444 if 'value' in element[1]:
445 property_value = element[1].as_bool('value')
447 property_value = element[1].as_bool('default')
448 property_control = wxpg.BoolProperty(element[0], value=property_value)
449 pg.Append(property_control)
450 pg.SetPropertyAttribute(element[0], 'UseCheckbox', True)
452 #if element[0] == 'category':
453 #pg.Append(wxpg.PropertyCategory(element[1]))
455 if element[1]['type'] == 'color':
456 if 'value' in element[1]:
457 property_value = element[1]['value']
459 property_value = element[1]['default']
460 property_value = eval(property_value)
461 pg.Append(wxpg.ColourProperty(element[0], value=property_value))
463 if element[1]['type'] == 'enum':
464 elements = element[1]['elements']
465 if 'value' in element[1]:
466 property_value = element[1]['value']
468 property_value = element[1]['default']
469 pg.Append(wxpg.EnumProperty(element[0], element[0], elements, [], elements.index(property_value)))
471 if element[1]['type'] == 'filename':
472 if 'value' in element[1]:
473 property_value = element[1]['value']
475 property_value = element[1]['default']
476 pg.Append(wxpg.FileProperty(element[0], value=property_value))
478 if element[1]['type'] == 'float':
479 if 'value' in element[1]:
480 property_value = element[1].as_float('value')
482 property_value = element[1].as_float('default')
483 property_control = wxpg.FloatProperty(element[0], value=property_value)
484 pg.Append(property_control)
486 if element[1]['type'] == 'folder':
487 if 'value' in element[1]:
488 property_value = element[1]['value']
490 property_value = element[1]['default']
491 pg.Append(wxpg.DirProperty(element[0], value=property_value))
493 if element[1]['type'] == 'integer':
494 if 'value' in element[1]:
495 property_value = element[1].as_int('value')
497 property_value = element[1].as_int('default')
498 property_control = wxpg.IntProperty(element[0], value=property_value)
499 if 'maximum' in element[1]:
500 property_control.SetAttribute('Max', element[1].as_int('maximum'))
501 if 'minimum' in element[1]:
502 property_control.SetAttribute('Min', element[1].as_int('minimum'))
503 property_control.SetAttribute('Wrap', True)
504 pg.Append(property_control)
505 pg.SetPropertyEditor(element[0], 'SpinCtrl')
507 if element[1]['type'] == 'string':
508 if 'value' in element[1]:
509 property_value = element[1]['value']
511 property_value = element[1]['default']
512 pg.Append(wxpg.StringProperty(element[0], value=property_value))
516 def OnReserved(self, event):