4.1  imageviewer – Image viewer with panning and zooming

4.1.1  Inheritance Diagram

Inheritance diagram of imageviewer

4.1.2  Description

This module implements the ImageViewer class which allows panning and zooming of QPixmaps by using a SynchableGraphicsView embedded inside a QFrame.

It contains the following classes:

4.1.3  Reference

4.1.3.1  ImageViewer

class imageviewer.ImageViewer(pixmap=None, name=None)[source]

Bases: PyQt4.QtGui.QFrame

Image Viewer than can pan & zoom images (QPixmaps).

Parameters:
  • pixmap (QPixmap or None) – QPixmap to display

  • name (str or None) – name associated with this ImageViewer

sceneChanged

Scene Changed Signal.

Emitted whenever the QGraphicsScene content changes.

transformChanged

Transformed Changed Signal.

Emitted whenever the QGraphicsView Transform matrix has been changed.

scrollChanged

Scroll Changed Signal.

Emitted whenever the scrollbar position or range has changed.

connectSbarSignals(slot)[source]

Connect to scrollbar changed signals.

Parameters:
  • slot – slot to connect scrollbar signals to.

pixmap[source]

The currently viewed QPixmap (QPixmap).

viewName[source]

The name associated with ImageViewer (str).

handDragging[source]

Hand dragging state (bool)

scrollState[source]

Tuple of percentage of scene extents (sceneWidthPercent, sceneHeightPercent)

zoomFactor[source]

Zoom scale factor (float).

scrollToTop()[source]

Scroll to top of image.

scrollToBottom()[source]

Scroll to bottom of image.

scrollToBegin()[source]

Scroll to left side of image.

scrollToEnd()[source]

Scroll to right side of image.

centerView()[source]

Center image in view.

enableScrollBars(enable)[source]

Set visiblility of the view’s scrollbars.

Parameters:
  • enable (bool) – True to enable the scrollbars

enableHandDrag(enable)[source]

Set whether dragging the view with the hand cursor is allowed.

Parameters:
  • enable (bool) – True to enable hand dragging

zoomIn()[source]

Zoom in on image.

zoomOut()[source]

Zoom out on image.

actualSize()[source]

Change zoom to show image at actual size.

(image pixel is equal to screen pixel)

fitToWindow()[source]

Fit image within view.

fitWidth()[source]

Fit image width to view width.

fitHeight()[source]

Fit image height to view height.

handleWheelNotches(notches)[source]

Handle wheel notch event from underlying QGraphicsView.

Parameters:
  • notches (float) – Mouse wheel notches

closeEvent(event)[source]

Overriden in order to disconnect scrollbar signals before closing.

Parameters:
  • event (QEvent) – instance of a QEvent

If this isn’t done Python crashes!

scaleImage(factor, combine=True)[source]

Scale image by factor.

Parameters:
dumpTransform()[source]

Dump view transform to stdout.

4.1.3.2  SynchableGraphicsView

class imageviewer.SynchableGraphicsView(scene=None, parent=None)[source]

Bases: PyQt4.QtGui.QGraphicsView

QGraphicsView that can synchronize panning & zooming of multiple instances.

Also adds support for various scrolling operations and mouse wheel zooming.

Parameters:
  • scene (QGraphicsScene or None) – initial QGraphicsScene

  • QWidget (QWidget or None) – parent widget

transformChanged

Transformed Changed Signal.

Emitted whenever the QGraphicsView Transform matrix has been changed.

scrollChanged

Scroll Changed Signal.

Emitted whenever the scrollbar position or range has changed.

wheelNotches

Wheel Notches Signal (float).

Emitted whenever the mouse wheel has been rolled. A wheelnotch is equal to wheel delta / 240

connectSbarSignals(slot)[source]

Connect to scrollbar changed signals to synchronize panning.

Parameters:
  • slot – slot to connect scrollbar signals to.

disconnectSbarSignals()[source]

Disconnect from scrollbar changed signals.

handDragging[source]

Hand dragging state (bool)

scrollState[source]

Tuple of percentage of scene extents (sceneWidthPercent, sceneHeightPercent)

zoomFactor[source]

Zoom scale factor (float).

wheelEvent(wheelEvent)[source]

Overrides the wheelEvent to handle zooming.

Parameters:
keyReleaseEvent(keyEvent)[source]

Overrides to make sure key release passed on to other classes.

Parameters:
  • keyEvent (QKeyEvent) – instance of QKeyEvent

checkTransformChanged()[source]

Return True if view transform has changed.

Overkill. For current implementation really only need to check if m11() has changed.

Return type:bool
clearTransformChanges()[source]

Reset view transform changed info.

scrollToTop()[source]

Scroll view to top.

scrollToBottom()[source]

Scroll view to bottom.

scrollToBegin()[source]

Scroll view to left edge.

scrollToEnd()[source]

Scroll view to right edge.

centerView()[source]

Center view.

enableScrollBars(enable)[source]

Set visiblility of the view’s scrollbars.

Parameters:
  • enable (bool) – True to enable the scrollbars

enableHandDrag(enable)[source]

Set whether dragging the view with the hand cursor is allowed.

Parameters:
  • enable (bool) – True to enable hand dragging

dumpTransform(t, padding=u'')[source]

Dump the transform t to stdout.

Parameters:
  • t – the transform to dump

  • padding (str) – each line is preceded by padding

4.1.3.3  MainWindow

class imageviewer.MainWindow(pixmap)[source]

Bases: PyQt4.QtGui.QMainWindow

Sample app to test the ImageViewer class.

Parameters:
  • pixmap (QPixmap) – QPixmap to display

Usage:

python imageviewer.py imagefilename
createActions()[source]

Create actions for the menus.

createMenus()[source]

Create the menus.

sceneChanged(rects)[source]

Triggered when the underlying graphics scene has changed.

Parameters:
  • rects (list) – scene rectangles that indicate the area that has been changed.

transformChanged()[source]

Triggered when the underlying view has been scaled, translated, or rotated.

In practice, only scaling occurs.

scrollChanged()[source]

Triggered when the views scrollbars have changed.

keyPressEvent(keyEvent)[source]

Overrides to enable panning while dragging.

Parameters:
  • keyEvent (QKeyEvent) – instance of QKeyEvent

keyReleaseEvent(keyEvent)[source]

Overrides to disable panning while dragging.

Parameters:
  • keyEvent (QKeyEvent) – instance of QKeyEvent

closeEvent(event)[source]

Overrides close event to save application settings.

Parameters:
  • event (QEvent) – instance of QEvent

writeSettings()[source]

Write application settings.

readSettings()[source]

Read application settings.