toolbars.py 11 KB

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