KoShape.cpp 37.2 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 147

    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
148 149
};

Thomas Zander's avatar
Thomas Zander committed
150
KoShape::KoShape()
151
        : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
152
{
153 154 155 156
    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));
157

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

KoShape::~KoShape()
{
163
    d->shapeChanged(Deleted);
Thomas Zander's avatar
Thomas Zander committed
164
    delete d;
Thomas Zander's avatar
Thomas Zander committed
165 166
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return points;
556 557
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return true;
}
779

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

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

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

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

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

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

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

    // fill the style stack with the shapes style
832 833 834 835 836 837
    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");
838 839
    }

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

    styleStack.restore();
845 846
}

847
bool KoShape::loadOdfAttributes(const KoXmlElement & element, KoShapeLoadingContext &context, int attributes)
848
{
849 850 851 852 853 854 855
    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);
856
    }
857

858 859 860 861 862 863 864
    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);
865 866
    }

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

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

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

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

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