toolbars.py 11 KB

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