File indexing completed on 2024-12-15 04:01:01

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include "model/document.hpp"
0010 
0011 namespace glaxnimate::command {
0012 
0013 
0014 class UndoMacroGuard
0015 {
0016 public:
0017     UndoMacroGuard() noexcept : document(nullptr) {};
0018 
0019     UndoMacroGuard(const QString& name, model::Document* document, bool start_macro = true)
0020     : name(name), document(document)
0021     {
0022         if ( start_macro )
0023             start();
0024     }
0025 
0026     UndoMacroGuard(const UndoMacroGuard&) = delete;
0027     UndoMacroGuard& operator=(const UndoMacroGuard&) = delete;
0028 
0029     UndoMacroGuard(UndoMacroGuard&& other) noexcept
0030         : name(std::move(other.name)), document(other.document), end_macro(other.end_macro)
0031     {
0032         other.document = nullptr;
0033         other.end_macro = false;
0034     }
0035 
0036     UndoMacroGuard& operator=(UndoMacroGuard&& other) noexcept
0037     {
0038         std::swap(name, other.name);
0039         std::swap(document, other.document);
0040         std::swap(end_macro, other.end_macro);
0041         return *this;
0042     }
0043 
0044     ~UndoMacroGuard()
0045     {
0046         finish();
0047     }
0048 
0049     void start()
0050     {
0051         if ( !end_macro )
0052         {
0053             end_macro = true;
0054             document->undo_stack().beginMacro(name);
0055         }
0056     }
0057 
0058     void finish()
0059     {
0060         if ( end_macro )
0061         {
0062             end_macro = false;
0063             document->undo_stack().endMacro();
0064         }
0065     }
0066 
0067     bool started() const noexcept
0068     {
0069         return end_macro;
0070     }
0071 
0072 private:
0073     QString name;
0074     model::Document* document;
0075     bool end_macro = false;
0076 };
0077 
0078 
0079 } // namespace glaxnimate::command