KoShape.cpp 35 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-2007 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 74 75 76 77 78 79 80 81 82 83 84 85 86
            : size(50, 50),
            zIndex(0),
            parent(0),
            visible(true),
            printable(true),
            locked(false),
            keepAspect(false),
            selectable(true),
            detectCollision(false),
            userData(0),
            appData(0),
            fill(0),
            border(0),
            me(shape),
            shadow(0) {
Thomas Zander's avatar
Thomas Zander committed
87 88
    }

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

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

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

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

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

    bool visible;
127
    bool printable;
Thomas Zander's avatar
Thomas Zander committed
128 129 130 131 132 133 134 135
    bool locked;
    bool keepAspect;
    bool selectable;
    bool detectCollision;

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

Thomas Zander's avatar
Thomas Zander committed
145
KoShape::KoShape()
146
        : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
147
{
148 149 150 151
    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));
152

153
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
154 155 156 157
}

KoShape::~KoShape()
{
158
    d->shapeChanged(Deleted);
Thomas Zander's avatar
Thomas Zander committed
159
    delete d;
Thomas Zander's avatar
Thomas Zander committed
160 161
}

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

184
void KoShape::setScale(qreal sx, qreal sy)
Thomas Zander's avatar
Thomas Zander committed
185
{
186 187
    QPointF pos = position();
    QMatrix scaleMatrix;
188 189 190
    scaleMatrix.translate(pos.x(), pos.y());
    scaleMatrix.scale(sx, sy);
    scaleMatrix.translate(-pos.x(), -pos.y());
191
    d->localMatrix = d->localMatrix * scaleMatrix;
192

193
    notifyChanged();
194
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
195 196
}

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

206
    notifyChanged();
207
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
208 209
}

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

219
    notifyChanged();
220
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
221 222
}

223
void KoShape::setSize(const QSizeF &newSize)
Thomas Zander's avatar
Thomas Zander committed
224
{
225 226
    QSizeF oldSize(size());
    if (oldSize == newSize)
Thomas Zander's avatar
Thomas Zander committed
227
        return;
228

Thomas Zander's avatar
Thomas Zander committed
229
    d->size = newSize;
230

231
    notifyChanged();
232
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
233 234
}

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

    notifyChanged();
245
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
246 247
}

248
bool KoShape::hitTest(const QPointF &position) const
Thomas Zander's avatar
Thomas Zander committed
249
{
250
    if (d->parent && d->parent->childClipped(this) && !d->parent->hitTest(position))
Thomas Zander's avatar
Thomas Zander committed
251 252
        return false;

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

263
    // if there is no shadow we can as well just leave
264
    if (! d->shadow)
265
        return false;
Thomas Zander's avatar
Thomas Zander committed
266

267 268
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
269
    point = absoluteTransformation(0).inverted().map(position - d->shadow->offset());
270

271
    return bb.contains(point);
Thomas Zander's avatar
Thomas Zander committed
272 273
}

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

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

308
    if (converter) {
309 310 311
        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
312
    }
313 314

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
315 316
}

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

327
void KoShape::applyTransformation(const QMatrix &matrix)
328 329
{
    d->localMatrix = matrix * d->localMatrix;
330
    notifyChanged();
331
    d->shapeChanged(GenericMatrixChange);
332 333
}

334
void KoShape::setTransformation(const QMatrix &matrix)
335 336 337
{
    d->localMatrix = matrix;
    notifyChanged();
338
    d->shapeChanged(GenericMatrixChange);
339
}
Thomas Zander's avatar
Thomas Zander committed
340

341
QMatrix KoShape::transformation() const
342 343 344 345
{
    return d->localMatrix;
}

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

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

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

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

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

407 408
const QPainterPath KoShape::outline() const
{
Thomas Zander's avatar
Thomas Zander committed
409
    QPainterPath path;
410
    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
411 412 413
    return path;
}

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

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

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

447 448 449 450 451
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
452

Thomas Zander's avatar
Thomas Zander committed
453 454
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
455
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
456 457
}

Thomas Zander's avatar
Thomas Zander committed
458
void KoShape::notifyChanged()
459
{
460 461
    foreach(KoShapeManager * manager, d->shapeManagers) {
        manager->notifyShapeChanged(this);
462 463 464
    }
}

465 466
void KoShape::setUserData(KoShapeUserData *userData)
{
Thomas Zander's avatar
Thomas Zander committed
467
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
468
    d->userData = userData;
469 470
}

471 472
KoShapeUserData *KoShape::userData() const
{
Thomas Zander's avatar
Thomas Zander committed
473
    return d->userData;
474 475
}

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

482 483
KoShapeApplicationData *KoShape::applicationData() const
{
Thomas Zander's avatar
Thomas Zander committed
484
    return d->appData;
485 486
}

487 488
bool KoShape::hasTransparency()
{
489
    if (! d->fill)
Thomas Zander's avatar
Thomas Zander committed
490
        return true;
491 492
    else
        return d->fill->hasTransparency();
Thomas Zander's avatar
Thomas Zander committed
493 494
}

