libs/flake

KoShapeDeleteCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2006 Jan Hambrecht <jaham@gmx.net>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "KoShapeDeleteCommand.h"
00022 #include "KoShapeContainer.h"
00023 #include "KoShapeControllerBase.h"
00024 
00025 #include <klocale.h>
00026 
00027 class KoShapeDeleteCommand::Private
00028 {
00029 public:
00030     Private(KoShapeControllerBase *c)
00031             : controller(c),
00032             deleteShapes(false) {
00033     }
00034 
00035     ~Private() {
00036         if (! deleteShapes)
00037             return;
00038 
00039         foreach(KoShape *shape, shapes)
00040             delete shape;
00041     }
00042 
00043     KoShapeControllerBase *controller; 
00044     QList<KoShape*> shapes; 
00045     QList<KoShapeContainer*> oldParents; 
00046     bool deleteShapes;  
00047 };
00048 
00049 KoShapeDeleteCommand::KoShapeDeleteCommand(KoShapeControllerBase *controller, KoShape *shape, QUndoCommand *parent)
00050         : QUndoCommand(parent),
00051         d(new Private(controller))
00052 {
00053     d->shapes.append(shape);
00054     d->oldParents.append(shape->parent());
00055 
00056     setText(i18n("Delete shape"));
00057 }
00058 
00059 KoShapeDeleteCommand::KoShapeDeleteCommand(KoShapeControllerBase *controller, const QList<KoShape*> &shapes,
00060         QUndoCommand *parent)
00061         : QUndoCommand(parent),
00062         d(new Private(controller))
00063 {
00064     d->shapes = shapes;
00065     foreach(KoShape *shape, d->shapes) {
00066         d->oldParents.append(shape->parent());
00067     }
00068 
00069     setText(i18n("Delete shapes"));
00070 }
00071 
00072 KoShapeDeleteCommand::~KoShapeDeleteCommand()
00073 {
00074     delete d;
00075 }
00076 
00077 void KoShapeDeleteCommand::redo()
00078 {
00079     QUndoCommand::redo();
00080     if (! d->controller)
00081         return;
00082 
00083     for (int i = 0; i < d->shapes.count(); i++) {
00084         // the parent has to be there when it is removed from the KoShapeControllerBase
00085         d->controller->removeShape(d->shapes[i]);
00086         if (d->oldParents.at(i))
00087             d->oldParents.at(i)->removeChild(d->shapes[i]);
00088     }
00089     d->deleteShapes = true;
00090 }
00091 
00092 void KoShapeDeleteCommand::undo()
00093 {
00094     QUndoCommand::undo();
00095     if (! d->controller)
00096         return;
00097 
00098     for (int i = 0; i < d->shapes.count(); i++) {
00099         if (d->oldParents.at(i))
00100             d->oldParents.at(i)->addChild(d->shapes[i]);
00101         // the parent has to be there when it is added to the KoShapeControllerBase
00102         d->controller->addShape(d->shapes[i]);
00103     }
00104     d->deleteShapes = false;
00105 }