kspread

AbstractSelectionStrategy.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "AbstractSelectionStrategy.h"
00021 
00022 #include "Selection.h"
00023 #include "Sheet.h"
00024 
00025 #include <KoCanvasBase.h>
00026 #include <KoSelection.h>
00027 #include <KoShapeManager.h>
00028 #include <KoTool.h>
00029 
00030 using namespace KSpread;
00031 
00032 class AbstractSelectionStrategy::Private
00033 {
00034 public:
00035     Selection* selection;
00036     QPointF start;
00037 };
00038 
00039 AbstractSelectionStrategy::AbstractSelectionStrategy(KoTool* parent, KoCanvasBase* canvas, Selection* selection,
00040                                      const QPointF documentPos, Qt::KeyboardModifiers modifiers)
00041     : KoInteractionStrategy(parent, canvas)
00042     , d(new Private)
00043 {
00044     Q_UNUSED(modifiers)
00045     d->selection = selection;
00046     d->start = documentPos;
00047 }
00048 
00049 AbstractSelectionStrategy::~AbstractSelectionStrategy()
00050 {
00051     delete d;
00052 }
00053 
00054 void AbstractSelectionStrategy::handleMouseMove(const QPointF& documentPos, Qt::KeyboardModifiers modifiers)
00055 {
00056     Q_UNUSED(modifiers)
00057     const KoShape* shape = m_canvas->shapeManager()->selection()->firstSelectedShape();
00058     const QPointF position = documentPos - (shape ? shape->position() : QPointF(0.0, 0.0));
00059     // In which cell did the user click?
00060     double xpos;
00061     double ypos;
00062     int col = d->selection->activeSheet()->leftColumn(position.x(), xpos);
00063     int row = d->selection->activeSheet()->topRow(position.y(), ypos);
00064     // Check boundaries.
00065     if (col > KS_colMax || row > KS_rowMax)
00066     {
00067         kDebug(36005) << "col or row is out of range:" << "col:" << col << " row:" << row;
00068         return;
00069     }
00070     // Test whether mouse is over the Selection.handle
00071     const QRectF selectionHandle = d->selection->selectionHandleArea(m_canvas->viewConverter());
00072     if (selectionHandle.contains(position))
00073     {
00074         // If the cursor is over the handle, than it might be already on the next cell.
00075         // Recalculate the cell position!
00076         col = d->selection->activeSheet()->leftColumn(position.x() - m_canvas->viewConverter()->viewToDocumentX(2.0), xpos);
00077         row = d->selection->activeSheet()->topRow(position.y() - m_canvas->viewConverter()->viewToDocumentY(2.0), ypos);
00078     }
00079     // Update the selection.
00080     d->selection->update(QPoint(col, row));
00081     m_parent->repaintDecorations();
00082 }
00083 
00084 QUndoCommand* AbstractSelectionStrategy::createCommand()
00085 {
00086     return 0;
00087 }
00088 
00089 void AbstractSelectionStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
00090 {
00091     Q_UNUSED(modifiers)
00092     m_parent->repaintDecorations();
00093 }
00094 
00095 Selection* AbstractSelectionStrategy::selection() const
00096 {
00097     return d->selection;
00098 }
00099 
00100 const QPointF& AbstractSelectionStrategy::startPosition() const
00101 {
00102     return d->start;
00103 }