toolbars.py 12 KB

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