toolbars.py 12 KB

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