KoShape.cpp 62.3 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,2011 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"
46
#include "KoClipPath.h"
Thorsten Zachmann's avatar
Thorsten Zachmann committed
47 48
#include "KoEventAction.h"
#include "KoEventActionRegistry.h"
49
#include "KoOdfWorkaround.h"
50
#include "KoFilterEffectStack.h"
51
#include <KoSnapData.h>
Thomas Zander's avatar
Thomas Zander committed
52

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

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

71 72
#include <limits>

73 74
// KoShapePrivate

75 76
KoShapePrivate::KoShapePrivate(KoShape *shape)
    : size(50, 50),
77 78 79 80 81 82 83
      parent(0),
      userData(0),
      appData(0),
      fill(0),
      border(0),
      q_ptr(shape),
      shadow(0),
84
      clipPath(0),
85 86 87
      filterEffectStack(0),
      transparency(0.0),
      zIndex(0),
88
      runThrough(0),
89 90 91 92 93 94 95
      visible(true),
      printable(true),
      geometryProtected(false),
      keepAspect(false),
      selectable(true),
      detectCollision(false),
      protectContent(false),
96
      textRunAroundSide(KoShape::BiggestRunAroundSide),
97
      textRunAroundDistance(1.0),
98 99
      textRunAroundThreshold(0.0),
      anchored(false)
100
{
101 102 103 104
    connectors[KoConnectionPoint::TopConnectionPoint] = KoConnectionPoint::defaultConnectionPoint(KoConnectionPoint::TopConnectionPoint);
    connectors[KoConnectionPoint::RightConnectionPoint] = KoConnectionPoint::defaultConnectionPoint(KoConnectionPoint::RightConnectionPoint);
    connectors[KoConnectionPoint::BottomConnectionPoint] = KoConnectionPoint::defaultConnectionPoint(KoConnectionPoint::BottomConnectionPoint);
    connectors[KoConnectionPoint::LeftConnectionPoint] = KoConnectionPoint::defaultConnectionPoint(KoConnectionPoint::LeftConnectionPoint);
105
}
Thomas Zander's avatar
Thomas Zander committed
106

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

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

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

162 163 164 165 166 167 168 169
void KoShapePrivate::addShapeManager(KoShapeManager *manager)
{
    shapeManagers.insert(manager);
}

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

172
void KoShapePrivate::convertFromShapeCoordinates(KoConnectionPoint &point, const QSizeF &shapeSize) const
173
{
Jan Hambrecht's avatar
Jan Hambrecht committed
174
    switch(point.alignment) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        case KoConnectionPoint::AlignNone:
            point.position = KoFlake::toRelative(point.position, shapeSize);
            point.position.rx() = qBound<qreal>(0.0, point.position.x(), 1.0);
            point.position.ry() = qBound<qreal>(0.0, point.position.y(), 1.0);
            break;
        case KoConnectionPoint::AlignRight:
            point.position.rx() -= shapeSize.width();
        case KoConnectionPoint::AlignLeft:
            point.position.ry() = 0.5*shapeSize.height();
            break;
        case KoConnectionPoint::AlignBottom:
            point.position.ry() -= shapeSize.height();
        case KoConnectionPoint::AlignTop:
            point.position.rx() = 0.5*shapeSize.width();
            break;
        case KoConnectionPoint::AlignTopLeft:
            // nothing to do here
            break;
        case KoConnectionPoint::AlignTopRight:
            point.position.rx() -= shapeSize.width();
            break;
        case KoConnectionPoint::AlignBottomLeft:
            point.position.ry() -= shapeSize.height();
            break;
        case KoConnectionPoint::AlignBottomRight:
            point.position.rx() -= shapeSize.width();
            point.position.ry() -= shapeSize.height();
            break;
        case KoConnectionPoint::AlignCenter:
            point.position.rx() -= 0.5 * shapeSize.width();
            point.position.ry() -= 0.5 * shapeSize.height();
            break;
    }
}

