KoShape.cpp 34.8 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1
/* This file is part of the KDE project
Thomas Zander's avatar
Thomas Zander committed
2
   Copyright (C) 2006 Casper Boemann Rasmussen <cbr@boemann.dk>
3
   Copyright (C) 2006-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)
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 110
        foreach(KoShape * shape, dependees)
        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 252 253
    QPointF point = absoluteTransformation(0).inverted().map(position);
    QRectF bb(QPointF(), size());
    if (d->border) {
254
        KoInsets insets;
255
        d->border->borderInsets(this, insets);
256 257
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
258
    if (bb.contains(point))
259 260 261
        return true;

    // 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
            // childs trasformations are relative to the parents position
            QRectF containerRect( QPointF(), container->size() );
            QPointF containerPos = container->absolutePosition() - containerRect.center();
301
            matrix.translate(containerPos.x(), containerPos.y());
Thomas Zander's avatar
Thomas Zander committed
302 303 304
        }
    }

305 306
    QMatrix scaleMatrix;
    // calculate the scaling if a view converter is given
307
    if (converter) {
308 309 310
        qreal zoomX, zoomY;
        converter->zoom(&zoomX, &zoomY);
        scaleMatrix.scale(zoomX, zoomY);
Thomas Zander's avatar
Thomas Zander committed
311
    }
312 313
    
    return d->localMatrix * matrix * scaleMatrix;
Thomas Zander's avatar
Thomas Zander committed
314 315
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return points;
550 551
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

745 746
void KoShape::deleteLater()
{
747
    foreach(KoShapeManager *manager, d->shapeManagers)
748
    manager->remove(this);
749 750 751 752
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

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

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

    return true;
}
769

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

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

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

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

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

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

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

    styleStack.restore();
812 813
}

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

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

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

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

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

859
    if (attributes & OdfName) {
860

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

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

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

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

887 888 889 890
    if (attributes & OdfCommonChildElements) {
        const KoXmlElement eventActionsElement(KoXml::namedItemNS(element, KoXmlNS::office, "event-listeners"));
        if (!eventActionsElement.isNull()) {
            d->eventActions = KoEventActionRegistry::instance()->createEventActionsFromOdf(eventActionsElement, context);
Thorsten Zachmann's avatar
Thorsten Zachmann committed
891 892 893 894
        }
        // load glue points (connection points)
    }

895 896 897
    return true;
}