1
/* ============================================================
3
* Description : Preview image viewer.
5
* Copyright (C) 2008 by Kare Sars <kare dot sars at iki dot fi>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of
10
* the License or (at your option) version 3 or any later version
11
* accepted by the membership of KDE e.V. (or its successor approved
12
* by the membership of KDE e.V.), which shall act as a proxy
13
* defined in Section 14 of version 3 of the license.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License.
21
* along with this program. If not, see <http://www.gnu.org/licenses/>
23
* ============================================================ */
25
#include "ImageViewer.h"
26
#include "ImageViewer.moc"
28
#include <QGraphicsPixmapItem>
29
#include <QGraphicsScene>
37
struct ImageViewer::Private
39
QGraphicsScene *scene;
42
QAction *zoomInAction;
43
QAction *zoomOutAction;
44
QAction *zoom100Action;
45
QAction *zoom2FitAction;
48
ImageViewer::ImageViewer(QWidget *parent) : QGraphicsView(parent), d(new Private)
50
//setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
51
//setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
52
setMouseTracking(true);
55
d->scene = new QGraphicsScene;
58
// create context menu
59
d->zoomInAction = new QAction(KIcon("zoom-in"), i18n("Zoom In"), this);
60
connect(d->zoomInAction, SIGNAL(triggered()), this, SLOT(zoomIn()));
62
d->zoomOutAction = new QAction(KIcon("zoom-out"), i18n("Zoom Out"), this);
63
connect(d->zoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
65
d->zoom100Action = new QAction(KIcon("zoom-fit-best"), i18n("Zoom to Actual size"), this);
66
connect(d->zoom100Action, SIGNAL(triggered()), this, SLOT(zoomActualSize()));
68
d->zoom2FitAction = new QAction(KIcon("document-preview"), i18n("Zoom to Fit"), this);
69
connect(d->zoom2FitAction, SIGNAL(triggered()), this, SLOT(zoom2Fit()));
71
addAction(d->zoomInAction);
72
addAction(d->zoomOutAction);
73
addAction(d->zoom100Action);
74
addAction(d->zoom2FitAction);
75
setContextMenuPolicy(Qt::ActionsContextMenu);
78
// ------------------------------------------------------------------------
79
ImageViewer::~ImageViewer()
84
// ------------------------------------------------------------------------
85
void ImageViewer::setQImage(QImage *img)
90
d->scene->setSceneRect(0, 0, img->width(), img->height());
93
// ------------------------------------------------------------------------
94
void ImageViewer::drawBackground(QPainter *painter, const QRectF &rect)
96
painter->fillRect(rect, QColor(0x70, 0x70, 0x70));
97
painter->drawImage(rect, *d->img, rect);
100
// ------------------------------------------------------------------------
101
void ImageViewer::zoomIn()
106
// ------------------------------------------------------------------------
107
void ImageViewer::zoomOut()
109
scale(1.0 / 1.5, 1.0 / 1.5);
113
// ------------------------------------------------------------------------
114
void ImageViewer::zoomActualSize()
116
setMatrix(QMatrix());
119
// ------------------------------------------------------------------------
120
void ImageViewer::zoom2Fit()
122
fitInView(d->img->rect(), Qt::KeepAspectRatio);
125
// ------------------------------------------------------------------------
126
void ImageViewer::wheelEvent(QWheelEvent *e)
128
if(e->modifiers() == Qt::ControlModifier) {
135
QGraphicsView::wheelEvent(e);