void KoShapePrivate::convertToShapeCoordinates(KoConnectionPoint &point, const QSizeF &shapeSize) const
{
Jan Hambrecht's avatar
Jan Hambrecht committed
212
    switch(point.alignment) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        case KoConnectionPoint::AlignNone:
            point.position = KoFlake::toAbsolute(point.position, shapeSize);
            break;
        case KoConnectionPoint::AlignRight:
            point.position.rx() += shapeSize.width();
        case KoConnectionPoint::AlignLeft:
            point.position.ry() = 0.5*shapeSize.height();
            break;
        case KoConnectionPoint::AlignBottom:
            point.position.ry() += shapeSize.height();
        case KoConnectionPoint::AlignTop:
            point.position.rx() = 0.5*shapeSize.width();
            break;
        case KoConnectionPoint::AlignTopLeft:
            // nothing to do here
            break;
        case KoConnectionPoint::AlignTopRight:
            point.position.rx() += shapeSize.width();
            break;
        case KoConnectionPoint::AlignBottomLeft:
            point.position.ry() += shapeSize.height();
            break;
        case KoConnectionPoint::AlignBottomRight:
            point.position.rx() += shapeSize.width();
            point.position.ry() += shapeSize.height();
            break;
        case KoConnectionPoint::AlignCenter:
            point.position.rx() += 0.5 * shapeSize.width();
            point.position.ry() += 0.5 * shapeSize.height();
            break;
    }
244 245
}

246
// static
247
QString KoShapePrivate::getStyleProperty(const char *property, KoShapeLoadingContext &context)
248 249 250 251 252 253 254 255 256 257 258
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
    QString value;

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

    return value;
}

259 260


261
// ======== KoShape
Thomas Zander's avatar
Thomas Zander committed
262
KoShape::KoShape()
263
    : d_ptr(new KoShapePrivate(this))
Thomas Zander's avatar
Thomas Zander committed
264
{
265
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
266 267
}

268 269 270 271 272
KoShape::KoShape(KoShapePrivate &dd)
    : d_ptr(&dd)
{
}

Thomas Zander's avatar
Thomas Zander committed
273 274
KoShape::~KoShape()
{
275
    Q_D(KoShape);
276
    d->shapeChanged(Deleted);
277
    delete d_ptr;
Thomas Zander's avatar
Thomas Zander committed
278 279
}

280 281
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas)
{
Thomas Zander's avatar
Thomas Zander committed
282 283 284
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
285
    /* Since this code is not actually used (kivio is going to be the main user) lets disable instead of fix.
286
        if (selected)
Thomas Zander's avatar
Thomas Zander committed
287
        {
288
            // draw connectors
289 290 291 292 293
            QPen pen(Qt::blue);
            pen.setWidth(0);
            painter.setPen(pen);
            painter.setBrush(Qt::NoBrush);
            for (int i = 0; i < d->connectors.size(); ++i)
294
          {
295
                QPointF p = converter.documentToView(d->connectors[ i ]);
296 297
                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));
298 299
            }
        }*/
Thomas Zander's avatar
Thomas Zander committed
300 301
}

