toolbars.py 11 KB

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