KoShape.cpp 46 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-2010 Thomas Zander <zander@kde.org>
4
   Copyright (C) 2006-2010 Thorsten Zachmann <zachmann@kde.org>
5
   Copyright (C) 2007-2009 Jan Hambrecht <jaham@gmx.net>
6
   CopyRight (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
Thomas Zander's avatar
Thomas Zander committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20

   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,
21
   Boston, MA 02110-1301, USA.
Thomas Zander's avatar
Thomas Zander committed
22 23 24
*/

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

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

Thomas Zander's avatar
Thomas Zander committed
62
#include <QPainter>
63
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
64
#include <QPainterPath>
65
#include <QList>
66
#include <QMap>
67
#include <QByteArray>
68 69
#include <kdebug.h>

70 71
#include <limits>

72 73
// KoShapeCache

74
/// Empty all cached images from the image cache
75 76 77 78 79 80 81 82
void KoShapeCache::purge()
{
    qDeleteAll(deviceData);
    deviceData.clear();
}

// KoShapePrivate

83 84
KoShapePrivate::KoShapePrivate(KoShape *shape)
    : size(50, 50),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      parent(0),
      userData(0),
      appData(0),
      fill(0),
      border(0),
      q_ptr(shape),
      shadow(0),
      filterEffectStack(0),
      transparency(0.0),
      zIndex(0),
      visible(true),
      printable(true),
      geometryProtected(false),
      keepAspect(false),
      selectable(true),
      detectCollision(false),
      protectContent(false),
102
      cacheMode(KoShape::NoCache),
103
      cache(0)
104
{
105 106 107 108
    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));
109
}
Thomas Zander's avatar
Thomas Zander committed
110

111 112
KoShapePrivate::~KoShapePrivate()
{
113
    Q_Q(KoShape);
114
    if (parent)
115
        parent->removeShape(q);
116
    foreach(KoShapeManager *manager, shapeManagers) {
117 118
        manager->remove(q);
        manager->removeAdditional(q);
Thomas Zander's avatar
Thomas Zander committed
119
    }
120 121
    delete userData;
    delete appData;
122
    if (border && !border->deref())
123
        delete border;
124
    if (shadow && !shadow->deref())
125
        delete shadow;
126
    if (fill && !fill->deref())
127
        delete fill;
128
    if (filterEffectStack && !filterEffectStack->deref())
129
        delete filterEffectStack;
130 131
    qDeleteAll(eventActions);
}
Thomas Zander's avatar
Thomas Zander committed
132

133 134
void KoShapePrivate::shapeChanged(KoShape::ChangeType type)
{
135
    Q_Q(KoShape);
136
    if (parent)
137 138
        parent->model()->childChanged(q, type);
    q->shapeChanged(type);
139
    foreach(KoShape * shape, dependees)
140
        shape->shapeChanged(type, q);
141
}
142

143 144
void KoShapePrivate::updateBorder()
{
145
    Q_Q(KoShape);
146 147 148 149 150 151 152
    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,
153
                     inner.height() + insets.top + insets.bottom));
154 155
    // update top
    q->update(QRectF(-insets.left, -insets.top,
156
                     inner.width() + insets.left + insets.right, insets.top));
157 158
    // update right
    q->update(QRectF(inner.width(), -insets.top, insets.right,
159
                     inner.height() + insets.top + insets.bottom));
160 161
    // update bottom
    q->update(QRectF(-insets.left, inner.height(),
162
                     inner.width() + insets.left + insets.right, insets.bottom));
163
}
Thomas Zander's avatar
Thomas Zander committed
164

165 166 167 168 169 170 171 172
void KoShapePrivate::addShapeManager(KoShapeManager *manager)
{
    shapeManagers.insert(manager);
}

void KoShapePrivate::removeShapeManager(KoShapeManager *manager)
{
    shapeManagers.remove(manager);
173 174 175 176 177 178 179 180 181 182 183
    if (cacheMode == KoShape::ScaledCache) {
        if (KoShapeCache *cache = maybeShapeCache()) {
            KoShapeCache::DeviceData *deviceData = cache->deviceData.take(manager);
            delete deviceData;
        }
    }
}

KoShapeCache *KoShapePrivate::maybeShapeCache() const
{
    return cache;
184
}
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

KoShapeCache *KoShapePrivate::shapeCache() const
{
    if (!cache) {
        const_cast<KoShapePrivate *>(this)->cache = new KoShapeCache;
    }
    return cache;
}

void KoShapePrivate::removeShapeCache()
{
    if (cache) {
        cache->purge();
        delete cache;
        cache = 0;
    }
}


