1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
4  * Copyright (C) 2006-2008 Jan Hambrecht <jaham@gmx.net>
5  * Copyright (C) 2012 Inge Wallin <inge@lysator.liu.se>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "KoShapeStrokeCommand.h"
24 #include "KoShape.h"
25 #include "KoShapeStrokeModel.h"
26 
27 #include <klocalizedstring.h>
28 
29 #include "kis_command_ids.h"
30 
31 
32 class Q_DECL_HIDDEN KoShapeStrokeCommand::Private
33 {
34 public:
Private()35     Private() {}
~Private()36     ~Private()
37     {
38     }
39 
addOldStroke(KoShapeStrokeModelSP oldStroke)40     void addOldStroke(KoShapeStrokeModelSP oldStroke)
41     {
42         oldStrokes.append(oldStroke);
43     }
44 
addNewStroke(KoShapeStrokeModelSP newStroke)45     void addNewStroke(KoShapeStrokeModelSP newStroke)
46     {
47         newStrokes.append(newStroke);
48     }
49 
50     QList<KoShape*> shapes;                ///< the shapes to set stroke for
51     QList<KoShapeStrokeModelSP> oldStrokes; ///< the old strokes, one for each shape
52     QList<KoShapeStrokeModelSP> newStrokes; ///< the new strokes to set
53 };
54 
KoShapeStrokeCommand(const QList<KoShape * > & shapes,KoShapeStrokeModelSP stroke,KUndo2Command * parent)55 KoShapeStrokeCommand::KoShapeStrokeCommand(const QList<KoShape*> &shapes, KoShapeStrokeModelSP stroke, KUndo2Command *parent)
56     : KUndo2Command(parent)
57     , d(new Private())
58 {
59     d->shapes = shapes;
60 
61     // save old strokes
62     Q_FOREACH (KoShape *shape, d->shapes) {
63         d->addOldStroke(shape->stroke());
64         d->addNewStroke(stroke);
65     }
66 
67     setText(kundo2_i18n("Set stroke"));
68 }
69 
KoShapeStrokeCommand(const QList<KoShape * > & shapes,const QList<KoShapeStrokeModelSP> & strokes,KUndo2Command * parent)70 KoShapeStrokeCommand::KoShapeStrokeCommand(const QList<KoShape*> &shapes,
71         const QList<KoShapeStrokeModelSP> &strokes,
72         KUndo2Command *parent)
73         : KUndo2Command(parent)
74         , d(new Private())
75 {
76     Q_ASSERT(shapes.count() == strokes.count());
77 
78     d->shapes = shapes;
79 
80     // save old strokes
81     Q_FOREACH (KoShape *shape, shapes)
82         d->addOldStroke(shape->stroke());
83     foreach (KoShapeStrokeModelSP stroke, strokes)
84         d->addNewStroke(stroke);
85 
86     setText(kundo2_i18n("Set stroke"));
87 }
88 
KoShapeStrokeCommand(KoShape * shape,KoShapeStrokeModelSP stroke,KUndo2Command * parent)89 KoShapeStrokeCommand::KoShapeStrokeCommand(KoShape* shape, KoShapeStrokeModelSP stroke, KUndo2Command *parent)
90         : KUndo2Command(parent)
91         , d(new Private())
92 {
93     d->shapes.append(shape);
94     d->addNewStroke(stroke);
95     d->addOldStroke(shape->stroke());
96 
97     setText(kundo2_i18n("Set stroke"));
98 }
99 
~KoShapeStrokeCommand()100 KoShapeStrokeCommand::~KoShapeStrokeCommand()
101 {
102     delete d;
103 }
104 
redo()105 void KoShapeStrokeCommand::redo()
106 {
107     KUndo2Command::redo();
108     QList<KoShapeStrokeModelSP>::iterator strokeIt = d->newStrokes.begin();
109     Q_FOREACH (KoShape *shape, d->shapes) {
110         shape->update();
111         shape->setStroke(*strokeIt);
112         shape->update();
113         ++strokeIt;
114     }
115 }
116 
undo()117 void KoShapeStrokeCommand::undo()
118 {
119     KUndo2Command::undo();
120     QList<KoShapeStrokeModelSP>::iterator strokeIt = d->oldStrokes.begin();
121     Q_FOREACH (KoShape *shape, d->shapes) {
122         shape->update();
123         shape->setStroke(*strokeIt);
124         shape->update();
125         ++strokeIt;
126     }
127 }
128 
id() const129 int KoShapeStrokeCommand::id() const
130 {
131     return KisCommandUtils::ChangeShapeStrokeId;
132 }
133 
mergeWith(const KUndo2Command * command)134 bool KoShapeStrokeCommand::mergeWith(const KUndo2Command *command)
135 {
136     const KoShapeStrokeCommand *other = dynamic_cast<const KoShapeStrokeCommand*>(command);
137 
138     if (!other ||
139         other->d->shapes != d->shapes) {
140 
141         return false;
142     }
143 
144     d->newStrokes = other->d->newStrokes;
145     return true;
146 }
147