mapdisp.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. """
  2. @package gui_core.mapdisp
  3. @brief Base classes for Map display window
  4. Classes:
  5. - mapdisp::MapFrameBase
  6. - mapdisp::SingleMapFrame
  7. - mapdisp::DoubleMapFrame
  8. (C) 2009-2014 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 Martin Landa <landa.martin gmail.com>
  12. @author Michael Barton <michael.barton@asu.edu>
  13. @author Vaclav Petras <wenzeslaus gmail.com>
  14. @author Anna Kratochvilova <kratochanna gmail.com>
  15. """
  16. import os
  17. import sys
  18. import six
  19. import wx
  20. from core import globalvar
  21. from core.debug import Debug
  22. from gui_core.toolbars import ToolSwitcher
  23. from gui_core.wrap import NewId
  24. from mapdisp import statusbar as sb
  25. from grass.script import core as grass
  26. class MapFrameBase(wx.Frame):
  27. """Base class for map display window
  28. Derived class must use (create and initialize) \c statusbarManager
  29. or override
  30. GetProperty(), SetProperty() and HasProperty() methods.
  31. Several methods has to be overridden or
  32. \c NotImplementedError("MethodName") will be raised.
  33. If derived class enables and disables auto-rendering,
  34. it should override IsAutoRendered method.
  35. It is expected that derived class will call _setUpMapWindow().
  36. Derived class can has one or more map windows (and map renders)
  37. but implementation of MapFrameBase expects that one window and
  38. one map will be current.
  39. Current instances of map window and map renderer should be returned
  40. by methods GetWindow() and GetMap() respectively.
  41. AUI manager is stored in \c self._mgr.
  42. """
  43. def __init__(
  44. self,
  45. parent=None,
  46. id=wx.ID_ANY,
  47. title="",
  48. style=wx.DEFAULT_FRAME_STYLE,
  49. auimgr=None,
  50. name="",
  51. **kwargs,
  52. ):
  53. """
  54. .. warning::
  55. Use \a auimgr parameter only if you know what you are doing.
  56. :param parent: gui parent
  57. :param id: wx id
  58. :param title: window title
  59. :param style: \c wx.Frame style
  60. :param toolbars: array of activated toolbars, e.g. ['map', 'digit']
  61. :param auimgr: AUI manager (if \c None, wx.aui.AuiManager is used)
  62. :param name: frame name
  63. :param kwargs: arguments passed to \c wx.Frame
  64. """
  65. self.parent = parent
  66. wx.Frame.__init__(self, parent, id, title, style=style, name=name, **kwargs)
  67. #
  68. # set the size & system icon
  69. #
  70. self.SetClientSize(self.GetSize())
  71. self.iconsize = (16, 16)
  72. self.SetIcon(
  73. wx.Icon(
  74. os.path.join(globalvar.ICONDIR, "grass_map.ico"), wx.BITMAP_TYPE_ICO
  75. )
  76. )
  77. # toolbars
  78. self.toolbars = {}
  79. #
  80. # Fancy gui
  81. #
  82. if auimgr is None:
  83. from wx.aui import AuiManager
  84. self._mgr = AuiManager(self)
  85. else:
  86. self._mgr = auimgr
  87. # handles switching between tools in different toolbars
  88. self._toolSwitcher = ToolSwitcher()
  89. self._toolSwitcher.toggleToolChanged.connect(self._onToggleTool)
  90. self._initShortcuts()
  91. def _initShortcuts(self):
  92. # set accelerator table (fullscreen, close window)
  93. shortcuts_table = (
  94. (self.OnFullScreen, wx.ACCEL_NORMAL, wx.WXK_F11),
  95. (self.OnCloseWindow, wx.ACCEL_CTRL, ord("W")),
  96. (self.OnRender, wx.ACCEL_CTRL, ord("R")),
  97. (self.OnRender, wx.ACCEL_NORMAL, wx.WXK_F5),
  98. )
  99. accelTable = []
  100. for handler, entry, kdb in shortcuts_table:
  101. wxId = NewId()
  102. self.Bind(wx.EVT_MENU, handler, id=wxId)
  103. accelTable.append((entry, kdb, wxId))
  104. self.SetAcceleratorTable(wx.AcceleratorTable(accelTable))
  105. def _initMap(self, Map):
  106. """Initialize map display, set dimensions and map region"""
  107. if not grass.find_program("g.region", "--help"):
  108. sys.exit(
  109. _("GRASS module '%s' not found. Unable to start map " "display window.")
  110. % "g.region"
  111. )
  112. Debug.msg(2, "MapFrame._initMap():")
  113. Map.ChangeMapSize(self.GetClientSize())
  114. Map.region = Map.GetRegion() # g.region -upgc
  115. # self.Map.SetRegion() # adjust region to match display window
  116. def _resize(self):
  117. Debug.msg(1, "MapFrame._resize():")
  118. wm, hw = self.MapWindow.GetClientSize()
  119. wf, hf = self.GetSize()
  120. dw = wf - wm
  121. dh = hf - hw
  122. self.SetSize((wf + dw, hf + dh))
  123. def _onToggleTool(self, id):
  124. if self._toolSwitcher.IsToolInGroup(id, "mouseUse"):
  125. self.GetWindow().UnregisterAllHandlers()
  126. def OnSize(self, event):
  127. """Adjust statusbar on changing size"""
  128. # reposition checkbox in statusbar
  129. self.StatusbarReposition()
  130. # update statusbar
  131. self.StatusbarUpdate()
  132. def OnFullScreen(self, event):
  133. """!Switch fullscreen mode, hides also toolbars"""
  134. for toolbar in self.toolbars:
  135. self._mgr.GetPane(self.toolbars[toolbar]).Show(self.IsFullScreen())
  136. self._mgr.Update()
  137. self.ShowFullScreen(not self.IsFullScreen())
  138. event.Skip()
  139. def OnCloseWindow(self, event):
  140. self.Destroy()
  141. def GetToolSwitcher(self):
  142. return self._toolSwitcher
  143. def SetProperty(self, name, value):
  144. """Sets property"""
  145. self.statusbarManager.SetProperty(name, value)
  146. def GetProperty(self, name):
  147. """Returns property"""
  148. return self.statusbarManager.GetProperty(name)
  149. def HasProperty(self, name):
  150. """Checks whether object has property"""
  151. return self.statusbarManager.HasProperty(name)
  152. def GetPPM(self):
  153. """Get pixel per meter
  154. .. todo::
  155. now computed every time, is it necessary?
  156. .. todo::
  157. enable user to specify ppm (and store it in UserSettings)
  158. """
  159. # TODO: need to be fixed...
  160. # screen X region problem
  161. # user should specify ppm
  162. dc = wx.ScreenDC()
  163. dpSizePx = wx.DisplaySize() # display size in pixels
  164. dpSizeMM = wx.DisplaySizeMM() # display size in mm (system)
  165. dpSizeIn = (dpSizeMM[0] / 25.4, dpSizeMM[1] / 25.4) # inches
  166. sysPpi = dc.GetPPI()
  167. comPpi = (dpSizePx[0] / dpSizeIn[0], dpSizePx[1] / dpSizeIn[1])
  168. ppi = comPpi # pixel per inch
  169. ppm = ((ppi[0] / 2.54) * 100, (ppi[1] / 2.54) * 100) # pixel per meter
  170. Debug.msg(
  171. 4,
  172. "MapFrameBase.GetPPM(): size: px=%d,%d mm=%f,%f "
  173. "in=%f,%f ppi: sys=%d,%d com=%d,%d; ppm=%f,%f"
  174. % (
  175. dpSizePx[0],
  176. dpSizePx[1],
  177. dpSizeMM[0],
  178. dpSizeMM[1],
  179. dpSizeIn[0],
  180. dpSizeIn[1],
  181. sysPpi[0],
  182. sysPpi[1],
  183. comPpi[0],
  184. comPpi[1],
  185. ppm[0],
  186. ppm[1],
  187. ),
  188. )
  189. return ppm
  190. def SetMapScale(self, value, map=None):
  191. """Set current map scale
  192. :param value: scale value (n if scale is 1:n)
  193. :param map: Map instance (if none self.Map is used)
  194. """
  195. if not map:
  196. map = self.Map
  197. region = self.Map.region
  198. dEW = value * (region["cols"] / self.GetPPM()[0])
  199. dNS = value * (region["rows"] / self.GetPPM()[1])
  200. region["n"] = region["center_northing"] + dNS / 2.0
  201. region["s"] = region["center_northing"] - dNS / 2.0
  202. region["w"] = region["center_easting"] - dEW / 2.0
  203. region["e"] = region["center_easting"] + dEW / 2.0
  204. # add to zoom history
  205. self.GetWindow().ZoomHistory(region["n"], region["s"], region["e"], region["w"])
  206. def GetMapScale(self, map=None):
  207. """Get current map scale
  208. :param map: Map instance (if none self.Map is used)
  209. """
  210. if not map:
  211. map = self.GetMap()
  212. region = map.region
  213. ppm = self.GetPPM()
  214. heightCm = region["rows"] / ppm[1] * 100
  215. widthCm = region["cols"] / ppm[0] * 100
  216. Debug.msg(
  217. 4, "MapFrame.GetMapScale(): width_cm=%f, height_cm=%f" % (widthCm, heightCm)
  218. )
  219. xscale = (region["e"] - region["w"]) / (region["cols"] / ppm[0])
  220. yscale = (region["n"] - region["s"]) / (region["rows"] / ppm[1])
  221. scale = (xscale + yscale) / 2.0
  222. Debug.msg(
  223. 3,
  224. "MapFrame.GetMapScale(): xscale=%f, yscale=%f -> scale=%f"
  225. % (xscale, yscale, scale),
  226. )
  227. return scale
  228. def GetProgressBar(self):
  229. """Returns progress bar
  230. Progress bar can be used by other classes.
  231. """
  232. return self.statusbarManager.GetProgressBar()
  233. def GetMap(self):
  234. """Returns current map (renderer) instance"""
  235. raise NotImplementedError("GetMap")
  236. def GetWindow(self):
  237. """Returns current map window"""
  238. raise NotImplementedError("GetWindow")
  239. def GetWindows(self):
  240. """Returns list of map windows"""
  241. raise NotImplementedError("GetWindows")
  242. def GetMapToolbar(self):
  243. """Returns toolbar with zooming tools"""
  244. raise NotImplementedError("GetMapToolbar")
  245. def GetToolbar(self, name):
  246. """Returns toolbar if exists and is active, else None."""
  247. if name in self.toolbars and self.toolbars[name].IsShown():
  248. return self.toolbars[name]
  249. return None
  250. def StatusbarUpdate(self):
  251. """Update statusbar content"""
  252. if self.statusbarManager:
  253. Debug.msg(5, "MapFrameBase.StatusbarUpdate()")
  254. self.statusbarManager.Update()
  255. def IsAutoRendered(self):
  256. """Check if auto-rendering is enabled"""
  257. # TODO: this is now not the right place to access this attribute
  258. # TODO: add mapWindowProperties to init parameters
  259. # and pass the right object in the init of derived class?
  260. # or do not use this method at all, let mapwindow decide
  261. return self.mapWindowProperties.autoRender
  262. def CoordinatesChanged(self):
  263. """Shows current coordinates on statusbar."""
  264. # assuming that the first mode is coordinates
  265. # probably shold not be here but good solution is not available now
  266. if self.statusbarManager:
  267. if self.statusbarManager.GetMode() == 0:
  268. self.statusbarManager.ShowItem("coordinates")
  269. def CreateStatusbar(self, statusbarItems):
  270. """Create statusbar (default items)."""
  271. # create statusbar and its manager
  272. statusbar = wx.StatusBar(self, id=wx.ID_ANY)
  273. statusbar.SetMinHeight(24)
  274. statusbar.SetFieldsCount(4)
  275. statusbar.SetStatusWidths([-5, -2, -1, -1])
  276. self.statusbarManager = sb.SbManager(mapframe=self, statusbar=statusbar)
  277. # fill statusbar manager
  278. self.statusbarManager.AddStatusbarItemsByClass(
  279. statusbarItems, mapframe=self, statusbar=statusbar
  280. )
  281. self.statusbarManager.AddStatusbarItem(
  282. sb.SbMask(self, statusbar=statusbar, position=2)
  283. )
  284. self.statusbarManager.AddStatusbarItem(
  285. sb.SbRender(self, statusbar=statusbar, position=3)
  286. )
  287. self.statusbarManager.Update()
  288. return statusbar
  289. def AddStatusbarPane(self):
  290. """Add statusbar as a pane"""
  291. self._mgr.AddPane(
  292. self.statusbar,
  293. wx.aui.AuiPaneInfo()
  294. .Bottom()
  295. .MinSize(30, 30)
  296. .Fixed()
  297. .Name("statusbar")
  298. .CloseButton(False)
  299. .DestroyOnClose(True)
  300. .ToolbarPane()
  301. .Dockable(False)
  302. .PaneBorder(False)
  303. .Gripper(False),
  304. )
  305. def SetStatusText(self, *args):
  306. """Overide wx.StatusBar method"""
  307. self.statusbar.SetStatusText(*args)
  308. def ShowStatusbar(self, show):
  309. """Show/hide statusbar and associated pane"""
  310. self._mgr.GetPane("statusbar").Show(show)
  311. self._mgr.Update()
  312. def IsStatusbarShown(self):
  313. """Check if statusbar is shown"""
  314. return self._mgr.GetPane("statusbar").IsShown()
  315. def StatusbarReposition(self):
  316. """Reposition items in statusbar"""
  317. if self.statusbarManager:
  318. self.statusbarManager.Reposition()
  319. def StatusbarEnableLongHelp(self, enable=True):
  320. """Enable/disable toolbars long help"""
  321. for toolbar in six.itervalues(self.toolbars):
  322. toolbar.EnableLongHelp(enable)
  323. def ShowAllToolbars(self, show=True):
  324. if not show: # hide
  325. action = self.RemoveToolbar
  326. else:
  327. action = self.AddToolbar
  328. for toolbar in self.GetToolbarNames():
  329. action(toolbar)
  330. def AreAllToolbarsShown(self):
  331. return self.GetMapToolbar().IsShown()
  332. def GetToolbarNames(self):
  333. """Return toolbar names"""
  334. return list(self.toolbars.keys())
  335. def AddToolbar(self):
  336. """Add defined toolbar to the window"""
  337. raise NotImplementedError("AddToolbar")
  338. def RemoveToolbar(self, name, destroy=False):
  339. """Removes defined toolbar from the window
  340. :param name toolbar to remove
  341. :param destroy True to destroy otherwise toolbar is only hidden
  342. """
  343. self._mgr.DetachPane(self.toolbars[name])
  344. if destroy:
  345. self._toolSwitcher.RemoveToolbarFromGroup("mouseUse", self.toolbars[name])
  346. self.toolbars[name].Destroy()
  347. self.toolbars.pop(name)
  348. else:
  349. self.toolbars[name].Hide()
  350. self._mgr.Update()
  351. def IsPaneShown(self, name):
  352. """Check if pane (toolbar, mapWindow ...) of given name is currently shown"""
  353. if self._mgr.GetPane(name).IsOk():
  354. return self._mgr.GetPane(name).IsShown()
  355. return False
  356. def OnRender(self, event):
  357. """Re-render map composition (each map layer)"""
  358. raise NotImplementedError("OnRender")
  359. def OnDraw(self, event):
  360. """Re-display current map composition"""
  361. self.MapWindow.UpdateMap(render=False)
  362. def OnErase(self, event):
  363. """Erase the canvas"""
  364. self.MapWindow.EraseMap()
  365. def OnZoomIn(self, event):
  366. """Zoom in the map."""
  367. self.MapWindow.SetModeZoomIn()
  368. def OnZoomOut(self, event):
  369. """Zoom out the map."""
  370. self.MapWindow.SetModeZoomOut()
  371. def _setUpMapWindow(self, mapWindow):
  372. """Binds map windows' zoom history signals to map toolbar."""
  373. # enable or disable zoom history tool
  374. if self.GetMapToolbar():
  375. mapWindow.zoomHistoryAvailable.connect(
  376. lambda: self.GetMapToolbar().Enable("zoomBack", enable=True)
  377. )
  378. mapWindow.zoomHistoryUnavailable.connect(
  379. lambda: self.GetMapToolbar().Enable("zoomBack", enable=False)
  380. )
  381. mapWindow.mouseMoving.connect(self.CoordinatesChanged)
  382. def OnPointer(self, event):
  383. """Sets mouse mode to pointer."""
  384. self.MapWindow.SetModePointer()
  385. def OnPan(self, event):
  386. """Panning, set mouse to drag"""
  387. self.MapWindow.SetModePan()
  388. def OnZoomBack(self, event):
  389. """Zoom last (previously stored position)"""
  390. self.MapWindow.ZoomBack()
  391. def OnZoomToMap(self, event):
  392. """
  393. Set display extents to match selected raster (including NULLs)
  394. or vector map.
  395. """
  396. self.MapWindow.ZoomToMap(layers=self.Map.GetListOfLayers())
  397. def OnZoomToWind(self, event):
  398. """Set display geometry to match computational region
  399. settings (set with g.region)
  400. """
  401. self.MapWindow.ZoomToWind()
  402. def OnZoomToDefault(self, event):
  403. """Set display geometry to match default region settings"""
  404. self.MapWindow.ZoomToDefault()
  405. class SingleMapFrame(MapFrameBase):
  406. """Frame with one map window.
  407. It is base class for frames which needs only one map.
  408. Derived class should have \c self.MapWindow or
  409. it has to override GetWindow() methods.
  410. @note To access maps use getters only
  411. (when using class or when writing class itself).
  412. """
  413. def __init__(
  414. self,
  415. parent=None,
  416. giface=None,
  417. id=wx.ID_ANY,
  418. title="",
  419. style=wx.DEFAULT_FRAME_STYLE,
  420. Map=None,
  421. auimgr=None,
  422. name="",
  423. **kwargs,
  424. ):
  425. """
  426. :param parent: gui parent
  427. :param id: wx id
  428. :param title: window title
  429. :param style: \c wx.Frame style
  430. :param map: instance of render.Map
  431. :param name: frame name
  432. :param kwargs: arguments passed to MapFrameBase
  433. """
  434. MapFrameBase.__init__(
  435. self,
  436. parent=parent,
  437. id=id,
  438. title=title,
  439. style=style,
  440. auimgr=auimgr,
  441. name=name,
  442. **kwargs,
  443. )
  444. self.Map = Map # instance of render.Map
  445. #
  446. # initialize region values
  447. #
  448. if self.Map:
  449. self._initMap(Map=self.Map)
  450. def GetMap(self):
  451. """Returns map (renderer) instance"""
  452. return self.Map
  453. def GetWindow(self):
  454. """Returns map window"""
  455. return self.MapWindow
  456. def GetWindows(self):
  457. """Returns list of map windows"""
  458. return [self.MapWindow]
  459. def OnRender(self, event):
  460. """Re-render map composition (each map layer)"""
  461. self.GetWindow().UpdateMap(render=True, renderVector=True)
  462. # update statusbar
  463. self.StatusbarUpdate()
  464. class DoubleMapFrame(MapFrameBase):
  465. """Frame with two map windows.
  466. It is base class for frames which needs two maps.
  467. There is no primary and secondary map. Both maps are equal.
  468. However, one map is current.
  469. It is expected that derived class will call _bindWindowsActivation()
  470. when both map windows will be initialized.
  471. Drived class should have method GetMapToolbar() returns toolbar
  472. which has methods SetActiveMap() and Enable().
  473. @note To access maps use getters only
  474. (when using class or when writing class itself).
  475. .. todo:
  476. Use it in GCP manager (probably changes to both DoubleMapFrame
  477. and GCP MapFrame will be necessary).
  478. """
  479. def __init__(
  480. self,
  481. parent=None,
  482. id=wx.ID_ANY,
  483. title=None,
  484. style=wx.DEFAULT_FRAME_STYLE,
  485. firstMap=None,
  486. secondMap=None,
  487. auimgr=None,
  488. name=None,
  489. **kwargs,
  490. ):
  491. """
  492. \a firstMap is set as active (by assign it to \c self.Map).
  493. Derived class should assging to \c self.MapWindow to make one
  494. map window current by dafault.
  495. :param parent: gui parent
  496. :param id: wx id
  497. :param title: window title
  498. :param style: \c wx.Frame style
  499. :param name: frame name
  500. :param kwargs: arguments passed to MapFrameBase
  501. """
  502. MapFrameBase.__init__(
  503. self,
  504. parent=parent,
  505. id=id,
  506. title=title,
  507. style=style,
  508. auimgr=auimgr,
  509. name=name,
  510. **kwargs,
  511. )
  512. self.firstMap = firstMap
  513. self.secondMap = secondMap
  514. self.Map = firstMap
  515. #
  516. # initialize region values
  517. #
  518. self._initMap(Map=self.firstMap)
  519. self._initMap(Map=self.secondMap)
  520. self._bindRegions = False
  521. def _bindWindowsActivation(self):
  522. self.GetFirstWindow().Bind(wx.EVT_ENTER_WINDOW, self.ActivateFirstMap)
  523. self.GetSecondWindow().Bind(wx.EVT_ENTER_WINDOW, self.ActivateSecondMap)
  524. def _onToggleTool(self, id):
  525. if self._toolSwitcher.IsToolInGroup(id, "mouseUse"):
  526. self.GetFirstWindow().UnregisterAllHandlers()
  527. self.GetSecondWindow().UnregisterAllHandlers()
  528. def GetFirstMap(self):
  529. """Returns first Map instance"""
  530. return self.firstMap
  531. def GetSecondMap(self):
  532. """Returns second Map instance"""
  533. return self.secondMap
  534. def GetFirstWindow(self):
  535. """Get first map window"""
  536. return self.firstMapWindow
  537. def GetSecondWindow(self):
  538. """Get second map window"""
  539. return self.secondMapWindow
  540. def GetMap(self):
  541. """Returns current map (renderer) instance
  542. @note Use this method to access current map renderer.
  543. (It is not guarented that current map will be stored in
  544. \c self.Map in future versions.)
  545. """
  546. return self.Map
  547. def GetWindow(self):
  548. """Returns current map window
  549. :func:`GetMap()`
  550. """
  551. return self.MapWindow
  552. def GetWindows(self):
  553. """Return list of all windows"""
  554. return [self.firstMapWindow, self.secondMapWindow]
  555. def ActivateFirstMap(self, event=None):
  556. """Make first Map and MapWindow active and (un)bind regions of the two Maps."""
  557. if self.MapWindow == self.firstMapWindow:
  558. return
  559. self.Map = self.firstMap
  560. self.MapWindow = self.firstMapWindow
  561. self.GetMapToolbar().SetActiveMap(0)
  562. # bind/unbind regions
  563. if self._bindRegions:
  564. self.firstMapWindow.zoomChanged.connect(self.OnZoomChangedFirstMap)
  565. self.secondMapWindow.zoomChanged.disconnect(self.OnZoomChangedSecondMap)
  566. def ActivateSecondMap(self, event=None):
  567. """Make second Map and MapWindow active and (un)bind regions of the two Maps."""
  568. if self.MapWindow == self.secondMapWindow:
  569. return
  570. self.Map = self.secondMap
  571. self.MapWindow = self.secondMapWindow
  572. self.GetMapToolbar().SetActiveMap(1)
  573. if self._bindRegions:
  574. self.secondMapWindow.zoomChanged.connect(self.OnZoomChangedSecondMap)
  575. self.firstMapWindow.zoomChanged.disconnect(self.OnZoomChangedFirstMap)
  576. def SetBindRegions(self, on):
  577. """Set or unset binding display regions."""
  578. self._bindRegions = on
  579. if on:
  580. if self.MapWindow == self.firstMapWindow:
  581. self.firstMapWindow.zoomChanged.connect(self.OnZoomChangedFirstMap)
  582. else:
  583. self.secondMapWindow.zoomChanged.connect(self.OnZoomChangedSecondMap)
  584. else:
  585. if self.MapWindow == self.firstMapWindow:
  586. self.firstMapWindow.zoomChanged.disconnect(self.OnZoomChangedFirstMap)
  587. else:
  588. self.secondMapWindow.zoomChanged.disconnect(self.OnZoomChangedSecondMap)
  589. def OnZoomChangedFirstMap(self):
  590. """Display region of the first window (Map) changed.
  591. Synchronize the region of the second map and re-render it.
  592. This is the default implementation which can be overridden.
  593. """
  594. region = self.GetFirstMap().GetCurrentRegion()
  595. self.GetSecondMap().region.update(region)
  596. self.Render(mapToRender=self.GetSecondWindow())
  597. def OnZoomChangedSecondMap(self):
  598. """Display region of the second window (Map) changed.
  599. Synchronize the region of the second map and re-render it.
  600. This is the default implementation which can be overridden.
  601. """
  602. region = self.GetSecondMap().GetCurrentRegion()
  603. self.GetFirstMap().region.update(region)
  604. self.Render(mapToRender=self.GetFirstWindow())
  605. def OnZoomIn(self, event):
  606. """Zoom in the map."""
  607. self.GetFirstWindow().SetModeZoomIn()
  608. self.GetSecondWindow().SetModeZoomIn()
  609. def OnZoomOut(self, event):
  610. """Zoom out the map."""
  611. self.GetFirstWindow().SetModeZoomOut()
  612. self.GetSecondWindow().SetModeZoomOut()
  613. def OnPan(self, event):
  614. """Panning, set mouse to pan"""
  615. self.GetFirstWindow().SetModePan()
  616. self.GetSecondWindow().SetModePan()
  617. def OnPointer(self, event):
  618. """Set pointer mode (dragging overlays)"""
  619. self.GetFirstWindow().SetModePointer()
  620. self.GetSecondWindow().SetModePointer()
  621. def OnQuery(self, event):
  622. """Set query mode"""
  623. self.GetFirstWindow().SetModeQuery()
  624. self.GetSecondWindow().SetModeQuery()
  625. def OnRender(self, event):
  626. """Re-render map composition (each map layer)"""
  627. self.Render(mapToRender=self.GetFirstWindow())
  628. self.Render(mapToRender=self.GetSecondWindow())
  629. def Render(self, mapToRender):
  630. """Re-render map composition"""
  631. mapToRender.UpdateMap(
  632. render=True, renderVector=mapToRender == self.GetFirstWindow()
  633. )
  634. # update statusbar
  635. self.StatusbarUpdate()
  636. def OnErase(self, event):
  637. """Erase the canvas"""
  638. self.Erase(mapToErase=self.GetFirstWindow())
  639. self.Erase(mapToErase=self.GetSecondWindow())
  640. def Erase(self, mapToErase):
  641. """Erase the canvas"""
  642. mapToErase.EraseMap()
  643. def OnDraw(self, event):
  644. """Re-display current map composition"""
  645. self.Draw(mapToDraw=self.GetFirstWindow())
  646. self.Draw(mapToDraw=self.GetSecondWindow())
  647. def Draw(self, mapToDraw):
  648. """Re-display current map composition"""
  649. mapToDraw.UpdateMap(render=False)