[graphics/krita/rempt/copy_paste_layer_style] /: FEATURE: Add actions to copy/paste the layer style

Halla Rempt null at kde.org
Tue Sep 21 09:25:03 BST 2021


Git commit 9c247308703c1ed5426261ef260fb57149dd72df by Halla Rempt.
Committed on 21/09/2021 at 08:24.
Pushed by rempt into branch 'rempt/copy_paste_layer_style'.

FEATURE: Add actions to copy/paste the layer style

This puts the layerstyle xml on the clipboard, so people can also
put it in a text editor and save it or edit it.

CCMAIL:kimageshop at kde.org

M  +3    -1    krita/krita5.xmlgui
M  +25   -0    krita/kritamenu.action
M  +54   -0    libs/ui/kis_layer_manager.cc
M  +6    -0    libs/ui/kis_layer_manager.h
M  +2    -0    plugins/dockers/layerdocker/LayerBox.cpp

https://invent.kde.org/graphics/krita/commit/9c247308703c1ed5426261ef260fb57149dd72df

diff --git a/krita/krita5.xmlgui b/krita/krita5.xmlgui
index 078687884c..d8ef6c61dc 100644
--- a/krita/krita5.xmlgui
+++ b/krita/krita5.xmlgui
@@ -2,7 +2,7 @@
 <kpartgui xmlns="http://www.kde.org/standards/kxmlgui/1.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 name="Krita"
-version="501"
+version="502"
 xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0  http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd">
   <MenuBar>
     <Menu name="file">
@@ -52,12 +52,14 @@ xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0  http://www.kde.org
       <Action name="cut_sharp"/>
       <Action name="copy_sharp"/>
       <Action name="copy_merged"/>
+      <Action name="copy_layer_style"/>
       <Action name="edit_paste"/>
       <Action name="paste_at"/>
       <Action name="paste_into"/>
       <Action name="paste_new"/>
       <Action name="paste_as_reference"/>
       <Action name="paste_shape_style"/>
+      <Action name="paste_layer_style"/>
       <Action name="clear"/>
       <Action name="fill_selection_foreground_color"/>
       <Action name="fill_selection_background_color"/>
diff --git a/krita/kritamenu.action b/krita/kritamenu.action
index 0199b51e5e..7b398be833 100644
--- a/krita/kritamenu.action
+++ b/krita/kritamenu.action
@@ -344,6 +344,18 @@
       <isCheckable>false</isCheckable>
       <statusTip></statusTip>
     </Action>
+    <Action name="copy_layer_style">
+      <icon></icon>
+      <text>Copy Layer Style</text>
+      <whatsThis></whatsThis>
+      <toolTip>Copy Layer Style from the Current Node</toolTip>
+      <iconText>Copy Layer Style</iconText>
+      <activationFlags>1</activationFlags>
+      <activationConditions>0</activationConditions>
+      <shortcut></shortcut>
+      <isCheckable>false</isCheckable>
+      <statusTip></statusTip>
+    </Action>
     <Action name="edit_paste">
       <icon>edit-paste</icon>
       <text>&Paste</text>
@@ -416,6 +428,19 @@
       <isCheckable>false</isCheckable>
       <statusTip></statusTip>
     </Action>
+    <Action name="paste_layer_style">
+      <icon></icon>
+      <text>Paste Layer Style</text>
+      <whatsThis></whatsThis>
+      <toolTip>Paste Layer Style into the Current Node</toolTip>
+      <iconText>Paste Layer Style</iconText>
+      <activationFlags>1</activationFlags>
+      <activationConditions>0</activationConditions>
+      <shortcut></shortcut>
+      <isCheckable>false</isCheckable>
+      <statusTip></statusTip>
+    </Action>
+
     <Action name="clear">
       <icon>edit-clear</icon>
       <text>C&lear</text>
diff --git a/libs/ui/kis_layer_manager.cc b/libs/ui/kis_layer_manager.cc
index 96c94b339b..999c654176 100644
--- a/libs/ui/kis_layer_manager.cc
+++ b/libs/ui/kis_layer_manager.cc
@@ -15,6 +15,7 @@
 #include <QVBoxLayout>
 #include <QFileInfo>
 #include <QStandardPaths>
+#include <QClipboard>
 
 #include <kactioncollection.h>
 #include <klocalizedstring.h>
@@ -95,6 +96,7 @@
 #include "KisGlobalResourcesInterface.h"
 
 #include "KisSaveGroupVisitor.h"
+#include <kis_asl_layer_style_serializer.h>
 
 KisLayerManager::KisLayerManager(KisViewManager * view)
     : m_view(view)
