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

   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"
#include "KoSelection.h"
24
#include "KoPointerEvent.h"
Thomas Zander's avatar
Thomas Zander committed
25 26
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
27
#include "KoShapeManager.h"
28
#include "KoShapeUserData.h"
29
#include "KoShapeApplicationData.h"
30
#include "KoViewConverter.h"
Thomas Zander's avatar
Thomas Zander committed
31 32

#include <QPainter>
33
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
34 35
#include <QPainterPath>

Thomas Zander's avatar
Thomas Zander committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class KoShapePrivate {
public:
    KoShapePrivate()
        //: backgroundBrush(Qt::NoBrush),
        //border(0),
        : 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),
        appData(0)
    {
    }

    ~KoShapePrivate() {
        delete userData;
        delete appData;
    }

    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;

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

Thomas Zander's avatar
Thomas Zander committed
93
KoShape::KoShape()
Thomas Zander's avatar
Thomas Zander committed
94 95
    : d(new KoShapePrivate())
    , m_border(0)
Thomas Zander's avatar
Thomas Zander committed
96 97 98 99 100 101
{
    recalcMatrix();
}

KoShape::~KoShape()
{
Thomas Zander's avatar
Thomas Zander committed
102 103
    delete d;
    d = 0;
Thomas Zander's avatar
Thomas Zander committed
104 105
}

106
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, bool selected) {
Thomas Zander's avatar
Thomas Zander committed
107 108 109 110 111 112 113
    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
114
        for ( int i = 0; i < d->connectors.size(); ++i )
Thomas Zander's avatar
Thomas Zander committed
115
        {
Thomas Zander's avatar
Thomas Zander committed
116
            QPointF p = converter.documentToView(d->connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
117 118 119 120 121 122 123 124
            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 ) );
        }
    }
}

void KoShape::scale( double sx, double sy )
{
Thomas Zander's avatar
Thomas Zander committed
125
    if(d->scaleX == sx && d->scaleY == sy)
Thomas Zander's avatar
Thomas Zander committed
126
        return;
Thomas Zander's avatar
Thomas Zander committed
127 128
    d->scaleX = sx;
    d->scaleY = sy;
Thomas Zander's avatar
Thomas Zander committed
129
    recalcMatrix();
130
    shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
131 132 133 134
}

void KoShape::rotate( double angle )
{
Thomas Zander's avatar
Thomas Zander committed
135
    if(d->angle == angle)
Thomas Zander's avatar
Thomas Zander committed
136
        return;
Thomas Zander's avatar
Thomas Zander committed
137 138 139
    d->angle = angle;
    while(d->angle >= 360) d->angle -= 360;
    while(d->angle <= -360) d->angle += 360;
Thomas Zander's avatar
Thomas Zander committed
140
    recalcMatrix();
141
    shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
142 143 144 145
}

void KoShape::shear( double sx, double sy )
{
Thomas Zander's avatar
Thomas Zander committed
146
    if(d->shearX == sx && d->shearY == sy)
Thomas Zander's avatar
Thomas Zander committed
147
        return;
Thomas Zander's avatar
Thomas Zander committed
148 149
    d->shearX = sx;
    d->shearY = sy;
Thomas Zander's avatar
Thomas Zander committed
150
    recalcMatrix();
151
    shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
152 153
}

154
void KoShape::resize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
155
{
156 157
    QSizeF s( size() );
    if(s == newSize)
Thomas Zander's avatar
Thomas Zander committed
158
        return;
159

160 161
    double fx = newSize.width() / s.width();
    double fy = newSize.height() / s.height();
162

Thomas Zander's avatar
Thomas Zander committed
163
    d->size = newSize;
164

Thomas Zander's avatar
Thomas Zander committed
165
    for ( int i = 0; i < d->connectors.size(); ++i )
166
    {
Thomas Zander's avatar
Thomas Zander committed
167
        QPointF &point = d->connectors[i];
168 169 170
        point.setX(point.x() * fx);
        point.setY(point.y() * fy);
    }
171 172
    recalcMatrix();
    shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
173 174 175 176
}

void KoShape::setPosition( const QPointF &position )
{
Thomas Zander's avatar
Thomas Zander committed
177
    if(d->pos == position)
Thomas Zander's avatar
Thomas Zander committed
178
        return;
Thomas Zander's avatar
Thomas Zander committed
179
    d->pos = position;
Thomas Zander's avatar
Thomas Zander committed
180
    recalcMatrix();
181
    shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
182 183 184 185
}

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

    QPointF point( position * m_invMatrix );
