Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Refactor public API for "enter text group" functionality

Dmitry Kazakovafbc7cae4e51
diff --git a/libs/flake/KoCanvasBase.h b/libs/flake/KoCanvasBase.h
index cab031c623..3139943f1d 100644
--- a/libs/flake/KoCanvasBase.h
+++ b/libs/flake/KoCanvasBase.h
@@ -27,7 +27,6 @@ class KoCanvasController;
class KoShape;
class KoSnapGuide;
class KoSelectedShapesProxy;
-class KoSvgTextShape;
class QWidget;
class QCursor;
@@ -117,14 +116,40 @@ public:
virtual KoSelectedShapesProxy *selectedShapesProxy() const = 0;
/**
- * @brief textShapeManagerEnabled
- * textShape for which we're currently editing the child shapes.
- * @return KoSvgTextShape that is currently set.
+ * @brief the shape that owns the currently active shape manager
+ *
+ * When the editor enters some kind of "group editing mode", the
+ * user see only shapes belonging to this particular group. In other
+ * words, the shapeManager() of this canvas will contain only shapes
+ * of this "entered" group.
+ *
+ * currentShapeManagerOwnerShape() returns the shape that
+ * represents this currently entered group, so that the tools
+ * could detect it better
+ *
+ * If no group is currently entered, then the function returns
+ * nullptr.
+ *
+ * NOTE: the functionality is currently implemented for text
+ * shapes only, not for actual groups :)
*/
- virtual KoSvgTextShape *textShapeManagerEnabled() const {
+ virtual KoShape *currentShapeManagerOwnerShape() const {
return nullptr;
}
- virtual void setTextShapeManagerEnabled(KoSvgTextShape *source) {
+
+ /**
+ * @brief sets the group shape that is supposed to be "entered"
+ *
+ * The tool can instruct the canvas to enter the group
+ * represented by shape \p shape. The implementation of the
+ * canvas is supposed to switch its shapeManager() to
+ * the one owned by \p shape.
+ *
+ * NOTE: we currently have no common interface for fetching
+ * the shape manager from a particular shape. The canvas
+ * is supposed to know how to dynamic_cast the shape itself.
+ */
+ virtual void setCurrentShapeManagerOwnerShape(KoShape *source) {
Q_UNUSED(source)
}
/**
diff --git a/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp b/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp
index 1a13cc2914..620bce10f5 100644
--- a/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp
+++ b/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp
@@ -35,7 +35,7 @@ struct KoSvgTextShapeOutlineHelper::Private {
bool textWrappingAreasHovered = false;
KoSvgTextShape *getTextModeShape() {
- return canvas->textShapeManagerEnabled();
+ return dynamic_cast<KoSvgTextShape*>(canvas->currentShapeManagerOwnerShape());
}
KoSvgTextShape *getPotentialTextShape(const QPointF &point) {
@@ -269,10 +269,10 @@ bool KoSvgTextShapeOutlineHelper::updateTextContourMode()
void KoSvgTextShapeOutlineHelper::toggleTextContourMode(KoSvgTextShape *shape)
{
if (d->canvas) {
- if (shape == d->canvas->textShapeManagerEnabled()) {
- d->canvas->setTextShapeManagerEnabled(nullptr);
+ if (shape == d->canvas->currentShapeManagerOwnerShape()) {
+ d->canvas->setCurrentShapeManagerOwnerShape(nullptr);
} else {
- d->canvas->setTextShapeManagerEnabled(shape);
+ d->canvas->setCurrentShapeManagerOwnerShape(shape);
}
}
}
diff --git a/libs/ui/canvas/kis_canvas2.cpp b/libs/ui/canvas/kis_canvas2.cpp
index 533c36e7ad..c7ad8d72ac 100644
--- a/libs/ui/canvas/kis_canvas2.cpp
+++ b/libs/ui/canvas/kis_canvas2.cpp
@@ -111,6 +111,36 @@
#include <KisCanvasState.h>
+namespace {
+ struct ShapeLifetimeWrapper : KoShape::ShapeChangeListener
+ {
+ ShapeLifetimeWrapper(KoShape *shape, std::function<void()> destructionHandler)
+ : m_shape(shape),
+ m_destructionHandler(destructionHandler)
+ {
+ KIS_SAFE_ASSERT_RECOVER_RETURN(destructionHandler);
+ KIS_SAFE_ASSERT_RECOVER_RETURN(shape);
+
+ shape->addShapeChangeListener(this);
+ }
+
+ void notifyShapeChanged(KoShape::ChangeType type, KoShape *) override {
+ if (type == KoShape::Deleted) {
+ m_destructionHandler();
+ m_shape = nullptr;
+ }
+ }
+
+ KoShape* shape() const {
+ return m_shape;
+ }
+
+ private:
+ KoShape *m_shape = nullptr;
+ std::function<void()> m_destructionHandler;
+ };
+}
+
class Q_DECL_HIDDEN KisCanvas2::KisCanvas2Private
{
/**
@@ -190,7 +220,8 @@ public:
KoShapeManager shapeManager;
KisSelectedShapesProxy selectedShapesProxy;
bool currentCanvasIsOpenGL = true;
- KoSvgTextShape *textShape = nullptr;
+ std::optional<ShapeLifetimeWrapper> groupModeShapeWrapper;
+
int openGLFilterMode = 0;
KisToolProxy toolProxy;
KisPrescaledProjectionSP prescaledProjection;
@@ -576,16 +607,28 @@ KoShapeManager* KisCanvas2::globalShapeManager() const
return &m_d->shapeManager;
}
-KoSvgTextShape *KisCanvas2::textShapeManagerEnabled() const
+KoShape *KisCanvas2::currentShapeManagerOwnerShape() const
{
- return m_d->textShape;
+ return m_d->groupModeShapeWrapper ? m_d->groupModeShapeWrapper->shape() : nullptr;
}
-void KisCanvas2::setTextShapeManagerEnabled(KoSvgTextShape *source)
+void KisCanvas2::setCurrentShapeManagerOwnerShape(KoShape *source)
{
- if (m_d->textShape == source) return;
- m_d->textShape = source;
- slotTrySwitchShapeManager();
+ if (currentShapeManagerOwnerShape() == source) return;
+
+ m_d->groupModeShapeWrapper = std::nullopt;
+
+ // we currently support entering text shapes only
+ if (source && dynamic_cast<KoSvgTextShape*>(source)) {
+ m_d->groupModeShapeWrapper.emplace(source, [this] () {
+ this->setCurrentShapeManagerOwnerShape(nullptr);
+ });
+ }
+
+ /// recalculate the local shape manager and make sure
+ /// that selected shapes proxy emits all the necessary
+ /// signals
+ (void) localShapeManager();
}
KoShapeManager *KisCanvas2::localShapeManager() const
@@ -593,11 +636,28 @@ KoShapeManager *KisCanvas2::localShapeManager() const
KisNodeSP node = m_d->view->currentNode();
KoShapeManager *localShapeManager = fetchShapeManagerFromNode(node);
- if (m_d->textShape) {
- if (localShapeManager && localShapeManager->shapes().contains(m_d->textShape)) {
- localShapeManager = m_d->textShape->internalShapeManager();
- } else {
- m_d->textShape = nullptr;
+ /**
+ * The group shape should be manually reset by the external code
+ * when the active layer is switched.
+ */
+ KIS_SAFE_ASSERT_RECOVER(
+ !m_d->groupModeShapeWrapper ||
+ !localShapeManager ||
+ localShapeManager->shapes().contains(m_d->groupModeShapeWrapper->shape())) {
+
+ m_d->groupModeShapeWrapper = std::nullopt;
+ }
+
+ if (m_d->groupModeShapeWrapper) {
+ auto textShape = dynamic_cast<KoSvgTextShape*>(m_d->groupModeShapeWrapper->shape());
+
+ // we currently support entering text shapes only
+ KIS_SAFE_ASSERT_RECOVER(textShape) {
+ m_d->groupModeShapeWrapper = std::nullopt;
+ }
+
+ if (textShape) {
+ localShapeManager = textShape->internalShapeManager();
}
}
@@ -1314,16 +1374,7 @@ KisPopupPalette *KisCanvas2::popupPalette()
void KisCanvas2::slotTrySwitchShapeManager()
{
- // Just call localShapeManager to sync the shape manager.
- localShapeManager();
- /*
- KisNodeSP node = m_d->view->currentNode();
-
- QPointer<KoShapeManager> newManager;
- newManager = fetchShapeManagerFromNode(node);
-
- m_d->setActiveShapeManager(newManager);
- */
+ // noop, the shape manager is recalculated lazily on the fly
}
void KisCanvas2::notifyLevelOfDetailChange()
diff --git a/libs/ui/canvas/kis_canvas2.h b/libs/ui/canvas/kis_canvas2.h
index 5ca5377eb5..f4df18a262 100644
--- a/libs/ui/canvas/kis_canvas2.h
+++ b/libs/ui/canvas/kis_canvas2.h
@@ -47,7 +47,6 @@ class KisCoordinatesConverter;
class KoViewConverter;
class KisAbstractCanvasWidget;
class KisPopupPalette;
-class KoSvgTextShape;
/**
@@ -110,8 +109,8 @@ public: // KoCanvasBase implementation
*/
KoShapeManager *globalShapeManager() const;
- KoSvgTextShape *textShapeManagerEnabled() const override;
- void setTextShapeManagerEnabled(KoSvgTextShape *source) override;
+ KoShape *currentShapeManagerOwnerShape() const override;
+ void setCurrentShapeManagerOwnerShape(KoShape *source) override;
/**
* Return shape manager associated with the currently active node.
diff --git a/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp b/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp
index 654fb787f8..5a5e34adb6 100644
--- a/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp
+++ b/plugins/tools/defaulttool/defaulttool/DefaultTool.cpp
@@ -683,9 +683,14 @@ void DefaultTool::slotSubtractShapesFromFlow()
selection->select(textShape);
}
+KoSvgTextShape* DefaultTool::tryFetchCurrentShapeManagerOwnerTextShape() const
+{
+ return dynamic_cast<KoSvgTextShape*>(canvas()->currentShapeManagerOwnerShape());
+}
+
void DefaultTool::slotRemoveShapesFromFlow()
{
- KoSvgTextShape *textShape = canvas()->textShapeManagerEnabled();
+ KoSvgTextShape *textShape = tryFetchCurrentShapeManagerOwnerTextShape();
if (!textShape) return;
KoSelection *selection = koSelection();
if (!selection) return;
@@ -707,7 +712,7 @@ void DefaultTool::slotRemoveShapesFromFlow()
void DefaultTool::slotToggleFlowShapeType()
{
- KoSvgTextShape *textShape = canvas()->textShapeManagerEnabled();
+ KoSvgTextShape *textShape = tryFetchCurrentShapeManagerOwnerTextShape();
if (!textShape) return;
KoSelection *selection = koSelection();
if (!selection) return;
@@ -730,7 +735,7 @@ void DefaultTool::slotToggleFlowShapeType()
void DefaultTool::slotReorderFlowShapes(int type)
{
- KoSvgTextShape *textShape = canvas()->textShapeManagerEnabled();
+ KoSvgTextShape *textShape = tryFetchCurrentShapeManagerOwnerTextShape();
if (!textShape) return;
KoSelection *selection = koSelection();
if (!selection) return;
@@ -2128,7 +2133,8 @@ void DefaultTool::updateActions()
bool textShape = false;
bool otherShapes = false;
bool shapesInside = false;
- const bool editFlowShapes = (canvas()->textShapeManagerEnabled());
+ KoSvgTextShape *currentTextShapeGroup = tryFetchCurrentShapeManagerOwnerTextShape();
+ const bool editFlowShapes = bool(currentTextShapeGroup);
Q_FOREACH(KoShape *shape, editableShapes) {
KoSvgTextShape *text = dynamic_cast<KoSvgTextShape *>(shape);
if (text && !textShape) {
@@ -2136,7 +2142,7 @@ void DefaultTool::updateActions()
} else {
otherShapes = true;
if (editFlowShapes) {
- if (!shapesInside && canvas()->textShapeManagerEnabled()->shapesInside().contains(shape)) {
+ if (!shapesInside && currentTextShapeGroup->shapesInside().contains(shape)) {
shapesInside = true;
}
}
@@ -2413,8 +2419,11 @@ void DefaultToolTextPropertiesInterface::slotSelectionChanged()
if (!shape) continue;
shape->removeShapeChangeListener(this);
}
- if (d->parent->canvas()->textShapeManagerEnabled()) {
- d->shapes = {d->parent->canvas()->textShapeManagerEnabled()};
+
+ auto *textShapeGroup = d->parent->tryFetchCurrentShapeManagerOwnerTextShape();
+
+ if (textShapeGroup) {
+ d->shapes = {textShapeGroup};
} else {
d->shapes = d->parent->canvas()->selectedShapesProxy()->selection()->selectedEditableShapes();
}
diff --git a/plugins/tools/defaulttool/defaulttool/DefaultTool.h b/plugins/tools/defaulttool/defaulttool/DefaultTool.h
index c1eaf3f2ac..1ca9010ae1 100644
--- a/plugins/tools/defaulttool/defaulttool/DefaultTool.h
+++ b/plugins/tools/defaulttool/defaulttool/DefaultTool.h
@@ -93,6 +93,8 @@ public:
bool updateTextContourMode();
+ KoSvgTextShape* tryFetchCurrentShapeManagerOwnerTextShape() const;
+
public Q_SLOTS:
void activate(const QSet<KoShape *> &shapes) override;
void deactivate() override;
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 803c2712e2..3773828ef9 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -180,7 +180,7 @@ void SvgTextTool::activate(const QSet<KoShape *> &shapes)
KisCanvas2 *canvas2 = qobject_cast<KisCanvas2 *>(this->canvas());
if (canvas2) {
- canvas2->setTextShapeManagerEnabled(nullptr);
+ canvas2->setCurrentShapeManagerOwnerShape(nullptr);
canvas2->viewManager()->textPropertyManager()->setTextPropertiesInterface(m_textCursor.textPropertyInterface());
QDockWidget *docker = canvas2->viewManager()->mainWindow()->dockWidget("TextProperties");
if (docker && m_optionManager) {