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

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_STYLE_STORAGE_UNDO_COMMAND
0021 #define KSPREAD_STYLE_STORAGE_UNDO_COMMAND
0022 
0023 // Qt
0024 #include <QList>
0025 #include <QPair>
0026 #include <kundo2command.h>
0027 
0028 // Sheets
0029 #include "StyleStorage.h"
0030 
0031 namespace Calligra
0032 {
0033 namespace Sheets
0034 {
0035 
0036 /**
0037  * \ingroup Commands
0038  * \brief An undo command for StyleStorage data.
0039  *
0040  * Implements undo functionality only. Glue it to another command,
0041  * that provides the appropriate applying (redoing).
0042  *
0043  * Used for recording undo data in CellStorage.
0044  */
0045 class StyleStorageUndoCommand : public KUndo2Command
0046 {
0047 public:
0048     typedef QPair<QRectF, SharedSubStyle> Pair;
0049 
0050     explicit StyleStorageUndoCommand(StyleStorage *storage, KUndo2Command *parent = 0);
0051 
0052     void undo() override;
0053 
0054     void add(const QList<Pair> &pairs);
0055 
0056     StyleStorageUndoCommand& operator<<(const Pair &pair);
0057     StyleStorageUndoCommand& operator<<(const QList<Pair> &pairs);
0058 
0059 private:
0060     StyleStorage *const m_storage;
0061     QList<Pair> m_undoData;
0062 };
0063 
0064 StyleStorageUndoCommand::StyleStorageUndoCommand(StyleStorage *storage, KUndo2Command *parent)
0065         : KUndo2Command(parent)
0066         , m_storage(storage)
0067 {
0068 }
0069 
0070 void StyleStorageUndoCommand::undo()
0071 {
0072     for (int i = 0; i < m_undoData.count(); ++i) {
0073         m_storage->insert(m_undoData[i].first.toRect(), m_undoData[i].second);
0074     }
0075     KUndo2Command::undo(); // undo possible child commands
0076 }
0077 
0078 void StyleStorageUndoCommand::add(const QList<Pair>& pairs)
0079 {
0080     m_undoData << pairs;
0081 }
0082 
0083 StyleStorageUndoCommand& StyleStorageUndoCommand::operator<<(const Pair& pair)
0084 {
0085     m_undoData << pair;
0086     return *this;
0087 }
0088 
0089 StyleStorageUndoCommand& StyleStorageUndoCommand::operator<<(const QList<Pair>& pairs)
0090 {
0091     m_undoData << pairs;
0092     return *this;
0093 }
0094 
0095 } // namespace Sheets
0096 } // namespace Calligra
0097 
0098 #endif // KSPREAD_STYLE_STORAGE_UNDO_COMMAND