KoShape.cpp 37.8 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 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 24

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

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

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

65 66
#include <kdebug.h>

67 68
#include <limits>

69 70
class KoShape::Private
{
Thomas Zander's avatar
Thomas Zander committed
71
public:
72
    Private(KoShape *shape)
73 74
            : size(50, 50),
            parent(0),
75 76 77 78 79 80 81
            userData(0),
            appData(0),
            fill(0),
            border(0),
            me(shape),
            shadow(0),
            zIndex(0),
82 83 84 85 86 87
            visible(true),
            printable(true),
            locked(false),
            keepAspect(false),
            selectable(true),
            detectCollision(false),
88 89
            protectContent(false)
    {
Thomas Zander's avatar
Thomas Zander committed
90 91
    }

92
    ~Private() {
93 94
        if (parent)
            parent->removeChild(me);
Jan Hambrecht's avatar
Jan Hambrecht committed
95
        foreach(KoShapeManager *manager, shapeManagers) {
Jan Hambrecht's avatar
Jan Hambrecht committed
96
            manager->remove(me);
Jan Hambrecht's avatar
Jan Hambrecht committed
97 98
            manager->removeAdditional(me);
        }
Thomas Zander's avatar
Thomas Zander committed
99 100
        delete userData;
        delete appData;
101
        if (border && ! border->removeUser())
102
            delete border;
103
        if (shadow && ! shadow->removeUser())
104
            delete shadow;
105
        if (fill && ! fill->removeUser())
106
            delete fill;
107
        qDeleteAll(eventActions);
Thomas Zander's avatar
Thomas Zander committed
108 109
    }

110
    void shapeChanged(ChangeType type) {
111
        if (parent)
112 113
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
114
        foreach(KoShape * shape, dependees)
Jan Hambrecht's avatar
Jan Hambrecht committed
115
            shape->notifyShapeChanged(me, type);
116 117
    }

Thomas Zander's avatar
Thomas Zander committed
118 119
    QSizeF size; // size in pt
    QString shapeId;
120
    QString name; ///< the shapes names
Thomas Zander's avatar
Thomas Zander committed
121

122
    QMatrix localMatrix; ///< the shapes local transformation matrix
Thomas Zander's avatar
Thomas Zander committed
123 124 125 126 127 128 129

    QVector<QPointF> connectors; // in pt

    KoShapeContainer *parent;
    QSet<KoShapeManager *> shapeManagers;
    KoShapeUserData *userData;
    KoShapeApplicationData *appData;
130
    KoShapeBackground * fill; ///< Stands for the background color / fill etc.
131
    KoShapeBorderModel *border; ///< points to a border, or 0 if there is no border
132
    KoShape *me;
133
    QList<KoShape*> dependees; ///< list of shape dependent on this shape
134
    KoShapeShadow * shadow; ///< the current shape shadow
135
    QMap<QByteArray, QString> additionalAttributes;
136
    QMap<QByteArray, QString> additionalStyleAttributes;
Thorsten Zachmann's avatar
Thorsten Zachmann committed
137
    QList<KoEventAction *> eventActions; ///< list of event actions the shape has
138

139 140 141 142 143 144 145 146
    int zIndex : 14; // should be enough ;)
    int visible : 1;
    int printable : 1;
    int locked : 1;
    int keepAspect : 1;
    int selectable : 1;
    int detectCollision : 1;
    int protectContent : 1;
Thomas Zander's avatar
Thomas Zander committed
147 148
};

Thomas Zander's avatar
Thomas Zander committed
149
KoShape::KoShape()
150
        : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
151
{
152 153 154 155
    d->connectors.append(QPointF(0.5, 0.0));
    d->connectors.append(QPointF(1.0, 0.5));
    d->connectors.append(QPointF(0.5, 1.0));
    d->connectors.append(QPointF(0.0, 0.5));
156

157
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
158 159 160 161
}

KoShape::~KoShape()
{
162
    d->shapeChanged(Deleted);
Thomas Zander's avatar
Thomas Zander committed
163
    delete d;
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 191
    QPointF pos = position();
    QMatrix scaleMatrix;
192 193 194
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
195
    d->localMatrix = d->localMatrix * scaleMatrix;
196

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

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

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

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

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

227
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
228
{
229 230
    QSizeF oldSize(size());
    if (oldSize == newSize)
Thomas Zander's avatar
Thomas Zander committed
231
        return;
232

233
    QMatrix matrix;
234 235
    oldSize.setHeight(qMax((qreal) 1E-4, oldSize.height())); // avoids devision by zero below
    oldSize.setWidth(qMax((qreal) 1E-4, oldSize.width()));
236
    matrix.scale(newSize.width()/oldSize.width(), newSize.height()/oldSize.height());
237 238

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

Thomas Zander's avatar
Thomas Zander committed
249
    d->size = newSize;
250

251
    notifyChanged();
252
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
253 254
}

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

    notifyChanged();
265
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
266 267
}