Thomas Zander's avatar
Thomas Zander committed
302
void KoShape::scale(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
303
{
304
    Q_D(KoShape);
305
    QPointF pos = position();
306
    QTransform scaleMatrix;
307 308 309
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
310
    d->localMatrix = d->localMatrix * scaleMatrix;
311

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

316
void KoShape::rotate(qreal angle)
Thomas Zander's avatar
Thomas Zander committed
317
{
318
    Q_D(KoShape);
319
    QPointF center = d->localMatrix.map(QPointF(0.5 * size().width(), 0.5 * size().height()));
320
    QTransform rotateMatrix;
321 322 323
    rotateMatrix.translate(center.x(), center.y());
    rotateMatrix.rotate(angle);
    rotateMatrix.translate(-center.x(), -center.y());
324
    d->localMatrix = d->localMatrix * rotateMatrix;
325

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

Thomas Zander's avatar
Thomas Zander committed
330
void KoShape::shear(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
331
{
332
    Q_D(KoShape);
333
    QPointF pos = position();
334
    QTransform shearMatrix;
335 336 337
    shearMatrix.translate(pos.x(), pos.y());
    shearMatrix.shear(sx, sy);
    shearMatrix.translate(-pos.x(), -pos.y());
338
    d->localMatrix = d->localMatrix * shearMatrix;
339

340
    notifyChanged();
341
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
342 343
}

344
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
345
{
346
    Q_D(KoShape);
347
    QSizeF oldSize(size());
348

349
    // always set size, as d->size and size() may vary
Thomas Zander's avatar
Thomas Zander committed
350
    d->size = newSize;
351

352 353 354
    if (oldSize == newSize)
        return;

355
    notifyChanged();
356
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
357 358
}

359
void KoShape::setPosition(const QPointF &newPosition)
Thomas Zander's avatar
Thomas Zander committed
360
{
361
    Q_D(KoShape);
362
    QPointF currentPos = position();
363
    if (newPosition == currentPos)
Thomas Zander's avatar
Thomas Zander committed
364
        return;
365
    QTransform translateMatrix;
366
    translateMatrix.translate(newPosition.x() - currentPos.x(), newPosition.y() - currentPos.y());
367 368 369
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
370
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
371 372
}

373
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
374
{
375
    Q_D(const KoShape);
376
    if (d->parent && d->parent->isClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
377 378
        return false;

379
    QPointF point = absoluteTransformation(0).inverted().map(position);
380
    QRectF bb(QPointF(), size());
381
    if (d->border) {
382
        KoInsets insets;
383
        d->border->borderInsets(this, insets);
384
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
385
    }
386 387 388
    if (bb.contains(point))
        return true;

389
    // if there is no shadow we can as well just leave
390
    if (! d->shadow)
391
        return false;
Thomas Zander's avatar
Thomas Zander committed
392

393 394
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
395
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
396

397
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
398 399
}

400
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
401
{
402
    Q_D(const KoShape);
403
    QSizeF mySize = size();
404
    QTransform transform = absoluteTransformation(0);
405
    QRectF bb = outlineRect();
406
    if (d->border) {
407 408 409 410
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
411
    bb = transform.mapRect(bb);
412
    if (d->shadow) {
413
        KoInsets insets;
Thomas Zander's avatar
Thomas Zander committed
414
        d->shadow->insets(insets);
415 416
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
417
    if (d->filterEffectStack) {
418
        QRectF clipRect = d->filterEffectStack->clipRectForBoundingRect(outlineRect());
419 420
        bb |= transform.mapRect(clipRect);
    }
Thomas Zander's avatar
Thomas Zander committed
421

422
    return bb;
Thomas Zander's avatar
Thomas Zander committed
423 424
}

425
QTransform KoShape::absoluteTransformation(const KoViewConverter *converter) const
426
{
427
    Q_D(const KoShape);
428
    QTransform matrix;
Thomas Zander's avatar
Thomas Zander committed
429
    // apply parents matrix to inherit any transformations done there.
430
    KoShapeContainer * container = d->parent;
431
    if (container) {
432
        if (container->inheritsTransform(this)) {
433 434 435 436
            // We do need to pass the converter here, otherwise the parent's
            // translation is not inherited.
            matrix = container->absoluteTransformation(converter);
        } else {
437 438 439 440
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
441
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
442 443 444
        }
    }

445
    if (converter) {
446 447 448
        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
449
    }
450 451

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
452 453
}

454
void KoShape::applyAbsoluteTransformation(const QTransform &matrix)
455
{
456
    QTransform globalMatrix = absoluteTransformation(0);
457 458
    // 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
459
    // to be relative to the local coordinate system
460
    QTransform transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
461
    applyTransformation(transformMatrix);
462 463
}

464
void KoShape::applyTransformation(const QTransform &matrix)
465
{
466
    Q_D(KoShape);
467
    d->localMatrix = matrix * d->localMatrix;
468
    notifyChanged();
469
    d->shapeChanged(GenericMatrixChange);
470 471
}

472
void KoShape::setTransformation(const QTransform &matrix)
473
{
474
    Q_D(KoShape);
475 476
    d->localMatrix = matrix;
    notifyChanged();
477
    d->shapeChanged(GenericMatrixChange);
478
}
Thomas Zander's avatar
Thomas Zander committed
479

480
QTransform KoShape::transformation() const
481
{
482
    Q_D(const KoShape);
483 484 485
    return d->localMatrix;
}

486 487
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
488 489 490 491 492 493 494
    if(s1->runThrough() > s2->runThrough()) {
        return false;
    }
    if(s1->runThrough() < s2->runThrough()) {
        return true;
    }

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    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();
510
        }
511 512 513 514

        if (!foundCommonParent) {
            index1 = parentShapeS1->zIndex();
            parentShapeS1 = parentShapeS1->parent();
515 516
        }
    }
517 518 519 520 521 522 523
    if (s1 == parentShapeS2) {
        return true;
    }
    else if (s2 == parentShapeS1) {
        return false;
    }
    return index1 < index2;
Thomas Zander's avatar
Thomas Zander committed
524 525
}

526 527
void KoShape::setParent(KoShapeContainer *parent)
{
528
    Q_D(KoShape);
529
    if (d->parent == parent)
530
        return;
531 532 533
    KoShapeContainer *oldParent = d->parent;
    d->parent = 0; // avoids recursive removing
    if (oldParent)
534
        oldParent->removeShape(this);
Thomas Zander's avatar
Thomas Zander committed
535
    if (parent && parent != this) {
Thomas Zander's avatar
Thomas Zander committed
536
        d->parent = parent;
537
        parent->addShape(this);
538
    }
539
    notifyChanged();
540
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
541 542
}

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

549 550
void KoShape::update() const
{
551
    Q_D(const KoShape);
552

553 554
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
555
        foreach(KoShapeManager * manager, d->shapeManagers) {
Jan Hambrecht's avatar
Jan Hambrecht committed
556
            manager->update(rect, this, true);
557
        }
Thomas Zander's avatar
Thomas Zander committed
558 559 560
    }
}

561
void KoShape::update(const QRectF &rect) const
562
{
563 564 565 566 567

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

568
    Q_D(const KoShape);
569

570
    if (!d->shapeManagers.empty() && isVisible()) {
571
        QRectF rc(absoluteTransformation(0).mapRect(rect));
572
        foreach(KoShapeManager * manager, d->shapeManagers) {
573
            manager->update(rc);
574 575
        }
    }
Thomas Zander's avatar
Thomas Zander committed
576 577
}

578
QPainterPath KoShape::outline() const
579
{
Thomas Zander's avatar
Thomas Zander committed
580
    QPainterPath path;
581
    path.addRect(outlineRect());
Thomas Zander's avatar
Thomas Zander committed
582 583 584
    return path;
}

585 586
QRectF KoShape::outlineRect() const
{
587 588
    const QSizeF s = size();
    return QRectF(QPointF(0, 0), QSizeF(qMax(s.width(), qreal(0.0001)), qMax(s.height(), qreal(0.0001))));
589 590
}

591 592
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
593
    QPointF point;
594 595 596 597 598 599
    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;
600
    }
601
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
602 603
}

604 605
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
606
    Q_D(KoShape);
607
    QPointF currentAbsPosition = absolutePosition(anchor);
608
    QPointF translate = newPosition - currentAbsPosition;
609
    QTransform translateMatrix;
610 611
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
612
    notifyChanged();
613
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
614 615
}

