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-2007 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
#include "KoShapeManager.h"
32
#include "KoShapeUserData.h"
33
#include "KoShapeApplicationData.h"
34
#include "KoShapeSavingContext.h"
35
#include "KoShapeLoadingContext.h"
36
#include "KoViewConverter.h"
37
#include "KoLineBorder.h"
38
#include "ShapeDeleter_p.h"
39
#include "KoShapeStyleWriter.h"
40
#include "KoShapeShadow.h"
Thomas Zander's avatar
Thomas Zander committed
41

42
#include <KoXmlReader.h>
43
#include <KoXmlWriter.h>
44
#include <KoXmlNS.h>
45
#include <KoGenStyle.h>
46
#include <KoUnit.h>
47
#include <KoOdfStylesReader.h>
48
#include <KoOdfGraphicStyles.h>
49
#include <KoOdfLoadingContext.h>
50

Thomas Zander's avatar
Thomas Zander committed
51
#include <QPainter>
52
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
53
#include <QPainterPath>
54
#include <QList>
Thomas Zander's avatar
Thomas Zander committed
55

56 57
#include <kdebug.h>

58
class KoShape::Private {
Thomas Zander's avatar
Thomas Zander committed
59
public:
60
    Private(KoShape *shape)
61
        : size( 50, 50 ),
Thomas Zander's avatar
Thomas Zander committed
62 63 64
        zIndex( 0 ),
        parent( 0 ),
        visible( true ),
65
        printable( true ),
Thomas Zander's avatar
Thomas Zander committed
66 67 68 69 70
        locked( false ),
        keepAspect( false ),
        selectable( true ),
        detectCollision( false ),
        userData(0),
71 72
        appData(0),
        backgroundBrush(Qt::NoBrush),
73
        border(0),
74 75
        me(shape),
        shadow(0)
Thomas Zander's avatar
Thomas Zander committed
76 77 78
    {
    }

79
    ~Private() {
80 81
        if( parent )
            parent->removeChild( me );
82 83
        foreach(KoShapeManager *manager, shapeManagers)
            manager->remove(me);
Thomas Zander's avatar
Thomas Zander committed
84 85
        delete userData;
        delete appData;
86 87 88 89 90
        if(border) {
            border->removeUser();
            if(border->useCount() == 0)
                delete border;
        }
91 92
        if( shadow && shadow->removeUser() == 0 )
            delete shadow;
Thomas Zander's avatar
Thomas Zander committed
93 94
    }

95 96 97 98
    void shapeChanged(ChangeType type) {
        if(parent)
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
99 100
        foreach( KoShape * shape, dependees )
            shape->notifyShapeChanged( me, type );
101 102
    }

Thomas Zander's avatar
Thomas Zander committed
103 104
    QSizeF size; // size in pt
    QString shapeId;
105
    QString name; ///< the shapes names
Thomas Zander's avatar
Thomas Zander committed
106

107
    QMatrix localMatrix; ///< the shapes local transformation matrix
Thomas Zander's avatar
Thomas Zander committed
108 109 110 111 112 113 114

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

    bool visible;
115
    bool printable;
Thomas Zander's avatar
Thomas Zander committed
116 117 118 119 120 121 122 123
    bool locked;
    bool keepAspect;
    bool selectable;
    bool detectCollision;

    QSet<KoShapeManager *> shapeManagers;
    KoShapeUserData *userData;
    KoShapeApplicationData *appData;
124 125
    QBrush backgroundBrush; ///< Stands for the background color / fill etc.
    KoShapeBorderModel *border; ///< points to a border, or 0 if there is no border
126
    QList<KoShapeConnection*> connections;
127
    KoShape *me;
128
    QList<KoShape*> dependees; ///< list of shape dependent on this shape
129
    KoShapeShadow * shadow; ///< the current shape shadow
Thomas Zander's avatar
Thomas Zander committed
130 131
};

Thomas Zander's avatar
Thomas Zander committed
132
KoShape::KoShape()
133
    : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
134
{
135 136 137 138 139
    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 ) );

140
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
141 142 143 144
}

KoShape::~KoShape()
{
145
    d->shapeChanged( Deleted );
Thomas Zander's avatar
Thomas Zander committed
146
    delete d;
Thomas Zander's avatar
Thomas Zander committed
147 148
}

