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

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

105 106 107 108
    void shapeChanged(ChangeType type) {
        if(parent)
            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 150
    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 ) );

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
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas) {
Thomas Zander's avatar
Thomas Zander committed
161 162 163
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
164
/* Since this code is not actually used (kivio is going to be the main user) lets disable instead of fix.
Thomas Zander's avatar
Thomas Zander committed
165 166 167 168 169 170 171
    if ( selected )
    {
        // draw connectors
        QPen pen( Qt::blue );
        pen.setWidth( 0 );
        painter.setPen( pen );
        painter.setBrush( Qt::NoBrush );
Thomas Zander's avatar
Thomas Zander committed
172
        for ( int i = 0; i < d->connectors.size(); ++i )
Thomas Zander's avatar
Thomas Zander committed
173
        {
Thomas Zander's avatar
Thomas Zander committed
174
            QPointF p = converter.documentToView(d->connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
175 176 177
            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 ) );
        }
178
    }*/
Thomas Zander's avatar
Thomas Zander committed
179 180
}

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

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

void KoShape::rotate( double angle )
{
196 197 198 199 200
    QPointF center = d->localMatrix.map( QPointF( 0.5*size().width(), 0.5*size().height() ) );
    QMatrix rotateMatrix;
    rotateMatrix.translate( center.x(), center.y() );
    rotateMatrix.rotate( angle );
    rotateMatrix.translate( -center.x(), -center.y() );
201
    d->localMatrix = d->localMatrix * rotateMatrix;
202

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

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

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

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

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

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

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

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

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

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

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

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

    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 276 277 278 279
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
280 281 282 283 284 285 286 287
    bb = absoluteTransformation(0).mapRect( bb );
    if( d->shadow )
    {
        KoInsets insets;
        d->shadow->insets( this, insets );
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
    return bb;
Thomas Zander's avatar
Thomas Zander committed
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 294 295
    KoShapeContainer * container = d->parent;
    if( container ) {
        if( container->childClipped(this) )
296
            matrix = container->absoluteTransformation(converter);
Thomas Zander's avatar
Thomas Zander committed
297
        else {
298 299
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF( 0.5*containerSize.width(), 0.5*containerSize.height() );
Thomas Zander's avatar
Thomas Zander committed
300
            if(converter)
301
                containerPos = converter->documentToView(containerPos);
302
            matrix.translate( containerPos.x(), containerPos.y() );
Thomas Zander's avatar
Thomas Zander committed
303 304 305
        }
    }

306
    if(converter) {
Jan Hambrecht's avatar
Jan Hambrecht committed
307
        QPointF pos = d->localMatrix.map( QPointF() );
308 309
        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 323 324 325 326 327
    applyTransformation( transformMatrix );
}

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

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

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

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

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

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

const QPainterPath KoShape::outline() const {
    QPainterPath path;
406
    path.addRect(QRectF( QPointF(0, 0), QSizeF( qMax(d->size.width(), 0.0001), qMax(d->size.height(), 0.0001) ) ));
Thomas Zander's avatar
Thomas Zander committed
407 408 409
    return path;
}

410 411 412 413
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const {
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
414 415 416
        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;
Jan Hambrecht's avatar
Jan Hambrecht committed
417
        case KoFlake::CenteredPosition: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
418
    }
419
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
420 421
}

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

Thomas Zander's avatar
Thomas Zander committed
432
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
433 434
    d->size = shape->size();
    d->connectors.clear();
Jan Hambrecht's avatar
Jan Hambrecht committed
435
    foreach(QPointF point, shape->connectionPoints())
Thomas Zander's avatar
Thomas Zander committed
436
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
437 438
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
439 440 441 442 443 444 445
    
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
        
Thomas Zander's avatar
Thomas Zander committed
446 447
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
448
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
449 450
}

Thomas Zander's avatar
Thomas Zander committed
451
void KoShape::notifyChanged()
452
{
Thomas Zander's avatar
Thomas Zander committed
453
    foreach( KoShapeManager * manager, d->shapeManagers )
454
    {
Thomas Zander's avatar
Thomas Zander committed
455
        manager->notifyShapeChanged( this );
456 457 458
    }
}

459
void KoShape::setUserData(KoShapeUserData *userData) {
460
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
461
    d->userData = userData;
462 463 464
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
465
    return d->userData;
466 467
}

