libs/flake

KoPathCombineCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006,2008 Jan Hambrecht <jaham@gmx.net>
00003  * Copyright (C) 2006,2007 Thorsten Zachmann <zachmann@kde.org>
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 "KoPathCombineCommand.h"
00022 #include "KoShapeControllerBase.h"
00023 #include "KoShapeContainer.h"
00024 #include <klocale.h>
00025 
00026 class KoPathCombineCommand::Private
00027 {
00028 public:
00029     Private(KoShapeControllerBase *c, const QList<KoPathShape*> &p)
00030         : controller(c), paths(p)
00031         , combinedPath(0), combinedPathParent(0)
00032         , isCombined(false)
00033     {
00034         foreach( KoPathShape * path, paths )
00035             oldParents.append( path->parent() );
00036     }
00037     ~Private() {
00038         if (isCombined && controller) {
00039             foreach(KoPathShape* path, paths)
00040                 delete path;
00041         } else
00042             delete combinedPath;
00043     }
00044 
00045     KoShapeControllerBase *controller;
00046     QList<KoPathShape*> paths;
00047     QList<KoShapeContainer*> oldParents;
00048     KoPathShape *combinedPath;
00049     KoShapeContainer *combinedPathParent;
00050     bool isCombined;
00051 };
00052 
00053 KoPathCombineCommand::KoPathCombineCommand(KoShapeControllerBase *controller,
00054         const QList<KoPathShape*> &paths, QUndoCommand *parent)
00055 : QUndoCommand(parent)
00056 , d(new Private(controller, paths))
00057 {
00058     setText(i18n("Combine paths"));
00059 
00060     d->combinedPath = new KoPathShape();
00061     d->combinedPath->setBorder(d->paths.first()->border());
00062     d->combinedPath->setShapeId(d->paths.first()->shapeId());
00063     // combine the paths
00064     foreach(KoPathShape* path, d->paths) {
00065         d->combinedPath->combine(path);
00066         if (! d->combinedPathParent && path->parent())
00067             d->combinedPathParent = path->parent();
00068     }
00069 }
00070 
00071 KoPathCombineCommand::~KoPathCombineCommand()
00072 {
00073     delete d;
00074 }
00075 
00076 void KoPathCombineCommand::redo()
00077 {
00078     QUndoCommand::redo();
00079 
00080     if (! d->paths.size())
00081         return;
00082 
00083     d->isCombined = true;
00084 
00085     if (d->controller) {
00086         QList<KoShapeContainer*>::iterator parentIt = d->oldParents.begin();
00087         foreach(KoPathShape* p, d->paths) {
00088             d->controller->removeShape(p);
00089             if (*parentIt)
00090                 (*parentIt)->removeChild(p);
00091             parentIt++;
00092 
00093         }
00094         if (d->combinedPathParent)
00095             d->combinedPathParent->addChild(d->combinedPath);
00096         d->controller->addShape(d->combinedPath);
00097     }
00098 }
00099 
00100 void KoPathCombineCommand::undo()
00101 {
00102     if (! d->paths.size())
00103         return;
00104 
00105     d->isCombined = false;
00106 
00107     if (d->controller) {
00108         d->controller->removeShape(d->combinedPath);
00109         if (d->combinedPath->parent())
00110             d->combinedPath->parent()->removeChild(d->combinedPath);
00111         QList<KoShapeContainer*>::iterator parentIt = d->oldParents.begin();
00112         foreach(KoPathShape* p, d->paths) {
00113             d->controller->addShape(p);
00114             p->setParent(*parentIt);
00115             parentIt++;
00116         }
00117     }
00118     QUndoCommand::undo();
00119 }
00120