test_mapdisp.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: Map window and mapdisplay test module
  5. # AUTHOR(S): Vaclav Petras
  6. # PURPOSE: Test functionality using small GUI applications.
  7. # COPYRIGHT: (C) 2013 by Vaclav Petras, and the GRASS Development Team
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. ############################################################################
  20. # %module
  21. # % description: Tests map display and map window widgets
  22. # % keyword: general
  23. # % keyword: GUI
  24. # % keyword: test
  25. # %end
  26. # %option
  27. # % key: test
  28. # % description: Test to run
  29. # % options: mapwindow,mapdisplay,apitest,distance,profile
  30. # % descriptions: mapwindow;Opens map window ;mapdisplay;Opens map display; apitest;Open an application to test API of map window; distance;Starts map window with distance measurement activated; profile;Starts map window with profile tool activated
  31. # % required: yes
  32. # %end
  33. # %option G_OPT_R_INPUT
  34. # % key: raster
  35. # % multiple: yes
  36. # % required: no
  37. # %end
  38. # %option G_OPT_V_INPUT
  39. # % key: vector
  40. # % multiple: yes
  41. # % required: no
  42. # %end
  43. """
  44. Module to run test map window (BufferedWidnow) and map display (MapFrame).
  45. @author Vaclav Petras <wenzeslaus gmail.com>
  46. """
  47. import os
  48. import sys
  49. import wx
  50. import grass.script as grass
  51. from grass.script.setup import set_gui_path
  52. set_gui_path()
  53. # GUI imports require path to GUI code to be set.
  54. from core.settings import UserSettings # noqa: E402
  55. from core.giface import StandaloneGrassInterface # noqa: E402
  56. from mapwin.base import MapWindowProperties # noqa: E402
  57. from mapwin.buffered import BufferedMapWindow # noqa: E402
  58. from core.render import Map # noqa: E402
  59. from rlisetup.sampling_frame import RLiSetupMapPanel # noqa: E402
  60. from mapdisp.main import LayerList # noqa: E402
  61. from gui_core.wrap import StaticText # noqa: E402
  62. class MapdispGrassInterface(StandaloneGrassInterface):
  63. """@implements GrassInterface"""
  64. def __init__(self, map_):
  65. StandaloneGrassInterface.__init__(self)
  66. self._map = map_
  67. self.mapWindow = None
  68. def GetLayerList(self):
  69. return LayerList(self._map, giface=self)
  70. def GetMapWindow(self):
  71. return self.mapWindow
  72. # this is a copy of method from some frame class
  73. def copyOfInitMap(map_, width, height):
  74. """Initialize map display, set dimensions and map region"""
  75. if not grass.find_program("g.region", "--help"):
  76. sys.exit(
  77. _("GRASS module '%s' not found. Unable to start map " "display window.")
  78. % "g.region"
  79. )
  80. map_.ChangeMapSize((width, height))
  81. map_.region = map_.GetRegion() # g.region -upgc
  82. # self.Map.SetRegion() # adjust region to match display window
  83. class TextShower(object):
  84. def __init__(self, parent, title):
  85. self._cf = wx.Frame(parent=parent, title=title)
  86. self._cp = wx.Panel(parent=self._cf, id=wx.ID_ANY)
  87. self._cs = wx.BoxSizer(wx.VERTICAL)
  88. self._cl = StaticText(parent=self._cp, id=wx.ID_ANY, label="No text set yet")
  89. self._cs.Add(self._cl, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  90. self._cp.SetSizer(self._cs)
  91. self._cp.Layout()
  92. self._cf.Show()
  93. def SetLabel(self, text):
  94. self._cl.SetLabel(text)
  95. class Tester(object):
  96. def _listenToAllMapWindowSignals(self, window):
  97. output = sys.stderr
  98. # will make bad thigs after it is closed but who cares
  99. coordinatesShower = TextShower(window, "Coordinates")
  100. window.zoomChanged.connect(lambda: output.write("zoomChanged\n"))
  101. window.zoomHistoryUnavailable.connect(
  102. lambda: output.write("zoomHistoryUnavailable\n")
  103. )
  104. window.zoomHistoryAvailable.connect(
  105. lambda: output.write("zoomHistoryAvailable\n")
  106. )
  107. window.mapQueried.connect(lambda: output.write("mapQueried\n"))
  108. window.mouseEntered.connect(lambda: output.write("mouseEntered\n"))
  109. window.mouseLeftUpPointer.connect(lambda: output.write("mouseLeftUpPointer\n"))
  110. window.mouseLeftUp.connect(lambda: output.write("mouseLeftUp\n"))
  111. window.mouseMoving.connect(
  112. lambda x, y: coordinatesShower.SetLabel("%s , %s" % (x, y))
  113. )
  114. window.mouseHandlerRegistered.connect(
  115. lambda: output.write("mouseHandlerRegistered\n")
  116. )
  117. window.mouseHandlerUnregistered.connect(
  118. lambda: output.write("mouseHandlerUnregistered\n")
  119. )
  120. def testMapWindow(self, giface, map_):
  121. self.frame = wx.Frame(parent=None, title=_("Map window test frame"))
  122. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  123. sizer = wx.BoxSizer(wx.VERTICAL)
  124. mapWindowProperties = MapWindowProperties()
  125. mapWindowProperties.setValuesFromUserSettings()
  126. width, height = self.frame.GetClientSize()
  127. copyOfInitMap(map_, width, height)
  128. window = BufferedMapWindow(
  129. parent=panel, giface=giface, Map=map_, properties=mapWindowProperties
  130. )
  131. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  132. panel.SetSizer(sizer)
  133. panel.Layout()
  134. self.frame.Show()
  135. def testMapDisplay(self, giface, map_):
  136. from mapdisp.frame import MapFrame
  137. # known issues (should be similar with d.mon):
  138. # * opening map in digitizer ends with: vdigit/toolbars.py:723: 'selection' referenced before assignment
  139. # * nviz start fails (closes window? segfaults?) after mapdisp/frame.py:306: 'NoneType' object has no attribute 'GetLayerNotebook'
  140. frame = MapFrame(
  141. parent=None, title=_("Map display test"), giface=giface, Map=map_
  142. )
  143. # this is questionable: how complete the giface when creating objects
  144. # which are in giface
  145. giface.mapWindow = frame.GetMapWindow()
  146. frame.GetMapWindow().ZoomToMap()
  147. frame.Show()
  148. def testMapWindowApi(self, giface, map_):
  149. self.frame = wx.Frame(parent=None, title=_("Map window API test frame"))
  150. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  151. sizer = wx.BoxSizer(wx.VERTICAL)
  152. mapWindowProperties = MapWindowProperties()
  153. mapWindowProperties.setValuesFromUserSettings()
  154. mapWindowProperties.showRegion = True
  155. width, height = self.frame.GetClientSize()
  156. copyOfInitMap(map_, width, height)
  157. window = BufferedMapWindow(
  158. parent=panel, giface=giface, Map=map_, properties=mapWindowProperties
  159. )
  160. giface.mapWindow = window
  161. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  162. panel.SetSizer(sizer)
  163. panel.Layout()
  164. window.ZoomToWind()
  165. self.frame.Show()
  166. def testMapWindowDistance(self, giface, map_):
  167. self.frame = wx.Frame(
  168. parent=None, title=_("Map window distance measurement test frame")
  169. )
  170. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  171. sizer = wx.BoxSizer(wx.VERTICAL)
  172. mapWindowProperties = MapWindowProperties()
  173. mapWindowProperties.setValuesFromUserSettings()
  174. mapWindowProperties.showRegion = True
  175. width, height = self.frame.GetClientSize()
  176. copyOfInitMap(map_, width, height)
  177. window = BufferedMapWindow(
  178. parent=panel, giface=giface, Map=map_, properties=mapWindowProperties
  179. )
  180. giface.mapWindow = window
  181. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  182. panel.SetSizer(sizer)
  183. panel.Layout()
  184. window.ZoomToWind()
  185. self._listenToAllMapWindowSignals(window)
  186. self.frame.Show()
  187. from mapwin.analysis import MeasureDistanceController
  188. self.controller = MeasureDistanceController(giface, window)
  189. self.controller.Start()
  190. def testMapWindowProfile(self, giface, map_):
  191. self.frame = wx.Frame(
  192. parent=None, title=_("Map window profile tool test frame")
  193. )
  194. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  195. sizer = wx.BoxSizer(wx.VERTICAL)
  196. mapWindowProperties = MapWindowProperties()
  197. mapWindowProperties.setValuesFromUserSettings()
  198. mapWindowProperties.showRegion = True
  199. width, height = self.frame.GetClientSize()
  200. copyOfInitMap(map_, width, height)
  201. window = BufferedMapWindow(
  202. parent=panel, giface=giface, Map=map_, properties=mapWindowProperties
  203. )
  204. giface.mapWindow = window
  205. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  206. panel.SetSizer(sizer)
  207. panel.Layout()
  208. window.ZoomToWind()
  209. self._listenToAllMapWindowSignals(window)
  210. self.frame.Show()
  211. from mapwin.analysis import ProfileController
  212. self.controller = ProfileController(giface, window)
  213. self.controller.Start()
  214. rasters = []
  215. for layer in giface.GetLayerList().GetSelectedLayers():
  216. if layer.maplayer.GetType() == "raster":
  217. rasters.append(layer.maplayer.GetName())
  218. from wxplot.profile import ProfileFrame
  219. profileWindow = ProfileFrame(
  220. parent=self.frame,
  221. giface=giface,
  222. controller=self.controller,
  223. units=map_.projinfo["units"],
  224. rasterList=rasters,
  225. )
  226. profileWindow.CentreOnParent()
  227. profileWindow.Show()
  228. # Open raster select dialog to make sure that a raster (and
  229. # the desired raster) is selected to be profiled
  230. profileWindow.OnSelectRaster(None)
  231. def testMapWindowRlisetup(self, map_):
  232. self.frame = wx.Frame(parent=None, title=_("Map window rlisetup test frame"))
  233. RLiSetupMapPanel(parent=self.frame, map_=map_)
  234. self.frame.Show()
  235. def main():
  236. """Sets the GRASS display driver"""
  237. driver = UserSettings.Get(group="display", key="driver", subkey="type")
  238. if driver == "png":
  239. os.environ["GRASS_RENDER_IMMEDIATE"] = "png"
  240. else:
  241. os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo"
  242. # TODO: message format should not be GUI
  243. # TODO: should messages here be translatable?
  244. # (for test its great, for translator not)
  245. options, flags = grass.parser()
  246. test = options["test"]
  247. app = wx.App()
  248. map_ = Map()
  249. if options["raster"]:
  250. names = options["raster"]
  251. for name in names.split(","):
  252. cmdlist = ["d.rast", "map=%s" % name]
  253. map_.AddLayer(
  254. ltype="raster",
  255. command=cmdlist,
  256. active=True,
  257. name=name,
  258. hidden=False,
  259. opacity=1.0,
  260. render=True,
  261. )
  262. if options["vector"]:
  263. names = options["vector"]
  264. for name in names.split(","):
  265. cmdlist = ["d.vect", "map=%s" % name]
  266. map_.AddLayer(
  267. ltype="vector",
  268. command=cmdlist,
  269. active=True,
  270. name=name,
  271. hidden=False,
  272. opacity=1.0,
  273. render=True,
  274. )
  275. giface = MapdispGrassInterface(map_=map_)
  276. tester = Tester()
  277. if test == "mapwindow":
  278. tester.testMapWindow(giface, map_)
  279. elif test == "mapdisplay":
  280. tester.testMapDisplay(giface, map_)
  281. elif test == "apitest":
  282. tester.testMapWindowApi(giface, map_)
  283. elif test == "distance":
  284. tester.testMapWindowDistance(giface, map_)
  285. elif test == "profile":
  286. tester.testMapWindowProfile(giface, map_)
  287. elif test == "rlisetup":
  288. tester.testMapWindowRlisetup(map_)
  289. else:
  290. # TODO: this should not happen but happens
  291. import grass.script as sgrass
  292. sgrass.fatal(_("Unknown value %s of test parameter." % test))
  293. app.MainLoop()
  294. if __name__ == "__main__":
  295. main()