Skip to content

Commit b1cf260

Browse files
committed
Canvas: Add zoom and remove zoom toggle button
1 parent 303fccf commit b1cf260

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

Orange/canvas/application/canvasmain.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,9 @@ def setup_ui(self):
291291

292292
tool_actions = self.current_document().toolbarActions()
293293

294-
(self.canvas_zoom_action, self.canvas_align_to_grid_action,
294+
(self.canvas_align_to_grid_action,
295295
self.canvas_text_action, self.canvas_arrow_action,) = tool_actions
296296

297-
self.canvas_zoom_action.setIcon(canvas_icons("Search.svg"))
298297
self.canvas_align_to_grid_action.setIcon(canvas_icons("Grid.svg"))
299298
self.canvas_text_action.setIcon(canvas_icons("Text Size.svg"))
300299
self.canvas_arrow_action.setIcon(canvas_icons("Arrow.svg"))
@@ -535,6 +534,21 @@ def setup_actions(self):
535534
shortcut=QKeySequence(Qt.ShiftModifier | Qt.Key_R)
536535
)
537536

537+
self.zoom_in_action = \
538+
QAction(self.tr("Zoom in"), self,
539+
triggered=self.zoom_in,
540+
shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_Plus))
541+
542+
self.zoom_out_action = \
543+
QAction(self.tr("Zoom out"), self,
544+
triggered=self.zoom_out,
545+
shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_Minus))
546+
547+
self.zoom_reset_action = \
548+
QAction(self.tr("Reset Zoom"), self,
549+
triggered=self.zoom_reset,
550+
shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_0))
551+
538552
if sys.platform == "darwin":
539553
# Actions for native Mac OSX look and feel.
540554
self.minimize_action = \
@@ -649,6 +663,12 @@ def setup_menu(self):
649663
self.view_menu.addAction(self.show_report_action)
650664

651665
self.view_menu.addSeparator()
666+
self.view_menu.addAction(self.zoom_in_action)
667+
self.view_menu.addAction(self.zoom_out_action)
668+
self.view_menu.addAction(self.zoom_reset_action)
669+
670+
self.view_menu.addSeparator()
671+
652672
self.view_menu.addAction(self.toogle_margins_action)
653673
menu_bar.addMenu(self.view_menu)
654674

@@ -1893,6 +1913,15 @@ def changeEvent(self, event):
18931913

18941914
QMainWindow.changeEvent(self, event)
18951915

1916+
def zoom_in(self):
1917+
self.scheme_widget.view().change_zoom(1)
1918+
1919+
def zoom_out(self):
1920+
self.scheme_widget.view().change_zoom(-1)
1921+
1922+
def zoom_reset(self):
1923+
self.scheme_widget.view().reset_zoom()
1924+
18961925
def sizeHint(self):
18971926
"""
18981927
Reimplemented from QMainWindow.sizeHint

Orange/canvas/canvas/view.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Canvas Graphics View
33
"""
44
import logging
5+
from math import floor, copysign
56

67
from AnyQt.QtWidgets import QGraphicsView
7-
from AnyQt.QtGui import QCursor, QIcon
8-
from AnyQt.QtCore import Qt, QRect, QSize, QRectF, QPoint, QTimer
8+
from AnyQt.QtGui import QCursor, QIcon, QTransform, QWheelEvent
9+
from AnyQt.QtCore import QT_VERSION, Qt, QRect, QSize, QRectF, QPoint, QTimer
910

1011
log = logging.getLogger(__name__)
1112

@@ -25,6 +26,8 @@ def __init__(self, *args):
2526
self.__autoScrollTimer = QTimer(self)
2627
self.__autoScrollTimer.timeout.connect(self.__autoScrollAdvance)
2728

29+
self.__scale = 10
30+
2831
def setScene(self, scene):
2932
QGraphicsView.setScene(self, scene)
3033
self._ensureSceneRect(scene)
@@ -63,6 +66,32 @@ def mouseReleaseEvent(self, event):
6366

6467
return QGraphicsView.mouseReleaseEvent(self, event)
6568

69+
def reset_zoom(self):
70+
self.__set_zoom(10)
71+
72+
def change_zoom(self, delta):
73+
self.__set_zoom(self.__scale + delta)
74+
75+
def __set_zoom(self, scale):
76+
self.__scale = min(15, max(scale, 3))
77+
transform = QTransform()
78+
transform.scale(self.__scale / 10, self.__scale / 10)
79+
self.setTransform(transform)
80+
81+
def wheelEvent(self, event: QWheelEvent):
82+
# use mouse position as anchor while zooming
83+
self.setTransformationAnchor(2)
84+
if event.modifiers() & Qt.ControlModifier:
85+
delta = event.angleDelta().y()
86+
if QT_VERSION >= 0x050300 \
87+
and event.source() != Qt.MouseEventNotSynthesized \
88+
and abs(delta) < 50:
89+
self.change_zoom(delta / 10)
90+
else:
91+
self.change_zoom(copysign(1, delta))
92+
else:
93+
super().wheelEvent(event)
94+
6695
def __shouldAutoScroll(self, pos):
6796
if self.__autoScroll:
6897
margin = self.__autoScrollMargin

Orange/canvas/document/schemeedit.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,6 @@ def __init__(self, parent=None, ):
171171
self.__linkMenu.addAction(self.__linkResetAction)
172172

173173
def __setupActions(self):
174-
175-
self.__zoomAction = \
176-
QAction(self.tr("Zoom"), self,
177-
objectName="zoom-action",
178-
checkable=True,
179-
shortcut=QKeySequence.ZoomIn,
180-
toolTip=self.tr("Zoom in the workflow."),
181-
toggled=self.toggleZoom,
182-
)
183-
184174
self.__cleanUpAction = \
185175
QAction(self.tr("Clean Up"), self,
186176
objectName="cleanup-action",
@@ -467,14 +457,12 @@ def toolbarActions(self):
467457
Return a list of actions that can be inserted into a toolbar.
468458
At the moment these are:
469459
470-
- 'Zoom' action
471460
- 'Clean up' action (align to grid)
472461
- 'New text annotation' action (with a size menu)
473462
- 'New arrow annotation' action (with a color menu)
474463
475464
"""
476-
return [self.__zoomAction,
477-
self.__cleanUpAction,
465+
return [self.__cleanUpAction,
478466
self.__newTextAnnotationAction,
479467
self.__newArrowAnnotationAction]
480468

@@ -867,18 +855,6 @@ def selectAll(self):
867855
if item.flags() & QGraphicsItem.ItemIsSelectable:
868856
item.setSelected(True)
869857

870-
def toggleZoom(self, zoom):
871-
"""
872-
Toggle view zoom. If `zoom` is True the scheme is displayed
873-
scaled to 150%.
874-
875-
"""
876-
view = self.view()
877-
if zoom:
878-
view.scale(1.5, 1.5)
879-
else:
880-
view.resetTransform()
881-
882858
def alignToGrid(self):
883859
"""
884860
Align nodes to a grid.

0 commit comments

Comments
 (0)