libs/flake

KoShapeUngroupCommand.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 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 "KoShapeUngroupCommand.h"
00022 #include "KoShapeContainer.h"
00023 
00024 #include <klocale.h>
00025 
00026 KoShapeUngroupCommand::KoShapeUngroupCommand(KoShapeContainer *container, const QList<KoShape *> &shapes,
00027                                              const QList<KoShape*> &topLevelShapes, QUndoCommand *parent)
00028         : KoShapeGroupCommand(parent)
00029 {
00030     QList<KoShape*> orderdShapes(shapes);
00031     qSort(orderdShapes.begin(), orderdShapes.end(), KoShape::compareShapeZIndex);
00032     m_shapes = orderdShapes;
00033     m_container = container;
00034 
00035     QList<KoShape*> ancestors = m_container->parent()? m_container->parent()->childShapes(): topLevelShapes;
00036     qSort(ancestors.begin(), ancestors.end(), KoShape::compareShapeZIndex);
00037     QList<KoShape*>::const_iterator it(qFind(ancestors, m_container));
00038 
00039     Q_ASSERT(it != ancestors.constEnd());
00040     for ( ; it != ancestors.constEnd(); ++it ) {
00041         m_oldAncestorsZIndex.append(QPair<KoShape*, int>(*it, (*it)->zIndex()));
00042     }
00043 
00044     int zIndex = m_container->zIndex();
00045     foreach(KoShape *shape, m_shapes) {
00046         m_clipped.append(m_container->childClipped(shape));
00047         m_oldParents.append(m_container->parent());
00048         m_oldClipped.append(m_container->childClipped(shape));
00049         // TODO this might also need to change the childs of the parent but that is very problematic if the parent is 0
00050         m_oldZIndex.append(zIndex++);
00051     }
00052 
00053     setText(i18n("Ungroup shapes"));
00054 }
00055 
00056 void KoShapeUngroupCommand::redo()
00057 {
00058     KoShapeGroupCommand::undo();
00059     int zIndex = m_container->zIndex() + m_oldZIndex.count() - 1;
00060     for (QList<QPair<KoShape*, int> >::const_iterator it( m_oldAncestorsZIndex.constBegin()); it != m_oldAncestorsZIndex.constEnd(); ++it) {
00061         it->first->setZIndex(zIndex++);
00062     }
00063 }
00064 
00065 void KoShapeUngroupCommand::undo()
00066 {
00067     KoShapeGroupCommand::redo();
00068     for (QList<QPair<KoShape*, int> >::const_iterator it( m_oldAncestorsZIndex.constBegin()); it != m_oldAncestorsZIndex.constEnd(); ++it) {
00069         it->first->setZIndex(it->second);
00070     }
00071 }