libs/flake

KoPathSeparateCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 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 "KoPathSeparateCommand.h"
00022 #include "KoShapeControllerBase.h"
00023 #include "KoPathShape.h"
00024 #include <klocale.h>
00025 
00026 class KoPathSeparateCommand::Private
00027 {
00028 public:
00029     Private(KoShapeControllerBase *c, const QList<KoPathShape*> &p)
00030             : controller(c),
00031             paths(p),
00032             isSeparated(false) {
00033     }
00034 
00035     ~Private() {
00036         if (isSeparated && controller) {
00037             foreach(KoPathShape* p, paths)
00038                 delete p;
00039         } else {
00040             foreach(KoPathShape* p, separatedPaths)
00041                 delete p;
00042         }
00043     }
00044 
00045     KoShapeControllerBase *controller;
00046     QList<KoPathShape*> paths;
00047     QList<KoPathShape*> separatedPaths;
00048     bool isSeparated;
00049 };
00050 
00051 
00052 KoPathSeparateCommand::KoPathSeparateCommand(KoShapeControllerBase *controller, const QList<KoPathShape*> &paths, QUndoCommand *parent)
00053         : QUndoCommand(parent),
00054         d(new Private(controller, paths))
00055 {
00056     setText(i18n("Separate paths"));
00057 }
00058 
00059 KoPathSeparateCommand::~KoPathSeparateCommand()
00060 {
00061     delete d;
00062 }
00063 
00064 void KoPathSeparateCommand::redo()
00065 {
00066     QUndoCommand::redo();
00067     if (d->separatedPaths.isEmpty()) {
00068         foreach(KoPathShape* p, d->paths) {
00069             QList<KoPathShape*> separatedPaths;
00070             if (p->separate(separatedPaths))
00071                 d->separatedPaths << separatedPaths;
00072         }
00073     }
00074 
00075     d->isSeparated = true;
00076 
00077     if (d->controller) {
00078         foreach(KoPathShape* p, d->paths)
00079             d->controller->removeShape(p);
00080         foreach(KoPathShape *p, d->separatedPaths)
00081             d->controller->addShape(p);
00082     }
00083     foreach(KoPathShape* p, d->paths)
00084         p->update();
00085 }
00086 
00087 void KoPathSeparateCommand::undo()
00088 {
00089     QUndoCommand::undo();
00090     if (d->controller) {
00091         foreach(KoPathShape *p, d->separatedPaths)
00092             d->controller->removeShape(p);
00093         foreach(KoPathShape* p, d->paths)
00094             d->controller->addShape(p);
00095     }
00096 
00097     d->isSeparated = false;
00098 
00099     foreach(KoPathShape* p, d->paths)
00100         p->update();
00101 }