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_RECT_STORAGE_UNDO_COMMAND
0021 #define KSPREAD_RECT_STORAGE_UNDO_COMMAND
0022 
0023 // Qt
0024 #include <QList>
0025 #include <QPair>
0026 #include <kundo2command.h>
0027 
0028 // Sheets
0029 #include "ModelSupport.h"
0030 #include "SheetModel.h"
0031 
0032 namespace Calligra
0033 {
0034 namespace Sheets
0035 {
0036 
0037 /**
0038  * \ingroup Commands
0039  * \brief An undo command for RectStorage 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 RectStorageUndoCommand : public KUndo2Command
0048 {
0049 public:
0050     typedef QPair<QRectF, T> Pair;
0051 
0052     RectStorageUndoCommand(QAbstractItemModel *const model, int role, KUndo2Command *parent = 0);
0053 
0054     void undo() override;
0055 
0056     void add(const QList<Pair> &pairs);
0057 
0058     RectStorageUndoCommand& operator<<(const Pair &pair);
0059     RectStorageUndoCommand& operator<<(const QList<Pair> &pairs);
0060 
0061 private:
0062     QAbstractItemModel *const m_model;
0063     const int m_role;
0064     QList<Pair> m_undoData;
0065 };
0066 
0067 template<typename T>
0068 RectStorageUndoCommand<T>::RectStorageUndoCommand(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 RectStorageUndoCommand<T>::undo()
0078 {
0079     SheetModel *const model = static_cast<SheetModel*>(m_model);
0080     for (int i = 0; i < m_undoData.count(); ++i) {
0081         QVariant data;
0082         data.setValue(m_undoData[i].second);
0083         model->setData(fromRange(m_undoData[i].first.toRect(), model), data, m_role);
0084     }
0085     KUndo2Command::undo(); // undo possible child commands
0086 }
0087 
0088 template<typename T>
0089 void RectStorageUndoCommand<T>::add(const QList<Pair>& pairs)
0090 {
0091     m_undoData << pairs;
0092 }
0093 
0094 template<typename T>
0095 RectStorageUndoCommand<T>& RectStorageUndoCommand<T>::operator<<(const Pair& pair)
0096 {
0097     m_undoData << pair;
0098     return *this;
0099 }
0100 
0101 template<typename T>
0102 RectStorageUndoCommand<T>& RectStorageUndoCommand<T>::operator<<(const QList<Pair>& pairs)
0103 {
0104     m_undoData << pairs;
0105     return *this;
0106 }
0107 
0108 } // namespace Sheets
0109 } // namespace Calligra
0110 
0111 #endif // KSPREAD_RECT_STORAGE_UNDO_COMMAND