KoShape.cpp 21.3 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 "KoViewConverter.h"
Thomas Zander's avatar
Thomas Zander committed
36

37 38 39 40
#include <KoXmlWriter.h>
#include <KoGenStyles.h>
#include <KoGenStyle.h>

Thomas Zander's avatar
Thomas Zander committed
41
#include <QPainter>
42
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
43
#include <QPainterPath>
44
#include <QList>
Thomas Zander's avatar
Thomas Zander committed
45

46 47
#include <kdebug.h>

48
class KoShape::Private {
Thomas Zander's avatar
Thomas Zander committed
49
public:
50
    Private(KoShape *shape)
Thomas Zander's avatar
Thomas Zander committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        : scaleX( 1 ),
        scaleY( 1 ),
        angle( 0 ),
        shearX( 0 ),
        shearY( 0 ),
        size( 50, 50 ),
        pos( 0, 0 ),
        zIndex( 0 ),
        parent( 0 ),
        visible( true ),
        locked( false ),
        keepAspect( false ),
        selectable( true ),
        detectCollision( false ),
        userData(0),
66 67
        appData(0),
        backgroundBrush(Qt::NoBrush),
68 69
        border(0),
        me(shape)
Thomas Zander's avatar
Thomas Zander committed
70 71 72
    {
    }

73
    ~Private() {
Thomas Zander's avatar
Thomas Zander committed
74 75 76 77
        delete userData;
        delete appData;
    }

78 79 80 81 82 83
    void shapeChanged(ChangeType type) {
        if(parent)
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
    }

Thomas Zander's avatar
Thomas Zander committed
84 85 86 87 88 89 90 91 92
    double scaleX;
    double scaleY;
    double angle; // degrees
    double shearX;
    double shearY;

    QSizeF size; // size in pt
    QPointF pos; // position (top left) in pt
    QString shapeId;
93
    QString name; ///< the shapes names
Thomas Zander's avatar
Thomas Zander committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    QMatrix matrix;

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

    bool visible;
    bool locked;
    bool keepAspect;
    bool selectable;
    bool detectCollision;

    QSet<KoShapeManager *> shapeManagers;
    KoShapeUserData *userData;
    KoShapeApplicationData *appData;
111 112
    QBrush backgroundBrush; ///< Stands for the background color / fill etc.
    KoShapeBorderModel *border; ///< points to a border, or 0 if there is no border
113
    QList<KoShapeConnection*> connections;
114
    KoShape *me;
Thomas Zander's avatar
Thomas Zander committed
115 116
};

Thomas Zander's avatar
Thomas Zander committed
117
KoShape::KoShape()
118
    : d(new Private(this))
Thomas Zander's avatar
Thomas Zander committed
119 120 121 122 123 124
{
    recalcMatrix();
}

KoShape::~KoShape()
{
Thomas Zander's avatar
Thomas Zander committed
125
    delete d;
Thomas Zander's avatar
Thomas Zander committed
126 127
}

128
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, const KoCanvasBase *canvas) {
Thomas Zander's avatar
Thomas Zander committed
129 130 131
    Q_UNUSED(painter);
    Q_UNUSED(converter);
    Q_UNUSED(canvas);
132
/* 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
133 134 135 136 137 138 139
    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
140
        for ( int i = 0; i < d->connectors.size(); ++i )
Thomas Zander's avatar
Thomas Zander committed
141
        {
Thomas Zander's avatar
Thomas Zander committed
142
            QPointF p = converter.documentToView(d->connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
143 144 145
            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 ) );
        }
146
    }*/
Thomas Zander's avatar
Thomas Zander committed
147 148 149 150
}

void KoShape::scale( double sx, double sy )
{
Thomas Zander's avatar
Thomas Zander committed
151
    if(d->scaleX == sx && d->scaleY == sy)
Thomas Zander's avatar
Thomas Zander committed
152
        return;
Thomas Zander's avatar
Thomas Zander committed
153 154
    d->scaleX = sx;
    d->scaleY = sy;
Thomas Zander's avatar
Thomas Zander committed
155
    recalcMatrix();
156
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
157 158 159 160
}

