toolbars.py 11 KB

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