dialogs.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. """
  2. @package location_wizard.dialogs
  3. @brief Location wizard - dialogs
  4. Classes:
  5. - dialogs::RegionDef
  6. - dialogs::TransList
  7. - dialogs::SelectTransformDialog
  8. (C) 2007-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Michael Barton
  12. @author Jachym Cepicky
  13. @author Martin Landa <landa.martin gmail.com>
  14. """
  15. import os
  16. import wx
  17. import wx.lib.scrolledpanel as scrolled
  18. from core import globalvar
  19. from core.gcmd import RunCommand
  20. from location_wizard.base import BaseClass
  21. from gui_core.wrap import Button, StaticText, StaticBox, \
  22. TextCtrl
  23. class RegionDef(BaseClass, wx.Dialog):
  24. """Page for setting default region extents and resolution
  25. """
  26. def __init__(self, parent, id=wx.ID_ANY, size=(800, 600), title=_(
  27. "Set default region extent and resolution"), location=None):
  28. wx.Dialog.__init__(self, parent, id, title, size=size)
  29. panel = wx.Panel(self, id=wx.ID_ANY)
  30. self.SetIcon(
  31. wx.Icon(
  32. os.path.join(
  33. globalvar.ICONDIR,
  34. 'grass.ico'),
  35. wx.BITMAP_TYPE_ICO))
  36. self.parent = parent
  37. self.location = location
  38. #
  39. # default values
  40. #
  41. # 2D
  42. self.north = 1.0
  43. self.south = 0.0
  44. self.east = 1.0
  45. self.west = 0.0
  46. self.nsres = 1.0
  47. self.ewres = 1.0
  48. # 3D
  49. self.top = 1.0
  50. self.bottom = 0.0
  51. # self.nsres3 = 1.0
  52. # self.ewres3 = 1.0
  53. self.tbres = 1.0
  54. #
  55. # inputs
  56. #
  57. # 2D
  58. self.tnorth = self.MakeTextCtrl(
  59. text=str(
  60. self.north), size=(
  61. 150, -1), parent=panel)
  62. self.tsouth = self.MakeTextCtrl(
  63. str(self.south),
  64. size=(150, -1),
  65. parent=panel)
  66. self.twest = self.MakeTextCtrl(
  67. str(self.west),
  68. size=(150, -1),
  69. parent=panel)
  70. self.teast = self.MakeTextCtrl(
  71. str(self.east),
  72. size=(150, -1),
  73. parent=panel)
  74. self.tnsres = self.MakeTextCtrl(
  75. str(self.nsres),
  76. size=(150, -1),
  77. parent=panel)
  78. self.tewres = self.MakeTextCtrl(
  79. str(self.ewres),
  80. size=(150, -1),
  81. parent=panel)
  82. #
  83. # labels
  84. #
  85. self.lrows = self.MakeLabel(parent=panel)
  86. self.lcols = self.MakeLabel(parent=panel)
  87. self.lcells = self.MakeLabel(parent=panel)
  88. #
  89. # buttons
  90. #
  91. self.bset = self.MakeButton(
  92. text=_("&Set region"),
  93. id=wx.ID_OK, parent=panel)
  94. self.bcancel = Button(panel, id=wx.ID_CANCEL)
  95. self.bset.SetDefault()
  96. #
  97. # image
  98. #
  99. self.img = wx.Image(os.path.join(globalvar.IMGDIR, "qgis_world.png"),
  100. wx.BITMAP_TYPE_PNG).ConvertToBitmap()
  101. #
  102. # set current working environment to PERMANENT mapset
  103. # in selected location in order to set default region (WIND)
  104. #
  105. envval = {}
  106. ret = RunCommand('g.gisenv',
  107. read=True)
  108. if ret:
  109. for line in ret.splitlines():
  110. key, val = line.split('=')
  111. envval[key] = val
  112. self.currlocation = envval['LOCATION_NAME'].strip("';")
  113. self.currmapset = envval['MAPSET'].strip("';")
  114. if self.currlocation != self.location or self.currmapset != 'PERMANENT':
  115. RunCommand('g.gisenv',
  116. set='LOCATION_NAME=%s' % self.location)
  117. RunCommand('g.gisenv',
  118. set='MAPSET=PERMANENT')
  119. else:
  120. dlg = wx.MessageBox(
  121. parent=self,
  122. message=_('Invalid location selected.'),
  123. caption=_("Error"),
  124. style=wx.ID_OK | wx.ICON_ERROR)
  125. return
  126. #
  127. # get current region settings
  128. #
  129. region = {}
  130. ret = RunCommand('g.region',
  131. read=True,
  132. flags='gp3')
  133. if ret:
  134. for line in ret.splitlines():
  135. key, val = line.split('=')
  136. region[key] = float(val)
  137. else:
  138. dlg = wx.MessageBox(
  139. parent=self,
  140. message=_("Invalid region"),
  141. caption=_("Error"),
  142. style=wx.ID_OK | wx.ICON_ERROR)
  143. dlg.ShowModal()
  144. dlg.Destroy()
  145. return
  146. #
  147. # update values
  148. # 2D
  149. self.north = float(region['n'])
  150. self.south = float(region['s'])
  151. self.east = float(region['e'])
  152. self.west = float(region['w'])
  153. self.nsres = float(region['nsres'])
  154. self.ewres = float(region['ewres'])
  155. self.rows = int(region['rows'])
  156. self.cols = int(region['cols'])
  157. self.cells = int(region['cells'])
  158. # 3D
  159. self.top = float(region['t'])
  160. self.bottom = float(region['b'])
  161. # self.nsres3 = float(region['nsres3'])
  162. # self.ewres3 = float(region['ewres3'])
  163. self.tbres = float(region['tbres'])
  164. self.depth = int(region['depths'])
  165. self.cells3 = int(region['cells3'])
  166. #
  167. # 3D box collapsable
  168. #
  169. self.infoCollapseLabelExp = _("Click here to show 3D settings")
  170. self.infoCollapseLabelCol = _("Click here to hide 3D settings")
  171. self.settings3D = wx.CollapsiblePane(parent=panel,
  172. label=self.infoCollapseLabelExp,
  173. style=wx.CP_DEFAULT_STYLE |
  174. wx.CP_NO_TLW_RESIZE | wx.EXPAND)
  175. self.MakeSettings3DPaneContent(self.settings3D.GetPane())
  176. self.settings3D.Collapse(False) # FIXME
  177. self.Bind(
  178. wx.EVT_COLLAPSIBLEPANE_CHANGED,
  179. self.OnSettings3DPaneChanged,
  180. self.settings3D)
  181. #
  182. # set current region settings
  183. #
  184. self.tnorth.SetValue(str(self.north))
  185. self.tsouth.SetValue(str(self.south))
  186. self.twest.SetValue(str(self.west))
  187. self.teast.SetValue(str(self.east))
  188. self.tnsres.SetValue(str(self.nsres))
  189. self.tewres.SetValue(str(self.ewres))
  190. self.ttop.SetValue(str(self.top))
  191. self.tbottom.SetValue(str(self.bottom))
  192. # self.tnsres3.SetValue(str(self.nsres3))
  193. # self.tewres3.SetValue(str(self.ewres3))
  194. self.ttbres.SetValue(str(self.tbres))
  195. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  196. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  197. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  198. #
  199. # bindings
  200. #
  201. self.Bind(wx.EVT_BUTTON, self.OnSetButton, self.bset)
  202. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.bcancel)
  203. self.tnorth.Bind(wx.EVT_TEXT, self.OnValue)
  204. self.tsouth.Bind(wx.EVT_TEXT, self.OnValue)
  205. self.teast.Bind(wx.EVT_TEXT, self.OnValue)
  206. self.twest.Bind(wx.EVT_TEXT, self.OnValue)
  207. self.tnsres.Bind(wx.EVT_TEXT, self.OnValue)
  208. self.tewres.Bind(wx.EVT_TEXT, self.OnValue)
  209. self.ttop.Bind(wx.EVT_TEXT, self.OnValue)
  210. self.tbottom.Bind(wx.EVT_TEXT, self.OnValue)
  211. # self.tnsres3.Bind(wx.EVT_TEXT, self.OnValue)
  212. # self.tewres3.Bind(wx.EVT_TEXT, self.OnValue)
  213. self.ttbres.Bind(wx.EVT_TEXT, self.OnValue)
  214. self.__DoLayout(panel)
  215. self.SetMinSize(self.GetBestSize())
  216. self.minWindowSize = self.GetMinSize()
  217. wx.CallAfter(self.settings3D.Collapse, True)
  218. def MakeSettings3DPaneContent(self, pane):
  219. """Create 3D region settings pane"""
  220. border = wx.BoxSizer(wx.VERTICAL)
  221. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  222. # inputs
  223. self.ttop = TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.top),
  224. size=(150, -1))
  225. self.tbottom = TextCtrl(
  226. parent=pane, id=wx.ID_ANY, value=str(
  227. self.bottom), size=(
  228. 150, -1))
  229. self.ttbres = TextCtrl(
  230. parent=pane, id=wx.ID_ANY, value=str(
  231. self.tbres), size=(
  232. 150, -1))
  233. # self.tnsres3 = wx.TextCtrl(parent = pane, id = wx.ID_ANY, value = str(self.nsres3),
  234. # size = (150, -1))
  235. # self.tewres3 = wx.TextCtrl(parent = pane, id = wx.ID_ANY, value = str(self.ewres3),
  236. # size = (150, -1))
  237. # labels
  238. self.ldepth = StaticText(
  239. parent=pane,
  240. label=_("Depth: %d") %
  241. self.depth)
  242. self.lcells3 = StaticText(
  243. parent=pane,
  244. label=_("3D Cells: %d") %
  245. self.cells3)
  246. # top
  247. gridSizer.Add(StaticText(parent=pane, label=_("Top")),
  248. flag=wx.ALIGN_CENTER |
  249. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  250. pos=(0, 1))
  251. gridSizer.Add(self.ttop,
  252. flag=wx.ALIGN_CENTER_HORIZONTAL |
  253. wx.ALL, border=5, pos=(1, 1))
  254. # bottom
  255. gridSizer.Add(StaticText(parent=pane, label=_("Bottom")),
  256. flag=wx.ALIGN_CENTER |
  257. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  258. pos=(0, 2))
  259. gridSizer.Add(self.tbottom,
  260. flag=wx.ALIGN_CENTER_HORIZONTAL |
  261. wx.ALL, border=5, pos=(1, 2))
  262. # tbres
  263. gridSizer.Add(
  264. StaticText(
  265. parent=pane,
  266. label=_("T-B resolution")),
  267. flag=wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT | wx.TOP,
  268. border=5,
  269. pos=(
  270. 0,
  271. 3))
  272. gridSizer.Add(self.ttbres,
  273. flag=wx.ALIGN_CENTER_HORIZONTAL |
  274. wx.ALL, border=5, pos=(1, 3))
  275. # res
  276. # gridSizer.Add(item = wx.StaticText(parent = pane, label = _("3D N-S resolution")),
  277. # flag = wx.ALIGN_CENTER |
  278. # wx.LEFT | wx.RIGHT | wx.TOP, border = 5,
  279. # pos = (2, 1))
  280. # gridSizer.Add(item = self.tnsres3,
  281. # flag = wx.ALIGN_CENTER_HORIZONTAL |
  282. # wx.ALL, border = 5, pos = (3, 1))
  283. # gridSizer.Add(item = wx.StaticText(parent = pane, label = _("3D E-W resolution")),
  284. # flag = wx.ALIGN_CENTER |
  285. # wx.LEFT | wx.RIGHT | wx.TOP, border = 5,
  286. # pos = (2, 3))
  287. # gridSizer.Add(item = self.tewres3,
  288. # flag = wx.ALIGN_CENTER_HORIZONTAL |
  289. # wx.ALL, border = 5, pos = (3, 3))
  290. # rows/cols/cells
  291. gridSizer.Add(self.ldepth,
  292. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  293. wx.ALL, border=5, pos=(2, 1))
  294. gridSizer.Add(self.lcells3,
  295. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  296. wx.ALL, border=5, pos=(2, 2))
  297. border.Add(gridSizer, proportion=1,
  298. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  299. pane.SetSizer(border)
  300. border.Fit(pane)
  301. def OnSettings3DPaneChanged(self, event):
  302. """Collapse 3D settings box"""
  303. if self.settings3D.IsExpanded():
  304. self.settings3D.SetLabel(self.infoCollapseLabelCol)
  305. self.Layout()
  306. self.SetSize(self.GetBestSize())
  307. self.SetMinSize(self.GetSize())
  308. else:
  309. self.settings3D.SetLabel(self.infoCollapseLabelExp)
  310. self.Layout()
  311. self.SetSize(self.minWindowSize)
  312. self.SetMinSize(self.minWindowSize)
  313. self.SendSizeEvent()
  314. def __DoLayout(self, panel):
  315. """Window layout"""
  316. frameSizer = wx.BoxSizer(wx.VERTICAL)
  317. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  318. settings3DSizer = wx.BoxSizer(wx.VERTICAL)
  319. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  320. # north
  321. gridSizer.Add(self.MakeLabel(text=_("North"), parent=panel),
  322. flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL |
  323. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(0, 2))
  324. gridSizer.Add(self.tnorth,
  325. flag=wx.ALIGN_CENTER_HORIZONTAL |
  326. wx.ALIGN_CENTER_VERTICAL |
  327. wx.ALL, border=5, pos=(1, 2))
  328. # west
  329. gridSizer.Add(self.MakeLabel(text=_("West"), parent=panel),
  330. flag=wx.ALIGN_RIGHT |
  331. wx.ALIGN_CENTER_VERTICAL |
  332. wx.LEFT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 0))
  333. gridSizer.Add(self.twest,
  334. flag=wx.ALIGN_RIGHT |
  335. wx.ALIGN_CENTER_VERTICAL |
  336. wx.ALL, border=5, pos=(2, 1))
  337. gridSizer.Add(
  338. wx.StaticBitmap(
  339. panel, wx.ID_ANY, self.img, (-1, -1),
  340. (self.img.GetWidth(),
  341. self.img.GetHeight())),
  342. flag=wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5,
  343. pos=(2, 2))
  344. # east
  345. gridSizer.Add(self.teast,
  346. flag=wx.ALIGN_CENTER_HORIZONTAL |
  347. wx.ALIGN_CENTER_VERTICAL |
  348. wx.ALL, border=5, pos=(2, 3))
  349. gridSizer.Add(self.MakeLabel(text=_("East"), parent=panel),
  350. flag=wx.ALIGN_LEFT |
  351. wx.ALIGN_CENTER_VERTICAL |
  352. wx.RIGHT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 4))
  353. # south
  354. gridSizer.Add(self.tsouth,
  355. flag=wx.ALIGN_CENTER_HORIZONTAL |
  356. wx.ALIGN_CENTER_VERTICAL |
  357. wx.ALL, border=5, pos=(3, 2))
  358. gridSizer.Add(self.MakeLabel(text=_("South"), parent=panel),
  359. flag=wx.ALIGN_TOP | wx.ALIGN_CENTER_HORIZONTAL |
  360. wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5, pos=(4, 2))
  361. # ns-res
  362. gridSizer.Add(self.MakeLabel(text=_("N-S resolution"), parent=panel),
  363. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  364. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 1))
  365. gridSizer.Add(self.tnsres,
  366. flag=wx.ALIGN_RIGHT |
  367. wx.ALIGN_CENTER_VERTICAL |
  368. wx.ALL, border=5, pos=(6, 1))
  369. # ew-res
  370. gridSizer.Add(self.MakeLabel(text=_("E-W resolution"), parent=panel),
  371. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  372. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 3))
  373. gridSizer.Add(self.tewres,
  374. flag=wx.ALIGN_RIGHT |
  375. wx.ALIGN_CENTER_VERTICAL |
  376. wx.ALL, border=5, pos=(6, 3))
  377. # rows/cols/cells
  378. gridSizer.Add(self.lrows,
  379. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  380. wx.ALL, border=5, pos=(7, 1))
  381. gridSizer.Add(self.lcells,
  382. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  383. wx.ALL, border=5, pos=(7, 2))
  384. gridSizer.Add(self.lcols,
  385. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  386. wx.ALL, border=5, pos=(7, 3))
  387. # 3D
  388. settings3DSizer.Add(self.settings3D,
  389. flag=wx.ALL,
  390. border=5)
  391. # buttons
  392. buttonSizer.Add(self.bcancel, proportion=1,
  393. flag=wx.ALIGN_RIGHT |
  394. wx.ALIGN_CENTER_VERTICAL |
  395. wx.ALL, border=10)
  396. buttonSizer.Add(self.bset, proportion=1,
  397. flag=wx.ALIGN_CENTER |
  398. wx.ALIGN_CENTER_VERTICAL |
  399. wx.ALL, border=10)
  400. frameSizer.Add(gridSizer, proportion=1,
  401. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  402. frameSizer.Add(settings3DSizer, proportion=0,
  403. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  404. frameSizer.Add(buttonSizer, proportion=0,
  405. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  406. self.SetAutoLayout(True)
  407. panel.SetSizer(frameSizer)
  408. frameSizer.Fit(panel)
  409. self.Layout()
  410. def OnValue(self, event):
  411. """Set given value"""
  412. try:
  413. if event.GetId() == self.tnorth.GetId():
  414. self.north = float(event.GetString())
  415. elif event.GetId() == self.tsouth.GetId():
  416. self.south = float(event.GetString())
  417. elif event.GetId() == self.teast.GetId():
  418. self.east = float(event.GetString())
  419. elif event.GetId() == self.twest.GetId():
  420. self.west = float(event.GetString())
  421. elif event.GetId() == self.tnsres.GetId():
  422. self.nsres = float(event.GetString())
  423. elif event.GetId() == self.tewres.GetId():
  424. self.ewres = float(event.GetString())
  425. elif event.GetId() == self.ttop.GetId():
  426. self.top = float(event.GetString())
  427. elif event.GetId() == self.tbottom.GetId():
  428. self.bottom = float(event.GetString())
  429. # elif event.GetId() == self.tnsres3.GetId():
  430. # self.nsres3 = float(event.GetString())
  431. # elif event.GetId() == self.tewres3.GetId():
  432. # self.ewres3 = float(event.GetString())
  433. elif event.GetId() == self.ttbres.GetId():
  434. self.tbres = float(event.GetString())
  435. self.__UpdateInfo()
  436. except ValueError as e:
  437. if len(event.GetString()) > 0 and event.GetString() != '-':
  438. dlg = wx.MessageBox(parent=self,
  439. message=_("Invalid value: %s") % e,
  440. caption=_("Error"),
  441. style=wx.OK | wx.ICON_ERROR)
  442. # reset values
  443. self.tnorth.SetValue(str(self.north))
  444. self.tsouth.SetValue(str(self.south))
  445. self.teast.SetValue(str(self.east))
  446. self.twest.SetValue(str(self.west))
  447. self.tnsres.SetValue(str(self.nsres))
  448. self.tewres.SetValue(str(self.ewres))
  449. self.ttop.SetValue(str(self.top))
  450. self.tbottom.SetValue(str(self.bottom))
  451. self.ttbres.SetValue(str(self.tbres))
  452. # self.tnsres3.SetValue(str(self.nsres3))
  453. # self.tewres3.SetValue(str(self.ewres3))
  454. event.Skip()
  455. def __UpdateInfo(self):
  456. """Update number of rows/cols/cells"""
  457. try:
  458. rows = int((self.north - self.south) / self.nsres)
  459. cols = int((self.east - self.west) / self.ewres)
  460. except ZeroDivisionError:
  461. return
  462. self.rows = rows
  463. self.cols = cols
  464. self.cells = self.rows * self.cols
  465. try:
  466. depth = int((self.top - self.bottom) / self.tbres)
  467. except ZeroDivisionError:
  468. return
  469. self.depth = depth
  470. self.cells3 = self.rows * self.cols * self.depth
  471. # 2D
  472. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  473. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  474. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  475. # 3D
  476. self.ldepth.SetLabel(_("Depth: %d" % self.depth))
  477. self.lcells3.SetLabel(_("3D Cells: %d" % self.cells3))
  478. def OnSetButton(self, event=None):
  479. """Set default region"""
  480. ret = RunCommand('g.region',
  481. flags='sa',
  482. n=self.north,
  483. s=self.south,
  484. e=self.east,
  485. w=self.west,
  486. nsres=self.nsres,
  487. ewres=self.ewres,
  488. t=self.top,
  489. b=self.bottom,
  490. tbres=self.tbres)
  491. if ret == 0:
  492. self.Destroy()
  493. def OnCancel(self, event):
  494. self.Destroy()
  495. class TransList(wx.VListBox):
  496. """Creates a multiline listbox for selecting datum transforms"""
  497. def OnDrawItem(self, dc, rect, n):
  498. if self.GetSelection() == n:
  499. c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
  500. else:
  501. c = self.GetForegroundColour()
  502. dc.SetFont(self.GetFont())
  503. dc.SetTextForeground(c)
  504. dc.DrawLabel(self._getItemText(n), rect,
  505. wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  506. def OnMeasureItem(self, n):
  507. height = 0
  508. if self._getItemText(n) is None:
  509. return height
  510. for line in self._getItemText(n).splitlines():
  511. w, h = self.GetTextExtent(line)
  512. height += h
  513. return height + 5
  514. def _getItemText(self, item):
  515. global transformlist
  516. transitem = transformlist[item]
  517. if transitem.strip() != '':
  518. return transitem
  519. class SelectTransformDialog(wx.Dialog):
  520. """Dialog for selecting datum transformations"""
  521. def __init__(self, parent, transforms,
  522. title=_("Select datum transformation"),
  523. pos=wx.DefaultPosition, size=wx.DefaultSize,
  524. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  525. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  526. global transformlist
  527. self.CentreOnParent()
  528. # default transform number
  529. self.transnum = 0
  530. panel = scrolled.ScrolledPanel(self, wx.ID_ANY)
  531. sizer = wx.BoxSizer(wx.VERTICAL)
  532. #
  533. # set panel sizer
  534. #
  535. panel.SetSizer(sizer)
  536. panel.SetupScrolling()
  537. #
  538. # dialog body
  539. #
  540. bodyBox = StaticBox(
  541. parent=panel, id=wx.ID_ANY, label=" %s " %
  542. _("Select from list of datum transformations"))
  543. bodySizer = wx.StaticBoxSizer(bodyBox)
  544. # add no transform option
  545. transforms = '---\n\n0\nDo not apply any datum transformations\n\n' + transforms
  546. transformlist = transforms.split('---')
  547. tlistlen = len(transformlist)
  548. # calculate size for transform list
  549. height = 0
  550. width = 0
  551. for line in transforms.splitlines():
  552. w, h = self.GetTextExtent(line)
  553. height += h
  554. width = max(width, w)
  555. height = height + 5
  556. if height > 400:
  557. height = 400
  558. width = width + 5
  559. if width > 400:
  560. width = 400
  561. #
  562. # VListBox for displaying and selecting transformations
  563. #
  564. self.translist = TransList(
  565. panel, id=-1, size=(width, height),
  566. style=wx.SUNKEN_BORDER)
  567. self.translist.SetItemCount(tlistlen)
  568. self.translist.SetSelection(2)
  569. self.translist.SetFocus()
  570. self.Bind(wx.EVT_LISTBOX, self.ClickTrans, self.translist)
  571. bodySizer.Add(
  572. self.translist,
  573. proportion=1,
  574. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND)
  575. #
  576. # buttons
  577. #
  578. btnsizer = wx.StdDialogButtonSizer()
  579. btn = Button(parent=panel, id=wx.ID_OK)
  580. btn.SetDefault()
  581. btnsizer.AddButton(btn)
  582. btn = Button(parent=panel, id=wx.ID_CANCEL)
  583. btnsizer.AddButton(btn)
  584. btnsizer.Realize()
  585. sizer.Add(bodySizer, proportion=1,
  586. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  587. sizer.Add(btnsizer, proportion=0,
  588. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  589. sizer.Fit(panel)
  590. self.SetSize(self.GetBestSize())
  591. self.Layout()
  592. def ClickTrans(self, event):
  593. """Get the number of the datum transform to use in g.proj"""
  594. self.transnum = event.GetSelection()
  595. self.transnum = self.transnum - 1
  596. def GetTransform(self):
  597. """Get the number of the datum transform to use in g.proj"""
  598. self.transnum = self.translist.GetSelection()
  599. self.transnum = self.transnum - 1
  600. return self.transnum
  601. def testRegionDef():
  602. import wx.lib.inspection
  603. import grass.script as gscript
  604. app = wx.App()
  605. dlg = RegionDef(None, location=gscript.gisenv()["LOCATION_NAME"])
  606. dlg.Show()
  607. wx.lib.inspection.InspectionTool().Show()
  608. app.MainLoop()
  609. if __name__ == '__main__':
  610. testRegionDef()