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++
Fix undo for Split Alpha functionality
Dmitry Kazakov06e90fbb4b96
diff --git a/libs/command/kis_undo_stores.cpp b/libs/command/kis_undo_stores.cppindex 44ab99a892..a92ccb4e35 100644--- a/libs/command/kis_undo_stores.cpp+++ b/libs/command/kis_undo_stores.cpp@@ -87,6 +87,15 @@ void KisSurrogateUndoStore::redoAll()}}+bool KisSurrogateUndoStore::canRedo() const+{+ return m_undoStack->canRedo();+}++bool KisSurrogateUndoStore::canUndo() const+{+ return m_undoStack->canUndo();+}/*****************************************************************//* KisDumbUndoStore */diff --git a/libs/command/kis_undo_stores.h b/libs/command/kis_undo_stores.hindex fbda53fe0a..6db560b21f 100644--- a/libs/command/kis_undo_stores.h+++ b/libs/command/kis_undo_stores.h@@ -40,6 +40,9 @@ public:void clear();+ bool canRedo() const;+ bool canUndo() const;+private:KUndo2Stack *m_undoStack;};diff --git a/libs/image/kis_layer_utils.cpp b/libs/image/kis_layer_utils.cppindex 6f93f61c25..ff2d259d69 100644--- a/libs/image/kis_layer_utils.cpp+++ b/libs/image/kis_layer_utils.cpp@@ -55,7 +55,7 @@#include "KisFutureUtils.h"#include "KisBatchUpdateLayerModificationCommand.h"#include "commands_new/KisChangeCloneLayersCommand.h"-+#include "kis_transaction.h"namespace KisLayerUtils {@@ -793,19 +793,20 @@ namespace Private {SplitAlphaToMaskInfoSP m_info;};- struct SplitAlphaCommand : public KUndo2Command {+ struct SplitAlphaCommand : public KisCommandUtils::AggregateCommand {SplitAlphaCommand(SplitAlphaToMaskInfoSP info): m_info(info) {- m_cached = new KisPaintDevice(*m_info->node->paintDevice(), KritaUtils::CopyAllFrames);}- void redo() override {+ void populateChildCommands() override {KisPaintDeviceSP srcDevice = m_info->node->paintDevice();const KoColorSpace *srcCS = srcDevice->colorSpace();const QRect processRect =srcDevice->exactBounds() |srcDevice->defaultBounds()->bounds();+ KisTransaction transaction(srcDevice);+KisSequentialIterator srcIt(srcDevice, processRect);KisSequentialIterator dstIt(m_info->getMaskDevice(), processRect);@@ -816,39 +817,12 @@ namespace Private {*alpha8Ptr = srcCS->opacityU8(srcPtr);srcCS->setOpacity(srcPtr, OPACITY_OPAQUE_U8, 1);}- }-- void undo() override {- KisPaintDeviceSP srcDevice = m_info->node->paintDevice();- if (srcDevice->framesInterface()) { //Swap contents of all frames to reflect the pre-operation state.- KisPaintDeviceSP tempPD = new KisPaintDevice(*m_cached, KritaUtils::CopySnapshot);- Q_FOREACH(const int& frame, srcDevice->framesInterface()->frames() ) {- if (m_cached->framesInterface()->frames().contains(frame)) {- m_cached->framesInterface()->writeFrameToDevice(frame, tempPD);- srcDevice->framesInterface()->uploadFrame(frame, tempPD);- }- }- } else {- const QRect processRect =- srcDevice->exactBounds() |- srcDevice->defaultBounds()->bounds();-- const KoColorSpace *srcCS = srcDevice->colorSpace();- KisSequentialIterator srcIt(m_cached, processRect);- KisSequentialIterator dstIt(srcDevice, processRect);-- while (srcIt.nextPixel() && dstIt.nextPixel()) {- quint8 *srcPtr = srcIt.rawData();- quint8 *dstPtr = dstIt.rawData();- srcCS->setOpacity(dstPtr, srcCS->opacityU8(srcPtr), 1);- }- }+ addCommand(transaction.endAndTake());}private:SplitAlphaToMaskInfoSP m_info;- KisPaintDeviceSP m_cached;};struct UploadProjectionToFrameCommand : public KisCommandUtils::AggregateCommand {@@ -1340,9 +1314,13 @@ namespace Private {void populateChildCommands() override {KUndo2Command *cmd = new KUndo2Command;+KisNodeSP node = m_node ? m_node : m_mergeInfo->dstNode;KisKeyframeChannel *channel = node->getKeyframeChannel(KisKeyframeChannel::Raster.id(), true);- channel->addKeyframe(m_frame, cmd);++ if (!channel->keyframeAt(m_frame)) {+ channel->addKeyframe(m_frame, cmd);+ }if (m_sampledNodes.count() > 0) {applyKeyframeColorLabel(channel->keyframeAt(m_frame), m_sampledNodes);@@ -2431,7 +2409,7 @@ namespace Private {SplitAlphaToMaskInfoSP info( new SplitAlphaToMaskInfo(node->image(), node, maskName) );KisImageSignalVector emitSignals;- KisProcessingApplicator applicator(image, 0,+ KisProcessingApplicator applicator(image, node,KisProcessingApplicator::NONE,emitSignals,kundo2_i18n("Split Alpha into a Mask"));diff --git a/libs/image/tests/kis_image_test.cpp b/libs/image/tests/kis_image_test.cppindex c4f6095faa..a7ae27d003 100644--- a/libs/image/tests/kis_image_test.cpp+++ b/libs/image/tests/kis_image_test.cpp@@ -18,6 +18,9 @@#include "filter/kis_filter_registry.h"#include "kis_image.h"+#include <kis_transaction.h>+#include <kis_image_animation_interface.h>+#include <kis_paint_device_frames_interface.h>#include "kis_node_visitor.h"#include "kis_external_layer_iface.h"#include "kis_paint_layer.h"@@ -858,6 +861,121 @@ void KisImageTest::testFlattenLayer()}}+void KisImageTest::testSplitAlpha_data()+{+ QTest::addColumn<bool>("useFrames");++ QTest::addRow("no-frames") << false;+ QTest::addRow("multi-frames") << true;+}+++void KisImageTest::testSplitAlpha()+{+ using TestUtil::checkHole;++ QFETCH(bool, useFrames);++ const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();++ const QRect imageRect(0,0,100,100);+ const QRect fillRect1(20,20,60,60);++ const KoColor imageColor = KoColor::createTransparent(cs);+ const KoColor fillColor1(Qt::red, cs);+ const KoColor blackColorRGB(Qt::black, cs);+ const KoColor blackColorA = KoColor::createTransparent(KoColorSpaceRegistry::instance()->alpha8());+ const KoColor whiteColorA(Qt::white, KoColorSpaceRegistry::instance()->alpha8());++ TestUtil::MaskParent p(imageRect);++ KisPaintDeviceSP dev = p.layer->paintDevice();++ auto forAllFrames = [&] (auto func) {+ if (!useFrames) {+ func();+ } else {+ int oldTime = 0;+ KisImageAnimationInterface *interface = p.image->animationInterface();++ for (int time = 0; time < 3; time++) {+ interface->saveAndResetCurrentTime(time, &oldTime);+ func();+ interface->restoreCurrentTime(&oldTime);+ }+ }+ };++ if (useFrames) {+ p.layer->enableAnimation();+ KisKeyframeChannel *channel = p.layer->getKeyframeChannel(KisKeyframeChannel::Raster.id(), true);+ for (int time = 0; time < 3; time++) {+ channel->addKeyframe(time);+ }+ }++ forAllFrames([&] () {+ KisTransaction t(dev);+ dev->fill(fillRect1, fillColor1);+ t.commit(p.image->undoAdapter());+ });++ forAllFrames([&] () {+ QVERIFY(checkHole(dev, imageRect, imageColor, fillRect1, fillColor1));+ });++ KisLayerUtils::splitAlphaToMask(p.image, p.layer, "mask");+ p.image->waitForDone();++ KisMaskSP mask = dynamic_cast<KisMask*>(p.layer->firstChild().data());+ QVERIFY(mask);+ KisPaintDeviceSP maskDev = mask->paintDevice();+ QVERIFY(maskDev);++ if (useFrames) {+ QCOMPARE(maskDev->framesInterface()->frames().size(), 3);+ }++ forAllFrames([&] () {+ QVERIFY(checkHole(dev, imageRect, blackColorRGB, fillRect1, fillColor1));+ QVERIFY(checkHole(maskDev, imageRect, blackColorA, fillRect1, whiteColorA));+ });++ p.image->undoAdapter()->undoLastCommand();+ p.image->waitForDone();++ QCOMPARE(p.layer->childCount(), 0);++ forAllFrames([&] () {+ QVERIFY(checkHole(dev, imageRect, imageColor, fillRect1, fillColor1));+ });++ forAllFrames([&] () {+ p.image->undoAdapter()->undoLastCommand();+ p.image->waitForDone();+ QVERIFY(checkHole(dev, imageRect, imageColor, imageRect, imageColor));+ });++ while (p.undoStore->canRedo()) {+ p.undoStore->redo();+ p.image->waitForDone();+ }++ forAllFrames([&] () {+ QVERIFY(checkHole(dev, imageRect, blackColorRGB, fillRect1, fillColor1));+ QVERIFY(checkHole(maskDev, imageRect, blackColorA, fillRect1, whiteColorA));+ });++ while (p.undoStore->canUndo()) {+ p.undoStore->undo();+ p.image->waitForDone();+ }++ forAllFrames([&] () {+ QVERIFY(checkHole(dev, imageRect, imageColor, imageRect, imageColor));+ });+}+#include <kis_meta_data_merge_strategy_registry.h>template<class ContainerTest>diff --git a/libs/image/tests/kis_image_test.h b/libs/image/tests/kis_image_test.hindex afc0ae83af..de77e6fe7a 100644--- a/libs/image/tests/kis_image_test.h+++ b/libs/image/tests/kis_image_test.h@@ -28,6 +28,10 @@ private Q_SLOTS:void testLayerComposition();void testFlattenLayer();++ void testSplitAlpha_data();+ void testSplitAlpha();+void testMergeDown();void testMergeDownDestinationInheritsAlpha();void testMergeDownDestinationCustomCompositeOp();diff --git a/libs/ui/tests/kis_clipboard_test.cpp b/libs/ui/tests/kis_clipboard_test.cppindex b36d3cc6ce..df832b95e8 100644--- a/libs/ui/tests/kis_clipboard_test.cpp+++ b/libs/ui/tests/kis_clipboard_test.cpp@@ -26,43 +26,9 @@#include <testutil.h>-namespace {-bool checkHole(KisPaintDeviceSP dev,- const QRect &totalRect,- const KoColor &bgColor,- const QRect &holeRect,- const KoColor &holeColor)-{- KisSequentialIterator it(dev, totalRect);- while (it.nextPixel()) {- const KoColor &expectedColor = holeRect.contains(it.x(), it.y()) ? holeColor : bgColor;-- KoColor pixel(it.rawDataConst(), dev->colorSpace());-- if (pixel != expectedColor) {- qDebug() << "Failed to compare pixel at" << it.x() << it.y();- qDebug() << " " << ppVar(pixel);- qDebug() << " " << ppVar(expectedColor);- qDebug() << " " << ppVar(totalRect);- qDebug() << " " << ppVar(holeRect);- return false;- }- }-- return true;-}+using TestUtil::checkHole;-bool checkHole(const QImage &image,- const QRect &totalRect,- const KoColor &bgColor,- const QRect &holeRect,- const KoColor &holeColor)-{- const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();- KisPaintDeviceSP dev = new KisPaintDevice(cs);- dev->convertFromQImage(image, nullptr);- return checkHole(dev, totalRect, bgColor, holeRect, holeColor);-}+namespace {struct TestImageWrapper{diff --git a/sdk/tests/testutil.h b/sdk/tests/testutil.hindex 705b41ae7f..64403a3744 100644--- a/sdk/tests/testutil.h+++ b/sdk/tests/testutil.h@@ -15,6 +15,7 @@#include <KoResource.h>#include <KoTestConfig.h>+#include <KoColor.h>#include <KoColorSpace.h>#include <KoColorSpaceRegistry.h>#include <KoColorProfile.h>@@ -314,6 +315,43 @@ inline bool checkAlphaDeviceFilledWithPixel(KisPaintDeviceSP dev, const QRect &rreturn true;}+inline bool checkHole(KisPaintDeviceSP dev,+ const QRect &totalRect,+ const KoColor &bgColor,+ const QRect &holeRect,+ const KoColor &holeColor)+{+ KisSequentialConstIterator it(dev, totalRect);+ while (it.nextPixel()) {+ const KoColor &expectedColor = holeRect.contains(it.x(), it.y()) ? holeColor : bgColor;++ KoColor pixel(it.rawDataConst(), dev->colorSpace());++ if (pixel != expectedColor) {+ qDebug() << "Failed to compare pixel at" << it.x() << it.y();+ qDebug() << " " << ppVar(pixel);+ qDebug() << " " << ppVar(expectedColor);+ qDebug() << " " << ppVar(totalRect);+ qDebug() << " " << ppVar(holeRect);+ return false;+ }+ }++ return true;+}++inline bool checkHole(const QImage &image,+ const QRect &totalRect,+ const KoColor &bgColor,+ const QRect &holeRect,+ const KoColor &holeColor)+{+ const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();+ KisPaintDeviceSP dev = new KisPaintDevice(cs);+ dev->convertFromQImage(image, nullptr);+ return checkHole(dev, totalRect, bgColor, holeRect, holeColor);+}+class TestNode : public DefaultNode{Q_OBJECT