digit.py 4.7 KB

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