libs/flake

KoShapeBackgroundCommand.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) 2008 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 "KoShapeBackgroundCommand.h"
00022 #include "KoShape.h"
00023 #include "KoShapeBackground.h"
00024 
00025 #include <klocale.h>
00026 
00027 class KoShapeBackgroundCommand::Private
00028 {
00029 public:
00030     Private() {
00031     }
00032     ~Private() {
00033         foreach(KoShapeBackground* fill, oldFills) {
00034             if (fill && ! fill->removeUser())
00035                 delete fill;
00036         }
00037         foreach(KoShapeBackground* fill, newFills) {
00038             if (fill && ! fill->removeUser())
00039                 delete fill;
00040         }
00041     }
00042 
00043     void addOldFill( KoShapeBackground * oldFill )
00044     {
00045         if (oldFill)
00046             oldFill->addUser();
00047         oldFills.append(oldFill);
00048     }
00049 
00050     void addNewFill( KoShapeBackground * newFill )
00051     {
00052         if (newFill)
00053             newFill->addUser();
00054         newFills.append(newFill);
00055     }
00056 
00057     QList<KoShape*> shapes;    
00058     QList<KoShapeBackground*> oldFills;
00059     QList<KoShapeBackground*> newFills;
00060 };
00061 
00062 KoShapeBackgroundCommand::KoShapeBackgroundCommand(const QList<KoShape*> &shapes, KoShapeBackground * fill,
00063         QUndoCommand *parent)
00064         : QUndoCommand(parent)
00065         , d(new Private())
00066 {
00067     d->shapes = shapes;
00068     foreach(KoShape *shape, d->shapes) {
00069         d->addOldFill(shape->background());
00070         d->addNewFill(fill);
00071     }
00072 
00073     setText(i18n("Set background"));
00074 }
00075 
00076 KoShapeBackgroundCommand::KoShapeBackgroundCommand(KoShape * shape, KoShapeBackground * fill, QUndoCommand *parent)
00077         : QUndoCommand(parent)
00078         , d(new Private())
00079 {
00080     d->shapes.append(shape);
00081     d->addOldFill(shape->background());
00082     d->addNewFill(fill);
00083 
00084     setText(i18n("Set background"));
00085 }
00086 
00087 KoShapeBackgroundCommand::KoShapeBackgroundCommand(const QList<KoShape*> &shapes, const QList<KoShapeBackground*> &fills, QUndoCommand *parent)
00088         : QUndoCommand(parent)
00089         , d(new Private())
00090 {
00091     d->shapes = shapes;
00092     foreach(KoShape *shape, d->shapes) {
00093         d->addOldFill(shape->background());
00094     }
00095     foreach(KoShapeBackground * fill, fills ) {
00096         d->addNewFill(fill);
00097     }
00098 
00099     setText(i18n("Set background"));
00100 }
00101 
00102 void KoShapeBackgroundCommand::redo()
00103 {
00104     QUndoCommand::redo();
00105     QList<KoShapeBackground*>::iterator brushIt = d->newFills.begin();
00106     foreach(KoShape *shape, d->shapes) {
00107         shape->setBackground(*brushIt);
00108         shape->update();
00109         brushIt++;
00110     }
00111 }
00112 
00113 void KoShapeBackgroundCommand::undo()
00114 {
00115     QUndoCommand::undo();
00116     QList<KoShapeBackground*>::iterator brushIt = d->oldFills.begin();
00117     foreach(KoShape *shape, d->shapes) {
00118         shape->setBackground(*brushIt);
00119         shape->update();
00120         brushIt++;
00121     }
00122 }
00123 
00124 KoShapeBackgroundCommand::~KoShapeBackgroundCommand()
00125 {
00126     delete d;
00127 }