shape.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5. # FIXME:
  6. # - Add support for highlighting vertices.
  7. # TODO:
  8. # - [opt] Store paths instead of creating new ones at each paint.
  9. class Shape(object):
  10. P_SQUARE, P_ROUND = range(2)
  11. ## The following class variables influence the drawing
  12. ## of _all_ shape objects.
  13. line_color = QColor(0, 255, 0, 128)
  14. fill_color = QColor(255, 0, 0, 128)
  15. select_color = QColor(255, 255, 255)
  16. point_type = P_SQUARE
  17. point_size = 8
  18. scale = 1.0
  19. def __init__(self, label=None, line_color=None):
  20. self.label = label
  21. self.points = []
  22. self.fill = False
  23. self.selected = False
  24. if line_color is not None:
  25. # Override the class line_color attribute
  26. # with an object attribute. Currently this
  27. # is used for drawing the pending line a different color.
  28. self.line_color = line_color
  29. def addPoint(self, point):
  30. self.points.append(point)
  31. def popPoint(self):
  32. if self.points:
  33. return self.points.pop()
  34. return None
  35. def isClosed(self):
  36. return len(self.points) > 1 and self[0] == self[-1]
  37. def paint(self, painter):
  38. if self.points:
  39. pen = QPen(self.select_color if self.selected else self.line_color)
  40. # Try using integer sizes for smoother drawing(?)
  41. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  42. painter.setPen(pen)
  43. line_path = QPainterPath()
  44. vrtx_path = QPainterPath()
  45. line_path.moveTo(QPointF(self.points[0]))
  46. self.drawVertex(vrtx_path, self.points[0])
  47. for p in self.points[1:]:
  48. line_path.lineTo(QPointF(p))
  49. # Skip last element, otherwise its vertex is not filled.
  50. if p != self.points[0]:
  51. self.drawVertex(vrtx_path, p)
  52. painter.drawPath(line_path)
  53. painter.fillPath(vrtx_path, self.line_color)
  54. if self.fill:
  55. painter.fillPath(line_path, self.fill_color)
  56. def drawVertex(self, path, point):
  57. d = self.point_size / self.scale
  58. if self.point_type == self.P_SQUARE:
  59. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  60. else:
  61. path.addEllipse(point, d/2.0, d/2.0)
  62. def containsPoint(self, point):
  63. return self.makePath().contains(point)
  64. def makePath(self):
  65. path = QPainterPath(self.points[0])
  66. for p in self.points[1:]:
  67. path.lineTo(p)
  68. return path
  69. def boundingRect(self):
  70. return self.makePath().boundingRect()
  71. def moveBy(self, offset):
  72. self.points = [p + offset for p in self.points]
  73. def copy(self):
  74. shape = Shape("Copy of %s" % self.label )
  75. shape.points= [p for p in self.points]
  76. shape.fill = self.fill
  77. shape.selected = self.selected
  78. return shape
  79. def __len__(self):
  80. return len(self.points)
  81. def __getitem__(self, key):
  82. return self.points[key]
  83. def __setitem__(self, key, value):
  84. self.points[key] = value