libs/flake

KoPanTool.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "KoPanTool.h"
00022 #include "KoToolBase_p.h"
00023 #include "KoPointerEvent.h"
00024 #include "KoCanvasBase.h"
00025 #include "KoCanvasController.h"
00026 #include "KoViewConverter.h"
00027 
00028 #include <QKeyEvent>
00029 #include <QScrollBar>
00030 #include <kdebug.h>
00031 
00032 KoPanTool::KoPanTool(KoCanvasBase *canvas)
00033         : KoToolBase(canvas),
00034         m_controller(0),
00035         m_temporary(false)
00036 {
00037 }
00038 
00039 bool KoPanTool::wantsAutoScroll() const
00040 {
00041     return false;
00042 }
00043 
00044 void KoPanTool::mousePressEvent(KoPointerEvent *event)
00045 {
00046     m_lastPosition = documentToViewport(event->point);
00047     event->accept();
00048     useCursor(QCursor(Qt::ClosedHandCursor));
00049 }
00050 
00051 void KoPanTool::mouseMoveEvent(KoPointerEvent *event)
00052 {
00053     Q_ASSERT(m_controller);
00054     if (event->buttons() == 0)
00055         return;
00056     event->accept();
00057 
00058     QPointF actualPosition = documentToViewport(event->point);
00059     QPointF distance(m_lastPosition - actualPosition);
00060     m_controller->pan(distance.toPoint());
00061 
00062     m_lastPosition = actualPosition;
00063 }
00064 
00065 void KoPanTool::mouseReleaseEvent(KoPointerEvent *event)
00066 {
00067     event->accept();
00068     useCursor(QCursor(Qt::OpenHandCursor));
00069     if (m_temporary)
00070         emit done();
00071 }
00072 
00073 void KoPanTool::keyPressEvent(QKeyEvent *event)
00074 {
00075     switch (event->key()) {
00076         case Qt::Key_Up:
00077             m_controller->pan(QPoint(0, -m_controller->verticalScrollBar()->singleStep()));
00078             break;
00079         case Qt::Key_Down:
00080             m_controller->pan(QPoint(0, m_controller->verticalScrollBar()->singleStep()));
00081             break;
00082         case Qt::Key_Left:
00083             m_controller->pan(QPoint(-m_controller->horizontalScrollBar()->singleStep(), 0));
00084             break;
00085         case Qt::Key_Right:
00086             m_controller->pan(QPoint(m_controller->horizontalScrollBar()->singleStep(), 0));
00087             break;
00088     }
00089     event->accept();
00090 }
00091 
00092 void KoPanTool::activate(ToolActivation toolActivation, const QSet<KoShape*> &)
00093 {
00094     if (m_controller == 0) {
00095         emit done();
00096         return;
00097     }
00098     m_temporary = toolActivation == TemporaryActivation;
00099     useCursor(QCursor(Qt::OpenHandCursor));
00100 }
00101 
00102 void KoPanTool::customMoveEvent(KoPointerEvent * event)
00103 {
00104     m_controller->pan(QPoint(-event->x(), -event->y()));
00105     event->accept();
00106 }
00107 
00108 QPointF KoPanTool::documentToViewport(const QPointF &p)
00109 {
00110     Q_D(KoToolBase);
00111     QPointF viewportPoint = d->canvas->viewConverter()->documentToView(p);
00112     viewportPoint += d->canvas->documentOrigin();
00113     viewportPoint += QPoint(m_controller->canvasOffsetX(), m_controller->canvasOffsetY());
00114 
00115     return viewportPoint;
00116 }