void KoShape::rotate( double angle )
{
Thomas Zander's avatar
Thomas Zander committed
161
    if(d->angle == angle)
Thomas Zander's avatar
Thomas Zander committed
162
        return;
Thomas Zander's avatar
Thomas Zander committed
163 164 165
    d->angle = angle;
    while(d->angle >= 360) d->angle -= 360;
    while(d->angle <= -360) d->angle += 360;
Thomas Zander's avatar
Thomas Zander committed
166
    recalcMatrix();
167
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
168 169 170 171
}

void KoShape::shear( double sx, double sy )
{
Thomas Zander's avatar
Thomas Zander committed
172
    if(d->shearX == sx && d->shearY == sy)
Thomas Zander's avatar
Thomas Zander committed
173
        return;
Thomas Zander's avatar
Thomas Zander committed
174 175
    d->shearX = sx;
    d->shearY = sy;
Thomas Zander's avatar
Thomas Zander committed
176
    recalcMatrix();
177
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
178 179
}

180
void KoShape::resize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
181
{
182 183
    QSizeF s( size() );
    if(s == newSize)
Thomas Zander's avatar
Thomas Zander committed
184
        return;
185

186 187
    double fx = newSize.width() / s.width();
    double fy = newSize.height() / s.height();
188

Thomas Zander's avatar
Thomas Zander committed
189
    d->size = newSize;
190

Thomas Zander's avatar
Thomas Zander committed
191
    for ( int i = 0; i < d->connectors.size(); ++i )
192
    {
Thomas Zander's avatar
Thomas Zander committed
193
        QPointF &point = d->connectors[i];
194 195 196
        point.setX(point.x() * fx);
        point.setY(point.y() * fy);
    }
197
    recalcMatrix();
198
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
199 200 201 202
}

void KoShape::setPosition( const QPointF &position )
{
Thomas Zander's avatar
Thomas Zander committed
203
    if(d->pos == position)
Thomas Zander's avatar
Thomas Zander committed
204
        return;
Thomas Zander's avatar
Thomas Zander committed
205
    d->pos = position;
Thomas Zander's avatar
Thomas Zander committed
206
    recalcMatrix();
207
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
208 209 210 211
}

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

215
    QPointF point( position * d->matrix.inverted() );
216
    KoInsets insets(0, 0, 0, 0);
217 218
    if(d->border)
        d->border->borderInsets(this, insets);
Thomas Zander's avatar
Thomas Zander committed
219

220 221 222
    QSizeF s( size() );
    return point.x() >= -insets.left && point.x() <= s.width() + insets.right &&
             point.y() >= -insets.top && point.y() <= s.height() + insets.bottom;
Thomas Zander's avatar
Thomas Zander committed
223 224
}

225
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
226
{
227
    QRectF bb( QPointF(0, 0), size() );
228 229 230 231 232
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
Thomas Zander's avatar
Thomas Zander committed
233
    return d->matrix.mapRect( bb );
Thomas Zander's avatar
Thomas Zander committed
234 235 236 237
}

void KoShape::recalcMatrix()
{
Thomas Zander's avatar
Thomas Zander committed
238
    d->matrix = transformationMatrix(0);
Thomas Zander's avatar
Thomas Zander committed
239
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
240 241
}

