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

40
#include <KoXmlReader.h>
41
#include <KoXmlWriter.h>
42
#include <KoXmlNS.h>
43 44
#include <KoGenStyles.h>
#include <KoGenStyle.h>
45
#include <KoUnit.h>
46
#include <KoOasisStyles.h>
47
#include <KoOasisLoadingContext.h>
48

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

54 55
#include <kdebug.h>

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

76
    ~Private() {
77 78
        foreach(KoShapeManager *manager, shapeManagers)
            manager->remove(me);
Thomas Zander's avatar
Thomas Zander committed
79 80 81 82
        delete userData;
        delete appData;
    }

83 84 85 86 87 88
    void shapeChanged(ChangeType type) {
        if(parent)
            parent->model()->childChanged(me, type);
        me->shapeChanged(type);
    }

Thomas Zander's avatar
Thomas Zander committed
89 90 91
    QSizeF size; // size in pt
    QPointF pos; // position (top left) in pt
    QString shapeId;
92
    QString name; ///< the shapes names
Thomas Zander's avatar
Thomas Zander committed
93

94
    QMatrix localMatrix; ///< the shapes local transformation matrix
Thomas Zander's avatar
Thomas Zander committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    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;
110 111
    QBrush backgroundBrush; ///< Stands for the background color / fill etc.
    KoShapeBorderModel *border; ///< points to a border, or 0 if there is no border
112
    QList<KoShapeConnection*> connections;
113
    KoShape *me;
Thomas Zander's avatar
Thomas Zander committed
114 115
};

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

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

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

void KoShape::scale( double sx, double sy )
{
150 151 152 153 154
    QPointF pos = position();
    QMatrix scaleMatrix;
    scaleMatrix.translate( pos.x(), pos.y() );
    scaleMatrix.scale( sx, sy );
    scaleMatrix.translate( -pos.x(), -pos.y() );
155
    d->localMatrix = d->localMatrix * scaleMatrix;
156

157
    notifyChanged();
158
    d->shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
159 160 161 162
}

void KoShape::rotate( double angle )
{
163 164 165 166 167
    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() );
168
    d->localMatrix = d->localMatrix * rotateMatrix;
169

170
    notifyChanged();
171
    d->shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
172 173 174 175
}

void KoShape::shear( double sx, double sy )
{
176 177 178 179 180
    QPointF pos = position();
    QMatrix shearMatrix;
    shearMatrix.translate( pos.x(), pos.y() );
    shearMatrix.shear( sx, sy );
    shearMatrix.translate( -pos.x(), -pos.y() );
181
    d->localMatrix = d->localMatrix * shearMatrix;
182

183
    notifyChanged();
184
    d->shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
185 186
}

187
void KoShape::resize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
188
{
189 190
    QSizeF s( size() );
    if(s == newSize)
Thomas Zander's avatar
Thomas Zander committed
191
        return;
192

193 194
    double fx = newSize.width() / s.width();
    double fy = newSize.height() / s.height();
195

Thomas Zander's avatar
Thomas Zander committed
196
    d->size = newSize;
197

Thomas Zander's avatar
Thomas Zander committed
198
    for ( int i = 0; i < d->connectors.size(); ++i )
199
    {
Thomas Zander's avatar
Thomas Zander committed
200
        QPointF &point = d->connectors[i];
201 202 203
        point.setX(point.x() * fx);
        point.setY(point.y() * fy);
    }
204
    notifyChanged();
205
    d->shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
206 207
}

208
void KoShape::setPosition( const QPointF &newPosition )
Thomas Zander's avatar
Thomas Zander committed
209
{
210 211
    QPointF currentPos = position();
    if( newPosition == currentPos )
Thomas Zander's avatar
Thomas Zander committed
212
        return;
213
    QMatrix translateMatrix;
214
    translateMatrix.translate( newPosition.x()-currentPos.x(), newPosition.y()-currentPos.y() );
215 216 217
    d->localMatrix = d->localMatrix * translateMatrix;

    notifyChanged();
218
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
219 220 221 222
}

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

226
    QPointF point = transformationMatrix(0).inverted().map( position );
227
    KoInsets insets(0, 0, 0, 0);
228 229
    if(d->border)
        d->border->borderInsets(this, insets);