190
    KoInsets insets(0, 0, 0, 0);
Thomas Zander's avatar
Thomas Zander committed
191
    if(m_border)
192
        m_border->borderInsets(this, insets);
Thomas Zander's avatar
Thomas Zander committed
193

194 195 196
    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
197 198
}

199
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
200
{
201
    QRectF bb( QPointF(0, 0), size() );
Thomas Zander's avatar
Thomas Zander committed
202
    return d->matrix.mapRect( bb );
Thomas Zander's avatar
Thomas Zander committed
203 204 205 206
}

void KoShape::recalcMatrix()
{
Thomas Zander's avatar
Thomas Zander committed
207 208
    d->matrix = transformationMatrix(0);
    m_invMatrix = d->matrix.inverted();
Thomas Zander's avatar
Thomas Zander committed
209
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
210 211
}

212
QMatrix KoShape::transformationMatrix(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
213 214 215
    QMatrix matrix;
    QRectF zoomedRect = QRectF(position(), size());
    if(converter)
216
        zoomedRect = converter->documentToView(zoomedRect);
Thomas Zander's avatar
Thomas Zander committed
217 218 219
    matrix.translate( zoomedRect.x(), zoomedRect.y() );

    // apply parents matrix to inherit any transformations done there.
Thomas Zander's avatar
Thomas Zander committed
220
    KoShapeContainer *container = d->parent;
Thomas Zander's avatar
Thomas Zander committed
221 222 223 224 225 226 227
    KoShape const *child = this;
    while(container) {
        if(container->childClipped(child))
            matrix *= container->transformationMatrix(0);
        else {
            QPointF containerPos =container->position();
            if(converter)
228
                containerPos = converter->documentToView(containerPos);
Thomas Zander's avatar
Thomas Zander committed
229 230 231 232 233 234
            matrix.translate(containerPos.x(), containerPos.y());
        }
        container = dynamic_cast<KoShapeContainer*>(container->parent());
        child = child->parent();
    }

Thomas Zander's avatar
Thomas Zander committed
235
    if ( d->angle != 0 )
Thomas Zander's avatar
Thomas Zander committed
236
    {
Thomas Zander's avatar
Thomas Zander committed
237 238 239 240 241
        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
242
    }
Thomas Zander's avatar
Thomas Zander committed
243 244
    matrix.shear( d->shearX, d->shearY );
    matrix.scale( d->scaleX, d->scaleY );
Thomas Zander's avatar
Thomas Zander committed
245 246 247 248 249 250 251 252 253 254
    return matrix;
}


bool KoShape::compareShapeZIndex(KoShape *g1, KoShape *g2) {
    return g1->zIndex() < g2->zIndex();
}

void KoShape::setParent(KoShapeContainer *parent) {
    if(dynamic_cast<KoShape*>(parent) != this)
Thomas Zander's avatar
Thomas Zander committed
255
        d->parent = parent;
Thomas Zander's avatar
Thomas Zander committed
256
    else
Thomas Zander's avatar
Thomas Zander committed
257
        d->parent = 0;
Thomas Zander's avatar
Thomas Zander committed
258
    recalcMatrix();
259
    shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
260 261 262 263
}

int KoShape::zIndex() const {
    if(parent()) // we can't be under our parent...
Thomas Zander's avatar
Thomas Zander committed
264 265
        return qMax(d->zIndex, parent()->zIndex());
    return d->zIndex;
Thomas Zander's avatar
Thomas Zander committed
266 267 268
}

void KoShape::repaint() const {
Thomas Zander's avatar
Thomas Zander committed
269
    if ( !d->shapeManagers.empty() )
270
    {
Thomas Zander's avatar
Thomas Zander committed
271
        foreach( KoShapeManager * manager, d->shapeManagers )
272
        {
273
            QRectF rect(QPointF(0, 0), size() );
274 275 276 277 278
            if(m_border) {
                KoInsets insets(0, 0, 0, 0);
                m_border->borderInsets(this, insets);
                rect.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
            }
Thomas Zander's avatar
Thomas Zander committed
279
            rect = d->matrix.mapRect(rect);
280 281
            manager->repaint( rect, this, true );
        }
Thomas Zander's avatar
Thomas Zander committed
282 283 284
    }
}

