Skip to content

Commit

Permalink
Merge pull request #2841 from janezd/canvas-zoom
Browse files Browse the repository at this point in the history
[ENH] Canvas: Add zoom
  • Loading branch information
lanzagar authored Jan 19, 2018
2 parents f6d4f32 + 38dc9bb commit 17f3105
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
33 changes: 31 additions & 2 deletions Orange/canvas/application/canvasmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,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"))
Expand Down Expand Up @@ -536,6 +535,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 = \
Expand Down Expand Up @@ -650,6 +664,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)

Expand Down Expand Up @@ -1894,6 +1914,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
Expand Down
34 changes: 32 additions & 2 deletions Orange/canvas/canvas/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 1 addition & 25 deletions Orange/canvas/document/schemeedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 17f3105

Please sign in to comment.