KoShape.cpp 30.7 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"
Thomas Zander's avatar
Thomas Zander committed
40

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

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

55 56
#include <kdebug.h>

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

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

91 92 93 94
    void shapeChanged(ChangeType type) {
        if(parent)
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
95 96
        foreach( KoShape * shape, dependees )
            shape->notifyShapeChanged( me, type );
97 98
    }

Thomas Zander's avatar
Thomas Zander committed
99 100
    QSizeF size; // size in pt
    QString shapeId;
101
    QString name; ///< the shapes names
Thomas Zander's avatar
Thomas Zander committed
102

103
    QMatrix localMatrix; ///< the shapes local transformation matrix
Thomas Zander's avatar
Thomas Zander committed
104 105 106 107 108 109 110

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

    bool visible;
111
    bool printable;
Thomas Zander's avatar
Thomas Zander committed
112 113 114 115 116 117 118 119
    bool locked;
    bool keepAspect;
    bool selectable;
    bool detectCollision;

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

Thomas Zander's avatar
Thomas Zander committed
127
KoShape::KoShape()
128
    : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
129
{
130 131 132 133 134
    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 ) );

135
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
136 137 138 139
}

KoShape::~KoShape()
{
140
    d->shapeChanged( Deleted );
Thomas Zander's avatar
Thomas Zander committed
141
    delete d;
Thomas Zander's avatar
Thomas Zander committed
142 143
}

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

165
void KoShape::setScale( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
166
{
167 168 169 170 171
    QPointF pos = position();
    QMatrix scaleMatrix;
    scaleMatrix.translate( pos.x(), pos.y() );
    scaleMatrix.scale( sx, sy );
    scaleMatrix.translate( -pos.x(), -pos.y() );
172
    d->localMatrix = d->localMatrix * scaleMatrix;
173

174
    notifyChanged();
175
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
176 177 178 179
}

void KoShape::rotate( double angle )
{
180 181 182 183 184
    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() );
185
    d->localMatrix = d->localMatrix * rotateMatrix;
186

187
    notifyChanged();
188
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
189 190
}

191
void KoShape::setShear( double sx, double sy )
Thomas Zander's avatar
Thomas Zander committed
192
{
193 194 195 196 197
    QPointF pos = position();
    QMatrix shearMatrix;
    shearMatrix.translate( pos.x(), pos.y() );
    shearMatrix.shear( sx, sy );
    shearMatrix.translate( -pos.x(), -pos.y() );
198
    d->localMatrix = d->localMatrix * shearMatrix;
199

200
    notifyChanged();
201
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
202 203
}

204
void KoShape::setSize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
205
{
206 207
    QSizeF oldSize( size() );
    if(oldSize == newSize )
Thomas Zander's avatar
Thomas Zander committed
208
        return;
209

Thomas Zander's avatar
Thomas Zander committed
210
    d->size = newSize;
211

212
    notifyChanged();
213
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
214 215
}

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

    notifyChanged();
226
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
227 228 229 230
}

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

234
    QPointF point = absoluteTransformation(0).inverted().map( position );
235
    KoInsets insets(0, 0, 0, 0);
236 237
    if(d->border)
        d->border->borderInsets(this, insets);
Thomas Zander's avatar
Thomas Zander committed
238

239 240
    QSizeF s( size() );
    return point.x() >= -insets.left && point.x() <= s.width() + insets.right &&
241
           point.y() >= -insets.top && point.y() <= s.height() + insets.bottom;
Thomas Zander's avatar
Thomas Zander committed
242 243
}

244
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
245
{
246
    QRectF bb( QPointF(0, 0), size() );
247 248 249 250 251
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
252
    return absoluteTransformation(0).mapRect( bb );
Thomas Zander's avatar
Thomas Zander committed
253 254
}

255
QMatrix KoShape::absoluteTransformation(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
256 257
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
258 259 260
    KoShapeContainer * container = d->parent;
    if( container ) {
        if( container->childClipped(this) )
261
            matrix = container->absoluteTransformation(0);
Thomas Zander's avatar
Thomas Zander committed
262
        else {
263 264
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF( 0.5*containerSize.width(), 0.5*containerSize.height() );
Thomas Zander's avatar
Thomas Zander committed
265
            if(converter)
266
                containerPos = converter->documentToView(containerPos);
267
            matrix.translate( containerPos.x(), containerPos.y() );
Thomas Zander's avatar
Thomas Zander committed
268 269 270
        }
    }

271
    if(converter) {
272
        QPointF pos = d->localMatrix.map( QPoint() );
273 274
        QPointF trans = converter->documentToView( pos ) - pos;
        matrix.translate( trans.x(), trans.y() );
Thomas Zander's avatar
Thomas Zander committed
275
    }
276 277

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
278 279
}

