File indexing completed on 2024-06-09 04:20:45

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