File indexing completed on 2025-02-02 04:14:50

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
0004  * SPDX-FileCopyrightText: 2006-2008 Jan Hambrecht <jaham@gmx.net>
0005  * SPDX-FileCopyrightText: 2012 Inge Wallin <inge@lysator.liu.se>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.0-or-later
0008  */
0009 
0010 #include "KoShapeStrokeCommand.h"
0011 #include "KoShape.h"
0012 #include "KoShapeStrokeModel.h"
0013 
0014 #include <klocalizedstring.h>
0015 
0016 #include "kis_command_ids.h"
0017 
0018 
0019 class Q_DECL_HIDDEN KoShapeStrokeCommand::Private
0020 {
0021 public:
0022     Private() {}
0023     ~Private()
0024     {
0025     }
0026 
0027     void addOldStroke(KoShapeStrokeModelSP oldStroke)
0028     {
0029         oldStrokes.append(oldStroke);
0030     }
0031 
0032     void addNewStroke(KoShapeStrokeModelSP newStroke)
0033     {
0034         newStrokes.append(newStroke);
0035     }
0036 
0037     QList<KoShape*> shapes;                ///< the shapes to set stroke for
0038     QList<KoShapeStrokeModelSP> oldStrokes; ///< the old strokes, one for each shape
0039     QList<KoShapeStrokeModelSP> newStrokes; ///< the new strokes to set
0040 };
0041 
0042 KoShapeStrokeCommand::KoShapeStrokeCommand(const QList<KoShape*> &shapes, KoShapeStrokeModelSP stroke, KUndo2Command *parent)
0043     : KUndo2Command(parent)
0044     , d(new Private())
0045 {
0046     d->shapes = shapes;
0047 
0048     // save old strokes
0049     Q_FOREACH (KoShape *shape, d->shapes) {
0050         d->addOldStroke(shape->stroke());
0051         d->addNewStroke(stroke);
0052     }
0053 
0054     setText(kundo2_i18n("Set stroke"));
0055 }
0056 
0057 KoShapeStrokeCommand::KoShapeStrokeCommand(const QList<KoShape*> &shapes,
0058         const QList<KoShapeStrokeModelSP> &strokes,
0059         KUndo2Command *parent)
0060         : KUndo2Command(parent)
0061         , d(new Private())
0062 {
0063     Q_ASSERT(shapes.count() == strokes.count());
0064 
0065     d->shapes = shapes;
0066 
0067     // save old strokes
0068     Q_FOREACH (KoShape *shape, shapes)
0069         d->addOldStroke(shape->stroke());
0070     foreach (KoShapeStrokeModelSP stroke, strokes)
0071         d->addNewStroke(stroke);
0072 
0073     setText(kundo2_i18n("Set stroke"));
0074 }
0075 
0076 KoShapeStrokeCommand::KoShapeStrokeCommand(KoShape* shape, KoShapeStrokeModelSP stroke, KUndo2Command *parent)
0077         : KUndo2Command(parent)
0078         , d(new Private())
0079 {
0080     d->shapes.append(shape);
0081     d->addNewStroke(stroke);
0082     d->addOldStroke(shape->stroke());
0083 
0084     setText(kundo2_i18n("Set stroke"));
0085 }
0086 
0087 KoShapeStrokeCommand::~KoShapeStrokeCommand()
0088 {
0089     delete d;
0090 }
0091 
0092 void KoShapeStrokeCommand::redo()
0093 {
0094     KUndo2Command::redo();
0095     QList<KoShapeStrokeModelSP>::iterator strokeIt = d->newStrokes.begin();
0096     Q_FOREACH (KoShape *shape, d->shapes) {
0097         const QRectF oldDirtyRect = shape->boundingRect();
0098         shape->setStroke(*strokeIt);
0099         shape->updateAbsolute(oldDirtyRect | shape->boundingRect());
0100         ++strokeIt;
0101     }
0102 }
0103 
0104 void KoShapeStrokeCommand::undo()
0105 {
0106     KUndo2Command::undo();
0107     QList<KoShapeStrokeModelSP>::iterator strokeIt = d->oldStrokes.begin();
0108     Q_FOREACH (KoShape *shape, d->shapes) {
0109         const QRectF oldDirtyRect = shape->boundingRect();
0110         shape->setStroke(*strokeIt);
0111         shape->updateAbsolute(oldDirtyRect | shape->boundingRect());
0112         ++strokeIt;
0113     }
0114 }
0115 
0116 int KoShapeStrokeCommand::id() const
0117 {
0118     return KisCommandUtils::ChangeShapeStrokeId;
0119 }
0120 
0121 bool KoShapeStrokeCommand::mergeWith(const KUndo2Command *command)
0122 {
0123     const KoShapeStrokeCommand *other = dynamic_cast<const KoShapeStrokeCommand*>(command);
0124 
0125     if (!other ||
0126         other->d->shapes != d->shapes) {
0127 
0128         return false;
0129     }
0130 
0131     d->newStrokes = other->d->newStrokes;
0132     return true;
0133 }