dialogs.py 24 KB

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