digit.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """!
  2. @package iclass.digit
  3. @brief wxIClass digitizer classes
  4. Classes:
  5. - digit::IClassVDigit
  6. - digit::IClassVDigitWindow
  7. (C) 2006-2012 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. @author Vaclav Petras <wenzeslaus gmail.com>
  12. @author Anna Kratochvilova <kratochanna gmail.com>
  13. """
  14. import wx
  15. from vdigit.mapwindow import VDigitWindow
  16. from vdigit.wxdigit import IVDigit
  17. from vdigit.wxdisplay import DisplayDriver, TYPE_AREA
  18. from core.gcmd import GWarning
  19. from core.utils import _
  20. try:
  21. from grass.lib.gis import G_verbose, G_set_verbose
  22. from grass.lib.vector import *
  23. from grass.lib.vedit import *
  24. except ImportError:
  25. pass
  26. import grass.script as grass
  27. class IClassVDigitWindow(VDigitWindow):
  28. """! Class similar to VDigitWindow but specialized for wxIClass."""
  29. def __init__(self, parent, giface, map, frame):
  30. """!
  31. @a parent should has toolbar providing current class (category).
  32. @param parent gui parent
  33. @param map map renderer instance
  34. """
  35. VDigitWindow.__init__(self, parent = parent, giface = giface, Map = map, frame = frame)
  36. def _onLeftDown(self, event):
  37. action = self.toolbar.GetAction()
  38. if not action:
  39. return
  40. region = grass.region()
  41. e, n = self.Pixel2Cell(event.GetPositionTuple())
  42. if not ((region['s'] <= n <= region['n']) and (region['w'] <= e <= region['e'])):
  43. GWarning(parent = self.parent,
  44. message = _("You are trying to create a training area "
  45. "outside the computational region. "
  46. "Please, use g.region to set the appropriate region first."))
  47. return
  48. cat = self.GetCurrentCategory()
  49. if cat is None and action == "addLine":
  50. dlg = wx.MessageDialog(parent = self.parent,
  51. message = _("In order to create a training area, "
  52. "you have to select class first.\n\n"
  53. "There is no class yet, "
  54. "do you want to create one?"),
  55. caption = _("No class selected"),
  56. style = wx.YES_NO)
  57. if dlg.ShowModal() == wx.ID_YES:
  58. self.parent.OnCategoryManager(None)
  59. dlg.Destroy()
  60. event.Skip()
  61. return
  62. super(IClassVDigitWindow, self)._onLeftDown(event)
  63. def _addRecord(self):
  64. return False
  65. def _updateATM(self):
  66. pass
  67. def _onRightUp(self, event):
  68. super(IClassVDigitWindow, self)._onRightUp(event)
  69. self.parent.UpdateChangeState(changes = True)
  70. def GetCurrentCategory(self):
  71. """!Returns current category (class).
  72. Category should be assigned to new features (areas).
  73. It is taken from parent's toolbar.
  74. """
  75. return self.parent.GetToolbar("iClass").GetSelectedCategoryIdx()
  76. def GetCategoryColor(self, cat):
  77. """!Get color associated with given category"""
  78. r, g, b = map(int, self.parent.GetClassColor(cat).split(':'))
  79. return wx.Colour(r, g, b)
  80. class IClassVDigit(IVDigit):
  81. """! Class similar to IVDigit but specialized for wxIClass."""
  82. def __init__(self, mapwindow):
  83. IVDigit.__init__(self, mapwindow, driver = IClassDisplayDriver)
  84. self._settings['closeBoundary'] = True # snap to the first node
  85. def _getNewFeaturesLayer(self):
  86. return 1
  87. def _getNewFeaturesCat(self):
  88. cat = self.mapWindow.GetCurrentCategory()
  89. return cat
  90. def DeleteAreasByCat(self, cats):
  91. """!Delete areas (centroid+boundaries) by categories
  92. @param cats list of categories
  93. """
  94. for cat in cats:
  95. Vedit_delete_areas_cat(self.poMapInfo, 1, cat)
  96. class IClassDisplayDriver(DisplayDriver):
  97. """! Class similar to DisplayDriver but specialized for wxIClass
  98. @todo needs refactoring (glog, gprogress)
  99. """
  100. def __init__(self, device, deviceTmp, mapObj, window, glog, gprogress):
  101. DisplayDriver.__init__(self, device, deviceTmp, mapObj, window, glog, gprogress)
  102. self._cat = -1
  103. def _drawObject(self, robj):
  104. """!Draw given object to the device
  105. @param robj object to draw
  106. """
  107. if robj.type == TYPE_AREA:
  108. self._cat = Vect_get_area_cat(self.poMapInfo, robj.fid, 1)
  109. elif robj.type == TYPE_CENTROIDIN:
  110. return # skip centroids
  111. DisplayDriver._drawObject(self, robj)
  112. def _definePen(self, rtype):
  113. """!Define pen/brush based on rendered object)
  114. @param rtype type of the object
  115. @return pen, brush
  116. """
  117. pen, brush = DisplayDriver._definePen(self, rtype)
  118. if self._cat > 0 and rtype == TYPE_AREA:
  119. brush = wx.Brush(self.window.GetCategoryColor(self._cat), wx.SOLID)
  120. return pen, brush
  121. def CloseMap(self):
  122. """!Close training areas map - be quiet"""
  123. verbosity = G_verbose()
  124. G_set_verbose(0)
  125. DisplayDriver.CloseMap(self)
  126. G_set_verbose(verbosity)