~ubuntu-branches/ubuntu/raring/skanlite/raring

« back to all changes in this revision

Viewing changes to src/ImageViewer.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2012-01-06 20:18:11 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20120106201811-46qnlezawwe1vspu
Tags: 0.8-1ubuntu1
* Merge from Debian experimental, remaining changes:
  - Do not suggest skanlite-dbg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
* Date        : 2008-08-26
 
3
* Description : Preview image viewer.
 
4
*
 
5
* Copyright (C) 2008 by Kare Sars <kare dot sars at iki dot fi>
 
6
*
 
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.
 
14
*
 
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.
 
19
*
 
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/>
 
22
*
 
23
* ============================================================ */
 
24
 
 
25
#include "ImageViewer.h"
 
26
#include "ImageViewer.moc"
 
27
 
 
28
#include <QGraphicsPixmapItem>
 
29
#include <QGraphicsScene>
 
30
#include <QScrollBar>
 
31
#include <QAction>
 
32
#include <KIcon>
 
33
#include <KLocale>
 
34
 
 
35
#include <KDebug>
 
36
 
 
37
struct ImageViewer::Private
 
38
{
 
39
    QGraphicsScene      *scene;
 
40
    QImage              *img;
 
41
 
 
42
    QAction *zoomInAction;
 
43
    QAction *zoomOutAction;
 
44
    QAction *zoom100Action;
 
45
    QAction *zoom2FitAction;
 
46
};
 
47
 
 
48
ImageViewer::ImageViewer(QWidget *parent) : QGraphicsView(parent), d(new Private)
 
49
{
 
50
    //setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
 
51
    //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
 
52
    setMouseTracking(true);
 
53
    
 
54
    // Init the scene
 
55
    d->scene = new QGraphicsScene;
 
56
    setScene(d->scene);
 
57
 
 
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()));
 
61
    
 
62
    d->zoomOutAction = new QAction(KIcon("zoom-out"), i18n("Zoom Out"), this);
 
63
    connect(d->zoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
 
64
    
 
65
    d->zoom100Action = new QAction(KIcon("zoom-fit-best"), i18n("Zoom to Actual size"), this);
 
66
    connect(d->zoom100Action, SIGNAL(triggered()), this, SLOT(zoomActualSize()));
 
67
    
 
68
    d->zoom2FitAction = new QAction(KIcon("document-preview"), i18n("Zoom to Fit"), this);
 
69
    connect(d->zoom2FitAction, SIGNAL(triggered()), this, SLOT(zoom2Fit()));
 
70
    
 
71
    addAction(d->zoomInAction);
 
72
    addAction(d->zoomOutAction);
 
73
    addAction(d->zoom100Action);
 
74
    addAction(d->zoom2FitAction);
 
75
    setContextMenuPolicy(Qt::ActionsContextMenu);
 
76
}
 
77
 
 
78
// ------------------------------------------------------------------------
 
79
ImageViewer::~ImageViewer()
 
80
{
 
81
    delete d;
 
82
}
 
83
 
 
84
// ------------------------------------------------------------------------
 
85
void ImageViewer::setQImage(QImage *img)
 
86
{
 
87
    if (img == 0) return;
 
88
 
 
89
    d->img = img;
 
90
    d->scene->setSceneRect(0, 0, img->width(), img->height());
 
91
}
 
92
 
 
93
// ------------------------------------------------------------------------
 
94
void ImageViewer::drawBackground(QPainter *painter, const QRectF &rect)
 
95
{
 
96
    painter->fillRect(rect, QColor(0x70, 0x70, 0x70));
 
97
    painter->drawImage(rect, *d->img, rect);
 
98
}
 
99
 
 
100
// ------------------------------------------------------------------------
 
101
void ImageViewer::zoomIn()
 
102
{
 
103
    scale(1.5, 1.5);
 
104
}
 
105
 
 
106
// ------------------------------------------------------------------------
 
107
void ImageViewer::zoomOut()
 
108
{
 
109
    scale(1.0 / 1.5, 1.0 / 1.5);
 
110
}
 
111
 
 
112
 
 
113
// ------------------------------------------------------------------------
 
114
void ImageViewer::zoomActualSize()
 
115
{
 
116
    setMatrix(QMatrix());
 
117
}
 
118
 
 
119
// ------------------------------------------------------------------------
 
120
void ImageViewer::zoom2Fit()
 
121
{
 
122
    fitInView(d->img->rect(), Qt::KeepAspectRatio);
 
123
}
 
124
 
 
125
// ------------------------------------------------------------------------
 
126
void ImageViewer::wheelEvent(QWheelEvent *e)
 
127
{
 
128
    if(e->modifiers() == Qt::ControlModifier) {
 
129
        if(e->delta() > 0) {
 
130
            zoomIn();
 
131
        } else {
 
132
            zoomOut();
 
133
        }
 
134
    } else {
 
135
        QGraphicsView::wheelEvent(e);
 
136
    }
 
137
}
 
138