149
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas) {
Thomas Zander's avatar
Thomas Zander committed
150 151 152
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
153
/* 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
154 155 156 157 158 159 160
    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
161
        for ( int i = 0; i < d->connectors.size(); ++i )
Thomas Zander's avatar
Thomas Zander committed
162
        {
Thomas Zander's avatar
Thomas Zander committed
163
            QPointF p = converter.documentToView(d->connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
164 165 166
            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 ) );
        }
167
    }*/
Thomas Zander's avatar
Thomas Zander committed
168 169
}

170
void KoShape::setScale( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
171
{
172 173 174 175 176
    QPointF pos = position();
    QMatrix scaleMatrix;
    scaleMatrix.translate( pos.x(), pos.y() );
    scaleMatrix.scale( sx, sy );
    scaleMatrix.translate( -pos.x(), -pos.y() );
177
    d->localMatrix = d->localMatrix * scaleMatrix;
178

179
    notifyChanged();
180
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
181 182 183 184
}

void KoShape::rotate( double angle )
{
185 186 187 188 189
    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() );
190
    d->localMatrix = d->localMatrix * rotateMatrix;
191

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

196
void KoShape::setShear( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
197
{
198 199 200 201 202
    QPointF pos = position();
    QMatrix shearMatrix;
    shearMatrix.translate( pos.x(), pos.y() );
    shearMatrix.shear( sx, sy );
    shearMatrix.translate( -pos.x(), -pos.y() );
203
    d->localMatrix = d->localMatrix * shearMatrix;
204

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

209
void KoShape::setSize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
210
{
211 212
    QSizeF oldSize( size() );
    if(oldSize == newSize )
Thomas Zander's avatar
Thomas Zander committed
213
        return;
214

Thomas Zander's avatar
Thomas Zander committed
215
    d->size = newSize;
216

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

221
void KoShape::setPosition( const QPointF &newPosition )
Thomas Zander's avatar
Thomas Zander committed
222
{
223 224
    QPointF currentPos = position();
    if( newPosition == currentPos )
Thomas Zander's avatar
Thomas Zander committed
225
        return;
226
    QMatrix translateMatrix;
227
    translateMatrix.translate( newPosition.x()-currentPos.x(), newPosition.y()-currentPos.y() );
228 229 230
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
231
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
232 233 234 235
}

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

239
    QPointF point = absoluteTransformation(0).inverted().map( position );
240
    QRectF bb( QPointF(), size() );
241
    if(d->border)
242 243
    {
        KoInsets insets;
244
        d->border->borderInsets(this, insets);
245 246 247 248 249 250 251 252
        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
253

254 255 256 257 258
    // 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
259 260
}

261
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
262
{
263
    QRectF bb( QPointF(0, 0), size() );
264 265 266 267 268
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
269 270 271 272 273 274 275 276
    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
277 278
}

279
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
280 281
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
282 283 284
    KoShapeContainer * container = d->parent;
    if( container ) {
        if( container->childClipped(this) )
285
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
286
        else {
287 288
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF( 0.5*containerSize.width(), 0.5*containerSize.height() );
Thomas Zander's avatar
Thomas Zander committed
289
            if(converter)
290
                containerPos = converter->documentToView(containerPos);
291
            matrix.translate( containerPos.x(), containerPos.y() );
Thomas Zander's avatar
Thomas Zander committed
292 293 294
        }
    }

295
    if(converter) {
296
        QPointF pos = d->localMatrix.map( QPoint() );
297 298
        QPointF trans = converter->documentToView( pos ) - pos;
        matrix.translate( trans.x(), trans.y() );
Thomas Zander's avatar
Thomas Zander committed
299
    }
300 301

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
302 303
}

304
void KoShape::applyAbsoluteTransformation( const QMatrix &matrix )
305
{
306
    QMatrix globalMatrix = absoluteTransformation(0);
307 308
    // 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
309
    // to be relative to the local coordinate system
310
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
311 312 313 314 315 316
    applyTransformation( transformMatrix );
}

void KoShape::applyTransformation( const QMatrix &matrix )
{
    d->localMatrix = matrix * d->localMatrix;
317
    notifyChanged();
318
    d->shapeChanged(GenericMatrixChange);
319 320 321 322 323 324
}

void KoShape::setTransformation( const QMatrix &matrix )
{
    d->localMatrix = matrix;
    notifyChanged();
325
    d->shapeChanged(GenericMatrixChange);
326
}
Thomas Zander's avatar
Thomas Zander committed
327

328
QMatrix KoShape::transformation() const
329 330 331 332
{
    return d->localMatrix;
}

333
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2) {
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    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
350 351 352
}

void KoShape::setParent(KoShapeContainer *parent) {
353 354 355
    if(d->parent == parent)
        return;
    if(parent && dynamic_cast<KoShape*>(parent) != this) {
356 357
        if (d->parent)
            d->parent->removeChild(this);
Thomas Zander's avatar
Thomas Zander committed
358
        d->parent = parent;
359 360
        parent->addChild(this);
    }
Thomas Zander's avatar
Thomas Zander committed
361
    else
Thomas Zander's avatar
Thomas Zander committed
362
        d->parent = 0;
363
    notifyChanged();
364
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
365 366 367 368
}

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

373
void KoShape::update() const {
Thomas Zander's avatar
Thomas Zander committed
374
    if ( !d->shapeManagers.empty() )
375
    {
Thorsten Zachmann's avatar
Thorsten Zachmann committed
376
        QRectF rect( boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
377
        foreach( KoShapeManager * manager, d->shapeManagers )
378
            manager->update( rect, this, true );
Thomas Zander's avatar
Thomas Zander committed
379 380 381
    }
}

382
void KoShape::update(const QRectF &shape) const {
Thomas Zander's avatar
Thomas Zander committed
383
    if ( !d->shapeManagers.empty() && isVisible() )
384
    {
385
        QRectF rect(absoluteTransformation(0).mapRect(shape));
Thomas Zander's avatar
Thomas Zander committed
386
        foreach( KoShapeManager * manager, d->shapeManagers )
387
        {
388
            manager->update(rect);
389 390
        }
    }
Thomas Zander's avatar
Thomas Zander committed
391 392 393 394
}

const QPainterPath KoShape::outline() const {
    QPainterPath path;
395
    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
396 397 398
    return path;
}

399 400 401 402
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const {
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
403 404 405
        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
406
        case KoFlake::CenteredPosition: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
407
    }
408
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
409 410
}

411
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor) {
412 413
    QPointF currentAbsPosition = absolutePosition( anchor );
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
414 415
    QMatrix translateMatrix;
    translateMatrix.translate( translate.x(), translate.y() );
416
    applyAbsoluteTransformation( translateMatrix );
417
    notifyChanged();
418
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
419 420
}

Thomas Zander's avatar
Thomas Zander committed
421
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
422 423
    d->size = shape->size();
    d->connectors.clear();
Thomas Zander's avatar
Thomas Zander committed
424 425
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
426 427
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
428 429 430 431 432 433 434
    
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
        
Thomas Zander's avatar
Thomas Zander committed
435 436
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
437
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
438 439
}

