File indexing completed on 2024-12-08 04:27:22

0001 /*
0002     SPDX-FileCopyrightText: 2017 Nicolas Carion
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "undohelper.hpp"
0007 #ifdef CRASH_AUTO_TEST
0008 #include "logger.hpp"
0009 #endif
0010 #include <QDebug>
0011 #include <QTime>
0012 #include <utility>
0013 FunctionalUndoCommand::FunctionalUndoCommand(Fun undo, Fun redo, const QString &text, QUndoCommand *parent)
0014     : QUndoCommand(parent)
0015     , m_undo(std::move(undo))
0016     , m_redo(std::move(redo))
0017     , m_undone(false)
0018 {
0019     setText(QString("%1 %2").arg(QTime::currentTime().toString("hh:mm")).arg(text));
0020 }
0021 
0022 void FunctionalUndoCommand::undo()
0023 {
0024     // qDebug() << "UNDOING " <<text();
0025 #ifdef CRASH_AUTO_TEST
0026     Logger::log_undo(true);
0027 #endif
0028     m_undone = true;
0029     bool res = m_undo();
0030     Q_ASSERT(res);
0031 }
0032 
0033 void FunctionalUndoCommand::redo()
0034 {
0035     if (m_undone) {
0036         // qDebug() << "REDOING " <<text();
0037 #ifdef CRASH_AUTO_TEST
0038         Logger::log_undo(false);
0039 #endif
0040         bool res = m_redo();
0041         Q_ASSERT(res);
0042     }
0043 }