shape.py 3.4 KB

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