canvas.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from math import sqrt
  2. from PyQt4.QtGui import *
  3. from PyQt4.QtCore import *
  4. from shape import Shape
  5. class Canvas(QLabel):
  6. epsilon = 9.0 # TODO: Tune value
  7. def __init__(self, *args, **kwargs):
  8. super(Canvas, self).__init__(*args, **kwargs)
  9. self.startLabeling=False # has to click new label buttoon to starting drawing new polygons
  10. self.shapes = []
  11. self.current = None
  12. self.selectedShape=None # save the selected shape here
  13. self.line_color = QColor(0, 0, 255)
  14. self.line = Shape(line_color=self.line_color)
  15. def mouseMoveEvent(self, ev):
  16. """Update line with last point and current coordinates."""
  17. if self.current and self.startLabeling:
  18. if len(self.current) > 1 and self.closeEnough(ev.pos(), self.current[0]):
  19. self.line[1] = self.current[0]
  20. self.line.line_color = self.current.line_color
  21. else:
  22. self.line[1] = ev.pos()
  23. self.line.line_color = self.line_color
  24. self.repaint()
  25. def mousePressEvent(self, ev):
  26. if ev.button() == 1:
  27. if self.startLabeling:
  28. if self.current:
  29. self.current.addPoint(self.line[1])
  30. self.line[0] = self.current[-1]
  31. if self.current.isClosed():
  32. self.finalise()
  33. self.repaint()
  34. else:
  35. self.current = Shape()
  36. self.line.points = [ev.pos(), ev.pos()]
  37. self.current.addPoint(ev.pos())
  38. self.setMouseTracking(True)
  39. else: # not in adding new label mode
  40. self.selectShape(ev.pos())
  41. def mouseDoubleClickEvent(self, ev):
  42. if self.current and self.startLabeling:
  43. # Add first point in the list so that it is consistent
  44. # with shapes created the normal way.
  45. self.current.addPoint(self.current[0])
  46. self.finalise()
  47. def selectShape(self, point):
  48. """Select the first shape created which contains this point."""
  49. self.deSelectShape()
  50. for shape in self.shapes:
  51. if shape.containsPoint(point):
  52. shape.selected = True
  53. self.selectedShape = shape
  54. return self.repaint()
  55. def deSelectShape(self):
  56. if self.selectedShape:
  57. self.selectedShape.selected = False
  58. self.repaint()
  59. def paintEvent(self, event):
  60. super(Canvas, self).paintEvent(event)
  61. qp = QPainter()
  62. qp.begin(self)
  63. qp.setRenderHint(QPainter.Antialiasing)
  64. for shape in self.shapes:
  65. shape.paint(qp)
  66. if self.current:
  67. self.current.paint(qp)
  68. self.line.paint(qp)
  69. qp.end()
  70. def finalise(self):
  71. assert self.current
  72. self.current.fill = True
  73. self.shapes.append(self.current)
  74. self.current = None
  75. self.startLabeling = False
  76. # TODO: Mouse tracking is still useful for selecting shapes!
  77. self.setMouseTracking(False)
  78. self.repaint()
  79. def closeEnough(self, p1, p2):
  80. #d = distance(p1 - p2)
  81. #m = (p1-p2).manhattanLength()
  82. #print "d %.2f, m %d, %.2f" % (d, m, d - m)
  83. return distance(p1 - p2) < self.epsilon
  84. def distance(p):
  85. return sqrt(p.x() * p.x() + p.y() * p.y())