KoShape.cpp 41.6 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1
/* This file is part of the KDE project
Thomas Zander's avatar
Thomas Zander committed
2
   Copyright (C) 2006 Casper Boemann Rasmussen <cbr@boemann.dk>
3
   Copyright (C) 2006-2009 Thomas Zander <zander@kde.org>
4
   Copyright (C) 2006-2008 Thorsten Zachmann <zachmann@kde.org>
5
   Copyright (C) 2007-2009 Jan Hambrecht <jaham@gmx.net>
Thomas Zander's avatar
Thomas Zander committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   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 "KoShape.h"
24
#include "KoShape_p.h"
Thomas Zander's avatar
Thomas Zander committed
25
#include "KoShapeContainer.h"
26
#include "KoShapeLayer.h"
27
#include "KoShapeContainerModel.h"
Thomas Zander's avatar
Thomas Zander committed
28
#include "KoSelection.h"
29
#include "KoPointerEvent.h"
Thomas Zander's avatar
Thomas Zander committed
30 31
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
32 33 34 35
#include "KoShapeBackground.h"
#include "KoColorBackground.h"
#include "KoGradientBackground.h"
#include "KoPatternBackground.h"
36
#include "KoShapeManager.h"
37
#include "KoShapeUserData.h"
38
#include "KoShapeApplicationData.h"
39
#include "KoShapeSavingContext.h"
40
#include "KoShapeLoadingContext.h"
41
#include "KoViewConverter.h"
42
#include "KoLineBorder.h"
43
#include "ShapeDeleter_p.h"
44
#include "KoShapeShadow.h"
Thorsten Zachmann's avatar
Thorsten Zachmann committed
45 46
#include "KoEventAction.h"
#include "KoEventActionRegistry.h"
47
#include "KoOdfWorkaround.h"
48
#include "KoFilterEffectStack.h"
49
#include <KoSnapData.h>
Thomas Zander's avatar
Thomas Zander committed
50

51
#include <KoXmlReader.h>
52
#include <KoXmlWriter.h>
53
#include <KoXmlNS.h>
54
#include <KoGenStyle.h>
55
#include <KoGenStyles.h>
56
#include <KoUnit.h>
57
#include <KoOdfStylesReader.h>
58
#include <KoOdfGraphicStyles.h>
59
#include <KoOdfLoadingContext.h>
60

Thomas Zander's avatar
Thomas Zander committed
61
#include <QPainter>
62
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
63
#include <QPainterPath>
64
#include <QList>
65
#include <QMap>
66
#include <QByteArray>
Thomas Zander's avatar
Thomas Zander committed
67

68 69
#include <kdebug.h>

70 71
#include <limits>

72 73 74 75 76 77 78
KoShapePrivate::KoShapePrivate(KoShape *shape)
    : size(50, 50),
    parent(0),
    userData(0),
    appData(0),
    fill(0),
    border(0),
79
    q(shape),
80
    shadow(0),
81
    filterEffectStack(0),
82
    transparency(0.0),
83 84 85 86 87 88 89 90
    zIndex(0),
    visible(true),
    printable(true),
    geometryProtected(false),
    keepAspect(false),
    selectable(true),
    detectCollision(false),
    protectContent(false)
91
{
92 93 94 95
    connectors.append(QPointF(0.5, 0.0));
    connectors.append(QPointF(1.0, 0.5));
    connectors.append(QPointF(0.5, 1.0));
    connectors.append(QPointF(0.0, 0.5));
96
}
Thomas Zander's avatar
Thomas Zander committed
97

98 99 100
KoShapePrivate::~KoShapePrivate()
{
    if (parent)
101
        parent->removeChild(q);
102
    foreach(KoShapeManager *manager, shapeManagers) {
103 104
        manager->remove(q);
        manager->removeAdditional(q);
Thomas Zander's avatar
Thomas Zander committed
105
    }
106 107 108 109 110 111 112 113
    delete userData;
    delete appData;
    if (border && ! border->removeUser())
        delete border;
    if (shadow && ! shadow->removeUser())
        delete shadow;
    if (fill && ! fill->removeUser())
        delete fill;
114 115
    if (filterEffectStack && ! filterEffectStack->removeUser())
        delete filterEffectStack;
116 117
    qDeleteAll(eventActions);
}
Thomas Zander's avatar
Thomas Zander committed
118

119 120 121
void KoShapePrivate::shapeChanged(KoShape::ChangeType type)
{
    if (parent)
122 123
        parent->model()->childChanged(q, type);
    q->shapeChanged(type);
124
    foreach(KoShape * shape, dependees)
125
        shape->shapeChanged(type, q);
126
}
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
void KoShapePrivate::updateBorder()
{
    if (border == 0)
        return;
    KoInsets insets;
    border->borderInsets(q, insets);
    QSizeF inner = q->size();
    // update left
    q->update(QRectF(-insets.left, -insets.top, insets.left,
                inner.height() + insets.top + insets.bottom));
    // update top
    q->update(QRectF(-insets.left, -insets.top,
                inner.width() + insets.left + insets.right, insets.top));
    // update right
    q->update(QRectF(inner.width(), -insets.top, insets.right,
                inner.height() + insets.top + insets.bottom));
    // update bottom
    q->update(QRectF(-insets.left, inner.height(),
                inner.width() + insets.left + insets.right, insets.bottom));
}
Thomas Zander's avatar
Thomas Zander committed
148

149 150 151 152 153 154 155 156 157 158 159
void KoShapePrivate::addShapeManager(KoShapeManager *manager)
{
    shapeManagers.insert(manager);
}

void KoShapePrivate::removeShapeManager(KoShapeManager *manager)
{
    shapeManagers.remove(manager);
}


160
// ======== KoShape
Thomas Zander's avatar
Thomas Zander committed
161
KoShape::KoShape()
162
        : d_ptr(new KoShapePrivate(this))
Thomas Zander's avatar
Thomas Zander committed
163
{
164
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
165 166
}

167 168 169 170 171
KoShape::KoShape(KoShapePrivate &dd)
    : d_ptr(&dd)
{
}

Thomas Zander's avatar
Thomas Zander committed
172 173
KoShape::~KoShape()
{
174
    Q_D(KoShape);
175
    d->shapeChanged(Deleted);
176
    delete d_ptr;
Thomas Zander's avatar
Thomas Zander committed
177 178
}

179 180
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas)
{
Thomas Zander's avatar
Thomas Zander committed
181 182 183
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
184 185
    /* Since this code is not actually used (kivio is going to be the main user) lets disable instead of fix.
        if ( selected )
Thomas Zander's avatar
Thomas Zander committed
186
        {
187 188 189 190 191 192 193 194 195 196 197 198
            // draw connectors
            QPen pen( Qt::blue );
            pen.setWidth( 0 );
            painter.setPen( pen );
            painter.setBrush( Qt::NoBrush );
            for ( int i = 0; i < d->connectors.size(); ++i )
            {
                QPointF p = converter.documentToView(d->connectors[ i ]);
                painter.drawLine( QPointF( p.x() - 2, p.y() + 2 ), QPointF( p.x() + 2, p.y() - 2 ) );
                painter.drawLine( QPointF( p.x() + 2, p.y() + 2 ), QPointF( p.x() - 2, p.y() - 2 ) );
            }
        }*/
