test_mapdisp.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. """
  76. if not grass.find_program('g.region', '--help'):
  77. sys.exit(_("GRASS module '%s' not found. Unable to start map "
  78. "display window.") % 'g.region')
  79. map_.ChangeMapSize((width, height))
  80. map_.region = map_.GetRegion() # g.region -upgc
  81. # self.Map.SetRegion() # adjust region to match display window
  82. class TextShower(object):
  83. def __init__(self, parent, title):
  84. self._cf = wx.Frame(parent=parent, title=title)
  85. self._cp = wx.Panel(parent=self._cf, id=wx.ID_ANY)
  86. self._cs = wx.BoxSizer(wx.VERTICAL)
  87. self._cl = StaticText(
  88. parent=self._cp,
  89. id=wx.ID_ANY,
  90. label="No text set yet")
  91. self._cs.Add(
  92. self._cl,
  93. proportion=1,
  94. flag=wx.EXPAND | wx.ALL,
  95. border=5)
  96. self._cp.SetSizer(self._cs)
  97. self._cp.Layout()
  98. self._cf.Show()
  99. def SetLabel(self, text):
  100. self._cl.SetLabel(text)
  101. class Tester(object):
  102. def _listenToAllMapWindowSignals(self, window):
  103. output = sys.stderr
  104. # will make bad thigs after it is closed but who cares
  105. coordinatesShower = TextShower(window, "Coordinates")
  106. window.zoomChanged.connect(lambda: output.write("zoomChanged\n"))
  107. window.zoomHistoryUnavailable.connect(
  108. lambda: output.write("zoomHistoryUnavailable\n"))
  109. window.zoomHistoryAvailable.connect(
  110. lambda: output.write("zoomHistoryAvailable\n"))
  111. window.mapQueried.connect(lambda: output.write("mapQueried\n"))
  112. window.mouseEntered.connect(lambda: output.write("mouseEntered\n"))
  113. window.mouseLeftUpPointer.connect(
  114. lambda: output.write("mouseLeftUpPointer\n"))
  115. window.mouseLeftUp.connect(lambda: output.write("mouseLeftUp\n"))
  116. window.mouseMoving.connect(
  117. lambda x, y: coordinatesShower.SetLabel(
  118. "%s , %s" %
  119. (x, y)))
  120. window.mouseHandlerRegistered.connect(
  121. lambda: output.write("mouseHandlerRegistered\n"))
  122. window.mouseHandlerUnregistered.connect(
  123. lambda: output.write("mouseHandlerUnregistered\n"))
  124. def testMapWindow(self, giface, map_):
  125. self.frame = wx.Frame(parent=None, title=_("Map window test frame"))
  126. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  127. sizer = wx.BoxSizer(wx.VERTICAL)
  128. mapWindowProperties = MapWindowProperties()
  129. mapWindowProperties.setValuesFromUserSettings()
  130. width, height = self.frame.GetClientSize()
  131. copyOfInitMap(map_, width, height)
  132. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  133. properties=mapWindowProperties)
  134. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  135. panel.SetSizer(sizer)
  136. panel.Layout()
  137. self.frame.Show()
  138. def testMapDisplay(self, giface, map_):
  139. from mapdisp.frame import MapFrame
  140. # known issues (should be similar with d.mon):
  141. # * opening map in digitizer ends with: vdigit/toolbars.py:723: 'selection' referenced before assignment
  142. # * nviz start fails (closes window? segfaults?) after mapdisp/frame.py:306: 'NoneType' object has no attribute 'GetLayerNotebook'
  143. frame = MapFrame(parent=None, title=_("Map display test"),
  144. giface=giface, Map=map_)
  145. # this is questionable: how complete the giface when creating objects
  146. # which are in giface
  147. giface.mapWindow = frame.GetMapWindow()
  148. frame.GetMapWindow().ZoomToMap()
  149. frame.Show()
  150. def testMapWindowApi(self, giface, map_):
  151. self.frame = wx.Frame(parent=None,
  152. title=_("Map window API test frame"))
  153. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  154. sizer = wx.BoxSizer(wx.VERTICAL)
  155. mapWindowProperties = MapWindowProperties()
  156. mapWindowProperties.setValuesFromUserSettings()
  157. mapWindowProperties.showRegion = True
  158. width, height = self.frame.GetClientSize()
  159. copyOfInitMap(map_, width, height)
  160. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  161. properties=mapWindowProperties)
  162. giface.mapWindow = window
  163. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  164. panel.SetSizer(sizer)
  165. panel.Layout()
  166. window.ZoomToWind()
  167. self.frame.Show()
  168. def testMapWindowDistance(self, giface, map_):
  169. self.frame = wx.Frame(parent=None, title=_(
  170. "Map window distance measurement test frame"))
  171. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  172. sizer = wx.BoxSizer(wx.VERTICAL)
  173. mapWindowProperties = MapWindowProperties()
  174. mapWindowProperties.setValuesFromUserSettings()
  175. mapWindowProperties.showRegion = True
  176. width, height = self.frame.GetClientSize()
  177. copyOfInitMap(map_, width, height)
  178. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  179. properties=mapWindowProperties)
  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(parent=None,
  192. title=_("Map window profile tool test frame"))
  193. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  194. sizer = wx.BoxSizer(wx.VERTICAL)
  195. mapWindowProperties = MapWindowProperties()
  196. mapWindowProperties.setValuesFromUserSettings()
  197. mapWindowProperties.showRegion = True
  198. width, height = self.frame.GetClientSize()
  199. copyOfInitMap(map_, width, height)
  200. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  201. properties=mapWindowProperties)
  202. giface.mapWindow = window
  203. sizer.Add(window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  204. panel.SetSizer(sizer)
  205. panel.Layout()
  206. window.ZoomToWind()
  207. self._listenToAllMapWindowSignals(window)
  208. self.frame.Show()
  209. from mapwin.analysis import ProfileController
  210. self.controller = ProfileController(giface, window)
  211. self.controller.Start()
  212. rasters = []
  213. for layer in giface.GetLayerList().GetSelectedLayers():
  214. if layer.maplayer.GetType() == 'raster':
  215. rasters.append(layer.maplayer.GetName())
  216. from wxplot.profile import ProfileFrame
  217. profileWindow = ProfileFrame(parent=self.frame,
  218. giface=giface,
  219. controller=self.controller,
  220. units=map_.projinfo['units'],
  221. rasterList=rasters)
  222. profileWindow.CentreOnParent()
  223. profileWindow.Show()
  224. # Open raster select dialog to make sure that a raster (and
  225. # the desired raster) is selected to be profiled
  226. profileWindow.OnSelectRaster(None)
  227. def testMapWindowRlisetup(self, map_):
  228. self.frame = wx.Frame(parent=None,
  229. title=_("Map window rlisetup test frame"))
  230. RLiSetupMapPanel(parent=self.frame, map_=map_)
  231. self.frame.Show()
  232. def main():
  233. """Sets the GRASS display driver
  234. """
  235. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  236. if driver == 'png':
  237. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  238. else:
  239. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  240. # TODO: message format should not be GUI
  241. # TODO: should messages here be translatable?
  242. # (for test its great, for translator not)
  243. options, flags = grass.parser()
  244. test = options['test']
  245. app = wx.App()
  246. map_ = Map()
  247. if options['raster']:
  248. names = options['raster']
  249. for name in names.split(','):
  250. cmdlist = ['d.rast', 'map=%s' % name]
  251. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  252. name=name, hidden=False, opacity=1.0,
  253. render=True)
  254. if options['vector']:
  255. names = options['vector']
  256. for name in names.split(','):
  257. cmdlist = ['d.vect', 'map=%s' % name]
  258. map_.AddLayer(ltype='vector', command=cmdlist, active=True,
  259. name=name, hidden=False, opacity=1.0,
  260. render=True)
  261. giface = MapdispGrassInterface(map_=map_)
  262. tester = Tester()
  263. if test == 'mapwindow':
  264. tester.testMapWindow(giface, map_)
  265. elif test == 'mapdisplay':
  266. tester.testMapDisplay(giface, map_)
  267. elif test == 'apitest':
  268. tester.testMapWindowApi(giface, map_)
  269. elif test == 'distance':
  270. tester.testMapWindowDistance(giface, map_)
  271. elif test == 'profile':
  272. tester.testMapWindowProfile(giface, map_)
  273. elif test == 'rlisetup':
  274. tester.testMapWindowRlisetup(map_)
  275. else:
  276. # TODO: this should not happen but happens
  277. import grass.script as sgrass
  278. sgrass.fatal(_("Unknown value %s of test parameter." % test))
  279. app.MainLoop()
  280. if __name__ == '__main__':
  281. main()