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

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

130 131
void KoShapePrivate::updateBorder()
{
132
    Q_Q(KoShape);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    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
151

152 153 154 155 156 157 158 159 160 161 162
void KoShapePrivate::addShapeManager(KoShapeManager *manager)
{
    shapeManagers.insert(manager);
}

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


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

170 171 172 173 174
KoShape::KoShape(KoShapePrivate &dd)
    : d_ptr(&dd)
{
}

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

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

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

214
    notifyChanged();
215
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
216 217
}

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

228
    notifyChanged();
229
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
230 231
}

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

242
    notifyChanged();
243
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
244 245
}

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

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

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

Thomas Zander's avatar
Thomas Zander committed
269
    d->size = newSize;
270

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

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

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

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

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

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

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

313
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
314 315
}

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

341 342
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const
{
343
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
344 345
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
346
    KoShapeContainer * container = d->parent;
347
    if (container) {
348 349 350 351 352
        if (container->childClipped(this)) {
            // We do need to pass the converter here, otherwise the parent's
            // translation is not inherited.
            matrix = container->absoluteTransformation(converter);
        } else {
353 354 355 356
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
357
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
358 359 360
        }
    }

361
    if (converter) {
362 363 364
        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
365
    }
366 367

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
368 369
}

370
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
371
{
372
    QMatrix globalMatrix = absoluteTransformation(0);
373 374
    // 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
375
    // to be relative to the local coordinate system
376
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
377
    applyTransformation(transformMatrix);
378 379
}

380
void KoShape::applyTransformation(const QMatrix &matrix)
381
{
382
    Q_D(KoShape);
383
    d->localMatrix = matrix * d->localMatrix;
384
    notifyChanged();
385
    d->shapeChanged(GenericMatrixChange);
386 387
}

388
void KoShape::setTransformation(const QMatrix &matrix)
389
{
390
    Q_D(KoShape);
391 392
    d->localMatrix = matrix;
    notifyChanged();
393
    d->shapeChanged(GenericMatrixChange);
394
}
Thomas Zander's avatar
Thomas Zander committed
395

396
QMatrix KoShape::transformation() const
397
{
398
    Q_D(const KoShape);
399 400 401
    return d->localMatrix;
}

402 403
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    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();
419
        }
420 421 422 423

        if (!foundCommonParent) {
            index1 = parentShapeS1->zIndex();
            parentShapeS1 = parentShapeS1->parent();
424 425
        }
    }
426 427 428 429 430 431 432
    if (s1 == parentShapeS2) {
        return true;
    }
    else if (s2 == parentShapeS1) {
        return false;
    }
    return index1 < index2;
Thomas Zander's avatar
Thomas Zander committed
433 434
}

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

452 453
int KoShape::zIndex() const
{
454
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
455
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
456 457
}

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

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

479
QPainterPath KoShape::outline() const
480
{
481
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
482
    QPainterPath path;
483
    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
484 485 486
    return path;
}

487 488
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
489
    QPointF point;
490 491 492 493 494 495
    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;
496
    }
497
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
498 499
}

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

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

522 523 524 525 526
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
527

528
    d->geometryProtected = shape->isGeometryProtected();
Thomas Zander's avatar
Thomas Zander committed
529
    d->keepAspect = shape->keepAspectRatio();
530
    d->localMatrix = shape->d_ptr->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
531 532
}

Thomas Zander's avatar
Thomas Zander committed
533
void KoShape::notifyChanged()
534
{
535
    Q_D(KoShape);
536 537
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
538 539 540
    }
}

541 542
void KoShape::setUserData(KoShapeUserData *userData)
{
543
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
544
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
545
    d->userData = userData;
546 547
}

548 549
KoShapeUserData *KoShape::userData() const
{
550
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
551
    return d->userData;
552 553
}

554 555
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
556
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
557
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
558
    d->appData = appData;
559 560
}

561 562
KoShapeApplicationData *KoShape::applicationData() const
{
563
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
564
    return d->appData;
565 566
}

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

594 595
KoInsets KoShape::borderInsets() const
{
596
    Q_D(const KoShape);
597
    KoInsets answer;
598
    if (d->border)
599
        d->border->borderInsets(this, answer);
600 601 602
    return answer;
}

603 604
qreal KoShape::rotation() const
{
605
    Q_D(const KoShape);
606
    // try to extract the rotation angle out of the local matrix
607 608 609
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
610
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
611
        return std::numeric_limits<qreal>::quiet_NaN();
612
    // check if the matrix has scaling mixed in
613
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
614
        return std::numeric_limits<qreal>::quiet_NaN();
615 616

    // calculate the angle from the matrix elements
617 618
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
619 620 621
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
622 623
}

624
QSizeF KoShape::size() const
625
{
626
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
627 628 629
    return d->size;
}

630 631
QPointF KoShape::position() const
{
632
    Q_D(const KoShape);
633 634
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
635
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
636 637
}

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

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

    return points;
656 657
}

658
void KoShape::addEventAction(KoEventAction *action)
659
{
660
    Q_D(KoShape);
661
    d->eventActions.insert(action);
662 663
}

664
void KoShape::removeEventAction(KoEventAction *action)
665
{
666
    Q_D(KoShape);
667
    d->eventActions.remove(action);
668 669
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bool KoShape::isContentProtected() const
{
771
    Q_D(const KoShape);
772 773