Thomas Zander's avatar
Thomas Zander committed
230

231 232
    QSizeF s( size() );
    return point.x() >= -insets.left && point.x() <= s.width() + insets.right &&
233
           point.y() >= -insets.top && point.y() <= s.height() + insets.bottom;
Thomas Zander's avatar
Thomas Zander committed
234 235
}

236
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
237
{
238
    QRectF bb( QPointF(0, 0), size() );
239 240 241 242 243
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
244
    return transformationMatrix(0).mapRect( bb );
Thomas Zander's avatar
Thomas Zander committed
245 246
}

247
QMatrix KoShape::transformationMatrix(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
248 249
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
250 251 252 253
    KoShapeContainer * container = d->parent;
    if( container ) {
        if( container->childClipped(this) )
            matrix = container->transformationMatrix(0);
Thomas Zander's avatar
Thomas Zander committed
254
        else {
255 256
            QSizeF containerSize = container->size();
            QPointF containerPos = container->absolutePosition() - QPointF( 0.5*containerSize.width(), 0.5*containerSize.height() );
Thomas Zander's avatar
Thomas Zander committed
257
            if(converter)
258
                containerPos = converter->documentToView(containerPos);
259
            matrix.translate( containerPos.x(), containerPos.y() );
Thomas Zander's avatar
Thomas Zander committed
260 261 262
        }
    }

263
    if(converter) {
264
        QPointF pos = d->localMatrix.map( QPoint() );
265 266
        QPointF trans = converter->documentToView( pos ) - pos;
        matrix.translate( trans.x(), trans.y() );
Thomas Zander's avatar
Thomas Zander committed
267
    }
268 269

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
270 271
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
void KoShape::applyTransformation( const QMatrix &matrix )
{
    QMatrix globalMatrix = transformationMatrix(0);
    // the transformation is relative to the global coordinate system
    // but we want to change the local matrix, so convert the matrix
    // to relate to the local coordinate system
    QMatrix transformMatrix = globalMatrix * matrix * globalMatrix.inverted();
    d->localMatrix = transformMatrix * d->localMatrix;
    notifyChanged();
}

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

289
bool KoShape::compareShapeZIndex(KoShape *s1, KoShape *s2) {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    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
306 307 308
}

void KoShape::setParent(KoShapeContainer *parent) {
309 310 311
    if(d->parent == parent)
        return;
    if(parent && dynamic_cast<KoShape*>(parent) != this) {
Thomas Zander's avatar
Thomas Zander committed
312
        d->parent = parent;
313 314
        parent->addChild(this);
    }
Thomas Zander's avatar
Thomas Zander committed
315
    else
Thomas Zander's avatar
Thomas Zander committed
316
        d->parent = 0;
317
    notifyChanged();
318
    d->shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
319 320 321 322
}

int KoShape::zIndex() const {
    if(parent()) // we can't be under our parent...
Thomas Zander's avatar
Thomas Zander committed
323 324
        return qMax(d->zIndex, parent()->zIndex());
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
325 326 327
}

void KoShape::repaint() const {
Thomas Zander's avatar
Thomas Zander committed
328
    if ( !d->shapeManagers.empty() )
329
    {
330 331
        QRectF rect(QPointF(0, 0), size() );
        if(d->border) {
332
            KoInsets insets;
333 334 335
            d->border->borderInsets(this, insets);
            rect.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
        }
336
        rect = transformationMatrix(0).mapRect(rect);
Thomas Zander's avatar
Thomas Zander committed
337
        foreach( KoShapeManager * manager, d->shapeManagers )
338
            manager->repaint( rect, this, true );
Thomas Zander's avatar
Thomas Zander committed
339 340 341
    }
}

342
void KoShape::repaint(const QRectF &shape) const {
Thomas Zander's avatar
Thomas Zander committed
343
    if ( !d->shapeManagers.empty() && isVisible() )
344
    {
345
        QRectF rect(transformationMatrix(0).mapRect(shape));
Thomas Zander's avatar
Thomas Zander committed
346
        foreach( KoShapeManager * manager, d->shapeManagers )
347 348 349 350
        {
            manager->repaint(rect);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
351 352 353 354
}

const QPainterPath KoShape::outline() const {
    QPainterPath path;
355
    path.addRect(QRectF( QPointF(0, 0), size() ));
Thomas Zander's avatar
Thomas Zander committed
356 357 358
    return path;
}

359 360 361 362
QPointF KoShape::absolutePosition(KoFlake::Position anchor) const {
    QPointF point;
    switch(anchor) {
        case KoFlake::TopLeftCorner: break;
363 364 365 366
        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;
367
    }
368
    return transformationMatrix(0).map(point);
Thomas Zander's avatar
Thomas Zander committed
369 370
}

371
void KoShape::setAbsolutePosition(QPointF newPosition, KoFlake::Position anchor) {
372 373
    QPointF currentAbsPosition = absolutePosition( anchor );
    QPointF translate = newPosition - currentAbsPosition;
Jan Hambrecht's avatar
Jan Hambrecht committed
374 375
    QMatrix translateMatrix;
    translateMatrix.translate( translate.x(), translate.y() );
376
    applyTransformation( translateMatrix );
377
    notifyChanged();
378
    d->shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
379 380
}

Thomas Zander's avatar
Thomas Zander committed
381
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
382 383 384
    d->pos = shape->position();
    d->size = shape->size();
    d->connectors.clear();
Thomas Zander's avatar
Thomas Zander committed
385 386
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
387 388 389 390
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
391
    d->localMatrix = shape->d->localMatrix;
Thomas Zander's avatar
Thomas Zander committed
392 393
}

Thomas Zander's avatar
Thomas Zander committed
394 395 396
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
397 398
}

Thomas Zander's avatar
Thomas Zander committed
399
void KoShape::notifyChanged()
400
{
Thomas Zander's avatar
Thomas Zander committed
401
    foreach( KoShapeManager * manager, d->shapeManagers )
402
    {
Thomas Zander's avatar
Thomas Zander committed
403
        manager->notifyShapeChanged( this );
404 405 406
    }
}

407
void KoShape::setUserData(KoShapeUserData *userData) {
408
    delete d->userData;
Thomas Zander's avatar
Thomas Zander committed
409
    d->userData = userData;
410 411 412
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
413
    return d->userData;
414 415
}

416
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
417
    // appdata is deleted by the application.
Thomas Zander's avatar
Thomas Zander committed
418
    d->appData = appData;
419 420 421
}

