libs/flake

KoShapeGroup.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) 2007 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 "KoShapeGroup.h"
00022 #include "KoShapeContainerModel.h"
00023 #include "KoShapeLayer.h"
00024 #include "SimpleShapeContainerModel.h"
00025 #include "KoShapeSavingContext.h"
00026 #include "KoShapeLoadingContext.h"
00027 #include "KoXmlWriter.h"
00028 #include "KoXmlReader.h"
00029 #include "KoShapeRegistry.h"
00030 #include "KoShapeBorderModel.h"
00031 #include "KoShapeShadow.h"
00032 
00033 #include <QPainter>
00034 
00035 KoShapeGroup::KoShapeGroup()
00036         : KoShapeContainer(new SimpleShapeContainerModel())
00037 {
00038     setSize(QSizeF(0, 0));
00039 }
00040 
00041 void KoShapeGroup::paintComponent(QPainter &painter, const KoViewConverter &converter)
00042 {
00043     Q_UNUSED(painter);
00044     Q_UNUSED(converter);
00045 }
00046 
00047 bool KoShapeGroup::hitTest(const QPointF &position) const
00048 {
00049     Q_UNUSED(position);
00050     return false;
00051 }
00052 
00053 void KoShapeGroup::childCountChanged()
00054 {
00055     // TODO: why is this needed here ? the group/ungroup command should take care of this
00056     QRectF br = boundingRect();
00057     setAbsolutePosition(br.topLeft(), KoFlake::TopLeftCorner);
00058     setSize(br.size());
00059 }
00060 
00061 void KoShapeGroup::saveOdf(KoShapeSavingContext & context) const
00062 {
00063     context.xmlWriter().startElement("draw:g");
00064     saveOdfAttributes(context, (OdfMandatories ^ OdfLayer) | OdfAdditionalAttributes);
00065     context.xmlWriter().addAttributePt("svg:y", position().y());
00066 
00067     QList<KoShape*> shapes = childShapes();
00068     qSort(shapes.begin(), shapes.end(), KoShape::compareShapeZIndex);
00069 
00070     foreach(KoShape* shape, shapes) {
00071         shape->saveOdf(context);
00072     }
00073 
00074     saveOdfCommonChildElements(context);
00075     context.xmlWriter().endElement();
00076 }
00077 
00078 bool KoShapeGroup::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &context)
00079 {
00080     loadOdfAttributes(element, context, OdfMandatories | OdfAdditionalAttributes | OdfCommonChildElements);
00081 
00082     KoXmlElement child;
00083     QMap<KoShapeLayer*, int> usedLayers;
00084     forEachElement(child, element) {
00085         KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf(child, context);
00086         KoShapeLayer *layer = dynamic_cast<KoShapeLayer*>(shape->parent());
00087         if (layer) {
00088             usedLayers[layer]++;
00089         }
00090         if (shape) {
00091             addChild(shape);
00092         }
00093     }
00094     KoShapeLayer *parent = 0;
00095     int maxUseCount = 0;
00096     // find most used layer and use this as parent for the group
00097     for (QMap<KoShapeLayer*, int>::const_iterator it(usedLayers.constBegin()); it != usedLayers.constEnd(); ++it) {
00098         if (it.value() > maxUseCount) {
00099             maxUseCount = it.value();
00100             parent = it.key();
00101         }
00102     }
00103     setParent(parent);
00104 
00105     QRectF bound;
00106     bool boundInitialized = false;
00107     foreach(KoShape * shape, childShapes()) {
00108         if (! boundInitialized) {
00109             bound = shape->boundingRect();
00110             boundInitialized = true;
00111         } else
00112             bound = bound.united(shape->boundingRect());
00113     }
00114 
00115     setSize(bound.size());
00116     setPosition(bound.topLeft());
00117 
00118     foreach(KoShape * shape, childShapes())
00119         shape->setAbsolutePosition(shape->absolutePosition() - bound.topLeft());
00120 
00121     return true;
00122 }
00123 
00124 void KoShapeGroup::shapeChanged(ChangeType type, KoShape *shape)
00125 {
00126     Q_UNUSED(shape);
00127     switch( type )
00128     {
00129         case KoShape::BorderChanged:
00130         {
00131             KoShapeBorderModel * stroke = border();
00132             if( stroke ) {
00133                 if( stroke->removeUser() )
00134                     delete stroke;
00135                 setBorder(0);
00136             }
00137             break;
00138         }
00139         case KoShape::ShadowChanged:
00140         {
00141             KoShapeShadow * shade = shadow();
00142             if( shade ) {
00143                 if( shade->removeUser() )
00144                     delete shade;
00145                 setShadow(0);
00146             }
00147             break;
00148         }
00149         default:
00150             break;
00151     }
00152 }