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

« back to all changes in this revision

Viewing changes to lib/scrolltool.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 noexpandtab:
2
 
/*
3
 
Gwenview: an image viewer
4
 
Copyright 2007 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, Boston, MA 02110-1301, USA.
19
 
 
20
 
*/
21
 
// Self
22
 
#include "scrolltool.moc"
23
 
 
24
 
// Qt
25
 
#include <QApplication>
26
 
#include <QMouseEvent>
27
 
#include <QScrollBar>
28
 
 
29
 
// KDE
30
 
#include <kdebug.h>
31
 
 
32
 
// Local
33
 
#include "imageview.h"
34
 
 
35
 
namespace Gwenview {
36
 
 
37
 
enum State { StateNone, StateZooming, StateDragging };
38
 
 
39
 
struct ScrollToolPrivate {
40
 
        int mScrollStartX;
41
 
        int mScrollStartY;
42
 
        State mState;
43
 
};
44
 
 
45
 
 
46
 
ScrollTool::ScrollTool(ImageView* view)
47
 
: AbstractImageViewTool(view)
48
 
, d(new ScrollToolPrivate) {
49
 
        d->mState = StateNone;
50
 
}
51
 
 
52
 
 
53
 
ScrollTool::~ScrollTool() {
54
 
        delete d;
55
 
}
56
 
 
57
 
 
58
 
void ScrollTool::mousePressEvent(QMouseEvent* event) {
59
 
        if (imageView()->zoomToFit()) {
60
 
                return;
61
 
        }
62
 
 
63
 
        if (event->button() != Qt::LeftButton) {
64
 
                return;
65
 
        }
66
 
 
67
 
        d->mScrollStartY = event->y();
68
 
        d->mScrollStartX = event->x();
69
 
        d->mState = StateDragging;
70
 
        imageView()->viewport()->setCursor(Qt::ClosedHandCursor);
71
 
}
72
 
 
73
 
 
74
 
void ScrollTool::mouseMoveEvent(QMouseEvent* event) {
75
 
        if (d->mState != StateDragging) {
76
 
                return;
77
 
        }
78
 
 
79
 
        QScrollBar* hScrollBar = imageView()->horizontalScrollBar();
80
 
        QScrollBar* vScrollBar = imageView()->verticalScrollBar();
81
 
        int width = imageView()->width();
82
 
        int height = imageView()->height();
83
 
 
84
 
        QPoint mousePos = event->pos();
85
 
 
86
 
        int deltaX = d->mScrollStartX - mousePos.x();
87
 
        int deltaY = d->mScrollStartY - mousePos.y();
88
 
 
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
94
 
                mousePos.setY(5);
95
 
        }
96
 
 
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
102
 
                mousePos.setX(5);
103
 
        }
104
 
 
105
 
        if (mousePos != event->pos()) {
106
 
                QCursor::setPos(imageView()->mapToGlobal(mousePos));
107
 
        }
108
 
 
109
 
        d->mScrollStartX = mousePos.x();
110
 
        d->mScrollStartY = mousePos.y();
111
 
        hScrollBar->setValue(hScrollBar->value() + deltaX);
112
 
        vScrollBar->setValue(vScrollBar->value() + deltaY);
113
 
}
114
 
 
115
 
 
116
 
void ScrollTool::mouseReleaseEvent(QMouseEvent* /*event*/) {
117
 
        if (d->mState != StateDragging) {
118
 
                return;
119
 
        }
120
 
 
121
 
        d->mState = StateNone;
122
 
        imageView()->viewport()->setCursor(Qt::OpenHandCursor);
123
 
}
124
 
 
125
 
 
126
 
void ScrollTool::wheelEvent(QWheelEvent* event) {
127
 
        // Forward events to the scrollbars, like QAbstractScrollArea::wheelEvent()
128
 
        // does.
129
 
        if (event->orientation() == Qt::Horizontal) {
130
 
                QApplication::sendEvent(imageView()->horizontalScrollBar(), event);
131
 
        } else {
132
 
                QApplication::sendEvent(imageView()->verticalScrollBar(), event);
133
 
        }
134
 
}
135
 
 
136
 
 
137
 
void ScrollTool::toolActivated() {
138
 
        imageView()->viewport()->setCursor(Qt::OpenHandCursor);
139
 
}
140
 
 
141
 
 
142
 
void ScrollTool::toolDeactivated() {
143
 
        imageView()->viewport()->setCursor(Qt::ArrowCursor);
144
 
}
145
 
 
146
 
 
147
 
} // namespace