libs/flake

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