nviz_preferences.py 22 KB

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