@@ -166,6 +168,11 @@ void KisLayerManager::setup(KisActionManager* actionManager)
     m_layerStyle  = actionManager->createAction("layer_style");
     connect(m_layerStyle, SIGNAL(triggered()), this, SLOT(layerStyle()));
 
+    m_copyLayerStyle = actionManager->createAction("copy_layer_style");
+    connect(m_copyLayerStyle, SIGNAL(triggered()), this, SLOT(copyLayerStyle()));
+
+    m_pasteLayerStyle = actionManager->createAction("paste_layer_style");
+    connect(m_pasteLayerStyle, SIGNAL(triggered()), this, SLOT(pasteLayerStyle()));
 }
 
 void KisLayerManager::updateGUI()
@@ -367,6 +374,53 @@ void KisLayerManager::changeCloneSource()
     dialog->activateWindow();
 }
 
+void KisLayerManager::copyLayerStyle()
+{
+    KisImageSP image = m_view->image();
+    if (!image) return;
+
+    KisLayerSP layer = activeLayer();
+    if (!layer) return;
+
+    KisPSDLayerStyleSP layerStyle = layer->layerStyle();
+    if (!layerStyle) return;
+
+    KisAslLayerStyleSerializer serializer;
+    serializer.setStyles(QVector<KisPSDLayerStyleSP>() << layerStyle);
+    QString psdxml = serializer.formPsdXmlDocument().toString();
+
+    if (!psdxml.isEmpty()) {
+        QGuiApplication::clipboard()->setText(psdxml);
+    }
+}
+
+void KisLayerManager::pasteLayerStyle()
+{
+    KisImageSP image = m_view->image();
+    if (!image) return;
+
+    KisLayerSP layer = activeLayer();
+    if (!layer) return;
+
+    QString aslXml = QGuiApplication::clipboard()->text();
+    if (aslXml.isEmpty()) return;
+
+    QDomDocument aslDoc;
+    if (!aslDoc.setContent(aslXml)) return;
+
+    KisAslLayerStyleSerializer serializer;
+    serializer.registerPSDPattern(aslDoc);
+    serializer.readFromPSDXML(aslDoc);
+
+    if (serializer.styles().size() != 1) return;
+
+    KisPSDLayerStyleSP newStyle = serializer.styles().first();
+    KUndo2Command *cmd = new KisSetLayerStyleCommand(layer, layer->layerStyle(), newStyle);
+
+    KisProcessingApplicator::runSingleCommandStroke(image, cmd);
+    image->waitForDone();
+}
+
 void KisLayerManager::convertNodeToPaintLayer(KisNodeSP source)
 {
     KisImageWSP image = m_view->image();
diff --git a/libs/ui/kis_layer_manager.h b/libs/ui/kis_layer_manager.h
index bf8e84a4d2..acbf5773c1 100644
--- a/libs/ui/kis_layer_manager.h
+++ b/libs/ui/kis_layer_manager.h
@@ -99,6 +99,10 @@ private Q_SLOTS:
 
     void changeCloneSource();
 
+    void copyLayerStyle();
+
+    void pasteLayerStyle();
+
 private:
     void adjustLayerPosition(KisNodeSP node, KisNodeSP activeNode, KisNodeSP &parent, KisNodeSP &above);
     void addLayerCommon(KisNodeSP activeNode, KisNodeSP layer, bool updateImage = true, KisProcessingApplicator *applicator = 0);
@@ -118,6 +122,8 @@ private:
     KisNodeCommandsAdapter* m_commandsAdapter;
 
     KisAction *m_layerStyle {0};
+    KisAction *m_copyLayerStyle {0};
+    KisAction *m_pasteLayerStyle {0};
 };
 
 #endif
diff --git a/plugins/dockers/layerdocker/LayerBox.cpp b/plugins/dockers/layerdocker/LayerBox.cpp
index f48e105940..b045e8b109 100644
--- a/plugins/dockers/layerdocker/LayerBox.cpp
+++ b/plugins/dockers/layerdocker/LayerBox.cpp
@@ -673,6 +673,8 @@ void LayerBox::slotContextMenuRequested(const QPoint &pos, const QModelIndex &in
 
             if (singleLayer) {
                 addActionToMenu(&menu, "layer_style");
+                addActionToMenu(&menu, "copy_layer_style");
+                addActionToMenu(&menu, "paste_layer_style");
             }
 
             Q_FOREACH(KisNodeSP node, nodes) {


More information about the kimageshop mailing list