268
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
269
{
270
    if (d->parent && d->parent->childClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
271 272
        return false;

273
    QPointF point = absoluteTransformation(0).inverted().map(position);
274
    QRectF bb(QPointF(), size());
275
    if (d->border) {
276
        KoInsets insets;
277
        d->border->borderInsets(this, insets);
278
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
279
    }
280 281 282
    if (bb.contains(point))
        return true;

283
    // if there is no shadow we can as well just leave
284
    if (! d->shadow)
285
        return false;
Thomas Zander's avatar
Thomas Zander committed
286

287 288
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
289
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
290

291
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
292 293
}

294
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
295
{
296
    QRectF bb(QPointF(0, 0), size());
297
    if (d->border) {
298 299 300 301
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
302 303
    bb = absoluteTransformation(0).mapRect(bb);
    if (d->shadow) {
304
        KoInsets insets;
305
        d->shadow->insets(this, insets);
306 307 308
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
    return bb;
Thomas Zander's avatar
Thomas Zander committed
309 310
}

311 312
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const
{
Thomas Zander's avatar
Thomas Zander committed
313 314
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
315
    KoShapeContainer * container = d->parent;
316 317
    if (container) {
        if (container->childClipped(this))
Johannes Simon's avatar
Johannes Simon committed
318
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
319
        else {
320 321 322 323
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
324
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
325 326 327
        }
    }

328
    if (converter) {
329 330 331
        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
332
    }
333 334

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
335 336
}

337
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
338
{
339
    QMatrix globalMatrix = absoluteTransformation(0);
340 341
    // 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
342
    // to be relative to the local coordinate system
343
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
344
    applyTransformation(transformMatrix);
345 346
}

347
void KoShape::applyTransformation(const QMatrix &matrix)
348 349
{
    d->localMatrix = matrix * d->localMatrix;
350
    notifyChanged();
351
    d->shapeChanged(GenericMatrixChange);
352 353
}

354
void KoShape::setTransformation(const QMatrix &matrix)
355 356 357
{
    d->localMatrix = matrix;
    notifyChanged();
358
    d->shapeChanged(GenericMatrixChange);
359
}
Thomas Zander's avatar
Thomas Zander committed
360

361
QMatrix KoShape::transformation() const
362 363 364 365
{
    return d->localMatrix;
}

366 367
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
368
    int diff = s1->zIndex() - s2->zIndex();
369
    if (diff == 0) {
370
        KoShape *s = s1->parent();
371
        while (s) {
372
            if (s == s2) // s1 is a child of s2
373 374 375 376
                return false; // children are always on top of their parents.
            s = s->parent();
        }
        s = s2->parent();
377
        while (s) {
378
            if (s == s1) // s2 is a child of s1
379 380 381 382 383
                return true;
            s = s->parent();
        }
    }
    return diff < 0;
Thomas Zander's avatar
Thomas Zander committed
384 385
}

386 387 388
void KoShape::setParent(KoShapeContainer *parent)
{
    if (d->parent == parent)
389
        return;
390
    if (parent && dynamic_cast<KoShape*>(parent) != this) {
391 392
        if (d->parent)
            d->parent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
393
        d->parent = parent;
394
        parent->addChild(this);
395
    } else
Thomas Zander's avatar
Thomas Zander committed
396
        d->parent = 0;
397
    notifyChanged();
398
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
399 400
}

401 402 403
int KoShape::zIndex() const
{
    if (parent()) // we can't be under our parent...
404
        return qMax((int) d->zIndex, parent()->zIndex());
Thomas Zander's avatar
Thomas Zander committed
405
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
406 407
}

408 409
void KoShape::update() const
{
410 411 412
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
        foreach(KoShapeManager * manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
413
            manager->update(rect, this, true);
Thomas Zander's avatar
Thomas Zander committed
414 415 416
    }
}

417 418
void KoShape::update(const QRectF &shape) const
{
419
    if (!d->shapeManagers.empty() && isVisible()) {
420
        QRectF rect(absoluteTransformation(0).mapRect(shape));
421
        foreach(KoShapeManager * manager, d->shapeManagers) {
422
            manager->update(rect);
423 424
        }
    }
Thomas Zander's avatar
Thomas Zander committed
425 426
}

427 428
const QPainterPath KoShape::outline() const
{
Thomas Zander's avatar
Thomas Zander committed
429
    QPainterPath path;
430
    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
431 432 433
    return path;
}

434 435
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
436
    QPointF point;
437 438 439 440 441 442
    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;
443
    }
444
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
445 446
}

