KoShapeContainer.cpp 6.92 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* This file is part of the KDE project
 * Copyright (C) 2006 Thomas Zander <zander@kde.org>
 *
 * 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"

#include <QPointF>
#include <QPainter>

KoShapeContainer::KoShapeContainer() : KoShape() {
    m_children = 0;
}

KoShapeContainer::KoShapeContainer(KoGraphicsContainerModel *model)
: KoShape()
, m_children(model) {
}

KoShapeContainer::~KoShapeContainer() {
    delete m_children;
}

38
v v v v v v v
39 40
void KoShapeContainer::addChild(KoShape *shape) {
    Q_ASSERT(shape);
41
^ ^ ^ ^ ^ ^ ^
Thomas Zander's avatar
Thomas Zander committed
42 43
    if(m_children == 0)
        m_children = new ChildrenData();
44
v v v v v v v
45 46 47
    m_children->add(shape);
    shape->setParent(this);
    childCountChanged();
48
^ ^ ^ ^ ^ ^ ^
Thomas Zander's avatar
Thomas Zander committed
49 50
}

51
v v v v v v v
52 53
void KoShapeContainer::removeChild(KoShape *shape) {
    Q_ASSERT(shape);
54
^ ^ ^ ^ ^ ^ ^
Thomas Zander's avatar
Thomas Zander committed
55 56
    if(m_children == 0)
        return;
57
v v v v v v v
58 59 60
    m_children->remove(shape);
    shape->setParent(0);
    childCountChanged();
61
^ ^ ^ ^ ^ ^ ^
Thomas Zander's avatar
Thomas Zander committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

int  KoShapeContainer::childCount() const {
    if(m_children == 0)
        return 0;
    return m_children->count();
}

void KoShapeContainer::setClipping(const KoShape *child, bool clipping) {
    if(m_children == 0)
        return;
    m_children->setClipping(child, clipping);
}

76
void KoShapeContainer::paint(QPainter &painter, const KoViewConverter &converter) {
Thomas Zander's avatar
Thomas Zander committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    painter.save();
    applyConversion(painter, converter);
    paintComponent(painter, converter);
    painter.restore();
    if(m_children == 0 || m_children->count() == 0)
        return;

    QList<KoShape*> sorterdObjects = m_children->iterator();
    qSort(sorterdObjects.begin(), sorterdObjects.end(), KoShape::compareShapeZIndex);
    painter.setMatrix( m_invMatrix * painter.matrix() );
    QMatrix myMatrix = transformationMatrix(&converter);
    foreach (KoShape *shape, sorterdObjects) {
        if(! shape->isVisible())
            continue;
        painter.save();
        // TODO this is not perfect yet..
//           QRectF clipRect(QPoint(0,0), size()); // old
//           QPolygon clip = (myMatrix * shapeMatrix.inverted()).mapToPolygon(clipRect.toRect());
//           painter.setClipRegion(QRegion(clip));

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
v v v v v v v
        if(! childClipped(shape) )
            continue;
        QRectF clipRect(QPointF(0, 0), size());
        clipRect = converter.documentToView(clipRect);

            QRectF clipRect(QPointF(0, 0), size());
            clipRect = converter.documentToView(clipRect);

        QPolygon clip = myMatrix.mapToPolygon(clipRect.toRect());
        clip.translate( (position() - converter.documentToView(position())).toPoint() );
        painter.setClipRegion(QRegion(clip));
*************
        if(! childClipped(shape) )
            continue;
        QRectF clipRect(QPointF(0, 0), size());
        clipRect = converter.documentToView(clipRect);

        QPolygon clip = myMatrix.mapToPolygon(clipRect.toRect());
        clip.translate( (position() - converter.documentToView(position())).toPoint() );
        painter.setClipRegion(QRegion(clip));
*************
119 120 121 122
        if(! childClipped(shape) )
            continue;
        QRectF clipRect(QPointF(0, 0), size());
        clipRect = converter.documentToView(clipRect);
Jan Hambrecht's avatar
Jan Hambrecht committed
123

124 125 126
        QPolygon clip = myMatrix.mapToPolygon(clipRect.toRect());
        clip.translate( (position() - converter.documentToView(position())).toPoint() );
        painter.setClipRegion(QRegion(clip));
127
^ ^ ^ ^ ^ ^ ^
128 129
//kDebug() << "rect: " << position() << endl;
//kDebug() << "polygon: " << clip.boundingRect() << endl;
Thomas Zander's avatar
Thomas Zander committed
130 131 132 133 134 135 136
        //painter.drawPolygon(clip);
        painter.setMatrix( shape->transformationMatrix(&converter) * painter.matrix() );
        shape->paint(painter, converter);
        painter.restore();
    }
}

137 138
void KoShapeContainer::shapeChanged(ChangeType type) {
    Q_UNUSED(type);
Thomas Zander's avatar
Thomas Zander committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if(m_children == 0)
        return;
    m_children->containerChanged(this);
    foreach (KoShape *shape, m_children->iterator())
        shape->recalcMatrix();
}

bool KoShapeContainer::childClipped(const KoShape *child) const {
    if(m_children == 0) // throw exception??
        return false;
    return m_children->childClipped(child);
}


// ##  inner class ChildrenData
KoShapeContainer::ChildrenData::ChildrenData() {
}

KoShapeContainer::ChildrenData::~ChildrenData() {
Thomas Zander's avatar
Thomas Zander committed
158
    qDeleteAll(m_relations);
Thomas Zander's avatar
Thomas Zander committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

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

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

void KoShapeContainer::ChildrenData::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;
    relation->child()->repaint();
    relation->child()->recalcMatrix();
    relation->child()->repaint();
}

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

int KoShapeContainer::ChildrenData::count() const {
    return m_relations.count();
}

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

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

void KoShapeContainer::ChildrenData::containerChanged(KoShapeContainer *container) {
    Q_UNUSED(container);
}

void KoShapeContainer::repaint() const {
    KoShape::repaint();
217 218 219
    if(m_children)
        foreach ( KoShape *shape, m_children->iterator())
            shape->repaint();
Thomas Zander's avatar
Thomas Zander committed
220 221 222 223 224 225 226 227 228 229 230
}

QList<KoShape*> KoShapeContainer::iterator() const {
    return m_children->iterator();
}

// ## inner class KoGraphicsContainerModel::Relation
KoShapeContainer::ChildrenData::Relation::Relation(KoShape *child)
:m_inside(false)
, m_child(child) {
}