KoShape.cpp 33.6 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1
/* This file is part of the KDE project
Thomas Zander's avatar
Thomas Zander committed
2
   Copyright (C) 2006 Casper Boemann Rasmussen <cbr@boemann.dk>
3
   Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
4
   Copyright (C) 2006-2008 Thorsten Zachmann <zachmann@kde.org>
5
   Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
Thomas Zander's avatar
Thomas Zander committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoShape.h"
#include "KoShapeContainer.h"
25
#include "KoShapeLayer.h"
26
#include "KoShapeContainerModel.h"
Thomas Zander's avatar
Thomas Zander committed
27
#include "KoSelection.h"
28
#include "KoPointerEvent.h"
Thomas Zander's avatar
Thomas Zander committed
29 30
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
31
#include "KoShapeManager.h"
32
#include "KoShapeUserData.h"
33
#include "KoShapeApplicationData.h"
34
#include "KoShapeSavingContext.h"
35
#include "KoShapeLoadingContext.h"
36
#include "KoViewConverter.h"
37
#include "KoLineBorder.h"
38
#include "ShapeDeleter_p.h"
39
#include "KoShapeStyleWriter.h"
40
#include "KoShapeShadow.h"
Thomas Zander's avatar
Thomas Zander committed
41

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

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

58 59
#include <kdebug.h>

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

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

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

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

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

    QVector<QPointF> connectors; // in pt

    int zIndex;
    KoShapeContainer *parent;

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

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

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

142
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
143 144 145 146
}

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

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

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

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

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

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

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

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

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

Thomas Zander's avatar
Thomas Zander committed
217
    d->size = newSize;
218

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

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

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

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

241
    QPointF point = absoluteTransformation(0).inverted().map( position );
242
    QRectF bb( QPointF(), size() );
243
    if(d->border)
244 245
    {
        KoInsets insets;
246
        d->border->borderInsets(this, insets);
247 248 249 250 251 252 253 254
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
    if( bb.contains( point ) )
        return true;

    // if there is no shadow we can as well just leave
    if( ! d->shadow )
        return false;
Thomas Zander's avatar
Thomas Zander committed
255

256 257 258 259 260
    // the shadow has an offset to the shape, so we simply
    // check if the position minus the shadow offset hits the shape
    point = absoluteTransformation(0).inverted().map( position-d->shadow->offset() );

    return bb.contains( point );
Thomas Zander's avatar
Thomas Zander committed
261 262
}

263
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
264
{
265
    QRectF bb( QPointF(0, 0), size() );
266 267 268 269 270
    if(d->border) {
        KoInsets insets;
        d->border->borderInsets(this, insets);
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
271 272 273 274 275 276 277 278
    bb = absoluteTransformation(0).mapRect( bb );
    if( d->shadow )
    {
        KoInsets insets;
        d->shadow->insets( this, insets );
        bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
    }
    return bb;
Thomas Zander's avatar
Thomas Zander committed
279 280
}

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

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

    return d->localMatrix * matrix;
Thomas Zander's avatar
Thomas Zander committed
304 305
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thomas Zander's avatar
Thomas Zander committed
481
double KoShape::rotation() const {
482
    // try to extract the rotation angle out of the local matrix
483 484 485 486
    // 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
487
        return NAN;
488 489
    // 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
490
        return NAN;
491 492

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

    return angle;
Thomas Zander's avatar
Thomas Zander committed
498 499 500 501 502 503 504
}

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

QPointF KoShape::position() const {
505 506 507
    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
508 509 510
}

void KoShape::addConnectionPoint( const QPointF &point ) {
511 512 513
    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
514 515
}

516
QList<QPointF> KoShape::connectionPoints() const {
517 518 519 520 521 522 523
    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;
524 525
}

Thomas Zander's avatar
Thomas Zander committed
526
void KoShape::setBackground ( const QBrush & brush ) {
527
    d->backgroundBrush = brush;
528 529
    d->shapeChanged(BackgroundChanged);
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
530 531
}

532
QBrush KoShape::background() const {
533
    return d->backgroundBrush;
Thomas Zander's avatar
Thomas Zander committed
534 535 536
}

void KoShape::setZIndex(int zIndex) {
537
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
538 539 540 541 542 543 544 545 546 547 548
    d->zIndex = zIndex;
}

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

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

549 550 551 552 553 554 555
void KoShape::setPrintable(bool on)
{
    d->printable = on;
}
    
bool KoShape::isPrintable() const
{
556 557 558 559
    if (d->visible)
        return d->printable;
    else
        return false;
560 561
}

Thomas Zander's avatar
Thomas Zander committed
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 604 605 606 607 608 609 610 611 612 613
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 );
}

614 615 616 617 618
KoShapeBorderModel *KoShape::border() const {
    return d->border;
}

void KoShape::setBorder(KoShapeBorderModel *border) {
619 620
    if(d->border)
        d->border->removeUser();
621
    d->border = border;
622 623
    if(d->border)
        d->border->addUser();
624 625
    d->shapeChanged(BorderChanged);
    notifyChanged();
626 627
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
void KoShape::setShadow( KoShapeShadow * shadow )
{
    if( d->shadow )
        d->shadow->removeUser();
    d->shadow = shadow;
    if( d->shadow )
        d->shadow->addUser();
    d->shapeChanged(ShadowChanged);
    notifyChanged();
}

KoShapeShadow * KoShape::shadow() const
{
    return d->shadow;
}

644
const QMatrix& KoShape::matrix() const {
645
    return d->localMatrix;
646 647
}

648
void KoShape::removeConnectionPoint( int index )
649 650 651 652 653
{
    if( index < d->connectors.count() )
        d->connectors.remove( index );
}

654
QString KoShape::name() const {
655 656 657 658 659 660
    return d->name;
}

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

662 663 664 665 666 667 668
void KoShape::deleteLater() {
    foreach(KoShapeManager *manager, d->shapeManagers)
        manager->remove(this);
    d->shapeManagers.clear();
    new ShapeDeleter(this);
}

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
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;
}
685

686
// loading & saving methods
687
void KoShape::saveOdfConnections(KoShapeSavingContext &context) const {
688
    // TODO  save "draw-glue-point" elements (9.2.19)
689
    Q_UNUSED( context );
690
}
691

692
QString KoShape::saveStyle( KoGenStyle &style, KoShapeSavingContext &context ) const
693 694 695 696 697 698 699
{
    // and fill the style
    KoShapeBorderModel * b = border();
    if ( b )
    {
        b->fillStyle( style, context );
    }
700 701 702
    KoShapeShadow * s = shadow();
    if( s )
        s->fillStyle( style, context );
703

704
    KoShapeStyleWriter styleWriter( context );
705

706
    return styleWriter.addFillStyle( style, background() );
707 708
}

709 710
void KoShape::loadStyle( const KoXmlElement & element, KoShapeLoadingContext &context )
{
711
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
712 713 714 715 716
    styleStack.save();

    // fill the style stack with the shapes style
    if( element.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
    {
717
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::draw, "style-name", "graphic" );
718 719 720 721
        styleStack.setTypeProperties( "graphic" );
    }
    else if( element.hasAttributeNS( KoXmlNS::presentation, "style-name" ) )
    {
722
        context.odfLoadingContext().fillStyleStack( element, KoXmlNS::presentation, "style-name", "presentation" );
723
        styleStack.setTypeProperties( "graphic" );
724 725
    }

726 727
    setBackground( loadOdfFill( element, context ) );
    setBorder( loadOdfStroke( element, context ) );
728
    setShadow( loadOdfShadow( element, context ) );
729 730

    styleStack.restore();
731 732
}

733 734
bool KoShape::loadOdfAttributes( const KoXmlElement & element, KoShapeLoadingContext &context, int attributes )
{
735 736 737 738 739 740 741 742 743
    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() ) ) );
744
        setSize( size );
745 746
    }

747 748 749 750 751 752 753
    if ( attributes & OdfMandatories ) {
        if ( element.hasAttributeNS( KoXmlNS::draw, "layer" ) ) {
            KoShapeLayer * layer = context.layer( element.attributeNS( KoXmlNS::draw, "layer" ) );
            if ( layer ) {
                setParent( layer );
            }
        }
754 755 756 757 758 759
        if ( element.hasAttributeNS( KoXmlNS::draw, "id" ) ) {
            QString id = element.attributeNS( KoXmlNS::draw, "id" );
            if ( !id.isNull() ) {
                context.addShapeId( this, id );
            }
        }
760
        if ( element.hasAttributeNS( KoXmlNS::draw, "z-index" ) ) {
761
            context.addShapeZIndex( this, element.attributeNS( KoXmlNS::draw, "z-index" ).toInt() );
762
        }
763
        setZIndex( context.zIndex() );
764

765
        if( element.hasAttributeNS( KoXmlNS::draw, "name" ) ) {
Jan Hambrecht's avatar
Jan Hambrecht committed
766
            setName( element.attributeNS( KoXmlNS::draw, "name" ) );
767
        }
Jan Hambrecht's avatar
Jan Hambrecht committed
768

769
        loadStyle( element, context );
770
    }
771

Jan Hambrecht's avatar
 
Jan Hambrecht committed
772 773 774 775
    if( attributes & OdfTransformation )
    {
        QString transform = element.attributeNS( KoXmlNS::draw, "transform", QString() );
        if( ! transform.isEmpty() )
776
            applyAbsoluteTransformation( parseOdfTransform( transform ) );
Jan Hambrecht's avatar
 
Jan Hambrecht committed
777 778
    }

779 780 781
    if ( attributes & OdfAdditionalAttributes ) {
    }

782 783 784
    return true;
}

785
QString KoShape::getStyleProperty( const char *property, const KoXmlElement & element, KoShapeLoadingContext & context )
786
{
787
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
788
    QString value;
789

790 791
    if ( styleStack.hasProperty( KoXmlNS::draw, property ) ) {
        value = styleStack.property( KoXmlNS::draw, property );
792 793
    }

794 795 796 797 798
    return value;
}

QBrush KoShape::loadOdfFill( const KoXmlElement & element, KoShapeLoadingContext & context )
{
799
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
800
    QString fill = getStyleProperty( "fill", element, context );
801
    if ( fill == "solid" || fill == "hatch" )
802
        return KoOdfGraphicStyles::loadOasisFillStyle( styleStack, fill, context.odfLoadingContext().stylesReader() );
803
    else if( fill == "gradient" )
804
        return KoOdfGraphicStyles::loadOasisGradientStyle( styleStack, context.odfLoadingContext().stylesReader(), size() );
805
    else if( fill == "bitmap" )
806
        return KoOdfGraphicStyles::loadOasisPatternStyle( styleStack, context.odfLoadingContext(), size() );
807 808 809 810

    return QBrush();
}

811 812
KoShapeBorderModel * KoShape::loadOdfStroke( const KoXmlElement & element, KoShapeLoadingContext & context )
{
813
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
814
    QString stroke = getStyleProperty( "stroke", element, context );
815
    if( stroke == "solid" || stroke == "dash" )
816
    {
817
        QPen pen = KoOdfGraphicStyles::loadOasisStrokeStyle( styleStack, stroke, context.odfLoadingContext().stylesReader() );
818

819
        KoLineBorder * border = new KoLineBorder();
820
        border->setLineWidth( pen.widthF() );
821 822 823
        border->setColor( pen.color() );
        border->setJoinStyle( pen.joinStyle() );
        border->setLineStyle( pen.style(), pen.dashPattern() );
824

825 826 827 828
        return border;
    }
    else
        return 0;
829 830
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
KoShapeShadow * KoShape::loadOdfShadow( const KoXmlElement & element, KoShapeLoadingContext & context )
{
    KoStyleStack &styleStack = context.odfLoadingContext().styleStack();
    QString stroke = getStyleProperty( "shadow", element, context );
    if( stroke == "visible" || stroke == "hidden" )
    {
        KoShapeShadow * shadow = new KoShapeShadow();
        QColor shadowColor( styleStack.property( KoXmlNS::draw, "shadow-color" ) );
        qreal offsetX = KoUnit::parseValue( styleStack.property( KoXmlNS::draw, "shadow-offset-x" ) );
        qreal offsetY = KoUnit::parseValue( styleStack.property( KoXmlNS::draw, "shadow-offset-y" ) );
        shadow->setOffset( QPointF( offsetX, offsetY ) );

        QString opacity = styleStack.property( KoXmlNS::draw, "shadow-opacity" );
        if( ! opacity.isEmpty() && opacity.right( 1 ) == "%" )
            shadowColor.setAlphaF( opacity.left( opacity.length()-1 ).toFloat() / 100.0 );
        shadow->setColor( shadowColor );

        return shadow;
    }
    return 0;
}

Jan Hambrecht's avatar
 
Jan Hambrecht committed
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
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
870
        if(subtransform[0].startsWith(';') || subtransform[0].startsWith(','))
Jan Hambrecht's avatar
 
Jan Hambrecht committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
            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;
}

926
void KoShape::saveOdfFrameAttributes(KoShapeSavingContext &context) const {
927
    saveOdfAttributes( context, OdfAllAttributes );
928
    context.addOption(KoShapeSavingContext::FrameOpened);
929 930
}

931
void KoShape::saveOdfAttributes(KoShapeSavingContext &context, int attributes) const {
932
    if(attributes & OdfMandatories) {
933
        KoGenStyle style;
934
        // all items that should be written to 'draw:frame' and any other 'draw:' object that inherits this shape
935 936 937 938 939 940 941 942
        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 ) );
        }
943

944
        if ( context.isSet( KoShapeSavingContext::DrawId ) )
945
        {
946
            context.xmlWriter().addAttribute( "draw:id", context.drawId( this ) );
947 948
        }

Jan Hambrecht's avatar
Jan Hambrecht committed
949 950 951
        if( ! name().isEmpty() )
            context.xmlWriter().addAttribute( "draw:name", name() );

952
        if(d->parent && dynamic_cast<KoShapeLayer*> (d->parent))
953
            context.xmlWriter().addAttribute("draw:layer", d->parent->name());
954 955
    }

956 957
    // all items after this should not be written out when they have already be written in
    // a 'draw:frame' attribute.
958 959
    if(context.isSet(KoShapeSavingContext::FrameOpened)) {
        context.removeOption(KoShapeSavingContext::FrameOpened);
960
        return;
961 962
    }

963 964
    if(attributes & OdfSize) {
        QSizeF s( size() );
965 966
        context.xmlWriter().addAttributePt( "svg:width", s.width() );
        context.xmlWriter().addAttributePt( "svg:height", s.height() );
Jan Hambrecht's avatar
Jan Hambrecht committed
967 968 969
        // the position is hidden in the transformation
        context.xmlWriter().addAttributePt( "svg:x", 0.0 );
        context.xmlWriter().addAttributePt( "svg:y", 0.0 );
970 971
    }

972 973 974
    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.
975
        /*
976 977 978 979 980
        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)) {
981
            QMatrix matrix; // can't use absoluteTransformation() as that includes transformation of the container as well.
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
            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() );
997
            context.xmlWriter().addAttribute( "draw:transform", m );
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        }
        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 += ')';
            }

1012
            context.xmlWriter().addAttribute( "draw:transform", transform );
1013
        }
1014
        */
