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

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2023 Wolthera van Hövell <griffinvalley@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoShapePaintOrderCommand.h"
0008 
0009 #include <klocalizedstring.h>
0010 #include "kis_command_ids.h"
0011 
0012 class Q_DECL_HIDDEN KoShapePaintOrderCommand::Private
0013 {
0014 public:
0015     Private() {
0016     }
0017     ~Private() {
0018     }
0019 
0020     QList<KoShape*> shapes;
0021     QList<KoShape::PaintOrder> oldFirst;
0022     QList<KoShape::PaintOrder> oldSecond;
0023     QList<bool> paintOrderInherited;
0024     KoShape::PaintOrder first;
0025     KoShape::PaintOrder second;
0026 };
0027 
0028 KoShapePaintOrderCommand::KoShapePaintOrderCommand(const QList<KoShape *> &shapes,
0029                                                    KoShape::PaintOrder first,
0030                                                    KoShape::PaintOrder second,
0031                                                    KUndo2Command *parent)
0032     : KUndo2Command(parent)
0033     , d(new Private())
0034 {
0035     d->shapes = shapes;
0036     Q_FOREACH (KoShape *shape, d->shapes) {
0037         d->oldFirst.append(shape->paintOrder().at(0));
0038         d->oldSecond.append(shape->paintOrder().at(1));
0039         d->paintOrderInherited.append(shape->inheritPaintOrder());
0040     }
0041     d->first = first;
0042     d->second = second;
0043 
0044     setText(kundo2_i18n("Set paint order"));
0045 }
0046 
0047 KoShapePaintOrderCommand::~KoShapePaintOrderCommand()
0048 {
0049     delete d;
0050 }
0051 
0052 
0053 void KoShapePaintOrderCommand::redo()
0054 {
0055     KUndo2Command::redo();
0056     Q_FOREACH (KoShape *shape, d->shapes) {
0057         shape->setPaintOrder(d->first, d->second);
0058         shape->update();
0059     }
0060 }
0061 
0062 void KoShapePaintOrderCommand::undo()
0063 {
0064     KUndo2Command::undo();
0065     QList<KoShape::PaintOrder>::iterator firstIt = d->oldFirst.begin();
0066     QList<KoShape::PaintOrder>::iterator secondIt = d->oldSecond.begin();
0067     QList<bool>::iterator inheritIt = d->paintOrderInherited.begin();
0068     Q_FOREACH (KoShape *shape, d->shapes) {
0069         shape->setPaintOrder(*firstIt, *secondIt);
0070         shape->setInheritPaintOrder(*inheritIt);
0071         shape->update();
0072         ++firstIt;
0073         ++secondIt;
0074         ++inheritIt;
0075     }
0076 }
0077 
0078 int KoShapePaintOrderCommand::id() const
0079 {
0080     return KisCommandUtils::ChangePaintOrderCommand;
0081 }
0082 
0083 bool KoShapePaintOrderCommand::mergeWith(const KUndo2Command *command)
0084 {
0085     const KoShapePaintOrderCommand *other = dynamic_cast<const KoShapePaintOrderCommand*>(command);
0086 
0087     if (!other || other->d->shapes != d->shapes) {
0088         return false;
0089     }
0090 
0091     d->first = other->d->first;
0092     d->second = other->d->second;
0093     return true;
0094 }