242
QMatrix KoShape::transformationMatrix(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
243 244 245
    QMatrix matrix;
    QRectF zoomedRect = QRectF(position(), size());
    if(converter)
246
        zoomedRect = converter->documentToView(zoomedRect);
Thomas Zander's avatar
Thomas Zander committed
247 248 249
    matrix.translate( zoomedRect.x(), zoomedRect.y() );

    // apply parents matrix to inherit any transformations done there.
Thomas Zander's avatar
Thomas Zander committed
250
    KoShapeContainer *container = d->parent;
Thomas Zander's avatar
Thomas Zander committed
251 252 253 254 255 256 257
    KoShape const *child = this;
    while(container) {
        if(container->childClipped(child))
            matrix *= container->transformationMatrix(0);
        else {
            QPointF containerPos =container->position();
            if(converter)
258
                containerPos = converter->documentToView(containerPos);
Thomas Zander's avatar
Thomas Zander committed
259 260
            matrix.translate(containerPos.x(), containerPos.y());
        }
Thomas Zander's avatar
Thomas Zander committed
261
        container = container->parent();
Thomas Zander's avatar
Thomas Zander committed
262 263 264
        child = child->parent();
    }

Thomas Zander's avatar
Thomas Zander committed
265
    if ( d->angle != 0 )
Thomas Zander's avatar
Thomas Zander committed
266
    {
Thomas Zander's avatar
Thomas Zander committed
267 268 269 270 271
        matrix.translate( zoomedRect.width() / 2.0 * d->scaleX, zoomedRect.height() / 2.0 * d->scaleY );
        matrix.translate( zoomedRect.height() / 2.0 * d->shearX, zoomedRect.width() / 2.0 * d->shearY );
        matrix.rotate( d->angle );
        matrix.translate( -zoomedRect.width() / 2.0 * d->scaleX, -zoomedRect.height() / 2.0 * d->scaleY );
        matrix.translate( -zoomedRect.height() / 2.0 * d->shearX, -zoomedRect.width() / 2.0 * d->shearY );
Thomas Zander's avatar
Thomas Zander committed
272
    }
Thomas Zander's avatar
Thomas Zander committed
273 274
    matrix.shear( d->shearX, d->shearY );
    matrix.scale( d->scaleX, d->scaleY );
Thomas Zander's avatar
Thomas Zander committed
275 276 277 278
    return matrix;
}


279
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    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
296 297 298
}

void KoShape::setParent(KoShapeContainer *parent) {
299 300 301
    if(d->parent == parent)
        return;
    if(parent && dynamic_cast<KoShape*>(parent) != this) {
Thomas Zander's avatar
Thomas Zander committed
302
        d->parent = parent;
303 304
        parent->addChild(this);
    }
Thomas Zander's avatar
Thomas Zander committed
305
    else
Thomas Zander's avatar
Thomas Zander committed
306
        d->parent = 0;
Thomas Zander's avatar
Thomas Zander committed
307
    recalcMatrix();
308
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
309 310 311 312
}

int KoShape::zIndex() const {
    if(parent()) // we can't be under our parent...
Thomas Zander's avatar
Thomas Zander committed
313 314
        return qMax(d->zIndex, parent()->zIndex());
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
315 316 317
}

void KoShape::repaint() const {
Thomas Zander's avatar
Thomas Zander committed
318
    if ( !d->shapeManagers.empty() )
319
    {
320 321
        QRectF rect(QPointF(0, 0), size() );
        if(d->border) {
322
            KoInsets insets;
323 324 325 326
            d->border->borderInsets(this, insets);
            rect.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
        }
        rect = d->matrix.mapRect(rect);
Thomas Zander's avatar
Thomas Zander committed
327
        foreach( KoShapeManager * manager, d->shapeManagers )
328
            manager->repaint( rect, this, true );
Thomas Zander's avatar
Thomas Zander committed
329 330 331
    }
}

332
void KoShape::repaint(const QRectF &shape) const {
Thomas Zander's avatar
Thomas Zander committed
333
    if ( !d->shapeManagers.empty() && isVisible() )
334
    {
Thomas Zander's avatar
Thomas Zander committed
335 336
        QRectF rect(d->matrix.mapRect(shape));
        foreach( KoShapeManager * manager, d->shapeManagers )
337 338 339 340
        {
            manager->repaint(rect);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
341 342 343 344
}

const QPainterPath KoShape::outline() const {
    QPainterPath path;
345
    path.addRect(QRectF( QPointF(0, 0), size() ));
Thomas Zander's avatar
Thomas Zander committed
346 347 348
    return path;
}

349 350 351 352
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const {
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
353 354 355 356
        case KoFlake::TopRightCorner: point = QPointF(size().width(), 0.0); break;
        case KoFlake::BottomLeftCorner: point = QPointF(0.0, size().height()); break;
        case KoFlake::BottomRightCorner: point = QPointF(size().width(), size().height()); break;
        case KoFlake::CenteredPositon: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
357 358
    }
    return d->matrix.map(point);
Thomas Zander's avatar
Thomas Zander committed
359 360
}

361
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor) {
Thomas Zander's avatar
Thomas Zander committed
362 363 364
    QPointF zero(0, 0);
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
Thomas Zander's avatar
Thomas Zander committed
365
    KoShapeContainer *container = d->parent;
Thomas Zander's avatar
Thomas Zander committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    KoShape const *child = this;
    while(container) {
        if(container->childClipped(child)) {
            matrix *= container->transformationMatrix(0);
            break;
        }
        else {
            QPointF containerPos =container->position();
            matrix.translate(containerPos.x(), containerPos.y());
        }
        container = dynamic_cast<KoShapeContainer*>(container->parent());
        child = child->parent();
    }
    QPointF vector1 = matrix.inverted().map(zero);

    matrix = QMatrix();
Thomas Zander's avatar
Thomas Zander committed
382
    if ( d->angle != 0 )
383
    {
Thomas Zander's avatar
Thomas Zander committed
384 385 386 387 388
        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 );
389
    }
