libs/flake
KoShapeCreateCommand.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 "KoShapeCreateCommand.h" 00022 #include "KoShape.h" 00023 #include "KoShapeContainer.h" 00024 #include "KoShapeControllerBase.h" 00025 00026 #include <klocale.h> 00027 00028 class KoShapeCreateCommand::Private 00029 { 00030 public: 00031 Private(KoShapeControllerBase *c, KoShape *s) 00032 : controller(c), 00033 shape(s), 00034 shapeParent(shape->parent()), 00035 deleteShape(true) { 00036 } 00037 ~Private() { 00038 if (shape && deleteShape) 00039 delete shape; 00040 } 00041 00042 KoShapeControllerBase *controller; 00043 KoShape *shape; 00044 KoShapeContainer *shapeParent; 00045 bool deleteShape; 00046 }; 00047 00048 KoShapeCreateCommand::KoShapeCreateCommand(KoShapeControllerBase *controller, KoShape *shape, QUndoCommand *parent) 00049 : QUndoCommand(parent), 00050 d(new Private(controller, shape)) 00051 { 00052 setText(i18n("Create shape")); 00053 } 00054 00055 KoShapeCreateCommand::~KoShapeCreateCommand() 00056 { 00057 delete d; 00058 } 00059 00060 void KoShapeCreateCommand::redo() 00061 { 00062 QUndoCommand::redo(); 00063 Q_ASSERT(d->shape); 00064 Q_ASSERT(d->controller); 00065 if (d->shapeParent) 00066 d->shapeParent->addChild(d->shape); 00067 // the parent has to be there when it is added to the KoShapeControllerBase 00068 d->controller->addShape(d->shape); 00069 d->shapeParent = d->shape->parent(); // update parent if the 'addShape' changed it 00070 d->deleteShape = false; 00071 } 00072 00073 void KoShapeCreateCommand::undo() 00074 { 00075 QUndoCommand::undo(); 00076 Q_ASSERT(d->shape); 00077 Q_ASSERT(d->controller); 00078 // the parent has to be there when it is removed from the KoShapeControllerBase 00079 d->controller->removeShape(d->shape); 00080 if (d->shapeParent) 00081 d->shapeParent->removeChild(d->shape); 00082 d->deleteShape = true; 00083 }
