dialogs.py 24 KB

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