Thomas Zander's avatar
Thomas Zander committed
390 391
    matrix.shear( d->shearX, d->shearY );
    matrix.scale( d->scaleX, d->scaleY );
Thomas Zander's avatar
Thomas Zander committed
392

393 394 395 396 397 398 399 400 401
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
        case KoFlake::TopRightCorner: point = QPointF(size().width(), 0.0); break;
        case KoFlake::BottomLeftCorner: point = QPointF(0.0, size().height()); break;
        case KoFlake::BottomRightCorner: point = QPointF(size().width(), size().height()); break;
        case KoFlake::CenteredPositon: point = QPointF(size().width() / 2.0, size().height() / 2.0); break;
    }

Thomas Zander's avatar
Thomas Zander committed
402
    QPointF vector2 = matrix.map( point );
Thomas Zander's avatar
Thomas Zander committed
403
    //kDebug(30006) << "vector1: " << vector1 << ", vector2: " << vector2 << endl;
Thomas Zander's avatar
Thomas Zander committed
404 405 406 407

    setPosition(newPosition + vector1 - vector2);
}

Thomas Zander's avatar
Thomas Zander committed
408
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
409 410 411 412 413 414 415 416
    d->pos = shape->position();
    d->scaleX = shape->scaleX();
    d->scaleY = shape->scaleY();
    d->angle = shape->rotation();
    d->shearX = shape->shearX();
    d->shearY = shape->shearY();
    d->size = shape->size();
    d->connectors.clear();
Thomas Zander's avatar
Thomas Zander committed
417 418
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
419 420 421 422
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
Thomas Zander's avatar
Thomas Zander committed
423 424
}

Thomas Zander's avatar
Thomas Zander committed
425 426 427
void KoShape::moveBy(double distanceX, double distanceY) {
    QPointF p = absolutePosition();
    setAbsolutePosition(QPointF(p.x() + distanceX, p.y() + distanceY));
Thomas Zander's avatar
Thomas Zander committed
428 429
}

Thomas Zander's avatar
Thomas Zander committed
430
void KoShape::notifyChanged()
431
{
Thomas Zander's avatar
Thomas Zander committed
432
    foreach( KoShapeManager * manager, d->shapeManagers )
433
    {
Thomas Zander's avatar
Thomas Zander committed
434
        manager->notifyShapeChanged( this );
435 436 437
    }
}

438
void KoShape::setUserData(KoShapeUserData *userData) {
Thomas Zander's avatar
Thomas Zander committed
439 440 441
    if(d->userData)
        delete d->userData;
    d->userData = userData;
442 443 444
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
445
    return d->userData;
446 447
}

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

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

457
bool KoShape::hasTransparency() {
458
    if(d->backgroundBrush.style() == Qt::NoBrush)
Thomas Zander's avatar
Thomas Zander committed
459
        return true;
460
    return !d->backgroundBrush.isOpaque();
Thomas Zander's avatar
Thomas Zander committed
461 462
}

463 464
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
465 466
    if(d->border)
        d->border->borderInsets(this, answer);
467 468 469
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
double KoShape::scaleX() const {
    return d->scaleX;
}
double KoShape::scaleY() const {
    return d->scaleY;
}

double KoShape::rotation() const {
    return d->angle;
}

double KoShape::shearX() const {
    return d->shearX;
}

double KoShape::shearY() const {
    return d->shearY;
}

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

QPointF KoShape::position() const {
    return d->pos;
}

void KoShape::addConnectionPoint( const QPointF &point ) {
    d->connectors.append( point );
}

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

void KoShape::setBackground ( const QBrush & brush ) {
506
    d->backgroundBrush = brush;
Thomas Zander's avatar
Thomas Zander committed
507 508
}

