KoShape.cpp 37.1 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"
Thomas Zander's avatar
Thomas Zander committed
46

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

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

64 65
#include <kdebug.h>

66 67
#include <limits>

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

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

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

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

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

    QVector<QPointF> connectors; // in pt

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

    uint zIndex : 10; // should be enough ;)
    uint visible : 1;
    uint printable : 1;
    uint locked : 1;
    uint keepAspect : 1;
    uint selectable : 1;
    uint detectCollision : 1;
    uint protectContent : 1;
    uint reserved : 15; // for future extensions, keep total bits used 32!
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

Thomas Zander's avatar
Thomas Zander committed
233
    d->size = newSize;
234

235
    notifyChanged();
236
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
237 238
}

239
void KoShape::setPosition(const QPointF &newPosition)
Thomas Zander's avatar
Thomas Zander committed
240
{
241
    QPointF currentPos = position();
242
    if (newPosition == currentPos)
Thomas Zander's avatar
Thomas Zander committed
243
        return;
244
    QMatrix translateMatrix;
245
    translateMatrix.translate(newPosition.x() - currentPos.x(), newPosition.y() - currentPos.y());
246 247 248
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
249
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
250 251
}

252
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
253
{
254
    if (d->parent && d->parent->childClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
255 256
        return false;

257
    QPointF point = absoluteTransformation(0).inverted().map(position);
258
    QRectF bb(QPointF(), size());
259
    if (d->border) {
260
        KoInsets insets;
261
        d->border->borderInsets(this, insets);
262
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
263
    }
264 265 266
    if (bb.contains(point))
        return true;

267
    // if there is no shadow we can as well just leave
268
    if (! d->shadow)
269
        return false;
Thomas Zander's avatar
Thomas Zander committed
270

271 272
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
273
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
274

275
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
276 277
}

278
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
279
{
280
    QRectF bb(QPointF(0, 0), size());
281
    if (d->border) {
282 283 284 285
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
286 287
    bb = absoluteTransformation(0).mapRect(bb);
    if (d->shadow) {
288
        KoInsets insets;
289
        d->shadow->insets(this, insets);
290 291 292
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
    return bb;
Thomas Zander's avatar
Thomas Zander committed
293 294
}

295 296
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const
{
Thomas Zander's avatar
Thomas Zander committed
297 298
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
299
    KoShapeContainer * container = d->parent;
300 301
    if (container) {
        if (container->childClipped(this))
302
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
303
        else {
304 305 306 307
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF(0.5 * containerSize.width(), 0.5 * containerSize.height());
            if (converter)
                containerPos = converter->documentToView(containerPos);
308
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
309 310 311
        }
    }

312
    if (converter) {
313 314 315
        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
316
    }
317 318

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
319 320
}

321
void KoShape::applyAbsoluteTransformation(const QMatrix &matrix)
322
{
323
    QMatrix globalMatrix = absoluteTransformation(0);
324 325
    // 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
326
    // to be relative to the local coordinate system
327
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
328
    applyTransformation(transformMatrix);
329 330
}

331
void KoShape::applyTransformation(const QMatrix &matrix)
332 333
{
    d->localMatrix = matrix * d->localMatrix;
334
    notifyChanged();
335
    d->shapeChanged(GenericMatrixChange);
336 337
}

338
void KoShape::setTransformation(const QMatrix &matrix)
339 340 341
{
    d->localMatrix = matrix;
    notifyChanged();
342
    d->shapeChanged(GenericMatrixChange);
343
}
Thomas Zander's avatar
Thomas Zander committed
344

345
QMatrix KoShape::transformation() const
346 347 348 349
{
    return d->localMatrix;
}

350 351
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2)
{
352
    int diff = s1->zIndex() - s2->zIndex();
353
    if (diff == 0) {
354
        KoShape *s = s1->parent();
355
        while (s) {
356
            if (s == s2) // s1 is a child of s2
357 358 359 360
                return false; // children are always on top of their parents.
            s = s->parent();
        }
        s = s2->parent();
361
        while (s) {
362
            if (s == s1) // s2 is a child of s1
363 364 365 366 367
                return true;
            s = s->parent();
        }
    }
    return diff < 0;
Thomas Zander's avatar
Thomas Zander committed
368 369
}

370 371 372
void KoShape::setParent(KoShapeContainer *parent)
{
    if (d->parent == parent)
373
        return;
374
    if (parent && dynamic_cast<KoShape*>(parent) != this) {
375 376
        if (d->parent)
            d->parent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
377
        d->parent = parent;
378
        parent->addChild(this);
379
    } else
Thomas Zander's avatar
Thomas Zander committed
380
        d->parent = 0;
381
    notifyChanged();
382
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
383 384
}

385 386 387
int KoShape::zIndex() const
{
    if (parent()) // we can't be under our parent...
388
        return qMax((int) d->zIndex, parent()->zIndex());
Thomas Zander's avatar
Thomas Zander committed
389
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
390 391
}

392 393
void KoShape::update() const
{
394 395 396
    if (!d->shapeManagers.empty()) {
        QRectF rect(boundingRect());
        foreach(KoShapeManager * manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
397
            manager->update(rect, this, true);
Thomas Zander's avatar
Thomas Zander committed
398 399 400
    }
}

401 402
void KoShape::update(const QRectF &shape) const
{
403
    if (!d->shapeManagers.empty() && isVisible()) {
404
        QRectF rect(absoluteTransformation(0).mapRect(shape));
405
        foreach(KoShapeManager * manager, d->shapeManagers) {
406
            manager->update(rect);
407 408
        }
    }
Thomas Zander's avatar
Thomas Zander committed
409 410
}

411 412
const QPainterPath KoShape::outline() const
{
Thomas Zander's avatar
Thomas Zander committed
413
    QPainterPath path;
414
    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
415 416 417
    return path;
}

418 419
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const
{
420
    QPointF point;
421 422 423 424 425 426
    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;
427
    }
428
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
429 430
}

431 432
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor)
{
433
    QPointF currentAbsPosition = absolutePosition(anchor);
434
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
435
    QMatrix translateMatrix;
436 437
    translateMatrix.translate(translate.x(), translate.y());
    applyAbsoluteTransformation(translateMatrix);
438
    notifyChanged();
439
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
440 441
}

442 443
void KoShape::copySettings(const KoShape *shape)
{
Thomas Zander's avatar
Thomas Zander committed
444 445
    d->size = shape->size();
    d->connectors.clear();
Jan Hambrecht's avatar
Jan Hambrecht committed
446 447
    foreach(const QPointF & point, shape->connectionPoints())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
448 449
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
450

451 452 453 454 455
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
456

Thomas Zander's avatar
Thomas Zander committed
457 458
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
459
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
460 461
}

Thomas Zander's avatar
Thomas Zander committed
462
void KoShape::notifyChanged()
463
{
464 465
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
466 467 468
    }
}

