[krita] libs: FEATURE: Allow to deselect/reselect local selection if it is present

Dmitry Kazakov null at kde.org
Wed Aug 22 16:08:41 BST 2018


Git commit 40a489ee0b099124059e23b4f0462eb6b9135f8b by Dmitry Kazakov.
Committed on 22/08/2018 at 15:08.
Pushed by dkazakov into branch 'master'.

FEATURE: Allow to deselect/reselect local selection if it is present

Previously Deselect and Reselect actions could be used only
for global selection. Now, if there is a local selection active,
they will activate/deactivate the selection mask.

CCBUG:341973
CC:kimageshop at kde.org

M  +2    -0    libs/image/CMakeLists.txt
A  +65   -0    libs/image/commands/KisDeselectActiveSelectionCommand.cpp     [License: GPL (v2+)]
C  +17   -6    libs/image/commands/KisDeselectActiveSelectionCommand.h [from: libs/image/commands/kis_selection_commands.h - 056% similarity]
A  +83   -0    libs/image/commands/KisReselectActiveSelectionCommand.cpp     [License: GPL (v2+)]
C  +17   -6    libs/image/commands/KisReselectActiveSelectionCommand.h [from: libs/image/commands/kis_selection_commands.h - 059% similarity]
M  +1    -1    libs/image/commands/kis_deselect_global_selection_command.h
M  +1    -1    libs/image/commands/kis_reselect_global_selection_command.h
M  +2    -0    libs/image/commands/kis_selection_commands.h
M  +1    -0    libs/image/kis_selection.h
M  +2    -2    libs/ui/actions/kis_selection_action_factories.cpp

https://commits.kde.org/krita/40a489ee0b099124059e23b4f0462eb6b9135f8b

