1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
3
Gwenview: an image viewer
4
Copyright 2007 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, Boston, MA 02110-1301, USA.
22
#include "scrolltool.moc"
25
#include <QApplication>
26
#include <QMouseEvent>
33
#include "imageview.h"
37
enum State { StateNone, StateZooming, StateDragging };
39
struct ScrollToolPrivate {
46
ScrollTool::ScrollTool(ImageView* view)
47
: AbstractImageViewTool(view)
48
, d(new ScrollToolPrivate) {
49
d->mState = StateNone;
53
ScrollTool::~ScrollTool() {
58
void ScrollTool::mousePressEvent(QMouseEvent* event) {
59
if (imageView()->zoomToFit()) {
63
if (event->button() != Qt::LeftButton) {
67
d->mScrollStartY = event->y();
68
d->mScrollStartX = event->x();
69
d->mState = StateDragging;
70
imageView()->viewport()->setCursor(Qt::ClosedHandCursor);
74
void ScrollTool::mouseMoveEvent(QMouseEvent* event) {
75
if (d->mState != StateDragging) {
79
QScrollBar* hScrollBar = imageView()->horizontalScrollBar();
80
QScrollBar* vScrollBar = imageView()->verticalScrollBar();
81
int width = imageView()->width();
82
int height = imageView()->height();
84
QPoint mousePos = event->pos();
86
int deltaX = d->mScrollStartX - mousePos.x();
87
int deltaY = d->mScrollStartY - mousePos.y();
89
if (mousePos.y() <= 4 && vScrollBar->value() < vScrollBar->maximum() - 10) {
90
// wrap mouse from top to bottom
91
mousePos.setY(height - 5);
92
} else if (mousePos.y() >= height - 4 && vScrollBar->value() > 10) {
93
// wrap mouse from bottom to top
97
if (mousePos.x() <= 4 && hScrollBar->value() < hScrollBar->maximum() - 10) {
98
// wrap mouse from left to right
99
mousePos.setX(width - 5);
100
} else if (mousePos.x() >= width - 4 && hScrollBar->value() > 10) {
101
// wrap mouse from right to left
105
if (mousePos != event->pos()) {
106
QCursor::setPos(imageView()->mapToGlobal(mousePos));
109
d->mScrollStartX = mousePos.x();
110
d->mScrollStartY = mousePos.y();
111
hScrollBar->setValue(hScrollBar->value() + deltaX);
112
vScrollBar->setValue(vScrollBar->value() + deltaY);
116
void ScrollTool::mouseReleaseEvent(QMouseEvent* /*event*/) {
117
if (d->mState != StateDragging) {
121
d->mState = StateNone;
122
imageView()->viewport()->setCursor(Qt::OpenHandCursor);
126
void ScrollTool::wheelEvent(QWheelEvent* event) {
127
// Forward events to the scrollbars, like QAbstractScrollArea::wheelEvent()
129
if (event->orientation() == Qt::Horizontal) {
130
QApplication::sendEvent(imageView()->horizontalScrollBar(), event);
132
QApplication::sendEvent(imageView()->verticalScrollBar(), event);
137
void ScrollTool::toolActivated() {
138
imageView()->viewport()->setCursor(Qt::OpenHandCursor);
142
void ScrollTool::toolDeactivated() {
143
imageView()->viewport()->setCursor(Qt::ArrowCursor);