canvas.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from math import sqrt
  2. from PyQt4.QtGui import *
  3. from PyQt4.QtCore import *
  4. from shape import Shape
  5. class Canvas(QWidget):
  6. epsilon = 9.0 # TODO: Tune value
  7. def __init__(self, *args, **kwargs):
  8. super(Canvas, self).__init__(*args, **kwargs)
  9. self.shapes = []
  10. self.current = None
  11. self.line_color = QColor(0, 0, 255)
  12. self.line = Shape(line_color=self.line_color)
  13. self.scale = 1.0
  14. self.pixmap = None
  15. self.setFocusPolicy(Qt.WheelFocus)
  16. def mouseMoveEvent(self, ev):
  17. """Update line with last point and current coordinates."""
  18. # Don't allow the user to draw outside the image area.
  19. # FIXME: When making fast mouse movements, there is not enough
  20. # spatial resolution to leave the cursor at the edge of the
  21. # picture. We should probably place the line at the projected
  22. # position here...
  23. pos = self.transformPos(ev.posF())
  24. if self.outOfPixmap(pos):
  25. return ev.ignore()
  26. if self.current:
  27. if len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
  28. self.line[1] = self.current[0]
  29. self.line.line_color = self.current.line_color
  30. else:
  31. self.line[1] = pos
  32. self.line.line_color = self.line_color
  33. self.repaint()
  34. def mousePressEvent(self, ev):
  35. if ev.button() == 1:
  36. if self.current:
  37. self.current.addPoint(self.line[1])
  38. self.line[0] = self.current[-1]
  39. if self.current.isClosed():
  40. self.finalise()
  41. self.repaint()
  42. else:
  43. pos = self.transformPos(ev.posF())
  44. self.current = Shape()
  45. self.line.points = [pos, pos]
  46. self.current.addPoint(pos)
  47. self.setMouseTracking(True)
  48. def mouseDoubleClickEvent(self, ev):
  49. if self.current:
  50. # Add first point in the list so that it is consistent
  51. # with shapes created the normal way.
  52. self.current.addPoint(self.current[0])
  53. self.finalise()
  54. def paintEvent(self, event):
  55. if not self.pixmap:
  56. return super(Canvas, self).paintEvent(event)
  57. p = QPainter()
  58. p.begin(self)
  59. p.setRenderHint(QPainter.Antialiasing)
  60. p.scale(self.scale, self.scale)
  61. p.translate(self.offsetToCenter())
  62. p.drawPixmap(0, 0, self.pixmap)
  63. for shape in self.shapes:
  64. shape.paint(p)
  65. if self.current:
  66. self.current.paint(p)
  67. self.line.paint(p)
  68. p.end()
  69. def transformPos(self, point):
  70. """Convert from widget-logical coordinates to painter-logical coordinates."""
  71. return point / self.scale - self.offsetToCenter()
  72. def offsetToCenter(self):
  73. s = self.scale
  74. area = super(Canvas, self).size()
  75. w, h = self.pixmap.width() * s, self.pixmap.height() * s
  76. aw, ah = area.width(), area.height()
  77. x = (aw-w)/(2*s) if aw > w else 0
  78. y = (ah-h)/(2*s) if ah > h else 0
  79. return QPointF(x, y)
  80. def outOfPixmap(self, p):
  81. w, h = self.pixmap.width(), self.pixmap.height()
  82. return not (0 <= p.x() <= w and 0 <= p.y() <= h)
  83. def finalise(self):
  84. assert self.current
  85. self.current.fill = True
  86. self.shapes.append(self.current)
  87. self.current = None
  88. # TODO: Mouse tracking is still useful for selecting shapes!
  89. self.setMouseTracking(False)
  90. self.repaint()
  91. def closeEnough(self, p1, p2):
  92. #d = distance(p1 - p2)
  93. #m = (p1-p2).manhattanLength()
  94. #print "d %.2f, m %d, %.2f" % (d, m, d - m)
  95. return distance(p1 - p2) < self.epsilon
  96. # These two, along with a call to adjustSize are required for the
  97. # scroll area.
  98. def sizeHint(self):
  99. return self.minimumSizeHint()
  100. def minimumSizeHint(self):
  101. if self.pixmap:
  102. return self.scale * self.pixmap.size()
  103. return super(Canvas, self).minimumSizeHint()
  104. def distance(p):
  105. return sqrt(p.x() * p.x() + p.y() * p.y())