KoShapeContainer.cpp 7.52 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1
/* This file is part of the KDE project
2
 * Copyright (C) 2006-2007 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
 *
 * 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 "KoShapeContainer.h"
21
#include "KoShapeContainerModel.h"
22
#include "KoShapeBorderModel.h"
Thomas Zander's avatar
Thomas Zander committed
23 24 25

#include <QPointF>
#include <QPainter>
26
#include <QPainterPath>
Thomas Zander's avatar
Thomas Zander committed
27

Thomas Zander's avatar
Thomas Zander committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class ChildrenData : public KoShapeContainerModel {
public:
    ChildrenData() {}
    ~ChildrenData() {
        qDeleteAll(m_relations);
    }

    void add(KoShape *child) {
        Relation *r = new Relation(child);
        m_relations.append(r);
    }

    void setClipping(const KoShape *child, bool clipping) {
        Relation *relation = findRelation(child);
        if(relation == 0) // throw exception?
            return;
        if(relation->m_inside == clipping)
            return;
        relation->m_inside = clipping;
47
        relation->child()->update();
48
        relation->child()->notifyChanged();
49
        relation->child()->update();
Thomas Zander's avatar
Thomas Zander committed
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
    }

    bool childClipped(const KoShape *child) const {
        Relation *relation = findRelation(child);
        if(relation == 0) // throw exception?
            return false;
        return relation->m_inside;
    }

    void remove(KoShape *child) {
        Relation *relation = findRelation(child);
        if(relation == 0)
            return;
        m_relations.removeAll(relation);
    }

    int count() const {
        return m_relations.count();
    }

    QList<KoShape*> iterator() const {
        QList<KoShape*> answer;
        foreach (Relation *relation, m_relations)
            answer.append(relation->child());
        return answer;
    }

77 78 79 80
    bool isChildLocked(const KoShape *child) const {
        return child->isLocked();
    }

Thomas Zander's avatar
Thomas Zander committed
81
    void containerChanged(KoShapeContainer *) { }
82
    void childChanged(KoShape *, KoShape::ChangeType ) { }
Thomas Zander's avatar
Thomas Zander committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

private:
    /**
     * This class is a simple data-storage class for Relation objects.
     */
    class Relation {
        public:
            explicit Relation(KoShape *child) :m_inside(false) , m_child(child) { }
            KoShape* child() { return m_child; }
            bool m_inside; ///< if true, the child will be clipped by the parent.
        private:
            KoShape *m_child;
    };

    Relation* findRelation(const KoShape *child) const {
        foreach(Relation *relation, m_relations) {
            if(relation->child() == child)
                return relation;
        }
        return 0;
    }

private: // members
    QList <Relation *> m_relations;
};

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
class KoShapeContainer::Private {
public:
    Private() : children(0) {}
    ~Private() {
        if(children)
        {
            foreach (KoShape *shape, children->iterator())
                shape->setParent(0);
            delete children;
        }
    }
    KoShapeContainerModel *children;
};

KoShapeContainer::KoShapeContainer() : KoShape(), d(new Private()) {
Thomas Zander's avatar
Thomas Zander committed
124 125
}

126
KoShapeContainer::KoShapeContainer(KoShapeContainerModel *model)
127 128 129 130
: KoShape(),
 d(new Private())
{
    d->children = model;
Thomas Zander's avatar
Thomas Zander committed
131 132 133
}

KoShapeContainer::~KoShapeContainer() {
134
    delete d;
Thomas Zander's avatar
Thomas Zander committed
135 136
}

137 138
void KoShapeContainer::addChild(KoShape *shape) {
    Q_ASSERT(shape);
139
    if(shape->parent() == this && iterator().contains( shape ) )
140
        return;
141 142
    if(d->children == 0)
        d->children = new ChildrenData();
143
    if( shape->parent() && shape->parent() != this)
144
        shape->parent()->removeChild( shape );
145
    d->children->add(shape);
146 147
    shape->setParent(this);
    childCountChanged();
Thomas Zander's avatar
Thomas Zander committed
148 149
}

150 151
void KoShapeContainer::removeChild(KoShape *shape) {
    Q_ASSERT(shape);
152
    if(d->children == 0)
Thomas Zander's avatar
Thomas Zander committed
153
        return;
154
    d->children->remove(shape);
155 156
    shape->setParent(0);
    childCountChanged();
Thomas Zander's avatar
Thomas Zander committed
157 158 159
}