204
// static
205
QString KoShapePrivate::getStyleProperty(const char *property, KoShapeLoadingContext &context)
206 207 208 209 210 211 212 213 214 215 216
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
    QString value;

    if (styleStack.hasProperty(KoXmlNS::draw, property)) {
        value = styleStack.property(KoXmlNS::draw, property);
    }

    return value;
}

217 218


219
// ======== KoShape
Thomas Zander's avatar
Thomas Zander committed
220
KoShape::KoShape()
221
    : d_ptr(new KoShapePrivate(this))
Thomas Zander's avatar
Thomas Zander committed
222
{
223
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
224 225
}

226 227 228 229 230
KoShape::KoShape(KoShapePrivate &dd)
    : d_ptr(&dd)
{
}

Thomas Zander's avatar
Thomas Zander committed
231 232
KoShape::~KoShape()
{
233
    Q_D(KoShape);
234
    d->shapeChanged(Deleted);
235
    d->removeShapeCache();
236
    delete d_ptr;
Thomas Zander's avatar
Thomas Zander committed
237 238
}

239 240
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas)
{
Thomas Zander's avatar
Thomas Zander committed
241 242 243
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
244
    /* Since this code is not actually used (kivio is going to be the main user) lets disable instead of fix.
245
        if (selected)
Thomas Zander's avatar
Thomas Zander committed
246
        {
247
            // draw connectors
248 249 250 251 252
            QPen pen(Qt::blue);
            pen.setWidth(0);
            painter.setPen(pen);
            painter.setBrush(Qt::NoBrush);
            for (int i = 0; i < d->connectors.size(); ++i)
253
          {
254
                QPointF p = converter.documentToView(d->connectors[ i ]);
255 256
                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));
257 258
            }
        }*/
Thomas Zander's avatar
Thomas Zander committed
259 260
}