Thomas Zander's avatar
Thomas Zander committed
199 200
}

201
void KoShape::setScale(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
202
{
203
    Q_D(KoShape);
204 205
    QPointF pos = position();
    QMatrix scaleMatrix;
206 207 208
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
209
    d->localMatrix = d->localMatrix * scaleMatrix;
210

211
    notifyChanged();
212
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
213 214
}

215
void KoShape::rotate(qreal angle)
Thomas Zander's avatar
Thomas Zander committed
216
{
217
    Q_D(KoShape);
218
    QPointF center = d->localMatrix.map(QPointF(0.5 * size().width(), 0.5 * size().height()));
219
    QMatrix rotateMatrix;
220 221 222
    rotateMatrix.translate(center.x(), center.y());
    rotateMatrix.rotate(angle);
    rotateMatrix.translate(-center.x(), -center.y());
223
    d->localMatrix = d->localMatrix * rotateMatrix;
224

225
    notifyChanged();
226
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
227 228
}

229
void KoShape::setShear(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
230
{
231
    Q_D(KoShape);
232 233
    QPointF pos = position();
    QMatrix shearMatrix;
234 235 236
    shearMatrix.translate(pos.x(), pos.y());
    shearMatrix.shear(sx, sy);
    shearMatrix.translate(-pos.x(), -pos.y());
237
    d->localMatrix = d->localMatrix * shearMatrix;
238

239
    notifyChanged();
240
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
241 242
}

243
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
244
{
245
    Q_D(KoShape);
246 247
    QSizeF oldSize(size());
    if (oldSize == newSize)
Thomas Zander's avatar
Thomas Zander committed
248
        return;
249

250
    QMatrix matrix;
251 252
    oldSize.setHeight(qMax((qreal) 1E-4, oldSize.height())); // avoids devision by zero below
    oldSize.setWidth(qMax((qreal) 1E-4, oldSize.width()));
253
    matrix.scale(newSize.width()/oldSize.width(), newSize.height()/oldSize.height());
254 255

    KoGradientBackground * g = dynamic_cast<KoGradientBackground*>(d->fill);
256
    if (g) {
257
        g->setMatrix(g->matrix() * matrix);
258
    }
259
    KoLineBorder *l = dynamic_cast<KoLineBorder*>(d->border);
260 261
    if (l && l->lineBrush().gradient()) {
        QBrush brush = l->lineBrush();
262 263
        brush.setMatrix(brush.matrix() * matrix);
        l->setLineBrush(brush);
264
    }
265

Thomas Zander's avatar
Thomas Zander committed
266
    d->size = newSize;
267

268
    notifyChanged();
269
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
270 271
}

272
void KoShape::setPosition(const QPointF &newPosition)
Thomas Zander's avatar
Thomas Zander committed
273
{
274
    Q_D(KoShape);
275
    QPointF currentPos = position();
276
    if (newPosition == currentPos)
Thomas Zander's avatar
Thomas Zander committed
277
        return;
278
    QMatrix translateMatrix;
279
    translateMatrix.translate(newPosition.x() - currentPos.x(), newPosition.y() - currentPos.y());
280 281 282
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
283
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
284 285
}

286
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
287
{
288
    Q_D(const KoShape);
289
    if (d->parent && d->parent->childClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
290 291
        return false;

292
    QPointF point = absoluteTransformation(0).inverted().map(position);
293
    QRectF bb(QPointF(), size());
294
    if (d->border) {
295
        KoInsets insets;
296
        d->border->borderInsets(this, insets);
297
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
298
    }
299 300 301
    if (bb.contains(point))
        return true;

302
    // if there is no shadow we can as well just leave
303
    if (! d->shadow)
304
        return false;
Thomas Zander's avatar
Thomas Zander committed
305

306 307
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
308
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
309

310
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
311 312
}

313
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
314
{
315
    Q_D(const KoShape);
316 317 318
    QSizeF mySize = size();
    QMatrix transform = absoluteTransformation(0);
    QRectF bb(QPointF(0, 0), mySize);
319
    if (d->border) {
320 321 322 323
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
324
    bb = transform.mapRect(bb);
325
    if (d->shadow) {
326
        KoInsets insets;
327
        d->shadow->insets(this, insets);
328 329
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
330 331 332 333 334
    if (d->filterEffectStack) {
        QRectF clipRect = d->filterEffectStack->clipRectForBoundingRect(QRectF(QPointF(), mySize));
        bb |= transform.mapRect(clipRect);
    }
    
335
    return bb;
Thomas Zander's avatar
Thomas Zander committed
336 337
}

338 339
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const
{
340
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
341 342
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
343
    KoShapeContainer * container = d->parent;
344 345
    if (container) {
        if (container->childClipped(this))
346
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
347
        else {
348 349 350 351
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
352
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
353 354 355
        }
    }

356
    if (converter) {
357 358 359
        QPointF pos = d->localMatrix.map(QPointF());
        QPointF trans = converter->documentToView(pos) - pos;
        matrix.translate(trans.x(), trans.y());
Thomas Zander's avatar
Thomas Zander committed
360
    }
361 362

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
363 364
}

365
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
366
{
367
    QMatrix globalMatrix = absoluteTransformation(0);
368 369
    // the transformation is relative to the global coordinate system
    // but we want to change the local matrix, so convert the matrix
Thomas Zander's avatar
Thomas Zander committed
370
    // to be relative to the local coordinate system
371
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
372
    applyTransformation(transformMatrix);
373 374
}

375
void KoShape::applyTransformation(const QMatrix &matrix)
376
{
377
    Q_D(KoShape);
378
    d->localMatrix = matrix * d->localMatrix;
379
    notifyChanged();
380
    d->shapeChanged(GenericMatrixChange);
381 382
}

383
void KoShape::setTransformation(const QMatrix &matrix)
384
{
385
    Q_D(KoShape);
386 387
    d->localMatrix = matrix;
    notifyChanged();
388
    d->shapeChanged(GenericMatrixChange);
389
}
Thomas Zander's avatar
Thomas Zander committed
390

391
QMatrix KoShape::transformation() const
392
{
393
    Q_D(const KoShape);
394 395 396
    return d->localMatrix;
}

397 398
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    bool foundCommonParent = false;
    KoShape *parentShapeS1 = s1;
    KoShape *parentShapeS2 = s2;
    int index1 = parentShapeS1->zIndex();
    int index2 = parentShapeS2->zIndex();
    while (parentShapeS1 && !foundCommonParent) {
        parentShapeS2 = s2;
        index2 = parentShapeS2->zIndex();
        while (parentShapeS2) {
            if (parentShapeS2 == parentShapeS1) {
                foundCommonParent = true;
                break;
            }
            index2 = parentShapeS2->zIndex();
            parentShapeS2 = parentShapeS2->parent();
414
        }
415 416 417 418

        if (!foundCommonParent) {
            index1 = parentShapeS1->zIndex();
            parentShapeS1 = parentShapeS1->parent();
419 420
        }
    }
421 422 423 424 425 426 427
    if (s1 == parentShapeS2) {
        return true;
    }
    else if (s2 == parentShapeS1) {
        return false;
    }
    return index1 < index2;
Thomas Zander's avatar
Thomas Zander committed
428 429
}

430 431
void KoShape::setParent(KoShapeContainer *parent)
{
432
    Q_D(KoShape);
433
    if (d->parent == parent)
434
        return;
435 436 437 438
    KoShapeContainer *oldParent = d->parent;
    d->parent = 0; // avoids recursive removing
    if (oldParent)
        oldParent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
439
    if (parent && parent != this) {
Thomas Zander's avatar
Thomas Zander committed
440
        d->parent = parent;
441
        parent->addChild(this);
442
    }
443
    notifyChanged();
444
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
445 446
}

447 448
int KoShape::zIndex() const
{
449
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
450
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
451 452
}

453 454
void KoShape::update() const
{
455
    Q_D(const KoShape);
456 457 458
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
        foreach(KoShapeManager * manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
459
            manager->update(rect, this, true);
Thomas Zander's avatar
Thomas Zander committed
460 461 462
    }
}

463 464
void KoShape::update(const QRectF &shape) const
{
465
    Q_D(const KoShape);
466
    if (!d->shapeManagers.empty() && isVisible()) {
467
        QRectF rect(absoluteTransformation(0).mapRect(shape));
468
        foreach(KoShapeManager * manager, d->shapeManagers) {
469
            manager->update(rect);
470 471
        }
    }
Thomas Zander's avatar
Thomas Zander committed
472 473
}

474
QPainterPath KoShape::outline() const
475
{
476
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
477
    QPainterPath path;
478
    path.addRect(QRectF(QPointF(0, 0), QSizeF(qMax(d->size.width(), qreal(0.0001)), qMax(d->size.height(), qreal(0.0001)))));
Thomas Zander's avatar
Thomas Zander committed
479 480 481
    return path;
}

482 483
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
484
    QPointF point;
485 486 487 488 489 490
    switch (anchor) {
    case KoFlake::TopLeftCorner: break;
    case KoFlake::TopRightCorner: point = QPointF(size().width(), 0.0); break;
    case KoFlake::BottomLeftCorner: point = QPointF(0.0, size().height()); break;
    case KoFlake::BottomRightCorner: point = QPointF(size().width(), size().height()); break;
    case KoFlake::CenteredPosition: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
491
    }
492
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
493 494
}

495 496
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
497
    Q_D(KoShape);
498
    QPointF currentAbsPosition = absolutePosition(anchor);
499
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
500
    QMatrix translateMatrix;
501 502
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
503
    notifyChanged();
504
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
505 506
}

507 508
void KoShape::copySettings(const KoShape *shape)
{
509
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
510 511
    d->size = shape->size();
    d->connectors.clear();
Jan Hambrecht's avatar
Jan Hambrecht committed
512 513
    foreach(const QPointF & point, shape->connectionPoints())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
514 515
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
516

517 518 519 520 521
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
522

523
    d->geometryProtected = shape->isGeometryProtected();
Thomas Zander's avatar
Thomas Zander committed
524
    d->keepAspect = shape->keepAspectRatio();
525
    d->localMatrix = shape->d_ptr->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
526 527
}

Thomas Zander's avatar
Thomas Zander committed
528
void KoShape::notifyChanged()
529
{
530
    Q_D(KoShape);
531 532
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
533 534 535
    }
}

536 537
void KoShape::setUserData(KoShapeUserData *userData)
{
538
    Q_D(KoShape);
539
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
540
    d->userData = userData;
541 542
}

543 544
KoShapeUserData *KoShape::userData() const
{
545
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
546
    return d->userData;
547 548
}

549 550
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
551
    Q_D(KoShape);
552
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
553
    d->appData = appData;
554 555
}

