KoShapeGroupCommand.cpp 3.4 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
 *
 * 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.
 */

21
#include "KoShapeGroupCommand.h"
22 23 24 25 26 27
#include "KoShape.h"
#include "KoShapeGroup.h"
#include "KoShapeContainer.h"

#include <klocale.h>

28
KoShapeGroupCommand::KoShapeGroupCommand(KoShapeContainer *container, QList<KoShape *> shapes, QList<bool> clipped,
29 30 31
                                            QUndoCommand *parent)
: QUndoCommand( parent )
, m_shapes(shapes)
32 33 34 35 36 37
, m_clipped(clipped)
, m_container(container)
{
    Q_ASSERT(m_clipped.count() == m_shapes.count());
    foreach( KoShape* shape, m_shapes )
        m_oldParents.append( shape->parent() );
38 39

    setText( i18n( "Group shapes" ) );
40 41
}

42
KoShapeGroupCommand::KoShapeGroupCommand(KoShapeGroup *container, QList<KoShape *> shapes, QUndoCommand *parent)
43 44
: QUndoCommand(parent)
, m_shapes(shapes)
45 46 47 48 49 50 51
, m_container(container)
{
    foreach( KoShape* shape, m_shapes )
    {
        m_clipped.append(false);
        m_oldParents.append( shape->parent() );
    }
52 53

    setText( i18n( "Group shapes" ) );
54 55
}

56
KoShapeGroupCommand::KoShapeGroupCommand(QUndoCommand *parent)
57 58
: QUndoCommand(parent)
{
59 60
}

61
void KoShapeGroupCommand::redo () {
62
    QUndoCommand::redo();
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    if( dynamic_cast<KoShapeGroup*>( m_container ) )
    {
        bool boundingRectInitialized=true;
        QRectF bound;
        if(m_container->childCount() > 0)
            bound = m_container->boundingRect();
        else
            boundingRectInitialized = false;
        foreach(KoShape *shape, m_shapes) {
            if(boundingRectInitialized)
                bound = bound.unite(shape->boundingRect());
            else {
                bound = shape->boundingRect();
                boundingRectInitialized = true;
            }
79
        }
80
        m_container->setPosition( bound.topLeft() );
81
        m_container->setSize( bound.size() );
82 83
    }

84 85 86 87 88 89 90 91 92 93
    QMatrix groupTransform = m_container->transformationMatrix(0).inverted();

    uint shapeCount = m_shapes.count();
    for( uint i = 0; i < shapeCount; ++i )
    {
        KoShape * shape = m_shapes[i];
        shape->applyTransformation( groupTransform );
        m_container->addChild(shape);
        m_container->setClipping( shape, m_clipped[i] );
    }
94 95
}

96
void KoShapeGroupCommand::undo () {
97
    QUndoCommand::undo();
98 99 100 101 102 103

    for(int i=0; i < m_shapes.count(); i++) {
        m_container->removeChild(m_shapes[i]);
        if( m_oldParents.at( i ) )
            m_oldParents.at( i )->addChild( m_shapes[i] );
    }
104 105 106
    QMatrix ungroupTransform = m_container->transformationMatrix(0);
    foreach(KoShape *shape, m_shapes)
        shape->applyTransformation( ungroupTransform );
107
}