Thomas Zander's avatar
Thomas Zander committed
440
void KoShape::notifyChanged()
441
{
Thomas Zander's avatar
Thomas Zander committed
442
    foreach( KoShapeManager * manager, d->shapeManagers )
443
    {
Thomas Zander's avatar
Thomas Zander committed
444
        manager->notifyShapeChanged( this );
445 446 447
    }
}

448
void KoShape::setUserData(KoShapeUserData *userData) {
Thomas Zander's avatar
Thomas Zander committed
449
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
450
    d->userData = userData;
451 452 453
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
454
    return d->userData;
455 456
}

457
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
Thomas Zander's avatar
Thomas Zander committed
458
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
459
    d->appData = appData;
460 461 462
}

KoShapeApplicationData *KoShape::applicationData() const {
Thomas Zander's avatar
Thomas Zander committed
463
    return d->appData;
464 465
}

466
bool KoShape::hasTransparency() {
467
    if(d->backgroundBrush.style() == Qt::NoBrush)
Thomas Zander's avatar
Thomas Zander committed
468
        return true;
469
    return !d->backgroundBrush.isOpaque();
Thomas Zander's avatar
Thomas Zander committed
470 471
}

472 473
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
474 475
    if(d->border)
        d->border->borderInsets(this, answer);
476 477 478
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
479
double KoShape::scaleX() const {
480
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
481
}
482

Thomas Zander's avatar
Thomas Zander committed
483
double KoShape::scaleY() const {
484
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
485 486 487
}

double KoShape::rotation() const {
488
    // try to extract the rotation angle out of the local matrix
489 490 491 492
    // 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 )
Jarosław Staniek's avatar
Jarosław Staniek committed
493
        return NAN;
494 495
    // check if the matrix has scaling mixed in
    if( fabs( d->localMatrix.m11() - d->localMatrix.m22() ) > 1e-10 )
Jarosław Staniek's avatar
Jarosław Staniek committed
496
        return NAN;
497 498

    // calculate the angle from the matrix elements
499
    double angle = atan2( -d->localMatrix.m21(), d->localMatrix.m11() ) * 180.0 / M_PI;
500 501 502 503
    if( angle < 0.0 )
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
504 505 506
}

