libs/flake

KoShapeShadowCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "KoShapeShadowCommand.h"
00021 #include "KoShape.h"
00022 #include "KoShapeShadow.h"
00023 
00024 #include <klocale.h>
00025 
00026 class KoShapeShadowCommand::Private
00027 {
00028 public:
00029     Private() {}
00030     ~Private() {
00031         foreach(KoShapeShadow* shadow, oldShadows) {
00032             if (shadow && ! shadow->removeUser())
00033                 delete shadow;
00034         }
00035     }
00036 
00037     void addOldShadow( KoShapeShadow * oldShadow )
00038     {
00039         if (oldShadow)
00040             oldShadow->addUser();
00041         oldShadows.append(oldShadow);
00042     }
00043 
00044     void addNewShadow( KoShapeShadow * newShadow )
00045     {
00046         if (newShadow)
00047             newShadow->addUser();
00048         newShadows.append(newShadow);
00049     }
00050 
00051     QList<KoShape*> shapes;           
00052     QList<KoShapeShadow*> oldShadows; 
00053     QList<KoShapeShadow*> newShadows; 
00054 };
00055 
00056 KoShapeShadowCommand::KoShapeShadowCommand(const QList<KoShape*> &shapes, KoShapeShadow *shadow,  QUndoCommand *parent)
00057     : QUndoCommand(parent)
00058     , d(new Private())
00059 {
00060     d->shapes = shapes;
00061     // save old shadows
00062     foreach(KoShape *shape, d->shapes) {
00063         d->addOldShadow(shape->shadow());
00064         d->addNewShadow(shadow);
00065     }
00066 
00067     setText(i18n("Set Shadow"));
00068 }
00069 
00070 KoShapeShadowCommand::KoShapeShadowCommand(const QList<KoShape*> &shapes, const QList<KoShapeShadow*> &shadows, QUndoCommand *parent)
00071     : QUndoCommand(parent)
00072     , d(new Private())
00073 {
00074     Q_ASSERT(shapes.count() == shadows.count());
00075 
00076     d->shapes = shapes;
00077 
00078     // save old shadows
00079     foreach(KoShape *shape, shapes)
00080         d->addOldShadow(shape->shadow());
00081     foreach(KoShapeShadow * shadow, shadows)
00082         d->addNewShadow(shadow);
00083 
00084     setText(i18n("Set Shadow"));
00085 }
00086 
00087 KoShapeShadowCommand::KoShapeShadowCommand(KoShape* shape, KoShapeShadow *shadow, QUndoCommand *parent)
00088     : QUndoCommand(parent)
00089     , d(new Private())
00090 {
00091     d->shapes.append(shape);
00092     d->addNewShadow(shadow);
00093     d->addOldShadow(shape->shadow());
00094 
00095     setText(i18n("Set Shadow"));
00096 }
00097 
00098 KoShapeShadowCommand::~KoShapeShadowCommand()
00099 {
00100     delete d;
00101 }
00102 
00103 void KoShapeShadowCommand::redo()
00104 {
00105     QUndoCommand::redo();
00106     int shapeCount = d->shapes.count();
00107     for (int i = 0; i < shapeCount; ++i) {
00108         KoShape *shape = d->shapes[i];
00109         shape->update();
00110         shape->setShadow(d->newShadows[i]);
00111         shape->update();
00112     }
00113 }
00114 
00115 void KoShapeShadowCommand::undo()
00116 {
00117     QUndoCommand::undo();
00118     int shapeCount = d->shapes.count();
00119     for (int i = 0; i < shapeCount; ++i) {
00120         KoShape *shape = d->shapes[i];
00121         shape->update();
00122         shape->setShadow(d->oldShadows[i]);
00123         shape->update();
00124     }
00125 }