toolbars.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. """
  2. @package mapdisp.toolbars
  3. @brief Map display frame - toolbars
  4. Classes:
  5. - toolbars::MapToolbar
  6. (C) 2007-2015 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Michael Barton
  10. @author Jachym Cepicky
  11. @author Martin Landa <landa.martin gmail.com>
  12. """
  13. import wx
  14. from gui_core.toolbars import BaseToolbar, BaseIcons
  15. from nviz.main import haveNviz
  16. from vdigit.main import haveVDigit
  17. from icons.icon import MetaIcon
  18. MapIcons = {
  19. "query": MetaIcon(
  20. img="info",
  21. label=_("Query raster/vector map(s)"),
  22. desc=_("Query selected raster/vector map(s)"),
  23. ),
  24. "select": MetaIcon(
  25. img="select",
  26. label=_("Select vector feature(s)"),
  27. desc=_("Select features interactively from vector map"),
  28. ),
  29. "addBarscale": MetaIcon(img="scalebar-add", label=_("Add scale bar")),
  30. "addRasterLegend": MetaIcon(img="legend-add", label=_("Add raster legend")),
  31. "addVectorLegend": MetaIcon(img="legend-add", label=_("Add vector legend")),
  32. "addNorthArrow": MetaIcon(img="north-arrow-add", label=_("Add north arrow")),
  33. "analyze": MetaIcon(
  34. img="layer-raster-analyze",
  35. label=_("Analyze map"),
  36. desc=_("Measuring, profiling, histogramming, ..."),
  37. ),
  38. "measureDistance": MetaIcon(img="measure-length", label=_("Measure distance")),
  39. "measureArea": MetaIcon(img="area-measure", label=_("Measure area")),
  40. "profile": MetaIcon(img="layer-raster-profile", label=_("Profile surface map")),
  41. "scatter": MetaIcon(
  42. img="layer-raster-profile",
  43. label=_("Create bivariate scatterplot of raster maps"),
  44. ),
  45. "addText": MetaIcon(img="text-add", label=_("Add text")),
  46. "histogram": MetaIcon(
  47. img="layer-raster-histogram", label=_("Create histogram of raster map")
  48. ),
  49. "vnet": MetaIcon(img="vector-tools", label=_("Vector network analysis tool")),
  50. }
  51. NvizIcons = {
  52. "rotate": MetaIcon(
  53. img="3d-rotate",
  54. label=_("Rotate 3D scene"),
  55. desc=_("Drag with mouse to rotate 3D scene"),
  56. ),
  57. "flyThrough": MetaIcon(
  58. img="flythrough",
  59. label=_("Fly-through mode"),
  60. desc=_(
  61. "Drag with mouse, hold Ctrl down for different mode"
  62. " or Shift to accelerate"
  63. ),
  64. ),
  65. "zoomIn": BaseIcons["zoomIn"].SetLabel(desc=_("Click mouse to zoom")),
  66. "zoomOut": BaseIcons["zoomOut"].SetLabel(desc=_("Click mouse to unzoom")),
  67. }
  68. class MapToolbar(BaseToolbar):
  69. """Map Display toolbar"""
  70. def __init__(self, parent, toolSwitcher):
  71. """Map Display constructor
  72. :param parent: reference to MapFrame
  73. """
  74. BaseToolbar.__init__(self, parent=parent, toolSwitcher=toolSwitcher) # MapFrame
  75. self.InitToolbar(self._toolbarData())
  76. self._default = self.pointer
  77. # optional tools
  78. toolNum = 0
  79. choices = [
  80. _("2D view"),
  81. ]
  82. self.toolId = {"2d": toolNum}
  83. toolNum += 1
  84. if self.parent.GetLayerManager():
  85. log = self.parent.GetLayerManager().GetLogWindow()
  86. if haveNviz:
  87. choices.append(_("3D view"))
  88. self.toolId["3d"] = toolNum
  89. toolNum += 1
  90. else:
  91. from nviz.main import errorMsg
  92. if self.parent.GetLayerManager():
  93. log.WriteCmdLog(_("3D view mode not available"))
  94. log.WriteWarning(_("Reason: %s") % str(errorMsg))
  95. self.toolId["3d"] = -1
  96. if haveVDigit:
  97. choices.append(_("Vector digitizer"))
  98. self.toolId["vdigit"] = toolNum
  99. toolNum += 1
  100. else:
  101. from vdigit.main import errorMsg
  102. if self.parent.GetLayerManager():
  103. log.WriteCmdLog(_("Vector digitizer not available"))
  104. log.WriteWarning(_("Reason: %s") % errorMsg)
  105. log.WriteLog(
  106. _(
  107. "Note that the wxGUI's vector digitizer is disabled in this installation. "
  108. "Please keep an eye out for updated versions of GRASS. "
  109. 'In the meantime you can use "v.edit" for non-interactive editing '
  110. "from the Develop vector map menu."
  111. ),
  112. wrap=60,
  113. )
  114. self.toolId["vdigit"] = -1
  115. choices.append(_("Raster digitizer"))
  116. self.toolId["rdigit"] = toolNum
  117. self.combo = wx.ComboBox(
  118. parent=self,
  119. id=wx.ID_ANY,
  120. choices=choices,
  121. style=wx.CB_READONLY,
  122. size=(110, -1),
  123. )
  124. self.combo.SetSelection(0)
  125. self.comboid = self.AddControl(self.combo)
  126. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  127. # realize the toolbar
  128. self.Realize()
  129. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  130. self.combo.Hide()
  131. self.combo.Show()
  132. for tool in (
  133. self.pointer,
  134. self.select,
  135. self.query,
  136. self.pan,
  137. self.zoomIn,
  138. self.zoomOut,
  139. ):
  140. self.toolSwitcher.AddToolToGroup(group="mouseUse", toolbar=self, tool=tool)
  141. self.EnableTool(self.zoomBack, False)
  142. self.FixSize(width=90)
  143. def _toolbarData(self):
  144. """Toolbar data"""
  145. return self._getToolbarData(
  146. (
  147. ("renderMap", BaseIcons["render"], self.parent.OnRender),
  148. ("pointer", BaseIcons["pointer"], self.parent.OnPointer, wx.ITEM_CHECK),
  149. ("select", MapIcons["select"], self.parent.OnSelect, wx.ITEM_CHECK),
  150. ("query", MapIcons["query"], self.parent.OnQuery, wx.ITEM_CHECK),
  151. ("pan", BaseIcons["pan"], self.parent.OnPan, wx.ITEM_CHECK),
  152. ("zoomIn", BaseIcons["zoomIn"], self.parent.OnZoomIn, wx.ITEM_CHECK),
  153. ("zoomOut", BaseIcons["zoomOut"], self.parent.OnZoomOut, wx.ITEM_CHECK),
  154. ("zoomExtent", BaseIcons["zoomExtent"], self.parent.OnZoomToMap),
  155. ("zoomRegion", BaseIcons["zoomRegion"], self.parent.OnZoomToWind),
  156. ("zoomBack", BaseIcons["zoomBack"], self.parent.OnZoomBack),
  157. ("zoomMenu", BaseIcons["zoomMenu"], self.parent.OnZoomMenu),
  158. ("analyze", MapIcons["analyze"], self.OnAnalyze),
  159. ("overlay", BaseIcons["overlay"], self.OnDecoration),
  160. ("saveFile", BaseIcons["saveFile"], self.parent.SaveToFile),
  161. )
  162. )
  163. def InsertTool(self, data):
  164. """Insert tool to toolbar
  165. :param data: toolbar data"""
  166. data = self._getToolbarData(data)
  167. for tool in data:
  168. self.CreateTool(*tool)
  169. self.Realize()
  170. self.parent._mgr.GetPane("mapToolbar").BestSize(self.GetBestSize())
  171. self.parent._mgr.Update()
  172. def RemoveTool(self, tool):
  173. """Remove tool from toolbar
  174. :param tool: tool id"""
  175. self.DeleteTool(tool)
  176. self.parent._mgr.GetPane("mapToolbar").BestSize(self.GetBestSize())
  177. self.parent._mgr.Update()
  178. def ChangeToolsDesc(self, mode2d):
  179. """Change description of zoom tools for 2D/3D view"""
  180. if mode2d:
  181. icons = BaseIcons
  182. else:
  183. icons = NvizIcons
  184. for i, data in enumerate(self._data):
  185. for tool in ("zoomIn", "zoomOut"):
  186. if data[0] == tool:
  187. tmp = list(data)
  188. tmp[4] = icons[tool].GetDesc()
  189. self._data[i] = tuple(tmp)
  190. def OnSelectTool(self, event):
  191. """Select / enable tool available in tools list"""
  192. tool = event.GetSelection()
  193. if tool == self.toolId["2d"]:
  194. self.ExitToolbars()
  195. self.Enable2D(True)
  196. elif tool == self.toolId["3d"] and not (
  197. self.parent.MapWindow3D and self.parent.IsPaneShown("3d")
  198. ):
  199. self.ExitToolbars()
  200. self.parent.AddNviz()
  201. elif tool == self.toolId["vdigit"] and not self.parent.GetToolbar("vdigit"):
  202. self.ExitToolbars()
  203. self.parent.AddToolbar("vdigit")
  204. self.parent.MapWindow.SetFocus()
  205. elif tool == self.toolId["rdigit"]:
  206. self.ExitToolbars()
  207. self.parent.AddRDigit()
  208. def OnAnalyze(self, event):
  209. """Analysis tools menu"""
  210. self._onMenu(
  211. (
  212. (MapIcons["measureDistance"], self.parent.OnMeasureDistance),
  213. (MapIcons["measureArea"], self.parent.OnMeasureArea),
  214. (MapIcons["profile"], self.parent.OnProfile),
  215. (MapIcons["scatter"], self.parent.OnScatterplot),
  216. (MapIcons["histogram"], self.parent.OnHistogramPyPlot),
  217. (BaseIcons["histogramD"], self.parent.OnHistogram),
  218. (MapIcons["vnet"], self.parent.OnVNet),
  219. )
  220. )
  221. def OnDecoration(self, event):
  222. """Decorations overlay menu"""
  223. self._onMenu(
  224. (
  225. (MapIcons["addRasterLegend"], lambda evt: self.parent.AddLegendRast()),
  226. (MapIcons["addVectorLegend"], lambda evt: self.parent.AddLegendVect()),
  227. (MapIcons["addBarscale"], lambda evt: self.parent.AddBarscale()),
  228. (MapIcons["addNorthArrow"], lambda evt: self.parent.AddArrow()),
  229. (MapIcons["addText"], lambda evt: self.parent.AddDtext()),
  230. )
  231. )
  232. def ExitToolbars(self):
  233. if self.parent.GetToolbar("vdigit"):
  234. self.parent.toolbars["vdigit"].OnExit()
  235. if self.parent.GetLayerManager() and self.parent.GetLayerManager().IsPaneShown(
  236. "toolbarNviz"
  237. ):
  238. self.parent.RemoveNviz()
  239. if self.parent.GetToolbar("rdigit"):
  240. self.parent.QuitRDigit()
  241. def Enable2D(self, enabled):
  242. """Enable/Disable 2D display mode specific tools"""
  243. for tool in (self.zoomRegion, self.zoomMenu, self.analyze, self.select):
  244. self.EnableTool(tool, enabled)
  245. self.ChangeToolsDesc(enabled)
  246. if enabled:
  247. self.combo.SetValue(_("2D view"))