1015
        QMatrix matrix = absoluteTransformation(0);
1016
        if( ! matrix.isIdentity() )
1017
        {
Jan Hambrecht's avatar
 
Jan Hambrecht committed
1018
            QString m = QString( "matrix(%1 %2 %3 %4 %5pt %6pt)" )
1019 1020 1021
                    .arg( matrix.m11() ).arg( matrix.m12() )
                    .arg( matrix.m21() ).arg( matrix.m22() )
                    .arg( matrix.dx() ) .arg( matrix.dy() );
1022 1023
            context.xmlWriter().addAttribute( "draw:transform", m );
        }
1024
    }
1025 1026 1027 1028 1029 1030 1031

    if ( attributes & OdfAdditionalAttributes ) {
        QMap<QByteArray, QString>::const_iterator it( d->additionalAttributes.constBegin() );
        for ( ; it != d->additionalAttributes.constEnd(); ++it ) {
            context.xmlWriter().addAttribute( it.key(), it.value() );
        }
    }
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
}

// end loading & saving methods


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

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
QPointF KoShape::shapeToDocument( const QPointF &point ) const
{
    return absoluteTransformation(0).map( point );
}

QRectF KoShape::shapeToDocument( const QRectF &rect ) const 
{
    return absoluteTransformation(0).mapRect( rect );
}

QPointF KoShape::documentToShape( const QPointF &point ) const
{
    return absoluteTransformation(0).inverted().map( point );
}

QRectF KoShape::documentToShape( const QRectF &rect ) const
{
    return absoluteTransformation(0).inverted().mapRect( rect );
}


1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
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 );
}
1083 1084 1085 1086 1087

KoSnapData KoShape::snapData() const
{
    return KoSnapData();
}
1088

1089
void KoShape::setAddtionalAttribute( const char * name, const QString & value )