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

Fix "Copy into new Layer" action when working with vector layers

Dmitry Kazakov75b20e7594c4
diff --git a/krita/krita.action b/krita/krita.action
index f088b3fdae..f0c13796fa 100644
--- a/krita/krita.action
+++ b/krita/krita.action
@@ -2833,7 +2833,7 @@
<whatsThis/>
<toolTip>Cut Selection to New Layer</toolTip>
<iconText>Cut Selection to New Layer</iconText>
- <activationFlags>100000000</activationFlags>
+ <activationFlags>11000000000</activationFlags>
<activationConditions>1</activationConditions>
<shortcut>Ctrl+Shift+J</shortcut>
<isCheckable>false</isCheckable>
@@ -2845,7 +2845,7 @@
<whatsThis/>
<toolTip>Copy Selection to New Layer</toolTip>
<iconText>Copy Selection to New Layer</iconText>
- <activationFlags>100000000</activationFlags>
+ <activationFlags>11000000000</activationFlags>
<activationConditions>0</activationConditions>
<shortcut>Ctrl+Alt+J</shortcut>
<isCheckable>false</isCheckable>
diff --git a/libs/flake/KoShapeController.cpp b/libs/flake/KoShapeController.cpp
index 1a22a28761..3e72cfbaad 100644
--- a/libs/flake/KoShapeController.cpp
+++ b/libs/flake/KoShapeController.cpp
@@ -93,7 +93,7 @@ public:
if (!parentShape) {
resultCommand = new KUndo2Command(parent);
- parentShape = shapeController->createParentForShapes(shapes, resultCommand);
+ parentShape = shapeController->createParentForShapes(shapes, false, resultCommand);
KUndo2Command *addShapeCommand = new KoShapeCreateCommand(shapeController, shapes, parentShape, resultCommand);
resultCommand->setText(addShapeCommand->text());
} else {
diff --git a/libs/flake/KoShapeControllerBase.cpp b/libs/flake/KoShapeControllerBase.cpp
index 9342f15ff6..320d72d824 100644
--- a/libs/flake/KoShapeControllerBase.cpp
+++ b/libs/flake/KoShapeControllerBase.cpp
@@ -55,9 +55,10 @@ KoShapeControllerBase::~KoShapeControllerBase()
delete d;
}
-KoShapeContainer* KoShapeControllerBase::createParentForShapes(const QList<KoShape*> shapes, KUndo2Command *parentCommand)
+KoShapeContainer* KoShapeControllerBase::createParentForShapes(const QList<KoShape*> shapes, bool forceNewLayer, KUndo2Command *parentCommand)
{
Q_UNUSED(parentCommand);
+ Q_UNUSED(forceNewLayer);
Q_UNUSED(shapes);
return 0;
diff --git a/libs/flake/KoShapeControllerBase.h b/libs/flake/KoShapeControllerBase.h
index 06e2f0bda5..58235ef53b 100644
--- a/libs/flake/KoShapeControllerBase.h
+++ b/libs/flake/KoShapeControllerBase.h
@@ -43,7 +43,7 @@ public:
* proposed parent will be added to the document (if
* new layer should be created)
*/
- virtual KoShapeContainer* createParentForShapes(const QList<KoShape*> shapes, KUndo2Command *parentCommand);
+ virtual KoShapeContainer* createParentForShapes(const QList<KoShape*> shapes, bool forceNewLayer, KUndo2Command *parentCommand);
/**
* Return a pointer to the resource manager associated with the
diff --git a/libs/ui/actions/KisPasteActionFactories.cpp b/libs/ui/actions/KisPasteActionFactories.cpp
index 0659be2596..d5d8ed7e2e 100644
--- a/libs/ui/actions/KisPasteActionFactories.cpp
+++ b/libs/ui/actions/KisPasteActionFactories.cpp
@@ -28,6 +28,7 @@
#include <KoDocumentInfo.h>
#include <KoSvgPaste.h>
#include <KoShapeController.h>
+#include <KoShapeControllerBase.h>
#include <KoShapeManager.h>
#include <KoSelection.h>
#include <KoSelectedShapesProxy.h>
@@ -49,7 +50,6 @@
#include <QApplication>
#include <QClipboard>
-
namespace {
QPointF getFittingOffset(QList<KoShape*> shapes,
const QPointF &shapesOffset,
@@ -75,7 +75,7 @@ QPointF getFittingOffset(QList<KoShape*> shapes,
return accumulatedFitOffset;
}
-bool tryPasteShapes(bool pasteAtCursorPosition, KisViewManager *view)
+bool tryPasteShapes(KisPasteActionFactory::Flags flags, KisViewManager *view)
{
bool result = false;
@@ -118,17 +118,25 @@ bool tryPasteShapes(bool pasteAtCursorPosition, KisViewManager *view)
}
KUndo2Command *parentCommand = new KUndo2Command(kundo2_i18n("Paste shapes"));
- canvas->shapeController()->addShapesDirect(shapes, 0, parentCommand);
+
+ const bool forceCreateNewLayer = flags.testFlag(KisPasteActionFactory::ForceNewLayer);
+ KoShapeContainer *parentLayer = nullptr;
+
+ if (forceCreateNewLayer) {
+ parentLayer = canvas->shapeController()->documentBase()->createParentForShapes(shapes, true, parentCommand);
+ }
+
+ canvas->shapeController()->addShapesDirect(shapes, parentLayer, parentCommand);
QPointF finalShapesOffset;
- if (pasteAtCursorPosition) {
+ if (flags.testFlag(KisPasteActionFactory::PasteAtCursor)) {
QRectF boundingRect = KoShape::boundingRect(shapes);
const QPointF cursorPos = canvas->canvasController()->currentCursorPosition();
finalShapesOffset = cursorPos - boundingRect.center();
- } else {
+ } else if (!forceCreateNewLayer) {
bool foundOverlapping = false;
QRectF boundingRect = KoShape::boundingRect(shapes);
@@ -174,7 +182,10 @@ bool tryPasteShapes(bool pasteAtCursorPosition, KisViewManager *view)
}
const QRectF documentRect = canvas->shapeController()->documentRect();
- finalShapesOffset += getFittingOffset(shapes, finalShapesOffset, documentRect, 0.1);
+
+ if (!forceCreateNewLayer) {
+ finalShapesOffset += getFittingOffset(shapes, finalShapesOffset, documentRect, 0.1);
+ }
if (!finalShapesOffset.isNull()) {
new KoShapeMoveCommand(shapes, finalShapesOffset, parentCommand);
@@ -195,8 +206,10 @@ bool tryPasteShapes(bool pasteAtCursorPosition, KisViewManager *view)
}
-void KisPasteActionFactory::run(bool pasteAtCursorPosition, KisViewManager *view)
+void KisPasteActionFactory::run(Flags flags, KisViewManager *view)
{
+ bool pasteAtCursorPosition = flags.testFlag(PasteAtCursor);
+
KisImageSP image = view->image();
if (!image) return;
@@ -209,7 +222,7 @@ void KisPasteActionFactory::run(bool pasteAtCursorPosition, KisViewManager *view
}
// Paste shapes
- if (tryPasteShapes(pasteAtCursorPosition, view)) {
+ if (tryPasteShapes(flags, view)) {
return;
}
diff --git a/libs/ui/actions/KisPasteActionFactories.h b/libs/ui/actions/KisPasteActionFactories.h
index 816e0fba36..1fd8543a18 100644
--- a/libs/ui/actions/KisPasteActionFactories.h
+++ b/libs/ui/actions/KisPasteActionFactories.h
@@ -12,15 +12,27 @@
#include "operations/kis_operation_configuration.h"
struct KRITAUI_EXPORT KisPasteActionFactory : public KisOperation {
+ enum Flag {
+ None = 0x0,
+ PasteAtCursor = 0x1,
+ ForceNewLayer = 0x2
+ };
+ Q_DECLARE_FLAGS(Flags, Flag)
+
KisPasteActionFactory() : KisOperation("paste-ui-action") {}
void runFromXML(KisViewManager *view, const KisOperationConfiguration &config) override {
- run(config.getBool("paste-at-cursor-position", false), view);
+ Flags flags;
+ flags.setFlag(PasteAtCursor, config.getBool("paste-at-cursor-position", false));
+ flags.setFlag(ForceNewLayer, config.getBool("force-new-layer", false));
+ run(flags, view);
}
- void run(bool pasteAtCursorPosition, KisViewManager *view);
+ void run(Flags flags, KisViewManager *view);
};
+Q_DECLARE_OPERATORS_FOR_FLAGS(KisPasteActionFactory::Flags)
+
struct KRITAUI_EXPORT KisPasteIntoActionFactory : public KisNoParameterActionFactory {
KisPasteIntoActionFactory() : KisNoParameterActionFactory("paste-into-ui-action") {}
void run(KisViewManager *view) override;
diff --git a/libs/ui/actions/kis_selection_action_factories.cpp b/libs/ui/actions/kis_selection_action_factories.cpp
index 54b801b0de..bc4ea3d1c8 100644
--- a/libs/ui/actions/kis_selection_action_factories.cpp
+++ b/libs/ui/actions/kis_selection_action_factories.cpp
@@ -297,7 +297,7 @@ void KisImageResizeToSelectionActionFactory::run(KisViewManager *view)
view->image()->cropImage(selection->selectedExactRect());
}
-void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManager *view)
+void KisCutCopyActionFactory::run(Flags flags, KisViewManager *view)
{
KisImageSP image = view->image();
if (!image) return;
@@ -310,12 +310,15 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
view->canvasBase()->toolProxy()->hasSelection();
const bool haveShapesSelected =
- view->selectionManager()->haveShapesSelected();
+ view->selectionManager()->haveShapesSelected();
- KisNodeSP node = view->activeNode();
KisSelectionSP selection = view->selection();
- if (!makeSharpClip && (haveShapesSelected || currentToolHasSelection)) {
+ const bool skipShapes = selection && !currentToolHasSelection;
+
+ if (!skipShapes && !flags.testFlag(SharpClip) && (haveShapesSelected || currentToolHasSelection)) {
+ // XXX: "Add saving of XML data for Cut/Copy of shapes"
+
/**
* Make sure that we use tryBarrierLock() here becasue it does **not**
* cause requestStrokeEnd() to be called in the tools, hence does not
@@ -324,8 +327,7 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
KisImageBarrierLock lock(image, std::try_to_lock);
if (!lock.owns_lock()) return;
- // XXX: "Add saving of XML data for Cut/Copy of shapes"
- if (willCut) {
+ if (flags & CutClip) {
view->canvasBase()->toolProxy()->cut();
} else {
view->canvasBase()->toolProxy()->copy();
@@ -356,7 +358,7 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
}
Q_FOREACH (KisNodeSP node, nodes) {
- KisLayerUtils::recursiveApplyNodes(node, [image, view, makeSharpClip] (KisNodeSP node) {
+ KisLayerUtils::recursiveApplyNodes(node, [image, view, flags] (KisNodeSP node) {
if (node && node->paintDevice()) {
node->paintDevice()->burnKeyframe();
}
@@ -370,7 +372,7 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
}
if (node && node->paintDevice() && !node->inherits("KisMask")) {
- ActionHelper::trimDevice(view, node->paintDevice(), makeSharpClip, range);
+ ActionHelper::trimDevice(view, node->paintDevice(), flags.testFlag(SharpClip), range);
}
});
}
@@ -379,12 +381,12 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
KisClipboard::instance()->setLayers(nodes, tempImage);
- KUndo2MagicString actionName = willCut ?
+ KUndo2MagicString actionName = flags & CutClip ?
kundo2_i18n("Cut") :
kundo2_i18n("Copy");
KisProcessingApplicator *ap = beginAction(view, actionName);
- if (willCut) {
+ if (flags & CutClip) {
selectedNodes.append(masks);
Q_FOREACH (KisNodeSP node, selectedNodes) {
KisLayerUtils::recursiveApplyNodes(node, [selection, masks, ap] (KisNodeSP node){
@@ -438,10 +440,11 @@ void KisCutCopyActionFactory::run(bool willCut, bool makeSharpClip, KisViewManag
}
KisOperationConfiguration config(id());
- config.setProperty("will-cut", willCut);
+ config.setProperty("will-cut", flags.testFlag(CutClip));
+ config.setProperty("use-sharp-clip", flags.testFlag(SharpClip));
endAction(ap, config.toXML());
- } else if (!makeSharpClip) {
- if (willCut) {
+ } else if (!flags.testFlag(SharpClip)) {
+ if (flags & CutClip) {
view->nodeManager()->cutLayersToClipboard();
} else {
view->nodeManager()->copyLayersToClipboard();
diff --git a/libs/ui/actions/kis_selection_action_factories.h b/libs/ui/actions/kis_selection_action_factories.h
index c632851cd1..67da2d5c84 100644
--- a/libs/ui/actions/kis_selection_action_factories.h
+++ b/libs/ui/actions/kis_selection_action_factories.h
@@ -55,14 +55,26 @@ struct KRITAUI_EXPORT KisImageResizeToSelectionActionFactory : public KisNoParam
};
struct KRITAUI_EXPORT KisCutCopyActionFactory : public KisOperation {
+ enum Flag {
+ None = 0x0,
+ CutClip = 0x1,
+ SharpClip = 0x2
+ };
+ Q_DECLARE_FLAGS(Flags, Flag)
+
KisCutCopyActionFactory() : KisOperation("cut-copy-ui-action") {}
void runFromXML(KisViewManager *view, const KisOperationConfiguration &config) override {
- run(config.getBool("will-cut", false), config.getBool("use-sharp-clip", false), view);
+ Flags flags;
+ flags.setFlag(CutClip, config.getBool("will-cut", false));
+ flags.setFlag(SharpClip, config.getBool("use-sharp-clip", false));
+ run(flags, view);
}
- void run(bool willCut, bool makeSharpClip, KisViewManager *view);
+ void run(Flags flags, KisViewManager *view);
};
+Q_DECLARE_OPERATORS_FOR_FLAGS(KisCutCopyActionFactory::Flags)
+
struct KRITAUI_EXPORT KisCopyMergedActionFactory : public KisNoParameterActionFactory {
KisCopyMergedActionFactory() : KisNoParameterActionFactory("copy-merged-ui-action") {}
void run(KisViewManager *view) override;
diff --git a/libs/ui/flake/kis_shape_controller.cpp b/libs/ui/flake/kis_shape_controller.cpp
index 821c7a25be..d5b61597a1 100644
--- a/libs/ui/flake/kis_shape_controller.cpp
+++ b/libs/ui/flake/kis_shape_controller.cpp
@@ -150,7 +150,7 @@ static inline bool belongsToShapeSelection(KoShape* shape) {
return dynamic_cast<KisShapeSelectionMarker*>(shape->userData());
}
-KoShapeContainer *KisShapeController::createParentForShapes(const QList<KoShape *> shapes, KUndo2Command *parentCommand)
+KoShapeContainer *KisShapeController::createParentForShapes(const QList<KoShape *> shapes, bool forceNewLayer, KUndo2Command *parentCommand)
{
KoShapeContainer *resultParent = 0;
KisCommandUtils::CompositeCommand *resultCommand =
@@ -191,7 +191,7 @@ KoShapeContainer *KisShapeController::createParentForShapes(const QList<KoShape
dynamic_cast<KisShapeLayer*>(
canvas->selectedShapesProxy()->selection()->activeLayer());
- if (!shapeLayer) {
+ if (!shapeLayer || forceNewLayer) {
shapeLayer = new KisShapeLayer(this, image(),
i18n("Vector Layer %1", m_d->nameServer->number()),
OPACITY_OPAQUE_U8);
diff --git a/libs/ui/flake/kis_shape_controller.h b/libs/ui/flake/kis_shape_controller.h
index 938c91902a..c111767635 100644
--- a/libs/ui/flake/kis_shape_controller.h
+++ b/libs/ui/flake/kis_shape_controller.h
@@ -76,7 +76,7 @@ Q_SIGNALS:
void currentLayerChanged(const KoShapeLayer*);
public:
- KoShapeContainer* createParentForShapes(const QList<KoShape*> shapes, KUndo2Command *parentCommand) override;
+ KoShapeContainer *createParentForShapes(const QList<KoShape *> shapes, bool forceNewLayer, KUndo2Command *parentCommand) override;
QRectF documentRectInPixels() const override;
qreal pixelsPerInch() const override;
diff --git a/libs/ui/kis_selection_manager.cc b/libs/ui/kis_selection_manager.cc
index d85377a332..0e06235705 100644
--- a/libs/ui/kis_selection_manager.cc
+++ b/libs/ui/kis_selection_manager.cc
@@ -347,25 +347,25 @@ void KisSelectionManager::selectionChanged()
void KisSelectionManager::cut()
{
KisCutCopyActionFactory factory;
- factory.run(true, false, m_view);
+ factory.run(KisCutCopyActionFactory::CutClip, m_view);
}
void KisSelectionManager::copy()
{
KisCutCopyActionFactory factory;
- factory.run(false, false, m_view);
+ factory.run(KisCutCopyActionFactory::None, m_view);
}
void KisSelectionManager::cutSharp()
{
KisCutCopyActionFactory factory;
- factory.run(true, true, m_view);
+ factory.run(KisCutCopyActionFactory::CutClip | KisCutCopyActionFactory::SharpClip, m_view);
}
void KisSelectionManager::copySharp()
{
KisCutCopyActionFactory factory;
- factory.run(false, true, m_view);
+ factory.run(KisCutCopyActionFactory::SharpClip, m_view);
}
void KisSelectionManager::copyMerged()
@@ -377,13 +377,13 @@ void KisSelectionManager::copyMerged()
void KisSelectionManager::paste()
{
KisPasteActionFactory factory;
- factory.run(false, m_view);
+ factory.run(KisPasteActionFactory::None, m_view);
}
void KisSelectionManager::pasteAt()
{
KisPasteActionFactory factory;
- factory.run(true, m_view);
+ factory.run(KisPasteActionFactory::PasteAtCursor, m_view);
}
void KisSelectionManager::pasteInto()
@@ -547,13 +547,15 @@ void KisSelectionManager::fillPatternOpacity()
void KisSelectionManager::copySelectionToNewLayer()
{
copy();
- paste();
+ KisPasteActionFactory factory;
+ factory.run(KisPasteActionFactory::ForceNewLayer, m_view);
}
void KisSelectionManager::cutToNewLayer()
{
cut();
- paste();
+ KisPasteActionFactory factory;
+ factory.run(KisPasteActionFactory::ForceNewLayer, m_view);
}
void KisSelectionManager::toggleDisplaySelection()