toolbars.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. """!
  2. @package iclass.toolbars
  3. @brief wxIClass toolbars and icons.
  4. Classes:
  5. - toolbars::IClassMapToolbar
  6. - toolbars::IClassToolbar
  7. - toolbars::IClassMapManagerToolbar
  8. - toolbars::IClassMiscToolbar
  9. (C) 2006-2011 by the GRASS Development Team
  10. This program is free software under the GNU General Public
  11. License (>=v2). Read the file COPYING that comes with GRASS
  12. for details.
  13. @author Vaclav Petras <wenzeslaus gmail.com>
  14. @author Anna Kratochvilova <kratochanna gmail.com>
  15. """
  16. import wx
  17. from core.utils import _
  18. from gui_core.toolbars import BaseToolbar, BaseIcons
  19. from icons.icon import MetaIcon
  20. from iclass.dialogs import IClassMapDialog
  21. from gui_core.forms import GUI
  22. import grass.script as grass
  23. iClassIcons = {
  24. 'opacity' : MetaIcon(img = 'layer-opacity',
  25. label = _('Set opacity level')),
  26. 'classManager' : MetaIcon(img = 'table-manager',
  27. label = _('Class manager')),
  28. 'selectGroup' : MetaIcon(img = 'layer-group-add',
  29. label = _('Select imagery group')),
  30. 'run' : MetaIcon(img = 'execute',
  31. label = _('Run analysis')),
  32. 'sigFile' : MetaIcon(img = 'script-save',
  33. label = _('Save signature file')),
  34. 'delCmd' : MetaIcon(img = 'layer-remove',
  35. label = _('Delete selected map layer')),
  36. 'exportAreas' : MetaIcon(img = 'layer-export',
  37. label = _('Export training areas to vector map')),
  38. 'importAreas' : MetaIcon(img = 'layer-import',
  39. label = _('Import training areas from vector map')),
  40. 'addRgb' : MetaIcon(img = 'layer-rgb-add',
  41. label = _('Add RGB map layer')),
  42. 'scatt_plot' : MetaIcon(img = 'layer-raster-analyze',
  43. label = _('Open Scatter Plot Tool (EXPERIMENTAL GSoC 2013)')),
  44. }
  45. class IClassMapToolbar(BaseToolbar):
  46. """!IClass Map toolbar
  47. """
  48. def __init__(self, parent, toolSwitcher):
  49. """!IClass Map toolbar constructor
  50. """
  51. BaseToolbar.__init__(self, parent, toolSwitcher)
  52. self.InitToolbar(self._toolbarData())
  53. self._default = self.pan
  54. # add tool to toggle active map window
  55. self.togglemapid = wx.NewId()
  56. self.togglemap = wx.Choice(parent = self, id = self.togglemapid,
  57. choices = [_('Training'), _('Preview')])
  58. self.InsertControl(9, self.togglemap)
  59. self.SetToolShortHelp(self.togglemapid, '%s %s %s' % (_('Set map canvas for '),
  60. BaseIcons["zoomBack"].GetLabel(),
  61. _('/ Zoom to map')))
  62. for tool in (self.pan, self.zoomIn, self.zoomOut):
  63. self.toolSwitcher.AddToolToGroup(group='mouseUse', toolbar=self, tool=tool)
  64. # realize the toolbar
  65. self.Realize()
  66. self.EnableTool(self.zoomBack, False)
  67. def GetActiveMapTool(self):
  68. """!Return widget for selecting active maps"""
  69. return self.togglemap
  70. def GetActiveMap(self):
  71. """!Get currently selected map"""
  72. return self.togglemap.GetSelection()
  73. def SetActiveMap(self, index):
  74. """!Set currently selected map"""
  75. return self.togglemap.SetSelection(index)
  76. def _toolbarData(self):
  77. """!Toolbar data"""
  78. icons = BaseIcons
  79. return self._getToolbarData((("displaymap", icons["display"],
  80. self.parent.OnDraw),
  81. ("rendermap", icons["render"],
  82. self.parent.OnRender),
  83. ("erase", icons["erase"],
  84. self.parent.OnErase),
  85. (None, ),
  86. ("pan", icons["pan"],
  87. self.parent.OnPan,
  88. wx.ITEM_CHECK),
  89. ("zoomIn", icons["zoomIn"],
  90. self.parent.OnZoomIn,
  91. wx.ITEM_CHECK),
  92. ("zoomOut", icons["zoomOut"],
  93. self.parent.OnZoomOut,
  94. wx.ITEM_CHECK),
  95. ("zoomMenu", icons["zoomMenu"],
  96. self.parent.OnZoomMenu),
  97. (None, ),
  98. ("zoomBack", icons["zoomBack"],
  99. self.parent.OnZoomBack),
  100. ("zoomToMap", icons["zoomExtent"],
  101. self.parent.OnZoomToMap),
  102. (None, ),
  103. ("scatt_plot", iClassIcons["scatt_plot"],
  104. self.parent.OnScatterplot)
  105. ))
  106. class IClassToolbar(BaseToolbar):
  107. """!IClass toolbar
  108. """
  109. def __init__(self, parent):
  110. """!IClass toolbar constructor
  111. """
  112. BaseToolbar.__init__(self, parent)
  113. self.InitToolbar(self._toolbarData())
  114. self.choice = wx.Choice(parent = self, id = wx.ID_ANY, size = (110, -1))
  115. choiceid = self.InsertControl(3, self.choice)
  116. self.choice.Bind(wx.EVT_CHOICE, self.OnSelectCategory)
  117. # stupid workaround to insert small space between controls
  118. self.InsertControl(4, wx.StaticText(self, id = wx.ID_ANY, label = ' '))
  119. self.combo = wx.ComboBox(self, id = wx.ID_ANY, size = (130, -1),
  120. style = wx.TE_PROCESS_ENTER)
  121. self.InitStddev()
  122. comboid = self.InsertControl(5, self.combo)
  123. self.EnableControls(False)
  124. self.combo.Bind(wx.EVT_COMBOBOX, self.OnStdChangeSelection)
  125. self.combo.Bind(wx.EVT_TEXT_ENTER, self.OnStdChangeText)
  126. # realize the toolbar
  127. self.Realize()
  128. def _toolbarData(self):
  129. """!Toolbar data"""
  130. icons = iClassIcons
  131. return self._getToolbarData((("selectGroup", icons['selectGroup'],
  132. lambda event : self.parent.AddBands()),
  133. (None, ),
  134. ("classManager", icons['classManager'],
  135. self.parent.OnCategoryManager),
  136. (None, ),
  137. ("runAnalysis", icons['run'],
  138. self.parent.OnRunAnalysis),
  139. (None, ),
  140. ("importAreas", icons['importAreas'],
  141. self.parent.OnImportAreas),
  142. ("exportAreas", icons['exportAreas'],
  143. self.parent.OnExportAreas),
  144. ("sigFile", icons['sigFile'],
  145. self.parent.OnSaveSigFile),
  146. ))
  147. def OnSelectCategory(self, event):
  148. idx = self.choice.GetSelection()
  149. cat = self.choice.GetClientData(idx)
  150. self.parent.CategoryChanged(currentCat = cat)
  151. def SetCategories(self, catNames, catIdx):
  152. self.choice.Clear()
  153. for name, idx in zip(catNames, catIdx):
  154. self.choice.Append(name, idx)
  155. def GetSelectedCategoryName(self):
  156. return self.choice.GetStringSelection()
  157. def GetSelectedCategoryIdx(self):
  158. idx = self.choice.GetSelection()
  159. if idx != wx.NOT_FOUND:
  160. return self.choice.GetClientData(idx)
  161. return None
  162. def OnStdChangeSelection(self, event):
  163. idx = self.combo.GetSelection()
  164. nstd = self.combo.GetClientData(idx)
  165. self.StddevChanged(nstd)
  166. def OnStdChangeText(self, event):
  167. val = self.combo.GetValue().strip()
  168. try:
  169. nstd = float(val)
  170. except ValueError:
  171. try:
  172. nstd = float(val.split()[0])
  173. except ValueError:
  174. nstd = None
  175. if nstd is not None:
  176. self.StddevChanged(nstd)
  177. def StddevChanged(self, nstd):
  178. idx = self.GetSelectedCategoryIdx()
  179. if not idx:
  180. return
  181. self.parent.StddevChanged(cat = idx, nstd = nstd)
  182. def UpdateStddev(self, nstd):
  183. self.combo.SetValue(' '.join(("%.2f" % nstd, _('std dev'))))
  184. def InitStddev(self):
  185. for nstd in range(50, 250, 25):
  186. nstd /= 100.
  187. self.combo.Append(item = ' '.join(("%.2f" % nstd, _('std dev'))), clientData = nstd)
  188. self.combo.SetSelection(4) # 1.5
  189. def EnableControls(self, enable = True):
  190. self.combo.Enable(enable)
  191. self.choice.Enable(enable)
  192. class IClassMapManagerToolbar(BaseToolbar):
  193. """!IClass toolbar
  194. """
  195. def __init__(self, parent, mapManager):
  196. """!IClass toolbar constructor
  197. """
  198. BaseToolbar.__init__(self, parent)
  199. self.InitToolbar(self._toolbarData())
  200. self.choice = wx.Choice(parent = self, id = wx.ID_ANY, size = (300, -1))
  201. self.choiceid = self.AddControl(self.choice)
  202. self.choice.Bind(wx.EVT_CHOICE, self.OnSelectLayer)
  203. self.mapManager = mapManager
  204. # realize the toolbar
  205. self.Realize()
  206. def _toolbarData(self):
  207. """!Toolbar data"""
  208. return self._getToolbarData((("addRast", BaseIcons['addRast'],
  209. self.OnAddRast),
  210. ('addRgb', iClassIcons['addRgb'],
  211. self.OnAddRGB),
  212. ("delRast", iClassIcons['delCmd'],
  213. self.OnDelRast),
  214. ("setOpacity", iClassIcons['opacity'],
  215. self.OnSetOpacity),
  216. ))
  217. def OnSelectLayer(self, event):
  218. layer = self.choice.GetStringSelection()
  219. self.mapManager.SelectLayer(name = layer)
  220. def OnAddRast(self, event):
  221. dlg = IClassMapDialog(self, title = _("Add raster map"), element = 'raster')
  222. if dlg.ShowModal() == wx.ID_OK:
  223. raster = grass.find_file(name = dlg.GetMap(), element = 'cell')
  224. if raster['fullname']:
  225. self.mapManager.AddLayer(name = raster['fullname'])
  226. dlg.Destroy()
  227. def OnAddRGB(self, event):
  228. cmd = ['d.rgb']
  229. GUI(parent = self.parent).ParseCommand(cmd, completed = (self.GetOptData, '', ''))
  230. def GetOptData(self, dcmd, layer, params, propwin):
  231. if dcmd:
  232. self.mapManager.AddLayerRGB(cmd = dcmd)
  233. def OnDelRast(self, event):
  234. layer = self.choice.GetStringSelection()
  235. idx = self.choice.GetSelection()
  236. if layer:
  237. self.mapManager.RemoveLayer(name = layer, idx = idx)
  238. def OnSetOpacity(self, event):
  239. layer = self.choice.GetStringSelection()
  240. idx = self.choice.GetSelection()
  241. if idx == wx.NOT_FOUND:
  242. return
  243. self.mapManager.SetOpacity(name = layer)
  244. class IClassMiscToolbar(BaseToolbar):
  245. """!IClass toolbar
  246. """
  247. def __init__(self, parent):
  248. """!IClass toolbar constructor
  249. """
  250. BaseToolbar.__init__(self, parent)
  251. self.InitToolbar(self._toolbarData())
  252. # realize the toolbar
  253. self.Realize()
  254. def _toolbarData(self):
  255. """!Toolbar data"""
  256. icons = BaseIcons
  257. return self._getToolbarData((("help", icons['help'],
  258. self.parent.OnHelp),
  259. ("quit", icons['quit'],
  260. self.parent.OnCloseWindow),
  261. ))