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

Implement KoShapeBulkActionInterface

Dmitry Kazakov908673576aa5
diff --git a/libs/flake/CMakeLists.txt b/libs/flake/CMakeLists.txt
index 282df049af..2ed76f7294 100644
--- a/libs/flake/CMakeLists.txt
+++ b/libs/flake/CMakeLists.txt
@@ -52,6 +52,7 @@ set(kritaflake_SRCS
KoSelectedShapesProxySimple.cpp
KoShape.cpp
KoShapeAnchor.cpp
+ KoShapeBulkActionLock.cpp
KoShapeControllerBase.cpp
KoShapeApplicationData.cpp
KoShapeContainer.cpp
diff --git a/libs/flake/KoShape.cpp b/libs/flake/KoShape.cpp
index 0f6be11a01..fc9c3b0945 100644
--- a/libs/flake/KoShape.cpp
+++ b/libs/flake/KoShape.cpp
@@ -1227,17 +1227,18 @@ bool KoShape::addDependee(KoShape *shape)
if (shape->hasDependee(this))
return false;
- if (! d->dependees.contains(shape))
+ if (! d->dependees.contains(shape)) {
d->dependees.append(shape);
+ shape->addShapeChangeListener(&d->dependeesLifetimeListener);
+ }
return true;
}
void KoShape::removeDependee(KoShape *shape)
{
- int index = d->dependees.indexOf(shape);
- if (index >= 0)
- d->dependees.removeAt(index);
+ d->dependees.removeOne(shape);
+ shape->removeShapeChangeListener(&d->dependeesLifetimeListener);
}
bool KoShape::hasDependee(KoShape *shape) const
@@ -1254,6 +1255,13 @@ void KoShape::shapeChanged(ChangeType type, KoShape *shape)
{
Q_UNUSED(type);
Q_UNUSED(shape);
+
+ /**
+ * NOTE: we don't need to handle Deleted type for the shapes
+ * in d->dependees list here, becasue this function is called
+ * on the other side of the relation, i.e. in the actual
+ * "dependee". We handle that in Private::DependeesLifetimeListener.
+ */
}
KoSnapData KoShape::snapData() const
diff --git a/libs/flake/KoShapeBulkActionInterface.h b/libs/flake/KoShapeBulkActionInterface.h
new file mode 100644
index 0000000000..ca4b8d735a
--- /dev/null
+++ b/libs/flake/KoShapeBulkActionInterface.h
@@ -0,0 +1,52 @@
+/* This file is part of the KDE project
+ SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#ifndef KOSHAPEBULKACTIONINTERFACE_H
+#define KOSHAPEBULKACTIONINTERFACE_H
+
+#include <QRect>
+
+/**
+ * @brief Interface for bulk actions on shapes
+ *
+ * This interface provides a mechanism for executing long-running actions
+ * on shapes that will result in multiple shapeChanged() callbacks.
+ *
+ * When a shape enters an "bulk action" state, the following semantics apply:
+ *
+ * 1) All methods of KoShape class except paint() and outline() are considered
+ * valid throughout any moment of the shape being in this state, including
+ * outlineRect() and boundingRect().
+ *
+ * 2) Painting-related methods cannot be used in this state, as the shape might
+ * need to perform heavy calculations for them.
+ *
+ * 3) outline() may (or may not) also become invalid in this state.
+ *
+ * 4) Any non-KoShape specific methods may be invalid in this state.
+ */
+struct KoShapeBulkActionInterface
+{
+ virtual ~KoShapeBulkActionInterface() = default;
+
+ /**
+ * Called by the GUI code when it wants to execute some kind
+ * of a long action, which will result in multiple shapeChanged()
+ * callbacks to be delivered.
+ *
+ * When started, the shape enters into an "bulk action" state
+ * as described in the class documentation.
+ */
+ virtual void startBulkAction() = 0;
+
+ /**
+ * Ends bulk action state and performs all calculation-heavy
+ * operations over the new state. Returns a rectangle in absolute
+ * coordinates that should be updated to make these changes visible
+ * to the user.
+ */
+ virtual QRectF endBulkAction() = 0;
+};
+
+#endif // KOSHAPEBULKACTIONINTERFACE_H
diff --git a/libs/flake/KoShapeBulkActionLock.cpp b/libs/flake/KoShapeBulkActionLock.cpp
new file mode 100644
index 0000000000..ef6ee1720b
--- /dev/null
+++ b/libs/flake/KoShapeBulkActionLock.cpp
@@ -0,0 +1,94 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <KoShapeBulkActionLock.h>
+
+#include <kis_debug.h>
+#include <KoShape.h>
+
+#include <KoShapeBulkActionInterface.h>
+
+KoShapeBulkActionLockAdapter::KoShapeBulkActionLockAdapter(const QList<KoShape*> &shapes)
+{
+ Q_FOREACH(KoShape *shape, shapes) {
+ if (!tryAddBulkInterfaceShape(shape)) {
+ m_normalUpdateShapes.append(shape);
+ }
+ addBulkInterfaceDependees(shape->dependees());
+ }
+}
+
+bool KoShapeBulkActionLockAdapter::tryAddBulkInterfaceShape(KoShape *shape)
+{
+ KoShapeBulkActionInterface *iface = dynamic_cast<KoShapeBulkActionInterface *>(shape);
+ if (iface) {
+ // postpone update till the end, if the shape is present
+ // multiple times
+ if (m_bulkInterfaceShapes.contains(iface)) {
+ m_bulkInterfaceShapes.removeOne(iface);
+ }
+ m_bulkInterfaceShapes.append(iface);
+ return true;
+ }
+ return false;
+};
+
+void KoShapeBulkActionLockAdapter::addBulkInterfaceDependees(const QList<KoShape*> dependees)
+{
+ Q_FOREACH(KoShape *shape, dependees) {
+ tryAddBulkInterfaceShape(shape);
+ addBulkInterfaceDependees(shape->dependees());
+ }
+}
+
+void KoShapeBulkActionLockAdapter::lock()
+{
+ m_finalUpdates.clear();
+
+ Q_FOREACH(KoShape *shape, m_normalUpdateShapes) {
+ m_finalUpdates[shape] |= shape->boundingRect();
+ }
+
+ Q_FOREACH(KoShapeBulkActionInterface *iface, m_bulkInterfaceShapes) {
+ iface->startBulkAction();
+ }
+
+}
+
+void KoShapeBulkActionLockAdapter::unlock()
+{
+ Q_FOREACH(KoShapeBulkActionInterface *iface, m_bulkInterfaceShapes) {
+ const QRectF update = iface->endBulkAction();
+
+ KoShape *shape = dynamic_cast<KoShape*>(iface);
+ KIS_SAFE_ASSERT_RECOVER(shape) { continue; }
+ m_finalUpdates[shape] |= update;
+ }
+
+ Q_FOREACH(KoShape *shape, m_normalUpdateShapes) {
+ m_finalUpdates[shape] |= shape->boundingRect();
+ }
+}
+
+KoShapeBulkActionLockAdapter::UpdatesList
+KoShapeBulkActionLockAdapter::takeFinalUpdatesList()
+{
+ UpdatesList result;
+ result.reserve(m_finalUpdates.size());
+
+ for (auto it = m_finalUpdates.begin(); it != m_finalUpdates.end(); ++it) {
+ result.emplace_back(it->first, it->second);
+ }
+
+ return result;
+}
+
+KoShapeBulkActionLock::~KoShapeBulkActionLock()
+{
+ if (owns_lock()) {
+ qWarning() << "WARNING: KoShapeBulkActionLock is destroyed while being locked. Update rect will be lost!";
+ }
+}
\ No newline at end of file
diff --git a/libs/flake/KoShapeBulkActionLock.h b/libs/flake/KoShapeBulkActionLock.h
new file mode 100644
index 0000000000..2a1de493b2
--- /dev/null
+++ b/libs/flake/KoShapeBulkActionLock.h
@@ -0,0 +1,64 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef KOSHAPEBULKACTIONLOCK_H
+#define KOSHAPEBULKACTIONLOCK_H
+
+#include "kritaflake_export.h"
+
+#include <KisAdaptedLock.h>
+#include <QList>
+#include <QRect>
+
+class KoShape;
+class KoShapeBulkActionInterface;
+
+class KRITAFLAKE_EXPORT KoShapeBulkActionLockAdapter {
+public:
+ using Update = std::pair<KoShape*, QRectF>;
+ using UpdatesList = std::vector<Update>;
+public:
+ KoShapeBulkActionLockAdapter(const QList<KoShape*> &shapes);
+ void lock();
+ void unlock();
+
+ UpdatesList takeFinalUpdatesList();
+
+private:
+ bool tryAddBulkInterfaceShape(KoShape *shape);
+ void addBulkInterfaceDependees(const QList<KoShape*> dependees);
+
+private:
+ QList<KoShapeBulkActionInterface*> m_bulkInterfaceShapes;
+ QList<KoShape*> m_normalUpdateShapes;
+ std::unordered_map<KoShape*, QRectF> m_finalUpdates;
+};
+
+class KRITAFLAKE_EXPORT KoShapeBulkActionLock : protected KisAdaptedLock<KoShapeBulkActionLockAdapter>
+{
+public:
+ using BaseClass = KisAdaptedLock<KoShapeBulkActionLockAdapter>;
+ using BaseClass::BaseClass;
+
+ ~KoShapeBulkActionLock();
+
+ using BaseClass::Update;
+ using BaseClass::UpdatesList;
+
+ using BaseClass::lock;
+
+ [[nodiscard]] UpdatesList unlock() {
+ BaseClass::unlock();
+ return KoShapeBulkActionLockAdapter::takeFinalUpdatesList();
+ }
+
+ using BaseClass::owns_lock;
+ using BaseClass::swap;
+ using BaseClass::release;
+ using BaseClass::operator bool;
+};
+
+#endif /* KOSHAPEBULKACTIONLOCK_H */
diff --git a/libs/flake/KoShape_p.h b/libs/flake/KoShape_p.h
index b9b6c59f2b..b69e64c16b 100644
--- a/libs/flake/KoShape_p.h
+++ b/libs/flake/KoShape_p.h
@@ -76,12 +76,39 @@ public:
class KoShape::Private
{
+private:
+ /**
+ * A special listener class to track the lifetime of the dependees
+ * and remove them from the notification list when they are deleted
+ */
+ struct DependeesLifetimeListener : KoShape::ShapeChangeListener
+ {
+ DependeesLifetimeListener(QList<KoShape*> &dependees)
+ : m_dependees(dependees)
+ {
+ }
+
+ void notifyShapeChanged(KoShape::ChangeType type, KoShape *shape) override {
+ if (type == KoShape::Deleted && m_dependees.contains(shape)) {
+ m_dependees.removeOne(shape);
+ // the listener will be unlinked on destruction automatically
+ }
+ }
+
+ private:
+ QList<KoShape*> &m_dependees;
+ };
+
public:
- KoShapeContainer *parent;
+ Private() : dependeesLifetimeListener(dependees) {}
+ Private(const Private &rhs) = delete;
+
+ KoShapeContainer *parent = nullptr;
QSet<KoShapeManager *> shapeManagers;
QSet<KoShape *> toolDelegates;
QList<KoShape*> dependees; ///< list of shape dependent on this shape
QList<KoShape::ShapeChangeListener*> listeners;
+ DependeesLifetimeListener dependeesLifetimeListener;
};
#endif
diff --git a/libs/flake/tests/CMakeLists.txt b/libs/flake/tests/CMakeLists.txt
index f4b74bbb52..f34c3812f6 100644
--- a/libs/flake/tests/CMakeLists.txt
+++ b/libs/flake/tests/CMakeLists.txt
@@ -30,6 +30,7 @@ kis_add_tests(
TestXsimdPainting.cpp
TestFontLibraryResourceUtils.cpp
TestSvgTextShape.cpp
+ TestDependentShapes.cpp
LINK_LIBRARIES kritaflake kritatestsdk
NAME_PREFIX "libs-flake-"
diff --git a/libs/flake/tests/TestDependentShapes.cpp b/libs/flake/tests/TestDependentShapes.cpp
new file mode 100644
index 0000000000..346014a418
--- /dev/null
+++ b/libs/flake/tests/TestDependentShapes.cpp
@@ -0,0 +1,465 @@
+/*
+ * SPDX-FileCopyrightText: 2006-2010 Thomas Zander <[email protected]>
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "TestDependentShapes.h"
+
+#include <MockShapes.h>
+#include <testflake.h>
+
+#include <simpletest.h>
+
+#include <KoShapeBulkActionInterface.h>
+#include <KoShapeBulkActionLock.h>
+
+struct BulkActionState {
+ QRectF originalBoundingRect;
+ bool linkedShapesChangedWhileLocked = false;
+};
+
+struct MockBulkShape : MockShape, KoShapeBulkActionInterface
+{
+ using ChangesList = std::vector<std::pair<ChangeType, KoShape*>>;
+
+ void startBulkAction() override {
+ KIS_SAFE_ASSERT_RECOVER_RETURN(!m_bulkActionState);
+ m_bulkActionState = {boundingRect(), false};
+ }
+ QRectF endBulkAction() override {
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_bulkActionState, QRectF());
+
+ QRectF updateRect;
+
+ if (m_bulkActionState->linkedShapesChangedWhileLocked) {
+ forceUpdateLinkedShapes();
+ notifyChanged();
+ updateRect = m_bulkActionState->originalBoundingRect | boundingRect();
+ }
+
+ m_bulkActionState = std::nullopt;
+
+ return updateRect;
+ }
+
+ bool isBulkActionInProgress() const {
+ return m_bulkActionState.has_value();
+ }
+
+ void shapeChanged(ChangeType type, KoShape *shape) override {
+ if (!shape && type == KoShape::GenericMatrixChange) {
+ // we skip our own changes for unittest simplicity reasons
+ return;
+ }
+
+ if (m_bulkActionState) {
+ m_receivedChangesWhileLocked.emplace_back(type, shape);
+ } else {
+ m_receivedChangesWhileUnlocked.emplace_back(type, shape);
+ }
+
+ if (m_linkedShapes.contains(shape) && type == KoShape::GenericMatrixChange) {
+ if (m_bulkActionState) {
+ // postpone until bulk action is finished
+ m_bulkActionState->linkedShapesChangedWhileLocked = true;
+ } else {
+ // slow recalculation method!
+ forceUpdateLinkedShapes();
+ }
+
+ // KoShapeManager's updates are issued without any delay,
+ // since they depend on the shape's boundingRect() and that
+ // is guaranteed to be correct all the time
+ notifyChanged();
+
+ // pass the update further to let unittest check
+ // transitive links
+ shapeChangedPriv(KoShape::GenericMatrixChange);
+ }
+ }
+
+ const ChangesList& receivedChangesWhileLocked() const {
+ return m_receivedChangesWhileLocked;
+ }
+
+ const ChangesList& receivedChangesWhileUnlocked() const {
+ return m_receivedChangesWhileUnlocked;
+ }
+
+ void clearReceivedChanges() {
+ m_receivedChangesWhileLocked.clear();
+ m_receivedChangesWhileUnlocked.clear();
+ }
+
+ void registerLinkedShapes(std::initializer_list<KoShape*> shapes) {
+ m_linkedShapes = std::move(shapes);
+
+ Q_FOREACH(KoShape *shape, m_linkedShapes) {
+ shape->addDependee(this);
+ }
+
+ forceUpdateLinkedShapes();
+ }
+
+ QRectF outlineRect() const override {
+ /**
+ * outlineRect() is recalculated on the fly as required
+ * by KoShapeBulkActionInterface
+ */
+
+ QRectF dependentOutlineRect;
+ Q_FOREACH(KoShape *shape, m_linkedShapes) {
+ dependentOutlineRect |= shape->absoluteOutlineRect();
+ }
+ return dependentOutlineRect;
+ }
+
+ // dependentOutlineRect() is the value that is "slow" to
+ // calculate, so we postpone that untill the end of the action
+ QRectF dependentOutlineRect() const {
+ return m_dependentOutlineRect;
+ }
+
+ void forceUpdateLinkedShapes() {
+ m_dependentOutlineRect = {};
+ Q_FOREACH(KoShape *shape, m_linkedShapes) {
+ m_dependentOutlineRect |= shape->absoluteOutlineRect();
+ }
+ }
+
+private:
+ ChangesList m_receivedChangesWhileLocked;
+ ChangesList m_receivedChangesWhileUnlocked;
+ QRectF m_dependentOutlineRect;
+ QVector<KoShape*> m_linkedShapes;
+
+ std::optional<BulkActionState> m_bulkActionState;
+};
+
+void TestDependentShapes::testDeletionNotifications_data()
+{
+ QTest::addColumn<bool>("dependeeFirst");
+
+ QTest::addRow("dependee-source") << true;
+ QTest::addRow("source-dependee") << false;
+}
+
+void TestDependentShapes::testDeletionNotifications()
+{
+ QFETCH(bool, dependeeFirst);
+
+ MockShape *shape1(new MockShape());
+ MockShape *shape2(new MockShape());
+
+ shape1->addDependee(shape2);
+
+ if (dependeeFirst) {
+ delete shape2;
+ QVERIFY(shape1->dependees().isEmpty());
+ delete shape1;
+ } else {
+ delete shape1;
+ delete shape2;
+ }
+}
+
+void TestDependentShapes::testBulkActionInterface_data()
+{
+ QTest::addColumn<bool>("useBulkAction");
+
+ QTest::addRow("non-bulk-action") << false;
+ QTest::addRow("bulk-action") << true;
+}
+
+bool verifyShapePickable(KoShape *shape, KoShapeManager *shapeManager)
+{
+ auto pickOnePoint = [=] (KoFlake::AnchorPosition pos) {
+ const QPointF pt = shape->absolutePosition(pos);
+ KoShape *pickedShape = shapeManager->shapeAt(pt);
+ if (pickedShape != shape) {
+ qCritical() << "Failed to pick a shape using shape manager"
+ << ppVar(shape->name())
+ << ppVar(shape->boundingRect())
+ << ppVar(pos)
+ << ppVar(pt);
+ return false;
+ }
+ return true;
+ };
+ return
+ pickOnePoint(KoFlake::TopLeft) &&
+ pickOnePoint(KoFlake::TopRight) &&
+ pickOnePoint(KoFlake::BottomRight) &&
+ pickOnePoint(KoFlake::BottomLeft);
+}
+
+void TestDependentShapes::testBulkActionInterface()
+{
+ QFETCH(bool, useBulkAction);
+
+ MockShapeController controller;
+ MockCanvas canvas(&controller);
+
+ KoShapeManager *shapeManager = canvas.shapeManager();
+
+ MockShape *shape1(new MockShape());
+ shape1->setSize({10, 10});
+ shape1->setAbsolutePosition({5, 5}, KoFlake::TopLeft);
+ QCOMPARE(shape1->absoluteOutlineRect(), QRectF(5, 5, 10, 10));
+
+ MockShape *shape2(new MockShape());
+ shape2->setSize({10, 10});
+ shape2->setAbsolutePosition({90, 90}, KoFlake::TopLeft);
+ QCOMPARE(shape2->absoluteOutlineRect(), QRectF(90, 90, 10, 10));
+
+ MockBulkShape *bulkInterfaceShape(new MockBulkShape());
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF());
+ bulkInterfaceShape->registerLinkedShapes({shape1, shape2});
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(5, 5, 95, 95));
+
+ shapeManager->addShape(bulkInterfaceShape);
+ QVERIFY(verifyShapePickable(bulkInterfaceShape, shapeManager));
+
+ QVERIFY(bulkInterfaceShape->receivedChangesWhileLocked().empty());
+ QVERIFY(bulkInterfaceShape->receivedChangesWhileUnlocked().empty());
+
+ if (!useBulkAction) {
+ // try update child shapes without locking
+ // move shape1 by 10,10
+ shape1->applyTransformation(QTransform::fromTranslate(10, 10));
+ QCOMPARE(shape1->absoluteOutlineRect(), QRectF(15, 15, 10, 10));
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileUnlocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, shape1)
+ }));
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileLocked(), {});
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(15, 15, 85, 85));
+ QVERIFY(verifyShapePickable(bulkInterfaceShape, shapeManager));
+
+ // move shape2 by 10,10
+ shape2->applyTransformation(QTransform::fromTranslate(10, 10));
+ QCOMPARE(shape2->absoluteOutlineRect(), QRectF(100, 100, 10, 10));
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileUnlocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, shape1),
+ std::make_pair(KoShape::GenericMatrixChange, shape2)
+ }));
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileLocked(), {});
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(15, 15, 95, 95));
+ QVERIFY(verifyShapePickable(bulkInterfaceShape, shapeManager));
+ } else {
+ bulkInterfaceShape->startBulkAction();
+
+ // move shape1 by 10,10
+ shape1->applyTransformation(QTransform::fromTranslate(10, 10));
+ QCOMPARE(shape1->absoluteOutlineRect(), QRectF(15, 15, 10, 10));
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileUnlocked(), {});
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileLocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, shape1)
+ }));
+
+ // unchanged!!!
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(5, 5, 95, 95));
+ // pickable, because boundingRect() is guaranteed to be correct
+ QVERIFY(verifyShapePickable(bulkInterfaceShape, shapeManager));
+
+ // move shape2 by 10,10
+ shape2->applyTransformation(QTransform::fromTranslate(10, 10));
+ QCOMPARE(shape2->absoluteOutlineRect(), QRectF(100, 100, 10, 10));
+
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileUnlocked(), {});
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileLocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, shape1),
+ std::make_pair(KoShape::GenericMatrixChange, shape2)
+ }));
+
+ // unchanged!!!
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(5, 5, 95, 95));
+ // pickable, because boundingRect() is guaranteed to be correct
+ QVERIFY(verifyShapePickable(bulkInterfaceShape, shapeManager));
+
+ const QRectF resultingUpdate = bulkInterfaceShape->endBulkAction();
+
+ // now finally updated!
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(15, 15, 95, 95));
+
+ // the update should include old and new bounding rects!
+ QCOMPARE(resultingUpdate, QRectF(5, 5, 95, 95) | QRectF(15, 15, 95, 95));
+ }
+
+ shapeManager->setShapes({});
+ delete shape1;
+ delete shape2;
+ delete bulkInterfaceShape;
+}
+
+bool compareUpdatesLists(const KoShapeBulkActionLock::UpdatesList &lhs,
+ const KoShapeBulkActionLock::UpdatesList &rhs)
+{
+ auto updateListToHash =
+ [] (const KoShapeBulkActionLock::UpdatesList &list) {
+
+ std::unordered_map<KoShape*, QRectF> result;
+
+ for (auto it = list.begin(); it != list.end(); ++it) {
+ result.insert(*it);
+ }
+
+ return result;
+ };
+
+ auto lhsHash = updateListToHash(lhs);
+ auto rhsHash = updateListToHash(rhs);
+
+ const bool result = lhsHash == rhsHash;
+
+ if (!result || (lhsHash.size() != lhs.size()) || (rhsHash.size() != rhs.size())) {
+ auto printList = [] (KoShapeBulkActionLock::UpdatesList list) {
+ std::sort(list.begin(), list.end(),
+ kismpl::mem_less(&KoShapeBulkActionLock::Update::first));
+ for (auto it = list.begin(); it != list.end(); ++it) {
+ qWarning() << " " << it->first << it->second;
+ }
+ };
+
+ qWarning() << "Failed to compare update lists:";
+ qWarning() << " lhs:";
+ printList(lhs);
+ qWarning() << " rhs:";
+ printList(rhs);
+ }
+
+ return result;
+}
+
+void TestDependentShapes::testBulkActionLock_data()
+{
+ QTest::addColumn<QPointF>("moveOffset");
+ QTest::addColumn<bool>("useBulkInterfaceShape");
+ QTest::addColumn<bool>("useSecondBulkInterfaceShape");
+
+ QTest::addRow("no-change") << QPointF() << false << false;
+ QTest::addRow("move") << QPointF(10, 10) << false << false;
+ QTest::addRow("move-and-check-bulk") << QPointF(10, 10) << true << false;
+ QTest::addRow("move-and-check-chained-bulk") << QPointF(10, 10) << true << false;
+}
+
+void TestDependentShapes::testBulkActionLock()
+{
+ QFETCH(QPointF, moveOffset);
+ QFETCH(bool, useBulkInterfaceShape);
+ QFETCH(bool, useSecondBulkInterfaceShape);
+
+ const QRectF originalRectShape1 = QRectF(5, 5, 10, 10);
+ const QRectF originalRectShape2 = QRectF(90, 90, 10, 10);
+
+ const QRectF expectedUpdateRectShape1 = originalRectShape1 | originalRectShape1.translated(moveOffset);
+ const QRectF expectedUpdateRectShape2 = originalRectShape2 | originalRectShape2.translated(moveOffset);
+
+ MockShape *shape1(new MockShape());
+ shape1->setSize(originalRectShape1.size());
+ shape1->setAbsolutePosition(originalRectShape1.topLeft(), KoFlake::TopLeft);
+ QCOMPARE(shape1->absoluteOutlineRect(), originalRectShape1);
+
+ MockShape *shape2(new MockShape());
+ shape2->setSize(originalRectShape2.size());
+ shape2->setAbsolutePosition(originalRectShape2.topLeft(), KoFlake::TopLeft);
+ QCOMPARE(shape2->absoluteOutlineRect(), originalRectShape2);
+
+ MockBulkShape *bulkInterfaceShape = nullptr;
+ MockBulkShape *secondBulkInterfaceShape = nullptr;
+
+ if (useBulkInterfaceShape) {
+ bulkInterfaceShape = new MockBulkShape();
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF());
+ bulkInterfaceShape->registerLinkedShapes({shape1, shape2});
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), QRectF(5, 5, 95, 95));
+ }
+
+ if (useSecondBulkInterfaceShape) {
+ secondBulkInterfaceShape = new MockBulkShape();
+ QCOMPARE(secondBulkInterfaceShape->dependentOutlineRect(), QRectF());
+ secondBulkInterfaceShape->registerLinkedShapes({bulkInterfaceShape});
+ QCOMPARE(secondBulkInterfaceShape->dependentOutlineRect(), QRectF(5, 5, 95, 95));
+ }
+
+ QList<KoShape*> shapes = {shape1, shape2};
+
+ KoShapeBulkActionLock lock(shapes);
+
+ if (!moveOffset.isNull()) {
+ shape1->applyTransformation(QTransform::fromTranslate(moveOffset.x(), moveOffset.y()));
+ shape2->applyTransformation(QTransform::fromTranslate(moveOffset.x(), moveOffset.y()));
+
+ if (bulkInterfaceShape) {
+ QCOMPARE(bulkInterfaceShape->receivedChangesWhileLocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, shape1),
+ std::make_pair(KoShape::GenericMatrixChange, shape2)
+ }));
+
+ // the dependent value is outdated!
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), originalRectShape1 | originalRectShape2);
+ }
+ }
+
+ if (secondBulkInterfaceShape) {
+ // we are going to receive this update twice!
+ QCOMPARE(secondBulkInterfaceShape->receivedChangesWhileLocked(),
+ MockBulkShape::ChangesList({
+ std::make_pair(KoShape::GenericMatrixChange, bulkInterfaceShape),
+ std::make_pair(KoShape::GenericMatrixChange, bulkInterfaceShape)
+ }));
+ // the dependent value is outdated!
+ QCOMPARE(secondBulkInterfaceShape->dependentOutlineRect(), originalRectShape1 | originalRectShape2);
+ }
+
+ const KoShapeBulkActionLock::UpdatesList resultingUpdate = lock.unlock();
+
+ KoShapeBulkActionLock::UpdatesList expectedUpdatesList;
+
+ if (bulkInterfaceShape) {
+ // the dependent value is now correct!
+ QCOMPARE(bulkInterfaceShape->dependentOutlineRect(), shape1->absoluteOutlineRect() | shape2->absoluteOutlineRect());
+ }
+
+ if (secondBulkInterfaceShape) {
+ // the dependent value is now correct!
+ QCOMPARE(secondBulkInterfaceShape->dependentOutlineRect(), shape1->absoluteOutlineRect() | shape2->absoluteOutlineRect());
+ }
+
+ expectedUpdatesList.emplace_back(shape1, expectedUpdateRectShape1);
+ expectedUpdatesList.emplace_back(shape2, expectedUpdateRectShape2);
+
+ if (bulkInterfaceShape) {
+ expectedUpdatesList.emplace_back(bulkInterfaceShape,
+ originalRectShape1 |
+ originalRectShape2 |
+ shape1->boundingRect() |
+ shape2->boundingRect());
+ }
+
+ if (secondBulkInterfaceShape) {
+ expectedUpdatesList.emplace_back(secondBulkInterfaceShape,
+ originalRectShape1 |
+ originalRectShape2 |
+ bulkInterfaceShape->boundingRect());
+ }
+
+ // NOTE: The order of shape updates is undefined by protocol
+ QVERIFY(compareUpdatesLists(resultingUpdate, expectedUpdatesList));
+
+ /**
+ * Delete bulk interface shapes **before** the actual shapes
+ * to possibly trigger "dead dependee" issue.
+ */
+ delete secondBulkInterfaceShape;
+ delete bulkInterfaceShape;
+
+ delete shape1;
+ delete shape2;
+}
+
+KISTEST_MAIN(TestDependentShapes)
diff --git a/libs/flake/tests/TestDependentShapes.h b/libs/flake/tests/TestDependentShapes.h
new file mode 100644
index 0000000000..14bf3b6058
--- /dev/null
+++ b/libs/flake/tests/TestDependentShapes.h
@@ -0,0 +1,27 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TESTDEPENDENTSHAPES_H
+#define TESTDEPENDENTSHAPES_H
+
+#include <QObject>
+
+class TestDependentShapes : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+
+ void testDeletionNotifications_data();
+ void testDeletionNotifications();
+
+ void testBulkActionInterface_data();
+ void testBulkActionInterface();
+
+ void testBulkActionLock_data();
+ void testBulkActionLock();
+};
+
+#endif /* TESTDEPENDENTSHAPES_H */