toolbars.py 7.6 KB

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