shape.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5. class Shape(object):
  6. def __init__(self, label=None,
  7. line_color=QColor(0, 255, 0, 128),
  8. fill_color=QColor(255, 0, 0, 128)):
  9. self.label = label
  10. self.line_color = line_color
  11. self.fill_color = fill_color
  12. self.pointSize=8 # draw square around each point , with length and width =8 pixels
  13. self.points = []
  14. self.fill = False
  15. def addPoint(self, point):
  16. self.points.append(point)
  17. def popPoint(self):
  18. if self.points:
  19. return self.points.pop()
  20. return None
  21. def isClosed(self):
  22. return len(self.points) > 1 and self[0] == self[-1]
  23. def paint(self, painter):
  24. if self.points:
  25. pen = QPen(self.line_color)
  26. painter.setPen(pen)
  27. path = QPainterPath()
  28. pathRect = QPainterPath()
  29. path.moveTo(self.points[0].x(), self.points[0].y())
  30. pathRect.addRect( self.points[0].x()-self.pointSize/2, self.points[0].y()-self.pointSize/2, self.pointSize,self.pointSize)
  31. for p in self.points[1:]:
  32. path.lineTo(p.x(), p.y())
  33. pathRect.addRect(p.x()-self.pointSize/2. ,p.y()-self.pointSize/2,self.pointSize,self.pointSize)
  34. painter.drawPath(path)
  35. #painter.drawPath(pathRect)
  36. if self.fill:
  37. painter.fillPath(path, self.fill_color)
  38. painter.fillPath(pathRect, self.line_color)
  39. def __len__(self):
  40. return len(self.points)
  41. def __getitem__(self, key):
  42. return self.points[key]
  43. def __setitem__(self, key, value):
  44. self.points[key] = value