double KoShape::shearX() const {
507
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
508 509 510
}

double KoShape::shearY() const {
511
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
512 513 514 515 516 517 518
}

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

QPointF KoShape::position() const {
519 520 521
    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
522 523 524
}

void KoShape::addConnectionPoint( const QPointF &point ) {
525 526 527
    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
528 529 530 531 532 533
}

QList<QPointF> KoShape::connectors() const {
    return d->connectors.toList();
}

534
QList<QPointF> KoShape::connectionPoints() const {
535 536 537 538 539 540 541
    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;
542 543
}

Thomas Zander's avatar
Thomas Zander committed
544
void KoShape::setBackground ( const QBrush & brush ) {
545
    d->backgroundBrush = brush;
546 547
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
548 549
}

550
QBrush KoShape::background() const {
551
    return d->backgroundBrush;
Thomas Zander's avatar
Thomas Zander committed
552 553 554
}

void KoShape::setZIndex(int zIndex) {
555
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
556 557 558 559 560 561 562 563 564 565 566
    d->zIndex = zIndex;
}

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

bool KoShape::isVisible() const {
    return d->visible;
}

567 568 569 570 571 572 573
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
    
bool KoShape::isPrintable() const
{
574 575 576 577
    if (d->visible)
        return d->printable;
    else
        return false;
578 579
}

Thomas Zander's avatar
Thomas Zander committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 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
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 );
}

632 633 634 635 636
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

void KoShape::setBorder(KoShapeBorderModel *border) {
637 638
    if(d->border)
        d->border->removeUser();
639
    d->border = border;
640 641
    if(d->border)
        d->border->addUser();
642 643
    d->shapeChanged(BorderChanged);
    notifyChanged();
644 645
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
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;
}

662
const QMatrix& KoShape::matrix() const {
663
    return d->localMatrix;
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678
void KoShape::addConnection(KoShapeConnection *connection) {
    d->connections.append(connection);
    foreach(KoShapeManager *sm, d->shapeManagers)
        sm->addShapeConnection( connection );
}

void KoShape::removeConnection(KoShapeConnection *connection) {
    d->connections.removeAll(connection);
}

QList<KoShapeConnection*> KoShape::connections() const {
    return d->connections;
}
679

680
void KoShape::removeConnectionPoint( int index )
681 682 683 684 685
{
    if( index < d->connectors.count() )
        d->connectors.remove( index );
}

686
QString KoShape::name() const {
687 688 689 690 691 692
    return d->name;
}

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

694 695 696 697 698 699 700
void KoShape::deleteLater() {
    foreach(KoShapeManager *manager, d->shapeManagers)
        manager->remove(this);
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
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;
}
717

718
// loading & saving methods
719
void KoShape::saveOdfConnections(KoShapeSavingContext &context) const {
720
    // TODO  save "draw-glue-point" elements (9.2.19)
721
    Q_UNUSED( context );
722
}
723

724
QString KoShape::saveStyle( KoGenStyle &style, KoShapeSavingContext &context ) const
725 726 727 728 729 730 731
{
    // and fill the style
    KoShapeBorderModel * b = border();
    if ( b )
    {
        b->fillStyle( style, context );
    }
732 733 734
    KoShapeShadow * s = shadow();
    if( s )
        s->fillStyle( style, context );
735

736
    KoShapeStyleWriter styleWriter( context );
737

738
    return styleWriter.addFillStyle( style, background() );
739 740
}

741 742
void KoShape::loadStyle( const KoXmlElement & element, KoShapeLoadingContext &context )
{
743
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
744 745 746 747 748
    styleStack.save();

    // fill the style stack with the shapes style
    if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
749
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::draw, "style-name", "graphic" );
750 751 752 753
        styleStack.setTypeProperties( "graphic" );
    }
    else if( element.hasAttributeNS( KoXmlNS::presentation, "style-name" ) )
    {
754
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::presentation, "style-name", "presentation" );
755 756 757
        styleStack.setTypeProperties( "presentation" );
    }

758 759
    setBackground( loadOdfFill( element, context ) );
    setBorder( loadOdfStroke( element, context ) );
760
    setShadow( loadOdfShadow( element, context ) );
761 762

    styleStack.restore();
763 764
}

765 766
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
767 768 769 770 771 772 773 774 775
    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() ) ) );