468
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
469
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
470
    d->appData = appData;
471 472 473
}

KoShapeApplicationData *KoShape::applicationData() const {
Thomas Zander's avatar
Thomas Zander committed
474
    return d->appData;
475 476
}

477
bool KoShape::hasTransparency() {
478
    if( ! d->fill )
Thomas Zander's avatar
Thomas Zander committed
479
        return true;
480 481
    else
        return d->fill->hasTransparency();
Thomas Zander's avatar
Thomas Zander committed
482 483
}

484 485
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
486 487
    if(d->border)
        d->border->borderInsets(this, answer);
488 489 490
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
491
double KoShape::rotation() const {
492
    // try to extract the rotation angle out of the local matrix
493 494 495 496
    // if it is a pure rotation matrix

    // check if the matrix has shearing mixed in
    if( fabs( fabs(d->localMatrix.m12()) - fabs(d->localMatrix.m21()) ) > 1e-10 )
497
        return std::numeric_limits<double>::quiet_NaN();
498 499
    // check if the matrix has scaling mixed in
    if( fabs( d->localMatrix.m11() - d->localMatrix.m22() ) > 1e-10 )
500
        return std::numeric_limits<double>::quiet_NaN();
501 502

    // calculate the angle from the matrix elements
503
    double angle = atan2( -d->localMatrix.m21(), d->localMatrix.m11() ) * 180.0 / M_PI;
504 505 506 507
    if( angle < 0.0 )
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
508 509 510 511 512 513 514
}

QSizeF KoShape::size () const {
    return d->size;
}

QPointF KoShape::position() const {
515 516 517
    QPointF center( 0.5*size().width(), 0.5*size().height() );
    return d->localMatrix.map( center ) - center;
    //return d->localMatrix.map( QPointF(0,0) );
Thomas Zander's avatar
Thomas Zander committed
518 519 520
}

void KoShape::addConnectionPoint( const QPointF &point ) {
521 522 523
    QSizeF s = size();
    // convert glue point from shape coordinates to factors of size
    d->connectors.append( QPointF( point.x() / s.width(), point.y() / s.height() ) );
Thomas Zander's avatar
Thomas Zander committed
524 525
}

