toolbars.py 11 KB

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