From 9617c999c365f0af4e3518e9f87eb7334cc90d0e Mon Sep 17 00:00:00 2001 From: janezd Date: Fri, 22 Dec 2017 09:53:36 +0100 Subject: [PATCH] Canvas: Add zoom and remove zoom toggle button --- Orange/canvas/application/canvasmain.py | 33 ++++++++++++++++++++++-- Orange/canvas/canvas/view.py | 34 +++++++++++++++++++++++-- Orange/canvas/document/schemeedit.py | 26 +------------------ 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Orange/canvas/application/canvasmain.py b/Orange/canvas/application/canvasmain.py index d4c677d6182..5e74c182ce1 100644 --- a/Orange/canvas/application/canvasmain.py +++ b/Orange/canvas/application/canvasmain.py @@ -291,10 +291,9 @@ def setup_ui(self): tool_actions = self.current_document().toolbarActions() - (self.canvas_zoom_action, self.canvas_align_to_grid_action, + (self.canvas_align_to_grid_action, self.canvas_text_action, self.canvas_arrow_action,) = tool_actions - self.canvas_zoom_action.setIcon(canvas_icons("Search.svg")) self.canvas_align_to_grid_action.setIcon(canvas_icons("Grid.svg")) self.canvas_text_action.setIcon(canvas_icons("Text Size.svg")) self.canvas_arrow_action.setIcon(canvas_icons("Arrow.svg")) @@ -535,6 +534,21 @@ def setup_actions(self): shortcut=QKeySequence(Qt.ShiftModifier | Qt.Key_R) ) + self.zoom_in_action = \ + QAction(self.tr("Zoom in"), self, + triggered=self.zoom_in, + shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_Plus)) + + self.zoom_out_action = \ + QAction(self.tr("Zoom out"), self, + triggered=self.zoom_out, + shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_Minus)) + + self.zoom_reset_action = \ + QAction(self.tr("Reset Zoom"), self, + triggered=self.zoom_reset, + shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_0)) + if sys.platform == "darwin": # Actions for native Mac OSX look and feel. self.minimize_action = \ @@ -649,6 +663,12 @@ def setup_menu(self): self.view_menu.addAction(self.show_report_action) self.view_menu.addSeparator() + self.view_menu.addAction(self.zoom_in_action) + self.view_menu.addAction(self.zoom_out_action) + self.view_menu.addAction(self.zoom_reset_action) + + self.view_menu.addSeparator() + self.view_menu.addAction(self.toogle_margins_action) menu_bar.addMenu(self.view_menu) @@ -1893,6 +1913,15 @@ def changeEvent(self, event): QMainWindow.changeEvent(self, event) + def zoom_in(self): + self.scheme_widget.view().change_zoom(1) + + def zoom_out(self): + self.scheme_widget.view().change_zoom(-1) + + def zoom_reset(self): + self.scheme_widget.view().reset_zoom() + def sizeHint(self): """ Reimplemented from QMainWindow.sizeHint diff --git a/Orange/canvas/canvas/view.py b/Orange/canvas/canvas/view.py index edf7b87343e..2343e681a5c 100644 --- a/Orange/canvas/canvas/view.py +++ b/Orange/canvas/canvas/view.py @@ -2,10 +2,11 @@ Canvas Graphics View """ import logging +from math import copysign from AnyQt.QtWidgets import QGraphicsView -from AnyQt.QtGui import QCursor, QIcon -from AnyQt.QtCore import Qt, QRect, QSize, QRectF, QPoint, QTimer +from AnyQt.QtGui import QCursor, QIcon, QTransform, QWheelEvent +from AnyQt.QtCore import QT_VERSION, Qt, QRect, QSize, QRectF, QPoint, QTimer log = logging.getLogger(__name__) @@ -25,6 +26,8 @@ def __init__(self, *args): self.__autoScrollTimer = QTimer(self) self.__autoScrollTimer.timeout.connect(self.__autoScrollAdvance) + self.__scale = 10 + def setScene(self, scene): QGraphicsView.setScene(self, scene) self._ensureSceneRect(scene) @@ -63,6 +66,33 @@ def mouseReleaseEvent(self, event): return QGraphicsView.mouseReleaseEvent(self, event) + def reset_zoom(self): + self.__set_zoom(10) + + def change_zoom(self, delta): + self.__set_zoom(self.__scale + delta) + + def __set_zoom(self, scale): + self.__scale = min(15, max(scale, 3)) + transform = QTransform() + transform.scale(self.__scale / 10, self.__scale / 10) + self.setTransform(transform) + + def wheelEvent(self, event: QWheelEvent): + # use mouse position as anchor while zooming + self.setTransformationAnchor(2) + if event.modifiers() & Qt.ControlModifier \ + and event.buttons() == Qt.NoButton: + delta = event.angleDelta().y() + if QT_VERSION >= 0x050500 \ + and event.source() != Qt.MouseEventNotSynthesized \ + and abs(delta) < 50: + self.change_zoom(delta / 10) + else: + self.change_zoom(copysign(1, delta)) + else: + super().wheelEvent(event) + def __shouldAutoScroll(self, pos): if self.__autoScroll: margin = self.__autoScrollMargin diff --git a/Orange/canvas/document/schemeedit.py b/Orange/canvas/document/schemeedit.py index 6eac780c34c..1437a7f5fc3 100644 --- a/Orange/canvas/document/schemeedit.py +++ b/Orange/canvas/document/schemeedit.py @@ -171,16 +171,6 @@ def __init__(self, parent=None, ): self.__linkMenu.addAction(self.__linkResetAction) def __setupActions(self): - - self.__zoomAction = \ - QAction(self.tr("Zoom"), self, - objectName="zoom-action", - checkable=True, - shortcut=QKeySequence.ZoomIn, - toolTip=self.tr("Zoom in the workflow."), - toggled=self.toggleZoom, - ) - self.__cleanUpAction = \ QAction(self.tr("Clean Up"), self, objectName="cleanup-action", @@ -467,14 +457,12 @@ def toolbarActions(self): Return a list of actions that can be inserted into a toolbar. At the moment these are: - - 'Zoom' action - 'Clean up' action (align to grid) - 'New text annotation' action (with a size menu) - 'New arrow annotation' action (with a color menu) """ - return [self.__zoomAction, - self.__cleanUpAction, + return [self.__cleanUpAction, self.__newTextAnnotationAction, self.__newArrowAnnotationAction] @@ -867,18 +855,6 @@ def selectAll(self): if item.flags() & QGraphicsItem.ItemIsSelectable: item.setSelected(True) - def toggleZoom(self, zoom): - """ - Toggle view zoom. If `zoom` is True the scheme is displayed - scaled to 150%. - - """ - view = self.view() - if zoom: - view.scale(1.5, 1.5) - else: - view.resetTransform() - def alignToGrid(self): """ Align nodes to a grid.