KoShapeContainer.cpp 5.89 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 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
/* 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;
}

void KoShapeContainer::addChild(KoShape *object) {
    Q_ASSERT(object);
    if(m_children == 0)
        m_children = new ChildrenData();
    m_children->add(object);
    object->setParent(this);
}

void KoShapeContainer::removeChild(KoShape *object) {
    Q_ASSERT(object);
    if(m_children == 0)
        return;
    m_children->remove(object);
    object->setParent(0);
}

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);
}

void KoShapeContainer::paint(QPainter &painter, KoViewConverter &converter) {
    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));

87 88 89 90
        if(! childClipped(shape) )
            continue;
        QRectF clipRect(QPointF(0, 0), size());
        clipRect = converter.documentToView(clipRect);
Jan Hambrecht's avatar
Jan Hambrecht committed
91

92 93 94
        QPolygon clip = myMatrix.mapToPolygon(clipRect.toRect());
        clip.translate( (position() - converter.documentToView(position())).toPoint() );
        painter.setClipRegion(QRegion(clip));
95 96
//kDebug() << "rect: " << position() << endl;
//kDebug() << "polygon: " << clip.boundingRect() << endl;
Thomas Zander's avatar
Thomas Zander committed
97 98 99 100 101 102 103
        //painter.drawPolygon(clip);
        painter.setMatrix( shape->transformationMatrix(&converter) * painter.matrix() );
        shape->paint(painter, converter);
        painter.restore();
    }
}

104 105
void KoShapeContainer::shapeChanged(ChangeType type) {
    Q_UNUSED(type);
Thomas Zander's avatar
Thomas Zander committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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
125
    qDeleteAll(m_relations);
Thomas Zander's avatar
Thomas Zander committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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
}

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();
184 185 186
    if(m_children)
        foreach ( KoShape *shape, m_children->iterator())
            shape->repaint();
Thomas Zander's avatar
Thomas Zander committed
187 188 189 190 191 192 193 194 195 196 197
}

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) {
}