280
void KoShape::applyAbsoluteTransformation( const QMatrix &matrix )
281
{
282
    QMatrix globalMatrix = absoluteTransformation(0);
283 284
    // 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
285
    // to be relative to the local coordinate system
286
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
287 288 289 290 291 292
    applyTransformation( transformMatrix );
}

void KoShape::applyTransformation( const QMatrix &matrix )
{
    d->localMatrix = matrix * d->localMatrix;
293
    notifyChanged();
294
    d->shapeChanged(GenericMatrixChange);
295 296 297 298 299 300
}

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

304
QMatrix KoShape::transformation() const
305 306 307 308
{
    return d->localMatrix;
}

309
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2) {
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    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
326 327 328
}

void KoShape::setParent(KoShapeContainer *parent) {
329 330 331
    if(d->parent == parent)
        return;
    if(parent && dynamic_cast<KoShape*>(parent) != this) {
Thomas Zander's avatar
Thomas Zander committed
332
        d->parent = parent;
333 334
        parent->addChild(this);
    }
Thomas Zander's avatar
Thomas Zander committed
335
    else
Thomas Zander's avatar
Thomas Zander committed
336
        d->parent = 0;
337
    notifyChanged();
338
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
339 340 341 342
}

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

347
void KoShape::update() const {
Thomas Zander's avatar
Thomas Zander committed
348
    if ( !d->shapeManagers.empty() )
349
    {
Thorsten Zachmann's avatar
Thorsten Zachmann committed
350
        QRectF rect( boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
351
        foreach( KoShapeManager * manager, d->shapeManagers )
352
            manager->update( rect, this, true );
Thomas Zander's avatar
Thomas Zander committed
353 354 355
    }
}

356
void KoShape::update(const QRectF &shape) const {
Thomas Zander's avatar
Thomas Zander committed
357
    if ( !d->shapeManagers.empty() && isVisible() )
358
    {
359
        QRectF rect(absoluteTransformation(0).mapRect(shape));
Thomas Zander's avatar
Thomas Zander committed
360
        foreach( KoShapeManager * manager, d->shapeManagers )
361
        {
362
            manager->update(rect);
363 364
        }
    }
Thomas Zander's avatar
Thomas Zander committed
365 366 367 368
}

const QPainterPath KoShape::outline() const {
    QPainterPath path;
369
    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
370 371 372
    return path;
}

373 374 375 376
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const {
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
377 378 379
        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
380
        case KoFlake::CenteredPosition: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
381
    }
382
    return absoluteTransformation(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
383 384
}

385
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor) {
386 387
    QPointF currentAbsPosition = absolutePosition( anchor );
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
388 389
    QMatrix translateMatrix;
    translateMatrix.translate( translate.x(), translate.y() );
390
    applyAbsoluteTransformation( translateMatrix );
391
    notifyChanged();
392
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
393 394
}

Thomas Zander's avatar
Thomas Zander committed
395
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
396 397
    d->size = shape->size();
    d->connectors.clear();
Thomas Zander's avatar
Thomas Zander committed
398 399
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
400 401
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
402 403 404 405 406 407 408
    
    // Ensure printable is true by default
    if (!d->visible)
        d->printable = true;
    else
        d->printable = shape->isPrintable();
        
Thomas Zander's avatar
Thomas Zander committed
409 410
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
411
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
412 413
}

Thomas Zander's avatar
Thomas Zander committed
414
void KoShape::notifyChanged()
415
{
Thomas Zander's avatar
Thomas Zander committed
416
    foreach( KoShapeManager * manager, d->shapeManagers )
417
    {
Thomas Zander's avatar
Thomas Zander committed
418
        manager->notifyShapeChanged( this );
419 420 421
    }
}

422
void KoShape::setUserData(KoShapeUserData *userData) {
423
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
424
    d->userData = userData;
425 426 427
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
428
    return d->userData;
429 430
}

431
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
432
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
433
    d->appData = appData;
434 435 436
}

KoShapeApplicationData *KoShape::applicationData() const {
Thomas Zander's avatar
Thomas Zander committed
437
    return d->appData;
438 439
}

440
bool KoShape::hasTransparency() {
441
    if(d->backgroundBrush.style() == Qt::NoBrush)
Thomas Zander's avatar
Thomas Zander committed
442
        return true;
443
    return !d->backgroundBrush.isOpaque();
Thomas Zander's avatar
Thomas Zander committed
444 445
}

446 447
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
448 449
    if(d->border)
        d->border->borderInsets(this, answer);