526
QList<QPointF> KoShape::connectionPoints() const {
527 528 529 530 531 532 533
    QList<QPointF> points;
    QSizeF s = size();
    // convert glue points to shape coordinates
    foreach( QPointF cp, d->connectors )
        points.append( QPointF( s.width() * cp.x(), s.height() * cp.y() ) );

    return points;
534 535
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
void KoShape::addEventAction( KoEventAction * action )
{
    if ( ! d->eventActions.contains( action ) ) {
        d->eventActions.append( action );
    }
}

void KoShape::removeEventAction( KoEventAction * action )
{
    if ( d->eventActions.contains( action ) ) {
        d->eventActions.removeAll( action );
    }
}

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

555 556 557 558 559 560
void KoShape::setBackground( KoShapeBackground * fill ) {
    if(d->fill)
        d->fill->removeUser();
    d->fill = fill;
    if(d->fill)
        d->fill->addUser();
561 562
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
563 564
}

565 566
KoShapeBackground * KoShape::background() const {
    return d->fill;
Thomas Zander's avatar
Thomas Zander committed
567 568 569
}

void KoShape::setZIndex(int zIndex) {
570
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
571 572 573 574 575 576 577
    d->zIndex = zIndex;
}

void KoShape::setVisible(bool on) {
    d->visible = on;
}

578 579 580
bool KoShape::isVisible( bool recursive ) const {
    if( ! recursive )
        return d->visible;
Jan Hambrecht's avatar
Jan Hambrecht committed
581 582 583
    if( recursive && ! d->visible )
        return false;

584 585 586 587 588 589 590 591
    KoShapeContainer * parentShape = parent();
    while( parentShape )
    {
        if( ! parentShape->isVisible() )
            return false;
        parentShape = parentShape->parent();
    }
    return true;
Thomas Zander's avatar
Thomas Zander committed
592 593
}

594 595 596 597
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
598

599 600
bool KoShape::isPrintable() const
{
601 602 603 604
    if (d->visible)
        return d->printable;
    else
        return false;
605 606
}

Thomas Zander's avatar
Thomas Zander committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
void KoShape::setSelectable(bool selectable) {
    d->selectable = selectable;
}

bool KoShape::isSelectable() const {
    return d->selectable;
}

void KoShape::setLocked(bool locked) {
    d->locked = locked;
}

bool KoShape::isLocked() const {
    return d->locked;
}

KoShapeContainer *KoShape::parent() const {
    return d->parent;
}

void KoShape::setKeepAspectRatio(bool keepAspect) {
    d->keepAspect = keepAspect;
}

bool KoShape::keepAspectRatio() const {
    return d->keepAspect;
}

const QString &KoShape::shapeId() const {
    return d->shapeId;
}

void KoShape::setShapeId(const QString &id) {
    d->shapeId = id;
}

void KoShape::setCollisionDetection(bool detect) {
    d->detectCollision = detect;
}

bool KoShape::collisionDetection() {
    return d->detectCollision;
}

void KoShape::addShapeManager( KoShapeManager * manager ) {
    d->shapeManagers.insert( manager );
}

void KoShape::removeShapeManager( KoShapeManager * manager ) {
    d->shapeManagers.remove( manager );
}

659 660 661 662 663
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

void KoShape::setBorder(KoShapeBorderModel *border) {
664 665
    if(d->border)
        d->border->removeUser();
666
    d->border = border;
667 668
    if(d->border)
        d->border->addUser();
669 670
    d->shapeChanged(BorderChanged);
    notifyChanged();
671 672
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
void KoShape::setShadow( KoShapeShadow * shadow )
{
    if( d->shadow )
        d->shadow->removeUser();
    d->shadow = shadow;
    if( d->shadow )
        d->shadow->addUser();
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

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

689
const QMatrix& KoShape::matrix() const {
690
    return d->localMatrix;
691 692
}

693
void KoShape::removeConnectionPoint( int index )
694 695 696 697 698
{
    if( index < d->connectors.count() )
        d->connectors.remove( index );
}

699
QString KoShape::name() const {
700 701 702 703 704 705
    return d->name;
}

void KoShape::setName( const QString & name ) {
    d->name = name;
}
706

707 708 709 710 711 712 713
void KoShape::deleteLater() {
    foreach(KoShapeManager *manager, d->shapeManagers)
        manager->remove(this);
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
bool KoShape::isEditable() const {
    if( !d->visible || d->locked )
        return false;

    KoShapeContainer * p = parent();
    if(p && p->isChildLocked(this))
        return false;
    while( p )
    {
        if( ! p->isVisible() )
            return false;
        p = p->parent();
    }

    return true;
}
730

731
// loading & saving methods
732
QString KoShape::saveStyle( KoGenStyle &style, KoShapeSavingContext &context ) const
733 734 735 736 737 738 739
{
    // and fill the style
    KoShapeBorderModel * b = border();
    if ( b )
    {
        b->fillStyle( style, context );
    }
740 741 742
    KoShapeShadow * s = shadow();
    if( s )
        s->fillStyle( style, context );
743

744 745 746 747 748 749 750
    KoShapeBackground * bg = background();
    if( bg )
        bg->fillStyle( style, context );

    if ( context.isSet( KoShapeSavingContext::AutoStyleInStyleXml ) ) {
        style.setAutoStyleInStylesDotXml( true );
    }
751

752
    return context.mainStyles().lookup( style, context.isSet( KoShapeSavingContext::PresentationShape ) ? "pr" : "gr" );
753 754
}

755 756
void KoShape::loadStyle( const KoXmlElement & element, KoShapeLoadingContext &context )
{
757
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
758 759 760 761 762
    styleStack.save();

    // fill the style stack with the shapes style
    if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
763
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::draw, "style-name", "graphic" );
764 765 766 767
        styleStack.setTypeProperties( "graphic" );
    }
    else if( element.hasAttributeNS( KoXmlNS::presentation, "style-name" ) )
    {
768
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::presentation, "style-name", "presentation" );
769
        styleStack.setTypeProperties( "graphic" );
770 771
    }

772 773
    setBackground( loadOdfFill( element, context ) );
    setBorder( loadOdfStroke( element, context ) );
774
    setShadow( loadOdfShadow( element, context ) );
775 776

    styleStack.restore();
777 778
}

779 780
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
781 782 783 784 785 786 787 788 789
    if ( attributes & OdfSize ) {
        QPointF pos;
        pos.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", QString() ) ) );
        pos.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", QString() ) ) );
        setPosition( pos );

        QSizeF size;
        size.setWidth( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", QString() ) ) );
        size.setHeight( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", QString() ) ) );
790
        setSize( size );
791 792
    }

793 794 795 796 797 798 799
    if ( attributes & OdfMandatories ) {
        if ( element.hasAttributeNS( KoXmlNS::draw, "layer" ) ) {
            KoShapeLayer * layer = context.layer( element.attributeNS( KoXmlNS::draw, "layer" ) );
            if ( layer ) {
                setParent( layer );
            }
        }
800 801 802 803 804 805
        if ( element.hasAttributeNS( KoXmlNS::draw, "id" ) ) {
            QString id = element.attributeNS( KoXmlNS::draw, "id" );
            if ( !id.isNull() ) {
                context.addShapeId( this, id );
            }
        }
806
        if ( element.hasAttributeNS( KoXmlNS::draw, "z-index" ) ) {
807
            context.addShapeZIndex( this, element.attributeNS( KoXmlNS::draw, "z-index" ).toInt() );
808
        }
809
        setZIndex( context.zIndex() );
810

811
        if( element.hasAttributeNS( KoXmlNS::draw, "name" ) ) {
Jan Hambrecht's avatar
Jan Hambrecht committed
812
            setName( element.attributeNS( KoXmlNS::draw, "name" ) );
813
        }
Jan Hambrecht's avatar
Jan Hambrecht committed
814

815
        loadStyle( element, context );
816
    }
817

Jan Hambrecht's avatar
 
Jan Hambrecht committed
818 819 820 821
    if( attributes & OdfTransformation )
    {
        QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
        if( ! transform.isEmpty() )
822
            applyAbsoluteTransformation( parseOdfTransform( transform ) );
Jan Hambrecht's avatar
 
Jan Hambrecht committed
823 824
    }

825
    if ( attributes & OdfAdditionalAttributes ) {
826 827 828 829 830
        QSet<KoShapeLoadingContext::AdditionalAttributeData> additionalAttributeData = KoShapeLoadingContext::additionalAttributeData();
        foreach ( KoShapeLoadingContext::AdditionalAttributeData attributeData, additionalAttributeData ) {
            if ( element.hasAttributeNS( attributeData.ns, attributeData.tag ) ) {
                QString value = element.attributeNS( attributeData.ns, attributeData.tag );
                //kDebug(30006) << "load additional attribute" << attributeData.tag << value;
831
                setAdditionalAttribute( attributeData.name, value );
832 833
            }
        }
834 835
    }

Thorsten Zachmann's avatar
Thorsten Zachmann committed
836 837 838 839 840 841 842 843
    if ( attributes & OdfCommonChildElements ) {
        const KoXmlElement eventActionsElement( KoXml::namedItemNS( element, KoXmlNS::office, "event-listeners" ) );
        if ( !eventActionsElement.isNull() ) {
            d->eventActions = KoEventActionRegistry::instance()->createEventActionsFromOdf( eventActionsElement, context );
        }
        // load glue points (connection points)
    }

844 845 846
    return true;
}

847
QString KoShape::getStyleProperty( const char *property, const KoXmlElement & element, KoShapeLoadingContext & context )
848
{
Jan Hambrecht's avatar
Jan Hambrecht committed
849
    Q_UNUSED(element);
850
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
851
    QString value;
852

853 854
    if ( styleStack.hasProperty( KoXmlNS::draw, property ) ) {
        value = styleStack.property( KoXmlNS::draw, property );
855 856
    }

857 858 859
    return value;
}

860
KoShapeBackground * KoShape::loadOdfFill( const KoXmlElement & element, KoShapeLoadingContext & context )
861 862
{
    QString fill = getStyleProperty( "fill", element, context );
863
    KoShapeBackground * bg = 0;
864
    if ( fill == "solid" || fill == "hatch" )
865
        bg = new KoColorBackground();
866
    else if( fill == "gradient" )
867
        bg = new KoGradientBackground( new QLinearGradient() );
868
    else if( fill == "bitmap" )
869
        bg = new KoPatternBackground( context.imageCollection() );
870 871 872 873 874 875 876 877 878

    if( ! bg )
        return 0;

    if( ! bg->loadStyle( context.odfLoadingContext(), size() ) )
    {
        delete bg;
        return 0;
    }
879

880
    return bg;
881 882
}

883 884
KoShapeBorderModel * KoShape::loadOdfStroke( const KoXmlElement & element, KoShapeLoadingContext & context )
{
885
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
886
    QString stroke = getStyleProperty( "stroke", element, context );
887
    if( stroke == "solid" || stroke == "dash" )
888
    {
889
        QPen pen = KoOdfGraphicStyles::loadOasisStrokeStyle( styleStack, stroke, context.odfLoadingContext().stylesReader() );
890

891
        KoLineBorder * border = new KoLineBorder();
892
        border->setLineWidth( pen.widthF() );
893 894 895
        border->setColor( pen.color() );
        border->setJoinStyle( pen.joinStyle() );
        border->setLineStyle( pen.style(), pen.dashPattern() );
896

897 898 899 900
        return border;
    }
    else
        return 0;
901 902
}

903 904 905
KoShapeShadow * KoShape::loadOdfShadow( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
906 907
    QString shadowStyle = getStyleProperty( "shadow", element, context );
    if( shadowStyle == "visible" || shadowStyle == "hidden" )
908 909 910 911 912 913 914 915 916 917 918
    {
        KoShapeShadow * shadow = new KoShapeShadow();
        QColor shadowColor( styleStack.property( KoXmlNS::draw, "shadow-color" ) );
        qreal offsetX = KoUnit::parseValue( styleStack.property( KoXmlNS::draw, "shadow-offset-x" ) );
        qreal offsetY = KoUnit::parseValue( styleStack.property( KoXmlNS::draw, "shadow-offset-y" ) );
        shadow->setOffset( QPointF( offsetX, offsetY ) );

        QString opacity = styleStack.property( KoXmlNS::draw, "shadow-opacity" );
        if( ! opacity.isEmpty() && opacity.right( 1 ) == "%" )
            shadowColor.setAlphaF( opacity.left( opacity.length()-1 ).toFloat() / 100.0 );
        shadow->setColor( shadowColor );
919
        shadow->setVisibility( shadowStyle == "visible" );
920 921 922 923 924 925

        return shadow;
    }
    return 0;
}

Jan Hambrecht's avatar
 
Jan Hambrecht committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
QMatrix KoShape::parseOdfTransform( const QString &transform )
{
    QMatrix matrix;

    // Split string for handling 1 transform statement at a time
    QStringList subtransforms = transform.split(')', QString::SkipEmptyParts);
    QStringList::ConstIterator it = subtransforms.begin();
    QStringList::ConstIterator end = subtransforms.end();
    for(; it != end; ++it)
    {
        QStringList subtransform = (*it).split('(', QString::SkipEmptyParts);

        subtransform[0] = subtransform[0].trimmed().toLower();
        subtransform[1] = subtransform[1].simplified();
        QRegExp reg("[,( ]");
        QStringList params = subtransform[1].split(reg, QString::SkipEmptyParts);

Thomas Zander's avatar
Thomas Zander committed
943
        if(subtransform[0].startsWith(';') || subtransform[0].startsWith(','))
Jan Hambrecht's avatar
 
Jan Hambrecht committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
            subtransform[0] = subtransform[0].right(subtransform[0].length() - 1);

        if(subtransform[0] == "rotate")
        {
            // TODO find out what oo2 really does when rotating, it seems severly broken
            if(params.count() == 3)
            {
                double x = KoUnit::parseValue( params[1] );
                double y = KoUnit::parseValue( params[2] );

                matrix.translate(x, y);
                // oo2 rotates by radians
                matrix.rotate( params[0].toDouble()*180.0/M_PI );
                matrix.translate(-x, -y);
            }
            else
            {
                // oo2 rotates by radians
                matrix.rotate( params[0].toDouble()*180.0/M_PI );
            }
        }
        else if(subtransform[0] == "translate")
        {
            if(params.count() == 2)
            {
                double x = KoUnit::parseValue( params[0] );
                double y = KoUnit::parseValue( params[1] );
                matrix.translate(x, y);
            }
            else    // Spec : if only one param given, assume 2nd param to be 0
                matrix.translate( KoUnit::parseValue( params[0] ) , 0);
        }
        else if(subtransform[0] == "scale")
        {
            if(params.count() == 2)
                matrix.scale(params[0].toDouble(), params[1].toDouble());
            else    // Spec : if only one param given, assume uniform scaling
                matrix.scale(params[0].toDouble(), params[0].toDouble());
        }
        else if(subtransform[0] == "skewx")
            matrix.shear(tan(params[0].toDouble()), 0.0F);
        else if(subtransform[0] == "skewy")
            matrix.shear(tan(params[0].toDouble()), 0.0F);
        else if(subtransform[0] == "skewy")
            matrix.shear(0.0F, tan(params[0].toDouble()));
        else if(subtransform[0] == "matrix")
        {
            if(params.count() >= 6)
                matrix.setMatrix(params[0].toDouble(), params[1].toDouble(), params[2].toDouble(), params[3].toDouble(), KoUnit::parseValue( params[4] ), KoUnit::parseValue( params[5] ) );
        }
    }

    return matrix;
}

999
void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) const {
1000
    if(attributes & OdfMandatories) {
1001
        KoGenStyle style;
1002
        // all items that should be written to 'draw:frame' and any other 'draw:' object that inherits this shape
1003 1004 1005 1006 1007 1008 1009 1010
        if ( context.isSet( KoShapeSavingContext::PresentationShape ) ) {
            style = KoGenStyle( KoGenStyle::StylePresentationAuto, "presentation" );
            context.xmlWriter().addAttribute( "presentation:style-name", saveStyle( style, context ) );
        }
        else {
            style = KoGenStyle( KoGenStyle::StyleGraphicAuto, "graphic" );
            context.xmlWriter().addAttribute( "draw:style-name", saveStyle( style, context ) );
        }
1011

1012
        if ( context.isSet( KoShapeSavingContext::DrawId ) )
1013
        {
1014
            context.xmlWriter().addAttribute( "draw:id", context.drawId( this ) );
1015 1016
        }

Jan Hambrecht's avatar
Jan Hambrecht committed
1017 1018 1019
        if( ! name().isEmpty() )
            context.xmlWriter().addAttribute( "draw:name", name() );

1020
        if(d->parent && dynamic_cast<KoShapeLayer*> (d->parent))
1021
            context.xmlWriter().addAttribute("draw:layer", d->parent->name());
1022 1023
    }

1024 1025
    if(attributes & OdfSize) {
        QSizeF s( size() );
1026 1027
        context.xmlWriter().addAttributePt( "svg:width", s.width() );
        context.xmlWriter().addAttributePt( "svg:height", s.height() );
Jan Hambrecht's avatar
Jan Hambrecht committed
1028 1029 1030
        // the position is hidden in the transformation
        context.xmlWriter().addAttributePt( "svg:x", 0.0 );
        context.xmlWriter().addAttributePt( "svg:y", 0.0 );
1031 1032
    }

1033
    if(attributes & OdfTransformation) {
1034
        QMatrix matrix = absoluteTransformation(0);
1035
        if( ! matrix.isIdentity() )
1036
        {
Jan Hambrecht's avatar
 
Jan Hambrecht committed
1037
            QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
1038 1039 1040
                    .arg( matrix.m11() ).arg( matrix.m12() )
                    .arg( matrix.m21() ).arg( matrix.m22() )
                    .arg( matrix.dx() ) .arg( matrix.dy() );
1041 1042
            context.xmlWriter().addAttribute( "draw:transform", m );
        }
1043
    }
1044 1045 1046 1047 1048 1049 1050

    if ( attributes & OdfAdditionalAttributes ) {
        QMap<QByteArray, QString>::const_iterator it( d->additionalAttributes.constBegin() );
        for ( ; it != d->additionalAttributes.constEnd(); ++it ) {
            context.xmlWriter().addAttribute( it.key(), it.value() );
        }
    }
1051 1052
}

Thorsten Zachmann's avatar
Thorsten Zachmann committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
void KoShape::saveOdfCommonChildElements( KoShapeSavingContext &context ) const
{
    // save event listeners see ODF 9.2.21 Event Listeners
    if ( d->eventActions.size() > 0 ) {
        context.xmlWriter().startElement( "office:event-listeners" );
        foreach ( KoEventAction * action, d->eventActions ) {
            action->saveOdf( context );
        }
        context.xmlWriter().endElement();
    }

    // save glue points see ODF 9.2.19 Glue Points
}

1067 1068 1069 1070