toolbars.py 11 KB

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