556 557
KoShapeApplicationData *KoShape::applicationData() const
{
558
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
559
    return d->appData;
560 561
}

562 563
bool KoShape::hasTransparency()
{
564
    Q_D(KoShape);
565
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
566
        return true;
567
    else
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        return d->fill->hasTransparency() || d->transparency > 0.0;
}

void KoShape::setTransparency(qreal transparency)
{
    Q_D(KoShape);
    d->transparency = qBound(0.0, transparency, 1.0);
}

qreal KoShape::transparency(bool recursive) const
{
    Q_D(const KoShape);
    if (!recursive || !parent()) {
        return d->transparency;
    } else {
        const qreal parentOpacity = 1.0-parent()->transparency(recursive);
        const qreal childOpacity = 1.0-d->transparency;
        return 1.0-(parentOpacity*childOpacity);
    }
Thomas Zander's avatar
Thomas Zander committed
587 588
}

589 590
KoInsets KoShape::borderInsets() const
{
591
    Q_D(const KoShape);
592
    KoInsets answer;
593
    if (d->border)
594
        d->border->borderInsets(this, answer);
595 596 597
    return answer;
}

598 599
qreal KoShape::rotation() const
{
600
    Q_D(const KoShape);
601
    // try to extract the rotation angle out of the local matrix
602 603 604
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
605
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
606
        return std::numeric_limits<qreal>::quiet_NaN();
607
    // check if the matrix has scaling mixed in
608
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
609
        return std::numeric_limits<qreal>::quiet_NaN();
610 611

    // calculate the angle from the matrix elements
612 613
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
614 615 616
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
617 618
}

