diff --git a/plugins/tools/tool_smart_patch/kis_tool_smart_patch.cpp b/plugins/tools/tool_smart_patch/kis_tool_smart_patch.cpp index 232a5345b8..4573bc783b 100644 --- a/plugins/tools/tool_smart_patch/kis_tool_smart_patch.cpp +++ b/plugins/tools/tool_smart_patch/kis_tool_smart_patch.cpp @@ -1,210 +1,212 @@ /* * Copyright (c) 2017 Eugene Ingerman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kis_tool_smart_patch.h" #include #include #include #include #include #include #include #include "kis_canvas2.h" #include "kis_cursor.h" #include "kis_config.h" #include "kundo2magicstring.h" #include "KoProperties.h" #include "KoColorSpaceRegistry.h" #include "kis_node_manager.h" #include "kis_tool_smart_patch_options_widget.h" #include "libs/image/kis_paint_device_debug_utils.h" #include "kis_resources_snapshot.h" +#include "kis_layer.h" +#include "kis_inpaint_mask.h" void patchImage(KisPaintDeviceSP imageDev, KisPaintDeviceSP maskDev, int radius, int accuracy); struct KisToolSmartPatch::Private { + KisMaskSP mask = nullptr; KisNodeSP maskNode = nullptr; KisNodeSP paintNode = nullptr; KisPaintDeviceSP imageDev = nullptr; KisPaintDeviceSP maskDev = nullptr; KisResourcesSnapshotSP resources = nullptr; KoColor currentFgColor; KisToolSmartPatchOptionsWidget *optionsWidget = nullptr; }; KisToolSmartPatch::KisToolSmartPatch(KoCanvasBase * canvas) : KisToolFreehand(canvas, KisCursor::load("tool_freehand_cursor.png", 5, 5), kundo2_i18n("Smart Patch Stroke")), m_d(new Private) { setObjectName("tool_SmartPatch"); } KisToolSmartPatch::~KisToolSmartPatch() { m_d->optionsWidget = nullptr; } void KisToolSmartPatch::activate(ToolActivation activation, const QSet &shapes) { KisToolFreehand::activate(activation, shapes); } void KisToolSmartPatch::deactivate() { - KisToolFreehand::deactivate(); } void KisToolSmartPatch::resetCursorStyle() { KisToolFreehand::resetCursorStyle(); } bool KisToolSmartPatch::canCreateInpaintMask() const { KisNodeSP node = currentNode(); return node && node->inherits("KisLayer"); } void KisToolSmartPatch::inpaintImage(KisPaintDeviceSP maskDev, KisPaintDeviceSP imageDev) { int accuracy = 0; int patchRadius = 2; - if( !m_d.isNull() && m_d->optionsWidget ){ accuracy = m_d->optionsWidget->getAccuracy(); patchRadius = m_d->optionsWidget->getPatchRadius(); } patchImage( imageDev, maskDev, patchRadius, accuracy ); } void KisToolSmartPatch::activatePrimaryAction() { KisToolFreehand::activatePrimaryAction(); } void KisToolSmartPatch::deactivatePrimaryAction() { KisToolFreehand::deactivatePrimaryAction(); } -void KisToolSmartPatch::beginPrimaryAction(KoPointerEvent *event) +void KisToolSmartPatch::createInpaintMask( void ) +{ + m_d->mask = new KisInpaintMask(); + KisLayerSP parentLayer = qobject_cast(m_d->paintNode.data()); + m_d->mask->initSelection(parentLayer); + image()->addNode( m_d->mask, m_d->paintNode ); +} + +void KisToolSmartPatch::deleteInpaintMask( void ) { - qDebug() << __FUNCTION__ << " 0"; + KisLayerSP inpaintMaskLayer = qobject_cast( m_d->mask ); + image()->removeNode(inpaintMaskLayer); + + KisCanvas2 * kiscanvas = static_cast(canvas()); + KisViewManager* viewManager = kiscanvas->viewManager(); + + if( ! m_d->paintNode.isNull() ) + viewManager->nodeManager()->slotNonUiActivatedNode( m_d->paintNode ); + + m_d->paintNode = nullptr; + m_d->mask = nullptr; +} +void KisToolSmartPatch::beginPrimaryAction(KoPointerEvent *event) +{ KisNodeSP node = currentNode(); if (!node) return; KisCanvas2 * kiscanvas = static_cast(canvas()); KisViewManager* viewManager = kiscanvas->viewManager(); - if (!m_d->maskNode.isNull()) { - viewManager->nodeManager()->slotNonUiActivatedNode(m_d->maskNode); - qDebug() << __FUNCTION__ << " 1"; + if (!m_d->mask.isNull()) { + viewManager->nodeManager()->slotNonUiActivatedNode(m_d->mask); } else { - m_d->paintNode = viewManager->nodeManager()->activeNode(); m_d->imageDev = m_d->paintNode->paintDevice(); - viewManager->nodeManager()->createNode("KisInpaintMask", true); - m_d->maskNode = currentNode(); - if ( ! m_d->maskNode.isNull() ){ - m_d->maskNode->setProperty("visible", false); - m_d->maskNode->setProperty("temporary", true); - m_d->maskNode->setProperty("inpaintmask", true); + createInpaintMask(); + viewManager->nodeManager()->slotNonUiActivatedNode(m_d->mask); + + if ( ! m_d->mask.isNull() ){ + m_d->mask->setProperty("visible", false); + m_d->mask->setProperty("temporary", true); + m_d->mask->setProperty("inpaintmask", true); } - viewManager->nodeManager()->slotNonUiActivatedNode(m_d->maskNode); - qDebug() << __FUNCTION__ << " 2"; KisToolFreehand::beginPrimaryAction(event); - qDebug() << __FUNCTION__ << " 3"; - m_d->currentFgColor = canvas()->resourceManager()->foregroundColor(); //resource(KoCanvasResourceManager::ForegroundColor).value(); - canvas()->resourceManager()->setForegroundColor(KoColor(Qt::black, KoColorSpaceRegistry::instance()->alpha8())); //maybe we should use alpha color space here + m_d->currentFgColor = canvas()->resourceManager()->foregroundColor(); + canvas()->resourceManager()->setForegroundColor(KoColor(Qt::black, KoColorSpaceRegistry::instance()->alpha8())); } } void KisToolSmartPatch::continuePrimaryAction(KoPointerEvent *event) { KisToolFreehand::continuePrimaryAction(event); } void KisToolSmartPatch::endPrimaryAction(KoPointerEvent *event) { - qDebug() << __FUNCTION__ << " 0"; if( mode() != KisTool::PAINT_MODE ) return; KisToolFreehand::endPrimaryAction(event); - - qDebug() << __FUNCTION__ << " 1"; - //Next line is important. We need to wait for the paint operation to finish otherwise //mask will be incomplete. image()->waitForDone(); m_d->maskDev = new KisPaintDevice(KoColorSpaceRegistry::instance()->alpha8()); m_d->maskDev->makeCloneFrom(currentNode()->paintDevice(), currentNode()->paintDevice()->extent()); - inpaintImage( m_d->maskDev, m_d->imageDev ); +// KIS_DUMP_DEVICE_2(m_d->imageDev, m_d->imageDev->extent(), "patched", "/home/eugening/Projects/Out"); +// KIS_DUMP_DEVICE_2(m_d->maskDev, m_d->imageDev->extent(), "output", "/home/eugening/Projects/Out"); - KIS_DUMP_DEVICE_2(m_d->imageDev, m_d->imageDev->extent(), "patched", "/home/eugening/Projects/Out"); - KIS_DUMP_DEVICE_2(m_d->maskDev, m_d->imageDev->extent(), "output", "/home/eugening/Projects/Out"); - - KisCanvas2 * kiscanvas = static_cast(canvas()); - KisViewManager* viewManager = kiscanvas->viewManager(); - if( ! m_d->maskNode.isNull() ) - viewManager->nodeManager()->removeSingleNode(m_d->maskNode); - if( ! m_d->paintNode.isNull() ) - viewManager->nodeManager()->slotNonUiActivatedNode( m_d->paintNode ); - m_d->paintNode = nullptr; - m_d->maskNode = nullptr; - + deleteInpaintMask(); canvas()->resourceManager()->setForegroundColor(m_d->currentFgColor); - - qDebug() << __FUNCTION__ << " 3"; } QWidget * KisToolSmartPatch::createOptionWidget() { KisCanvas2 * kiscanvas = dynamic_cast(canvas()); m_d->optionsWidget = new KisToolSmartPatchOptionsWidget(kiscanvas->viewManager()->resourceProvider(), 0); m_d->optionsWidget->setObjectName(toolId() + "option widget"); return m_d->optionsWidget; } diff --git a/plugins/tools/tool_smart_patch/kis_tool_smart_patch.h b/plugins/tools/tool_smart_patch/kis_tool_smart_patch.h index 1cc5386a02..9fbf825cfd 100644 --- a/plugins/tools/tool_smart_patch/kis_tool_smart_patch.h +++ b/plugins/tools/tool_smart_patch/kis_tool_smart_patch.h @@ -1,100 +1,102 @@ /* * Copyright (c) 2017 Eugene Ingerman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KIS_TOOL_SMART_PATCH_H_ #define KIS_TOOL_SMART_PATCH_H_ #include #include "kis_tool_freehand.h" #include "KoToolFactoryBase.h" #include #include #include #include #include #include class KActionCollection; class KoCanvasBase; class KisToolSmartPatch : public KisToolFreehand { Q_OBJECT public: KisToolSmartPatch(KoCanvasBase * canvas); virtual ~KisToolSmartPatch(); QWidget * createOptionWidget(); void activatePrimaryAction(); void deactivatePrimaryAction(); void beginPrimaryAction(KoPointerEvent *event); void continuePrimaryAction(KoPointerEvent *event); void endPrimaryAction(KoPointerEvent *event); protected Q_SLOTS: void resetCursorStyle(); public Q_SLOTS: virtual void activate(ToolActivation toolActivation, const QSet &shapes); void deactivate(); Q_SIGNALS: private: bool canCreateInpaintMask() const; void inpaintImage( KisPaintDeviceSP maskDev, KisPaintDeviceSP imageDev ); private: struct Private; const QScopedPointer m_d; + void createInpaintMask(); + void deleteInpaintMask(); }; class KisToolSmartPatchFactory : public KoToolFactoryBase { public: KisToolSmartPatchFactory() : KoToolFactoryBase("KritaShape/KisToolSmartPatch") { setToolTip(i18n("Smart Patch Tool")); // Temporarily setSection(TOOL_TYPE_FILL); setIconName(koIconNameCStr("krita_tool_smart_patch")); //setShortcut(QKeySequence(Qt::Key_Shift + Qt::Key_B)); setPriority(4); setActivationShapeId(KRITA_TOOL_ACTIVATION_ID); } virtual ~KisToolSmartPatchFactory() {} virtual KoToolBase * createTool(KoCanvasBase *canvas) { return new KisToolSmartPatch(canvas); } }; #endif // KIS_TOOL_SMART_PATCH_H_