1 /*
2  *  Copyright (c) 2018 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "KisReselectActiveSelectionCommand.h"
20 
21 #include "kis_image.h"
22 #include "kis_node.h"
23 #include "kis_layer.h"
24 #include "kis_selection_mask.h"
25 #include <KoProperties.h>
26 
27 
KisReselectActiveSelectionCommand(KisNodeSP activeNode,KisImageWSP image,KUndo2Command * parent)28 KisReselectActiveSelectionCommand::KisReselectActiveSelectionCommand(KisNodeSP activeNode, KisImageWSP image, KUndo2Command *parent)
29     : KisReselectGlobalSelectionCommand(image, parent),
30       m_activeNode(activeNode)
31 {
32 }
33 
redo()34 void KisReselectActiveSelectionCommand::redo()
35 {
36     bool shouldReselectFGlobalSelection = true;
37 
38     if (m_activeNode) {
39         KisSelectionMaskSP mask = dynamic_cast<KisSelectionMask*>(m_activeNode.data());
40 
41         if (!mask) {
42 
43             KisLayerSP layer;
44             KisNodeSP node = m_activeNode;
45             while (node && !(layer = dynamic_cast<KisLayer*>(node.data()))) {
46                 node = node->parent();
47             }
48 
49             if (layer && !layer->selectionMask()) {
50                 KoProperties properties;
51                 properties.setProperty("active", false);
52                 properties.setProperty("visible", true);
53                 QList<KisNodeSP> masks = layer->childNodes(QStringList("KisSelectionMask"), properties);
54 
55                 if (!masks.isEmpty()) {
56                     mask = dynamic_cast<KisSelectionMask*>(masks.first().data());
57                 }
58             } else if (layer && layer->selectionMask()) {
59                 shouldReselectFGlobalSelection = false;
60             }
61         }
62 
63         if (mask) {
64             mask->setActive(true);
65             shouldReselectFGlobalSelection = false;
66             m_reselectedMask = mask;
67         }
68     }
69 
70     if (shouldReselectFGlobalSelection) {
71         KisReselectGlobalSelectionCommand::redo();
72     }
73 }
74 
undo()75 void KisReselectActiveSelectionCommand::undo()
76 {
77     if (m_reselectedMask) {
78         m_reselectedMask->setActive(false);
79         m_reselectedMask.clear();
80     } else {
81         KisReselectGlobalSelectionCommand::undo();
82     }
83 }
84