KoShapeManager.cpp 9.69 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
/* This file is part of the KDE project

   Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
   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 "KoShapeManager.h"
#include "KoSelection.h"
#include "KoShape.h"
#include "KoCanvasBase.h"
#include "KoShapeContainer.h"
#include "KoShapeBorderModel.h"
28
#include "KoShapeGroup.h"
29
#include "KoToolProxy.h"
Thomas Zander's avatar
Thomas Zander committed
30 31 32

#include <QPainter>

Thomas Zander's avatar
Thomas Zander committed
33
KoShapeManager::KoShapeManager( KoCanvasBase *canvas, const QList<KoShape *> &shapes )
Thomas Zander's avatar
Thomas Zander committed
34
: m_selection( new KoSelection() )
Thomas Zander's avatar
Thomas Zander committed
35
, m_canvas( canvas )
36
, m_tree( 4, 2 )
Thomas Zander's avatar
Thomas Zander committed
37
{
Thomas Zander's avatar
Thomas Zander committed
38
    Q_ASSERT(m_canvas); // not optional.
Thomas Zander's avatar
Thomas Zander committed
39
    connect( m_selection, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()) );
Thomas Zander's avatar
Thomas Zander committed
40
    setShapes(shapes);
41
    m_selection->addShapeManager( this );
Thomas Zander's avatar
Thomas Zander committed
42 43 44
}

KoShapeManager::KoShapeManager(KoCanvasBase *canvas)
Thomas Zander's avatar
Thomas Zander committed
45
: m_shapes()
Thomas Zander's avatar
Thomas Zander committed
46
, m_selection( new KoSelection() )
Thomas Zander's avatar
Thomas Zander committed
47
, m_canvas( canvas )
48
, m_tree( 4, 2 )
Thomas Zander's avatar
Thomas Zander committed
49
{
Thomas Zander's avatar
Thomas Zander committed
50
    Q_ASSERT(m_canvas); // not optional.
Thomas Zander's avatar
Thomas Zander committed
51 52 53 54 55
    connect( m_selection, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()) );
}

KoShapeManager::~KoShapeManager()
{
56 57
    foreach(KoShape *shape, m_shapes)
        shape->removeShapeManager( this );
Thomas Zander's avatar
Thomas Zander committed
58 59 60 61
    delete m_selection;
}


Thomas Zander's avatar
Thomas Zander committed
62
void KoShapeManager::setShapes( const QList<KoShape *> &shapes )
Thomas Zander's avatar
Thomas Zander committed
63
{
64 65
    //clear selection
    m_selection->deselectAll();
66 67 68 69 70 71
    foreach(KoShape *shape, m_shapes)
    {
        m_aggregate4update.remove( shape );
        m_tree.remove( shape );
        shape->removeShapeManager( this );
    }
72 73
    m_shapes.clear();
    foreach(KoShape *shape, shapes)
74 75 76
    {
        add( shape );
    }
Thomas Zander's avatar
Thomas Zander committed
77 78
}

79 80
void KoShapeManager::add( KoShape *shape )
{
81 82
    if(m_shapes.contains(shape))
        return;
83
    shape->addShapeManager( this );
Thomas Zander's avatar
Thomas Zander committed
84
    m_shapes.append(shape);
85 86 87
    if( ! dynamic_cast<KoShapeGroup*>( shape ))
    {
        QRectF br( shape->boundingRect() );
88
        m_tree.insert( br, shape );
89
    }
90
    shape->repaint();
91 92 93 94 95 96 97 98 99 100 101

    // add the children of a KoShapeContainer
    KoShapeContainer* container = dynamic_cast<KoShapeContainer*>(shape);

    if(container)
    {
        foreach(KoShape* containerShape, container->iterator())
        {
            add(containerShape);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
102 103
}

104 105
void KoShapeManager::remove( KoShape *shape )
{
106
    shape->repaint();
107
    shape->removeShapeManager( this );
108
    m_selection->deselect( shape );
109 110
    m_aggregate4update.remove( shape );
    m_tree.remove( shape );
Thomas Zander's avatar
Thomas Zander committed
111
    m_shapes.removeAll(shape);
112 113 114 115 116 117 118 119 120 121 122

    // remove the children of a KoShapeContainer
    KoShapeContainer* container = dynamic_cast<KoShapeContainer*>(shape);

    if(container)
    {
        foreach(KoShape* containerShape, container->iterator())
        {
            remove(containerShape);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
123 124
}

125
void KoShapeManager::paint( QPainter &painter, const KoViewConverter &converter, bool forPrint)
Thomas Zander's avatar
Thomas Zander committed
126
{
127
    updateTree();
128 129
    painter.setPen( Qt::NoPen );// painters by default have a black stroke, lets turn that off.
    painter.setBrush( Qt::NoBrush );
Thomas Zander's avatar
Thomas Zander committed
130 131
    QList<KoShape*> sortedShapes( m_tree.intersects( converter.viewToDocument( painter.clipRegion().boundingRect() ) ) );
    qSort(sortedShapes.begin(), sortedShapes.end(), KoShape::compareShapeZIndex);
Thomas Zander's avatar
Thomas Zander committed
132
    const QRegion clipRegion = painter.clipRegion();
133

Thomas Zander's avatar
Thomas Zander committed
134
    foreach ( KoShape * shape, sortedShapes ) {
135
        if(! shape->isVisible() || ( shape->parent() && ! shape->parent()->isVisible() ) )
Thomas Zander's avatar
Thomas Zander committed
136 137 138 139
            continue;
        if(shape->parent() != 0 && shape->parent()->childClipped(shape))
            continue;
        if(painter.hasClipping()) {
Thomas Zander's avatar
Thomas Zander committed
140
            QRectF shapeBox = shape->boundingRect();
141
            shapeBox = converter.documentToView(shapeBox);
Thomas Zander's avatar
Thomas Zander committed
142
            QRegion shapeRegion = QRegion(shapeBox.toRect());
Thomas Zander's avatar
Thomas Zander committed
143

Thomas Zander's avatar
Thomas Zander committed
144
            if(clipRegion.intersect(shapeRegion).isEmpty())
Thomas Zander's avatar
Thomas Zander committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                continue;
        }
        painter.save();
        painter.setMatrix( shape->transformationMatrix(&converter) * painter.matrix() );

        painter.save();
        shape->paint( painter, converter );
        painter.restore();
        if(shape->border()) {
            painter.save();
            shape->border()->paintBorder(shape, painter, converter);
            painter.restore();
        }
        if(! forPrint) {
            painter.save();
            painter.setRenderHint( QPainter::Antialiasing, false );
            shape->paintDecorations( painter, converter, m_selection->isSelected(shape) );
            painter.restore();
        }
        painter.restore();  // for the matrix
    }

167
#ifdef KOFFICE_RTREE_DEBUG
168 169 170 171 172 173 174 175 176 177
    // paint tree
    double zx = 0;
    double zy = 0;
    converter.zoom( &zx, &zy );
    painter.save();
    painter.scale( zx, zy );
    m_tree.paint( painter );
    painter.restore();
#endif

Thomas Zander's avatar
Thomas Zander committed
178 179 180 181
    if(! forPrint)
        m_selection->paint( painter, converter );
}

Thomas Zander's avatar
Thomas Zander committed
182
KoShape * KoShapeManager::shapeAt( const QPointF &position, KoFlake::ShapeSelection selection, bool omitHiddenShapes )
Thomas Zander's avatar
Thomas Zander committed
183
{
184
    updateTree();
Thomas Zander's avatar
Thomas Zander committed
185 186
    QList<KoShape*> sortedShapes( m_tree.contains( position ) );
    qSort(sortedShapes.begin(), sortedShapes.end(), KoShape::compareShapeZIndex);
Thomas Zander's avatar
Thomas Zander committed
187
    KoShape *firstUnselectedShape = 0;
Thomas Zander's avatar
Thomas Zander committed
188 189
    for(int count = sortedShapes.count()-1; count >= 0; count--) {
        KoShape *shape = sortedShapes.at(count);
190
        if ( omitHiddenShapes && ! shape->isVisible() )
191
            continue;
192 193 194 195
        if ( ! shape->hitTest( position ) )
            continue;

        switch ( selection )
Thomas Zander's avatar
Thomas Zander committed
196
        {
Thomas Zander's avatar
Thomas Zander committed
197
            case KoFlake::ShapeOnTop:
198
                return shape;
Thomas Zander's avatar
Thomas Zander committed
199
            case KoFlake::Selected:
200 201 202
                if ( m_selection->isSelected( shape ) )
                    return shape;
                break;
Thomas Zander's avatar
Thomas Zander committed
203
            case KoFlake::Unselected:
204 205 206
                if ( ! m_selection->isSelected( shape ) )
                    return shape;
                break;
Thomas Zander's avatar
Thomas Zander committed
207
            case KoFlake::NextUnselected:
208 209 210 211
                // we want an unselected shape
                if ( m_selection->isSelected( shape ) )
                    continue;
                // memorize the first unselected shape
Thomas Zander's avatar
Thomas Zander committed
212
                if( ! firstUnselectedShape )
213 214
                    firstUnselectedShape = shape;
                // check if the shape above is selected
Thomas Zander's avatar
Thomas Zander committed
215
                if( count + 1 < sortedShapes.count() && m_selection->isSelected( sortedShapes.at(count + 1) ) )
216 217
                    return shape;
                break;
Thomas Zander's avatar
Thomas Zander committed
218 219
        }
    }
220 221
    // if we want the next unselected below a selected but there was none selected, 
    // return the first found unselected shape
Thomas Zander's avatar
Thomas Zander committed
222
    if( selection == KoFlake::NextUnselected && firstUnselectedShape )
223 224
        return firstUnselectedShape;

Thomas Zander's avatar
Thomas Zander committed
225 226 227 228 229
    if ( m_selection->hitTest( position ) )
        return m_selection;

    return 0; // missed everything
}
Laurent Montel's avatar
Laurent Montel committed
230

231
QList<KoShape *> KoShapeManager::shapesAt( const QRectF &rect, bool omitHiddenShapes )
232 233
{
    updateTree();
Thomas Zander's avatar
Thomas Zander committed
234 235
    //TODO check if object (outline) is really in the rect and we are not just
    // adding objects by their bounding rect
236 237 238 239 240 241 242 243 244 245 246
    if( omitHiddenShapes ) {
        QList<KoShape*> intersectedShapes( m_tree.intersects( rect ) );
        for(int count = intersectedShapes.count()-1; count >= 0; count--) {
            KoShape *shape = intersectedShapes.at( count );
            if( ! shape->isVisible() )
                intersectedShapes.removeAt( count );
        }
        return intersectedShapes;
    }
    else
        return m_tree.intersects( rect );
247 248 249 250 251 252 253
}

void KoShapeManager::repaint( QRectF &rect, const KoShape *shape, bool selectionHandles )
{
    m_canvas->updateCanvas( rect );
    if ( selectionHandles && m_selection->isSelected( shape ) )
    {
254 255
        if ( m_canvas->toolProxy() )
            m_canvas->toolProxy()->repaintDecorations();
256 257 258
    }
}

Thomas Zander's avatar
Thomas Zander committed
259
void KoShapeManager::notifyShapeChanged( KoShape * shape )
260 261 262 263 264 265
{
    m_aggregate4update.insert( shape );
}

void KoShapeManager::updateTree()
{
Thomas Zander's avatar
Thomas Zander committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    // for detecting collisions between shapes.
    class DetectCollision {
      public:
        DetectCollision() {}
        void detect(KoRTree<KoShape *> &m_tree, KoShape *s) {
            foreach(KoShape *shape, m_tree.intersects( s->boundingRect() )) {
                if(s->zIndex() <= 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() {
            foreach(KoShape *shape, shapesWithCollisionDetection)
                shape->shapeChanged(KoShape::CollisionDetected);
        }

      private:
        QList<KoShape*> shapesWithCollisionDetection;
    };
    DetectCollision detector;
    foreach ( KoShape *shape, m_aggregate4update )
        detector.detect(m_tree, shape);

292 293 294 295
    foreach ( KoShape * shape, m_aggregate4update )
    {
        m_tree.remove( shape );
        QRectF br( shape->boundingRect() );
296
        m_tree.insert( br, shape );
297
    }
Thomas Zander's avatar
Thomas Zander committed
298 299 300 301

    // do it again to see which shapes we intersect with _after_ moving.
    foreach ( KoShape *shape, m_aggregate4update )
        detector.detect(m_tree, shape);
302
    m_aggregate4update.clear();
Thomas Zander's avatar
Thomas Zander committed
303 304

    detector.fireSignals();
305 306
}

Laurent Montel's avatar
Laurent Montel committed
307
#include "KoShapeManager.moc"