1
// vim: set tabstop=4 shiftwidth=4 expandtab:
3
Gwenview: an image viewer
4
Copyright 2011 Aurélien Gâteau <agateau@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22
#include "birdeyeview.moc"
25
#include <lib/document/document.h>
26
#include <lib/documentview/documentview.h>
33
#include <QGraphicsSceneEvent>
39
static qreal MAX_SIZE = 96;
40
static qreal VIEW_OFFSET = MAX_SIZE / 4;
41
static qreal Y_POSITION_PERCENT = 1 / 3.;
43
struct BirdEyeViewPrivate {
45
DocumentView* mDocView;
50
BirdEyeView::BirdEyeView(DocumentView* docView)
51
: QGraphicsWidget(docView)
52
, d(new BirdEyeViewPrivate)
54
setFlag(ItemIsSelectable);
56
d->mDocView = docView;
59
connect(docView->document().data(), SIGNAL(metaInfoUpdated()), SLOT(adjustGeometry()));
60
connect(docView, SIGNAL(zoomChanged(qreal)), SLOT(adjustGeometry()));
61
connect(docView, SIGNAL(zoomToFitChanged(bool)), SLOT(adjustGeometry()));
62
connect(docView, SIGNAL(positionChanged()), SLOT(adjustVisibleRect()));
65
BirdEyeView::~BirdEyeView()
70
void BirdEyeView::adjustGeometry()
72
if (d->mDocView->zoomToFit()) {
77
QSize size = d->mDocView->document()->size();
78
size.scale(MAX_SIZE, MAX_SIZE, Qt::KeepAspectRatio);
79
QRectF rect = d->mDocView->boundingRect();
82
rect.right() - VIEW_OFFSET - size.width(),
83
qMax(rect.top() + rect.height() * Y_POSITION_PERCENT - size.height(), 0.),
88
setVisible(d->mVisibleRect != boundingRect());
91
void BirdEyeView::adjustVisibleRect()
93
QSizeF docSize = d->mDocView->document()->size();
94
qreal viewZoom = d->mDocView->zoom();
95
qreal bevZoom = size().width() / docSize.width();
96
if (qFuzzyIsNull(viewZoom) || qFuzzyIsNull(bevZoom)) {
97
// Prevent divide-by-zero crashes
101
d->mVisibleRect = QRectF(
102
QPointF(d->mDocView->position()) / viewZoom * bevZoom,
103
(d->mDocView->size() / viewZoom).boundedTo(docSize) * bevZoom);
107
inline void drawTransparentRect(QPainter* painter, const QRectF& rect, const QColor& color)
114
painter->setBrush(bg);
115
painter->drawRect(rect.adjusted(0, 0, -1, -1));
118
void BirdEyeView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
120
drawTransparentRect(painter, boundingRect(), Qt::black);
121
drawTransparentRect(painter, d->mVisibleRect, Qt::white);
124
void BirdEyeView::mousePressEvent(QGraphicsSceneMouseEvent* event)
126
if (d->mVisibleRect.contains(event->pos())) {
127
setCursor(Qt::ClosedHandCursor);
128
d->mLastDragPos = event->pos();
132
void BirdEyeView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
134
QGraphicsItem::mouseMoveEvent(event);
135
if (d->mLastDragPos.isNull()) {
138
qreal ratio = d->mDocView->boundingRect().width() / d->mVisibleRect.width();
140
QPointF mousePos = event->pos();
141
QPointF viewPos = d->mDocView->position() + (mousePos - d->mLastDragPos) * ratio;
143
d->mLastDragPos = mousePos;
144
d->mDocView->setPosition(viewPos.toPoint());
147
void BirdEyeView::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
149
QGraphicsItem::mouseReleaseEvent(event);
150
if (d->mLastDragPos.isNull()) {
153
setCursor(Qt::OpenHandCursor);
154
d->mLastDragPos = QPointF();