450 451 452
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
453
double KoShape::scaleX() const {
454
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
455
}
456

Thomas Zander's avatar
Thomas Zander committed
457
double KoShape::scaleY() const {
458
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
459 460 461
}

double KoShape::rotation() const {
462
    // try to extract the rotation angle out of the local matrix
463 464 465 466
    // 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
467
        return NAN;
468 469
    // 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
470
        return NAN;
471 472

    // calculate the angle from the matrix elements
473
    double angle = atan2( -d->localMatrix.m21(), d->localMatrix.m11() ) * 180.0 / M_PI;
474 475 476 477
    if( angle < 0.0 )
        angle += 360.0;

    return angle;
Thomas Zander's avatar
Thomas Zander committed
478 479 480
}

double KoShape::shearX() const {
481
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
482 483 484
}

double KoShape::shearY() const {
485
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
486 487 488 489 490 491 492
}

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

QPointF KoShape::position() const {
493 494 495
    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
496 497 498
}

void KoShape::addConnectionPoint( const QPointF &point ) {
499 500 501
    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
502 503 504 505 506 507
}

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

508
QList<QPointF> KoShape::connectionPoints() const {
509 510 511 512 513 514 515
    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;
516 517
}

Thomas Zander's avatar
Thomas Zander committed
518
void KoShape::setBackground ( const QBrush & brush ) {
519
    d->backgroundBrush = brush;
Thomas Zander's avatar
Thomas Zander committed
520 521
}

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

void KoShape::setZIndex(int zIndex) {
527
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
528 529 530 531 532 533 534 535 536 537 538
    d->zIndex = zIndex;
}

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

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

539 540 541 542 543 544 545
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
    
bool KoShape::isPrintable() const
{
546 547 548 549
    if (d->visible)
        return d->printable;
    else
        return false;
550 551
}

Thomas Zander's avatar
Thomas Zander committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
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 );
}

604 605 606 607 608
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

void KoShape::setBorder(KoShapeBorderModel *border) {
609 610
    if(d->border)
        d->border->removeUser();
611
    d->border = border;
612 613
    if(d->border)
        d->border->addUser();
614 615 616
}

const QMatrix& KoShape::matrix() const {
617
    return d->localMatrix;
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632
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;
}
633

634
void KoShape::removeConnectionPoint( int index )
635 636 637 638 639
{
    if( index < d->connectors.count() )
        d->connectors.remove( index );
}

640
QString KoShape::name() const {
641 642 643 644 645 646
    return d->name;
}

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

648 649 650 651 652 653 654
void KoShape::deleteLater() {
    foreach(KoShapeManager *manager, d->shapeManagers)
        manager->remove(this);
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
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;
}
671

672
// loading & saving methods
673
void KoShape::saveOdfConnections(KoShapeSavingContext &context) const {
674
    // TODO  save "draw-glue-point" elements (9.2.19)
675
    Q_UNUSED( context );
676
}
677

678
QString KoShape::saveStyle( KoGenStyle &style, KoShapeSavingContext &context ) const
679 680 681 682 683 684 685 686
{
    // and fill the style
    KoShapeBorderModel * b = border();
    if ( b )
    {
        b->fillStyle( style, context );
    }

687
    KoShapeStyleWriter styleWriter( context );
688

689
    return styleWriter.addFillStyle( style, background() );
690 691
}

692 693 694 695 696 697
void KoShape::loadStyle( const KoXmlElement & element, KoShapeLoadingContext &context )
{
    setBackground( loadOdfFill( element, context ) );
    setBorder( loadOdfStroke( element, context ) );
}

698 699
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
700 701 702 703 704 705 706 707 708
    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() ) ) );
709
        setSize( size );
710 711
    }

712 713 714 715 716 717 718
    if ( attributes & OdfMandatories ) {
        if ( element.hasAttributeNS( KoXmlNS::draw, "layer" ) ) {
            KoShapeLayer * layer = context.layer( element.attributeNS( KoXmlNS::draw, "layer" ) );
            if ( layer ) {
                setParent( layer );
            }
        }
719 720 721 722 723 724
        if ( element.hasAttributeNS( KoXmlNS::draw, "id" ) ) {
            QString id = element.attributeNS( KoXmlNS::draw, "id" );
            if ( !id.isNull() ) {
                context.addShapeId( this, id );
            }
        }
725
        if ( element.hasAttributeNS( KoXmlNS::draw, "z-index" ) ) {
726
            context.addShapeZIndex( this, element.attributeNS( KoXmlNS::draw, "z-index" ).toInt() );
727
        }
728
        setZIndex( context.zIndex() );
729

730
        if( element.hasAttributeNS( KoXmlNS::draw, "name" ) ) {
Jan Hambrecht's avatar
Jan Hambrecht committed
731
            setName( element.attributeNS( KoXmlNS::draw, "name" ) );
732
        }
Jan Hambrecht's avatar
Jan Hambrecht committed
733

734
        loadStyle( element, context );
735
    }
