File indexing completed on 2024-05-12 16:36:07

0001 /* This file is part of the KDE project
0002    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "DragAndDropStrategy.h"
0021 
0022 #include "Cell.h"
0023 #include "calligra_sheets_limits.h"
0024 #include "Selection.h"
0025 #include "Sheet.h"
0026 #include "CellToolBase.h"
0027 
0028 #include <KoCanvasBase.h>
0029 #include <KoToolBase.h>
0030 #include <KoShape.h>
0031 #include <KoSelection.h>
0032 #include <KoShapeManager.h>
0033 
0034 #include <QBuffer>
0035 #include <QDomDocument>
0036 #include <QDrag>
0037 #include <QMimeData>
0038 #include <QTextStream>
0039 
0040 #include "commands/CopyCommand.h"
0041 
0042 using namespace Calligra::Sheets;
0043 
0044 class Q_DECL_HIDDEN DragAndDropStrategy::Private
0045 {
0046 public:
0047     Private() : started(false) { }
0048 
0049     Cell cell;
0050     QPointF lastPoint;
0051     bool started;
0052 };
0053 
0054 DragAndDropStrategy::DragAndDropStrategy(CellToolBase *cellTool,
0055         const QPointF &documentPos, Qt::KeyboardModifiers modifiers)
0056         : AbstractSelectionStrategy(cellTool, documentPos, modifiers)
0057         , d(new Private)
0058 {
0059     d->lastPoint = documentPos;
0060     Selection *const selection = this->selection();
0061     //const KoShape *shape = tool()->canvas()->shapeManager()->selection()->firstSelectedShape();
0062     const QPointF position = documentPos /*- (shape ? shape->position() : QPointF(0.0, 0.0))*/;
0063 
0064     // In which cell did the user click?
0065     qreal xpos;
0066     qreal ypos;
0067     int col = selection->activeSheet()->leftColumn(position.x(), xpos);
0068     int row = selection->activeSheet()->topRow(position.y(), ypos);
0069     // Check boundaries.
0070     if (col > KS_colMax || row > KS_rowMax) {
0071         debugSheetsUI << "col or row is out of range:" << "col:" << col << " row:" << row;
0072     } else {
0073         d->cell = Cell(selection->activeSheet(), col, row);
0074     }
0075 }
0076 
0077 DragAndDropStrategy::~DragAndDropStrategy()
0078 {
0079     delete d;
0080 }
0081 
0082 void DragAndDropStrategy::handleMouseMove(const QPointF& documentPos, Qt::KeyboardModifiers modifiers)
0083 {
0084     Q_UNUSED(modifiers);
0085     if (d->started)
0086         return;
0087     const QPointF position = documentPos - cellTool()->offset();
0088     d->lastPoint = position;
0089 
0090     // In which cell did the user click?
0091     qreal xpos;
0092     qreal ypos;
0093     int col = selection()->activeSheet()->leftColumn(position.x(), xpos);
0094     int row = selection()->activeSheet()->topRow(position.y(), ypos);
0095     // Check boundaries.
0096     if (col > KS_colMax || row > KS_rowMax) {
0097         debugSheetsUI << "col or row is out of range:" << "col:" << col << " row:" << row;
0098     } else if (d->cell == Cell(selection()->activeSheet(), col, row)) {
0099     } else {
0100         QDomDocument doc = CopyCommand::saveAsXml(*selection(), true);
0101 
0102         // Save to buffer
0103         QBuffer buffer;
0104         buffer.open(QIODevice::WriteOnly);
0105         QTextStream str(&buffer);
0106         str.setCodec("UTF-8");
0107         str << doc;
0108         buffer.close();
0109 
0110         QMimeData* mimeData = new QMimeData();
0111         mimeData->setText(CopyCommand::saveAsPlainText(*selection()));
0112         mimeData->setData("application/x-kspread-snippet", buffer.buffer());
0113 
0114         QDrag *drag = new QDrag(tool()->canvas()->canvasWidget());
0115         drag->setMimeData(mimeData);
0116         drag->exec();
0117         d->started = true;
0118     }
0119 }
0120 
0121 KUndo2Command* DragAndDropStrategy::createCommand()
0122 {
0123     //const KoShape *shape = tool()->canvas()->shapeManager()->selection()->firstSelectedShape();
0124     const QPointF position = d->lastPoint /*- (shape ? shape->position() : QPointF(0.0, 0.0))*/;
0125 
0126     // In which cell did the user click?
0127     qreal xpos;
0128     qreal ypos;
0129     int col = selection()->activeSheet()->leftColumn(position.x(), xpos);
0130     int row = selection()->activeSheet()->topRow(position.y(), ypos);
0131     // Check boundaries.
0132     if (col > KS_colMax || row > KS_rowMax) {
0133         debugSheetsUI << "col or row is out of range:" << "col:" << col << " row:" << row;
0134     } else if (d->cell == Cell(selection()->activeSheet(), col, row)) {
0135         selection()->initialize(QPoint(col, row), selection()->activeSheet());
0136     }
0137     return 0;
0138 }
0139 
0140 bool DragAndDropStrategy::dragStarted() const
0141 {
0142     return d->started;
0143 }