KoShapeBorderCommand.cpp 3.14 KB
Newer Older
1 2
/* This file is part of the KDE project
 * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3
 * Copyright (C) 2006-2007 Jan Hambrecht <jaham@gmx.net>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "KoShapeBorderCommand.h"
22 23
#include "KoShape.h"
#include "KoShapeBorderModel.h"
24 25 26

#include <klocale.h>

27 28
class KoShapeBorderCommand::Private {
public:
29
    Private() {}
30 31 32 33 34 35
    ~Private() {
        foreach(KoShapeBorderModel* border, oldBorders) {
            if(border->useCount() <= 0)
                delete border;
        }
    }
36 37
    QList<KoShape*> shapes;                ///< the shapes to set border for
    QList<KoShapeBorderModel*> oldBorders; ///< the old borders, one for each shape
38
    QList<KoShapeBorderModel*> newBorders; ///< the new borders to set
39
};
40

41
KoShapeBorderCommand::KoShapeBorderCommand( const QList<KoShape*> &shapes, KoShapeBorderModel *border,
42 43
                                            QUndoCommand *parent )
: QUndoCommand( parent )
44
, d(new Private())
45
{
46
    d->shapes = shapes;
47 48 49 50 51 52 53 54 55 56 57
    int shapeCount = shapes.count();
    for( int i = 0; i < shapeCount; ++i )
        d->newBorders.append( border );

    // save old borders
    foreach( KoShape *shape, d->shapes )
        d->oldBorders.append( shape->border() );

    setText( i18n( "Set border" ) );
}

Jan Hambrecht's avatar
Jan Hambrecht committed
58 59
KoShapeBorderCommand::KoShapeBorderCommand( const QList<KoShape*> &shapes,
                                            const QList<KoShapeBorderModel*> &borders,
60 61 62 63 64 65 66 67 68 69
                                            QUndoCommand *parent )
: QUndoCommand( parent )
, d(new Private())
{
    Q_ASSERT( shapes.count() == borders.count() );

    d->shapes = shapes;
    d->newBorders = borders;

    // save old borders
70
    foreach( KoShape *shape, shapes )
71 72
        d->oldBorders.append( shape->border() );

73
    setText( i18n( "Set border" ) );
74 75 76
}

KoShapeBorderCommand::~KoShapeBorderCommand() {
77
    delete d;
78 79
}

80
void KoShapeBorderCommand::redo () {
81
    QUndoCommand::redo();
82
    QList<KoShapeBorderModel*>::iterator borderIt = d->newBorders.begin();
83
    foreach( KoShape *shape, d->shapes ) {
84
        shape->repaint();
85
        shape->setBorder( *borderIt );
86
        shape->repaint();
87
        ++borderIt;
88 89 90
    }
}

91
void KoShapeBorderCommand::undo () {
92
    QUndoCommand::undo();
93 94
    QList<KoShapeBorderModel*>::iterator borderIt = d->oldBorders.begin();
    foreach( KoShape *shape, d->shapes ) {
95
        shape->repaint();
96 97
        shape->setBorder( *borderIt );
        shape->repaint();
98
        ++borderIt;
99 100
    }
}