KoShape.cpp 34.9 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);
92
        foreach(KoShapeManager *manager, shapeManagers)
Jan Hambrecht's avatar
Jan Hambrecht committed
93
            manager->remove(me);
Thomas Zander's avatar
Thomas Zander committed
94 95
        delete userData;
        delete appData;
96
        if (border && ! border->removeUser())
97
            delete border;
98
        if (shadow && ! shadow->removeUser())
99
            delete shadow;
100
        if (fill && ! fill->removeUser())
101
            delete fill;
102
        qDeleteAll(eventActions);
Thomas Zander's avatar
Thomas Zander committed
103 104
    }

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

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

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

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

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

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

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

151
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
152 153 154 155
}

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

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

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

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

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

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

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

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

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

Thomas Zander's avatar
Thomas Zander committed
227
    d->size = newSize;
228

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

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

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

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

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

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

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

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

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

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

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

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
313 314
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return angle;
Thomas Zander's avatar
Thomas Zander committed
519 520
}

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

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

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

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

    return points;
549 550
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    KoShapeContainer * p = parent();
758
    if (p && p->isChildLocked(this))
759
        return false;
760
    while (p) {
761
        if (! p->isEditable())
762 763 764 765 766 767
            return false;
        p = p->parent();
    }

    return true;
}
768

769
// loading & saving methods
770
QString KoShape::saveStyle(KoGenStyle &style, KoShapeSavingContext &context) const
771 772 773
{
    // and fill the style
    KoShapeBorderModel * b = border();
774 775
    if (b) {
        b->fillStyle(style, context);
776
    }
777
    KoShapeShadow * s = shadow();
778 779
    if (s)
        s->fillStyle(style, context);
780

781
    KoShapeBackground * bg = background();
782 783
    if (bg)
        bg->fillStyle(style, context);
784

785 786
    if (context.isSet(KoShapeSavingContext::AutoStyleInStyleXml)) {
        style.setAutoStyleInStylesDotXml(true);
787
    }
788

789
    return context.mainStyles().lookup(style, context.isSet(KoShapeSavingContext::PresentationShape) ? "pr" : "gr");
790 791
}

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

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

806 807 808
    setBackground(loadOdfFill(element, context));
    setBorder(loadOdfStroke(element, context));
    setShadow(loadOdfShadow(element, context));
809 810

    styleStack.restore();
811 812
}

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

824 825 826 827 828 829 830
    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);
831 832
    }

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

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

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

858
    if (attributes & OdfName) {
859

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

865 866
    if (attributes & OdfStyle) {
        loadStyle(element, context);
867
    }
868

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

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