Jelajahi Sumber

Change canvas cursors depending on the operation

Michael Pitidis 13 tahun lalu
induk
melakukan
389c96d37b
1 mengubah file dengan 20 tambahan dan 1 penghapusan
  1. 20 1
      canvas.py

+ 20 - 1
canvas.py

@@ -6,11 +6,22 @@ from PyQt4.QtCore import *
 
 from shape import Shape
 
+# FIXME:
+# - After drawing a new shape, the cursor stops changing for a random
+#   amount of time.
+
 # TODO:
 # - [maybe] Add 2 painters, one for the pixmap one for the shape,
 #   since performance on big images is a problem...
 # - [maybe] Highlight source vertex when "attracting" line.
 # - [maybe] Find optimal epsilon value.
+# - [maybe] Add cursor stack so that releasing the mouse after moving
+#   pops the previous, grab cursor.
+
+CURSOR_DEFAULT = Qt.ArrowCursor
+CURSOR_DRAW    = Qt.CrossCursor
+CURSOR_MOVE    = Qt.ClosedHandCursor
+CURSOR_GRAB    = Qt.OpenHandCursor
 
 class Canvas(QWidget):
     zoomRequest = pyqtSignal(int)
@@ -61,9 +72,12 @@ class Canvas(QWidget):
         """Update line with last point and current coordinates."""
         pos = self.transformPos(ev.posF())
 
+        self.setCursor(CURSOR_DEFAULT)
+
         # Polygon copy moving.
         if Qt.RightButton & ev.buttons():
             if self.selectedShapeCopy and self.prevPoint:
+                self.setCursor(CURSOR_MOVE)
                 self.boundedMoveShape(self.selectedShapeCopy, pos)
                 self.repaint()
             elif self.selectedShape:
@@ -74,6 +88,9 @@ class Canvas(QWidget):
             return
 
         # Polygon drawing.
+        if self.editing():
+            self.setCursor(CURSOR_DRAW)
+
         if self.current and self.editing():
             color = self.lineColor
             if self.outOfPixmap(pos):
@@ -91,6 +108,7 @@ class Canvas(QWidget):
 
         # Polygon moving.
         if Qt.LeftButton & ev.buttons() and self.selectedShape and self.prevPoint:
+            self.setCursor(CURSOR_MOVE)
             self.boundedMoveShape(self.selectedShape, pos)
             self.shapeMoved.emit()
             self.repaint()
@@ -104,6 +122,7 @@ class Canvas(QWidget):
             if shape.containsPoint(pos) and self.isVisible(shape):
                 self.setToolTip("Object '%s'" % shape.label)
                 self.highlightedShape = shape
+                self.setCursor(CURSOR_GRAB)
                 break
         else:
             self.highlightedShape = None
@@ -404,7 +423,7 @@ class Canvas(QWidget):
         self.shapes = list(shapes)
         self.current = None
         self.repaint()
-        
+
     def copySelectedShape(self):
         if self.selectedShape:
             newShape=self.selectedShape.copy()