Преглед на файлове

Support Ctrl/Command Key + mouse scrolling for zoom in/out

Kentaro Wada преди 7 години
родител
ревизия
403e0571f6
променени са 2 файла, в които са добавени 17 реда и са изтрити 22 реда
  1. 7 8
      labelme/app.py
  2. 10 14
      labelme/canvas.py

+ 7 - 8
labelme/app.py

@@ -32,11 +32,11 @@ try:
     from PyQt5.QtGui import *
     from PyQt5.QtCore import *
     from PyQt5.QtWidgets import *
-    PYQT_VERSION = 5
+    PYQT5 = True
 except ImportError:
     from PyQt4.QtGui import *
     from PyQt4.QtCore import *
-    PYQT_VERSION = 4
+    PYQT5 = False
 
 from labelme import resources
 from labelme.lib import struct, newAction, newIcon, addActions, fmtShortcut
@@ -615,7 +615,7 @@ class MainWindow(QMainWindow, WindowMixin):
             self.canvas.undoLastLine()
 
     def scrollRequest(self, delta, orientation):
-        units = - delta / (8 * 15)
+        units = - delta * 0.1 # natural scroll
         bar = self.scrollBars[orientation]
         bar.setValue(bar.value() + bar.singleStep() * units)
 
@@ -629,9 +629,8 @@ class MainWindow(QMainWindow, WindowMixin):
         self.setZoom(self.zoomWidget.value() + increment)
 
     def zoomRequest(self, delta):
-        units = delta / (8 * 15)
-        scale = 10
-        self.addZoom(scale * units)
+        units = delta * 0.1
+        self.addZoom(units)
 
     def setFitWindow(self, value=True):
         if value:
@@ -769,7 +768,7 @@ class MainWindow(QMainWindow, WindowMixin):
                 ' '.join(formats + ['*%s' % LabelFile.suffix])
         filename = QFileDialog.getOpenFileName(self,
             '%s - Choose Image or Label file' % __appname__, path, filters)
-        if PYQT_VERSION >= 5:
+        if PYQT5:
             filename, _ = filename
         filename = str(filename)
         if filename:
@@ -804,7 +803,7 @@ class MainWindow(QMainWindow, WindowMixin):
         filename = dlg.getSaveFileName(
             self, 'Choose File', default_labelfile_name,
             'Label files (*%s)' % LabelFile.suffix)
-        if PYQT_VERSION >= 5:
+        if PYQT5:
             filename, _ = filename
         filename = str(filename)
         return filename

+ 10 - 14
labelme/canvas.py

@@ -492,23 +492,19 @@ class Canvas(QWidget):
 
     def wheelEvent(self, ev):
         if PYQT5:
-            if not hasattr(ev, 'inverted'):
-                # supported >=5.9
-                return
-            if ev.inverted():
-                mods = ev.modifiers()
-                if Qt.ControlModifier == int(mods):
-                    self.zoomRequest.emit(ev.pixelDelta())
-                else:
-                    self.scrollRequest.emit(ev.pixelDelta(),
-                            Qt.Horizontal if (Qt.ShiftModifier == int(mods))\
-                                          else Qt.Vertical)
+            mods = ev.modifiers()
+            delta = ev.pixelDelta()
+            if Qt.ControlModifier == int(mods):  # with Ctrl/Command key
+                # zoom
+                self.zoomRequest.emit(delta.y())
             else:
-                self.scrollRequest.emit(ev.pixelDelta(), Qt.Horizontal)
+                # scroll
+                self.scrollRequest.emit(delta.x(), Qt.Horizontal)
+                self.scrollRequest.emit(delta.y(), Qt.Vertical)
         else:
-            if ev.orientation() == qt.Vertical:
+            if ev.orientation() == Qt.Vertical:
                 mods = ev.modifiers()
-                if Qt.ControlModifier == int(mods):
+                if Qt.ControlModifier == int(mods):  # with Ctrl/Command key
                     self.zoomRequest.emit(ev.delta())
                 else:
                     self.scrollRequest.emit(ev.delta(),