digit.py 4.4 KB

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