Fix #1968472 [[Enhancement - Image viewer] Add ability to scroll to zoom while holding down Ctrl](https://bugs.launchpad.net/calibre/+bug/1968472)

This commit is contained in:
Kovid Goyal 2022-04-12 07:30:04 +05:30
parent 6b773361c2
commit cf33d39a2e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -38,6 +38,7 @@ def render_svg(widget, path):
class Label(QLabel): class Label(QLabel):
toggle_fit = pyqtSignal() toggle_fit = pyqtSignal()
zoom_requested = pyqtSignal(bool)
def __init__(self, scrollarea): def __init__(self, scrollarea):
super().__init__(scrollarea) super().__init__(scrollarea)
@ -48,6 +49,7 @@ class Label(QLabel):
self.in_drag = False self.in_drag = False
self.prev_drag_position = None self.prev_drag_position = None
self.scrollarea = scrollarea self.scrollarea = scrollarea
self.current_wheel_angle_delta = 0
@property @property
def is_pannable(self): def is_pannable(self):
@ -74,6 +76,17 @@ class Label(QLabel):
self.dragged(pos.x() - p.x(), pos.y() - p.y()) self.dragged(pos.x() - p.x(), pos.y() - p.y())
return super().mouseMoveEvent(ev) return super().mouseMoveEvent(ev)
def wheelEvent(self, ev):
if ev.modifiers() == Qt.KeyboardModifier.ControlModifier:
ad = ev.angleDelta().y()
if ad * self.current_wheel_angle_delta < 0:
self.current_wheel_angle_delta = 0
self.current_wheel_angle_delta += ad
if abs(self.current_wheel_angle_delta) >= 120:
self.zoom_requested.emit(self.current_wheel_angle_delta < 0)
self.current_wheel_angle_delta = 0
ev.accept()
def dragged(self, dx, dy): def dragged(self, dx, dy):
h = self.scrollarea.horizontalScrollBar() h = self.scrollarea.horizontalScrollBar()
if h.isVisible(): if h.isVisible():
@ -111,6 +124,7 @@ class ImageView(QDialog):
sa.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter) sa.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
sa.setBackgroundRole(QPalette.ColorRole.Dark) sa.setBackgroundRole(QPalette.ColorRole.Dark)
self.label = l = Label(sa) self.label = l = Label(sa)
l.zoom_requested.connect(self.zoom_requested)
sa.toggle_fit.connect(self.toggle_fit) sa.toggle_fit.connect(self.toggle_fit)
sa.setWidget(l) sa.setWidget(l)
@ -178,6 +192,10 @@ class ImageView(QDialog):
actual_height = self.current_img.size().height() actual_height = self.current_img.size().height()
return scaled_height / actual_height return scaled_height / actual_height
def zoom_requested(self, zoom_out):
if (zoom_out and self.zo_button.isEnabled()) or (not zoom_out and self.zi_button.isEnabled()):
(self.zoom_out if zoom_out else self.zoom_in)()
def zoom_in(self): def zoom_in(self):
if self.fit_image.isChecked(): if self.fit_image.isChecked():
factor = self.factor_from_fit() factor = self.factor_from_fit()
@ -285,12 +303,6 @@ class ImageView(QDialog):
else: else:
self.showNormal() self.showNormal()
def wheelEvent(self, event):
d = event.angleDelta().y()
if abs(d) > 0 and not self.scrollarea.verticalScrollBar().isVisible():
event.accept()
(self.zoom_out if d < 0 else self.zoom_in)()
class ImagePopup: class ImagePopup: