Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Implement reorder and toggle functions for text shapes-inside.
Wolthera van Hövell tot Westerflier30fa7999130c
diff --git a/krita/data/actions/InteractionTool.action b/krita/data/actions/InteractionTool.actionindex 078a9d5f3c..52e0801a76 100644--- a/krita/data/actions/InteractionTool.action+++ b/krita/data/actions/InteractionTool.action@@ -357,5 +357,53 @@<statusTip></statusTip><isCheckable>false</isCheckable></Action>-</Actions>++<Action name="flow_shape_type_toggle">+ <iconText>Toggle Flow Shape Type</iconText>+ <toolTip>Switch the current Flow Shape between Inside or Subtract mode</toolTip>+ <icon></icon>+ <whatsThis></whatsThis>+ <statusTip></statusTip>+ <isCheckable>false</isCheckable>+ <text>Toggle Flow Shape Type</text>+</Action>++<Action name="flow_shape_order_back">+ <iconText>Set Flow Shape as Last</iconText>+ <toolTip>Make this flow shape the last in the flow shape order as used by text layout.</toolTip>+ <icon>object-order-back-calligra</icon>+ <whatsThis></whatsThis>+ <statusTip></statusTip>+ <isCheckable>false</isCheckable>+ <text>Set Flow Shape as Last</text>+</Action>+<Action name="flow_shape_order_earlier">+ <iconText>Decrease Flow Shape Index</iconText>+ <toolTip>Make this flow shape be evaluated later.</toolTip>+ <icon>object-order-raise-calligra</icon>+ <whatsThis></whatsThis>+ <statusTip></statusTip>+ <isCheckable>false</isCheckable>+ <text>Decrease Flow Shape Index</text>+</Action>+<Action name="flow_shape_order_later">+ <iconText>Increase Flow Shape Index</iconText>+ <toolTip>Make this flow shape be evaluated later.</toolTip>+ <icon>object-order-lower-calligra</icon>+ <whatsThis></whatsThis>+ <statusTip></statusTip>+ <isCheckable>false</isCheckable>+ <text>Increase Flow Shape Index</text>+</Action>+<Action name="flow_shape_order_front">+ <iconText>Set Flow Shape as First</iconText>+ <toolTip>Make this flow shape the first in the flow shape order as used by text layout.</toolTip>+ <icon>object-order-front-calligra</icon>+ <whatsThis></whatsThis>+ <statusTip></statusTip>+ <isCheckable>false</isCheckable>+ <text>Set Flow Shape as First</text>+</Action>++ </Actions></ActionCollection>diff --git a/libs/flake/CMakeLists.txt b/libs/flake/CMakeLists.txtindex 92b65febab..62ce790ec0 100644--- a/libs/flake/CMakeLists.txt+++ b/libs/flake/CMakeLists.txt@@ -174,6 +174,8 @@ set(kritaflake_SRCScommands/KoSvgConvertTextTypeCommand.cppcommands/KoSvgChangeTextContoursCommand.cppcommands/KoSvgTextAddRemoveShapeCommands.cpp+ commands/KoSvgTextReorderShapeInsideCommand.cpp+ commands/KoSvgTextFlipShapeContourTypeCommand.cpphtml/HtmlSavingContext.cpphtml/HtmlWriter.cppdiff --git a/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.cpp b/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.cppnew file mode 100644index 0000000000..cfe5760aaa--- /dev/null+++ b/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.cpp@@ -0,0 +1,49 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "KoSvgTextFlipShapeContourTypeCommand.h"++#include <KoSvgTextShape.h>++struct KoSvgTextFlipShapeContourTypeCommand::Private {+ Private(KoSvgTextShape *_text, KoShape *_shape)+ : textShape(_text)+ , shape(_shape)+ , isInside(textShape->shapesInside().contains(_shape))+ {+ }++ KoSvgTextShape *textShape;+ KoShape *shape;+ bool isInside = false;+};++KoSvgTextFlipShapeContourTypeCommand::KoSvgTextFlipShapeContourTypeCommand(KoSvgTextShape *textShape, KoShape *shape, KUndo2Command *parent)+ : KUndo2Command(parent)+ , d(new Private(textShape, shape))+{++}++KoSvgTextFlipShapeContourTypeCommand::~KoSvgTextFlipShapeContourTypeCommand()+{++}++void KoSvgTextFlipShapeContourTypeCommand::redo()+{+ QRectF updateRect = d->textShape->boundingRect();+ d->textShape->addShapeContours({d->shape}, !(d->isInside));+ updateRect |= d->textShape->boundingRect();+ d->textShape->updateAbsolute(updateRect);+}++void KoSvgTextFlipShapeContourTypeCommand::undo()+{+ QRectF updateRect = d->textShape->boundingRect();+ d->textShape->addShapeContours({d->shape}, d->isInside);+ updateRect |= d->textShape->boundingRect();+ d->textShape->updateAbsolute(updateRect);+}diff --git a/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.h b/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.hnew file mode 100644index 0000000000..7eed475ba4--- /dev/null+++ b/libs/flake/commands/KoSvgTextFlipShapeContourTypeCommand.h@@ -0,0 +1,28 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KOSVGTEXTFLIPSHAPETYPECOMMAND_H+#define KOSVGTEXTFLIPSHAPETYPECOMMAND_H++#include <kundo2command.h>+#include <kritaflake_export.h>++class KoSvgTextShape;+class KoShape;++class KRITAFLAKE_EXPORT KoSvgTextFlipShapeContourTypeCommand : public KUndo2Command+{+public:+ KoSvgTextFlipShapeContourTypeCommand(KoSvgTextShape* textShape, KoShape *shape, KUndo2Command *parent = nullptr);+ ~KoSvgTextFlipShapeContourTypeCommand();++ void redo() override;+ void undo() override;+private:+ struct Private;+ QScopedPointer<Private> d;+};++#endif // KOSVGTEXTFLIPSHAPETYPECOMMAND_Hdiff --git a/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.cpp b/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.cppnew file mode 100644index 0000000000..54d84b4bca--- /dev/null+++ b/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.cpp@@ -0,0 +1,65 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include "KoSvgTextReorderShapeInsideCommand.h"+#include <KoSvgTextShape.h>++struct KoSvgTextReorderShapeInsideCommand::Private {+ Private(KoSvgTextShape *_text, KoShape *_shape, KoSvgTextReorderShapeInsideCommand::MoveShapeType _type)+ : textShape(_text)+ , memento(textShape->getMemento())+ , shape(_shape)+ , type(_type)+ {+ }++ KoSvgTextShape *textShape;+ KoSvgTextShapeMementoSP memento;+ KoShape *shape;+ int oldIndex = 0;+ KoSvgTextReorderShapeInsideCommand::MoveShapeType type;+};++KoSvgTextReorderShapeInsideCommand::KoSvgTextReorderShapeInsideCommand(KoSvgTextShape *textShape, KoShape *shape, MoveShapeType type, KUndo2Command *parent)+ : KUndo2Command(parent)+ , d(new Private(textShape, shape, type))+{++}++KoSvgTextReorderShapeInsideCommand::~KoSvgTextReorderShapeInsideCommand()+{++}++void KoSvgTextReorderShapeInsideCommand::redo()+{+ QRectF updateRect = d->textShape->boundingRect();++ d->oldIndex = d->textShape->shapesInside().indexOf(d->shape);+ int newIndex = d->oldIndex;+ const int max = d->textShape->shapesInside().size();+ if (d->type == MoveEarlier) {+ newIndex = qMax(0, d->oldIndex - 1);+ } else if (d->type == MoveLater) {+ newIndex = qMin(max, d->oldIndex + 1);+ } else if (d->type == BringToFront) {+ newIndex = 0;+ } else /*if (type == SendToBack)*/ {+ newIndex = max;+ }+ d->textShape->moveShapeInsideToIndex(d->shape, newIndex);+ updateRect |= d->textShape->boundingRect();+ d->textShape->updateAbsolute(updateRect);+}++void KoSvgTextReorderShapeInsideCommand::undo()+{+ QRectF updateRect = d->textShape->boundingRect();+ d->textShape->moveShapeInsideToIndex(d->shape, d->oldIndex);+ d->textShape->setMemento(d->memento);+ updateRect |= d->textShape->boundingRect();+ d->textShape->updateAbsolute(updateRect);+}diff --git a/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.h b/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.hnew file mode 100644index 0000000000..5dd5ac414c--- /dev/null+++ b/libs/flake/commands/KoSvgTextReorderShapeInsideCommand.h@@ -0,0 +1,41 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KOSVGTEXTREORDERSHAPEINSIDECOMMAND_H+#define KOSVGTEXTREORDERSHAPEINSIDECOMMAND_H++#include <kundo2command.h>+#include <kritaflake_export.h>++class KoSvgTextShape;+class KoShape;++/**+ * @brief The KoSvgTextReorderShapeInsideCommand class+ * Within a text shape, the order of the shapes inside determines the order+ * in which the shapes are evaluated for filling. This command allows changing+ * that order.+ */+class KRITAFLAKE_EXPORT KoSvgTextReorderShapeInsideCommand: public KUndo2Command+{+public:+ enum MoveShapeType {+ MoveEarlier,+ MoveLater,+ BringToFront,+ SendToBack+ };++ KoSvgTextReorderShapeInsideCommand(KoSvgTextShape* textShape, KoShape *shape, MoveShapeType type, KUndo2Command *parent = nullptr);+ ~KoSvgTextReorderShapeInsideCommand();++ void redo() override;+ void undo() override;+private:+ struct Private;+ QScopedPointer<Private> d;+};++#endif // KOSVGTEXTREORDERSHAPEINSIDECOMMAND_Hdiff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex a50c94eb0c..b91d3fca36 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -2135,6 +2135,19 @@ void KoSvgTextShape::removeShapesFromContours(QList<KoShape *> shapes, bool call}}+void KoSvgTextShape::moveShapeInsideToIndex(KoShape *shapeInside, const int index)+{+ const int oldIndex = d->shapesInside.indexOf(shapeInside);+ if (oldIndex < 0) return;++ // Update.+ d->shapesInside.move(oldIndex, index);+ d->updateTextWrappingAreas();+ d->updateInternalShapesList();+ shapeChangedPriv(ContentChanged);+ update();+}+QList<KoShape *> KoSvgTextShape::shapesInside() const{return d->shapesInside;diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 3b330d41f3..bca212f7a1 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -121,6 +121,17 @@ public:*/void removeShapesFromContours(QList<KoShape*> shapes, bool callUpdate = true);+ /**+ * @brief moveShapeInsideToIndex+ * Because the order of shapes inside shape-inside affects the text layout,+ * it can be useful to be able to reorder them.+ * @param shapeInside -- shape from the shapesInside list. This function will do nothing+ * @param index -- new index to move the shape inside to. Will be bound to the list size.+ *+ * @seealso shapesInside();+ */+ void moveShapeInsideToIndex(KoShape* shapeInside, const int index);+/*** @brief shapesInside* @return the list of shapes that make up the content area.diff --git a/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp b/plugins/tools/defaulttool/defaulttool/DefaultTool.cppindex e30fd9013c..9bdc592bc8 100644--- a/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp+++ b/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp@@ -43,6 +43,8 @@#include <commands/KoShapeMergeTextPropertiesCommand.h>#include <commands/KoSvgConvertTextTypeCommand.h>#include <commands/KoSvgTextAddRemoveShapeCommands.h>+#include <commands/KoSvgTextFlipShapeContourTypeCommand.h>+#include <commands/KoSvgTextReorderShapeInsideCommand.h>#include <KoSnapGuide.h>#include <KoStrokeConfigWidget.h>@@ -651,7 +653,6 @@ void DefaultTool::slotRemoveShapesFromFlow()if (!selection) return;QList<KoShape *> selectedShapes = selection->selectedEditableShapes();- std::sort(selectedShapes.begin(), selectedShapes.end(), KoShape::compareShapeZIndex);if (selectedShapes.isEmpty()) return;KUndo2Command *parentCommand = new KUndo2Command(kundo2_i18n("Remove shapes from text flow."));@@ -669,6 +670,62 @@ void DefaultTool::slotRemoveShapesFromFlow()canvas()->addCommand(parentCommand);}+void DefaultTool::slotToggleFlowShapeType()+{+ KoSvgTextShape *textShape = canvas()->textShapeManagerEnabled();+ if (!textShape) return;+ KoSelection *selection = koSelection();+ if (!selection) return;++ QList<KoShape *> selectedShapes = selection->selectedEditableShapes();+ if (selectedShapes.isEmpty()) return;+ KUndo2Command *parentCommand = new KUndo2Command(kundo2_i18n("Toggle Flow Shape Type"));++ bool addToCanvas = false;+ Q_FOREACH(KoShape *shape, selectedShapes) {+ if (!textShape->shapesInside().contains(shape)+ && !textShape->shapesSubtract().contains(shape)) continue;+ addToCanvas = true;+ new KoSvgTextFlipShapeContourTypeCommand(textShape, shape, parentCommand);+ }+ if (addToCanvas) {+ canvas()->addCommand(parentCommand);+ }+}++void DefaultTool::slotReorderFlowShapes(int type)+{+ KoSvgTextShape *textShape = canvas()->textShapeManagerEnabled();+ if (!textShape) return;+ KoSelection *selection = koSelection();+ if (!selection) return;++ QList<KoShape *> selectedShapes = selection->selectedEditableShapes();+ if (selectedShapes.isEmpty()) return;++ KUndo2Command *parentCommand = new KUndo2Command();+ if (type == KoSvgTextReorderShapeInsideCommand::BringToFront) {+ parentCommand->setText(kundo2_i18n("Set Flow Shape as First"));+ } else if (type == KoSvgTextReorderShapeInsideCommand::MoveEarlier) {+ parentCommand->setText(kundo2_i18n("Decrease Flow Shape Index"));+ } else if (type == KoSvgTextReorderShapeInsideCommand::MoveLater) {+ parentCommand->setText(kundo2_i18n("Increase Flow Shape Index"));+ } else {+ parentCommand->setText(kundo2_i18n("Set Flow Shape as Last"));+ }++ bool addToCanvas = false;+ Q_FOREACH(KoShape *shape, selectedShapes) {+ if (!textShape->shapesInside().contains(shape)) continue;+ new KoSvgTextReorderShapeInsideCommand(textShape, shape, KoSvgTextReorderShapeInsideCommand::MoveShapeType(type), parentCommand);+ addToCanvas = true;+ }++ if (addToCanvas) {+ canvas()->addCommand(parentCommand);+ }+}+bool DefaultTool::wantsAutoScroll() const{return true;@@ -724,6 +781,13 @@ void DefaultTool::setupActions()addMappedAction(m_textTypeSignalsMapper, "text_type_inline_wrap", KoSvgTextShape::InlineWrap);addMappedAction(m_textTypeSignalsMapper, "text_type_pre_positioned", KoSvgTextShape::PrePositionedText);+ m_textFlowSignalsMapper = new KisSignalMapper(this);++ addMappedAction(m_textFlowSignalsMapper, "flow_shape_order_back", KoSvgTextReorderShapeInsideCommand::SendToBack);+ addMappedAction(m_textFlowSignalsMapper, "flow_shape_order_earlier", KoSvgTextReorderShapeInsideCommand::MoveEarlier);+ addMappedAction(m_textFlowSignalsMapper, "flow_shape_order_later", KoSvgTextReorderShapeInsideCommand::MoveLater);+ addMappedAction(m_textFlowSignalsMapper, "flow_shape_order_front", KoSvgTextReorderShapeInsideCommand::BringToFront);+m_contextMenu.reset(new QMenu());}@@ -1363,6 +1427,7 @@ void DefaultTool::activate(const QSet<KoShape *> &shapes)connect(m_transformSignalsMapper, SIGNAL(mapped(int)), SLOT(selectionTransform(int)));connect(m_booleanSignalsMapper, SIGNAL(mapped(int)), SLOT(selectionBooleanOp(int)));connect(m_textTypeSignalsMapper, SIGNAL(mapped(int)), SLOT(slotChangeTextType(int)));+ connect(m_textFlowSignalsMapper, SIGNAL(mapped(int)), SLOT(slotReorderFlowShapes(int)));QAction *actionTextInside = action("add_shape_to_flow_area");connect(actionTextInside, SIGNAL(triggered()), this, SLOT(slotAddShapesToFlow()), Qt::UniqueConnection);@@ -1373,6 +1438,9 @@ void DefaultTool::activate(const QSet<KoShape *> &shapes)QAction *actionTextRemoveFlow = action("remove_shapes_from_text_flow");connect(actionTextRemoveFlow, SIGNAL(triggered()), this, SLOT(slotRemoveShapesFromFlow()), Qt::UniqueConnection);+ QAction *actionTextFlowToggle = action("flow_shape_type_toggle");+ connect(actionTextFlowToggle, SIGNAL(triggered()), this, SLOT(slotToggleFlowShapeType()), Qt::UniqueConnection);+m_mouseWasInsideHandles = false;m_lastHandle = KoFlake::NoHandle;useCursor(Qt::ArrowCursor);@@ -1420,6 +1488,7 @@ void DefaultTool::deactivate()disconnect(m_transformSignalsMapper, 0, this, 0);disconnect(m_booleanSignalsMapper, 0, this, 0);disconnect(m_textTypeSignalsMapper, 0, this, 0);+ disconnect(m_textFlowSignalsMapper, 0, this, 0);QAction *actionTextInside = action("add_shape_to_flow_area");disconnect(actionTextInside, 0, this, 0);@@ -1427,6 +1496,8 @@ void DefaultTool::deactivate()disconnect(actionTextSubtract, 0, this, 0);QAction *actionTextRemoveFlow = action("remove_shapes_from_text_flow");disconnect(actionTextRemoveFlow, 0, this, 0);+ QAction *actionTextFlowToggle = action("flow_shape_type_toggle");+ disconnect(actionTextFlowToggle, 0, this, 0);const KisCanvas2 *canvas2 = qobject_cast<const KisCanvas2 *>(this->canvas());if (canvas2) {@@ -2000,21 +2071,35 @@ void DefaultTool::updateActions()action("object_distribute_vertical_bottom")->setEnabled(distributionEnabled);action("object_distribute_vertical_gaps")->setEnabled(distributionEnabled);+ /* Handling the text actions */bool textShape = false;bool otherShapes = false;+ bool shapesInside = false;+ const bool editFlowShapes = (canvas()->textShapeManagerEnabled());Q_FOREACH(KoShape *shape, editableShapes) {KoSvgTextShape *text = dynamic_cast<KoSvgTextShape *>(shape);if (text && !textShape) {textShape = true;} else {otherShapes = true;+ if (editFlowShapes) {+ if (!shapesInside && canvas()->textShapeManagerEnabled()->shapesInside().contains(shape)) {+ shapesInside = true;+ }+ }}if (textShape && otherShapes) break;}const bool editContours = textShape && otherShapes;+action("add_shape_to_flow_area")->setEnabled(editContours);action("subtract_shape_from_flow_area")->setEnabled(editContours);- action("remove_shapes_from_text_flow")->setEnabled((canvas()->textShapeManagerEnabled()));+ action("remove_shapes_from_text_flow")->setEnabled(editFlowShapes);+ action("flow_shape_type_toggle")->setEnabled(editFlowShapes);+ action("flow_shape_order_back")->setEnabled(shapesInside);+ action("flow_shape_order_earlier")->setEnabled(shapesInside);+ action("flow_shape_order_later")->setEnabled(shapesInside);+ action("flow_shape_order_front")->setEnabled(shapesInside);updateDistinctiveActions(editableShapes);@@ -2135,7 +2220,14 @@ QMenu* DefaultTool::popupActionsMenu()QMenu *text = m_contextMenu->addMenu(i18n("Text"));text->addAction(action("add_shape_to_flow_area"));text->addAction(action("subtract_shape_from_flow_area"));+ text->addSeparator();text->addAction(action("remove_shapes_from_text_flow"));+ text->addAction(action("flow_shape_type_toggle"));+ text->addSeparator();+ text->addAction(action("flow_shape_order_back"));+ text->addAction(action("flow_shape_order_earlier"));+ text->addAction(action("flow_shape_order_later"));+ text->addAction(action("flow_shape_order_front"));}return m_contextMenu.data();diff --git a/plugins/tools/defaulttool/defaulttool/DefaultTool.h b/plugins/tools/defaulttool/defaulttool/DefaultTool.hindex 9e8abfd88d..c4d8d9a942 100644--- a/plugins/tools/defaulttool/defaulttool/DefaultTool.h+++ b/plugins/tools/defaulttool/defaulttool/DefaultTool.h@@ -125,6 +125,8 @@ private Q_SLOTS:void slotAddShapesToFlow();void slotSubtractShapesFromFlow();void slotRemoveShapesFromFlow();+ void slotToggleFlowShapeType();+ void slotReorderFlowShapes(int type);protected Q_SLOTS:/// Update actions on selection change@@ -209,6 +211,7 @@ private:KisSignalMapper *m_transformSignalsMapper {0};KisSignalMapper *m_booleanSignalsMapper {0};KisSignalMapper *m_textTypeSignalsMapper {0};+ KisSignalMapper *m_textFlowSignalsMapper {0};DefaultToolTextPropertiesInterface *m_textPropertyInterface{0};};diff --git a/plugins/tools/defaulttool/defaulttool/DefaultToolFactory.cpp b/plugins/tools/defaulttool/defaulttool/DefaultToolFactory.cppindex c3da521492..2059f1ee48 100644--- a/plugins/tools/defaulttool/defaulttool/DefaultToolFactory.cpp+++ b/plugins/tools/defaulttool/defaulttool/DefaultToolFactory.cpp@@ -81,6 +81,12 @@ QList<QAction *> DefaultToolFactory::createActionsImpl()actions << actionRegistry->makeQAction("subtract_shape_from_flow_area", this);actions << actionRegistry->makeQAction("remove_shapes_from_text_flow", this);+ actions << actionRegistry->makeQAction("flow_shape_type_toggle", this);+ actions << actionRegistry->makeQAction("flow_shape_order_back", this);+ actions << actionRegistry->makeQAction("flow_shape_order_earlier", this);+ actions << actionRegistry->makeQAction("flow_shape_order_later", this);+ actions << actionRegistry->makeQAction("flow_shape_order_front", this);+return actions;}