digit.py 5.3 KB

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