495 496
KoInsets KoShape::borderInsets() const
{
497
    KoInsets answer;
498
    if (d->border)
499
        d->border->borderInsets(this, answer);
500 501 502
    return answer;
}

503 504
qreal KoShape::rotation() const
{
505
    // try to extract the rotation angle out of the local matrix
506 507 508
    // if it is a pure rotation matrix

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

    // calculate the angle from the matrix elements
516 517
    qreal angle = atan2(-d->localMatrix.m21(), d->localMatrix.m11()) * 180.0 / M_PI;
    if (angle < 0.0)
518 519 520
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
521 522
}

523
QSizeF KoShape::size() const
524
{
Thomas Zander's avatar
Thomas Zander committed
525 526 527
    return d->size;
}

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

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

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

    return points;
551 552
}

553
void KoShape::addEventAction(KoEventAction * action)
554
{
555 556
    if (! d->eventActions.contains(action)) {
        d->eventActions.append(action);
557 558 559
    }
}

560
void KoShape::removeEventAction(KoEventAction * action)
561
{
562 563
    if (d->eventActions.contains(action)) {
        d->eventActions.removeAll(action);
564 565 566 567 568 569 570 571
    }
}

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

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

583 584
KoShapeBackground * KoShape::background() const
{
585
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
586 587
}

588 589
void KoShape::setZIndex(int zIndex)
{
590
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
591 592 593
    d->zIndex = zIndex;
}

594 595
void KoShape::setVisible(bool on)
{
Thomas Zander's avatar
Thomas Zander committed
596 597 598
    d->visible = on;
}

599
bool KoShape::isVisible(bool recursive) const
600
{
601
    if (! recursive)
602
        return d->visible;
603
    if (recursive && ! d->visible)
Jan Hambrecht's avatar
Jan Hambrecht committed
604 605
        return false;

606
    KoShapeContainer * parentShape = parent();
607 608
    while (parentShape) {
        if (! parentShape->isVisible())
609 610 611 612
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
613 614
}

615 616 617 618
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
619

620 621
bool KoShape::isPrintable() const
{
622 623 624 625
    if (d->visible)
        return d->printable;
    else
        return false;
626 627
}

628 629
void KoShape::setSelectable(bool selectable)
{
Thomas Zander's avatar
Thomas Zander committed
630 631 632
    d->selectable = selectable;
}

633 634
bool KoShape::isSelectable() const
{
Thomas Zander's avatar
Thomas Zander committed
635 636 637
    return d->selectable;
}

638 639
void KoShape::setLocked(bool locked)
{
Thomas Zander's avatar
Thomas Zander committed
640 641 642
    d->locked = locked;
}

643 644
bool KoShape::isLocked() const
{
Thomas Zander's avatar
Thomas Zander committed
645 646 647
    return d->locked;
}

648 649
KoShapeContainer *KoShape::parent() const
{
Thomas Zander's avatar
Thomas Zander committed
650 651 652
    return d->parent;
}

653 654
void KoShape::setKeepAspectRatio(bool keepAspect)
{
Thomas Zander's avatar
Thomas Zander committed
655 656 657
    d->keepAspect = keepAspect;
}

658 659
bool KoShape::keepAspectRatio() const
{
Thomas Zander's avatar
Thomas Zander committed
660 661 662
    return d->keepAspect;
}

663 664
const QString &KoShape::shapeId() const
{
Thomas Zander's avatar
Thomas Zander committed
665 666 667
    return d->shapeId;
}

668 669
void KoShape::setShapeId(const QString &id)
{
Thomas Zander's avatar
Thomas Zander committed
670 671 672
    d->shapeId = id;
}

673 674
void KoShape::setCollisionDetection(bool detect)
{
Thomas Zander's avatar
Thomas Zander committed
675 676 677
    d->detectCollision = detect;
}

678 679
bool KoShape::collisionDetection()
{
Thomas Zander's avatar
Thomas Zander committed
680 681 682
    return d->detectCollision;
}

683
void KoShape::addShapeManager(KoShapeManager * manager)
684
{
685
    d->shapeManagers.insert(manager);
Thomas Zander's avatar
Thomas Zander committed
686 687
}

688
void KoShape::removeShapeManager(KoShapeManager * manager)
689
{
690
    d->shapeManagers.remove(manager);
Thomas Zander's avatar
Thomas Zander committed
691 692
}

693 694
KoShapeBorderModel *KoShape::border() const
{
695 696 697
    return d->border;
}

698 699 700
void KoShape::setBorder(KoShapeBorderModel *border)
{
    if (d->border)
701
        d->border->removeUser();
702
    d->border = border;
703
    if (d->border)
704
        d->border->addUser();
705 706
    d->shapeChanged(BorderChanged);
    notifyChanged();
707 708
}

709
void KoShape::setShadow(KoShapeShadow * shadow)
710
{
711
    if (d->shadow)
712 713
        d->shadow->removeUser();
    d->shadow = shadow;
714
    if (d->shadow)
715 716 717 718 719 720 721 722 723 724
        d->shadow->addUser();
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

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

725 726
const QMatrix& KoShape::matrix() const
{
727
    return d->localMatrix;
728 729
}

730
void KoShape::removeConnectionPoint(int index)
731
{
732 733
    if (index < d->connectors.count())
        d->connectors.remove(index);
734 735
}

736 737
QString KoShape::name() const
{
738 739 740
    return d->name;
}

741
void KoShape::setName(const QString & name)
742
{
743 744
    d->name = name;
}
745

746 747
void KoShape::deleteLater()
{
748
    foreach(KoShapeManager *manager, d->shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
749
        manager->remove(this);
750 751 752 753
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

754 755
bool KoShape::isEditable() const
{
756
    if (!d->visible || d->locked)
757 758
        return false;

Thomas Zander's avatar
Thomas Zander committed
759
    if (d->parent && d->parent->isChildLocked(this))
760 761 762 763
        return false;

    return true;
}
764

765
// loading & saving methods
766
QString KoShape::saveStyle(KoGenStyle &style, KoShapeSavingContext &context) const
767 768 769
{
    // and fill the style
    KoShapeBorderModel * b = border();
770 771
    if (b) {
        b->fillStyle(style, context);
772
    }
773 774 775
    else {
        style.addProperty( "draw:stroke", "none" );
    }
776
    KoShapeShadow * s = shadow();
777 778
    if (s)
        s->fillStyle(style, context);
779

780
    KoShapeBackground * bg = background();
781
    if (bg) {
782
        bg->fillStyle(style, context);
783 784 785 786
    }
    else {
        style.addProperty( "draw:fill", "none" );
    }
787

788 789
    if (context.isSet(KoShapeSavingContext::AutoStyleInStyleXml)) {
        style.setAutoStyleInStylesDotXml(true);
790
    }
791

792
    return context.mainStyles().lookup(style, context.isSet(KoShapeSavingContext::PresentationShape) ? "pr" : "gr");
793 794
}

795
void KoShape::loadStyle(const KoXmlElement & element, KoShapeLoadingContext &context)
796
{
797
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
798 799 800
    styleStack.save();

    // fill the style stack with the shapes style
801 802 803 804 805 806
    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");
807 808
    }

809 810 811
    setBackground(loadOdfFill(element, context));
    setBorder(loadOdfStroke(element, context));
    setShadow(loadOdfShadow(element, context));
812 813

    styleStack.restore();
814 815
}

816
bool KoShape::loadOdfAttributes(const KoXmlElement & element, KoShapeLoadingContext &context, int attributes)
817
{
818 819 820 821 822 823 824
    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);
825
    }
826

827 828 829 830 831 832 833
    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);
834 835
    }

