Slower zoom actions
[Skavegra.git] / svgviewer.cpp
blobb9c59c989e42c0ae0d061255d988472321fae18e
1 #include "svgviewer.h"
3 SVGViewer::SVGViewer(QWidget *parent) : QGraphicsView(parent) {
4 /* Init scene */
5 setScene(new QGraphicsScene(this));
6 setTransformationAnchor(AnchorUnderMouse);
7 setViewportUpdateMode(FullViewportUpdate);
10 void SVGViewer::paintEvent(QPaintEvent *event) {
11 QGraphicsView::paintEvent(event);
14 bool SVGViewer::openFile(QString fileName) {
15 svgItem = new QGraphicsSvgItem(fileName);
17 /* Reject invalid SVG documents */
18 if (!svgItem->renderer()->isValid()) {
19 /* TODO: Also show details in error message */
20 QMessageBox::critical(this, "Invalid SVG document",
21 "The selected file is not a valid SVG document and could not be opened.");
22 return false;
25 /* Set up scene */
26 setDragMode(ScrollHandDrag);
28 scene()->clear();
29 resetTransform();
31 svgItem->setFlags(QGraphicsItem::ItemClipsToShape);
32 svgItem->setCacheMode(QGraphicsItem::NoCache);
34 /* Show image */
35 scene()->addItem(svgItem);
36 return true;
39 void SVGViewer::closeFile() {
40 delete svgItem;
41 svgItem = NULL;
42 /* Reset view parameters */
43 scene()->clear();
44 resetTransform();
45 setDragMode(NoDrag);
48 void SVGViewer::zoomIn() {
49 zoomChange(1.25);
51 void SVGViewer::zoomOut() {
52 zoomChange(0.875);
54 void SVGViewer::zoomReset() {
55 if(svgItem != NULL) {
56 resetTransform();
60 void SVGViewer::zoomChange(qreal factor) {
61 if(svgItem != NULL) {
62 scale(factor, factor);
66 void SVGViewer::wheelEvent(QWheelEvent *event) {
67 /* Change zoom on using the mouse wheel */
68 qreal factor = qPow(1.2, event->delta() / 240.0);
69 zoomChange(factor);
70 event->accept();