toolbars.py 7.6 KB

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