toolbars.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. MapIcons = {
  19. 'query': MetaIcon(img='info',
  20. label=_('Query raster/vector map(s)'),
  21. desc=_('Query selected raster/vector map(s)')),
  22. 'select': MetaIcon(img='select',
  23. label=_('Select vector feature(s)'),
  24. desc=_('Select features interactively from vector map')),
  25. 'addBarscale': MetaIcon(img='scalebar-add',
  26. label=_('Add scale bar')),
  27. 'addRasterLegend': MetaIcon(img='legend-add',
  28. label=_('Add raster legend')),
  29. 'addVectorLegend': MetaIcon(img='legend-add',
  30. label=_('Add vector legend')),
  31. 'addNorthArrow': MetaIcon(img='north-arrow-add',
  32. label=_('Add north arrow')),
  33. 'analyze': MetaIcon(img='layer-raster-analyze',
  34. label=_('Analyze map'),
  35. desc=_('Measuring, profiling, histogramming, ...')),
  36. 'measureDistance': MetaIcon(img='measure-length',
  37. label=_('Measure distance')),
  38. 'measureArea': MetaIcon(img='area-measure',
  39. label=_('Measure area')),
  40. 'profile': MetaIcon(img='layer-raster-profile',
  41. label=_('Profile surface map')),
  42. 'scatter': MetaIcon(img='layer-raster-profile',
  43. label=_("Create bivariate scatterplot of raster maps")),
  44. 'addText': MetaIcon(img='text-add',
  45. label=_('Add text')),
  46. 'histogram': MetaIcon(img='layer-raster-histogram',
  47. label=_('Create histogram of raster map')),
  48. 'vnet': MetaIcon(img='vector-tools',
  49. label=_('Vector network analysis tool')),
  50. }
  51. NvizIcons = {
  52. 'rotate': MetaIcon(
  53. img='3d-rotate',
  54. label=_('Rotate 3D scene'),
  55. desc=_('Drag with mouse to rotate 3D scene')),
  56. 'flyThrough': MetaIcon(
  57. img='flythrough',
  58. label=_('Fly-through mode'),
  59. desc=_(
  60. 'Drag with mouse, hold Ctrl down for different mode'
  61. ' or Shift to accelerate')),
  62. 'zoomIn': BaseIcons['zoomIn'].SetLabel(
  63. desc=_('Click mouse to zoom')),
  64. 'zoomOut': BaseIcons['zoomOut'].SetLabel(
  65. desc=_('Click mouse to unzoom'))}
  66. class MapToolbar(BaseToolbar):
  67. """Map Display toolbar
  68. """
  69. def __init__(self, parent, toolSwitcher):
  70. """Map Display constructor
  71. :param parent: reference to MapFrame
  72. """
  73. BaseToolbar.__init__(
  74. self,
  75. parent=parent,
  76. toolSwitcher=toolSwitcher) # MapFrame
  77. self.InitToolbar(self._toolbarData())
  78. self._default = self.pointer
  79. # optional tools
  80. toolNum = 0
  81. choices = [_('2D view'), ]
  82. self.toolId = {'2d': toolNum}
  83. toolNum += 1
  84. if self.parent.GetLayerManager():
  85. log = self.parent.GetLayerManager().GetLogWindow()
  86. if haveNviz:
  87. choices.append(_('3D view'))
  88. self.toolId['3d'] = toolNum
  89. toolNum += 1
  90. else:
  91. from nviz.main import errorMsg
  92. if self.parent.GetLayerManager():
  93. log.WriteCmdLog(_('3D view mode not available'))
  94. log.WriteWarning(_('Reason: %s') % str(errorMsg))
  95. self.toolId['3d'] = -1
  96. if haveVDigit:
  97. choices.append(_("Vector digitizer"))
  98. self.toolId['vdigit'] = toolNum
  99. toolNum += 1
  100. else:
  101. from vdigit.main import errorMsg
  102. if self.parent.GetLayerManager():
  103. log.WriteCmdLog(_('Vector digitizer not available'))
  104. log.WriteWarning(_('Reason: %s') % errorMsg)
  105. log.WriteLog(
  106. _(
  107. 'Note that the wxGUI\'s vector digitizer is disabled in this installation. '
  108. 'Please keep an eye out for updated versions of GRASS. '
  109. 'In the meantime you can use "v.edit" for non-interactive editing '
  110. 'from the Develop vector map menu.'),
  111. wrap=60)
  112. self.toolId['vdigit'] = -1
  113. choices.append(_("Raster digitizer"))
  114. self.toolId['rdigit'] = toolNum
  115. self.combo = wx.ComboBox(parent=self, id=wx.ID_ANY,
  116. choices=choices,
  117. style=wx.CB_READONLY, size=(110, -1))
  118. self.combo.SetSelection(0)
  119. self.comboid = self.AddControl(self.combo)
  120. self.parent.Bind(wx.EVT_COMBOBOX, self.OnSelectTool, self.comboid)
  121. # realize the toolbar
  122. self.Realize()
  123. # workaround for Mac bug. May be fixed by 2.8.8, but not before then.
  124. self.combo.Hide()
  125. self.combo.Show()
  126. for tool in (self.pointer, self.select, self.query,
  127. self.pan, self.zoomIn, self.zoomOut):
  128. self.toolSwitcher.AddToolToGroup(
  129. group='mouseUse', toolbar=self, tool=tool)
  130. self.EnableTool(self.zoomBack, False)
  131. self.FixSize(width=90)
  132. def _toolbarData(self):
  133. """Toolbar data"""
  134. return self._getToolbarData((
  135. ('renderMap', BaseIcons['render'],
  136. self.parent.OnRender),
  137. ('pointer', BaseIcons['pointer'],
  138. self.parent.OnPointer,
  139. wx.ITEM_CHECK),
  140. ('select', MapIcons['select'],
  141. self.parent.OnSelect,
  142. wx.ITEM_CHECK),
  143. ('query', MapIcons['query'],
  144. self.parent.OnQuery,
  145. wx.ITEM_CHECK),
  146. ('pan', BaseIcons['pan'],
  147. self.parent.OnPan,
  148. wx.ITEM_CHECK),
  149. ('zoomIn', BaseIcons['zoomIn'],
  150. self.parent.OnZoomIn,
  151. wx.ITEM_CHECK),
  152. ('zoomOut', BaseIcons['zoomOut'],
  153. self.parent.OnZoomOut,
  154. wx.ITEM_CHECK),
  155. ('zoomExtent', BaseIcons['zoomExtent'],
  156. self.parent.OnZoomToMap),
  157. ('zoomRegion', BaseIcons['zoomRegion'],
  158. self.parent.OnZoomToWind),
  159. ('zoomBack', BaseIcons['zoomBack'],
  160. self.parent.OnZoomBack),
  161. ('zoomMenu', BaseIcons['zoomMenu'],
  162. self.parent.OnZoomMenu),
  163. ('analyze', MapIcons['analyze'],
  164. self.OnAnalyze),
  165. ('overlay', BaseIcons['overlay'],
  166. self.OnDecoration),
  167. ('saveFile', BaseIcons['saveFile'],
  168. self.parent.SaveToFile),
  169. ))
  170. def InsertTool(self, data):
  171. """Insert tool to toolbar
  172. :param data: toolbar data"""
  173. data = self._getToolbarData(data)
  174. for tool in data:
  175. self.CreateTool(*tool)
  176. self.Realize()
  177. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  178. self.parent._mgr.Update()
  179. def RemoveTool(self, tool):
  180. """Remove tool from toolbar
  181. :param tool: tool id"""
  182. self.DeleteTool(tool)
  183. self.parent._mgr.GetPane('mapToolbar').BestSize(self.GetBestSize())
  184. self.parent._mgr.Update()
  185. def ChangeToolsDesc(self, mode2d):
  186. """Change description of zoom tools for 2D/3D view"""
  187. if mode2d:
  188. icons = BaseIcons
  189. else:
  190. icons = NvizIcons
  191. for i, data in enumerate(self._data):
  192. for tool in (('zoomIn', 'zoomOut')):
  193. if data[0] == tool:
  194. tmp = list(data)
  195. tmp[4] = icons[tool].GetDesc()
  196. self._data[i] = tuple(tmp)
  197. def OnSelectTool(self, event):
  198. """Select / enable tool available in tools list
  199. """
  200. tool = event.GetSelection()
  201. if tool == self.toolId['2d']:
  202. self.ExitToolbars()
  203. self.Enable2D(True)
  204. elif tool == self.toolId['3d'] and \
  205. not (self.parent.MapWindow3D and self.parent.IsPaneShown('3d')):
  206. self.ExitToolbars()
  207. self.parent.AddNviz()
  208. elif tool == self.toolId['vdigit'] and \
  209. not self.parent.GetToolbar('vdigit'):
  210. self.ExitToolbars()
  211. self.parent.AddToolbar("vdigit")
  212. self.parent.MapWindow.SetFocus()
  213. elif tool == self.toolId['rdigit']:
  214. self.ExitToolbars()
  215. self.parent.AddRDigit()
  216. def OnAnalyze(self, event):
  217. """Analysis tools menu
  218. """
  219. self._onMenu(((MapIcons["measureDistance"], self.parent.OnMeasureDistance),
  220. (MapIcons["measureArea"], self.parent.OnMeasureArea),
  221. (MapIcons["profile"], self.parent.OnProfile),
  222. (MapIcons["scatter"], self.parent.OnScatterplot),
  223. (MapIcons["histogram"], self.parent.OnHistogramPyPlot),
  224. (BaseIcons["histogramD"], self.parent.OnHistogram),
  225. (MapIcons["vnet"], self.parent.OnVNet)))
  226. def OnDecoration(self, event):
  227. """Decorations overlay menu
  228. """
  229. self._onMenu(
  230. ((MapIcons["addRasterLegend"],
  231. lambda evt: self.parent.AddLegendRast()),
  232. (MapIcons["addVectorLegend"],
  233. lambda evt: self.parent.AddLegendVect()),
  234. (MapIcons["addBarscale"],
  235. lambda evt: self.parent.AddBarscale()),
  236. (MapIcons["addNorthArrow"],
  237. lambda evt: self.parent.AddArrow()),
  238. (MapIcons["addText"],
  239. lambda evt: self.parent.AddDtext())))
  240. def ExitToolbars(self):
  241. if self.parent.GetToolbar('vdigit'):
  242. self.parent.toolbars['vdigit'].OnExit()
  243. if self.parent.GetLayerManager() and \
  244. self.parent.GetLayerManager().IsPaneShown('toolbarNviz'):
  245. self.parent.RemoveNviz()
  246. if self.parent.GetToolbar('rdigit'):
  247. self.parent.QuitRDigit()
  248. def Enable2D(self, enabled):
  249. """Enable/Disable 2D display mode specific tools"""
  250. for tool in (self.zoomRegion,
  251. self.zoomMenu,
  252. self.analyze,
  253. self.select):
  254. self.EnableTool(tool, enabled)
  255. self.ChangeToolsDesc(enabled)
  256. if enabled:
  257. self.combo.SetValue(_("2D view"))