619
QSizeF KoShape::size() const
620
{
621
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
622 623 624
    return d->size;
}

625 626
QPointF KoShape::position() const
{
627
    Q_D(const KoShape);
628 629
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
630
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
631 632
}

633
void KoShape::addConnectionPoint(const QPointF &point)
634
{
635
    Q_D(KoShape);
636 637
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
638
    d->connectors.append(QPointF(point.x() / s.width(), point.y() / s.height()));
Thomas Zander's avatar
Thomas Zander committed
639 640
}

641 642
QList<QPointF> KoShape::connectionPoints() const
{
643
    Q_D(const KoShape);
644 645 646
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
Jan Hambrecht's avatar
Jan Hambrecht committed
647 648
    foreach(const QPointF & cp, d->connectors)
        points.append(QPointF(s.width() * cp.x(), s.height() * cp.y()));
649 650

    return points;
651 652
}

653
void KoShape::addEventAction(KoEventAction * action)
654
{
655
    Q_D(KoShape);
656 657
    if (! d->eventActions.contains(action)) {
        d->eventActions.append(action);
658 659 660
    }
}

661
void KoShape::removeEventAction(KoEventAction * action)
662
{
663
    Q_D(KoShape);
664 665
    if (d->eventActions.contains(action)) {
        d->eventActions.removeAll(action);
666 667 668 669 670
    }
}

