toolbars.py 10 KB

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