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 a crash when converting the only layer of the group into a selection mask
Dmitry Kazakovbee001fd20ef
diff --git a/libs/image/kis_default_bounds.cpp b/libs/image/kis_default_bounds.cppindex 7aad60d10a..a096ff45ae 100644--- a/libs/image/kis_default_bounds.cpp+++ b/libs/image/kis_default_bounds.cpp@@ -83,70 +83,111 @@ void *KisDefaultBounds::sourceCookie() const}/******************************************************************/-/* KisSelectionDefaultBounds */+/* KisSelectionDefaultBoundsBase *//******************************************************************/-struct Q_DECL_HIDDEN KisSelectionDefaultBounds::Private+KisSelectionDefaultBoundsBase::KisSelectionDefaultBoundsBase(){- KisPaintDeviceWSP parentDevice;-};+}-KisSelectionDefaultBounds::KisSelectionDefaultBounds(KisPaintDeviceSP parentDevice)- : m_d(new Private())+KisSelectionDefaultBoundsBase::~KisSelectionDefaultBoundsBase(){- m_d->parentDevice = parentDevice;}-KisSelectionDefaultBounds::~KisSelectionDefaultBounds()+QRect KisSelectionDefaultBoundsBase::bounds() const{- delete m_d;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ?+ parentDevice->extent() | parentDevice->defaultBounds()->bounds() : QRect();}-QRect KisSelectionDefaultBounds::bounds() const+QRect KisSelectionDefaultBoundsBase::imageBorderRect() const{- return m_d->parentDevice ?- m_d->parentDevice->extent() | m_d->parentDevice->defaultBounds()->bounds() : QRect();+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->bounds() : QRect();}-QRect KisSelectionDefaultBounds::imageBorderRect() const+bool KisSelectionDefaultBoundsBase::wrapAroundMode() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->bounds() : QRect();+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->wrapAroundMode() : false;}-bool KisSelectionDefaultBounds::wrapAroundMode() const+WrapAroundAxis KisSelectionDefaultBoundsBase::wrapAroundModeAxis() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->wrapAroundMode() : false;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->wrapAroundModeAxis() : WRAPAROUND_BOTH;}-WrapAroundAxis KisSelectionDefaultBounds::wrapAroundModeAxis() const+int KisSelectionDefaultBoundsBase::currentLevelOfDetail() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->wrapAroundModeAxis() : WRAPAROUND_BOTH;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->currentLevelOfDetail() : 0;}-int KisSelectionDefaultBounds::currentLevelOfDetail() const+int KisSelectionDefaultBoundsBase::currentTime() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->currentLevelOfDetail() : 0;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->currentTime() : 0;}-int KisSelectionDefaultBounds::currentTime() const+bool KisSelectionDefaultBoundsBase::externalFrameActive() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->currentTime() : 0;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice ? parentDevice->defaultBounds()->externalFrameActive() : false;}-bool KisSelectionDefaultBounds::externalFrameActive() const+void *KisSelectionDefaultBoundsBase::sourceCookie() const{- return m_d->parentDevice ?- m_d->parentDevice->defaultBounds()->externalFrameActive() : false;+ KisPaintDeviceSP parentDevice = this->parentPaintDevice();++ return parentDevice.data();}-void *KisSelectionDefaultBounds::sourceCookie() const+/******************************************************************/+/* KisSelectionDefaultBounds */+/******************************************************************/++KisSelectionDefaultBounds::KisSelectionDefaultBounds(KisPaintDeviceSP parentPaintDevice)+ : m_paintDevice(parentPaintDevice){- return m_d->parentDevice.data();+}++KisSelectionDefaultBounds::~KisSelectionDefaultBounds()+{+}++KisPaintDeviceSP KisSelectionDefaultBounds::parentPaintDevice() const+{+ return m_paintDevice;+}++/******************************************************************/+/* KisMaskDefaultBounds */+/******************************************************************/+++KisMaskDefaultBounds::KisMaskDefaultBounds(KisNodeSP parentNode)+ : m_parentNode(parentNode)+{+}++KisMaskDefaultBounds::~KisMaskDefaultBounds()+{+}++KisPaintDeviceSP KisMaskDefaultBounds::parentPaintDevice() const+{+ KisNodeSP parentNode = m_parentNode;++ return parentNode ? parentNode->original() : nullptr;}/******************************************************************/diff --git a/libs/image/kis_default_bounds.h b/libs/image/kis_default_bounds.hindex 1c9bdac57b..ca030e3695 100644--- a/libs/image/kis_default_bounds.h+++ b/libs/image/kis_default_bounds.h@@ -46,11 +46,21 @@ private:Private * const m_d;};-class KRITAIMAGE_EXPORT KisSelectionDefaultBounds : public KisDefaultBoundsBase+/**+ * For a selection, the default bounds are defined not by the image, but+ * by the device the selection is attached to, i.e. the selection with+ * non-transparent default pixel should cover the entire parent paint+ * device, not just the image bounds.+ *+ * KisSelectionDefaultBoundsBase is a base class for selection-targeted+ * default bounds objects. The descendants can redefine how exactly "the+ * parent paint device" is fetched.+ */+class KRITAIMAGE_EXPORT KisSelectionDefaultBoundsBase : public KisDefaultBoundsBase{public:- KisSelectionDefaultBounds(KisPaintDeviceSP parentPaintDevice);- ~KisSelectionDefaultBounds() override;+ KisSelectionDefaultBoundsBase();+ ~KisSelectionDefaultBoundsBase() override;QRect bounds() const override;QRect imageBorderRect() const override;@@ -61,13 +71,62 @@ public:bool externalFrameActive() const override;void * sourceCookie() const override;+protected:+ /**+ * Return the actual paint device the selection is attached to+ */+ virtual KisPaintDeviceSP parentPaintDevice() const = 0;++private:+ Q_DISABLE_COPY(KisSelectionDefaultBoundsBase)+};++/**+ * KisSelectionDefaultBounds directly attaches a selection to a paint device+ *+ * This object is mostly used in immediate actions, like filter application,+ * where we don't have to store the link to the paint device for a long time.+ */+class KRITAIMAGE_EXPORT KisSelectionDefaultBounds : public KisSelectionDefaultBoundsBase+{+public:+ KisSelectionDefaultBounds(KisPaintDeviceSP parentPaintDevice);+ ~KisSelectionDefaultBounds() override;++protected:+ virtual KisPaintDeviceSP parentPaintDevice() const override;+private:Q_DISABLE_COPY(KisSelectionDefaultBounds)+ KisPaintDeviceWSP m_paintDevice;+};- struct Private;- Private * const m_d;+/**+ * KisMaskDefaultBounds is used to attach a selection of a mask to the origin()+ * of the parent layer. We cannot connect it directly to parent->origin(), because+ * in some cases the origin() of the parent layer may change randomly+ * (e.g. the origin() of a group layer may change due to the oblige child+ * mechanism).+ */+class KRITAIMAGE_EXPORT KisMaskDefaultBounds : public KisSelectionDefaultBoundsBase+{+public:+ KisMaskDefaultBounds(KisNodeSP parentNode);+ ~KisMaskDefaultBounds() override;++protected:+ virtual KisPaintDeviceSP parentPaintDevice() const override;++private:+ Q_DISABLE_COPY(KisMaskDefaultBounds)+ KisNodeWSP m_parentNode;};+/**+ * This is a stub default bounds object that returns null rect+ * as the default bounds (in contrast to the infinite rect+ * returned by the base class).+ */class KRITAIMAGE_EXPORT KisSelectionEmptyBounds : public KisDefaultBounds{public:diff --git a/libs/image/kis_mask.cc b/libs/image/kis_mask.ccindex 5f976bfa8c..e83d83e027 100644--- a/libs/image/kis_mask.cc+++ b/libs/image/kis_mask.cc@@ -105,8 +105,8 @@ void KisMask::setImage(KisImageWSP image)KisPaintDeviceSP parentPaintDevice = parent() ? parent()->original() : 0;KisDefaultBoundsBaseSP defaultBounds;- if (parentPaintDevice) {- defaultBounds = new KisSelectionDefaultBounds(parentPaintDevice);+ if (parent() && parent()->original()) {+ defaultBounds = new KisMaskDefaultBounds(parent());} else {if (image) {qWarning() << "WARNING: KisMask::setImage() was called without any parent layer being set";@@ -180,7 +180,7 @@ void KisMask::Private::initSelectionImpl(KisSelectionSP copyFrom, KisLayerSP par* We can't use setSelection as we may not have parent() yet*/selection = new KisSelection(*copyFrom);- selection->setDefaultBounds(new KisSelectionDefaultBounds(parentPaintDevice));+ selection->setDefaultBounds(new KisMaskDefaultBounds(parentLayer));selection->setResolutionProxy(copyFrom->resolutionProxy()->createOrCloneDetached(image));} else if (copyFromDevice) {KritaUtils::DeviceCopyMode copyMode =@@ -188,7 +188,7 @@ void KisMask::Private::initSelectionImpl(KisSelectionSP copyFrom, KisLayerSP parKritaUtils::CopyAllFrames : KritaUtils::CopySnapshot;selection = new KisSelection(copyFromDevice, copyMode,- new KisSelectionDefaultBounds(parentPaintDevice),+ new KisMaskDefaultBounds(parentLayer),toQShared(new KisImageResolutionProxy(image)));KisPixelSelectionSP pixelSelection = selection->pixelSelection();@@ -200,7 +200,7 @@ void KisMask::Private::initSelectionImpl(KisSelectionSP copyFrom, KisLayerSP parq->enableAnimation();}} else {- selection = new KisSelection(new KisSelectionDefaultBounds(parentPaintDevice),+ selection = new KisSelection(new KisMaskDefaultBounds(parentLayer),toQShared(new KisImageResolutionProxy(image)));selection->pixelSelection()->setDefaultPixel(KoColor(Qt::white, selection->pixelSelection()->colorSpace()));@@ -488,7 +488,7 @@ int KisMask::thumbnailSeqNo() constvoid KisMask::testingInitSelection(const QRect &rect, KisLayerSP parentLayer){if (parentLayer) {- m_d->selection = new KisSelection(new KisSelectionDefaultBounds(parentLayer->paintDevice()), toQShared(new KisImageResolutionProxy(image())));+ m_d->selection = new KisSelection(new KisMaskDefaultBounds(parentLayer), toQShared(new KisImageResolutionProxy(image())));} else {m_d->selection = new KisSelection(new KisSelectionEmptyBounds(), toQShared(new KisImageResolutionProxy(image())));}diff --git a/libs/image/tests/kis_image_test.cpp b/libs/image/tests/kis_image_test.cppindex 08d4c47994..75e03ff16d 100644--- a/libs/image/tests/kis_image_test.cpp+++ b/libs/image/tests/kis_image_test.cpp@@ -1264,7 +1264,7 @@ void KisImageTest::testPaintOverlayMask()layer1->paintDevice()->fill(fillRect, KoColor(Qt::yellow, layer1->colorSpace()));KisSelectionMaskSP mask = new KisSelectionMask(p.image);- KisSelectionSP selection = new KisSelection(new KisSelectionDefaultBounds(layer1->paintDevice()), toQShared(new KisImageResolutionProxy(p.image)));+ KisSelectionSP selection = new KisSelection(new KisMaskDefaultBounds(layer1), toQShared(new KisImageResolutionProxy(p.image)));selection->pixelSelection()->select(selectionRect, 128);selection->pixelSelection()->select(KisAlgebra2D::blowRect(selectionRect,-0.3), 255);