shape.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.points = []
  13. self.fill = False
  14. def addPoint(self, point):
  15. self.points.append(point)
  16. def popPoint(self):
  17. if self.points:
  18. return self.points.pop()
  19. return None
  20. def isClosed(self):
  21. return len(self.points) > 1 and self[0] == self[-1]
  22. def paint(self, painter):
  23. if self.points:
  24. pen = QPen(self.line_color)
  25. painter.setPen(pen)
  26. path = QPainterPath()
  27. path.moveTo(self.points[0].x(), self.points[0].y())
  28. for p in self.points[1:]:
  29. path.lineTo(p.x(), p.y())
  30. painter.drawPath(path)
  31. if self.fill:
  32. painter.fillPath(path, self.fill_color)
  33. def __len__(self):
  34. return len(self.points)
  35. def __getitem__(self, key):
  36. return self.points[key]
  37. def __setitem__(self, key, value):
  38. self.points[key] = value