469 470
void KoShape::setUserData(KoShapeUserData *userData)
{
471
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
472
    d->userData = userData;
473 474
}

475 476
KoShapeUserData *KoShape::userData() const
{
Thomas Zander's avatar
Thomas Zander committed
477
    return d->userData;
478 479
}

480 481
void KoShape::setApplicationData(KoShapeApplicationData *appData)
{
482
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
483
    d->appData = appData;
484 485
}

486 487
KoShapeApplicationData *KoShape::applicationData() const
{
Thomas Zander's avatar
Thomas Zander committed
488
    return d->appData;
489 490
}

491 492
bool KoShape::hasTransparency()
{
493
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
494
        return true;
495 496
    else
        return d->fill->hasTransparency();
Thomas Zander's avatar
Thomas Zander committed
497 498
}

499 500
KoInsets KoShape::borderInsets() const
{
501
    KoInsets answer;
502
    if (d->border)
503
        d->border->borderInsets(this, answer);
504 505 506
    return answer;
}

507 508
qreal KoShape::rotation() const
{
509
    // try to extract the rotation angle out of the local matrix
510 511 512
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
513
    if (fabs(fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21())) > 1e-10)
514
        return std::numeric_limits<qreal>::quiet_NaN();
515
    // check if the matrix has scaling mixed in
