shape.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.vertices = []
  13. self.fill = False
  14. def addVertex(self, vertex):
  15. self.vertices.append(vertex)
  16. def popVertex(self):
  17. if self.vertices:
  18. return self.vertices.pop()
  19. return None
  20. def paint(self, painter):
  21. if self.vertices:
  22. pen = QPen(self.line_color)
  23. painter.setPen(pen)
  24. path = QPainterPath()
  25. p0 = self.vertices[0]
  26. path.moveTo(p0.x(), p0.y())
  27. for v in self.vertices[1:]:
  28. path.lineTo(v.x(), v.y())
  29. painter.drawPath(path)
  30. if self.fill:
  31. painter.fillPath(path, self.fill_color)
  32. def __len__(self):
  33. return len(self.vertices)
  34. def __getitem__(self, key):
  35. return self.vertices[key]