616 617
void KoShape::copySettings(const KoShape *shape)
{
618
    Q_D(KoShape);
Thomas Zander's avatar
Thomas Zander committed
619 620
    d->size = shape->size();
    d->connectors.clear();
621
    foreach(const KoConnectionPoint &point, shape->connectionPoints())
Jan Hambrecht's avatar
Jan Hambrecht committed
622
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
623 624
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
625

626 627 628 629 630
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
631

632
    d->geometryProtected = shape->isGeometryProtected();
Thomas Zander's avatar
Thomas Zander committed
633 634
    d->protectContent = shape->isContentProtected();
    d->selectable = shape->isSelectable();
Thomas Zander's avatar
Thomas Zander committed
635
    d->keepAspect = shape->keepAspectRatio();
636
    d->localMatrix = shape->d_ptr->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
637 638
}

Thomas Zander's avatar
Thomas Zander committed
639
void KoShape::notifyChanged()
640
{
641
    Q_D(KoShape);
642 643
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
644 645 646
    }
}

647 648
void KoShape::setUserData(KoShapeUserData *userData)
{
649
    Q_D(KoShape);
650
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
651
    d->userData = userData;
652 653
}

654 655
KoShapeUserData *KoShape::userData() const
{
656
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
657
    return d->userData;
658 659
}

