File indexing completed on 2024-05-12 16:35:12

0001 /* This file is part of the KDE project
0002    Copyright 2009 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 #ifndef KSPREAD_POINT_STORAGE_UNDO_COMMAND
0021 #define KSPREAD_POINT_STORAGE_UNDO_COMMAND
0022 
0023 // Qt
0024 #include <QAbstractItemModel>
0025 #include <QPair>
0026 #include <kundo2command.h>
0027 #include <QVector>
0028 
0029 // Sheets
0030 #include "Formula.h"
0031 
0032 namespace Calligra
0033 {
0034 namespace Sheets
0035 {
0036 
0037 /**
0038  * \ingroup Commands
0039  * \brief An undo command for PointStorage data.
0040  *
0041  * Implements undo functionality only. Glue it to another command,
0042  * that provides the appropriate applying (redoing).
0043  *
0044  * Used for recording undo data in CellStorage.
0045  */
0046 template<typename T>
0047 class PointStorageUndoCommand : public KUndo2Command
0048 {
0049 public:
0050     typedef QPair<QPoint, T> Pair;
0051 
0052     PointStorageUndoCommand(QAbstractItemModel *const model, int role, KUndo2Command *parent = 0);
0053 
0054     void undo() override;
0055 
0056     void add(const QVector<Pair> &pairs);
0057 
0058     PointStorageUndoCommand& operator<<(const Pair &pair);
0059     PointStorageUndoCommand& operator<<(const QVector<Pair> &pairs);
0060 
0061 private:
0062     QAbstractItemModel *const m_model;
0063     const int m_role;
0064     QVector<Pair> m_undoData;
0065 };
0066 
0067 template<typename T>
0068 PointStorageUndoCommand<T>::PointStorageUndoCommand(QAbstractItemModel *const model,
0069         int role, KUndo2Command *parent)
0070         : KUndo2Command(parent)
0071         , m_model(model)
0072         , m_role(role)
0073 {
0074 }
0075 
0076 template<typename T>
0077 void PointStorageUndoCommand<T>::undo()
0078 {
0079     // In reverse order for the case that a location was altered multiple times.
0080     for (int i = m_undoData.count() - 1; i >= 0; --i) {
0081         const int column = m_undoData[i].first.x();
0082         const int row = m_undoData[i].first.y();
0083         const QModelIndex index = m_model->index(row - 1, column - 1);
0084         QVariant data;
0085         data.setValue(m_undoData[i].second);
0086         m_model->setData(index, data, m_role);
0087     }
0088     KUndo2Command::undo(); // undo possible child commands
0089 }
0090 
0091 template<typename T>
0092 void PointStorageUndoCommand<T>::add(const QVector<Pair>& pairs)
0093 {
0094     m_undoData << pairs;
0095 }
0096 
0097 template<typename T>
0098 PointStorageUndoCommand<T>& PointStorageUndoCommand<T>::operator<<(const Pair& pair)
0099 {
0100     m_undoData << pair;
0101     return *this;
0102 }
0103 
0104 template<typename T>
0105 PointStorageUndoCommand<T>& PointStorageUndoCommand<T>::operator<<(const QVector<Pair>& pairs)
0106 {
0107     m_undoData << pairs;
0108     return *this;
0109 }
0110 
0111 } // namespace Sheets
0112 } // namespace Calligra
0113 
0114 #endif // KSPREAD_POINT_STORAGE_UNDO_COMMAND