toolbars.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. """
  2. @package iscatt.toolbars
  3. @brief Scatter plot - toolbars
  4. Classes:
  5. - toolbars::MainToolbar
  6. (C) 2013 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Stepan Turek <stepan.turek seznam.cz> (mentor: Martin Landa)
  10. """
  11. import wx
  12. from icons.icon import MetaIcon
  13. from gui_core.toolbars import BaseToolbar, BaseIcons
  14. from core.gcmd import RunCommand
  15. from iscatt.dialogs import SettingsDialog
  16. class MainToolbar(BaseToolbar):
  17. """Main toolbar"""
  18. def __init__(self, parent, scatt_mgr, opt_tools=None):
  19. BaseToolbar.__init__(self, parent)
  20. self.scatt_mgr = scatt_mgr
  21. self.opt_tools = opt_tools
  22. self.InitToolbar(self._toolbarData())
  23. # realize the toolbar
  24. self.Realize()
  25. self.scatt_mgr.modeSet.connect(self.ModeSet)
  26. def _toolbarData(self):
  27. icons = {
  28. "selectGroup": MetaIcon(
  29. img="layer-group-add", label=_("Select imagery group")
  30. ),
  31. "settings": BaseIcons["settings"],
  32. "help": BaseIcons["help"],
  33. "add_scatt_pl": MetaIcon(
  34. img="layer-raster-analyze", label=_("Add scatter plot")
  35. ),
  36. "selCatPol": MetaIcon(img="polygon", label=_("Select area with polygon")),
  37. "pan": MetaIcon(img="pan", label=_("Pan mode for scatter plots")),
  38. "zoomIn": MetaIcon(
  39. img="zoom-in",
  40. label=_("Zoom mode for scatter plots (left mouse button, wheel)"),
  41. ),
  42. "zoomExtent": MetaIcon(
  43. img="zoom-extent",
  44. label=_(
  45. "Zoom to scatter plot data extend mode (click on scatter plot for zooming to extend)"
  46. ),
  47. ),
  48. "cats_mgr": MetaIcon(
  49. img="table-manager", label=_("Show/hide class manager")
  50. ),
  51. }
  52. tools = [
  53. (
  54. ("add_scatt", icons["add_scatt_pl"].label),
  55. icons["add_scatt_pl"],
  56. lambda event: self.scatt_mgr.AddScattPlot(),
  57. ),
  58. (None,),
  59. (
  60. ("cats_mgr", icons["cats_mgr"].label),
  61. icons["cats_mgr"],
  62. lambda event: self.parent.ShowCategoryPanel(event.IsChecked()),
  63. wx.ITEM_CHECK,
  64. ),
  65. (None,),
  66. (
  67. ("pan", icons["pan"].label),
  68. icons["pan"],
  69. lambda event: self.SetPloltsMode(event, "pan"),
  70. wx.ITEM_CHECK,
  71. ),
  72. (
  73. ("zoom", icons["zoomIn"].label),
  74. icons["zoomIn"],
  75. lambda event: self.SetPloltsMode(event, "zoom"),
  76. wx.ITEM_CHECK,
  77. ),
  78. (
  79. ("zoom_extend", icons["zoomExtent"].label),
  80. icons["zoomExtent"],
  81. lambda event: self.SetPloltsMode(event, "zoom_extend"),
  82. wx.ITEM_CHECK,
  83. ),
  84. (None,),
  85. (
  86. ("sel_pol_mode", icons["selCatPol"].label),
  87. icons["selCatPol"],
  88. self.ActivateSelectionPolygonMode,
  89. wx.ITEM_CHECK,
  90. ),
  91. (None,),
  92. (
  93. ("settings", icons["settings"].label),
  94. icons["settings"],
  95. self.OnSettings,
  96. ),
  97. (
  98. ("help", icons["help"].label),
  99. icons["help"],
  100. self.OnHelp,
  101. ),
  102. ]
  103. if self.opt_tools and "add_group" in self.opt_tools:
  104. tools.insert(
  105. 0,
  106. (
  107. ("selectGroup", icons["selectGroup"].label),
  108. icons["selectGroup"],
  109. lambda event: self.scatt_mgr.SetData(),
  110. ),
  111. )
  112. return self._getToolbarData(tools)
  113. def GetToolId(self, toolName): # TODO can be useful in base
  114. return vars(self)[toolName]
  115. def SetPloltsMode(self, event, tool_name):
  116. self.scatt_mgr.modeSet.disconnect(self.ModeSet)
  117. if event.IsChecked():
  118. for i_tool_data in self._data:
  119. i_tool_name = i_tool_data[0]
  120. if not i_tool_name or i_tool_name in ["cats_mgr", "sel_pol_mode"]:
  121. continue
  122. if i_tool_name == tool_name:
  123. continue
  124. i_tool_id = vars(self)[i_tool_name]
  125. self.ToggleTool(i_tool_id, False)
  126. self.scatt_mgr.SetPlotsMode(mode=tool_name)
  127. else:
  128. self.scatt_mgr.SetPlotsMode(mode=None)
  129. self.scatt_mgr.modeSet.connect(self.ModeSet)
  130. def ActivateSelectionPolygonMode(self, event):
  131. activated = self.scatt_mgr.ActivateSelectionPolygonMode(event.IsChecked())
  132. self.parent.ShowPlotEditingToolbar(activated)
  133. i_tool_id = vars(self)["sel_pol_mode"]
  134. self.ToggleTool(i_tool_id, activated)
  135. def ModeSet(self, mode):
  136. self.UnsetMode()
  137. def UnsetMode(self):
  138. for i_tool_data in self._data:
  139. i_tool_name = i_tool_data[0]
  140. if not i_tool_name or i_tool_name in ["cats_mgr", "sel_pol_mode"]:
  141. continue
  142. i_tool_id = vars(self)[i_tool_name]
  143. self.ToggleTool(i_tool_id, False)
  144. def OnSettings(self, event):
  145. dlg = SettingsDialog(
  146. parent=self, id=wx.ID_ANY, title=_("Settings"), scatt_mgr=self.scatt_mgr
  147. )
  148. dlg.ShowModal()
  149. dlg.Destroy()
  150. def OnHelp(self, event):
  151. RunCommand("g.manual", entry="wxGUI.iscatt")
  152. class EditingToolbar(BaseToolbar):
  153. """Main toolbar"""
  154. def __init__(self, parent, scatt_mgr):
  155. BaseToolbar.__init__(self, parent)
  156. self.scatt_mgr = scatt_mgr
  157. self.InitToolbar(self._toolbarData())
  158. # realize the toolbar
  159. self.Realize()
  160. self.scatt_mgr.modeSet.connect(self.ModeSet)
  161. def _toolbarData(self):
  162. """Toolbar data"""
  163. self.icons = {
  164. "sel_add": MetaIcon(
  165. img="layer-add",
  166. label=_("Include selected area to class"),
  167. desc=_("Include selected area to class"),
  168. ),
  169. "sel_remove": MetaIcon(
  170. img="layer-remove",
  171. label=_("Exclude selected area from class"),
  172. desc=_("Exclude selected area from class"),
  173. ),
  174. "addVertex": MetaIcon(
  175. img="vertex-create",
  176. label=_("Add new vertex"),
  177. desc=_("Add new vertex to polygon boundary scatter plot"),
  178. ),
  179. "editLine": MetaIcon(
  180. img="polygon-create",
  181. label=_("Create selection polygon"),
  182. desc=_("Add new vertex between last and first points of the boundary"),
  183. ),
  184. "moveVertex": MetaIcon(
  185. img="vertex-move",
  186. label=_("Move vertex"),
  187. desc=_("Move boundary vertex"),
  188. ),
  189. "removeVertex": MetaIcon(
  190. img="vertex-delete",
  191. label=_("Remove vertex"),
  192. desc=_("Remove boundary vertex"),
  193. ),
  194. "delete": MetaIcon(
  195. img="polygon-delete",
  196. label=_(
  197. "Remove polygon (click on scatter plot for removing it's polygon)"
  198. ),
  199. ),
  200. }
  201. return self._getToolbarData(
  202. (
  203. (
  204. ("sel_add", self.icons["sel_add"].label),
  205. self.icons["sel_add"],
  206. lambda event: self.scatt_mgr.ProcessSelectionPolygons("add"),
  207. ),
  208. (
  209. ("sel_remove", self.icons["sel_remove"].label),
  210. self.icons["sel_remove"],
  211. lambda event: self.scatt_mgr.ProcessSelectionPolygons("remove"),
  212. ),
  213. (None,),
  214. (
  215. ("add_vertex", self.icons["editLine"].label),
  216. self.icons["editLine"],
  217. lambda event: self.SetMode(event, "add_vertex"),
  218. wx.ITEM_CHECK,
  219. ),
  220. (
  221. ("add_boundary_vertex", self.icons["addVertex"].label),
  222. self.icons["addVertex"],
  223. lambda event: self.SetMode(event, "add_boundary_vertex"),
  224. wx.ITEM_CHECK,
  225. ),
  226. (
  227. ("move_vertex", self.icons["moveVertex"].label),
  228. self.icons["moveVertex"],
  229. lambda event: self.SetMode(event, "move_vertex"),
  230. wx.ITEM_CHECK,
  231. ),
  232. (
  233. ("delete_vertex", self.icons["removeVertex"].label),
  234. self.icons["removeVertex"],
  235. lambda event: self.SetMode(event, "delete_vertex"),
  236. wx.ITEM_CHECK,
  237. ),
  238. (
  239. ("remove_polygon", self.icons["delete"].label),
  240. self.icons["delete"],
  241. lambda event: self.SetMode(event, "remove_polygon"),
  242. wx.ITEM_CHECK,
  243. ),
  244. )
  245. )
  246. def SetMode(self, event, tool_name):
  247. self.scatt_mgr.modeSet.disconnect(self.ModeSet)
  248. if event.IsChecked():
  249. for i_tool_data in self._data:
  250. i_tool_name = i_tool_data[0]
  251. if not i_tool_name:
  252. continue
  253. if i_tool_name == tool_name:
  254. continue
  255. i_tool_id = vars(self)[i_tool_name]
  256. self.ToggleTool(i_tool_id, False)
  257. self.scatt_mgr.SetPlotsMode(tool_name)
  258. else:
  259. self.scatt_mgr.SetPlotsMode(None)
  260. self.scatt_mgr.modeSet.connect(self.ModeSet)
  261. def ModeSet(self, mode):
  262. if mode in ["zoom", "pan", "zoom_extend", None]:
  263. self.UnsetMode()
  264. def UnsetMode(self):
  265. for i_tool_data in self._data:
  266. i_tool_name = i_tool_data[0]
  267. if not i_tool_name:
  268. continue
  269. i_tool_id = vars(self)[i_tool_name]
  270. self.ToggleTool(i_tool_id, False)
  271. def GetToolId(self, toolName):
  272. return vars(self)[toolName]
  273. class CategoryToolbar(BaseToolbar):
  274. """Main toolbar"""
  275. def __init__(self, parent, scatt_mgr, cats_list):
  276. BaseToolbar.__init__(self, parent)
  277. self.scatt_mgr = scatt_mgr
  278. self.cats_mgr = self.scatt_mgr.GetCategoriesManager()
  279. self.cats_list = cats_list
  280. self.InitToolbar(self._toolbarData())
  281. # realize the toolbar
  282. self.Realize()
  283. def _toolbarData(self):
  284. """Toolbar data"""
  285. self.icons = {
  286. "add_class": MetaIcon(img="layer-add", label=_("Add class")),
  287. "remove_class": MetaIcon(
  288. img="layer-remove", label=_("Remove selected class")
  289. ),
  290. }
  291. return self._getToolbarData(
  292. (
  293. (
  294. ("add_class", self.icons["add_class"].label),
  295. self.icons["add_class"],
  296. lambda event: self.cats_mgr.AddCategory(),
  297. ),
  298. (
  299. ("remove_class", self.icons["remove_class"].label),
  300. self.icons["remove_class"],
  301. lambda event: self.cats_list.DeleteCategory(),
  302. ),
  303. )
  304. )