~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/documentview/birdeyeview.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2011 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "birdeyeview.moc"
 
23
 
 
24
// Local
 
25
#include <lib/document/document.h>
 
26
#include <lib/documentview/documentview.h>
 
27
 
 
28
// KDE
 
29
#include <kdebug.h>
 
30
 
 
31
// Qt
 
32
#include <QCursor>
 
33
#include <QGraphicsSceneEvent>
 
34
#include <QPainter>
 
35
 
 
36
namespace Gwenview
 
37
{
 
38
 
 
39
static qreal MAX_SIZE = 96;
 
40
static qreal VIEW_OFFSET = MAX_SIZE / 4;
 
41
static qreal Y_POSITION_PERCENT = 1 / 3.;
 
42
 
 
43
struct BirdEyeViewPrivate {
 
44
    BirdEyeView* q;
 
45
    DocumentView* mDocView;
 
46
    QRectF mVisibleRect;
 
47
    QPointF mLastDragPos;
 
48
};
 
49
 
 
50
BirdEyeView::BirdEyeView(DocumentView* docView)
 
51
: QGraphicsWidget(docView)
 
52
, d(new BirdEyeViewPrivate)
 
53
{
 
54
    setFlag(ItemIsSelectable);
 
55
    d->q = this;
 
56
    d->mDocView = docView;
 
57
    adjustGeometry();
 
58
 
 
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()));
 
63
}
 
64
 
 
65
BirdEyeView::~BirdEyeView()
 
66
{
 
67
    delete d;
 
68
}
 
69
 
 
70
void BirdEyeView::adjustGeometry()
 
71
{
 
72
    if (d->mDocView->zoomToFit()) {
 
73
        hide();
 
74
        return;
 
75
    }
 
76
    show();
 
77
    QSize size = d->mDocView->document()->size();
 
78
    size.scale(MAX_SIZE, MAX_SIZE, Qt::KeepAspectRatio);
 
79
    QRectF rect = d->mDocView->boundingRect();
 
80
    setGeometry(
 
81
        QRectF(
 
82
            rect.right() - VIEW_OFFSET - size.width(),
 
83
            qMax(rect.top() + rect.height() * Y_POSITION_PERCENT - size.height(), 0.),
 
84
            size.width(),
 
85
            size.height()
 
86
        ));
 
87
    adjustVisibleRect();
 
88
    setVisible(d->mVisibleRect != boundingRect());
 
89
}
 
90
 
 
91
void BirdEyeView::adjustVisibleRect()
 
92
{
 
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
 
98
        return;
 
99
    }
 
100
 
 
101
    d->mVisibleRect = QRectF(
 
102
                          QPointF(d->mDocView->position()) / viewZoom * bevZoom,
 
103
                          (d->mDocView->size() / viewZoom).boundedTo(docSize) * bevZoom);
 
104
    update();
 
105
}
 
106
 
 
107
inline void drawTransparentRect(QPainter* painter, const QRectF& rect, const QColor& color)
 
108
{
 
109
    QColor bg = color;
 
110
    bg.setAlphaF(.33);
 
111
    QColor fg = color;
 
112
    fg.setAlphaF(.66);
 
113
    painter->setPen(fg);
 
114
    painter->setBrush(bg);
 
115
    painter->drawRect(rect.adjusted(0, 0, -1, -1));
 
116
}
 
117
 
 
118
void BirdEyeView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
 
119
{
 
120
    drawTransparentRect(painter, boundingRect(), Qt::black);
 
121
    drawTransparentRect(painter, d->mVisibleRect, Qt::white);
 
122
}
 
123
 
 
124
void BirdEyeView::mousePressEvent(QGraphicsSceneMouseEvent* event)
 
125
{
 
126
    if (d->mVisibleRect.contains(event->pos())) {
 
127
        setCursor(Qt::ClosedHandCursor);
 
128
        d->mLastDragPos = event->pos();
 
129
    }
 
130
}
 
131
 
 
132
void BirdEyeView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
 
133
{
 
134
    QGraphicsItem::mouseMoveEvent(event);
 
135
    if (d->mLastDragPos.isNull()) {
 
136
        return;
 
137
    }
 
138
    qreal ratio = d->mDocView->boundingRect().width() / d->mVisibleRect.width();
 
139
 
 
140
    QPointF mousePos = event->pos();
 
141
    QPointF viewPos = d->mDocView->position() + (mousePos - d->mLastDragPos) * ratio;
 
142
 
 
143
    d->mLastDragPos = mousePos;
 
144
    d->mDocView->setPosition(viewPos.toPoint());
 
145
}
 
146
 
 
147
void BirdEyeView::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
 
148
{
 
149
    QGraphicsItem::mouseReleaseEvent(event);
 
150
    if (d->mLastDragPos.isNull()) {
 
151
        return;
 
152
    }
 
153
    setCursor(Qt::OpenHandCursor);
 
154
    d->mLastDragPos = QPointF();
 
155
}
 
156
 
 
157
} // namespace