516
    if (fabs(d->localMatrix.m11() - d->localMatrix.m22()) > 1e-10)
517
        return std::numeric_limits<qreal>::quiet_NaN();
518 519

    // calculate the angle from the matrix elements
520 521
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
522 523 524
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
525 526
}

527
QSizeF KoShape::size() const
528
{
Thomas Zander's avatar
Thomas Zander committed
529 530 531
    return d->size;
}

532 533
QPointF KoShape::position() const
{
534 535
    QPointF center(0.5*size().width(), 0.5*size().height());
    return d->localMatrix.map(center) - center;
536
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
537 538
}

539
void KoShape::addConnectionPoint(const QPointF &point)
540
{
541 542
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
543
    d->connectors.append(QPointF(point.x() / s.width(), point.y() / s.height()));
Thomas Zander's avatar
Thomas Zander committed
544 545
}

546 547
QList<QPointF> KoShape::connectionPoints() const
{
548 549 550
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
Jan Hambrecht's avatar
Jan Hambrecht committed
551 552
    foreach(const QPointF & cp, d->connectors)
        points.append(QPointF(s.width() * cp.x(), s.height() * cp.y()));
553 554

    return points;
555 556
}

557
void KoShape::addEventAction(KoEventAction * action)
558
{
559 560
    if (! d->eventActions.contains(action)) {
        d->eventActions.append(action);
561 562 563
    }
}

564
void KoShape::removeEventAction(KoEventAction * action)
565
{
566 567
    if (d->eventActions.contains(action)) {
        d->eventActions.removeAll(action);
568 569 570 571 572 573 574 575
    }
}

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

576
void KoShape::setBackground(KoShapeBackground * fill)
577 578
{
    if (d->fill)
579 580
        d->fill->removeUser();
    d->fill = fill;
581
    if (d->fill)
582
        d->fill->addUser();
583 584
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
585 586
}

587 588
KoShapeBackground * KoShape::background() const
{
589
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
590 591
}

592 593
void KoShape::setZIndex(int zIndex)
{
594
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
595 596 597
    d->zIndex = zIndex;
}

598 599
void KoShape::setVisible(bool on)
{
Thomas Zander's avatar
Thomas Zander committed
600 601 602
    d->visible = on;
}

