toolbars.py 7.3 KB

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