1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
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>
31 #include <kstandarddirs.h>
34 #include "imageview.h"
38 enum State
{ StateNone
, StateZooming
, StateDragging
};
40 struct ScrollToolPrivate
{
41 ScrollTool::MouseWheelBehavior mMouseWheelBehavior
;
49 ScrollTool::ScrollTool(ImageView
* view
)
50 : AbstractImageViewTool(view
)
51 , d(new ScrollToolPrivate
) {
52 d
->mState
= StateNone
;
53 d
->mMouseWheelBehavior
= MouseWheelScroll
;
55 QString path
= KStandardDirs::locate("appdata", "cursors/zoom.png");
56 QPixmap cursorPixmap
= QPixmap(path
);
57 d
->mZoomCursor
= QCursor(cursorPixmap
);
61 ScrollTool::~ScrollTool() {
66 void ScrollTool::setMouseWheelBehavior(ScrollTool::MouseWheelBehavior behavior
) {
67 d
->mMouseWheelBehavior
= behavior
;
71 ScrollTool::MouseWheelBehavior
ScrollTool::mouseWheelBehavior() const {
72 return d
->mMouseWheelBehavior
;
76 void ScrollTool::mousePressEvent(QMouseEvent
* event
) {
77 if (event
->modifiers() == Qt::ControlModifier
) {
78 // Ctrl + Left or right button => zoom in or out
79 if (event
->button() == Qt::LeftButton
) {
80 emit
zoomInRequested(event
->pos());
81 } else if (event
->button() == Qt::RightButton
) {
82 emit
zoomOutRequested(event
->pos());
87 if (imageView()->zoomToFit()) {
91 if (event
->button() != Qt::LeftButton
) {
95 d
->mScrollStartY
= event
->y();
96 d
->mScrollStartX
= event
->x();
97 d
->mState
= StateDragging
;
98 imageView()->viewport()->setCursor(Qt::ClosedHandCursor
);
102 void ScrollTool::mouseMoveEvent(QMouseEvent
* event
) {
103 if (d
->mState
!= StateDragging
) {
109 deltaX
= d
->mScrollStartX
- event
->x();
110 deltaY
= d
->mScrollStartY
- event
->y();
112 d
->mScrollStartX
= event
->x();
113 d
->mScrollStartY
= event
->y();
114 imageView()->horizontalScrollBar()->setValue(
115 imageView()->horizontalScrollBar()->value() + deltaX
);
116 imageView()->verticalScrollBar()->setValue(
117 imageView()->verticalScrollBar()->value() + deltaY
);
121 void ScrollTool::mouseReleaseEvent(QMouseEvent
* /*event*/) {
122 if (d
->mState
!= StateDragging
) {
126 d
->mState
= StateNone
;
127 imageView()->viewport()->setCursor(Qt::OpenHandCursor
);
131 void ScrollTool::wheelEvent(QWheelEvent
* event
) {
132 if (event
->modifiers() & Qt::ControlModifier
) {
133 // Ctrl + wheel => zoom in or out
134 if (event
->delta() > 0) {
135 emit
zoomInRequested(event
->pos());
137 emit
zoomOutRequested(event
->pos());
142 if (d
->mMouseWheelBehavior
== MouseWheelScroll
) {
143 // Forward events to the scrollbars, like
144 // QAbstractScrollArea::wheelEvent() do.
145 if (event
->orientation() == Qt::Horizontal
) {
146 QApplication::sendEvent(imageView()->horizontalScrollBar(), event
);
148 QApplication::sendEvent(imageView()->verticalScrollBar(), event
);
152 if (event
->delta() > 0) {
153 emit
previousImageRequested();
155 emit
nextImageRequested();
161 void ScrollTool::keyPressEvent(QKeyEvent
* event
) {
162 if (event
->modifiers() == Qt::ControlModifier
&& d
->mState
== StateNone
) {
163 d
->mState
= StateZooming
;
164 imageView()->viewport()->setCursor(d
->mZoomCursor
);
169 void ScrollTool::keyReleaseEvent(QKeyEvent
* event
) {
170 if (!(event
->modifiers() & Qt::ControlModifier
) && d
->mState
== StateZooming
) {
171 d
->mState
= StateNone
;
172 imageView()->viewport()->setCursor(Qt::ArrowCursor
);
177 void ScrollTool::toolActivated() {
178 imageView()->viewport()->setCursor(Qt::OpenHandCursor
);
179 imageView()->viewport()->installEventFilter(this);
183 void ScrollTool::toolDeactivated() {
184 imageView()->viewport()->removeEventFilter(this);
185 imageView()->viewport()->setCursor(Qt::ArrowCursor
);
189 bool ScrollTool::eventFilter(QObject
*, QEvent
* event
) {
190 if (event
->type() == QEvent::ContextMenu
) {
191 // Filter out context menu if Ctrl is down to avoid showing it when
192 // zooming out with Ctrl + Right button
193 QContextMenuEvent
* contextMenuEvent
= static_cast<QContextMenuEvent
*>(event
);
194 if (contextMenuEvent
->modifiers() == Qt::ControlModifier
) {