toolbars.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 = _('Add scalebar and north arrow')),
  25. 'addLegend' : MetaIcon(img = 'legend-add',
  26. label = _('Add legend')),
  27. 'addNorthArrow': MetaIcon(img = 'north-arrow-add',
  28. label = _('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, mapcontent):
  60. """!Map Display constructor
  61. @param parent reference to MapFrame
  62. @param mapcontent reference to render.Map (registred by MapFrame)
  63. """
  64. self.mapcontent = mapcontent # render.Map
  65. BaseToolbar.__init__(self, parent = parent) # MapFrame
  66. self.InitToolbar(self._toolbarData())
  67. # optional tools
  68. choices = [ _('2D view'), ]
  69. self.toolId = { '2d' : 0 }
  70. if self.parent.GetLayerManager():
  71. log = self.parent.GetLayerManager().GetLogWindow()
  72. if haveNviz:
  73. choices.append(_('3D view'))
  74. self.toolId['3d'] = 1
  75. else:
  76. from nviz.main import errorMsg
  77. if self.parent.GetLayerManager():
  78. log.WriteCmdLog(_('3D view mode not available'))
  79. log.WriteWarning(_('Reason: %s') % str(errorMsg))
  80. self.toolId['3d'] = -1
  81. if haveVDigit:
  82. choices.append(_('Digitize'))
  83. if self.toolId['3d'] > -1:
  84. self.toolId['vdigit'] = 2
  85. else:
  86. self.toolId['vdigit'] = 1
  87. else:
  88. from vdigit.main import errorMsg
  89. if self.parent.GetLayerManager():
  90. log.WriteCmdLog(_('Vector digitizer not available'))
  91. log.WriteWarning(_('Reason: %s') % errorMsg)
  92. log.WriteLog(_('Note that the wxGUI\'s vector digitizer is currently disabled '
  93. '(hopefully this will be fixed soon). '
  94. 'Please keep an eye out for updated versions of GRASS. '
  95. 'In the meantime you can use "v.digit" from the Develop Vector menu.'), wrap = 60)
  96. self.toolId['vdigit'] = -1
  97. self.combo = wx.ComboBox(parent = self, id = wx.ID_ANY,
  98. choices = choices,
  99. style = wx.CB_READONLY, size = (110, -1))
  100. self.combo.SetSelection(0)
  101. self.comboid = self.AddControl(self.combo)
  102. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  103. # realize the toolbar
  104. self.Realize()
  105. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  106. self.combo.Hide()
  107. self.combo.Show()
  108. self.action = { 'id' : self.pointer }
  109. self.defaultAction = { 'id' : self.pointer,
  110. 'bind' : self.parent.OnPointer }
  111. self.OnTool(None)
  112. self.EnableTool(self.zoomBack, False)
  113. self.FixSize(width = 90)
  114. def _toolbarData(self):
  115. """!Toolbar data"""
  116. return self._getToolbarData((('displayMap', BaseIcons['display'],
  117. self.parent.OnDraw),
  118. ('renderMap', BaseIcons['render'],
  119. self.parent.OnRender),
  120. ('erase', BaseIcons['erase'],
  121. self.parent.OnErase),
  122. (None, ),
  123. ('pointer', BaseIcons['pointer'],
  124. self.parent.OnPointer,
  125. wx.ITEM_CHECK),
  126. ('query', MapIcons['query'],
  127. self.parent.OnQuery,
  128. wx.ITEM_CHECK),
  129. ('pan', BaseIcons['pan'],
  130. self.parent.OnPan,
  131. wx.ITEM_CHECK),
  132. ('zoomIn', BaseIcons['zoomIn'],
  133. self.parent.OnZoomIn,
  134. wx.ITEM_CHECK),
  135. ('zoomOut', BaseIcons['zoomOut'],
  136. self.parent.OnZoomOut,
  137. wx.ITEM_CHECK),
  138. ('zoomExtent', BaseIcons['zoomExtent'],
  139. self.parent.OnZoomToMap),
  140. ('zoomBack', BaseIcons['zoomBack'],
  141. self.parent.OnZoomBack),
  142. ('zoomMenu', BaseIcons['zoomMenu'],
  143. self.parent.OnZoomMenu),
  144. (None, ),
  145. ('analyze', MapIcons['analyze'],
  146. self.OnAnalyze),
  147. (None, ),
  148. ('overlay', BaseIcons['overlay'],
  149. self.OnDecoration),
  150. (None, ),
  151. ('saveFile', BaseIcons['saveFile'],
  152. self.parent.SaveToFile),
  153. ('printMap', BaseIcons['print'],
  154. self.parent.PrintMenu),
  155. (None, ))
  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. self.ChangeToolsDesc(mode2d = True)
  192. elif tool == self.toolId['3d'] and \
  193. not (self.parent.MapWindow3D and self.parent.IsPaneShown('3d')):
  194. self.ExitToolbars()
  195. self.parent.AddNviz()
  196. elif tool == self.toolId['vdigit'] and \
  197. not self.parent.GetToolbar('vdigit'):
  198. self.ExitToolbars()
  199. self.parent.AddToolbar("vdigit")
  200. self.parent.MapWindow.SetFocus()
  201. def OnAnalyze(self, event):
  202. """!Analysis tools menu
  203. """
  204. self._onMenu(((MapIcons["measure"], self.parent.OnMeasure),
  205. (MapIcons["profile"], self.parent.OnProfile),
  206. (MapIcons["scatter"], self.parent.OnScatterplot),
  207. (MapIcons["histogram"], self.parent.OnHistogramPyPlot),
  208. (BaseIcons["histogramD"], self.parent.OnHistogram),
  209. (MapIcons["vnet"], self.parent.OnVNet),
  210. (MapIcons["scatter"], self.parent.OnScatterplot2)))
  211. def OnDecoration(self, event):
  212. """!Decorations overlay menu
  213. """
  214. if self.parent.IsPaneShown('3d'):
  215. self._onMenu(((MapIcons["addNorthArrow"], self.parent.OnAddArrow),
  216. (MapIcons["addLegend"], lambda evt: self.parent.AddLegend()),
  217. (MapIcons["addText"], self.parent.OnAddText)))
  218. else:
  219. self._onMenu(((MapIcons["addBarscale"], lambda evt: self.parent.AddBarscale()),
  220. (MapIcons["addLegend"], lambda evt: self.parent.AddLegend()),
  221. (MapIcons["addText"], self.parent.OnAddText)))
  222. def ExitToolbars(self):
  223. if self.parent.GetToolbar('vdigit'):
  224. self.parent.toolbars['vdigit'].OnExit()
  225. if self.parent.GetLayerManager() and \
  226. self.parent.GetLayerManager().IsPaneShown('toolbarNviz'):
  227. self.parent.RemoveNviz()
  228. def Enable2D(self, enabled):
  229. """!Enable/Disable 2D display mode specific tools"""
  230. for tool in (self.zoomMenu,
  231. self.analyze,
  232. self.printMap):
  233. self.EnableTool(tool, enabled)