File indexing completed on 2024-05-19 04:26:30

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_PROCESSING_APPLICATOR_H
0008 #define __KIS_PROCESSING_APPLICATOR_H
0009 
0010 #include <future>
0011 
0012 #include <kundo2commandextradata.h>
0013 #include <kundo2magicstring.h>
0014 
0015 #include "KisImageSignals.h"
0016 #include "kis_stroke_job_strategy.h"
0017 #include "kis_types.h"
0018 
0019 #include "kritaimage_export.h"
0020 
0021 class KRITAIMAGE_EXPORT KisProcessingApplicator
0022 {
0023 public:
0024     enum ProcessingFlag {
0025         NONE = 0x0,
0026         RECURSIVE = 0x1,
0027         NO_UI_UPDATES = 0x2,
0028         SUPPORTS_WRAPAROUND_MODE = 0x4,
0029         NO_IMAGE_UPDATES = 0x8
0030     };
0031 
0032     Q_DECLARE_FLAGS(ProcessingFlags, ProcessingFlag)
0033 
0034 public:
0035     KisProcessingApplicator(KisImageWSP image,
0036                             KisNodeSP node,
0037                             ProcessingFlags flags = NONE,
0038                             KisImageSignalVector emitSignals = KisImageSignalVector(),
0039                             const KUndo2MagicString &name = KUndo2MagicString(),
0040                             KUndo2CommandExtraData *extraData = 0,
0041                             int macroId = -1);
0042     KisProcessingApplicator(KisImageWSP image,
0043                             KisNodeList nodes = KisNodeList(),
0044                             ProcessingFlags flags = NONE,
0045                             KisImageSignalVector emitSignals = KisImageSignalVector(),
0046                             const KUndo2MagicString &name = KUndo2MagicString(),
0047                             KUndo2CommandExtraData *extraData = 0,
0048                             int macroId = -1);
0049 
0050     ~KisProcessingApplicator();
0051 
0052     void applyVisitor(KisProcessingVisitorSP visitor,
0053                       KisStrokeJobData::Sequentiality sequentiality = KisStrokeJobData::SEQUENTIAL,
0054                       KisStrokeJobData::Exclusivity exclusivity = KisStrokeJobData::NORMAL);
0055 
0056     void applyCommand(KUndo2Command *command,
0057                       KisStrokeJobData::Sequentiality sequentiality = KisStrokeJobData::SEQUENTIAL,
0058                       KisStrokeJobData::Exclusivity exclusivity = KisStrokeJobData::NORMAL);
0059 
0060     void applyVisitorAllFrames(KisProcessingVisitorSP visitor,
0061                                KisStrokeJobData::Sequentiality sequentiality = KisStrokeJobData::SEQUENTIAL,
0062                                KisStrokeJobData::Exclusivity exclusivity = KisStrokeJobData::NORMAL);
0063 
0064     /**
0065      * This method emits all the final update signals of the stroke
0066      * without actually ending the stroke. This can be used for
0067      * long-running strokes which are kept open to implement preview
0068      * of the actions.
0069      *
0070      * WARNING: you cannot add new commands/processings after the
0071      * final signals has been emitted. You should either call end() or
0072      * cancel().
0073      */
0074     void explicitlyEmitFinalSignals();
0075 
0076     void end();
0077     void cancel();
0078 
0079     /**
0080      * Returns the underlying stroke.
0081      * Useful if you want to inject additional jobs in the applicator.
0082      */
0083     const KisStrokeId getStroke() const;
0084 
0085     /**
0086      * A future that notifies the caller when the whole processing
0087      * stroke has been completed. The returned value shows if the
0088      * stroke has been completed (true) or cancelled (false).
0089      */
0090     std::future<bool> &&successfullyCompletedFuture();
0091 
0092     /**
0093      * @brief runSingleCommandStroke creates a stroke and runs \p cmd in it.
0094      *        The text() field fo \p cmd is used as a title of the stroke.
0095      * @param image the image to run the stroke on
0096      * @param cmd the command to be executed
0097      * @param sequentiality sequentiality property of the command being executed (see strokes documentation)
0098      * @param exclusivity sequentiality property of the command being executed (see strokes documentation)
0099      */
0100     static void runSingleCommandStroke(KisImageSP image,
0101                                        KUndo2Command *cmd,
0102                                        KisStrokeJobData::Sequentiality sequentiality = KisStrokeJobData::SEQUENTIAL,
0103                                        KisStrokeJobData::Exclusivity exclusivity = KisStrokeJobData::NORMAL);
0104 
0105 private:
0106     void visitRecursively(KisNodeSP node,
0107                           KisProcessingVisitorSP visitor,
0108                           KisStrokeJobData::Sequentiality sequentiality,
0109                           KisStrokeJobData::Exclusivity exclusivity);
0110 
0111 private:
0112     KisImageWSP m_image;
0113     KisNodeList m_nodes;
0114     ProcessingFlags m_flags;
0115     KisImageSignalVector m_emitSignals;
0116     KisStrokeId m_strokeId;
0117     bool m_finalSignalsEmitted;
0118     QSharedPointer<bool> m_sharedAllFramesToken;
0119     std::future<bool> m_successfullyCompletedFuture;
0120 };
0121 
0122 Q_DECLARE_OPERATORS_FOR_FLAGS(KisProcessingApplicator::ProcessingFlags)
0123 
0124 
0125 #endif /* __KIS_PROCESSING_APPLICATOR_H */