603
bool KoShape::isVisible(bool recursive) const
604
{
605
    if (! recursive)
606
        return d->visible;
607
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
608 609
        return false;

610
    KoShapeContainer * parentShape = parent();
611 612
    while (parentShape) {
        if (! parentShape->isVisible())
613 614 615 616
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
617 618
}

619 620 621 622
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
623

624 625
bool KoShape::isPrintable() const
{
626 627 628 629
    if (d->visible)
        return d->printable;
    else
        return false;
630 631
}

632 633
void KoShape::setSelectable(bool selectable)
{
Thomas Zander's avatar
Thomas Zander committed
634 635 636
    d->selectable = selectable;
}

637 638
bool KoShape::isSelectable() const
{
Thomas Zander's avatar
Thomas Zander committed
639 640 641
    return d->selectable;
}

642 643
void KoShape::setLocked(bool locked)
{
Thomas Zander's avatar
Thomas Zander committed
644 645 646
    d->locked = locked;
}

647 648
bool KoShape::isLocked() const
{
Thomas Zander's avatar
Thomas Zander committed
649 650 651
    return d->locked;
}

652 653 654 655 656 657 658 659 660 661
void KoShape::setContentProtected(bool protect)
{
    d->protectContent = protect;
}

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

662 663
KoShapeContainer *KoShape::parent() const
{
Thomas Zander's avatar
Thomas Zander committed
664 665 666
    return d->parent;
}

667 668
void KoShape::setKeepAspectRatio(bool keepAspect)
{
Thomas Zander's avatar
Thomas Zander committed
669 670 671
    d->keepAspect = keepAspect;
}

672 673
bool KoShape::keepAspectRatio() const
{
Thomas Zander's avatar
Thomas Zander committed
674 675 676
    return d->keepAspect;
}

677 678
const QString &KoShape::shapeId() const
{
Thomas Zander's avatar
Thomas Zander committed
679 680 681
    return d->shapeId;
}

682 683
void KoShape::setShapeId(const QString &id)
{
Thomas Zander's avatar
Thomas Zander committed
684 685 686
    d->shapeId = id;
}

687 688
void KoShape::setCollisionDetection(bool detect)
{
Thomas Zander's avatar
Thomas Zander committed
689 690 691
    d->detectCollision = detect;
}

692 693
bool KoShape::collisionDetection()
{
Thomas Zander's avatar
Thomas Zander committed
694 695 696
    return d->detectCollision;
}

697
void KoShape::addShapeManager(KoShapeManager * manager)
698
{
699
    d->shapeManagers.insert(manager);
Thomas Zander's avatar
Thomas Zander committed
700 701
}

702
void KoShape::removeShapeManager(KoShapeManager * manager)
703
{
704
    d->shapeManagers.remove(manager);
Thomas Zander's avatar
Thomas Zander committed
705 706
}

707 708
KoShapeBorderModel *KoShape::border() const
{
709 710 711
    return d->border;
}

712 713 714
void KoShape::setBorder(KoShapeBorderModel *border)
{
    if (d->border)
715
        d->border->removeUser();
716
    d->border = border;
717
    if (d->border)
718
        d->border->addUser();
719 720
    d->shapeChanged(BorderChanged);
    notifyChanged();
721 722
}

723
void KoShape::setShadow(KoShapeShadow * shadow)
724
{
725
    if (d->shadow)
726 727
        d->shadow->removeUser();
    d->shadow = shadow;
728
    if (d->shadow)
729 730 731 732 733 734 735 736 737 738
        d->shadow->addUser();
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

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

739 740
const QMatrix& KoShape::matrix() const
{
741
    return d->localMatrix;
742 743
}

744
void KoShape::removeConnectionPoint(int index)
745
{
746 747
    if (index < d->connectors.count())
        d->connectors.remove(index);
748 749
}

750 751
QString KoShape::name() const
{
752 753 754
    return d->name;
}

755
void KoShape::setName(const QString & name)
756
{
757 758
    d->name = name;
}
759

760 761
void KoShape::deleteLater()
{
762
    foreach(KoShapeManager *manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
763
        manager->remove(this);
764 765 766 767
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

768 769
bool KoShape::isEditable() const
{
770
    if (!d->visible || d->locked)
771 772
        return false;

773
    if (d->parent && d->parent->isChildLocked(this))
774 775 776 777
        return false;

    return true;
}
778

779
// loading & saving methods
780
QString KoShape::saveStyle(KoGenStyle &style, KoShapeSavingContext &context) const
781 782 783
{
    // and fill the style
    KoShapeBorderModel * b = border();
784 785
    if (b) {
        b->fillStyle(style, context);
786
    }
787 788 789
    else {
        style.addProperty( "draw:stroke", "none" );
    }
790
    KoShapeShadow * s = shadow();
791 792
    if (s)
        s->fillStyle(style, context);
793

794
    KoShapeBackground * bg = background();
795
    if (bg) {
796
        bg->fillStyle(style, context);
797 798 799 800
    }
    else {
        style.addProperty( "draw:fill", "none" );
    }
801

802 803
    if (context.isSet(KoShapeSavingContext::AutoStyleInStyleXml)) {
        style.setAutoStyleInStylesDotXml(true);
804
    }
805

806 807 808 809 810 811 812 813 814 815 816
    QString value;
    if (isLocked())
        value = "position size";
    if (isContentProtected()) {
        if (! value.isEmpty())
            value += ' ';
        value += "content";
    }
    if (!value.isEmpty())
        d->additionalStyleAttributes.insert("style:protect", value);

817 818 819 820 821
    QMap<QByteArray, QString>::const_iterator it(d->additionalStyleAttributes.constBegin());
    for (; it != d->additionalStyleAttributes.constEnd(); ++it) {
        style.addProperty(it.key(), it.value());
    }

822
    return context.mainStyles().lookup(style, context.isSet(KoShapeSavingContext::PresentationShape) ? "pr" : "gr");
823 824
}

825
void KoShape::loadStyle(const KoXmlElement & element, KoShapeLoadingContext &context)
826
{
827
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
828 829 830
    styleStack.save();

    // fill the style stack with the shapes style
831 832 833 834 835 836
    if (element.hasAttributeNS(KoXmlNS::draw, "style-name")) {
        context.odfLoadingContext().fillStyleStack(element, KoXmlNS::draw, "style-name", "graphic");
        styleStack.setTypeProperties("graphic");
    } else if (element.hasAttributeNS(KoXmlNS::presentation, "style-name")) {
        context.odfLoadingContext().fillStyleStack(element, KoXmlNS::presentation, "style-name", "presentation");
        styleStack.setTypeProperties("graphic");
837 838
    }

839 840 841
    setBackground(loadOdfFill(element, context));
    setBorder(loadOdfStroke(element, context));
    setShadow(loadOdfShadow(element, context));
842 843

    styleStack.restore();
844 845
}

846
bool KoShape::loadOdfAttributes(const KoXmlElement & element, KoShapeLoadingContext &context, int attributes)
847
{
848 849 850 851 852 853 854
    if (attributes & OdfPosition) {
        QPointF pos(position());
        if (element.hasAttributeNS(KoXmlNS::svg, "x"))
            pos.setX(KoUnit::parseValue(element.attributeNS(KoXmlNS::svg, "x", QString())));
        if (element.hasAttributeNS(KoXmlNS::svg, "y"))
            pos.setY(KoUnit::parseValue(element.attributeNS(KoXmlNS::svg, "y", QString())));
        setPosition(pos);
855
    }
856

857 858 859 860 861 862 863
    if (attributes & OdfSize) {
        QSizeF s(size());
        if (element.hasAttributeNS(KoXmlNS::svg, "width"))
            s.setWidth(KoUnit::parseValue(element.attributeNS(KoXmlNS::svg, "width", QString())));
        if (element.hasAttributeNS(KoXmlNS::svg, "height"))
            s.setHeight(KoUnit::parseValue(element.attributeNS(KoXmlNS::svg, "height", QString())));
        setSize(s);
864 865
    }

866 867 868 869 870
    if (attributes & OdfLayer) {
        if (element.hasAttributeNS(KoXmlNS::draw, "layer")) {
            KoShapeLayer * layer = context.layer(element.attributeNS(KoXmlNS::draw, "layer"));
            if (layer) {
                setParent(layer);
871 872
            }
        }
873 874
    }

875 876 877 878 879
    if (attributes & OdfId) {
        if (element.hasAttributeNS(KoXmlNS::draw, "id")) {
            QString id = element.attributeNS(KoXmlNS::draw, "id");
            if (!id.isNull()) {
                context.addShapeId(this, id);
880 881
            }
        }
882 883
    }

884 885
    if (attributes & OdfZIndex) {
        if (element.hasAttributeNS(KoXmlNS::draw, "z-index")) {
886
            setZIndex(element.attributeNS(KoXmlNS::draw, "z-index").toInt());
Elvis Stansvik's avatar
Elvis Stansvik committed
887 888
        }
        else {
889
            setZIndex(context.zIndex());
890
        }
891 892
    }

893 894 895
    if (attributes & OdfName) {
        if (element.hasAttributeNS(KoXmlNS::draw, "name")) {
            setName(element.attributeNS(KoXmlNS::draw, "name"));
896
        }
897
    }
Jan Hambrecht's avatar
Jan Hambrecht committed
898

899 900
    if (attributes & OdfStyle) {
        loadStyle(element, context);
901
    }