toolbars.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. toolNum = 0
  70. choices = [ _('2D view'), ]
  71. self.toolId = { '2d' : toolNum }
  72. toolNum += 1
  73. if self.parent.GetLayerManager():
  74. log = self.parent.GetLayerManager().GetLogWindow()
  75. if haveNviz:
  76. choices.append(_('3D view'))
  77. self.toolId['3d'] = toolNum
  78. toolNum += 1
  79. else:
  80. from nviz.main import errorMsg
  81. if self.parent.GetLayerManager():
  82. log.WriteCmdLog(_('3D view mode not available'))
  83. log.WriteWarning(_('Reason: %s') % str(errorMsg))
  84. self.toolId['3d'] = -1
  85. if haveVDigit:
  86. choices.append(_("Vector digitizer"))
  87. self.toolId['vdigit'] = toolNum
  88. toolNum += 1
  89. else:
  90. from vdigit.main import errorMsg
  91. if self.parent.GetLayerManager():
  92. log.WriteCmdLog(_('Vector digitizer not available'))
  93. log.WriteWarning(_('Reason: %s') % errorMsg)
  94. log.WriteLog(_('Note that the wxGUI\'s vector digitizer is currently disabled '
  95. '(hopefully this will be fixed soon). '
  96. 'Please keep an eye out for updated versions of GRASS. '
  97. 'In the meantime you can use "v.digit" from the Develop Vector menu.'), wrap = 60)
  98. self.toolId['vdigit'] = -1
  99. choices.append(_("Raster digitizer"))
  100. self.toolId['rdigit'] = toolNum
  101. self.combo = wx.ComboBox(parent = self, id = wx.ID_ANY,
  102. choices = choices,
  103. style = wx.CB_READONLY, size = (110, -1))
  104. self.combo.SetSelection(0)
  105. self.comboid = self.AddControl(self.combo)
  106. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  107. # realize the toolbar
  108. self.Realize()
  109. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  110. self.combo.Hide()
  111. self.combo.Show()
  112. for tool in (self.pointer, self.query, self.pan, self.zoomIn, self.zoomOut):
  113. self.toolSwitcher.AddToolToGroup(group='mouseUse', toolbar=self, tool=tool)
  114. self.EnableTool(self.zoomBack, False)
  115. self.FixSize(width = 90)
  116. def _toolbarData(self):
  117. """Toolbar data"""
  118. return self._getToolbarData((('displayMap', BaseIcons['display'],
  119. self.parent.OnDraw),
  120. ('renderMap', BaseIcons['render'],
  121. self.parent.OnRender),
  122. ('erase', BaseIcons['erase'],
  123. self.parent.OnErase),
  124. (None, ),
  125. ('pointer', BaseIcons['pointer'],
  126. self.parent.OnPointer,
  127. wx.ITEM_CHECK),
  128. ('query', MapIcons['query'],
  129. self.parent.OnQuery,
  130. wx.ITEM_CHECK),
  131. ('pan', BaseIcons['pan'],
  132. self.parent.OnPan,
  133. wx.ITEM_CHECK),
  134. ('zoomIn', BaseIcons['zoomIn'],
  135. self.parent.OnZoomIn,
  136. wx.ITEM_CHECK),
  137. ('zoomOut', BaseIcons['zoomOut'],
  138. self.parent.OnZoomOut,
  139. wx.ITEM_CHECK),
  140. ('zoomExtent', BaseIcons['zoomExtent'],
  141. self.parent.OnZoomToMap),
  142. ('zoomRegion', BaseIcons['zoomRegion'],
  143. self.parent.OnZoomToWind),
  144. ('zoomBack', BaseIcons['zoomBack'],
  145. self.parent.OnZoomBack),
  146. ('zoomMenu', BaseIcons['zoomMenu'],
  147. self.parent.OnZoomMenu),
  148. (None, ),
  149. ('analyze', MapIcons['analyze'],
  150. self.OnAnalyze),
  151. (None, ),
  152. ('overlay', BaseIcons['overlay'],
  153. self.OnDecoration),
  154. (None, ),
  155. ('saveFile', BaseIcons['saveFile'],
  156. self.parent.SaveToFile),
  157. ('printMap', BaseIcons['print'],
  158. self.parent.PrintMenu),
  159. (None, ))
  160. )
  161. def InsertTool(self, data):
  162. """Insert tool to toolbar
  163. :param data: toolbar data"""
  164. data = self._getToolbarData(data)
  165. for tool in data:
  166. self.CreateTool(*tool)
  167. self.Realize()
  168. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  169. self.parent._mgr.Update()
  170. def RemoveTool(self, tool):
  171. """Remove tool from toolbar
  172. :param tool: tool id"""
  173. self.DeleteTool(tool)
  174. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  175. self.parent._mgr.Update()
  176. def ChangeToolsDesc(self, mode2d):
  177. """Change description of zoom tools for 2D/3D view"""
  178. if mode2d:
  179. icons = BaseIcons
  180. else:
  181. icons = NvizIcons
  182. for i, data in enumerate(self._data):
  183. for tool in (('zoomIn', 'zoomOut')):
  184. if data[0] == tool:
  185. tmp = list(data)
  186. tmp[4] = icons[tool].GetDesc()
  187. self._data[i] = tuple(tmp)
  188. def OnSelectTool(self, event):
  189. """Select / enable tool available in tools list
  190. """
  191. tool = event.GetSelection()
  192. if tool == self.toolId['2d']:
  193. self.ExitToolbars()
  194. self.Enable2D(True)
  195. elif tool == self.toolId['3d'] and \
  196. not (self.parent.MapWindow3D and self.parent.IsPaneShown('3d')):
  197. self.ExitToolbars()
  198. self.parent.AddNviz()
  199. elif tool == self.toolId['vdigit'] and \
  200. not self.parent.GetToolbar('vdigit'):
  201. self.ExitToolbars()
  202. self.parent.AddToolbar("vdigit")
  203. self.parent.MapWindow.SetFocus()
  204. elif tool == self.toolId['rdigit']:
  205. self.ExitToolbars()
  206. self.parent.AddRDigit()
  207. def OnAnalyze(self, event):
  208. """Analysis tools menu
  209. """
  210. self._onMenu(((MapIcons["measureDistance"], self.parent.OnMeasureDistance),
  211. (MapIcons["measureArea"], self.parent.OnMeasureArea),
  212. (MapIcons["profile"], self.parent.OnProfile),
  213. (MapIcons["scatter"], self.parent.OnScatterplot),
  214. (MapIcons["histogram"], self.parent.OnHistogramPyPlot),
  215. (BaseIcons["histogramD"], self.parent.OnHistogram),
  216. (MapIcons["vnet"], self.parent.OnVNet)))
  217. def OnDecoration(self, event):
  218. """Decorations overlay menu
  219. """
  220. self._onMenu(((MapIcons["addLegend"], lambda evt: self.parent.AddLegend()),
  221. (MapIcons["addBarscale"], lambda evt: self.parent.AddBarscale()),
  222. (MapIcons["addNorthArrow"], lambda evt: self.parent.AddArrow()),
  223. (MapIcons["addText"], self.parent.OnAddText)))
  224. def ExitToolbars(self):
  225. if self.parent.GetToolbar('vdigit'):
  226. self.parent.toolbars['vdigit'].OnExit()
  227. if self.parent.GetLayerManager() and \
  228. self.parent.GetLayerManager().IsPaneShown('toolbarNviz'):
  229. self.parent.RemoveNviz()
  230. if self.parent.GetToolbar('rdigit'):
  231. self.parent.QuitRDigit()
  232. def Enable2D(self, enabled):
  233. """Enable/Disable 2D display mode specific tools"""
  234. for tool in (self.zoomRegion,
  235. self.zoomMenu,
  236. self.analyze,
  237. self.printMap):
  238. self.EnableTool(tool, enabled)
  239. self.ChangeToolsDesc(enabled)
  240. if enabled:
  241. self.combo.SetValue(_("2D view"))