KoShapeManager_p.h 3.97 KB
Newer Older
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
/* This file is part of the KDE project

   Copyright (C) 2006-2008 Thorsten Zachmann <zachmann@kde.org>
   Copyright (C) 2006-2010 Thomas Zander <zander@kde.org>
   Copyright (C) 2009-2010 Jan Hambrecht <jaham@gmx.net>

   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.
*/
#ifndef KoShapeManager_p_h
#define KoShapeManager_p_h

#include "KoSelection.h"
#include "KoShape.h"
#include "KoShape_p.h"
#include "KoShapeContainer.h"
29
#include "KoShapeManager.h"
30
#include <KoRTree.h>
31
#include <QMutex>
32

33

34 35 36 37
class KoCanvasBase;
class KoShapeGroup;
class KoShapePaintingContext;
class QPainter;
38

39
class Q_DECL_HIDDEN KoShapeManager::Private
40 41 42
{
public:
    Private(KoShapeManager *shapeManager, KoCanvasBase *c)
43
        : selection(new KoSelection(shapeManager)),
44 45
          canvas(c),
          tree(4, 2),
46
          q(shapeManager),
47
          shapeInterface(shapeManager)
48 49 50 51 52 53 54 55 56 57 58 59 60
    {
    }

    ~Private() {
        delete selection;
    }

    /**
     * Update the tree when there are shapes in m_aggregate4update. This is done so not all
     * updates to the tree are done when they are asked for but when they are needed.
     */
    void updateTree();

61 62 63 64 65 66
    /**
     * Returns whether the shape should be added to the RTree for collision and ROI
     * detection.
     */
    bool shapeUsedInRenderingTree(KoShape *shape);

67 68 69 70 71
    /**
     * Recursively detach the shapes from this shape manager
     */
    void unlinkFromShapesRecursively(const QList<KoShape *> &shapes);

72 73 74 75 76
    /**
     * Recursively paints the given group shape to the specified painter
     * This is needed for filter effects on group shapes where the filter effect
     * applies to all the children of the group shape at once
     */
77
    static void paintGroup(KoShapeGroup *group, QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &paintContext);
78 79 80 81 82 83

    class DetectCollision
    {
    public:
        DetectCollision() {}
        void detect(KoRTree<KoShape *> &tree, KoShape *s, int prevZIndex) {
84
            Q_FOREACH (KoShape *shape, tree.intersects(s->boundingRect())) {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
                bool isChild = false;
                KoShapeContainer *parent = s->parent();
                while (parent && !isChild) {
                    if (parent == shape)
                        isChild = true;
                    parent = parent->parent();
                }
                if (isChild)
                    continue;
                if (s->zIndex() <= shape->zIndex() && prevZIndex <= shape->zIndex())
                    // Moving a shape will only make it collide with shapes below it.
                    continue;
                if (shape->collisionDetection() && !shapesWithCollisionDetection.contains(shape))
                    shapesWithCollisionDetection.append(shape);
            }
        }

        void fireSignals() {
103
            Q_FOREACH (KoShape *shape, shapesWithCollisionDetection)
104
                shape->shapeChangedPriv(KoShape::CollisionDetected);
105 106 107 108 109 110 111 112 113 114 115 116 117
        }

    private:
        QList<KoShape*> shapesWithCollisionDetection;
    };

    QList<KoShape *> shapes;
    KoSelection *selection;
    KoCanvasBase *canvas;
    KoRTree<KoShape *> tree;
    QSet<KoShape *> aggregate4update;
    QHash<KoShape*, int> shapeIndexesBeforeUpdate;
    KoShapeManager *q;
118
    KoShapeManager::ShapeInterface shapeInterface;
119
    QMutex mutex;
120 121
};

122
#endif