447 448
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
449
    QPointF currentAbsPosition = absolutePosition(anchor);
450
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
451
    QMatrix translateMatrix;
452 453
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
454
    notifyChanged();
455
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
456 457
}

458 459
void KoShape::copySettings(const KoShape *shape)
{
Thomas Zander's avatar
Thomas Zander committed
460 461
    d->size = shape->size();
    d->connectors.clear();
Jan Hambrecht's avatar
Jan Hambrecht committed
462 463
    foreach(const QPointF & point, shape->connectionPoints())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
464 465
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
466

467 468 469 470 471
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
472

Thomas Zander's avatar
Thomas Zander committed
473 474
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
475
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
476 477
}

Thomas Zander's avatar
Thomas Zander committed
478
void KoShape::notifyChanged()
479
{
480 481
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
482 483 484
    }
}

485 486
void KoShape::setUserData(KoShapeUserData *userData)
{
Thomas Zander's avatar
Thomas Zander committed
487
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
488
    d->userData = userData;
489 490
}

491 492
KoShapeUserData *KoShape::userData() const
{
Thomas Zander's avatar
Thomas Zander committed
493
    return d->userData;
494 495
}

496 497
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
Thomas Zander's avatar
Thomas Zander committed
498
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
499
    d->appData = appData;
500 501
}

502 503
KoShapeApplicationData *KoShape::applicationData() const
{
Thomas Zander's avatar
Thomas Zander committed
504
    return d->appData;
505 506
}

507 508
bool KoShape::hasTransparency()
{
509
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
510
        return true;
511 512
    else
        return d->fill->hasTransparency();
Thomas Zander's avatar
Thomas Zander committed
513 514
}

515 516
KoInsets KoShape::borderInsets() const
{
517
    KoInsets answer;
518
    if (d->border)
519
        d->border->borderInsets(this, answer);
520 521 522
    return answer;
}

523 524
qreal KoShape::rotation() const
{
525
    // try to extract the rotation angle out of the local matrix
526 527 528
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
529
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
530
        return std::numeric_limits<qreal>::quiet_NaN();
531
    // check if the matrix has scaling mixed in
532
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
533
        return std::numeric_limits<qreal>::quiet_NaN();
534 535

    // calculate the angle from the matrix elements
536 537
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
538 539 540
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
541 542
}

543
QSizeF KoShape::size() const
544
{
Thomas Zander's avatar
Thomas Zander committed
545 546 547
    return d->size;
}

548 549
QPointF KoShape::position() const
{
550 551
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
552
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
553 554
}

555
void KoShape::addConnectionPoint(const QPointF &point)
556
{
557 558
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
559
    d->connectors.append(QPointF(point.x() / s.width(), point.y() / s.height()));
Thomas Zander's avatar
Thomas Zander committed
560 561
}

562 563
QList<QPointF> KoShape::connectionPoints() const
{
564 565 566
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
Jan Hambrecht's avatar
Jan Hambrecht committed
567 568
    foreach(const QPointF & cp, d->connectors)
        points.append(QPointF(s.width() * cp.x(), s.height() * cp.y()));
569 570

    return points;
571 572
}

573
void KoShape::addEventAction(KoEventAction * action)
574
{
575 576
    if (! d->eventActions.contains(action)) {
        d->eventActions.append(action);
577 578 579
    }
}

580
void KoShape::removeEventAction(KoEventAction * action)
581
{
582 583
    if (d->eventActions.contains(action)) {
        d->eventActions.removeAll(action);
584 585 586 587 588 589 590 591
    }
}

QList<KoEventAction *> KoShape::eventActions() const
{
    return d->eventActions;
}

592
void KoShape::setBackground(KoShapeBackground * fill)
593 594
{
    if (d->fill)
595 596
        d->fill->removeUser();
    d->fill = fill;
597
    if (d->fill)
598
        d->fill->addUser();
599 600
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
601 602
}

603 604
KoShapeBackground * KoShape::background() const
{
605
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
606 607
}

608 609
void KoShape::setZIndex(int zIndex)
{
610
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
611 612 613
    d->zIndex = zIndex;
}

614 615
void KoShape::setVisible(bool on)
{
Thomas Zander's avatar
Thomas Zander committed
616 617 618
    d->visible = on;
}

