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"
Thomas Zander's avatar
Thomas Zander committed
49

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

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

67 68
#include <kdebug.h>

69 70
#include <limits>

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

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

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

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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
147

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

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


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

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

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

178 179
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas)
{
Thomas Zander's avatar
Thomas Zander committed
180 181 182
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
183 184
    /* 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
185
        {
186 187 188 189 190 191 192 193 194 195 196 197
            // 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
198 199
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

355
    if (converter) {
356 357 358
        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
359
    }
360 361

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

364
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
365
{
366
    QMatrix globalMatrix = absoluteTransformation(0);
367 368
    // 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
369
    // to be relative to the local coordinate system
370
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
371
    applyTransformation(transformMatrix);
372 373
}

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

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

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

396 397
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    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();
413
        }
414 415 416 417

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

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

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

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

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

473
QPainterPath KoShape::outline() const
474
{
475
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
476
    QPainterPath path;
477
    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
478 479 480
    return path;
}

481 482
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
483
    QPointF point;
484 485 486 487 488 489
    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;
490
    }
491
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
492 493
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return points;
650 651
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

868 869
QString KoShape::name() const
{
870
    Q_D(const KoShape);
871 872 873
    return d->name;
}