KoShapeApplicationData *KoShape::applicationData() const {
Thomas Zander's avatar
Thomas Zander committed
422
    return d->appData;
423 424
}

425
bool KoShape::hasTransparency() {
426
    if(d->backgroundBrush.style() == Qt::NoBrush)
Thomas Zander's avatar
Thomas Zander committed
427
        return true;
428
    return !d->backgroundBrush.isOpaque();
Thomas Zander's avatar
Thomas Zander committed
429 430
}

431 432
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
433 434
    if(d->border)
        d->border->borderInsets(this, answer);
435 436 437
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
438
double KoShape::scaleX() const {
439
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
440 441
}
double KoShape::scaleY() const {
442
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
443 444 445
}

double KoShape::rotation() const {
446 447 448 449 450
    // try to extract the rotation angle out of the local matrix 
    // 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
451
        return NAN;
452 453
    // 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
454
        return NAN;
455 456 457 458 459 460 461

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

    return angle;
Thomas Zander's avatar
Thomas Zander committed
462 463 464
}

double KoShape::shearX() const {
465
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
466 467 468
}

double KoShape::shearY() const {
469
    return 0.0;
Thomas Zander's avatar
Thomas Zander committed
470 471 472 473 474 475 476
}

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

QPointF KoShape::position() const {
477 478 479
    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
480 481 482 483 484 485 486 487 488 489 490
}

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 ) {
491
    d->backgroundBrush = brush;
Thomas Zander's avatar
Thomas Zander committed
492 493
}

494
QBrush KoShape::background() const {
495
    return d->backgroundBrush;
Thomas Zander's avatar
Thomas Zander committed
496 497 498
}

void KoShape::setZIndex(int zIndex) {
499
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 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
    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 );
}

563 564 565 566 567 568 569 570 571
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

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

const QMatrix& KoShape::matrix() const {
572
    return d->localMatrix;
573 574
}

575 576 577 578 579 580 581 582 583 584 585 586 587
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;
}
588