285
void KoShape::repaint(const QRectF &shape) const {
Thomas Zander's avatar
Thomas Zander committed
286
    if ( !d->shapeManagers.empty() && isVisible() )
287
    {
Thomas Zander's avatar
Thomas Zander committed
288 289
        QRectF rect(d->matrix.mapRect(shape));
        foreach( KoShapeManager * manager, d->shapeManagers )
290 291 292 293
        {
            manager->repaint(rect);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
294 295 296 297 298 299 300 301 302
}

void KoShape::repaint(double x, double y, double width, double height) const {
    QRectF rect(x, y, width, height);
    repaint(rect);
}

const QPainterPath KoShape::outline() const {
    QPainterPath path;
303
    path.addRect(QRectF( QPointF(0, 0), size() ));
Thomas Zander's avatar
Thomas Zander committed
304 305 306 307
    return path;
}

QPointF KoShape::absolutePosition() const {
Thomas Zander's avatar
Thomas Zander committed
308
    return d->matrix.map(QPointF(size().width() / 2.0 , size().height() / 2.0));
Thomas Zander's avatar
Thomas Zander committed
309 310 311 312 313 314
}

void KoShape::setAbsolutePosition(QPointF newPosition) {
    QPointF zero(0, 0);
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
Thomas Zander's avatar
Thomas Zander committed
315
    KoShapeContainer *container = d->parent;
Thomas Zander's avatar
Thomas Zander committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    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
332
    if ( d->angle != 0 )
333
    {
Thomas Zander's avatar
Thomas Zander committed
334 335 336 337 338
        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 );
339
    }
Thomas Zander's avatar
Thomas Zander committed
340 341
    matrix.shear( d->shearX, d->shearY );
    matrix.scale( d->scaleX, d->scaleY );
Thomas Zander's avatar
Thomas Zander committed
342 343

    QPointF vector2 = matrix.map( QPointF(size().width() / 2.0, size().height() / 2.0) );
344
    //kDebug() << "vector1: " << vector1 << ", vector2: " << vector2 << endl;
Thomas Zander's avatar
Thomas Zander committed
345 346 347 348

    setPosition(newPosition + vector1 - vector2);
}

Thomas Zander's avatar
Thomas Zander committed
349
void KoShape::copySettings(const KoShape *shape) {
Thomas Zander's avatar
Thomas Zander committed
350 351 352 353 354 355 356 357
    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
358 359
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
Thomas Zander's avatar
Thomas Zander committed
360 361 362 363
    d->zIndex = shape->zIndex();
    d->visible = shape->isVisible();
    d->locked = shape->isLocked();
    d->keepAspect = shape->keepAspectRatio();
Thomas Zander's avatar
Thomas Zander committed
364 365
}

Thomas Zander's avatar
Thomas Zander committed
366 367 368
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
369 370
}

Thomas Zander's avatar
Thomas Zander committed
371
void KoShape::notifyChanged()
372
{
Thomas Zander's avatar
Thomas Zander committed
373
    foreach( KoShapeManager * manager, d->shapeManagers )
374
    {
Thomas Zander's avatar
Thomas Zander committed
375
        manager->notifyShapeChanged( this );
376 377 378
    }
}

379
void KoShape::setUserData(KoShapeUserData *userData) {
Thomas Zander's avatar
Thomas Zander committed
380 381 382
    if(d->userData)
        delete d->userData;
    d->userData = userData;
383 384 385
}

KoShapeUserData *KoShape::userData() const {
Thomas Zander's avatar
Thomas Zander committed
386
    return d->userData;
387 388
}

389
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
Thomas Zander's avatar
Thomas Zander committed
390 391
    delete d->appData;
    d->appData = appData;
392 393 394
}

KoShapeApplicationData *KoShape::applicationData() const {
Thomas Zander's avatar
Thomas Zander committed
395
    return d->appData;
396 397
}

398
bool KoShape::hasTransparency() {
Thomas Zander's avatar
Thomas Zander committed
399 400 401 402 403
    if(m_backgroundBrush.style() == Qt::NoBrush)
        return true;
    return !m_backgroundBrush.isOpaque();
}

404 405 406 407 408 409 410
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
    if(m_border)
        m_border->borderInsets(this, answer);
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 506 507 508 509 510 511 512 513 514 515 516 517
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 ) {
    m_backgroundBrush = brush;
}

const QBrush& KoShape::background () {
    return m_backgroundBrush;
}

void KoShape::setZIndex(int zIndex) {
    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 );
}

Thomas Zander's avatar
Thomas Zander committed
518 519 520 521 522 523
// static
void KoShape::applyConversion(QPainter &painter, const KoViewConverter &converter) {
    double zoomX, zoomY;
    converter.zoom(&zoomX, &zoomY);
    painter.scale(zoomX, zoomY);
}
524