forked from danheeks/PyCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesCanvas.py
417 lines (335 loc) · 16.3 KB
/
PropertiesCanvas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import sys, time, math, os, os.path
import wx
import wx.adv
_ = wx.GetTranslation
import wx.propgrid as wxpg
import cad
class PropertyMapItem:
def __init__(self, prop = None):
self.prop = prop
self.children = {} # map from key string to PropertyMapItem
self.properties = [] # list of cad.Property objects, when multiple objects selected, there may be multiple properties with the same name which get represented by a single item in the property grid
def OnAddProperty(self, prop):
label = prop.GetLabel()
is_new = label not in self.children
if is_new:
new_map_item = PropertyMapItem(prop)
self.children[label] = new_map_item
return self.children[label], is_new
class PropertiesObserver(cad.Observer):
def __init__(self, window):
cad.Observer.__init__(self)
self.window = window
def OnAdded(self, added):
self.window.Refresh()
def OnRemoved(self, removed):
self.window.Refresh()
def OnModified(self, modified):
self.window.Refresh()
self.window.pg.Refresh()
def OnSelectionChanged(self, added, removed):
self.window.Refresh()
def Freeze(self):
self.window.Freeze()
def Thaw(self):
self.window.Thaw()
class SampleMultiButtonEditor(wxpg.PGTextCtrlEditor):
def __init__(self):
wxpg.PGTextCtrlEditor.__init__(self)
def CreateControls(self, propGrid, property, pos, sz):
# Create and populate buttons-subwindow
buttons = wxpg.PGMultiButton(propGrid, sz)
# Add two regular buttons
buttons.AddButton("...")
buttons.AddButton("A")
# Add a bitmap button
buttons.AddBitmapButton(wx.ArtProvider.GetBitmap(wx.ART_FOLDER))
# Create the 'primary' editor control (textctrl in this case)
wnd = super(SampleMultiButtonEditor, self).CreateControls(
propGrid,
property,
pos,
buttons.GetPrimarySize())
wnd = wnd.m_primary
# Finally, move buttons-subwindow to correct position and make sure
# returned wxPGWindowList contains our custom button list.
buttons.Finalize(propGrid, pos);
# We must maintain a reference to any editor objects we created
# ourselves. Otherwise they might be freed prematurely. Also,
# we need it in OnEvent() below, because in Python we cannot "cast"
# result of wxPropertyGrid.GetEditorControlSecondary() into
# PGMultiButton instance.
self.buttons = buttons
return wxpg.PGWindowList(wnd, buttons)
def OnEvent(self, propGrid, prop, ctrl, event):
if event.GetEventType() == wx.wxEVT_COMMAND_BUTTON_CLICKED:
buttons = self.buttons
evtId = event.GetId()
if evtId == buttons.GetButtonId(0):
# Do something when the first button is pressed
wx.LogDebug("First button pressed")
return False # Return false since value did not change
if evtId == buttons.GetButtonId(1):
# Do something when the second button is pressed
wx.MessageBox("Second button pressed")
return False # Return false since value did not change
if evtId == buttons.GetButtonId(2):
# Do something when the third button is pressed
wx.MessageBox("Third button pressed")
return False # Return false since value did not change
return super(SampleMultiButtonEditor, self).OnEvent(propGrid, prop, ctrl, event)
class CustomTextCtrl(wx.TextCtrl):
def __init__(self, parent, id, value, pos, width, style):
wx.TextCtrl.__init__(self, parent, id, value, pos, width, style)
self.Bind(wx.EVT_CHAR, self.onCharEvent)
def onCharEvent(self, event):
keycode = event.GetKeyCode()
controlDown = event.CmdDown()
altDown = event.AltDown()
shiftDown = event.ShiftDown()
if keycode == wx.WXK_RETURN:
self.WriteText('\n')
else:
event.Skip()
class TrivialPropertyEditor(wxpg.PGEditor):
"""
This is a simple re-creation of TextCtrlWithButton. Note that it does
not take advantage of wx.TextCtrl and wx.Button creation helper functions
in wx.PropertyGrid.
"""
def __init__(self):
wxpg.PGEditor.__init__(self)
def Txt_Ent(self,event):
msg1 = (str(self.tc.GetValue()))
wx.MessageBox(msg1)
def CreateControls(self, propgrid, property, pos, sz):
""" Create the actual wxPython controls here for editing the
property value.
You must use propgrid.GetPanel() as parent for created controls.
Return value is either single editor control or tuple of two
editor controls, of which first is the primary one and second
is usually a button.
"""
try:
x, y = pos
w, h = sz
h = 100
s = property.GetDisplayedString();
self.tc = CustomTextCtrl(propgrid.GetPanel(), wxpg.PG_SUBID1, s,
(x,y), (w ,h),
wx.TE_MULTILINE | wx.TE_PROCESS_ENTER)
return wxpg.PGWindowList(self.tc)#, btn)
except:
import traceback
print(traceback.print_exc())
def UpdateControl(self, property, ctrl):
ctrl.SetValue(property.GetDisplayedString())
def DrawValue(self, dc, rect, property, text):
if not property.IsValueUnspecified():
print(property.GetDisplayedString())
dc.DrawText(property.GetDisplayedString(), rect.x+5, rect.y)
def OnEvent(self, propgrid, property, ctrl, event):
""" Return True if modified editor value should be committed to
the property. To just mark the property value modified, call
propgrid.EditorsValueWasModified().
"""
if not ctrl:
return False
evtType = event.GetEventType()
if evtType == wx.wxEVT_COMMAND_TEXT_ENTER:
self.tc.SetFocus()
#if propgrid.IsEditorsValueModified():
# return True
elif evtType == wx.wxEVT_COMMAND_TEXT_UPDATED:
#
# Pass this event outside wxPropertyGrid so that,
# if necessary, program can tell when user is editing
# a textctrl.
event.Skip()
event.SetId(propgrid.GetId())
propgrid.EditorsValueWasModified()
return False
elif evtType == wx.wxEVT_COMMAND_BUTTON_CLICKED:
wx.MessageBox("Button Clicked")
return False
def GetValueFromControl(self, property, ctrl):
""" Return tuple (wasSuccess, newValue), where wasSuccess is True if
different value was acquired successfully.
"""
tc = ctrl
textVal = tc.GetValue()
if property.UsesAutoUnspecified() and not textVal:
return (True, None)
res, value = property.StringToValue(textVal, wxpg.PG_FULL_VALUE)
# Changing unspecified always causes event (returning
# True here should be enough to trigger it).
if not res and value is None:
res = True
return (res, value)
def SetValueToUnspecified(self, property, ctrl):
ctrl.Remove(0,len(ctrl.GetValue()))
def SetControlStringValue(self, property, ctrl, text):
ctrl.SetValue(text)
def OnFocus(self, property, ctrl):
ctrl.SetSelection(-1,-1)
############################################################################
#
# MAIN PROPERTY GRID TEST PANEL
#
############################################################################
class PropertiesCanvas( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__(self, parent, wx.ID_ANY)
self.panel = wx.Panel(self, wx.ID_ANY)
# Difference between using PropertyGridManager vs PropertyGrid is that
# the manager supports multiple pages and a description box.
self.pg = wxpg.PropertyGrid(self.panel, style=wxpg.PG_DEFAULT_STYLE | wxpg.PG_SPLITTER_AUTO_CENTER)
self.topsizer = wx.BoxSizer(wx.VERTICAL)
self.topsizer.Add(self.pg, 1, wx.EXPAND)
self.ClearProperties() # initialize data structures
# Show help as tooltips
self.pg.SetExtraStyle(wxpg.PG_EX_HELP_AS_TOOLTIPS)
self.pg.Bind( wxpg.EVT_PG_CHANGED, self.OnPropGridChange )
self.pg.Bind( wxpg.EVT_PG_PAGE_CHANGED, self.OnPropGridPageChange )
self.pg.Bind( wxpg.EVT_PG_SELECTED, self.OnPropGridSelect )
self.pg.Bind( wxpg.EVT_PG_RIGHT_CLICK, self.OnPropGridRightClick )
self.pg.Bind( wx.EVT_MOUSE_EVENTS, self.OnMouse )
self.panel.SetSizer(self.topsizer)
self.topsizer.SetSizeHints(self.panel)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(True)
self.inAddProperty = 0
self.in_OnPropGridChange = False
# Register editor class - needs only to be called once
self.multiButtonEditor = SampleMultiButtonEditor()
self.pg.RegisterEditor(self.multiButtonEditor)
self.trivialPropertyEditor = TrivialPropertyEditor()
self.pg.RegisterEditor(self.trivialPropertyEditor)
self.EXTRA_TOOLBAR_HEIGHT = 7 # todo set to 14 for Linux
def OnMouse(self, event):
if event.LeftDown():
# set the focus, so that the up/down keys work
self.pg.SetFocus()
event.Skip()
def FindMapItem(self, p):
if p in self.pmap:
return self.pmap[p]
return None
def Append(self, parent_property, new_prop, property):
if parent_property:
item = self.pmap[parent_property]
new_item, newly_inserted = item.OnAddProperty(new_prop)
if newly_inserted:
self.pg.AppendIn(parent_property, new_prop)
else:
new_item, newly_inserted = self.map.OnAddProperty(new_prop)
if newly_inserted:
self.pg.Append(new_prop)
new_item.properties.append(property)
self.pmap[new_prop] = new_item
self.pset[property] = True
def AddProperty(self, property, parent_property = None):
self.inAddProperty += 1
# add a cad.property, optionally to an existing wx.PGProperty
if property.GetType() == cad.PROPERTY_TYPE_STRING:
new_prop = wxpg.StringProperty(property.GetTitle(),value=property.GetString())
elif property.GetType() == cad.PROPERTY_TYPE_LONG_STRING:
#new_prop = wxpg.LongStringProperty("MultipleButtons", wxpg.PG_LABEL)
new_prop = wxpg.StringProperty(property.GetTitle(), value=property.GetString())
elif property.GetType() == cad.PROPERTY_TYPE_DOUBLE:
new_prop = wxpg.FloatProperty(property.GetTitle(),value=property.GetDouble())
elif property.GetType() == cad.PROPERTY_TYPE_LENGTH:
new_prop = wxpg.FloatProperty(property.GetTitle(),value=property.GetDouble() / cad.GetUnits())
elif property.GetType() == cad.PROPERTY_TYPE_INT:
new_prop = wxpg.IntProperty(property.GetTitle(),value=property.GetInt())
elif property.GetType() == cad.PROPERTY_TYPE_COLOR:
col = property.GetColor()
wcol = wx.Colour(col.red, col.green, col.blue)
new_prop = wxpg.ColourProperty(property.GetTitle(),value=wcol)
elif property.GetType() == cad.PROPERTY_TYPE_CHOICE:
new_prop = wxpg.EnumProperty(property.GetTitle(), labels = property.GetChoices(), value = property.GetInt())
elif property.GetType() == cad.PROPERTY_TYPE_CHECK:
new_prop = wxpg.BoolProperty(property.GetTitle(),value=property.GetBool())
new_prop.SetAttribute(wxpg.PG_BOOL_USE_CHECKBOX, True)
elif property.GetType() == cad.PROPERTY_TYPE_LIST:
new_prop = wxpg.StringProperty(property.GetTitle(), value='<composed>')
elif property.GetType() == cad.PROPERTY_TYPE_FILE:
new_prop = wxpg.FileProperty(property.GetTitle(),value=property.GetString())
else:
wx.MessageBox('invalid property type: ' + str(property.GetType()))
return
if not property.editable: new_prop.ChangeFlag(wxpg.PG_PROP_READONLY, True)
self.Append(parent_property, new_prop, property)
if property.GetType() == cad.PROPERTY_TYPE_LIST:
plist = property.GetProperties()
for p in plist:
self.AddProperty(p, new_prop)
new_prop.ChangeFlag(wxpg.PG_PROP_COLLAPSED, True)
elif property.GetType() == cad.PROPERTY_TYPE_LONG_STRING:
# Change property to use editor created in the previous code segment
#self.pg.SetPropertyEditor("MultipleButtons", self.multiButtonEditor)
self.pg.SetPropertyEditor(new_prop, "TrivialPropertyEditor")
self.inAddProperty -= 1
def ClearProperties(self):
self.pg.Clear()
self.map = PropertyMapItem() # root of a tree of properties
self.pmap = {} # map of wx.PGProperty to PropertyMapItem
self.pset = {} # a set of cad.Property objects ( as a dictionary )
def GetProperties(self, p):
item = self.FindMapItem(p)
if item == None:
return None
return item.properties
def OnPropGridChange(self, event):
self.in_OnPropGridChange = True
p = event.GetProperty()
properties = self.GetProperties(p)
if properties == None:
return
cad.StartHistory('Property Change')
for property in properties:
if property.GetType() == cad.PROPERTY_TYPE_STRING or property.GetType() == cad.PROPERTY_TYPE_LONG_STRING:
cad.ChangePropertyString(p.GetValue(), property)
elif property.GetType() == cad.PROPERTY_TYPE_DOUBLE:
cad.ChangePropertyDouble(p.GetValue(), property)
elif property.GetType() == cad.PROPERTY_TYPE_LENGTH:
cad.ChangePropertyLength(p.GetValue() * cad.GetUnits(), property)
elif property.GetType() == cad.PROPERTY_TYPE_INT:
cad.ChangePropertyInt(p.GetValue(), property)
elif property.GetType() == cad.PROPERTY_TYPE_CHOICE:
cad.ChangePropertyChoice(p.GetValue(), property)
elif property.GetType() == cad.PROPERTY_TYPE_COLOR:
c = p.GetValue()
cad.ChangePropertyColor(cad.Color(c.red, c.green, c.blue), property)
elif property.GetType() == cad.PROPERTY_TYPE_CHECK:
cad.ChangePropertyCheck(p.GetValue(), property)
elif property.GetType() == cad.PROPERTY_TYPE_LIST:
pass
elif property.GetType() == cad.PROPERTY_TYPE_FILE:
cad.ChangePropertyString(p.GetValue(), property)
#for changer in changers:
#cad.DoUndoable(changer)
wx.GetApp().EndHistory()
self.in_OnPropGridChange = False
def OnPropGridSelect(self, event):
p = event.GetProperty()
if p:
pass
#self.log.write('%s selected\n' % (event.GetProperty().GetName()))
else:
pass
#self.log.write('Nothing selected\n')
def OnPropGridRightClick(self, event):
p = event.GetProperty()
if p:
pass
#self.log.write('%s right clicked\n' % (event.GetProperty().GetName()))
else:
pass
#self.log.write('Nothing right clicked\n')
def OnPropGridPageChange(self, event):
index = self.pg.GetSelectedPage()
pass
#self.log.write('Page Changed to \'%s\'\n' % (self.pg.GetPageName(index)))