589
QString KoShape::name() const {
590 591 592 593 594 595
    return d->name;
}

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

597 598 599 600 601 602 603 604
void KoShape::deleteLater() {
    foreach(KoShapeManager *manager, d->shapeManagers)
        manager->remove(this);
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}


605
// loading & saving methods
606
void KoShape::saveOdfConnections(KoShapeSavingContext &context) const {
607
    // TODO  save "draw-glue-point" elements (9.2.19)
608
    Q_UNUSED( context );
609
}
610

611
QString KoShape::style( KoShapeSavingContext &context ) const
612 613
{
    KoGenStyle style;
614
    if ( context.isSet( KoShapeSavingContext::PresentationShape ) ) {
615
        style = KoGenStyle( KoGenStyle::StylePresentationAuto, "presentation" );
616 617
    }
    else {
618
        style = KoGenStyle( KoGenStyle::StyleGraphicAuto, "graphic" );
619 620 621 622 623 624 625 626 627 628 629 630 631 632
    }

    // 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;
633 634 635 636 637 638 639 640 641 642 643 644 645 646
        case Qt::SolidPattern:
        case Qt::Dense1Pattern:
        case Qt::Dense2Pattern:
        case Qt::Dense3Pattern:
        case Qt::Dense4Pattern:
        case Qt::Dense5Pattern:
        case Qt::Dense6Pattern:
        case Qt::Dense7Pattern:
        case Qt::HorPattern:
        case Qt::BDiagPattern:
        case Qt::VerPattern:
        case Qt::FDiagPattern:
        case Qt::CrossPattern:
        case Qt::DiagCrossPattern:
647
            KoOasisStyles::saveOasisFillStyle( style, context.mainStyles(), bg );
648
            break;
649 650
        default:    // TODO all the other ones.
        break;
651 652
    }

653
    if ( context.isSet( KoShapeSavingContext::AutoStyleInStyleXml ) ) {
654 655 656
        style.setAutoStyleInStylesDotXml( true );
    }

657
    return context.mainStyles().lookup( style, context.isSet( KoShapeSavingContext::PresentationShape ) ? "pr" : "gr" );
658 659
}

660 661 662 663 664 665 666 667 668
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
    if ( attributes & OdfMandatories ) {
        if ( element.hasAttributeNS( KoXmlNS::draw, "layer" ) ) {
            KoShapeLayer * layer = context.layer( element.attributeNS( KoXmlNS::draw, "layer" ) );
            if ( layer ) {
                setParent( layer );
            }
        }
669 670 671 672 673 674
        if ( element.hasAttributeNS( KoXmlNS::draw, "id" ) ) {
            QString id = element.attributeNS( KoXmlNS::draw, "id" );
            if ( !id.isNull() ) {
                context.addShapeId( this, id );
            }
        }
675
        if ( element.hasAttributeNS( KoXmlNS::draw, "z-index" ) ) {
Jan Hambrecht's avatar
Jan Hambrecht committed
676 677
            // TODO what do we do in case of copy/paste
            setZIndex( element.attributeNS( KoXmlNS::draw, "z-index" ).toInt() );
678 679 680 681 682
        }
        else {
            // TODO what do we do in the case the z-index is not there then the order in the doc
            // is the the order of the z-index
        }
683 684

        setBackground( loadOdfFill( element, context ) );
685
        setBorder( loadOdfStroke( element, context ) );
686
    }
687

688 689
    if ( attributes & OdfSize ) {
        QPointF pos;
Ariya Hidayat's avatar
Ariya Hidayat committed
690 691
        pos.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", QString() ) ) );
        pos.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", QString() ) ) );
692 693 694
        setPosition( pos );

        QSizeF size;
Ariya Hidayat's avatar
Ariya Hidayat committed
695 696
        size.setWidth( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", QString() ) ) );
        size.setHeight( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", QString() ) ) );
697
        resize( size );
698 699
    }

Jan Hambrecht's avatar
 
Jan Hambrecht committed
700 701 702 703 704 705 706
    if( attributes & OdfTransformation )
    {
        QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
        if( ! transform.isEmpty() )
            applyTransformation( parseOdfTransform( transform ) );
    }

707 708 709
    return true;
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
QBrush KoShape::loadOdfFill( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.koLoadingContext().styleStack();
    QString fill;
    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" );
        if( styleStack.hasProperty( KoXmlNS::draw, "fill" ) )
            fill = styleStack.property( KoXmlNS::draw, "fill" );
    }
    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" );
        if ( styleStack.hasProperty( KoXmlNS::presentation, "fill" ) )
            fill = styleStack.property( KoXmlNS::presentation, "fill" );
    }

    if ( fill == "solid" || fill == "hatch" )
        return KoOasisStyles::loadOasisFillStyle( styleStack, fill, context.koLoadingContext().oasisStyles() );

    return QBrush();
}

