toolbars.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. """
  2. @package rdigit.toolbars
  3. @brief rdigit toolbars and icons.
  4. Classes:
  5. - toolbars::RDigitToolbar
  6. (C) 2014 by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. @author Anna Petrasova <kratochanna gmail.com>
  11. """
  12. import wx
  13. from gui_core.toolbars import BaseToolbar
  14. from icons.icon import MetaIcon
  15. from gui_core.widgets import FloatValidator
  16. import wx.lib.colourselect as csel
  17. from gui_core.wrap import TextCtrl, StaticText, ColourSelect
  18. from gui_core.toolbars import BaseIcons
  19. rdigitIcons = {
  20. "area": MetaIcon(img="polygon-create", label=_("Digitize area")),
  21. "line": MetaIcon(img="line-create", label=_("Digitize line")),
  22. "point": MetaIcon(img="point-create", label=_("Digitize point")),
  23. "save": MetaIcon(img="save", label=_("Save raster map")),
  24. "undo": MetaIcon(img="undo", label=_("Undo")),
  25. }
  26. class RDigitToolbar(BaseToolbar):
  27. """RDigit toolbar"""
  28. def __init__(self, parent, giface, controller, toolSwitcher):
  29. """RDigit toolbar constructor"""
  30. BaseToolbar.__init__(self, parent, toolSwitcher)
  31. self._controller = controller
  32. self._giface = giface
  33. self.InitToolbar(self._toolbarData())
  34. self._mapSelectionCombo = wx.ComboBox(
  35. self, id=wx.ID_ANY, value=_("Select raster map"), choices=[], size=(120, -1)
  36. )
  37. self._mapSelectionCombo.Bind(wx.EVT_COMBOBOX, self.OnMapSelection)
  38. self._mapSelectionCombo.SetEditable(False)
  39. self.InsertControl(0, self._mapSelectionCombo)
  40. self._previousMap = self._mapSelectionCombo.GetValue()
  41. self._color = ColourSelect(parent=self, colour=wx.GREEN, size=(30, 30))
  42. self._color.Bind(csel.EVT_COLOURSELECT, lambda evt: self._changeDrawColor())
  43. self._color.SetToolTip(_("Set drawing color (not raster cell color)"))
  44. self.InsertControl(4, self._color)
  45. self._cellValues = set(["1"])
  46. # validator does not work with combobox, SetBackgroundColor is not
  47. # working
  48. self._valueCombo = wx.ComboBox(
  49. self,
  50. id=wx.ID_ANY,
  51. choices=list(self._cellValues),
  52. size=(80, -1),
  53. validator=FloatValidator(),
  54. )
  55. self._valueCombo.Bind(wx.EVT_COMBOBOX, lambda evt: self._cellValueChanged())
  56. self._valueCombo.Bind(wx.EVT_TEXT, lambda evt: self._cellValueChanged())
  57. self._valueCombo.SetSelection(0)
  58. self._cellValueChanged()
  59. labelValue = StaticText(self, label=" %s" % _("Cell value:"))
  60. self.InsertControl(6, labelValue)
  61. self.InsertControl(7, self._valueCombo)
  62. # validator does not work with combobox, SetBackgroundColor is not
  63. # working
  64. self._widthValue = TextCtrl(
  65. self, id=wx.ID_ANY, value="0", size=(80, -1), validator=FloatValidator()
  66. )
  67. self._widthValue.Bind(wx.EVT_TEXT, lambda evt: self._widthValueChanged())
  68. self._widthValueChanged()
  69. self._widthValue.SetToolTip(
  70. _(
  71. "Width of currently digitized line or diameter of a digitized point in map units."
  72. )
  73. )
  74. labelWidth = StaticText(self, label=" %s" % _("Width:"))
  75. self.InsertControl(8, labelWidth)
  76. self.InsertControl(9, self._widthValue)
  77. for tool in (self.area, self.line, self.point):
  78. self.toolSwitcher.AddToolToGroup(group="mouseUse", toolbar=self, tool=tool)
  79. self.toolSwitcher.toggleToolChanged.connect(self.CheckSelectedTool)
  80. self._default = self.area
  81. # realize the toolbar
  82. self.Realize()
  83. # workaround Mac bug
  84. for t in (
  85. self._mapSelectionCombo,
  86. self._color,
  87. self._valueCombo,
  88. self._widthValue,
  89. labelValue,
  90. labelWidth,
  91. ):
  92. t.Hide()
  93. t.Show()
  94. def _toolbarData(self):
  95. """Toolbar data"""
  96. return self._getToolbarData(
  97. (
  98. (
  99. ("area", rdigitIcons["area"].label),
  100. rdigitIcons["area"],
  101. lambda event: self._controller.SelectType("area"),
  102. wx.ITEM_CHECK,
  103. ),
  104. (
  105. ("line", rdigitIcons["line"].label),
  106. rdigitIcons["line"],
  107. lambda event: self._controller.SelectType("line"),
  108. wx.ITEM_CHECK,
  109. ),
  110. (
  111. ("point", rdigitIcons["point"].label),
  112. rdigitIcons["point"],
  113. lambda event: self._controller.SelectType("point"),
  114. wx.ITEM_CHECK,
  115. ),
  116. (None,),
  117. (None,),
  118. (
  119. ("undo", rdigitIcons["undo"].label),
  120. rdigitIcons["undo"],
  121. lambda event: self._controller.Undo(),
  122. ),
  123. (
  124. ("save", rdigitIcons["save"].label),
  125. rdigitIcons["save"],
  126. lambda event: self._controller.Save(),
  127. ),
  128. (
  129. ("help", BaseIcons["help"].label),
  130. BaseIcons["help"],
  131. lambda event: self._giface.Help("wxGUI.rdigit"),
  132. ),
  133. (
  134. ("quit", BaseIcons["quit"].label),
  135. BaseIcons["quit"],
  136. lambda event: self._controller.Stop(),
  137. ),
  138. )
  139. )
  140. def CheckSelectedTool(self, id):
  141. if self.toolSwitcher.IsToolInGroup(tool=id, group="mouseUse") and id not in (
  142. self.area,
  143. self.line,
  144. self.point,
  145. ):
  146. self._controller.SelectType(None)
  147. def UpdateRasterLayers(self, rasters):
  148. new = _("New raster map")
  149. items = [raster.name for raster in rasters if raster.name is not None]
  150. items.insert(0, new)
  151. self._mapSelectionCombo.SetItems(items)
  152. def OnMapSelection(self, event=None):
  153. """!Either map to edit or create new map selected."""
  154. idx = self._mapSelectionCombo.GetSelection()
  155. if idx == 0:
  156. ret = self._controller.SelectNewMap()
  157. else:
  158. ret = self._controller.SelectOldMap(self._mapSelectionCombo.GetString(idx))
  159. if not ret:
  160. # in wxpython 3 we can't set value which is not in the items
  161. # when not editable
  162. self._mapSelectionCombo.SetEditable(True)
  163. self._mapSelectionCombo.SetValue(self._previousMap)
  164. self._mapSelectionCombo.SetEditable(False)
  165. # we need to get back to previous
  166. self._previousMap = self._mapSelectionCombo.GetValue()
  167. def NewRasterAdded(self, name):
  168. idx = self._mapSelectionCombo.Append(name)
  169. self._mapSelectionCombo.SetSelection(idx)
  170. def UpdateCellValues(self, values=None):
  171. orig = self._valueCombo.GetValue()
  172. if not values:
  173. values = [orig]
  174. for value in values:
  175. self._cellValues.add(str(value))
  176. valList = sorted(list(self._cellValues), key=float)
  177. self._valueCombo.SetItems(valList)
  178. self._valueCombo.SetStringSelection(orig)
  179. def _cellValueChanged(self):
  180. value = self._valueCombo.GetValue()
  181. try:
  182. value = float(value)
  183. self._controller.SetCellValue(value)
  184. except ValueError:
  185. return
  186. def _widthValueChanged(self):
  187. value = self._widthValue.GetValue()
  188. try:
  189. value = float(value)
  190. self._controller.SetWidthValue(value)
  191. except ValueError:
  192. self._controller.SetWidthValue(0)
  193. return
  194. def _changeDrawColor(self):
  195. color = self._color.GetColour()
  196. self._controller.ChangeDrawColor(color=color)