File indexing completed on 2022-11-29 15:26:22

0001 /* This file is part of the KDE project
0002    Copyright (C) 2000, 2009 David Faure <faure@kde.org>
0003    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public License as
0007    published by the Free Software Foundation; either version 2 of
0008    the License, or (at your option) version 3.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013    GNU General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program.  If not, see <http://www.gnu.org/licenses/>
0017 */
0018 
0019 #include "commandhistory.h"
0020 #include "commands.h"
0021 
0022 #include <KActionCollection>
0023 #include <KBookmarkManager>
0024 
0025 #include <QAction>
0026 #include <QUndoCommand>
0027 #include <QUndoStack>
0028 
0029 class CommandHistory::Private
0030 {
0031 public:
0032     Private()
0033         : m_manager(nullptr)
0034         , m_undoStack()
0035     {
0036     }
0037     KBookmarkManager *m_manager;
0038     QUndoStack m_undoStack;
0039 };
0040 
0041 CommandHistory::CommandHistory(QObject *parent)
0042     : QObject(parent)
0043     , d(new CommandHistory::Private)
0044 {
0045 }
0046 
0047 CommandHistory::~CommandHistory()
0048 {
0049     delete d;
0050 }
0051 
0052 void CommandHistory::setBookmarkManager(KBookmarkManager *manager)
0053 {
0054     clearHistory(); // we can't keep old commands pointing to the wrong model/manager...
0055     d->m_manager = manager;
0056 }
0057 
0058 void CommandHistory::createActions(KActionCollection *actionCollection)
0059 {
0060     // TODO use QUndoView?
0061 
0062     QAction *standardAction = KStandardAction::create(KStandardAction::Undo, nullptr, nullptr, nullptr);
0063     QAction *undoAction = d->m_undoStack.createUndoAction(actionCollection);
0064     undoAction->setIcon(standardAction->icon());
0065     actionCollection->addAction(QLatin1String(KStandardAction::name(KStandardAction::Undo)), undoAction);
0066     actionCollection->setDefaultShortcuts(undoAction, standardAction->shortcuts());
0067     disconnect(undoAction, &QAction::triggered, &d->m_undoStack, nullptr);
0068     connect(undoAction, &QAction::triggered, this, &CommandHistory::undo);
0069     delete standardAction;
0070 
0071     standardAction = KStandardAction::create(KStandardAction::Redo, nullptr, nullptr, nullptr);
0072     QAction *redoAction = d->m_undoStack.createRedoAction(actionCollection);
0073     redoAction->setIcon(standardAction->icon());
0074     actionCollection->addAction(QLatin1String(KStandardAction::name(KStandardAction::Redo)), redoAction);
0075     actionCollection->setDefaultShortcuts(redoAction, standardAction->shortcuts());
0076     disconnect(redoAction, &QAction::triggered, &d->m_undoStack, nullptr);
0077     connect(redoAction, &QAction::triggered, this, &CommandHistory::redo);
0078     delete standardAction;
0079 }
0080 
0081 void CommandHistory::undo()
0082 {
0083     const int idx = d->m_undoStack.index();
0084     const QUndoCommand *cmd = d->m_undoStack.command(idx - 1);
0085     if (cmd) {
0086         d->m_undoStack.undo();
0087         commandExecuted(cmd);
0088     }
0089 }
0090 
0091 void CommandHistory::redo()
0092 {
0093     const int idx = d->m_undoStack.index();
0094     const QUndoCommand *cmd = d->m_undoStack.command(idx);
0095     if (cmd) {
0096         d->m_undoStack.redo();
0097         commandExecuted(cmd);
0098     }
0099 }
0100 
0101 void CommandHistory::commandExecuted(const QUndoCommand *k)
0102 {
0103     const IKEBCommand *cmd = dynamic_cast<const IKEBCommand *>(k);
0104     Q_ASSERT(cmd);
0105 
0106     KBookmark bk = d->m_manager->findByAddress(cmd->affectedBookmarks());
0107     Q_ASSERT(bk.isGroup());
0108 
0109     Q_EMIT notifyCommandExecuted(bk.toGroup());
0110 }
0111 
0112 void CommandHistory::notifyDocSaved()
0113 {
0114     d->m_undoStack.setClean();
0115 }
0116 
0117 void CommandHistory::addCommand(QUndoCommand *cmd)
0118 {
0119     if (!cmd)
0120         return;
0121     d->m_undoStack.push(cmd); // calls cmd->redo()
0122     CommandHistory::commandExecuted(cmd);
0123 }
0124 
0125 void CommandHistory::clearHistory()
0126 {
0127     if (d->m_undoStack.count() > 0) {
0128         d->m_undoStack.clear();
0129         Q_EMIT notifyCommandExecuted(d->m_manager->root()); // not really, but we still want to update the GUI
0130     }
0131 }
0132 
0133 KBookmarkManager *CommandHistory::bookmarkManager()
0134 {
0135     return d->m_manager;
0136 }