737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
KoShapeBorderModel * KoShape::loadOdfStroke( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.koLoadingContext().styleStack();
    QString stroke;
    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" );
        if( styleStack.hasProperty( KoXmlNS::draw, "stroke" ) )
            stroke = styleStack.property( KoXmlNS::draw, "stroke" );
    }
    else if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
        // fill the style stack with the shapes style
        context.koLoadingContext().fillStyleStack( element, KoXmlNS::presentation, "style-name", "presentation" );
        styleStack.setTypeProperties( "presentation" );
        if ( styleStack.hasProperty( KoXmlNS::presentation, "stroke" ) )
            stroke = styleStack.property( KoXmlNS::presentation, "stroke" );
    }

758
    if( stroke == "solid" || stroke == "dash" )
759
    {
760
        QPen pen = KoOasisStyles::loadOasisStrokeStyle( styleStack, stroke, context.koLoadingContext().oasisStyles() );
761

762 763 764 765 766
        KoLineBorder * border = new KoLineBorder();
        border->setLineWidth( pen.width() );
        border->setColor( pen.color() );
        border->setJoinStyle( pen.joinStyle() );
        border->setLineStyle( pen.style(), pen.dashPattern() );
767

768 769 770 771
        return border;
    }
    else
        return 0;
772 773
}

Jan Hambrecht's avatar
 
Jan Hambrecht committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
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
791
        if(subtransform[0].startsWith(';') || subtransform[0].startsWith(','))
Jan Hambrecht's avatar
 
Jan Hambrecht committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
            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;
}

847
void KoShape::saveOdfFrameAttributes(KoShapeSavingContext &context) const {
848
    saveOdfAttributes(context, FrameAttributes);
849
    context.addOption(KoShapeSavingContext::FrameOpened);
850 851
}

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

859
        if ( context.isSet( KoShapeSavingContext::DrawId ) )
860
        {
861
            context.xmlWriter().addAttribute( "draw:id", context.drawId( this ) );
862 863
        }

864
        if(d->parent && dynamic_cast<KoShapeLayer*> (d->parent))
865
            context.xmlWriter().addAttribute("draw:layer", d->parent->name());
866 867
    }

868 869
    // all items after this should not be written out when they have already be written in
    // a 'draw:frame' attribute.
870 871
    if(context.isSet(KoShapeSavingContext::FrameOpened)) {
        context.removeOption(KoShapeSavingContext::FrameOpened);
872
        return;
873 874
    }

875 876
    if(attributes & OdfSize) {
        QSizeF s( size() );
877 878 879 880
        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() );
881 882
    }

883
    if(attributes & OdfMandatories) {
884
        context.xmlWriter().addAttribute("draw:z-index", zIndex());
885 886
    }

887 888 889
    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.
890
        /*
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
        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() );
912
            context.xmlWriter().addAttribute( "draw:transform", m );
913 914 915 916 917 918 919 920 921 922 923 924 925 926
        }
        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 += ')';
            }

927
            context.xmlWriter().addAttribute( "draw:transform", transform );
928
        }
929 930 931
        */
        if( ! d->localMatrix.isIdentity() )
        {
Jan Hambrecht's avatar
 
Jan Hambrecht committed
932 933
            QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
                    .arg( d->localMatrix.m11() ).arg( d->localMatrix.m12() )
934 935 936 937
                    .arg( d->localMatrix.m21() ).arg( d->localMatrix.m22() )
                    .arg( d->localMatrix.dx() ) .arg( d->localMatrix.dy() );
            context.xmlWriter().addAttribute( "draw:transform", m );
        }
938 939 940 941 942 943 944 945 946 947 948 949 950
    }
}

// end loading & saving methods


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