File indexing completed on 2025-01-26 04:04:52

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KoMultiPathPointMergeCommand.h"
0008 #include <KoPathPointData.h>
0009 
0010 #include <KoPathCombineCommand.h>
0011 #include <KoPathPointMergeCommand.h>
0012 #include <KoSelection.h>
0013 
0014 #include "kis_assert.h"
0015 
0016 
0017 struct Q_DECL_HIDDEN KoMultiPathPointMergeCommand::Private
0018 {
0019     Private(const KoPathPointData &_pointData1, const KoPathPointData &_pointData2, KoShapeControllerBase *_controller, KoSelection *_selection)
0020         : pointData1(_pointData1),
0021           pointData2(_pointData2),
0022           controller(_controller),
0023           selection(_selection)
0024     {
0025     }
0026 
0027     KoPathPointData pointData1;
0028     KoPathPointData pointData2;
0029     KoShapeControllerBase *controller;
0030     KoSelection *selection;
0031 
0032 
0033     QScopedPointer<KoPathCombineCommand> combineCommand;
0034     QScopedPointer<KUndo2Command> mergeCommand;
0035 };
0036 
0037 KoMultiPathPointMergeCommand::KoMultiPathPointMergeCommand(const KoPathPointData &pointData1, const KoPathPointData &pointData2, KoShapeControllerBase *controller, KoSelection *selection, KUndo2Command *parent)
0038     : KUndo2Command(kundo2_i18n("Merge points"), parent),
0039       m_d(new Private(pointData1, pointData2, controller, selection))
0040 {
0041 }
0042 
0043 KoMultiPathPointMergeCommand::~KoMultiPathPointMergeCommand()
0044 {
0045 }
0046 
0047 KUndo2Command *KoMultiPathPointMergeCommand::createMergeCommand(const KoPathPointData &pointData1, const KoPathPointData &pointData2)
0048 {
0049     return new KoPathPointMergeCommand(pointData1, pointData2);
0050 }
0051 
0052 void KoMultiPathPointMergeCommand::redo()
0053 {
0054     KoShape *mergedShape = 0;
0055 
0056     if (m_d->pointData1.pathShape != m_d->pointData2.pathShape) {
0057         KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->controller);
0058 
0059         QList<KoPathShape*> shapes = {m_d->pointData1.pathShape, m_d->pointData2.pathShape};
0060         m_d->combineCommand.reset(new KoPathCombineCommand(m_d->controller, shapes));
0061         m_d->combineCommand->redo();
0062 
0063         KoPathPointData newPD1 = m_d->combineCommand->originalToCombined(m_d->pointData1);
0064         KoPathPointData newPD2 = m_d->combineCommand->originalToCombined(m_d->pointData2);
0065 
0066         m_d->mergeCommand.reset(createMergeCommand(newPD1, newPD2));
0067         m_d->mergeCommand->redo();
0068 
0069         mergedShape = m_d->combineCommand->combinedPath();
0070 
0071     } else {
0072         m_d->mergeCommand.reset(createMergeCommand(m_d->pointData1, m_d->pointData2));
0073         m_d->mergeCommand->redo();
0074 
0075         mergedShape = m_d->pointData1.pathShape;
0076     }
0077 
0078     if (m_d->selection) {
0079         m_d->selection->select(mergedShape);
0080     }
0081 
0082     KUndo2Command::redo();
0083 }
0084 
0085 KoPathShape *KoMultiPathPointMergeCommand::testingCombinedPath() const
0086 {
0087     return m_d->combineCommand ? m_d->combineCommand->combinedPath() : 0;
0088 }
0089 
0090 void KoMultiPathPointMergeCommand::undo()
0091 {
0092     KUndo2Command::undo();
0093 
0094     if (m_d->mergeCommand) {
0095             m_d->mergeCommand->undo();
0096             m_d->mergeCommand.reset();
0097     }
0098 
0099     if (m_d->combineCommand) {
0100         m_d->combineCommand->undo();
0101         m_d->combineCommand.reset();
0102     }
0103 
0104     if (m_d->selection) {
0105         m_d->selection->select(m_d->pointData1.pathShape);
0106         if (m_d->pointData1.pathShape != m_d->pointData2.pathShape) {
0107             m_d->selection->select(m_d->pointData2.pathShape);
0108         }
0109     }
0110 }
0111