776
        setSize( size );
777 778
    }

779 780 781 782 783 784 785
    if ( attributes & OdfMandatories ) {
        if ( element.hasAttributeNS( KoXmlNS::draw, "layer" ) ) {
            KoShapeLayer * layer = context.layer( element.attributeNS( KoXmlNS::draw, "layer" ) );
            if ( layer ) {
                setParent( layer );
            }
        }
786 787 788 789 790 791
        if ( element.hasAttributeNS( KoXmlNS::draw, "id" ) ) {
            QString id = element.attributeNS( KoXmlNS::draw, "id" );
            if ( !id.isNull() ) {
                context.addShapeId( this, id );
            }
        }
792
        if ( element.hasAttributeNS( KoXmlNS::draw, "z-index" ) ) {
793
            context.addShapeZIndex( this, element.attributeNS( KoXmlNS::draw, "z-index" ).toInt() );
794
        }
795
        setZIndex( context.zIndex() );
796

797
        if( element.hasAttributeNS( KoXmlNS::draw, "name" ) ) {
Jan Hambrecht's avatar
Jan Hambrecht committed
798
            setName( element.attributeNS( KoXmlNS::draw, "name" ) );
799
        }
Jan Hambrecht's avatar
Jan Hambrecht committed
800

801
        loadStyle( element, context );
802
    }
803

Jan Hambrecht's avatar
 
Jan Hambrecht committed
804 805 806 807
    if( attributes & OdfTransformation )
    {
        QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
        if( ! transform.isEmpty() )
808
            applyAbsoluteTransformation( parseOdfTransform( transform ) );
Jan Hambrecht's avatar
 
Jan Hambrecht committed
809 810
    }

811 812 813
    return true;
}

814
QString KoShape::getStyleProperty( const char *property, const KoXmlElement & element, KoShapeLoadingContext & context )
815
{
816
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
817
    QString value;
818

819 820
    if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
821 822
        if( styleStack.hasProperty( KoXmlNS::draw, property ) )
            value = styleStack.property( KoXmlNS::draw, property );
823 824 825
    }
    else if( element.hasAttributeNS( KoXmlNS::presentation, "style-name" ) )
    {
826 827
        if ( styleStack.hasProperty( KoXmlNS::presentation, property ) )
            value = styleStack.property( KoXmlNS::presentation, property );
828 829
    }

830 831 832 833 834
    return value;
}

QBrush KoShape::loadOdfFill( const KoXmlElement & element, KoShapeLoadingContext & context )
{
835
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
836
    QString fill = getStyleProperty( "fill", element, context );
837
    if ( fill == "solid" || fill == "hatch" )
838
        return KoOdfGraphicStyles::loadOasisFillStyle( styleStack, fill, context.odfLoadingContext().stylesReader() );
839
    else if( fill == "gradient" )
840
        return KoOdfGraphicStyles::loadOasisGradientStyle( styleStack, context.odfLoadingContext().stylesReader(), size() );
841
    else if( fill == "bitmap" )
842
        return KoOdfGraphicStyles::loadOasisPatternStyle( styleStack, context.odfLoadingContext(), size() );
843 844 845 846

    return QBrush();
}

847 848
KoShapeBorderModel * KoShape::loadOdfStroke( const KoXmlElement & element, KoShapeLoadingContext & context )
{
849
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
850
    QString stroke = getStyleProperty( "stroke", element, context );
851
    if( stroke == "solid" || stroke == "dash" )
852
    {
853
        QPen pen = KoOdfGraphicStyles::loadOasisStrokeStyle( styleStack, stroke, context.odfLoadingContext().stylesReader() );
854

855
        KoLineBorder * border = new KoLineBorder();
856
        border->setLineWidth( pen.widthF() );
857 858 859
        border->setColor( pen.color() );
        border->setJoinStyle( pen.joinStyle() );
        border->setLineStyle( pen.style(), pen.dashPattern() );
860

861 862 863 864
        return border;
    }
    else
        return 0;
865 866
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
KoShapeShadow * KoShape::loadOdfShadow( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
    QString stroke = getStyleProperty( "shadow", element, context );
    if( stroke == "visible" || stroke == "hidden" )
    {
        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 );

        return shadow;
    }
    return 0;
}

Jan Hambrecht's avatar
 
Jan Hambrecht committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
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
906
        if(subtransform[0].startsWith(';') || subtransform[0].startsWith(','))
Jan Hambrecht's avatar
 
Jan Hambrecht committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
            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 par