1
/* ============================================================
3
* This file is a part of kipi-plugins project
4
* http://www.kipi-plugins.org
7
* Description : a widget to preview image effect.
9
* Copyright (C) 2009-2011 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
* This program is free software; you can redistribute it
12
* and/or modify it under the terms of the GNU General
13
* Public License as published by the Free Software Foundation;
14
* either version 2, or (at your option)
17
* This program is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
* GNU General Public License for more details.
22
* ============================================================ */
24
#include "previewimage.moc"
32
#include <QPaintEvent>
35
#include <QGraphicsPixmapItem>
36
#include <QGraphicsScene>
37
#include <QWheelEvent>
44
#include <kiconloader.h>
51
class PreviewImage::PreviewImagePriv
69
QGraphicsScene* scene;
70
QGraphicsPixmapItem* pixmapItem;
72
QAction* zoomInAction;
73
QAction* zoomOutAction;
74
QAction* zoom2FitAction;
79
PreviewImage::PreviewImage(QWidget* parent)
80
: QGraphicsView(parent), d(new PreviewImagePriv)
82
setAttribute(Qt::WA_DeleteOnClose);
83
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
84
setMouseTracking(true);
85
setCacheMode(QGraphicsView::CacheBackground);
87
d->scene = new QGraphicsScene;
88
d->pixmapItem = new QGraphicsPixmapItem;
90
d->scene->addItem(d->pixmapItem);
93
// create context menu
95
d->zoomInAction = new QAction(KIcon("zoom-in"), i18n("Zoom In"), this);
96
d->zoomInAction->setToolTip(i18n("Zoom In"));
97
d->zoomInAction->setShortcut(Qt::Key_Plus);
98
connect(d->zoomInAction, SIGNAL(triggered()),
99
this, SLOT(slotZoomIn()));
101
d->zoomOutAction = new QAction(KIcon("zoom-out"), i18n("Zoom Out"), this);
102
d->zoomOutAction->setToolTip(i18n("Zoom Out"));
103
d->zoomOutAction->setShortcut(Qt::Key_Minus);
104
connect(d->zoomOutAction, SIGNAL(triggered()),
105
this, SLOT(slotZoomOut()));
107
d->zoom2FitAction = new QAction(KIcon("zoom-fit-best"), i18n("Zoom to Fit"), this);
108
d->zoom2FitAction->setToolTip(i18n("Zoom to Fit"));
109
d->zoom2FitAction->setShortcut(Qt::Key_Asterisk);
110
connect(d->zoom2FitAction, SIGNAL(triggered()),
111
this, SLOT(slotZoom2Fit()));
113
addAction(d->zoomInAction);
114
addAction(d->zoomOutAction);
115
addAction(d->zoom2FitAction);
116
setContextMenuPolicy(Qt::ActionsContextMenu);
120
d->toolBar = new QToolBar(this);
121
d->toolBar->addAction(d->zoomInAction);
122
d->toolBar->addAction(d->zoomOutAction);
123
d->toolBar->addAction(d->zoom2FitAction);
125
d->toolBar->installEventFilter(this);
127
horizontalScrollBar()->installEventFilter(this);
128
verticalScrollBar()->installEventFilter(this);
131
PreviewImage::~PreviewImage()
136
bool PreviewImage::setImage(const QImage& img) const
140
d->pixmapItem->setPixmap(QPixmap::fromImage(img));
141
d->pixmapItem->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
142
d->scene->setSceneRect(0, 0, img.width(), img.height());
149
bool PreviewImage::load(const QString& file) const
152
return setImage(image);
155
void PreviewImage::slotZoomIn()
158
d->zoom2FitAction->setDisabled(false);
161
void PreviewImage::slotZoomOut()
163
scale(1.0 / 1.5, 1.0 / 1.5);
164
d->zoom2FitAction->setDisabled(false);
167
void PreviewImage::slotZoom2Fit()
169
fitInView(d->pixmapItem->boundingRect(), Qt::KeepAspectRatio);
170
d->zoom2FitAction->setDisabled(true);
173
void PreviewImage::wheelEvent(QWheelEvent *e)
175
if(e->modifiers() == Qt::ControlModifier)
188
QGraphicsView::wheelEvent(e);
192
void PreviewImage::mousePressEvent(QMouseEvent* e)
194
if (e->button() == Qt::LeftButton)
198
setCursor(Qt::ClosedHandCursor);
202
void PreviewImage::mouseReleaseEvent(QMouseEvent* e)
204
if (e->button() == Qt::LeftButton)
208
void PreviewImage::mouseMoveEvent(QMouseEvent* e)
210
if (e->buttons() & Qt::LeftButton)
212
int dx = e->x() - d->lastdx;
213
int dy = e->y() - d->lastdy;
214
verticalScrollBar()->setValue(verticalScrollBar()->value() - dy);
215
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - dx);
221
if (verticalScrollBar()->isVisible() || horizontalScrollBar()->isVisible())
222
setCursor(Qt::OpenHandCursor);
226
void PreviewImage::enterEvent(QEvent*)
231
void PreviewImage::leaveEvent(QEvent*)
236
bool PreviewImage::eventFilter(QObject *obj, QEvent *ev)
238
if ( obj == d->toolBar )
240
if ( ev->type() == QEvent::Enter)
241
setCursor(Qt::ArrowCursor);
242
else if ( ev->type() == QEvent::Leave)
247
else if ( obj == verticalScrollBar() && verticalScrollBar()->isVisible())
249
if ( ev->type() == QEvent::Enter)
250
setCursor(Qt::ArrowCursor);
251
else if ( ev->type() == QEvent::Leave)
256
else if ( obj == horizontalScrollBar() && horizontalScrollBar()->isVisible())
258
if ( ev->type() == QEvent::Enter)
259
setCursor(Qt::ArrowCursor);
260
else if ( ev->type() == QEvent::Leave)
266
return QGraphicsView::eventFilter(obj, ev);
269
} // namespace KIPIPlugins