File indexing completed on 2025-02-02 04:14:48

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2006 Jan Hambrecht <jaham@gmx.net>
0003  * SPDX-FileCopyrightText: 2006, 2007 Thorsten Zachmann <zachmann@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "KoPathSegmentBreakCommand.h"
0009 #include "KoPathPoint.h"
0010 #include <klocalizedstring.h>
0011 #include <math.h>
0012 
0013 KoPathSegmentBreakCommand::KoPathSegmentBreakCommand(const KoPathPointData & pointData, KUndo2Command *parent)
0014         : KUndo2Command(parent)
0015         , m_pointData(pointData)
0016         , m_startIndex(-1, -1)
0017         , m_broken(false)
0018 {
0019     if (m_pointData.pathShape->isClosedSubpath(m_pointData.pointIndex.first)) {
0020         m_startIndex = m_pointData.pointIndex;
0021 
0022         const int numPoints = m_pointData.pathShape->subpathPointCount(m_startIndex.first);
0023         m_startIndex.second = (m_startIndex.second + 1) % numPoints;
0024     }
0025     setText(kundo2_i18n("Break subpath"));
0026 }
0027 
0028 KoPathSegmentBreakCommand::~KoPathSegmentBreakCommand()
0029 {
0030 }
0031 
0032 void KoPathSegmentBreakCommand::redo()
0033 {
0034     KUndo2Command::redo();
0035     // a repaint before is needed as the shape can shrink during the break
0036     m_pointData.pathShape->update();
0037     if (m_startIndex.first != -1) {
0038         m_startIndex = m_pointData.pathShape->openSubpath(m_startIndex);
0039         m_pointData.pathShape->normalize();
0040         m_pointData.pathShape->update();
0041     } else {
0042         m_broken = m_pointData.pathShape->breakAfter(m_pointData.pointIndex);
0043         if (m_broken) {
0044             m_pointData.pathShape->normalize();
0045             m_pointData.pathShape->update();
0046         }
0047     }
0048 }
0049 
0050 void KoPathSegmentBreakCommand::undo()
0051 {
0052     KUndo2Command::undo();
0053     if (m_startIndex.first != -1) {
0054         m_startIndex = m_pointData.pathShape->closeSubpath(m_startIndex);
0055         m_pointData.pathShape->normalize();
0056         m_pointData.pathShape->update();
0057     } else if (m_broken) {
0058         m_pointData.pathShape->join(m_pointData.pointIndex.first);
0059         m_pointData.pathShape->normalize();
0060         m_pointData.pathShape->update();
0061     }
0062 }