736

Jan Hambrecht's avatar
 
Jan Hambrecht committed
737 738 739 740
    if( attributes & OdfTransformation )
    {
        QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
        if( ! transform.isEmpty() )
741
            applyAbsoluteTransformation( parseOdfTransform( transform ) );
Jan Hambrecht's avatar
 
Jan Hambrecht committed
742 743
    }

744 745 746
    return true;
}

747
QString KoShape::getStyleProperty( const char *property, const KoXmlElement & element, KoShapeLoadingContext & context )
748 749
{
    KoStyleStack &styleStack = context.koLoadingContext().styleStack();
750
    QString value;
751 752 753 754 755
    if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
        // fill the style stack with the shapes style
        context.koLoadingContext().fillStyleStack( element, KoXmlNS::draw, "style-name", "graphic" );
        styleStack.setTypeProperties( "graphic" );
756 757
        if( styleStack.hasProperty( KoXmlNS::draw, property ) )
            value = styleStack.property( KoXmlNS::draw, property );
758 759 760 761 762 763
    }
    else if( element.hasAttributeNS( KoXmlNS::presentation, "style-name" ) )
    {
        // fill the style stack with the shapes style
        context.koLoadingContext().fillStyleStack( element, KoXmlNS::presentation, "style-name", "presentation" );
        styleStack.setTypeProperties( "presentation" );
764 765
        if ( styleStack.hasProperty( KoXmlNS::presentation, property ) )
            value = styleStack.property( KoXmlNS::presentation, property );
766 767
    }

768 769 770 771 772 773 774
    return value;
}

QBrush KoShape::loadOdfFill( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.koLoadingContext().styleStack();
    QString fill = getStyleProperty( "fill", element, context );
775
    if ( fill == "solid" || fill == "hatch" )
776
        return KoOdfGraphicStyles::loadOasisFillStyle( styleStack, fill, context.koLoadingContext().stylesReader() );
777
    else if( fill == "gradient" )
778
        return KoOdfGraphicStyles::loadOasisGradientStyle( styleStack, context.koLoadingContext().stylesReader(), size() );
779
    else if( fill == "bitmap" )
780
        return KoOdfGraphicStyles::loadOasisPatternStyle( styleStack, context.koLoadingContext(), size() );
781 782 783 784

    return QBrush();
}

785 786 787
KoShapeBorderModel * KoShape::loadOdfStroke( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.koLoadingContext().styleStack();
788
    QString stroke = getStyleProperty( "stroke", element, context );
789
    if( stroke == "solid" || stroke == "dash" )
790
    {
791
        QPen pen = KoOdfGraphicStyles::loadOasisStrokeStyle( styleStack, stroke, context.koLoadingContext().stylesReader() );
792

793
        KoLineBorder * border = new KoLineBorder();
794
        border->setLineWidth( pen.widthF() );
795 796 797
        border->setColor( pen.color() );
        border->setJoinStyle( pen.joinStyle() );
        border->setLineStyle( pen.style(), pen.dashPattern() );
798

799 800 801 802
        return border;
    }
    else
        return 0;
803 804
}

Jan Hambrecht's avatar
 
Jan Hambrecht committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
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
822
        if(subtransform[0].startsWith(';') || subtransform[0].startsWith(','))
Jan Hambrecht's avatar
 
Jan Hambrecht committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
            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;
}

878
void KoShape::saveOdfFrameAttributes(KoShapeSavingContext &context) const {
879
    saveOdfAttributes(context, FrameAttributes);
880
    context.addOption(KoShapeSavingContext::FrameOpened);
881 882
}

883
void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) const {
884
    if(attributes & OdfMandatories) {
885
        KoGenStyle style;
886
        // all items that should be written to 'draw:frame' and any other 'draw:' object that inherits this shape
887 888 889 890 891 892 893 894
        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 ) );
        }
895

896
        if ( context.isSet( KoShapeSavingContext::DrawId ) )
897
        {
898
            context.xmlWriter().addAttribute( "draw:id", context.drawId( this ) );
899 900
        }

Jan Hambrecht's avatar
Jan Hambrecht committed
901 902 903
        if( ! name().isEmpty() )
            context.xmlWriter().addAttribute( "draw:name", name() );

904
        if(d->parent && dynamic_cast<KoShapeLayer*> (d->parent))