509
QBrush KoShape::background() const {
510
    return d->backgroundBrush;
Thomas Zander's avatar
Thomas Zander committed
511 512 513
}

void KoShape::setZIndex(int zIndex) {
514
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 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
    d->zIndex = zIndex;
}

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

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

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 );
}

578 579 580 581 582 583 584 585 586 587 588 589
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

void KoShape::setBorder(KoShapeBorderModel *border) {
    d->border = border;
}

const QMatrix& KoShape::matrix() const {
    return d->matrix;
}

590 591 592 593 594 595 596 597 598 599 600 601 602
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;
}
603

604
QString KoShape::name() const {
605 606 607 608 609 610
    return d->name;
}

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

// loading & saving methods
613 614 615
void KoShape::saveOdfConnections(KoShapeSavingContext *context) const {
    // TODO  save "draw-glue-point" elements (9.2.19)
}
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

QString KoShape::style( KoShapeSavingContext *context ) const
{
    KoGenStyle style;
    if ( context->isSet( KoShapeSavingContext::PresentationShape ) ) {
        style = KoGenStyle( KoGenStyle::STYLE_PRESENTATIONAUTO, "presentation" );
    }
    else {
        style = KoGenStyle( KoGenStyle::STYLE_GRAPHICAUTO, "graphic" );
    }

    // and fill the style
    KoShapeBorderModel * b = border();
    if ( b )
    {
        b->fillStyle( style, context );
    }
    QBrush bg( background() );
    switch ( bg.style() )
    {
        case Qt::NoBrush:
            style.addProperty( "draw:fill","none" );
            break;
        default:    // TODO all the other ones.
            //KoOasisStyles::saveOasisFillStyle( style, context->mainStyles(), bg );
            break;
    }

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

    return context->mainStyles().lookup( style, context->isSet( KoShapeSavingContext::PresentationShape ) ? "pr" : "gr" );
}

651 652 653 654 655 656 657 658 659 660 661
void KoShape::saveOdfFrameAttributes(KoShapeSavingContext *context) {
    saveOdfAttributes(context, FrameAttributes);
    context->addOption(KoShapeSavingContext::FrameOpened);
}

void KoShape::saveOdfAttributes(KoShapeSavingContext *context, int attributes) const {
    if(attributes & OdfMandatories) {
        // all items that should be written to 'draw:frame' and any other 'draw:' object that inherits this shape
        context->xmlWriter().addAttribute( context->isSet( KoShapeSavingContext::PresentationShape ) ?
                                          "presentation:style-name": "draw:style-name",
                                          style( context ) );
662

663
        if ( context->isSet( KoShapeSavingContext::DrawId ) )
664
        {
665
            context->xmlWriter().addAttribute( "draw:id", context->drawId( this ) );
666 667
        }

668 669
        if(d->parent && dynamic_cast<KoShapeLayer*> (d->parent))
            context->xmlWriter().addAttribute("draw:layer", d->parent->name());
670 671
    }

672 673 674 675 676
    // all items after this should not be written out when they have already be written in
    // a 'draw:frame' attribute.
    if(context->isSet(KoShapeSavingContext::FrameOpened)) {
        context->removeOption(KoShapeSavingContext::FrameOpened);
        return;
677 678
    }

679 680 681 682 683 684
    if(attributes & OdfSize) {
        QSizeF s( size() );
        context->xmlWriter().addAttributePt( "svg:width", s.width() );
        context->xmlWriter().addAttributePt( "svg:height", s.height() );
        context->xmlWriter().addAttributePt( "svg:x", d->pos.x() );
        context->xmlWriter().addAttributePt( "svg:y", d->pos.y() );
685 686
    }

687 688
    if(attributes & OdfMandatories) {
        context->xmlWriter().addAttribute("draw:z-index", zIndex());
689 690
    }

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    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.
        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)) {
            QMatrix matrix; // can't use transformationMatrix() as that includes transformation of the container as well.
            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() );
            context->xmlWriter().addAttribute( "draw:transform", m );
        }
        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 += ')';
            }

            context->xmlWriter().addAttribute( "draw:transform", transform );
        }
732 733 734 735 736 737 738 739 740 741 742 743 744
    }
}

// end loading & saving methods


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