toolbars.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. (None, ),
  129. ('pointer', BaseIcons['pointer'],
  130. self.parent.OnPointer,
  131. wx.ITEM_CHECK),
  132. ('select', MapIcons['select'],
  133. self.parent.OnSelect,
  134. wx.ITEM_CHECK),
  135. ('query', MapIcons['query'],
  136. self.parent.OnQuery,
  137. wx.ITEM_CHECK),
  138. ('pan', BaseIcons['pan'],
  139. self.parent.OnPan,
  140. wx.ITEM_CHECK),
  141. ('zoomIn', BaseIcons['zoomIn'],
  142. self.parent.OnZoomIn,
  143. wx.ITEM_CHECK),
  144. ('zoomOut', BaseIcons['zoomOut'],
  145. self.parent.OnZoomOut,
  146. wx.ITEM_CHECK),
  147. ('zoomExtent', BaseIcons['zoomExtent'],
  148. self.parent.OnZoomToMap),
  149. ('zoomRegion', BaseIcons['zoomRegion'],
  150. self.parent.OnZoomToWind),
  151. ('zoomBack', BaseIcons['zoomBack'],
  152. self.parent.OnZoomBack),
  153. ('zoomMenu', BaseIcons['zoomMenu'],
  154. self.parent.OnZoomMenu),
  155. (None, ),
  156. ('analyze', MapIcons['analyze'],
  157. self.OnAnalyze),
  158. (None, ),
  159. ('overlay', BaseIcons['overlay'],
  160. self.OnDecoration),
  161. (None, ),
  162. ('saveFile', BaseIcons['saveFile'],
  163. self.parent.SaveToFile),
  164. ('printMap', BaseIcons['print'],
  165. self.parent.PrintMenu),
  166. (None, ))
  167. )
  168. def InsertTool(self, data):
  169. """Insert tool to toolbar
  170. :param data: toolbar data"""
  171. data = self._getToolbarData(data)
  172. for tool in data:
  173. self.CreateTool(*tool)
  174. self.Realize()
  175. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  176. self.parent._mgr.Update()
  177. def RemoveTool(self, tool):
  178. """Remove tool from toolbar
  179. :param tool: tool id"""
  180. self.DeleteTool(tool)
  181. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  182. self.parent._mgr.Update()
  183. def ChangeToolsDesc(self, mode2d):
  184. """Change description of zoom tools for 2D/3D view"""
  185. if mode2d:
  186. icons = BaseIcons
  187. else:
  188. icons = NvizIcons
  189. for i, data in enumerate(self._data):
  190. for tool in (('zoomIn', 'zoomOut')):
  191. if data[0] == tool:
  192. tmp = list(data)
  193. tmp[4] = icons[tool].GetDesc()
  194. self._data[i] = tuple(tmp)
  195. def OnSelectTool(self, event):
  196. """Select / enable tool available in tools list
  197. """
  198. tool = event.GetSelection()
  199. if tool == self.toolId['2d']:
  200. self.ExitToolbars()
  201. self.Enable2D(True)
  202. elif tool == self.toolId['3d'] and \
  203. not (self.parent.MapWindow3D and self.parent.IsPaneShown('3d')):
  204. self.ExitToolbars()
  205. self.parent.AddNviz()
  206. elif tool == self.toolId['vdigit'] and \
  207. not self.parent.GetToolbar('vdigit'):
  208. self.ExitToolbars()
  209. self.parent.AddToolbar("vdigit")
  210. self.parent.MapWindow.SetFocus()
  211. elif tool == self.toolId['rdigit']:
  212. self.ExitToolbars()
  213. self.parent.AddRDigit()
  214. def OnAnalyze(self, event):
  215. """Analysis tools menu
  216. """
  217. self._onMenu(((MapIcons["measureDistance"], self.parent.OnMeasureDistance),
  218. (MapIcons["measureArea"], self.parent.OnMeasureArea),
  219. (MapIcons["profile"], self.parent.OnProfile),
  220. (MapIcons["scatter"], self.parent.OnScatterplot),
  221. (MapIcons["histogram"], self.parent.OnHistogramPyPlot),
  222. (BaseIcons["histogramD"], self.parent.OnHistogram),
  223. (MapIcons["vnet"], self.parent.OnVNet)))
  224. def OnDecoration(self, event):
  225. """Decorations overlay menu
  226. """
  227. self._onMenu(((MapIcons["addLegend"], lambda evt: self.parent.AddLegend()),
  228. (MapIcons["addBarscale"], lambda evt: self.parent.AddBarscale()),
  229. (MapIcons["addNorthArrow"], lambda evt: self.parent.AddArrow()),
  230. (MapIcons["addText"], self.parent.OnAddText)))
  231. def ExitToolbars(self):
  232. if self.parent.GetToolbar('vdigit'):
  233. self.parent.toolbars['vdigit'].OnExit()
  234. if self.parent.GetLayerManager() and \
  235. self.parent.GetLayerManager().IsPaneShown('toolbarNviz'):
  236. self.parent.RemoveNviz()
  237. if self.parent.GetToolbar('rdigit'):
  238. self.parent.QuitRDigit()
  239. def Enable2D(self, enabled):
  240. """Enable/Disable 2D display mode specific tools"""
  241. for tool in (self.zoomRegion,
  242. self.zoomMenu,
  243. self.analyze,
  244. self.printMap):
  245. self.EnableTool(tool, enabled)
  246. self.ChangeToolsDesc(enabled)
  247. if enabled:
  248. self.combo.SetValue(_("2D view"))