660 661
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
662
    Q_D(KoShape);
663
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
664
    d->appData = appData;
665 666
}

667 668
KoShapeApplicationData *KoShape::applicationData() const
{
669
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
670
    return d->appData;
671 672
}

Thomas Zander's avatar
Thomas Zander committed
673
bool KoShape::hasTransparency() const
674
{
Thomas Zander's avatar
Thomas Zander committed
675
    Q_D(const KoShape);
676
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
677
        return true;
678
    else
679 680 681 682 683 684
        return d->fill->hasTransparency() || d->transparency > 0.0;
}

void KoShape::setTransparency(qreal transparency)
{
    Q_D(KoShape);
685
    d->transparency = qBound<qreal>(0.0, transparency, 1.0);
686 687 688 689 690 691 692 693 694 695 696 697
}

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
698 699
}

700 701
KoInsets KoShape::borderInsets() const
{
702
    Q_D(const KoShape);
703
    KoInsets answer;
704
    if (d->border)
705
        d->border->borderInsets(this, answer);
706 707 708
    return answer;
}

709 710
qreal KoShape::rotation() const
{
711
    Q_D(const KoShape);
712
    // try to extract the rotation angle out of the local matrix
713 714 715
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
716
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
717
        return std::numeric_limits<qreal>::quiet_NaN();
718
    // check if the matrix has scaling mixed in
719
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
720
        return std::numeric_limits<qreal>::quiet_NaN();
721 722

    // calculate the angle from the matrix elements
723 724
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
725 726 727
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
728 729
}

730
QSizeF KoShape::size() const
731
{
732
    Q_D(const KoShape);
Thomas Zander's avatar
Thomas Zander committed
733 734 735
    return d->size;
}

736 737
QPointF KoShape::position() const
{
738
    Q_D(const KoShape);
739 740
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
Thomas Zander's avatar
Thomas Zander committed
741 742
}

743
int KoShape::addConnectionPoint(const KoConnectionPoint &point)
744
{
745
    Q_D(KoShape);
746

747
    // get next glue point id
748
    int nextConnectionPointId = KoConnectionPoint::FirstCustomConnectionPoint;
Jan Hambrecht's avatar
Jan Hambrecht committed
749
    if (d->connectors.size())
750 751
        nextConnectionPointId = qMax(nextConnectionPointId, (--d->connectors.end()).key()+1);

752
    KoConnectionPoint p = point;
753 754
    d->convertFromShapeCoordinates(p, size());
    d->connectors[nextConnectionPointId] = p;
755 756 757 758

    return nextConnectionPointId;
}