Thomas Zander's avatar
Thomas Zander committed
261
void KoShape::scale(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
262
{
263
    Q_D(KoShape);
264
    QPointF pos = position();
265
    QTransform scaleMatrix;
266 267 268
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
269
    d->localMatrix = d->localMatrix * scaleMatrix;
270

271
    notifyChanged();
272
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
273 274
}

275
void KoShape::rotate(qreal angle)
Thomas Zander's avatar
Thomas Zander committed
276
{
277
    Q_D(KoShape);
278
    QPointF center = d->localMatrix.map(QPointF(0.5 * size().width(), 0.5 * size().height()));
279
    QTransform rotateMatrix;
280 281 282
    rotateMatrix.translate(center.x(), center.y());
    rotateMatrix.rotate(angle);
    rotateMatrix.translate(-center.x(), -center.y());
283
    d->localMatrix = d->localMatrix * rotateMatrix;
284

285
    notifyChanged();
286
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
287 288
}

Thomas Zander's avatar
Thomas Zander committed
289
void KoShape::shear(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
290
{
291
    Q_D(KoShape);
292
    QPointF pos = position();
293
    QTransform shearMatrix;
294 295 296
    shearMatrix.translate(pos.x(), pos.y());
    shearMatrix.shear(sx, sy);
    shearMatrix.translate(-pos.x(), -pos.y());
297
    d->localMatrix = d->localMatrix * shearMatrix;
298

299
    notifyChanged();
300
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
301 302
}

303
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
304
{
305
    Q_D(KoShape);
306 307
    QSizeF oldSize(size());
    if (oldSize == newSize)
Thomas Zander's avatar
Thomas Zander committed
308
        return;
309

Thomas Zander's avatar
Thomas Zander committed
310
    d->size = newSize;
311

312
    notifyChanged();
313
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
314 315
}

316
void KoShape::setPosition(const QPointF &newPosition)
Thomas Zander's avatar
Thomas Zander committed
317
{
318
    Q_D(KoShape);
319
    QPointF currentPos = position();
320
    if (newPosition == currentPos)
Thomas Zander's avatar
Thomas Zander committed
321
        return;
322
    QTransform translateMatrix;
323
    translateMatrix.translate(newPosition.x() - currentPos.x(), newPosition.y() - currentPos.y());
324 325 326
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
327
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
328 329
}

330
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
331
{
332
    Q_D(const KoShape);
333
    if (d->parent && d->parent->isClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
334 335
        return false;

336
    QPointF point = absoluteTransformation(0).inverted().map(position);
337
    QRectF bb(QPointF(), size());
338
    if (d->border) {
339
        KoInsets insets;
340
        d->border->borderInsets(this, insets);
341
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
342
    }
343 344 345
    if (bb.contains(point))
        return true;

346
    // if there is no shadow we can as well just leave
347
    if (! d->shadow)
348
        return false;
Thomas Zander's avatar
Thomas Zander committed
349

350 351
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
352
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
353

354
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
355 356
}

357
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
358
{
359
    Q_D(const KoShape);
360
    QSizeF mySize = size();
361
    QTransform transform = absoluteTransformation(0);
362
    QRectF bb = outlineRect();
363
    if (d->border) {
364 365 366 367
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
368
    bb = transform.mapRect(bb);
369
    if (d->shadow) {
370
        KoInsets insets;
Thomas Zander's avatar
Thomas Zander committed
371
        d->shadow->insets(insets);
372 373
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
374
    if (d->filterEffectStack) {
375
        QRectF clipRect = d->filterEffectStack->clipRectForBoundingRect(outlineRect());
376 377
        bb |= transform.mapRect(clipRect);
    }
Thomas Zander's avatar
Thomas Zander committed
378

379
    return bb;
Thomas Zander's avatar
Thomas Zander committed
380 381
}

382
QTransform KoShape::absoluteTransformation(const KoViewConverter *converter) const
383
{
384
    Q_D(const KoShape);
385
    QTransform matrix;
Thomas Zander's avatar
Thomas Zander committed
386
    // apply parents matrix to inherit any transformations done there.
387
    KoShapeContainer * container = d->parent;
388
    if (container) {
389
        if (container->inheritsTransform(this)) {
390 391 392 393
            // We do need to pass the converter here, otherwise the parent's
            // translation is not inherited.
            matrix = container->absoluteTransformation(converter);
        } else {
394 395 396 397
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
398
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
399 400 401
        }
    }

402
    if (converter) {
403 404 405
        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
406
    }
407 408

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
409 410
}

411
void KoShape::applyAbsoluteTransformation(const QTransform &matrix)
412
{
413
    QTransform globalMatrix = absoluteTransformation(0);
414 415
    // 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
416
    // to be relative to the local coordinate system
417
    QTransform transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
418
    applyTransformation(transformMatrix);
419 420
}

421
void KoShape::applyTransformation(const QTransform &matrix)
422
{
423
    Q_D(KoShape);
424
    d->localMatrix = matrix * d->localMatrix;
425
    notifyChanged();
426
    d->shapeChanged(GenericMatrixChange);
427 428
}

429
void KoShape::setTransformation(const QTransform &matrix)
430
{
431
    Q_D(KoShape);
432 433
    d->localMatrix = matrix;
    notifyChanged();
434
    d->shapeChanged(GenericMatrixChange);
435
}
Thomas Zander's avatar
Thomas Zander committed
436

437
QTransform KoShape::transformation() const
438
{
439
    Q_D(const KoShape);
440 441 442
    return d->localMatrix;
}

443 444
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    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();
460
        }
461 462 463 464

        if (!foundCommonParent) {
            index1 = parentShapeS1->zIndex();
            parentShapeS1 = parentShapeS1->parent();
465 466
        }
    }
467 468 469 470 471 472 473
    if (s1 == parentShapeS2) {
        return true;
    }
    else if (s2 == parentShapeS1) {
        return false;
    }
    return index1 < index2;
Thomas Zander's avatar
Thomas Zander committed
474 475
}

476 477
void KoShape::setParent(KoShapeContainer *parent)
{
478
    Q_D(KoShape);
479
    if (d->parent == parent)
480
        return;
481 482 483
    KoShapeContainer *oldParent = d->parent;
    d->parent = 0; // avoids recursive removing
    if (oldParent)
484
        oldParent->removeShape(this);
Thomas Zander's avatar
Thomas Zander committed
485
    if (parent && parent != this) {
Thomas Zander's avatar
Thomas Zander committed
486
        d->parent = parent;
487
        parent->addShape(this);
488
    }
489
    notifyChanged();
490
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
491 492
}

493 494
int KoShape::zIndex() const
{
495
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
496
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
497 498
}

499 500
void KoShape::update() const
{
501
    Q_D(const KoShape);
502 503 504 505 506 507 508 509 510 511

    if (d->cacheMode != NoCache) {
        KoShapeCache *cache = d->shapeCache();
        foreach(KoShapeCache::DeviceData *data, cache->deviceData.values()) {
            data->allExposed = true;
            data->exposed.clear();
        }

    }

512 513
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
514
        foreach(KoShapeManager * manager, d->shapeManagers) {
Jan Hambrecht's avatar
Jan Hambrecht committed
515
            manager->update(rect, this, true);
516
        }
Thomas Zander's avatar
Thomas Zander committed
517 518 519
    }
}

520
void KoShape::update(const QRectF &rect) const
521
{
522 523 524 525 526

    if (rect.isEmpty() && !rect.isNull()) {
        return;
    }

527
    Q_D(const KoShape);
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

    if (d->cacheMode != NoCache) {
        KoShapeCache *cache = d->shapeCache();
        foreach(KoShapeCache::DeviceData *data, cache->deviceData.values()) {
            if (!data->allExposed) {
                if (rect.isNull()) {
                    data->allExposed = true;
                    data->exposed.clear();
                }
                else {
                    data->exposed.append(rect);
                }
            }
        }
    }

544
    if (!d->shapeManagers.empty() && isVisible()) {
545
        QRectF rc(absoluteTransformation(0).mapRect(rect));
546
        foreach(KoShapeManager * manager, d->shapeManagers) {
547
            manager->update(rc);
548 549
        }
    }
Thomas Zander's avatar
Thomas Zander committed
550 551
}

552
QPainterPath KoShape::outline() const
553
{
Thomas Zander's avatar
Thomas Zander committed
554
    QPainterPath path;
555
    path.addRect(outlineRect());
Thomas Zander's avatar
Thomas Zander committed
556 557 558
    return path;
}

559 560 561 562 563 564
QRectF KoShape::outlineRect() const
{
    Q_D(const KoShape);
    return QRectF(QPointF(0, 0), QSizeF(qMax(d->size.width(), qreal(0.0001)), qMax(d->size.height(), qreal(0.0001))));
}

565 566
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
567
    QPointF point;
568 569 570 571 572 573
    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;
574
    }