diff --git a/libs/image/CMakeLists.txt b/libs/image/CMakeLists.txt
index 1fa68ecb61c..06d757f161a 100644
--- a/libs/image/CMakeLists.txt
+++ b/libs/image/CMakeLists.txt
@@ -82,6 +82,7 @@ set(kritaimage_LIB_SRCS
    brushengine/KisStrokeSpeedMeasurer.cpp
    brushengine/KisPaintopSettingsIds.cpp
    commands/kis_deselect_global_selection_command.cpp
+   commands/KisDeselectActiveSelectionCommand.cpp
    commands/kis_image_change_layers_command.cpp
    commands/kis_image_change_visibility_command.cpp
    commands/kis_image_command.cpp
@@ -96,6 +97,7 @@ set(kritaimage_LIB_SRCS
    commands/kis_node_opacity_command.cpp
    commands/kis_node_property_list_command.cpp
    commands/kis_reselect_global_selection_command.cpp
+   commands/KisReselectActiveSelectionCommand.cpp
    commands/kis_set_global_selection_command.cpp
    commands_new/kis_saved_commands.cpp
    commands_new/kis_processing_command.cpp
diff --git a/libs/image/commands/KisDeselectActiveSelectionCommand.cpp b/libs/image/commands/KisDeselectActiveSelectionCommand.cpp
new file mode 100644
index 00000000000..195a6edd7e0
--- /dev/null
+++ b/libs/image/commands/KisDeselectActiveSelectionCommand.cpp
@@ -0,0 +1,65 @@
+/*
+ *  Copyright (c) 2018 Dmitry Kazakov <dimula73 at gmail.com>
+ *
+ *  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 "KisDeselectActiveSelectionCommand.h"
+#include "kis_image.h"
+#include "kis_selection.h"
+#include "kis_selection_mask.h"
+
+KisDeselectActiveSelectionCommand::KisDeselectActiveSelectionCommand(KisSelectionSP activeSelection, KisImageWSP image, KUndo2Command *parent)
+    : KisDeselectGlobalSelectionCommand(image, parent),
+      m_activeSelection(activeSelection)
+{
+}
+
+KisDeselectActiveSelectionCommand::~KisDeselectActiveSelectionCommand()
+{
+}
+
+void KisDeselectActiveSelectionCommand::redo()
+{
+    KisImageSP image = m_image.toStrongRef();
+    KIS_SAFE_ASSERT_RECOVER_RETURN(image);
+
+    if (m_activeSelection && m_activeSelection == image->globalSelection()) {
+        KisDeselectGlobalSelectionCommand::redo();
+    } else if (m_activeSelection) {
+        KisNodeSP parentNode = m_activeSelection->parentNode();
+        if (!parentNode) return;
+
+        m_deselectedMask = dynamic_cast<KisSelectionMask*>(parentNode.data());
+        if (m_deselectedMask) {
+            KIS_SAFE_ASSERT_RECOVER(m_deselectedMask->active()) {
+                m_deselectedMask.clear();
+                return;
+            }
+
+            m_deselectedMask->setActive(false);
+        }
+    }
+}
+
+void KisDeselectActiveSelectionCommand::undo()
+{
+    if (m_deselectedMask) {
+        m_deselectedMask->setActive(true);
+        m_deselectedMask.clear();
+    } else {
+        KisDeselectGlobalSelectionCommand::undo();
+    }
+}
diff --git a/libs/image/commands/kis_selection_commands.h b/libs/image/commands/KisDeselectActiveSelectionCommand.h
similarity index 56%
copy from libs/image/commands/kis_selection_commands.h
copy to libs/image/commands/KisDeselectActiveSelectionCommand.h
index 2cdc0fb0956..c9742ccb72f 100644
--- a/libs/image/commands/kis_selection_commands.h
+++ b/libs/image/commands/KisDeselectActiveSelectionCommand.h
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2008 Boudewijn Rempt <boud at kde.org>
+ *  Copyright (c) 2018 Dmitry Kazakov <dimula73 at gmail.com>
  *
  *  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
@@ -16,12 +16,23 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#ifndef KIS_SELECTION_COMMANDS_H
-#define KIS_SELECTION_COMMANDS_H
+#ifndef KISDESELECTACTIVESELECTIONCOMMAND_H
+#define KISDESELECTACTIVESELECTIONCOMMAND_H
 
 #include "kis_deselect_global_selection_command.h"
-#include "kis_reselect_global_selection_command.h"
-#include "kis_set_global_selection_command.h"
 
-#endif
+class KRITAIMAGE_EXPORT KisDeselectActiveSelectionCommand : public KisDeselectGlobalSelectionCommand
+{
+public:
+    KisDeselectActiveSelectionCommand(KisSelectionSP activeSelection, KisImageWSP image, KUndo2Command * parent = 0);
+    ~KisDeselectActiveSelectionCommand() override;
 
+    void redo() override;
+    void undo() override;
+
+private:
+    KisSelectionSP m_activeSelection;
+    KisSelectionMaskSP m_deselectedMask;
+};
+
+#endif // KISDESELECTACTIVESELECTIONCOMMAND_H
diff --git a/libs/image/commands/KisReselectActiveSelectionCommand.cpp b/libs/image/commands/KisReselectActiveSelectionCommand.cpp
new file mode 100644
index 00000000000..4af691f9c56
--- /dev/null
+++ b/libs/image/commands/KisReselectActiveSelectionCommand.cpp
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (c) 2018 Dmitry Kazakov <dimula73 at gmail.com>
+ *
+ *  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 "KisReselectActiveSelectionCommand.h"
+
+#include "kis_image.h"
+#include "kis_node.h"
+#include "kis_layer.h"
+#include "kis_selection_mask.h"
+#include <KoProperties.h>
+
+
+KisReselectActiveSelectionCommand::KisReselectActiveSelectionCommand(KisNodeSP activeNode, KisImageWSP image, KUndo2Command *parent)
+    : KisReselectGlobalSelectionCommand(image, parent),
+      m_activeNode(activeNode)
+{
+}
+
+void KisReselectActiveSelectionCommand::redo()
+{
+    bool shouldReselectFGlobalSelection = true;
+
+    if (m_activeNode) {
+        KisSelectionMaskSP mask = dynamic_cast<KisSelectionMask*>(m_activeNode.data());
+
+        if (!mask) {
+
+            KisLayerSP layer;
+            KisNodeSP node = m_activeNode;
+            while (node && !(layer = dynamic_cast<KisLayer*>(node.data()))) {
+                node = node->parent();
+            }
+
+            if (layer && !layer->selectionMask()) {
+                KoProperties properties;
+                properties.setProperty("active", false);
+                properties.setProperty("visible", true);
+                QList<KisNodeSP> masks = layer->childNodes(QStringList("KisSelectionMask"), properties);
+
+                if (!masks.isEmpty()) {
+                    mask = dynamic_cast<KisSelectionMask*>(masks.first().data());
+                }
+            } else if (layer && layer->selectionMask()) {
+                shouldReselectFGlobalSelection = false;
+            }
+        }
+
+        if (mask) {
+            mask->setActive(true);
+            shouldReselectFGlobalSelection = false;
+            m_reselectedMask = mask;
+        }
+    }
+
+    if (shouldReselectFGlobalSelection) {
+        KisReselectGlobalSelectionCommand::redo();
+    }
+}
+
+void KisReselectActiveSelectionCommand::undo()
+{
+    if (m_reselectedMask) {
+        m_reselectedMask->setActive(false);
+        m_reselectedMask.clear();
+    } else {
+        KisReselectGlobalSelectionCommand::undo();
+    }
+}
diff --git a/libs/image/commands/kis_selection_commands.h b/libs/image/commands/KisReselectActiveSelectionCommand.h
similarity index 59%
copy from libs/image/commands/kis_selection_commands.h
copy to libs/image/commands/KisReselectActiveSelectionCommand.h
index 2cdc0fb0956..2ffb565a416 100644
--- a/libs/image/commands/kis_selection_commands.h
+++ b/libs/image/commands/KisReselectActiveSelectionCommand.h
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2008 Boudewijn Rempt <boud at kde.org>
+ *  Copyright (c) 2018 Dmitry Kazakov <dimula73 at gmail.com>
  *
  *  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
@@ -16,12 +16,23 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#ifndef KIS_SELECTION_COMMANDS_H
-#define KIS_SELECTION_COMMANDS_H
+#ifndef KISRESELECTACTIVESELECTIONCOMMAND_H
+#define KISRESELECTACTIVESELECTIONCOMMAND_H
 
-#include "kis_deselect_global_selection_command.h"
 #include "kis_reselect_global_selection_command.h"
-#include "kis_set_global_selection_command.h"
 
-#endif
 
+class KRITAIMAGE_EXPORT KisReselectActiveSelectionCommand : public KisReselectGlobalSelectionCommand
+{
+public:
+    KisReselectActiveSelectionCommand(KisNodeSP activeNode, KisImageWSP image, KUndo2Command * parent = 0);
+
+    void redo() override;
+    void undo() override;
+
+private:
+    KisNodeSP m_activeNode;
+    KisSelectionMaskSP m_reselectedMask;
+};
+
+#endif // KISRESELECTACTIVESELECTIONCOMMAND_H
diff --git a/libs/image/commands/kis_deselect_global_selection_command.h b/libs/image/commands/kis_deselect_global_selection_command.h
index 39030beb8fb..64982eaace0 100644
--- a/libs/image/commands/kis_deselect_global_selection_command.h
+++ b/libs/image/commands/kis_deselect_global_selection_command.h
@@ -39,7 +39,7 @@ public:
     void redo() override;
     void undo() override;
 
-private:
+protected:
     KisImageWSP m_image;
     KisSelectionSP m_oldSelection;
 };
diff --git a/libs/image/commands/kis_reselect_global_selection_command.h b/libs/image/commands/kis_reselect_global_selection_command.h
index c27a7b848c9..0e5c9dad026 100644
--- a/libs/image/commands/kis_reselect_global_selection_command.h
+++ b/libs/image/commands/kis_reselect_global_selection_command.h
@@ -38,7 +38,7 @@ public:
     void redo() override;
     void undo() override;
 
-private:
+protected:
     KisImageWSP m_image;
     bool m_canReselect;
 };
diff --git a/libs/image/commands/kis_selection_commands.h b/libs/image/commands/kis_selection_commands.h
index 2cdc0fb0956..78f31252846 100644
--- a/libs/image/commands/kis_selection_commands.h
+++ b/libs/image/commands/kis_selection_commands.h
@@ -20,7 +20,9 @@
 #define KIS_SELECTION_COMMANDS_H
 
 #include "kis_deselect_global_selection_command.h"
+#include "KisDeselectActiveSelectionCommand.h"
 #include "kis_reselect_global_selection_command.h"
+#include "KisReselectActiveSelectionCommand.h"
 #include "kis_set_global_selection_command.h"
 
 #endif
diff --git a/libs/image/kis_selection.h b/libs/image/kis_selection.h
index 48930d815e3..97ce6f2c25b 100644
--- a/libs/image/kis_selection.h
+++ b/libs/image/kis_selection.h
@@ -204,6 +204,7 @@ private:
     friend class KisAdjustmentLayerTest;
     friend class KisUpdateSelectionJob;
     friend class KisSelectionUpdateCompressor;
+    friend class KisDeselectActiveSelectionCommand;
     KisNodeWSP parentNode() const;
 
     void copyFrom(const KisSelection &rhs);
diff --git a/libs/ui/actions/kis_selection_action_factories.cpp b/libs/ui/actions/kis_selection_action_factories.cpp
index 193ae2cf84b..93091ce5863 100644
--- a/libs/ui/actions/kis_selection_action_factories.cpp
+++ b/libs/ui/actions/kis_selection_action_factories.cpp
@@ -172,7 +172,7 @@ void KisDeselectActionFactory::run(KisViewManager *view)
     KisImageWSP image = view->image();
     if (!image) return;
 
-    KUndo2Command *cmd = new KisDeselectGlobalSelectionCommand(image);
+    KUndo2Command *cmd = new KisDeselectActiveSelectionCommand(view->selection(), image);
 
     KisProcessingApplicator *ap = beginAction(view, cmd->text());
     ap->applyCommand(cmd, KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::EXCLUSIVE);
@@ -184,7 +184,7 @@ void KisReselectActionFactory::run(KisViewManager *view)
     KisImageWSP image = view->image();
     if (!image) return;
 
-    KUndo2Command *cmd = new KisReselectGlobalSelectionCommand(image);
+    KUndo2Command *cmd = new KisReselectActiveSelectionCommand(view->activeNode(), image);
 
     KisProcessingApplicator *ap = beginAction(view, cmd->text());
     ap->applyCommand(cmd, KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::EXCLUSIVE);



More information about the kimageshop mailing list