QList<KoEventAction *> KoShape::eventActions() const
{
671
    Q_D(const KoShape);
672 673 674
    return d->eventActions;
}

675
void KoShape::setBackground(KoShapeBackground * fill)
676
{
677
    Q_D(KoShape);
678
    if (d->fill)
679 680
        d->fill->removeUser();
    d->fill = fill;
681
    if (d->fill)
682
        d->fill->addUser();
683 684
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
685 686
}

687 688
KoShapeBackground * KoShape::background() const
{
689
    Q_D(const KoShape);
690
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
691 692
}

693 694
void KoShape::setZIndex(int zIndex)
{
695
    Q_D(KoShape);
696
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
697 698 699
    d->zIndex = zIndex;
}

700 701
void KoShape::setVisible(bool on)
{
702
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
703 704 705
    d->visible = on;
}

706
bool KoShape::isVisible(bool recursive) const
707
{
708
    Q_D(const KoShape);
709
    if (! recursive)
710
        return d->visible;
711
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
712 713
        return false;

714
    KoShapeContainer * parentShape = parent();
715 716
    while (parentShape) {
        if (! parentShape->isVisible())
717 718 719 720
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
721 722
}

723 724
void KoShape::setPrintable(bool on)
{
725
    Q_D(KoShape);
726 727
    d->printable = on;
}
728

729 730
bool KoShape::isPrintable() const
{
731
    Q_D(const KoShape);
732 733 734 735
    if (d->visible)
        return d->printable;
    else
        return false;
736 737
}

738 739
void KoShape::setSelectable(bool selectable)
{
740
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
741 742 743
    d->selectable = selectable;
}

744 745
bool KoShape::isSelectable() const
{
746
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
747 748 749
    return d->selectable;
}

750
void KoShape::setGeometryProtected(bool on)
751
{
752
    Q_D(KoShape);
753
    d->geometryProtected = on;
Thomas Zander's avatar
Thomas Zander committed
754 755
}

756
bool KoShape::isGeometryProtected() const
757
{
758
    Q_D(const KoShape);
759
    return d->geometryProtected;
Thomas Zander's avatar
Thomas Zander committed
760 761
}

762 763
void KoShape::setContentProtected(bool protect)
{
764
    Q_D(KoShape);
765 766 767 768 769
    d->protectContent = protect;
}

bool KoShape::isContentProtected() const
{
770
    Q_D(const KoShape);
771 772 773
    return d->protectContent;
}

774 775
KoShapeContainer *KoShape::parent() const
{
776
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
777 778 779
    return d->parent;
}

780 781
void KoShape::setKeepAspectRatio(bool keepAspect)
{
782
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
783 784 785
    d->keepAspect = keepAspect;
}

786 787
bool KoShape::keepAspectRatio() const
{
788
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
789 790 791
    return d->keepAspect;
}

792
QString KoShape::shapeId() const
793
{
794
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
795 796 797
    return d->shapeId;
}

798 799
void KoShape::setShapeId(const QString &id)
{
800
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
801 802 803
    d->shapeId = id;
}

804 805
void KoShape::setCollisionDetection(bool detect)
{
806
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
807 808 809
    d->detectCollision = detect;
}

810 811
bool KoShape::collisionDetection()
{
812
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
813 814 815
    return d->detectCollision;
}

816 817
KoShapeBorderModel *KoShape::border() const
{
818
    Q_D(const KoShape);
819 820 821
    return d->border;
}

822 823
void KoShape::setBorder(KoShapeBorderModel *border)
{
824
    Q_D(KoShape);
825 826
    if (border)
        border->addUser();
827
    d->updateBorder();
828
    if (d->border)
829
        d->border->removeUser();
830
    d->border = border;
831
    d->updateBorder();
832 833
    d->shapeChanged(BorderChanged);
    notifyChanged();
834 835
}

836
void KoShape::setShadow(KoShapeShadow * shadow)
837
{
838
    Q_D(KoShape);
839
    if (d->shadow)
840 841
        d->shadow->removeUser();
    d->shadow = shadow;
842
    if (d->shadow) {
843
        d->shadow->addUser();
844 845
        // TODO update changed area
    }
846 847 848 849 850 851
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

KoShapeShadow * KoShape::shadow() const
{
852
    Q_D(const KoShape);
853 854 855
    return d->shadow;
}

856
QMatrix KoShape::matrix() const
857
{
858
    Q_D(const KoShape);
859
    return d->localMatrix;
860 861
}

862
void KoShape::removeConnectionPoint(int index)
863
{
864
    Q_D(KoShape);
865 866
    if (index < d->connectors.count())
        d->connectors.remove(index);
867 868
}

869 870
QString KoShape::name() const
{
871
    Q_D(const KoShape);
Jan Hambrecht's avatar