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

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisSimpleModifyTransformMaskCommand.h"
0008 
0009 #include <kis_command_ids.h>
0010 #include <kis_transform_mask.h>
0011 
0012 
0013 KisSimpleModifyTransformMaskCommand::KisSimpleModifyTransformMaskCommand(KisTransformMaskSP mask,
0014                                                                          KisTransformMaskParamsInterfaceSP newParams,
0015                                                                          QWeakPointer<boost::none_t> updatesBlockerCookie,
0016                                                                          KUndo2Command *parent)
0017     : KUndo2Command(parent),
0018       m_mask(mask),
0019       m_oldParams(m_mask->transformParams()),
0020       m_newParams(newParams),
0021       m_updatesBlockerCookie(updatesBlockerCookie)
0022 {
0023 }
0024 
0025 int KisSimpleModifyTransformMaskCommand::id() const
0026 {
0027     return KisCommandUtils::ChangeTransformMaskCommand;
0028 }
0029 
0030 bool KisSimpleModifyTransformMaskCommand::mergeWith(const KUndo2Command *other)
0031 {
0032     const KisSimpleModifyTransformMaskCommand *otherCommandConst =
0033             dynamic_cast<const KisSimpleModifyTransformMaskCommand*>(other);
0034 
0035     bool retval = false;
0036 
0037     if (otherCommandConst &&
0038             otherCommandConst->m_mask == m_mask &&
0039             otherCommandConst->m_oldParams == m_newParams) {
0040 
0041         KisSimpleModifyTransformMaskCommand *otherCommand =
0042                 const_cast<KisSimpleModifyTransformMaskCommand*>(otherCommandConst);
0043 
0044         m_newParams = otherCommand->m_newParams;
0045         std::move(otherCommand->m_undoCommands.begin(), otherCommand->m_undoCommands.end(),
0046                   std::back_inserter(m_undoCommands));
0047 
0048         retval = true;
0049     }
0050 
0051     return retval;
0052 }
0053 
0054 void KisSimpleModifyTransformMaskCommand::redo()
0055 {
0056     if (!m_isInitialized) {
0057         std::unique_ptr<KUndo2Command> parent(new KUndo2Command);
0058         m_mask->setTransformParamsWithUndo(m_newParams, parent.get());
0059         m_undoCommands.emplace_back(parent.release());
0060         m_isInitialized = true;
0061     }
0062 
0063     KUndo2Command::redo();
0064     std::for_each(m_undoCommands.begin(), m_undoCommands.end(), std::mem_fn(&KUndo2Command::redo));
0065 
0066     if (!m_updatesBlockerCookie) {
0067         m_mask->threadSafeForceStaticImageUpdate();
0068     }
0069 }
0070 
0071 void KisSimpleModifyTransformMaskCommand::undo()
0072 {
0073     KIS_SAFE_ASSERT_RECOVER_NOOP(m_isInitialized);
0074 
0075     std::for_each(m_undoCommands.begin(), m_undoCommands.end(), std::mem_fn(&KUndo2Command::undo));
0076     KUndo2Command::undo();
0077 
0078     if (!m_updatesBlockerCookie) {
0079         m_mask->threadSafeForceStaticImageUpdate();
0080     }
0081 }