GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nviz_preferences.py
Go to the documentation of this file.
1 """
2 @package nviz_preferences.py
3 
4 @brief Nviz (3D view) preferences window
5 
6 Classes:
7  - NvizPreferencesDialog
8 
9 (C) 2008-2010 by the GRASS Development Team
10 
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
13 
14 @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
15 @author Enhancements by Michael Barton <michael.barton@asu.edu>
16 """
17 
18 import types
19 
20 import wx
21 import wx.lib.colourselect as csel
22 
23 import globalvar
24 from preferences import globalSettings as UserSettings
25 from preferences import PreferencesBaseDialog
26 
27 class NvizPreferencesDialog(PreferencesBaseDialog):
28  """!Nviz preferences dialog"""
29  def __init__(self, parent, title = _("3D view settings"),
30  settings = UserSettings):
31  PreferencesBaseDialog.__init__(self, parent = parent, title = title,
32  settings = settings)
33  self.toolWin = self.parent.GetLayerManager().nviz
34  self.win = dict()
35 
36  # create notebook pages
37  self._createViewPage(self.notebook)
38  self._createVectorPage(self.notebook)
39 
40  self.SetMinSize(self.GetBestSize())
41  self.SetSize(self.size)
42 
43  def _createViewPage(self, notebook):
44  """!Create notebook page for general settings"""
45  panel = wx.Panel(parent = notebook, id = wx.ID_ANY)
46 
47  notebook.AddPage(page = panel,
48  text = " %s " % _("View"))
49 
50  pageSizer = wx.BoxSizer(wx.VERTICAL)
51 
52  self.win['general'] = {}
53  self.win['view'] = {}
54  box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
55  label = " %s " % (_("View")))
56  boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
57  gridSizer = wx.GridBagSizer(vgap = 3, hgap = 3)
58 
59  # perspective
60  self.win['view']['persp'] = {}
61  pvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'persp')
62  ipvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'persp', internal = True)
63  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
64  label = _("Perspective:")),
65  pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL)
66  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
67  label = _("(value)")),
68  pos = (0, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
69 
70  pval = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
71  initial = pvals['value'],
72  min = ipvals['min'],
73  max = ipvals['max'])
74  self.win['view']['persp']['value'] = pval.GetId()
75  gridSizer.Add(item = pval, pos = (0, 2),
76  flag = wx.ALIGN_CENTER_VERTICAL)
77 
78  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
79  label = _("(step)")),
80  pos = (0, 3), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
81 
82  pstep = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
83  initial = pvals['step'],
84  min = ipvals['min'],
85  max = ipvals['max']-1)
86  self.win['view']['persp']['step'] = pstep.GetId()
87  gridSizer.Add(item = pstep, pos = (0, 4),
88  flag = wx.ALIGN_CENTER_VERTICAL)
89 
90  # position
91  self.win['view']['pos'] = {}
92  posvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'position')
93  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
94  label = _("Position:")),
95  pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL)
96  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
97  label = _("(x)")),
98  pos = (1, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
99 
100  px = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
101  initial = posvals['x'] * 100,
102  min = 0,
103  max = 100)
104  self.win['view']['pos']['x'] = px.GetId()
105  gridSizer.Add(item = px, pos = (1, 2),
106  flag = wx.ALIGN_CENTER_VERTICAL)
107 
108  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
109  label = "(y)"),
110  pos = (1, 3), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
111 
112  py = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
113  initial = posvals['y'] * 100,
114  min = 0,
115  max = 100)
116  self.win['view']['pos']['y'] = py.GetId()
117  gridSizer.Add(item = py, pos = (1, 4),
118  flag = wx.ALIGN_CENTER_VERTICAL)
119 
120  # height
121  self.win['view']['height'] = {}
122  hvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'height')
123  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
124  label = _("Height:")),
125  pos = (2, 0), flag = wx.ALIGN_CENTER_VERTICAL)
126  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
127  label = _("(step)")),
128  pos = (2, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
129 
130  hstep = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
131  initial = hvals['step'],
132  min = 1,
133  max = 1e6)
134  self.win['view']['height']['step'] = hstep.GetId()
135  gridSizer.Add(item = hstep, pos = (2, 2),
136  flag = wx.ALIGN_CENTER_VERTICAL)
137 
138  # twist
139  self.win['view']['twist'] = {}
140  tvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'twist')
141  itvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'twist', internal = True)
142  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
143  label = _("Twist:")),
144  pos = (3, 0), flag = wx.ALIGN_CENTER_VERTICAL)
145  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
146  label = _("(value)")),
147  pos = (3, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
148 
149  tval = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
150  initial = tvals['value'],
151  min = itvals['min'],
152  max = itvals['max'])
153  self.win['view']['twist']['value'] = tval.GetId()
154  gridSizer.Add(item = tval, pos = (3, 2),
155  flag = wx.ALIGN_CENTER_VERTICAL)
156 
157  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
158  label = _("(step)")),
159  pos = (3, 3), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
160 
161  tstep = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
162  initial = tvals['step'],
163  min = itvals['min'],
164  max = itvals['max']-1)
165  self.win['view']['twist']['step'] = tstep.GetId()
166  gridSizer.Add(item = tstep, pos = (3, 4),
167  flag = wx.ALIGN_CENTER_VERTICAL)
168 
169  # z-exag
170  self.win['view']['z-exag'] = {}
171  zvals = UserSettings.Get(group = 'nviz', key = 'view', subkey = 'z-exag')
172  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
173  label = _("Z-exag:")),
174  pos = (4, 0), flag = wx.ALIGN_CENTER_VERTICAL)
175  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
176  label = _("(value)")),
177  pos = (4, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
178 
179  zval = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
180  min = -1e6,
181  max = 1e6)
182  self.win['view']['z-exag']['value'] = zval.GetId()
183  gridSizer.Add(item = zval, pos = (4, 2),
184  flag = wx.ALIGN_CENTER_VERTICAL)
185 
186  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
187  label = _("(step)")),
188  pos = (4, 3), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
189 
190  zstep = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
191  initial = zvals['step'],
192  min = -1e6,
193  max = 1e6)
194  self.win['view']['z-exag']['step'] = zstep.GetId()
195  gridSizer.Add(item = zstep, pos = (4, 4),
196  flag = wx.ALIGN_CENTER_VERTICAL)
197 
198  boxSizer.Add(item = gridSizer, proportion = 1,
199  flag = wx.ALL | wx.EXPAND, border = 3)
200  pageSizer.Add(item = boxSizer, proportion = 0,
201  flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
202  border = 3)
203 
204  box = wx.StaticBox(parent = panel, id = wx.ID_ANY,
205  label = " %s " % (_("Image Appearance")))
206  boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
207  gridSizer = wx.GridBagSizer(vgap = 3, hgap = 3)
208  gridSizer.AddGrowableCol(0)
209 
210  # background color
211  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
212  label = _("Background color:")),
213  pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL)
214 
215  color = csel.ColourSelect(panel, id = wx.ID_ANY,
216  colour = UserSettings.Get(group = 'nviz', key = 'settings',
217  subkey = ['general', 'bgcolor']),
218  size = globalvar.DIALOG_COLOR_SIZE)
219  self.win['general']['bgcolor'] = color.GetId()
220  gridSizer.Add(item = color, pos = (0, 1))
221 
222  boxSizer.Add(item = gridSizer, proportion = 1,
223  flag = wx.ALL | wx.EXPAND, border = 3)
224  pageSizer.Add(item = boxSizer, proportion = 0,
225  flag = wx.EXPAND | wx.ALL,
226  border = 3)
227 
228  panel.SetSizer(pageSizer)
229 
230  return panel
231 
232  def _createVectorPage(self, notebook):
233  """!Create notebook page for general settings"""
234  panel = wx.Panel(parent = notebook, id = wx.ID_ANY)
235 
236  notebook.AddPage(page = panel,
237  text = " %s " % _("Vector"))
238 
239  pageSizer = wx.BoxSizer(wx.VERTICAL)
240 
241  # vector lines
242  self.win['vector'] = {}
243  self.win['vector']['lines'] = {}
244  box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
245  label = " %s " % (_("Vector lines")))
246  boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
247  gridSizer = wx.GridBagSizer(vgap = 3, hgap = 3)
248 
249  # show
250  row = 0
251  showLines = wx.CheckBox(parent = panel, id = wx.ID_ANY,
252  label = _("Show lines"))
253  self.win['vector']['lines']['show'] = showLines.GetId()
254  showLines.SetValue(UserSettings.Get(group = 'nviz', key = 'vector',
255  subkey = ['lines', 'show']))
256  gridSizer.Add(item = showLines, pos = (row, 0))
257 
258  boxSizer.Add(item = gridSizer, proportion = 1,
259  flag = wx.ALL | wx.EXPAND, border = 3)
260  pageSizer.Add(item = boxSizer, proportion = 0,
261  flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
262  border = 3)
263 
264  # vector points
265  self.win['vector']['points'] = {}
266  box = wx.StaticBox (parent = panel, id = wx.ID_ANY,
267  label = " %s " % (_("Vector points")))
268  boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
269  gridSizer = wx.GridBagSizer(vgap = 3, hgap = 5)
270 
271  # show
272  row = 0
273  showPoints = wx.CheckBox(parent = panel, id = wx.ID_ANY,
274  label = _("Show points"))
275  showPoints.SetValue(UserSettings.Get(group = 'nviz', key = 'vector',
276  subkey = ['points', 'show']))
277  self.win['vector']['points']['show'] = showPoints.GetId()
278  gridSizer.Add(item = showPoints, pos = (row, 0), span = (1, 8))
279 
280  # icon size
281  row += 1
282  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
283  label = _("Size:")),
284  pos = (row, 0), flag = wx.ALIGN_CENTER_VERTICAL)
285 
286  isize = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
287  initial = 100,
288  min = 1,
289  max = 1e6)
290  self.win['vector']['points']['size'] = isize.GetId()
291  isize.SetValue(UserSettings.Get(group = 'nviz', key = 'vector',
292  subkey = ['points', 'size']))
293  gridSizer.Add(item = isize, pos = (row, 1),
294  flag = wx.ALIGN_CENTER_VERTICAL)
295 
296  # icon width
297  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
298  label = _("Width:")),
299  pos = (row, 2), flag = wx.ALIGN_CENTER_VERTICAL)
300 
301  iwidth = wx.SpinCtrl(parent = panel, id = wx.ID_ANY, size = (65, -1),
302  initial = 2,
303  min = 1,
304  max = 1e6)
305  self.win['vector']['points']['width'] = isize.GetId()
306  iwidth.SetValue(UserSettings.Get(group = 'nviz', key = 'vector',
307  subkey = ['points', 'width']))
308  gridSizer.Add(item = iwidth, pos = (row, 3),
309  flag = wx.ALIGN_CENTER_VERTICAL)
310 
311  # icon symbol
312  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
313  label = _("Marker:")),
314  pos = (row, 4), flag = wx.ALIGN_CENTER_VERTICAL)
315  isym = wx.Choice (parent = panel, id = wx.ID_ANY, size = (100, -1),
316  choices = UserSettings.Get(group = 'nviz', key = 'vector',
317  subkey = ['points', 'marker'], internal = True))
318  isym.SetName("selection")
319  self.win['vector']['points']['marker'] = isym.GetId()
320  isym.SetSelection(UserSettings.Get(group = 'nviz', key = 'vector',
321  subkey = ['points', 'marker']))
322  gridSizer.Add(item = isym, flag = wx.ALIGN_CENTER_VERTICAL,
323  pos = (row, 5))
324 
325  # icon color
326  gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
327  label = _("Color:")),
328  pos = (row, 6), flag = wx.ALIGN_CENTER_VERTICAL)
329  icolor = csel.ColourSelect(panel, id = wx.ID_ANY)
330  icolor.SetName("color")
331  self.win['vector']['points']['color'] = icolor.GetId()
332  icolor.SetColour(UserSettings.Get(group = 'nviz', key = 'vector',
333  subkey = ['points', 'color']))
334  gridSizer.Add(item = icolor, flag = wx.ALIGN_CENTER_VERTICAL,
335  pos = (row, 7))
336 
337  boxSizer.Add(item = gridSizer, proportion = 1,
338  flag = wx.ALL | wx.EXPAND, border = 3)
339  pageSizer.Add(item = boxSizer, proportion = 0,
340  flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
341  border = 3)
342 
343  panel.SetSizer(pageSizer)
344 
345  return panel
346 
347  def OnDefault(self, event):
348  """Restore default settings"""
349  settings = copy.deepcopy(UserSettings.GetDefaultSettings()['nviz'])
350  UserSettings.Set(group = 'nviz',
351  value = settings)
352 
353  for subgroup, key in settings.iteritems(): # view, surface, vector...
354  if subgroup != 'view':
355  continue
356  for subkey, value in key.iteritems():
357  for subvalue in value.keys():
358  win = self.FindWindowById(self.win[subgroup][subkey][subvalue])
359  val = settings[subgroup][subkey][subvalue]
360  if subkey == 'position':
361  val = int(val * 100)
362 
363  win.SetValue(val)
364 
365  event.Skip()
366 
367  def OnApply(self, event):
368  """Apply Nviz settings for current session"""
369  settings = UserSettings.Get(group = 'nviz')
370  for subgroup, key in settings.iteritems(): # view, surface, vector...
371  for subkey, value in key.iteritems():
372  if type(value) == types.DictType:
373  for subvalue in value.keys():
374  try: # TODO
375  win = self.FindWindowById(self.win[subgroup][subkey][subvalue])
376  except:
377  # print 'e', subgroup, subkey, subvalue
378  continue
379 
380  if win.GetName() == "selection":
381  value = win.GetSelection()
382  elif win.GetName() == "color":
383  value = tuple(win.GetColour())
384  else:
385  value = win.GetValue()
386  if subkey == 'pos':
387  value = float(value) / 100
388 
389  settings[subgroup][subkey][subvalue] = value
390 
391  def OnSave(self, event):
392  """!Apply changes, update map and save settings of selected
393  layer
394  """
395  # apply changes
396  self.OnApply(None)
397 
398  if self.GetSelection() == self.page['id']:
399  fileSettings = {}
400  UserSettings.ReadSettingsFile(settings = fileSettings)
401  fileSettings['nviz'] = UserSettings.Get(group = 'nviz')
402  file = UserSettings.SaveToFile(fileSettings)
403  self.parent.goutput.WriteLog(_('Nviz settings saved to file <%s>.') % file)
404 
405  def OnLoad(self, event):
406  """!Apply button pressed"""
407  self.LoadSettings()
408 
409  if event:
410  event.Skip()
411 
412  def LoadSettings(self):
413  """!Load saved Nviz settings and apply to current session"""
414  UserSettings.ReadSettingsFile()
415  settings = copy.deepcopy(UserSettings.Get(group = 'nviz'))
416 
417  for subgroup, key in settings.iteritems(): # view, surface, vector...
418  for subkey, value in key.iteritems():
419  for subvalue in value.keys():
420  if subvalue == 'step':
421  continue
422  else:
423  insetting = value[subvalue]
424  if subgroup == 'view':
425  for viewkey, viewitem in self.mapWindow.view[subkey].iteritems():
426  if viewkey == subvalue:
427  self.mapWindow.view[subkey][viewkey] = insetting
428  else:
429  continue
430  else:
431  for otherkey, otheritem in self.win[subgroup][subkey].iteritems():
432  if type(otheritem) == data:
433  for endkey, enditem in otheritem.iteritems():
434  if endkey == subvalue:
435  paramwin = self.FindWindowById(enditem)
436  else:
437  continue
438  else:
439  if otherkey == subvalue:
440  paramwin = self.FindWindowById(otheritem)
441  else:
442  continue
443  if type(insetting) in [tuple, list] and len(insetting) > 2:
444  insetting = tuple(insetting)
445  paramwin.SetColour(insetting)
446  else:
447  try:
448  paramwin.SetValue(insetting)
449  except:
450  try:
451  paramwin.SetStringSelection(insetting)
452  except:
453  continue
454 
455  self.toolWin.UpdateSettings()
456  self.FindWindowById(self.win['view']['pos']).Draw()
457  self.FindWindowById(self.win['view']['pos']).Refresh(False)
458 
459  self.mapWindow.render['quick'] = False
460  self.mapWindow.Refresh(False)
461 
462  def OnSave(self, event):
463  """!Save button pressed
464 
465  Save settings to configuration file
466  """
467  fileSettings = {}
468  UserSettings.ReadSettingsFile(settings = fileSettings)
469  fileSettings['nviz'] = UserSettings.Get(group = 'nviz')
470 
471  fileName = UserSettings.SaveToFile(fileSettings)
472  self.parent.GetLayerManager().goutput.WriteLog(_('3D view settings saved to file <%s>.') % fileName)
473 
474  self.Destroy()
475