canvas.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. self.mouseButtonIsPressed=False #when it is true and shape is selected , move the shape with the mouse move event
  16. def mouseMoveEvent(self, ev):
  17. """Update line with last point and current coordinates."""
  18. if self.current and self.startLabeling:
  19. if len(self.current) > 1 and self.closeEnough(ev.pos(), self.current[0]):
  20. self.line[1] = self.current[0]
  21. self.line.line_color = self.current.line_color
  22. else:
  23. self.line[1] = ev.pos()
  24. self.line.line_color = self.line_color
  25. self.repaint()
  26. return
  27. if self.mouseButtonIsPressed:
  28. print ev.x()
  29. def mousePressEvent(self, ev):
  30. if ev.button() == 1:
  31. if self.startLabeling:
  32. if self.current :
  33. self.current.addPoint(self.line[1])
  34. self.line[0] = self.current[-1]
  35. if self.current.isClosed():
  36. self.finalise()
  37. self.repaint()
  38. else:
  39. self.current = Shape()
  40. self.line.points = [ev.pos(), ev.pos()]
  41. self.current.addPoint(ev.pos())
  42. self.setMouseTracking(True)
  43. else: # not in adding new label mode
  44. self.selectShape(ev.pos())
  45. def mouseDoubleClickEvent(self, ev):
  46. if self.current and self.startLabeling:
  47. # Add first point in the list so that it is consistent
  48. # with shapes created the normal way.
  49. self.current.addPoint(self.current[0])
  50. self.finalise()
  51. def selectShape(self,point):
  52. self.deSelectShape()
  53. for shape in self.shapes:
  54. if shape.containPoint(point):
  55. shape.selected=True
  56. self.selectedShape=shape
  57. self.repaint()
  58. return
  59. #shape.select()
  60. def deSelectShape(self):
  61. if self.selectedShape:
  62. self.selectedShape.selected=False
  63. self.repaint()
  64. def paintEvent(self, event):
  65. super(Canvas, self).paintEvent(event)
  66. qp = QPainter()
  67. qp.begin(self)
  68. qp.setRenderHint(QPainter.Antialiasing)
  69. for shape in self.shapes:
  70. shape.paint(qp)
  71. if self.current:
  72. self.current.paint(qp)
  73. self.line.paint(qp)
  74. qp.end()
  75. def finalise(self):
  76. assert self.current
  77. self.current.fill = True
  78. self.shapes.append(self.current)
  79. self.current = None
  80. self.startLabeling=False
  81. # TODO: Mouse tracking is still useful for selecting shapes!
  82. self.setMouseTracking(False)
  83. self.repaint()
  84. def closeEnough(self, p1, p2):
  85. #d = distance(p1 - p2)
  86. #m = (p1-p2).manhattanLength()
  87. #print "d %.2f, m %d, %.2f" % (d, m, d - m)
  88. return distance(p1 - p2) < self.epsilon
  89. def distance(p):
  90. return sqrt(p.x() * p.x() + p.y() * p.y())