575
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
576 577
}

578 579
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
580
    Q_D(KoShape);
581
    QPointF currentAbsPosition = absolutePosition(anchor);
582
    QPointF translate = newPosition - currentAbsPosition;
583
    QTransform translateMatrix;
584 585
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
586
    notifyChanged();
587
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
588 589
}

590 591
void KoShape::copySettings(const KoShape *shape)
{
592
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
593 594
    d->size = shape->size();
    d->connectors.clear();
595
    foreach(const QPointF &point, shape->connectionPoints())
Jan Hambrecht's avatar
Jan Hambrecht committed
596
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
597 598
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
599

600 601 602 603 604
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
605

606
    d->geometryProtected = shape->isGeometryProtected();
Thomas Zander's avatar
Thomas Zander committed
607 608
    d->protectContent = shape->isContentProtected();
    d->selectable = shape->isSelectable();
Thomas Zander's avatar
Thomas Zander committed
609
    d->keepAspect = shape->keepAspectRatio();
610
    d->localMatrix = shape->d_ptr->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
611 612
}

Thomas Zander's avatar
Thomas Zander committed
613
void KoShape::notifyChanged()
614
{
615
    Q_D(KoShape);
616 617
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
618
    }
619 620 621 622
    KoShapeCache *cache = d->maybeShapeCache();
    if (cache) {
        cache->purge();
    }
623 624
}

625 626
void KoShape::setUserData(KoShapeUserData *userData)
{
627
    Q_D(KoShape);
628
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
629
    d->userData = userData;
630 631
}

632 633
KoShapeUserData *KoShape::userData() const
{
634
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
635
    return d->userData;
636 637
}

638 639
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
640
    Q_D(KoShape);
641
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
642
    d->appData = appData;
643 644
}

645 646
KoShapeApplicationData *KoShape::applicationData() const
{
647
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
648
    return d->appData;
649 650
}

Thomas Zander's avatar
Thomas Zander committed
651
bool KoShape::hasTransparency() const
652
{
Thomas Zander's avatar
Thomas Zander committed
653
    Q_D(const KoShape);
654
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
655
        return true;
656
    else
657 658 659 660 661 662
        return d->fill->hasTransparency() || d->transparency > 0.0;
}

void KoShape::setTransparency(qreal transparency)
{
    Q_D(KoShape);
663
    d->transparency = qBound<qreal>(0.0, transparency, 1.0);
664 665 666 667 668 669 670 671 672 673 674 675
}

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
676 677
}

678 679
KoInsets KoShape::borderInsets() const
{
680
    Q_D(const KoShape);
681
    KoInsets answer;
682
    if (d->border)
683
        d->border->borderInsets(this, answer);
684 685 686
    return answer;
}

