KoShapeGroup.cpp 6.99 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1 2
/* This file is part of the KDE project
 * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3
 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
Thomas Zander's avatar
Thomas Zander committed
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 "KoShapeGroup.h"
22
#include "KoShapeContainerModel.h"
23
#include "KoShapeContainer_p.h"
24
#include "KoShapeLayer.h"
25
#include "SimpleShapeContainerModel.h"
26
#include "KoShapeSavingContext.h"
27
#include "KoShapeLoadingContext.h"
28
#include "KoXmlWriter.h"
29 30
#include "KoXmlReader.h"
#include "KoShapeRegistry.h"
31
#include "KoShapeStrokeModel.h"
32
#include "KoShapeShadow.h"
Thomas Zander's avatar
Thomas Zander committed
33

Jarosław Staniek's avatar
Jarosław Staniek committed
34 35
#include <QPainter>

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
class ShapeGroupContainerModel : public SimpleShapeContainerModel
{
public:
    ShapeGroupContainerModel(KoShapeGroup *group) : m_group(group) {}
    ~ShapeGroupContainerModel() {}

    virtual void add(KoShape *child)
    {
        SimpleShapeContainerModel::add(child);
        m_group->invalidateSizeCache();
    }

    virtual void remove(KoShape *child)
    {
        SimpleShapeContainerModel::remove(child);
        m_group->invalidateSizeCache();
    }

    virtual void childChanged(KoShape *shape, KoShape::ChangeType type)
    {
        SimpleShapeContainerModel::childChanged(shape, type);
        //kDebug(30006) << type;
        switch (type) {
        case KoShape::PositionChanged:
        case KoShape::RotationChanged:
        case KoShape::ScaleChanged:
        case KoShape::ShearChanged:
        case KoShape::SizeChanged:
        case KoShape::GenericMatrixChange:
        case KoShape::ParameterChanged:
        case KoShape::ClipPathChanged :
            m_group->invalidateSizeCache();
            break;
        default:
            break;
        }
    }

private: // members
    KoShapeGroup * m_group;
};

class KoShapeGroupPrivate : public KoShapeContainerPrivate
{
public:
    KoShapeGroupPrivate(KoShapeGroup *q)
    : KoShapeContainerPrivate(q)
    {
        model = new ShapeGroupContainerModel(q);
    }

    ~KoShapeGroupPrivate()
    {
    }

    mutable bool sizeCached;
};

Thomas Zander's avatar
Thomas Zander committed
94
KoShapeGroup::KoShapeGroup()
95
        : KoShapeContainer(*(new KoShapeGroupPrivate(this)))
Thomas Zander's avatar
Thomas Zander committed
96
{
97
    setSize(QSizeF(0, 0));
Thomas Zander's avatar
Thomas Zander committed
98 99
}

100 101 102 103
KoShapeGroup::~KoShapeGroup()
{
}

104
void KoShapeGroup::paintComponent(QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &)
105
{
Thomas Zander's avatar
Thomas Zander committed
106 107 108 109
    Q_UNUSED(painter);
    Q_UNUSED(converter);
}

110
bool KoShapeGroup::hitTest(const QPointF &position) const
111
{
Thomas Zander's avatar
Thomas Zander committed
112 113 114 115
    Q_UNUSED(position);
    return false;
}

116 117
QSizeF KoShapeGroup::size() const
{
118 119 120 121 122 123 124 125 126 127 128 129 130
    Q_D(const KoShapeGroup);
    //kDebug(30006) << "size" << d->size;
    if (!d->sizeCached) {
        QRectF bound;
        foreach(KoShape *shape, shapes()) {
            if (bound.isEmpty())
                bound = shape->transformation().mapRect(shape->outlineRect());
            else
                bound |= shape->transformation().mapRect(shape->outlineRect());
        }
        d->size = bound.size();
        d->sizeCached = true;
        kDebug(30006) << "recalculated size" << d->size;
131
    }
132 133

    return d->size;
Yue Liu's avatar
Yue Liu committed
134 135
}

136 137
QRectF KoShapeGroup::boundingRect() const
{
138 139
    bool first = true;
    QRectF groupBound;
140
    QList<KoShape*> shapes = this->shapes();
141
    QList<KoShape*>::const_iterator it = shapes.constBegin();
142
    for (; it != shapes.constEnd(); ++it) {
143 144 145 146 147 148 149 150
        const QTransform shapeTransform = (*it)->absoluteTransformation(0);
        const QRectF shapeRect(QRectF(QPointF(), (*it)->boundingRect().size()));
        if (first) {
            groupBound = shapeTransform.mapRect(shapeRect);
            first = false;
        } else {
            groupBound = groupBound.united(shapeTransform.mapRect(shapeRect));
        }
151
    }
152

153 154 155
    if (this->shadow()) {
        KoInsets insets;
        this->shadow()->insets(insets);
156
        groupBound.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
157
    }
158
    return groupBound;
159 160
}

161
void KoShapeGroup::saveOdf(KoShapeSavingContext & context) const
162
{
163
    context.xmlWriter().startElement("draw:g");
164
    saveOdfAttributes(context, (OdfMandatories ^ (OdfLayer | OdfZIndex)) | OdfAdditionalAttributes);
165
    context.xmlWriter().addAttributePt("svg:y", position().y());
166

Thomas Zander's avatar
Thomas Zander committed
167
    QList<KoShape*> shapes = this->shapes();
168
    qSort(shapes.begin(), shapes.end(), KoShape::compareShapeZIndex);
169

170
    foreach(KoShape* shape, shapes) {
171
        shape->saveOdf(context);
172
    }
173

174
    saveOdfCommonChildElements(context);
175
    context.xmlWriter().endElement();
176
}
177

178
bool KoShapeGroup::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &context)
179
{
180
    Q_D(KoShapeGroup);
181
    loadOdfAttributes(element, context, OdfMandatories | OdfStyle | OdfAdditionalAttributes | OdfCommonChildElements);
182

183
    KoXmlElement child;
184
    QMap<KoShapeLayer*, int> usedLayers;
185 186 187
    forEachElement(child, element) {
        KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf(child, context);
        if (shape) {
188 189 190 191
            KoShapeLayer *layer = dynamic_cast<KoShapeLayer*>(shape->parent());
            if (layer) {
                usedLayers[layer]++;
            }
Thomas Zander's avatar
Thomas Zander committed
192
            addShape(shape);
193 194
        }
    }
195 196 197 198 199 200 201 202 203 204
    KoShapeLayer *parent = 0;
    int maxUseCount = 0;
    // find most used layer and use this as parent for the group
    for (QMap<KoShapeLayer*, int>::const_iterator it(usedLayers.constBegin()); it != usedLayers.constEnd(); ++it) {
        if (it.value() > maxUseCount) {
            maxUseCount = it.value();
            parent = it.key();
        }
    }
    setParent(parent);
205 206 207

    QRectF bound;
    bool boundInitialized = false;
Thomas Zander's avatar
Thomas Zander committed
208
    foreach(KoShape * shape, shapes()) {
209
        if (! boundInitialized) {
210
            bound = shape->boundingRect();
Thomas Zander's avatar
Thomas Zander committed
211
            boundInitialized = true;
212 213
        } else
            bound = bound.united(shape->boundingRect());
214 215
    }

216
    setSize(bound.size());
217
    d->sizeCached = true;
218
    setPosition(bound.topLeft());
219

Thomas Zander's avatar
Thomas Zander committed
220
    foreach(KoShape * shape, shapes())
Jan Hambrecht's avatar
Jan Hambrecht committed
221
        shape->setAbsolutePosition(shape->absolutePosition() - bound.topLeft());
222 223

    return true;
224
}
225

226
void KoShapeGroup::shapeChanged(ChangeType type, KoShape *shape)
227
{
228
    Q_UNUSED(shape);
229
    KoShapeContainer::shapeChanged(type, shape);
230
    switch (type) {
231
    case KoShape::StrokeChanged:
232
    {
233 234 235 236 237
        KoShapeStrokeModel *str = stroke();
        if (str) {
            if (str->deref())
                delete str;
            setStroke(0);
238
        }
239 240 241 242
        break;
    }
    default:
        break;
243 244
    }
}
245 246 247 248 249 250 251

void KoShapeGroup::invalidateSizeCache()
{
    Q_D(KoShapeGroup);
    d->sizeCached = false;
}