759
bool KoShape::setConnectionPoint(int connectionPointId, const KoConnectionPoint &point)
760 761
{
    Q_D(KoShape);
762
    if (connectionPointId < 0)
763 764
        return false;

765 766
    const bool insertPoint = !hasConnectionPoint(connectionPointId);

767
    switch(connectionPointId) {
768 769 770 771 772 773
        case KoConnectionPoint::TopConnectionPoint:
        case KoConnectionPoint::RightConnectionPoint:
        case KoConnectionPoint::BottomConnectionPoint:
        case KoConnectionPoint::LeftConnectionPoint:
        {
            KoConnectionPoint::PointId id = static_cast<KoConnectionPoint::PointId>(connectionPointId);
774
            d->connectors[id] = KoConnectionPoint::defaultConnectionPoint(id);
775
            break;
776
        }
777
        default:
778
        {
779
            KoConnectionPoint p = point;
780 781
            d->convertFromShapeCoordinates(p, size());
            d->connectors[connectionPointId] = p;
782
            break;
783
        }
784 785
    }

786 787
    if(!insertPoint)
        d->shapeChanged(ConnectionPointChanged);
788 789 790 791

    return true;
}

792 793 794 795 796 797
bool KoShape::hasConnectionPoint(int connectionPointId) const
{
    Q_D(const KoShape);
    return d->connectors.contains(connectionPointId);
}

798
KoConnectionPoint KoShape::connectionPoint(int connectionPointId) const
799 800
{
    Q_D(const KoShape);
801
    KoConnectionPoint p = d->connectors.value(connectionPointId, KoConnectionPoint());
802
    // convert glue point to shape coordinates
803
    d->convertToShapeCoordinates(p, size());
804
    return p;
Thomas Zander's avatar
Thomas Zander committed
805 806
}

807
KoConnectionPoints KoShape::connectionPoints() const
808
{
809
    Q_D(const KoShape);
810
    QSizeF s = size();
811 812 813
    KoConnectionPoints points = d->connectors;
    KoConnectionPoints::iterator point = points.begin();
    KoConnectionPoints::iterator lastPoint = points.end();
814
    // convert glue points to shape coordinates
815
    for(; point != lastPoint; ++point) {
816
        d->convertToShapeCoordinates(point.value(), s);
817
    }
818 819

    return points;
820 821
}

822 823 824 825
void KoShape::removeConnectionPoint(int connectionPointId)
{
    Q_D(KoShape);
    d->connectors.remove(connectionPointId);
826
    d->shapeChanged(ConnectionPointChanged);
827 828 829 830 831 832 833 834
}

void KoShape::clearConnectionPoints()
{
    Q_D(KoShape);
    d->connectors.clear();
}

835
void KoShape::addEventAction(KoEventAction *action)
836
{
837
    Q_D(KoShape);
838
    d->eventActions.insert(action);
839 840
}

841
void KoShape::removeEventAction(KoEventAction *action)
842
{
843
    Q_D(KoShape);
844
    d->eventActions.remove(action);
845 846
}

847
QSet<KoEventAction *> KoShape::eventActions() const
848
{
849
    Q_D(const KoShape);
850 851 852
    return d->eventActions;
}

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
KoShape::TextRunAroundSide KoShape::textRunAroundSide() const
{
    Q_D(const KoShape);
    return d->textRunAroundSide;
}

void KoShape::setTextRunAroundSide(TextRunAroundSide side, Through runThrought)
{
    Q_D(KoShape);

    if (side == RunThrough) {
        if (runThrought == Background) {
            setRunThrough(-1);
        } else {
            setRunThrough(1);
        }
    } else {
        setRunThrough(0);
    }
872 873 874 875 876 877 878 879

    if ( d->textRunAroundSide == side) {
        return;
    }

    d->textRunAroundSide = side;
    notifyChanged();
    d->shapeChanged(TextRunAroundChanged);
880 881 882 883 884 885 886 887 888 889 890 891 892 893
}

qreal KoShape::textRunAroundDistance() const
{
    Q_D(const KoShape);
    return d->textRunAroundDistance;
}

void KoShape::setTextRunAroundDistance(qreal distance)
{
    Q_D(KoShape);
    d->textRunAroundDistance = distance;
}

894 895 896 897 898 899
qreal KoShape::textRunAroundThreshold() const
{
    Q_D(const KoShape);
    return d->textRunAroundThreshold;
}

900 901 902 903 904 905 906 907 908 909 910 911
void KoShape::setAnchored(bool anchored)
{
    Q_D(KoShape);
    d->anchored