File indexing completed on 2024-06-09 04:28:39

0001 /*
0002  * SPDX-FileCopyrightText: 2023 Alvin <alvin@alvinhc.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "SvgInlineSizeChangeCommand.h"
0008 #include "SvgInlineSizeHelper.h"
0009 
0010 #include <QRegularExpression>
0011 
0012 #include <klocalizedstring.h>
0013 
0014 #include "KoSvgText.h"
0015 #include "KoSvgTextProperties.h"
0016 #include "KoSvgTextShape.h"
0017 #include "KoSvgTextShapeMarkupConverter.h"
0018 
0019 #include "kis_assert.h"
0020 #include "kis_command_ids.h"
0021 
0022 SvgInlineSizeChangeCommand::SvgInlineSizeChangeCommand(KoSvgTextShape *shape, double inlineSize, KUndo2Command *parent)
0023     : SvgInlineSizeChangeCommand(shape, inlineSize, SvgInlineSizeHelper::getInlineSizePt(shape), 0, 0, QPointF(), QPointF(), parent)
0024 {
0025 }
0026 
0027 SvgInlineSizeChangeCommand::SvgInlineSizeChangeCommand(KoSvgTextShape *shape,
0028                                                        double inlineSize,
0029                                                        double oldInlineSize,
0030                                                        int anchor,
0031                                                        int oldAnchor,
0032                                                        QPointF movePos,
0033                                                        QPointF oldPos,
0034                                                        KUndo2Command *parent)
0035     : KUndo2Command(parent)
0036     , m_shape(shape)
0037     , m_inlineSize(inlineSize)
0038     , m_oldInlineSize(oldInlineSize)
0039     , m_anchor(anchor)
0040     , m_oldAnchor(oldAnchor)
0041     , m_originalPos(oldPos)
0042     , m_movePos(movePos)
0043 {
0044     setText(kundo2_i18n("Adjust text auto wrap"));
0045 }
0046 
0047 void SvgInlineSizeChangeCommand::applyInlineSize(double inlineSize, int anchor, QPointF pos, bool undo)
0048 {
0049     QRectF updateRect = m_shape->boundingRect();
0050 
0051     KoSvgTextProperties properties = m_shape->propertiesForPos(-1);
0052     KoSvgText::AutoValue inlineSizeProp = properties.propertyOrDefault(KoSvgTextProperties::InlineSizeId).value<KoSvgText::AutoValue>();
0053     inlineSizeProp.customValue = inlineSize;
0054     inlineSizeProp.isAuto = false;
0055     properties.setProperty(KoSvgTextProperties::InlineSizeId, KoSvgText::fromAutoValue(inlineSizeProp));
0056     properties.setProperty(KoSvgTextProperties::TextAnchorId, QVariant(anchor));
0057 
0058     if (undo) {
0059         m_shape->setPropertiesAtPos(-1, properties);
0060         m_shape->setAbsolutePosition(pos, KoFlake::TopLeft);
0061     } else {
0062         m_shape->setAbsolutePosition(pos, KoFlake::TopLeft);
0063         m_shape->setPropertiesAtPos(-1, properties);
0064     }
0065 
0066     m_shape->updateAbsolute(updateRect | m_shape->boundingRect());
0067 }
0068 
0069 void SvgInlineSizeChangeCommand::redo()
0070 {
0071     applyInlineSize(m_inlineSize, m_anchor, m_movePos, false);
0072 }
0073 
0074 void SvgInlineSizeChangeCommand::undo()
0075 {
0076     applyInlineSize(m_oldInlineSize, m_oldAnchor, m_originalPos, true);
0077 }
0078 
0079 int SvgInlineSizeChangeCommand::id() const
0080 {
0081     return KisCommandUtils::SvgInlineSizeChangeCommand;
0082 }
0083 
0084 bool SvgInlineSizeChangeCommand::mergeWith(const KUndo2Command *otherCommand)
0085 {
0086     const SvgInlineSizeChangeCommand *other = dynamic_cast<const SvgInlineSizeChangeCommand *>(otherCommand);
0087 
0088     if (!other || other->m_shape != m_shape) {
0089         return false;
0090     }
0091 
0092     m_inlineSize = other->m_inlineSize;
0093     m_anchor = other->m_anchor;
0094     m_movePos = other->m_movePos;
0095 
0096     return true;
0097 }