836 837 838 839 840
    if (attributes & OdfLayer) {
        if (element.hasAttributeNS(KoXmlNS::draw, "layer")) {
            KoShapeLayer * layer = context.layer(element.attributeNS(KoXmlNS::draw, "layer"));
            if (layer) {
                setParent(layer);
841 842
            }
        }
843 844
    }

845 846 847 848 849
    if (attributes & OdfId) {
        if (element.hasAttributeNS(KoXmlNS::draw, "id")) {
            QString id = element.attributeNS(KoXmlNS::draw, "id");
            if (!id.isNull()) {
                context.addShapeId(this, id);
850 851
            }
        }
852 853
    }

854 855 856
    if (attributes & OdfZIndex) {
        if (element.hasAttributeNS(KoXmlNS::draw, "z-index")) {
            context.addShapeZIndex(this, element.attributeNS(KoXmlNS::draw, "z-index").toInt());
857
        }
858
        setZIndex(context.zIndex());
859 860
    }

861
    if (attributes & OdfName) {
862

863 864
        if (element.hasAttributeNS(KoXmlNS::draw, "name")) {
            setName(element.attributeNS(KoXmlNS::draw, "name"));
865
        }
866
    }
Jan Hambrecht's avatar
Jan Hambrecht committed
867

868 869
    if (attributes & OdfStyle) {
        loadStyle(element, context);
870
    }
871

872 873 874 875
    if (attributes & OdfTransformation) {
        QString transform = element.attributeNS(KoXmlNS::draw, "transform", QString());
        if (! transform.isEmpty())
            applyAbsoluteTransformation(parseOdfTransform(transform));
Jan Hambrecht's avatar
 
Jan Hambrecht committed
876 877
    }

878
    if (attributes & OdfAdditionalAttributes) {
879
        QSet<KoShapeLoadingContext::AdditionalAttributeData> additionalAttributeData = KoShapeLoadingContext::additionalAttributeData();
Jan Hambrecht's avatar
Jan Hambrecht committed
880
        foreach(const KoShapeLoadingContext::AdditionalAttributeData & attributeData, additionalAttributeData) {
881 882
            if (element.hasAttributeNS(attributeData.ns, attributeData.tag)) {
                QString value = element.attributeNS(attributeData.ns, attributeData.tag);
883
                //kDebug(30006) << "load additional attribute" << attributeData.tag << value;
884
                setAdditionalAttribute(attributeData.name, value);
885 886
            }
        }