toolbars.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 = _('Show/hide scale bar')),
  28. 'addLegend' : MetaIcon(img = 'legend-add',
  29. label = _('Show/hide legend')),
  30. 'addNorthArrow': MetaIcon(img = 'north-arrow-add',
  31. label = _('Show/hide 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 layer')),
  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(img = '3d-rotate',
  52. label = _('Rotate 3D scene'),
  53. desc = _('Drag with mouse to rotate 3D scene')),
  54. 'flyThrough': MetaIcon(img = 'flythrough',
  55. label = _('Fly-through mode'),
  56. desc = _('Drag with mouse, hold Ctrl down for different mode'
  57. ' or Shift to accelerate')),
  58. 'zoomIn' : BaseIcons['zoomIn'].SetLabel(desc = _('Click mouse to zoom')),
  59. 'zoomOut' : BaseIcons['zoomOut'].SetLabel(desc = _('Click mouse to unzoom'))
  60. }
  61. class MapToolbar(BaseToolbar):
  62. """Map Display toolbar
  63. """
  64. def __init__(self, parent, toolSwitcher):
  65. """Map Display constructor
  66. :param parent: reference to MapFrame
  67. """
  68. BaseToolbar.__init__(self, parent=parent, toolSwitcher=toolSwitcher) # MapFrame
  69. self.InitToolbar(self._toolbarData())
  70. self._default = self.pointer
  71. # optional tools
  72. toolNum = 0
  73. choices = [ _('2D view'), ]
  74. self.toolId = { '2d' : toolNum }
  75. toolNum += 1
  76. if self.parent.GetLayerManager():
  77. log = self.parent.GetLayerManager().GetLogWindow()
  78. if haveNviz:
  79. choices.append(_('3D view'))
  80. self.toolId['3d'] = toolNum
  81. toolNum += 1
  82. else:
  83. from nviz.main import errorMsg
  84. if self.parent.GetLayerManager():
  85. log.WriteCmdLog(_('3D view mode not available'))
  86. log.WriteWarning(_('Reason: %s') % str(errorMsg))
  87. self.toolId['3d'] = -1
  88. if haveVDigit:
  89. choices.append(_("Vector digitizer"))
  90. self.toolId['vdigit'] = toolNum
  91. toolNum += 1
  92. else:
  93. from vdigit.main import errorMsg
  94. if self.parent.GetLayerManager():
  95. log.WriteCmdLog(_('Vector digitizer not available'))
  96. log.WriteWarning(_('Reason: %s') % errorMsg)
  97. log.WriteLog(_('Note that the wxGUI\'s vector digitizer is currently disabled '
  98. '(hopefully this will be fixed soon). '
  99. 'Please keep an eye out for updated versions of GRASS. '
  100. 'In the meantime you can use "v.digit" from the Develop Vector menu.'), wrap = 60)
  101. self.toolId['vdigit'] = -1
  102. choices.append(_("Raster digitizer"))
  103. self.toolId['rdigit'] = toolNum
  104. self.combo = wx.ComboBox(parent = self, id = wx.ID_ANY,
  105. choices = choices,
  106. style = wx.CB_READONLY, size = (110, -1))
  107. self.combo.SetSelection(0)
  108. self.comboid = self.AddControl(self.combo)
  109. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  110. # realize the toolbar
  111. self.Realize()
  112. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  113. self.combo.Hide()
  114. self.combo.Show()
  115. for tool in (self.pointer, self.select, self.query, self.pan, self.zoomIn, self.zoomOut):
  116. self.toolSwitcher.AddToolToGroup(group='mouseUse', toolbar=self, tool=tool)
  117. self.EnableTool(self.zoomBack, False)
  118. self.FixSize(width = 90)
  119. def _toolbarData(self):
  120. """Toolbar data"""
  121. return self._getToolbarData((
  122. #('displayMap', BaseIcons['display'],
  123. #self.parent.OnDraw),
  124. ('renderMap', BaseIcons['render'],
  125. self.parent.OnRender),
  126. ('erase', BaseIcons['erase'],
  127. self.parent.OnErase),
  128. ('pointer', BaseIcons['pointer'],
  129. self.parent.OnPointer,
  130. wx.ITEM_CHECK),
  131. ('select', MapIcons['select'],
  132. self.parent.OnSelect,
  133. wx.ITEM_CHECK),
  134. ('query', MapIcons['query'],
  135. self.parent.OnQuery,
  136. wx.ITEM_CHECK),
  137. ('pan', BaseIcons['pan'],
  138. self.parent.OnPan,
  139. wx.ITEM_CHECK),
  140. ('zoomIn', BaseIcons['zoomIn'],
  141. self.parent.OnZoomIn,
  142. wx.ITEM_CHECK),
  143. ('zoomOut', BaseIcons['zoomOut'],
  144. self.parent.OnZoomOut,
  145. wx.ITEM_CHECK),
  146. ('zoomExtent', BaseIcons['zoomExtent'],
  147. self.parent.OnZoomToMap),
  148. ('zoomRegion', BaseIcons['zoomRegion'],
  149. self.parent.OnZoomToWind),
  150. ('zoomBack', BaseIcons['zoomBack'],
  151. self.parent.OnZoomBack),
  152. ('zoomMenu', BaseIcons['zoomMenu'],
  153. self.parent.OnZoomMenu),
  154. ('analyze', MapIcons['analyze'],
  155. self.OnAnalyze),
  156. ('overlay', BaseIcons['overlay'],
  157. self.OnDecoration),
  158. ('saveFile', BaseIcons['saveFile'],
  159. self.parent.SaveToFile),
  160. ('printMap', BaseIcons['print'],
  161. self.parent.PrintMenu),
  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. """
  193. tool = event.GetSelection()
  194. if tool == self.toolId['2d']:
  195. self.ExitToolbars()
  196. self.Enable2D(True)
  197. elif tool == self.toolId['3d'] and \
  198. not (self.parent.MapWindow3D and self.parent.IsPaneShown('3d')):
  199. self.ExitToolbars()
  200. self.parent.AddNviz()
  201. elif tool == self.toolId['vdigit'] and \
  202. not self.parent.GetToolbar('vdigit'):
  203. self.ExitToolbars()
  204. self.parent.AddToolbar("vdigit")
  205. self.parent.MapWindow.SetFocus()
  206. elif tool == self.toolId['rdigit']:
  207. self.ExitToolbars()
  208. self.parent.AddRDigit()
  209. def OnAnalyze(self, event):
  210. """Analysis tools menu
  211. """
  212. self._onMenu(((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. def OnDecoration(self, event):
  220. """Decorations overlay menu
  221. """
  222. self._onMenu(((MapIcons["addLegend"], lambda evt: self.parent.AddLegend()),
  223. (MapIcons["addBarscale"], lambda evt: self.parent.AddBarscale()),
  224. (MapIcons["addNorthArrow"], lambda evt: self.parent.AddArrow()),
  225. (MapIcons["addText"], self.parent.OnAddText)))
  226. def ExitToolbars(self):
  227. if self.parent.GetToolbar('vdigit'):
  228. self.parent.toolbars['vdigit'].OnExit()
  229. if self.parent.GetLayerManager() and \
  230. self.parent.GetLayerManager().IsPaneShown('toolbarNviz'):
  231. self.parent.RemoveNviz()
  232. if self.parent.GetToolbar('rdigit'):
  233. self.parent.QuitRDigit()
  234. def Enable2D(self, enabled):
  235. """Enable/Disable 2D display mode specific tools"""
  236. for tool in (self.zoomRegion,
  237. self.zoomMenu,
  238. self.analyze,
  239. self.printMap):
  240. self.EnableTool(tool, enabled)
  241. self.ChangeToolsDesc(enabled)
  242. if enabled:
  243. self.combo.SetValue(_("2D view"))