toolbars.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """
  2. @package datacatalog.toolbars
  3. @brief Data Catalog toolbars
  4. Classes:
  5. - toolbars::DataCatalogToolbar(BaseToolbar)
  6. (C) 2016 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 Martin Landa <landa.martin gmail.com>
  10. """
  11. import wx
  12. from gui_core.toolbars import BaseToolbar
  13. from gui_core.wrap import SearchCtrl
  14. from icons.icon import MetaIcon
  15. icons = {
  16. "reloadTree": MetaIcon(img="redraw", label=_("Reload GRASS locations")),
  17. "reloadMapset": MetaIcon(img="reload", label=_("Reload current GRASS mapset only")),
  18. "unlocked": MetaIcon(
  19. img="edit", label=_("Restrict edits to the current mapset only")
  20. ),
  21. "locked": MetaIcon(
  22. img="edit", label=_("Allow edits outside of the current mapset")
  23. ),
  24. "addGrassDB": MetaIcon(
  25. img="grassdb-add", label=_("Add existing or create new database")
  26. ),
  27. "addMapset": MetaIcon(
  28. img="mapset-add", label=_("Create new mapset in current location")
  29. ),
  30. "addLocation": MetaIcon(
  31. img="location-add", label=_("Create new location in current GRASS database")
  32. ),
  33. "downloadLocation": MetaIcon(
  34. img="location-download",
  35. label=_("Download sample location to current GRASS database"),
  36. ),
  37. "importRaster": MetaIcon(
  38. img="raster-import", label=_("Import raster data [r.import]")
  39. ),
  40. "importVector": MetaIcon(
  41. img="vector-import", label=_("Import vector data [v.import]")
  42. ),
  43. "importLayer": MetaIcon(
  44. img="layer-import", label=_("Select another import option")
  45. ),
  46. }
  47. class DataCatalogToolbar(BaseToolbar):
  48. """Main data catalog toolbar"""
  49. def __init__(self, parent):
  50. """Main toolbar constructor"""
  51. BaseToolbar.__init__(self, parent)
  52. self.InitToolbar(self._toolbarData())
  53. self.filter_element = None
  54. self.filter = SearchCtrl(parent=self)
  55. self.filter.SetDescriptiveText(_("Search"))
  56. self.filter.ShowCancelButton(True)
  57. self.filter.SetSize((150, self.filter.GetBestSize()[1]))
  58. self.filter.Bind(
  59. wx.EVT_TEXT,
  60. lambda event: self.parent.Filter(
  61. self.filter.GetValue(), self.filter_element
  62. ),
  63. )
  64. self.filter.Bind(
  65. wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.parent.Filter("")
  66. )
  67. self.AddControl(self.filter)
  68. filterMenu = wx.Menu()
  69. item = filterMenu.AppendRadioItem(-1, "All")
  70. self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
  71. item = filterMenu.AppendRadioItem(-1, "Raster maps")
  72. self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
  73. item = filterMenu.AppendRadioItem(-1, "Vector maps")
  74. self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
  75. item = filterMenu.AppendRadioItem(-1, "3D raster maps")
  76. self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
  77. self.filter.SetMenu(filterMenu)
  78. help = _(
  79. "Type to search database by map type or name. "
  80. "Use Python regular expressions to refine your search."
  81. )
  82. self.SetToolShortHelp(self.filter.GetId(), help)
  83. # realize the toolbar
  84. self.Realize()
  85. def _toolbarData(self):
  86. """Returns toolbar data (name, icon, handler)"""
  87. # BaseIcons are a set of often used icons. It is possible
  88. # to reuse icons in ./trunk/gui/icons/grass or add new ones there.
  89. return self._getToolbarData(
  90. (
  91. ("reloadTree", icons["reloadTree"], self.parent.OnReloadTree),
  92. (
  93. "reloadMapset",
  94. icons["reloadMapset"],
  95. self.parent.OnReloadCurrentMapset,
  96. ),
  97. ("lock", icons["locked"], self.OnSetRestriction, wx.ITEM_CHECK),
  98. ("addGrassDB", icons["addGrassDB"], self.parent.OnAddGrassDB),
  99. ("addLocation", icons["addLocation"], self.parent.OnCreateLocation),
  100. (
  101. "downloadLocation",
  102. icons["downloadLocation"],
  103. self.parent.OnDownloadLocation,
  104. ),
  105. ("addMapset", icons["addMapset"], self.parent.OnCreateMapset),
  106. ("importRaster", icons["importRaster"], self.parent.OnImportGdalLayers),
  107. ("importVector", icons["importVector"], self.parent.OnImportOgrLayers),
  108. ("importLayer", icons["importLayer"], self.parent.OnImportMenu),
  109. )
  110. )
  111. def OnFilterMenu(self, event):
  112. """Decide the element to filter by"""
  113. filterMenu = self.filter.GetMenu().GetMenuItems()
  114. self.filter_element = None
  115. if filterMenu[1].IsChecked():
  116. self.filter_element = "raster"
  117. elif filterMenu[2].IsChecked():
  118. self.filter_element = "vector"
  119. elif filterMenu[3].IsChecked():
  120. self.filter_element = "raster_3d"
  121. # trigger filter on change
  122. if self.filter.GetValue():
  123. self.parent.Filter(self.filter.GetValue(), self.filter_element)
  124. def OnSetRestriction(self, event):
  125. if self.GetToolState(self.lock):
  126. self.SetToolNormalBitmap(self.lock, icons["unlocked"].GetBitmap())
  127. self.SetToolShortHelp(self.lock, icons["unlocked"].GetLabel())
  128. self.parent.SetRestriction(restrict=False)
  129. else:
  130. self.SetToolNormalBitmap(self.lock, icons["locked"].GetBitmap())
  131. self.SetToolShortHelp(self.lock, icons["locked"].GetLabel())
  132. self.parent.SetRestriction(restrict=True)