1 /*
2  *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
3  *  Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "kis_image_commands.h"
21 #include <QString>
22 #include <QBitArray>
23 
24 #include <klocalizedstring.h>
25 
26 #include "KoColor.h"
27 #include "KoColorProfile.h"
28 
29 
30 #include "kis_image.h"
31 #include "kis_layer.h"
32 #include "kis_group_layer.h"
33 #include "kis_undo_adapter.h"
34 
35 
KisImageLayerMoveCommand(KisImageWSP image,KisNodeSP layer,KisNodeSP newParent,KisNodeSP newAbove,bool doUpdates)36 KisImageLayerMoveCommand::KisImageLayerMoveCommand(KisImageWSP image, KisNodeSP layer, KisNodeSP newParent, KisNodeSP newAbove, bool doUpdates)
37         : KisImageCommand(kundo2_i18n("Move Layer"), image)
38 {
39     m_layer = layer;
40     m_newParent = newParent;
41     m_newAbove = newAbove;
42     m_prevParent = layer->parent();
43     m_prevAbove = layer->prevSibling();
44     m_index = -1;
45     m_useIndex = false;
46     m_doUpdates = doUpdates;
47 }
48 
KisImageLayerMoveCommand(KisImageWSP image,KisNodeSP node,KisNodeSP newParent,quint32 index)49 KisImageLayerMoveCommand::KisImageLayerMoveCommand(KisImageWSP image, KisNodeSP node, KisNodeSP newParent, quint32 index)
50         : KisImageCommand(kundo2_i18n("Move Layer"), image)
51 {
52     m_layer = node;
53     m_newParent = newParent;
54     m_newAbove = 0;
55     m_prevParent = node->parent();
56     m_prevAbove = node->prevSibling();
57     m_index = index;
58     m_useIndex = true;
59     m_doUpdates = true;
60 }
61 
redo()62 void KisImageLayerMoveCommand::redo()
63 {
64     KisImageSP image = m_image.toStrongRef();
65     if (!image) {
66         return;
67     }
68     if (m_useIndex) {
69         image->moveNode(m_layer, m_newParent, m_index);
70     } else {
71         image->moveNode(m_layer, m_newParent, m_newAbove);
72     }
73 
74     if (m_doUpdates) {
75         image->refreshGraphAsync(m_prevParent);
76         if (m_newParent != m_prevParent) {
77             m_layer->setDirty(image->bounds());
78         }
79     }
80 }
81 
undo()82 void KisImageLayerMoveCommand::undo()
83 {
84     KisImageSP image = m_image.toStrongRef();
85     if (!image) {
86         return;
87     }
88     image->moveNode(m_layer, m_prevParent, m_prevAbove);
89 
90     if (m_doUpdates) {
91         image->refreshGraphAsync(m_newParent);
92         if (m_newParent != m_prevParent) {
93             m_layer->setDirty(image->bounds());
94         }
95     }
96 }
97