int  KoShapeContainer::childCount() const {
160
    if(d->children == 0)
Thomas Zander's avatar
Thomas Zander committed
161
        return 0;
162
    return d->children->count();
Thomas Zander's avatar
Thomas Zander committed
163 164
}

165 166 167 168 169 170
bool KoShapeContainer::isChildLocked(const KoShape *child) const {
    if(d->children == 0)
        return false;
    return d->children->isChildLocked(child);
}

Thomas Zander's avatar
Thomas Zander committed
171
void KoShapeContainer::setClipping(const KoShape *child, bool clipping) {
172
    if(d->children == 0)
Thomas Zander's avatar
Thomas Zander committed
173
        return;
174
    d->children->setClipping(child, clipping);
Thomas Zander's avatar
Thomas Zander committed
175 176
}

177
void KoShapeContainer::paint(QPainter &painter, const KoViewConverter &converter) {
Thomas Zander's avatar
Thomas Zander committed
178 179 180
    painter.save();
    paintComponent(painter, converter);
    painter.restore();
181
    if(d->children == 0 || d->children->count() == 0)
Thomas Zander's avatar
Thomas Zander committed
182 183
        return;

184
    QList<KoShape*> sortedObjects = d->children->iterator();
185
    qSort(sortedObjects.begin(), sortedObjects.end(), KoShape::compareShapeZIndex);
186

187
    QMatrix baseMatrix = absoluteTransformation(0).inverted() * painter.matrix();
188 189 190 191 192 193 194 195

    // clip the children to the parent outline.
    QMatrix m;
    double zoomX, zoomY;
    converter.zoom(&zoomX, &zoomY);
    m.scale(zoomX, zoomY);
    painter.setClipPath(m.map(outline()));

196
    foreach (KoShape *shape, sortedObjects) {
197
        //kDebug(30006) <<"KoShapeContainer::painting shape:" << shape->shapeId() <<"," << shape->boundingRect();
Thomas Zander's avatar
Thomas Zander committed
198 199
        if(! shape->isVisible())
            continue;
200 201 202 203 204 205 206 207
        if(! childClipped(shape) ) // the shapeManager will have to draw those, or else we can't do clipRects
            return;
        if(painter.hasClipping()) {
            QRectF rect = converter.viewToDocument( painter.clipRegion().boundingRect() );
            rect = matrix().mapRect(rect);
            // don't try to draw a child shape that is not in the clipping rect of the painter.
            if(! rect.intersects(shape->boundingRect()))
                continue;
208 209
        }

210
        painter.save();
211
        painter.setMatrix( shape->absoluteTransformation(&converter) * baseMatrix );
Thomas Zander's avatar
Thomas Zander committed
212 213
        shape->paint(painter, converter);
        painter.restore();
214 215
        if(shape->border()) {
            painter.save();
216
            painter.setMatrix( shape->absoluteTransformation(&converter) * baseMatrix );
217 218 219
            shape->border()->paintBorder(shape, painter, converter);
            painter.restore();
        }
Thomas Zander's avatar
Thomas Zander committed
220 221 222
    }
}

223
void KoShapeContainer::shapeChanged(ChangeType type) {
224
    if(d->children == 0)
Thomas Zander's avatar
Thomas Zander committed
225
        return;
226 227
    if(! (type == RotationChanged || type == ScaleChanged || type == ShearChanged
                || type == SizeChanged || type == PositionChanged))
Thomas Zander's avatar
Thomas Zander committed
228
        return;
229 230
    d->children->containerChanged(this);
    foreach (KoShape *shape, d->children->iterator())
231
        shape->notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
232 233 234
}

bool KoShapeContainer::childClipped(const KoShape *child) const {
235
    if(d->children == 0) // throw exception??
Thomas Zander's avatar
Thomas Zander committed
236
        return false;
237
    return d->children->childClipped(child);
Thomas Zander's avatar
Thomas Zander committed
238 239 240
}

void KoShapeContainer::repaint() const {
241
    KoShape::update();
242 243
    if(d->children)
        foreach ( KoShape *shape, d->children->iterator())
244
            shape->update();
Thomas Zander's avatar
Thomas Zander committed
245 246 247
}

QList<KoShape*> KoShapeContainer::iterator() const {
248
    if(d->children == 0)
249 250
        return QList<KoShape*>();

251
    return d->children->iterator();
Thomas Zander's avatar
Thomas Zander committed
252 253
}

Thomas Zander's avatar
Thomas Zander committed
254 255 256 257
KoShapeContainerModel *KoShapeContainer::model() const {
    return d->children;
}