toolbars.py 11 KB

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