Эх сурвалжийг харах

Add some wheel & key controls to canvas

Ctrl+wheel controls vertical zooming, while plain wheel events scroll the
canvas and Shift-wheel performs horizontal scroll.

In addition, the Escape key cancels the currently drawn shape.
Michael Pitidis 13 жил өмнө
parent
commit
7486c5809b
2 өөрчлөгдсөн 43 нэмэгдсэн , 0 устгасан
  1. 23 0
      canvas.py
  2. 20 0
      labelme.py

+ 23 - 0
canvas.py

@@ -7,6 +7,9 @@ from PyQt4.QtCore import *
 from shape import Shape
 
 class Canvas(QWidget):
+    zoomRequest = pyqtSignal(int)
+    scrollRequest = pyqtSignal(int, int)
+
     epsilon = 9.0 # TODO: Tune value
 
     def __init__(self, *args, **kwargs):
@@ -125,6 +128,26 @@ class Canvas(QWidget):
             return self.scale * self.pixmap.size()
         return super(Canvas, self).minimumSizeHint()
 
+    def wheelEvent(self, ev):
+        if ev.orientation() == Qt.Vertical:
+            mods = ev.modifiers()
+            if Qt.ControlModifier == int(mods):
+                self.zoomRequest.emit(ev.delta())
+            else:
+                self.scrollRequest.emit(ev.delta(),
+                        Qt.Horizontal if (Qt.ShiftModifier == int(mods))\
+                                      else Qt.Vertical)
+        else:
+            self.scrollRequest.emit(ev.delta(), Qt.Horizontal)
+        ev.accept()
+
+    def keyPressEvent(self, ev):
+        if ev.key() == Qt.Key_Escape and self.current:
+            self.current = None
+            self.setMouseTracking(False)
+            self.repaint()
+
+
 def distance(p):
     return sqrt(p.x() * p.x() + p.y() * p.y())
 

+ 20 - 0
labelme.py

@@ -17,6 +17,8 @@ from zoomwidget import ZoomWidget
 
 __appname__ = 'labelme'
 
+# TODO:
+# - Zoom is too "steppy".
 
 ### Utility functions and classes.
 
@@ -82,10 +84,16 @@ class MainWindow(QMainWindow, WindowMixin):
         self.canvas = Canvas()
         #self.canvas.setAlignment(Qt.AlignCenter)
         self.canvas.setContextMenuPolicy(Qt.ActionsContextMenu)
+        self.canvas.zoomRequest.connect(self.zoomRequest)
 
         scroll = QScrollArea()
         scroll.setWidget(self.canvas)
         scroll.setWidgetResizable(True)
+        self.scrollBars = {
+            Qt.Vertical: scroll.verticalScrollBar(),
+            Qt.Horizontal: scroll.horizontalScrollBar()
+            }
+        self.canvas.scrollRequest.connect(self.scrollRequest)
 
         self.setCentralWidget(scroll)
         self.addDockWidget(Qt.BottomDockWidgetArea, self.dock)
@@ -157,6 +165,18 @@ class MainWindow(QMainWindow, WindowMixin):
 
 
     ## Callback functions:
+    def scrollRequest(self, delta, orientation):
+        units = - delta / (8 * 15)
+        bar = self.scrollBars[orientation]
+        bar.setValue(bar.value() + bar.singleStep() * units)
+
+    def zoomRequest(self, delta):
+        if not self.fit_window:
+            units = delta / (8 * 15)
+            scale = 10
+            self.zoom_widget.setValue(self.zoom_widget.value() + scale * units)
+            self.zoom_widget.editingFinished.emit()
+
     def setFitWindow(self, value=True):
         self.zoom_widget.setEnabled(not value)
         self.fit_window = value