KoShape.cpp 33.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 "KoShapeStyleWriter.h"
44
#include "KoShapeShadow.h"
Thorsten Zachmann's avatar
Thorsten Zachmann committed
45 46
#include "KoEventAction.h"
#include "KoEventActionRegistry.h"
Thomas Zander's avatar
Thomas Zander committed
47

48
#include <KoXmlReader.h>
49
#include <KoXmlWriter.h>
50
#include <KoXmlNS.h>
51
#include <KoGenStyle.h>
52
#include <KoGenStyles.h>
53
#include <KoUnit.h>
54
#include <KoOdfStylesReader.h>
55
#include <KoOdfGraphicStyles.h>
56
#include <KoOdfLoadingContext.h>
57

Thomas Zander's avatar
Thomas Zander committed
58
#include <QPainter>
59
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
60
#include <QPainterPath>
61
#include <QList>
62
#include <QMap>
63
#include <QByteArray>
Thomas Zander's avatar
Thomas Zander committed
64

65 66
#include <kdebug.h>

67 68
#include <limits>

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

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

106 107 108 109
    void shapeChanged(ChangeType type) {
        if(parent)
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
110 111
        foreach( KoShape * shape, dependees )
            shape->notifyShapeChanged( me, type );
112 113
    }

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

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

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

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

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

Thomas Zander's avatar
Thomas Zander committed
144
KoShape::KoShape()
145
    : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
146
{
147 148 149 150 151
    d->connectors.append( QPointF( 0.5, 0.0 ) );
    d->connectors.append( QPointF( 1.0, 0.5 ) );
    d->connectors.append( QPointF( 0.5, 1.0 ) );
    d->connectors.append( QPointF( 0.0, 0.5 ) );

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

KoShape::~KoShape()
{
157
    d->shapeChanged( Deleted );
Thomas Zander's avatar
Thomas Zander committed
158
    delete d;
Thomas Zander's avatar
Thomas Zander committed
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
/* 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
166 167 168 169 170 171 172
    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
173
        for ( int i = 0; i < d->connectors.size(); ++i )
Thomas Zander's avatar
Thomas Zander committed
174
        {
Thomas Zander's avatar
Thomas Zander committed
175
            QPointF p = converter.documentToView(d->connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
176 177 178
            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 ) );
        }
179
    }*/
Thomas Zander's avatar
Thomas Zander committed
180 181
}

182
void KoShape::setScale( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
183
{
184 185 186 187 188
    QPointF pos = position();
    QMatrix scaleMatrix;
    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 196
}

void KoShape::rotate( double angle )
{
197 198 199 200 201
    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() );
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( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
209
{
210 211 212 213 214
    QPointF pos = position();
    QMatrix shearMatrix;
    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 236
    QPointF currentPos = position();
    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 247
}

bool KoShape::hitTest( const QPointF &position ) const
{
Thomas Zander's avatar
Thomas Zander committed
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 255
    {
        KoInsets insets;
256
        d->border->borderInsets(this, insets);
257 258 259 260 261 262 263 264
        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
265

266 267 268 269 270
    // 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
271 272
}

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

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

307
    if(converter) {
Jan Hambrecht's avatar
Jan Hambrecht committed
308
        QPointF pos = d->localMatrix.map( QPointF() );
309 310
        QPointF trans = converter->documentToView( pos ) - pos;
        matrix.translate( trans.x(), trans.y() );
Thomas Zander's avatar
Thomas Zander committed
311
    }
312 313

    return d->localMatrix * matrix;
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 324 325 326 327 328
    applyTransformation( transformMatrix );
}

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

void KoShape::setTransformation( const QMatrix &matrix )
{
    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
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2) {
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    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
362 363 364
}

void KoShape::setParent(KoShapeContainer *parent) {
365 366 367
    if(d->parent == parent)
        return;
    if(parent && dynamic_cast<KoShape*>(parent) != this) {
368 369
        if (d->parent)
            d->parent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
370
        d->parent = parent;
371 372
        parent->addChild(this);
    }
Thomas Zander's avatar
Thomas Zander committed
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
}

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

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

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

const QPainterPath KoShape::outline() const {
    QPainterPath path;
407
    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
408 409 410
    return path;
}

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

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

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

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

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

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

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

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

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

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

Thomas Zander's avatar
Thomas Zander committed
492
double KoShape::rotation() const {
493
    // try to extract the rotation angle out of the local matrix
494 495 496 497
    // 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 )
498
        return std::numeric_limits<double>::quiet_NaN();
499 500
    // check if the matrix has scaling mixed in
    if( fabs( d->localMatrix.m11() - d->localMatrix.m22() ) > 1e-10 )
501
        return std::numeric_limits<double>::quiet_NaN();
502 503

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

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

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

QPointF KoShape::position() const {
516 517 518
    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
519 520 521
}

void KoShape::addConnectionPoint( const QPointF &point ) {
522 523 524
    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
525 526
}

527
QList<QPointF> KoShape::connectionPoints() const {
528 529 530 531 532 533 534
    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;
535 536
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
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;
}

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

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

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

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

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

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

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

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

Thomas Zander's avatar
Thomas Zander committed
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 659
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 );
}

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

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

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
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;
}

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

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

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

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

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

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
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;
}
731

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

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

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

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

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

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

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

    styleStack.restore();
778 779
}

780 781
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
782 783 784 785 786 787 788 789 790
    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() ) ) );
791
        setSize( size );
792 793
    }

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

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

816
        loadStyle( element, context );
817
    }
818

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

826
    if ( attributes & OdfAdditionalAttributes ) {
827 828 829 830 831
        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;
832
                setAdditionalAttribute( attributeData.name, value );
833 834
            }
        }
835 836
    }

Thorsten Zachmann's avatar
Thorsten Zachmann committed
837 838 839 840 841 842 843 844
    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)
    }

845 846 847
    return true;
}

848
QString KoShape::getStyleProperty( const char *property, const KoXmlElement & element, KoShapeLoadingContext & context )
849
{
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
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
863
    QString fill = getStyleProperty( "fill", element, context );
864
    KoShapeBackground * bg = 0;
865
    if ( fill == "solid" || fill == "hatch" )
866
        bg = new KoColorBackground();
867
    else if( fill == "gradient" )
868
        bg = new KoGradientBackground( new QLinearGradient() );
869
    else if( fill == "bitmap" )
870
        bg = new KoPatternBackground( context.imageCollection() );
871 872 873 874 875 876 877 878 879

    if( ! bg )
        return 0;

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

881
    return bg;
882 883
}

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

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

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

904 905 906
KoShapeShadow * KoShape::loadOdfShadow( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
907 908
    QString shadowStyle = getStyleProperty( "shadow", element, context );
    if( shadowStyle == "visible" || shadowStyle == "hidden" )
909 910 911 912 913 914 915 916 917 918 919
    {
        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 );