687 688
qreal KoShape::rotation() const
{
689
    Q_D(const KoShape);
690
    // try to extract the rotation angle out of the local matrix
691 692 693
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
694
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
695
        return std::numeric_limits<qreal>::quiet_NaN();
696
    // check if the matrix has scaling mixed in
697
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
698
        return std::numeric_limits<qreal>::quiet_NaN();
699 700

    // calculate the angle from the matrix elements
701 702
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
703 704 705
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
706 707
}

708
QSizeF KoShape::size() const
709
{
710
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
711 712 713
    return d->size;
}

714 715
QPointF KoShape::position() const
{
716
    Q_D(const KoShape);
717 718
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
Thomas Zander's avatar
Thomas Zander committed
719 720
}

721
void KoShape::addConnectionPoint(const QPointF &point)
722
{
723
    Q_D(KoShape);
724 725
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
726
    d->connectors.append(QPointF(point.x() / s.width(), point.y() / s.height()));
Thomas Zander's avatar
Thomas Zander committed
727 728
}

729 730
QList<QPointF> KoShape::connectionPoints() const
{
731
    Q_D(const KoShape);
732 733 734
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
735
    foreach(const QPointF &cp, d->connectors)
Jan Hambrecht's avatar
Jan Hambrecht committed
736
        points.append(QPointF(s.width() * cp.x(), s.height() * cp.y()));
737 738

    return points;
739 740
}

741
void KoShape::addEventAction(KoEventAction *action)
742
{
743
    Q_D(KoShape);
744
    d->eventActions.insert(action);
745 746
}

747
void KoShape::removeEventAction(KoEventAction *action)
748
{
749
    Q_D(KoShape);
750
    d->eventActions.remove(action);
751 752
}

753
QSet<KoEventAction *> KoShape::eventActions() const
754
{
755
    Q_D(const KoShape);
756 757 758
    return d->eventActions;
}

759
void KoShape::setBackground(KoShapeBackground *fill)
760
{
761
    Q_D(KoShape);
762
    if (d->fill)
763
        d->fill->deref();
764
    d->fill = fill;
765
    if (d->fill)
766
        d->fill->ref();
767
    d->shapeChanged(BackgroundChanged);
Thomas Zander's avatar
Thomas Zander committed
768 769
}

770 771
KoShapeBackground * KoShape::background() const
{
772
    Q_D(const KoShape);
773
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
774 775
}

776 777
void KoShape::setZIndex(int zIndex)
{
778
    Q_D(KoShape);
779
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
780 781 782
    d->zIndex = zIndex;
}

783 784
void KoShape::setVisible(bool on)
{
785
    Q_D(KoShape);
786
    if (d->visible == on) return;
Thomas Zander's avatar
Thomas Zander committed
787
    d->visible = on;
788 789 790 791 792 793
    if (d->visible) {
        KoShapeCache *cache = d->maybeShapeCache();
        if (cache) {
            cache->purge();
        }
    }
Thomas Zander's avatar
Thomas Zander committed
794 795
}

796
bool KoShape::isVisible(bool recursive) const
797
{
798
    Q_D(const KoShape);
799
    if (! recursive)
800
        return d->visible;
801
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
802 803
        return false;

804
    KoShapeContainer * parentShape = parent();
805 806
    while (parentShape) {
        if (! parentShape->isVisible())
807 808 809 810
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
811 812
}

813 814
void KoShape::setPrintable(bool on)
{
815
    Q_D(KoShape);
816 817
    d->printable = on;
}
818

819 820
bool KoShape::isPrintable() const
{
821
    Q_D(const KoShape);
822 823 824 825
    if (d->visible)
        return d->printable;
    else
        return false;
826 827
}

828 829
void KoShape::setSelectable(bool selectable)
{
830
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
831 832 833
    d->selectable = selectable;
}

834 835
bool KoShape::isSelectable() const
{
836
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
837 838 839
    return d->selectable;
}

840
void KoShape::setGeometryProtected(bool on)
841
{
842
    Q_D(KoShape);
843
    d->geometryProtected = on;
Thomas Zander's avatar
Thomas Zander committed
844 845
}

846
bool KoShape::isGeometryProtected() const
847
{
848
    Q_D(const KoShape);
849
    return d->geometryProtected;
Thomas Zander's avatar
Thomas Zander committed
850 851
}

852 853
void KoShape::setContentProtected(bool protect)
{
854
    Q_D(KoShape);
855
    d->protectContent = protect;
856 857 858 859
}

bool KoShape::isContentProtected() const
{
860
    Q_D(const KoShape);
861
    return d->protectContent;
862 863
}

864 865
KoShapeContainer *KoShape::parent() const
{
866
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
867 868 869
    return d->parent;
}

870 871
void KoShape::setKeepAspectRatio(bool