File indexing completed on 2024-12-22 04:10:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_SAVED_COMMANDS_H
0008 #define __KIS_SAVED_COMMANDS_H
0009 
0010 #include <kundo2command.h>
0011 #include "kis_types.h"
0012 #include "kis_stroke_job_strategy.h"
0013 #include "KisCppQuirks.h"
0014 
0015 class KisStrokesFacade;
0016 
0017 
0018 class KRITAIMAGE_EXPORT KisSavedCommandBase : public KUndo2Command
0019 {
0020 public:
0021     KisSavedCommandBase(const KUndo2MagicString &name, KisStrokesFacade *strokesFacade);
0022     ~KisSavedCommandBase() override;
0023 
0024 
0025     void undo() override;
0026     void redo() override;
0027 
0028 protected:
0029     virtual void addCommands(KisStrokeId id, bool undo) = 0;
0030     KisStrokesFacade* strokesFacade();
0031 
0032 private:
0033     void runStroke(bool undo);
0034 
0035 private:
0036     KisStrokesFacade *m_strokesFacade;
0037     bool m_skipOneRedo;
0038 };
0039 
0040 class KRITAIMAGE_EXPORT KisSavedCommand : public KisSavedCommandBase
0041 {
0042 public:
0043     KisSavedCommand(KUndo2CommandSP command, KisStrokesFacade *strokesFacade);
0044     int timedId() const override;
0045     void setTimedID(int timedID) override;
0046 
0047     int id() const override;
0048     bool mergeWith(const KUndo2Command* command) override;
0049     bool canAnnihilateWith(const KUndo2Command *command) const override;
0050 
0051     bool timedMergeWith(KUndo2Command *other) override;
0052     QVector<KUndo2Command*> mergeCommandsVector() const override;
0053     void setTime(const QTime &time) override;
0054     using KisSavedCommandBase::setTime;
0055     QTime time() const override;
0056     void setEndTime(const QTime &time) override;
0057     using KisSavedCommandBase::setEndTime;
0058     QTime endTime() const override;
0059     bool isMerged() const override;
0060 
0061     /**
0062      * The function lazily unwraps a saved command `cmd` and passes the internal
0063      * command to the the function `func`. If passed `cmd` command is not a saved
0064      * command, then it is passed to the function directly.
0065      */
0066     template <typename Command, typename Func>
0067     static auto unwrap(Command *cmd, Func &&func)
0068         -> decltype(func(static_cast<Command*>(nullptr))) {
0069 
0070         using SavedCommand = std::copy_const_t<Command, KisSavedCommand>;
0071 
0072         SavedCommand *savedCommand =
0073             dynamic_cast<SavedCommand*>(cmd);
0074 
0075         return savedCommand ?
0076             std::forward<Func>(func)(savedCommand->m_command.data()) :
0077             std::forward<Func>(func)(cmd);
0078     }
0079 
0080 protected:
0081     void addCommands(KisStrokeId id, bool undo) override;
0082 
0083 private:
0084     KUndo2CommandSP m_command;
0085 };
0086 
0087 class KRITAIMAGE_EXPORT KisSavedMacroCommand : public KisSavedCommandBase
0088 {
0089 public:
0090     KisSavedMacroCommand(const KUndo2MagicString &name, KisStrokesFacade *strokesFacade);
0091     ~KisSavedMacroCommand() override;
0092 
0093     int id() const override;
0094     bool mergeWith(const KUndo2Command* command) override;
0095     bool canAnnihilateWith(const KUndo2Command *command) const override;
0096 
0097     void setMacroId(int value);
0098 
0099     void addCommand(KUndo2CommandSP command,
0100                     KisStrokeJobData::Sequentiality sequentiality = KisStrokeJobData::SEQUENTIAL,
0101                     KisStrokeJobData::Exclusivity exclusivity = KisStrokeJobData::NORMAL);
0102 
0103     void getCommandExecutionJobs(QVector<KisStrokeJobData*> *jobs, bool undo, bool shouldGoToHistory = true) const;
0104 
0105     void setOverrideInfo(const KisSavedMacroCommand *overriddenCommand, const QVector<const KUndo2Command *> &skipWhileOverride);
0106 protected:
0107     void addCommands(KisStrokeId id, bool undo) override;
0108 
0109 private:
0110     struct Private;
0111     Private * const m_d;
0112 };
0113 
0114 #endif /* __KIS_SAVED_COMMANDS_H */