619
bool KoShape::isVisible(bool recursive) const
620
{
621
    if (! recursive)
622
        return d->visible;
623
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
624 625
        return false;

626
    KoShapeContainer * parentShape = parent();
627 628
    while (parentShape) {
        if (! parentShape->isVisible())
629 630 631 632
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
633 634
}

635 636 637 638
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
639

640 641
bool KoShape::isPrintable() const
{
642 643 644 645
    if (d->visible)
        return d->printable;
    else
        return false;
646 647
}

648 649
void KoShape::setSelectable(bool selectable)
{
Thomas Zander's avatar
Thomas Zander committed
650 651 652
    d->selectable = selectable;
}

653 654
bool KoShape::isSelectable() const
{
Thomas Zander's avatar
Thomas Zander committed
655 656 657
    return d->selectable;
}

658 659
void KoShape::setLocked(bool locked)
{
Thomas Zander's avatar
Thomas Zander committed
660 661 662
    d->locked = locked;
}

663 664
bool KoShape::isLocked() const
{
Thomas Zander's avatar
Thomas Zander committed
665 666 667
    return d->locked;
}

668 669 670 671 672 673 674 675 676 677
void KoShape::setContentProtected(bool protect)
{
    d->protectContent = protect;
}

bool KoShape::isContentProtected() const
{
    return d->protectContent;
}

678 679
KoShapeContainer *KoShape::parent() const
{
Thomas Zander's avatar
Thomas Zander committed
680 681 682
    return d->parent;
}

683 684
void KoShape::setKeepAspectRatio(bool keepAspect)
{
Thomas Zander's avatar
Thomas Zander committed
685 686 687
    d->keepAspect = keepAspect;
}

688 689
bool KoShape::keepAspectRatio() const
{
Thomas Zander's avatar
Thomas Zander committed
690 691 692
    return d->keepAspect;
}

693 694
const QString &KoShape::shapeId() const
{
Thomas Zander's avatar
Thomas Zander committed
695 696 697
    return d->shapeId;
}

698 699
void KoShape::setShapeId(const QString &id)
{
Thomas Zander's avatar
Thomas Zander committed
700 701 702
    d->shapeId = id;
}

703 704
void KoShape::setCollisionDetection(bool detect)
{
Thomas Zander's avatar
Thomas Zander committed
705 706 707
    d->detectCollision = detect;
}

708 709
bool KoShape::collisionDetection()
{
Thomas Zander's avatar
Thomas Zander committed
710 711 712
    return d->detectCollision;
}

713
void KoShape::addShapeManager(KoShapeManager * manager)
714
{
715
    d->shapeManagers.insert(manager);
Thomas Zander's avatar
Thomas Zander committed
716 717
}

718
void KoShape::removeShapeManager(KoShapeManager * manager)
719
{
720
    d->shapeManagers.remove(manager);
Thomas Zander's avatar
Thomas Zander committed
721 722
}

723 724
KoShapeBorderModel *KoShape::border() const
{
725 726 727
    return d->border;
}

728 729 730
void KoShape::setBorder(KoShapeBorderModel *border)
{
    if (d->border)
731
        d->border->removeUser();
732
    d->border = border;
733
    if (d->border)
734
        d->border->addUser();
735 736
    d->shapeChanged(BorderChanged);
    notifyChanged();
737 738
}

739
void KoShape::setShadow(KoShapeShadow * shadow)
740
{
741
    if (d->shadow)
742 743
        d->shadow->removeUser();
    d->shadow = shadow;
744
    if (d->shadow)
745 746 747 748 749 750 751 752 753 754
        d->shadow->addUser();
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

KoShapeShadow * KoShape::shadow() const
{
    return d->shadow;
}

755 756
const QMatrix& KoShape::matrix() const
{
757
    return d->localMatrix;
758 759
}

760
void KoShape::removeConnectionPoint(int index)
761
{
762 763
    if (index < d->connectors.count())
        d->connectors.remove(index);
764 765
}

766 767
QString KoShape::name() const
{
768 769 770
    return d->name;
}

771
void KoShape::setName(const QString & name)
772
{
773 774
    d->name = name;
}
775

776 777
void KoShape::deleteLater()
{
778
    foreach(KoShapeManager *manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
779
        manager->remove(this);
780 781 782 783
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

784 785
bool KoShape::isEditable() const
{
786
    if (!d->visible || d->locked)
787 788
        return false;

Thomas Zander's avatar
Thomas Zander committed
789
    if (d->parent && d->parent->isChildLocked(this))
790 791 792 793
        return false;

    return true;
}
794

795
// loading & saving methods
796
QString KoShape::saveStyle(KoGenStyle &style, KoShapeSavingContext &context) const
797 798 799
{
    // and fill the sty