905
            context.xmlWriter().addAttribute("draw:layer", d->parent->name());
906 907
    }

908 909
    // all items after this should not be written out when they have already be written in
    // a 'draw:frame' attribute.
910 911
    if(context.isSet(KoShapeSavingContext::FrameOpened)) {
        context.removeOption(KoShapeSavingContext::FrameOpened);
912
        return;
913 914
    }

915 916
    if(attributes & OdfSize) {
        QSizeF s( size() );
917 918
        context.xmlWriter().addAttributePt( "svg:width", s.width() );
        context.xmlWriter().addAttributePt( "svg:height", s.height() );
Jan Hambrecht's avatar
Jan Hambrecht committed
919 920 921
        // the position is hidden in the transformation
        context.xmlWriter().addAttributePt( "svg:x", 0.0 );
        context.xmlWriter().addAttributePt( "svg:y", 0.0 );
922 923
    }

924 925 926
    if(attributes & OdfTransformation) {
        // just like in shapes; ODF allows you to manipulate the 'matrix' after setting an
        // ofset on the shape (using the x and y positions).   Lets save them here.
927
        /*
928 929 930 931 932
        bool rotate = qAbs(d->angle) > 1E-6;
        bool skew = qAbs(d->shearX) > 1E-6 || qAbs(d->shearY) > 1E-6;
        bool scale = qAbs(d->scaleX - 1) > 1E-6 || qAbs(d->scaleY -1) > 1E-6;

        if(rotate && (skew || scale)) {
933
            QMatrix matrix; // can't use absoluteTransformation() as that includes transformation of the container as well.
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
            QSizeF size(this->size());
            if ( d->angle != 0 )
            {
                matrix.translate( size.width() / 2.0 * d->scaleX, size.height() / 2.0 * d->scaleY );
                matrix.translate( size.height() / 2.0 * d->shearX, size.width() / 2.0 * d->shearY );
                matrix.rotate( d->angle );
                matrix.translate( -size.width() / 2.0 * d->scaleX, -size.height() / 2.0 * d->scaleY );
                matrix.translate( -size.height() / 2.0 * d->shearX, -size.width() / 2.0 * d->shearY );
            }
            matrix.shear( d->shearX, d->shearY );
            matrix.scale( d->scaleX, d->scaleY );

            QString m = QString( "matrix(0 0 %3 %4 %5pt %6pt)" ).arg( matrix.m11() ).arg( matrix.m12() )
                .arg( matrix.m21() ).arg( matrix.m22() )
                .arg( matrix.dx() ) .arg( matrix.dy() );
949
            context.xmlWriter().addAttribute( "draw:transform", m );
950 951 952 953 954 955 956 957 958 959 960 961 962 963
        }
        else if(rotate || skew || scale) {
            QString transform;
            if(rotate)
                transform = "rotate("+ QString::number(d->angle) +')';
            if(skew)
                transform = "skewX("+ QString::number(d->shearX) +") skewY("+ QString::number(d->shearY) +')';
            if(scale) {
                transform += "scale("+ QString::number(d->scaleX);
                if(d->scaleX != d->scaleY)
                    transform += ','+ QString::number(d->scaleY);
                transform += ')';
            }

964
            context.xmlWriter().addAttribute( "draw:transform", transform );
965
        }
966
        */
967
        QMatrix matrix = absoluteTransformation(0);
968
        if( ! matrix.isIdentity() )
969
        {
Jan Hambrecht's avatar
 
Jan Hambrecht committed
970
            QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
971 972 973
                    .arg( matrix.m11() ).arg( matrix.m12() )
                    .arg( matrix.m21() ).arg( matrix.m22() )
                    .arg( matrix.dx() ) .arg( matrix.dy() );
974 975
            context.xmlWriter().addAttribute( "draw:transform", m );
        }
976 977 978 979 980 981 982 983 984 985 986 987 988
    }
}

// end loading & saving methods


// static
void KoShape::applyConversion(QPainter &painter, const KoViewConverter &converter) {
    double zoomX, zoomY;
    converter.zoom(&zoomX, &zoomY);
    painter.scale(zoomX, zoomY);
}

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
void KoShape::addDependee( KoShape * shape )
{
    if( ! d->dependees.contains( shape ) )
        d->dependees.append( shape );
}

void KoShape::removeDependee( KoShape * shape )
{
    int index = d->dependees.indexOf( shape );
    if( index >= 0 )
        d->dependees.removeAt( index );
}

void KoShape::notifyShapeChanged( KoShape * shape, ChangeType type )
{
    Q_UNUSED( shape );
    Q_UNUSED( type );
}