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: 2006 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "KoShapeCreateCommand.h"
0009 #include "KoShape.h"
0010 #include "KoShapeContainer.h"
0011 #include "KoShapeControllerBase.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "kis_assert.h"
0016 #include <KoShapeLayer.h>
0017 #include <KoShapeReorderCommand.h>
0018 
0019 #include <vector>
0020 #include <memory>
0021 
0022 #include <kis_undo_stores.h>
0023 #include <KoAddRemoveShapeCommands.h>
0024 
0025 class Q_DECL_HIDDEN KoShapeCreateCommand::Private
0026 {
0027 public:
0028     Private(KoShapeControllerBase *_document, const QList<KoShape*> &_shapes, KoShapeContainer *_parentShape)
0029             : shapesDocument(_document),
0030             shapes(_shapes),
0031             explicitParentShape(_parentShape)
0032     {
0033     }
0034 
0035     KoShapeControllerBase *shapesDocument;
0036     QList<KoShape*> shapes;
0037     KoShapeContainer *explicitParentShape;
0038 
0039     KisSurrogateUndoStore undoStore;
0040     bool firstRedo = true;
0041 
0042 };
0043 
0044 KoShapeCreateCommand::KoShapeCreateCommand(KoShapeControllerBase *controller, KoShape *shape, KoShapeContainer *parentShape, KUndo2Command *parent)
0045     : KoShapeCreateCommand(controller, QList<KoShape *>() << shape, parentShape, parent)
0046 {
0047 }
0048 
0049 KoShapeCreateCommand::KoShapeCreateCommand(KoShapeControllerBase *controller, const QList<KoShape *> shapes, KoShapeContainer *parentShape, KUndo2Command *parent)
0050         : KoShapeCreateCommand(controller, shapes, parentShape, parent, kundo2_i18np("Create shape", "Create %1 shapes", shapes.size()))
0051 {
0052 }
0053 
0054 KoShapeCreateCommand::KoShapeCreateCommand(KoShapeControllerBase *controller, const QList<KoShape *> shapes, KoShapeContainer *parentShape, KUndo2Command *parent, const KUndo2MagicString &undoString)
0055         : KUndo2Command(undoString, parent)
0056         , d(new Private(controller, shapes, parentShape))
0057 {
0058 }
0059 
0060 KoShapeCreateCommand::~KoShapeCreateCommand()
0061 {
0062     delete d;
0063 }
0064 
0065 void KoShapeCreateCommand::redo()
0066 {
0067     KUndo2Command::redo();
0068     KIS_SAFE_ASSERT_RECOVER_RETURN(d->explicitParentShape);
0069 
0070     if (d->firstRedo) {
0071         Q_FOREACH(KoShape *shape, d->shapes) {
0072 
0073             d->undoStore.addCommand(new KoAddShapeCommand(shape, d->explicitParentShape));
0074 
0075             KoShapeContainer *shapeParent = shape->parent();
0076             KIS_SAFE_ASSERT_RECOVER_NOOP(shape->parent() ||
0077                                          dynamic_cast<KoShapeLayer*>(shape));
0078 
0079             if (shapeParent) {
0080                 d->undoStore.addCommand(KoShapeReorderCommand::mergeInShape(shapeParent->shapes(), shape));
0081             }
0082         }
0083         d->firstRedo = false;
0084     } else {
0085         d->undoStore.redoAll();
0086     }
0087 }
0088 
0089 void KoShapeCreateCommand::undo()
0090 {
0091     d->undoStore.undoAll();
0092     KUndo2Command::undo();
0093 }