KoShape.cpp 41 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 82 83 84 85 86 87 88
    zIndex(0),
    visible(true),
    printable(true),
    geometryProtected(false),
    keepAspect(false),
    selectable(true),
    detectCollision(false),
    protectContent(false)
89
{
90 91 92 93
    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));
94
}
Thomas Zander's avatar
Thomas Zander committed
95

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

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

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

147
// ======== KoShape
Thomas Zander's avatar
Thomas Zander committed
148
KoShape::KoShape()
149
        : d_ptr(new KoShapePrivate(this))
Thomas Zander's avatar
Thomas Zander committed
150
{
151
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
152 153
}

154 155 156 157 158
KoShape::KoShape(KoShapePrivate &dd)
    : d_ptr(&dd)
{
}

Thomas Zander's avatar
Thomas Zander committed
159 160
KoShape::~KoShape()
{
161
    Q_D(KoShape);
162
    d->shapeChanged(Deleted);
163
    delete d_ptr;
Thomas Zander's avatar
Thomas Zander committed
164 165
}

166 167
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas)
{
Thomas Zander's avatar
Thomas Zander committed
168 169 170
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
171 172
    /* 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
173
        {
174 175 176 177 178 179 180 181 182 183 184 185
            // 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
186 187
}

188
void KoShape::setScale(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
189
{
190
    Q_D(KoShape);
191 192
    QPointF pos = position();
    QMatrix scaleMatrix;
193 194 195
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
196
    d->localMatrix = d->localMatrix * scaleMatrix;
197

198
    notifyChanged();
199
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
200 201
}

202
void KoShape::rotate(qreal angle)
Thomas Zander's avatar
Thomas Zander committed
203
{
204
    Q_D(KoShape);
205
    QPointF center = d->localMatrix.map(QPointF(0.5 * size().width(), 0.5 * size().height()));
206
    QMatrix rotateMatrix;
207 208 209
    rotateMatrix.translate(center.x(), center.y());
    rotateMatrix.rotate(angle);
    rotateMatrix.translate(-center.x(), -center.y());
210
    d->localMatrix = d->localMatrix * rotateMatrix;
211

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

216
void KoShape::setShear(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
217
{
218
    Q_D(KoShape);
219 220
    QPointF pos = position();
    QMatrix shearMatrix;
221 222 223
    shearMatrix.translate(pos.x(), pos.y());
    shearMatrix.shear(sx, sy);
    shearMatrix.translate(-pos.x(), -pos.y());
224
    d->localMatrix = d->localMatrix * shearMatrix;
225

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

230
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
231
{
232
    Q_D(KoShape);
233 234
    QSizeF oldSize(size());
    if (oldSize == newSize)
Thomas Zander's avatar
Thomas Zander committed
235
        return;
236

237
    QMatrix matrix;
238 239
    oldSize.setHeight(qMax((qreal) 1E-4, oldSize.height())); // avoids devision by zero below
    oldSize.setWidth(qMax((qreal) 1E-4, oldSize.width()));
240
    matrix.scale(newSize.width()/oldSize.width(), newSize.height()/oldSize.height());
241 242

    KoGradientBackground * g = dynamic_cast<KoGradientBackground*>(d->fill);
243
    if (g) {
244
        g->setMatrix(g->matrix() * matrix);
245
    }
246
    KoLineBorder *l = dynamic_cast<KoLineBorder*>(d->border);
247 248
    if (l && l->lineBrush().gradient()) {
        QBrush brush = l->lineBrush();
249 250
        brush.setMatrix(brush.matrix() * matrix);
        l->setLineBrush(brush);
251
    }
252

Thomas Zander's avatar
Thomas Zander committed
253
    d->size = newSize;
254

255
    notifyChanged();
256
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
257 258
}

259
void KoShape::setPosition(const QPointF &newPosition)
Thomas Zander's avatar
Thomas Zander committed
260
{
261
    Q_D(KoShape);
262
    QPointF currentPos = position();
263
    if (newPosition == currentPos)
Thomas Zander's avatar
Thomas Zander committed
264
        return;
265
    QMatrix translateMatrix;
266
    translateMatrix.translate(newPosition.x() - currentPos.x(), newPosition.y() - currentPos.y());
267 268 269
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
270
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
271 272
}

273
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
274
{
275
    Q_D(const KoShape);
276
    if (d->parent && d->parent->childClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
277 278
        return false;

279
    QPointF point = absoluteTransformation(0).inverted().map(position);
280
    QRectF bb(QPointF(), size());
281
    if (d->border) {
282
        KoInsets insets;
283
        d->border->borderInsets(this, insets);
284
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
285
    }
286 287 288
    if (bb.contains(point))
        return true;

289
    // if there is no shadow we can as well just leave
290
    if (! d->shadow)
291
        return false;
Thomas Zander's avatar
Thomas Zander committed
292

293 294
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
295
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
296

297
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
298 299
}

300
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
301
{
302
    Q_D(const KoShape);
303 304 305
    QSizeF mySize = size();
    QMatrix transform = absoluteTransformation(0);
    QRectF bb(QPointF(0, 0), mySize);
306
    if (d->border) {
307 308 309 310
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
311
    bb = transform.mapRect(bb);
312
    if (d->shadow) {
313
        KoInsets insets;
314
        d->shadow->insets(this, insets);
315 316
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
317 318 319 320 321
    if (d->filterEffectStack) {
        QRectF clipRect = d->filterEffectStack->clipRectForBoundingRect(QRectF(QPointF(), mySize));
        bb |= transform.mapRect(clipRect);
    }
    
322
    return bb;
Thomas Zander's avatar
Thomas Zander committed
323 324
}

325 326
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const
{
327
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
328 329
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
330
    KoShapeContainer * container = d->parent;
331 332
    if (container) {
        if (container->childClipped(this))
333
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
334
        else {
335 336 337 338
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
339
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
340 341 342
        }
    }

343
    if (converter) {
344 345 346
        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
347
    }
348 349

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
350 351
}

352
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
353
{
354
    QMatrix globalMatrix = absoluteTransformation(0);
355 356
    // 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
357
    // to be relative to the local coordinate system
358
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
359
    applyTransformation(transformMatrix);
360 361
}

362
void KoShape::applyTransformation(const QMatrix &matrix)
363
{
364
    Q_D(KoShape);
365
    d->localMatrix = matrix * d->localMatrix;
366
    notifyChanged();
367
    d->shapeChanged(GenericMatrixChange);
368 369
}

370
void KoShape::setTransformation(const QMatrix &matrix)
371
{
372
    Q_D(KoShape);
373 374
    d->localMatrix = matrix;
    notifyChanged();
375
    d->shapeChanged(GenericMatrixChange);
376
}
Thomas Zander's avatar
Thomas Zander committed
377

378
QMatrix KoShape::transformation() const
379
{
380
    Q_D(const KoShape);
381 382 383
    return d->localMatrix;
}

384 385
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    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();
401
        }
402 403 404 405

        if (!foundCommonParent) {
            index1 = parentShapeS1->zIndex();
            parentShapeS1 = parentShapeS1->parent();
406 407
        }
    }
408 409 410 411 412 413 414
    if (s1 == parentShapeS2) {
        return true;
    }
    else if (s2 == parentShapeS1) {
        return false;
    }
    return index1 < index2;
Thomas Zander's avatar
Thomas Zander committed
415 416
}

417 418
void KoShape::setParent(KoShapeContainer *parent)
{
419
    Q_D(KoShape);
420
    if (d->parent == parent)
421
        return;
422 423 424 425
    KoShapeContainer *oldParent = d->parent;
    d->parent = 0; // avoids recursive removing
    if (oldParent)
        oldParent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
426
    if (parent && parent != this) {
Thomas Zander's avatar
Thomas Zander committed
427
        d->parent = parent;
428
        parent->addChild(this);
429
    }
430
    notifyChanged();
431
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
432 433
}

434 435
int KoShape::zIndex() const
{
436
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
437
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
438 439
}

440 441
void KoShape::update() const
{
442
    Q_D(const KoShape);
443 444 445
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
        foreach(KoShapeManager * manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
446
            manager->update(rect, this, true);
Thomas Zander's avatar
Thomas Zander committed
447 448 449
    }
}

450 451
void KoShape::update(const QRectF &shape) const
{
452
    Q_D(const KoShape);
453
    if (!d->shapeManagers.empty() && isVisible()) {
454
        QRectF rect(absoluteTransformation(0).mapRect(shape));
455
        foreach(KoShapeManager * manager, d->shapeManagers) {
456
            manager->update(rect);
457 458
        }
    }
Thomas Zander's avatar
Thomas Zander committed
459 460
}

461
QPainterPath KoShape::outline() const
462
{
463
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
464
    QPainterPath path;
465
    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
466 467 468
    return path;
}

469 470
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
471
    QPointF point;
472 473 474 475 476 477
    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;
478
    }
479
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
480 481
}

482 483
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
484
    Q_D(KoShape);
485
    QPointF currentAbsPosition = absolutePosition(anchor);
486
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
487
    QMatrix translateMatrix;
488 489
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
490
    notifyChanged();
491
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
492 493
}

494 495
void KoShape::copySettings(const KoShape *shape)
{
496
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
497 498
    d->size = shape->size();
    d->connectors.clear();
Jan Hambrecht's avatar
Jan Hambrecht committed
499 500
    foreach(const QPointF & point, shape->connectionPoints())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
501 502
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
503

504 505 506 507 508
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
509

510
    d->geometryProtected = shape->isGeometryProtected();
Thomas Zander's avatar
Thomas Zander committed
511
    d->keepAspect = shape->keepAspectRatio();
512
    d->localMatrix = shape->d_ptr->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
513 514
}

Thomas Zander's avatar
Thomas Zander committed
515
void KoShape::notifyChanged()
516
{
517
    Q_D(KoShape);
518 519
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
520 521 522
    }
}

523 524
void KoShape::setUserData(KoShapeUserData *userData)
{
525
    Q_D(KoShape);
526
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
527
    d->userData = userData;
528 529
}

530 531
KoShapeUserData *KoShape::userData() const
{
532
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
533
    return d->userData;
534 535
}

536 537
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
538
    Q_D(KoShape);
539
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
540
    d->appData = appData;
541 542
}

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

549 550
bool KoShape::hasTransparency()
{
551
    Q_D(KoShape);
552
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
553
        return true;
554 555
    else
        return d->fill->hasTransparency();
Thomas Zander's avatar
Thomas Zander committed
556 557
}

558 559
KoInsets KoShape::borderInsets() const
{
560
    Q_D(const KoShape);
561
    KoInsets answer;
562
    if (d->border)
563
        d->border->borderInsets(this, answer);
564 565 566
    return answer;
}

567 568
qreal KoShape::rotation() const
{
569
    Q_D(const KoShape);
570
    // try to extract the rotation angle out of the local matrix
571 572 573
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
574
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
575
        return std::numeric_limits<qreal>::quiet_NaN();
576
    // check if the matrix has scaling mixed in
577
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
578
        return std::numeric_limits<qreal>::quiet_NaN();
579 580

    // calculate the angle from the matrix elements
581 582
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
583 584 585
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
586 587
}

588
QSizeF KoShape::size() const
589
{
590
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
591 592 593
    return d->size;
}

594 595
QPointF KoShape::position() const
{
596
    Q_D(const KoShape);
597 598
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
599
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
600 601
}

602
void KoShape::addConnectionPoint(const QPointF &point)
603
{
604
    Q_D(KoShape);
605 606
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
607
    d->connectors.append(QPointF(point.x() / s.width(), point.y() / s.height()));
Thomas Zander's avatar
Thomas Zander committed
608 609
}

610 611
QList<QPointF> KoShape::connectionPoints() const
{
612
    Q_D(const KoShape);
613 614 615
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
Jan Hambrecht's avatar
Jan Hambrecht committed
616 617
    foreach(const QPointF & cp, d->connectors)
        points.append(QPointF(s.width() * cp.x(), s.height() * cp.y()));
618 619

    return points;
620 621
}

622
void KoShape::addEventAction(KoEventAction * action)
623
{
624
    Q_D(KoShape);
625 626
    if (! d->eventActions.contains(action)) {
        d->eventActions.append(action);
627 628 629
    }
}

630
void KoShape::removeEventAction(KoEventAction * action)
631
{
632
    Q_D(KoShape);
633 634
    if (d->eventActions.contains(action)) {
        d->eventActions.removeAll(action);
635 636 637 638 639
    }
}

QList<KoEventAction *> KoShape::eventActions() const
{
640
    Q_D(const KoShape);
641 642 643
    return d->eventActions;
}

644
void KoShape::setBackground(KoShapeBackground * fill)
645
{
646
    Q_D(KoShape);
647
    if (d->fill)
648 649
        d->fill->removeUser();
    d->fill = fill;
650
    if (d->fill)
651
        d->fill->addUser();
652 653
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
654 655
}

656 657
KoShapeBackground * KoShape::background() const
{
658
    Q_D(const KoShape);
659
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
660 661
}

662 663
void KoShape::setZIndex(int zIndex)
{
664
    Q_D(KoShape);
665
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
666 667 668
    d->zIndex = zIndex;
}

669 670
void KoShape::setVisible(bool on)
{
671
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
672 673 674
    d->visible = on;
}

675
bool KoShape::isVisible(bool recursive) const
676
{
677
    Q_D(const KoShape);
678
    if (! recursive)
679
        return d->visible;
680
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
681 682
        return false;

683
    KoShapeContainer * parentShape = parent();
684 685
    while (parentShape) {
        if (! parentShape->isVisible())
686 687 688 689
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
690 691
}

692 693
void KoShape::setPrintable(bool on)
{
694
    Q_D(KoShape);
695 696
    d->printable = on;
}
697

698 699
bool KoShape::isPrintable() const
{
700
    Q_D(const KoShape);
701 702 703 704
    if (d->visible)
        return d->printable;
    else
        return false;
705 706
}

707 708
void KoShape::setSelectable(bool selectable)
{
709
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
710 711 712
    d->selectable = selectable;
}

713 714
bool KoShape::isSelectable() const
{
715
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
716 717 718
    return d->selectable;
}

719
void KoShape::setGeometryProtected(bool on)
720
{
721
    Q_D(KoShape);
722
    d->geometryProtected = on;
Thomas Zander's avatar
Thomas Zander committed
723 724
}

725
bool KoShape::isGeometryProtected() const
726
{
727
    Q_D(const KoShape);
728
    return d->geometryProtected;
Thomas Zander's avatar
Thomas Zander committed
729 730
}

731 732
void KoShape::setContentProtected(bool protect)
{
733
    Q_D(KoShape);
734 735 736 737 738
    d->protectContent = protect;
}

bool KoShape::isContentProtected() const
{
739
    Q_D(const KoShape);
740 741 742
    return d->protectContent;
}

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

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

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

761
QString KoShape::shapeId() const
762
{
763
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
764 765 766
    return d->shapeId;
}

767 768
void KoShape::setShapeId(const QString &id)
{
769
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
770 771 772
    d->shapeId = id;
}

773 774
void KoShape::setCollisionDetection(bool detect)
{
775
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
776 777 778
    d->detectCollision = detect;
}

779 780
bool KoShape::collisionDetection()
{
781
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
782 783 784
    return d->detectCollision;
}

785
void KoShape::addShapeManager(KoShapeManager * manager)
786
{
787
    Q_D(KoShape);
788
    d->shapeManagers.insert(manager);
Thomas Zander's avatar
Thomas Zander committed
789 790
}

791
void KoShape::removeShapeManager(KoShapeManager * manager)
792
{
793
    Q_D(KoShape);
794
    d->shapeManagers.remove(manager);
Thomas Zander's avatar
Thomas Zander committed
795 796
}

797 798
KoShapeBorderModel *KoShape::border() const
{
799
    Q_D(const KoShape);
800 801 802
    return d->border;
}

803 804
void KoShape::setBorder(KoShapeBorderModel *border)
{
805
    Q_D(KoShape);
806 807
    if (border)
        border->addUser();
808
    d->updateBorder();
809
    if (d->border)
810
        d->border->removeUser();
811
    d->border = border;
812
    d->updateBorder();
813 814
    d->shapeChanged(BorderChanged);
    notifyChanged();
815 816
}

817
void KoShape::setShadow(KoShapeShadow * shadow)
818
{
819
    Q_D(KoShape);
820
    if (d->shadow)
821 822
        d->shadow->removeUser();
    d->shadow = shadow;
823
    if (d->shadow) {
824
        d->shadow->addUser();
825 826
        // TODO update changed area
    }
827 828 829 830 831 832
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

KoShapeShadow * KoShape::shadow() const
{
833
    Q_D(const KoShape);
834 835 836
    return d->shadow;
}

837
QMatrix KoShape::matrix() const
838
{
839
    Q_D(const KoShape);
840
    return d->localMatrix;
841 842
}

843
void KoShape::removeConnectionPoint(int index)
844
{
845
    Q_D(KoShape);
846 847
    if (index < d->connectors.count())
        d->connectors.remove(index);
848 849
}

850 851
QString KoShape::name() const
{