KoShape.cpp 10.6 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* This file is part of the KDE project
   Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
   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"
25
#include "KoPointerEvent.h"
Thomas Zander's avatar
Thomas Zander committed
26 27
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
28
#include "KoShapeManager.h"
29
#include "KoShapeUserData.h"
30
#include "KoShapeApplicationData.h"
31
#include "KoViewConverter.h"
Thomas Zander's avatar
Thomas Zander committed
32 33

#include <QPainter>
34
#include <QVariant>
Thomas Zander's avatar
Thomas Zander committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <QPainterPath>

KoShape::KoShape()
: m_backgroundBrush(Qt::NoBrush)
, m_border(0)
, m_scaleX( 1 )
, m_scaleY( 1 )
, m_angle( 0 )
, m_shearX( 0 )
, m_shearY( 0 )
, m_size( 50, 50 )
, m_pos( 0, 0 )
, m_zIndex( 0 )
, m_parent( 0 )
, m_visible( true )
, m_locked( false )
, m_keepAspect( false )
52
, m_selectable( true )
Thomas Zander's avatar
Thomas Zander committed
53
, m_detectCollision( false )
54
, m_userData(0)
55
, m_appData(0)
Thomas Zander's avatar
Thomas Zander committed
56 57 58 59 60 61
{
    recalcMatrix();
}

KoShape::~KoShape()
{
62
    delete m_userData;
63
    delete m_appData;
Thomas Zander's avatar
Thomas Zander committed
64 65
}

66
void KoShape::paintDecorations(QPainter &painter, const KoViewConverter &converter, bool selected) {
Thomas Zander's avatar
Thomas Zander committed
67 68 69 70 71 72 73 74 75
    if ( selected )
    {
        // draw connectors
        QPen pen( Qt::blue );
        pen.setWidth( 0 );
        painter.setPen( pen );
        painter.setBrush( Qt::NoBrush );
        for ( int i = 0; i < m_connectors.size(); ++i )
        {
76
            QPointF p = converter.documentToView(m_connectors[ i ]);
Thomas Zander's avatar
Thomas Zander committed
77 78 79 80 81 82 83 84 85 86 87 88 89
            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 )
{
    if(m_scaleX == sx && m_scaleY == sy)
        return;
    m_scaleX = sx;
    m_scaleY = sy;
    recalcMatrix();
90
    shapeChanged(ScaleChanged);
Thomas Zander's avatar
Thomas Zander committed
91 92 93 94 95 96 97 98 99 100
}

void KoShape::rotate( double angle )
{
    if(m_angle == angle)
        return;
    m_angle = angle;
    while(m_angle >= 360) m_angle -= 360;
    while(m_angle <= -360) m_angle += 360;
    recalcMatrix();
101
    shapeChanged(RotationChanged);
Thomas Zander's avatar
Thomas Zander committed
102 103 104 105 106 107 108 109 110
}

void KoShape::shear( double sx, double sy )
{
    if(m_shearX == sx && m_shearY == sy)
        return;
    m_shearX = sx;
    m_shearY = sy;
    recalcMatrix();
111
    shapeChanged(ShearChanged);
Thomas Zander's avatar
Thomas Zander committed
112 113
}

114
void KoShape::resize( const QSizeF &newSize )
Thomas Zander's avatar
Thomas Zander committed
115
{
116 117
    QSizeF s( size() );
    if(s == newSize)
Thomas Zander's avatar
Thomas Zander committed
118
        return;
119

120 121
    double fx = newSize.width() / s.width();
    double fy = newSize.height() / s.height();
122

123
    m_size = newSize;
124 125 126 127 128 129 130

    for ( int i = 0; i < m_connectors.size(); ++i )
    {
        QPointF &point = m_connectors[i];
        point.setX(point.x() * fx);
        point.setY(point.y() * fy);
    }
131 132
    recalcMatrix();
    shapeChanged(SizeChanged);
Thomas Zander's avatar
Thomas Zander committed
133 134 135 136 137 138 139 140
}

void KoShape::setPosition( const QPointF &position )
{
    if(m_pos == position)
        return;
    m_pos = position;
    recalcMatrix();
141
    shapeChanged(PositionChanged);
Thomas Zander's avatar
Thomas Zander committed
142 143 144 145 146 147 148 149
}

bool KoShape::hitTest( const QPointF &position ) const
{
    if(m_parent && m_parent->childClipped(this) && !m_parent->hitTest(position))
        return false;

    QPointF point( position * m_invMatrix );
150
    KoInsets insets(0, 0, 0, 0);
Thomas Zander's avatar
Thomas Zander committed
151
    if(m_border)
152
        m_border->borderInsets(this, insets);
Thomas Zander's avatar
Thomas Zander committed
153

154 155 156
    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
157 158
}

159
QRectF KoShape::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
160
{
161
    QRectF bb( QPointF(0, 0), size() );
Thomas Zander's avatar
Thomas Zander committed
162 163 164 165 166 167 168
    return m_matrix.mapRect( bb );
}

void KoShape::recalcMatrix()
{
    m_matrix = transformationMatrix(0);
    m_invMatrix = m_matrix.inverted();
Thomas Zander's avatar
Thomas Zander committed
169
    notifyChanged();
Thomas Zander's avatar
Thomas Zander committed
170 171
}

172
QMatrix KoShape::transformationMatrix(const KoViewConverter *converter) const {
Thomas Zander's avatar
Thomas Zander committed
173 174 175
    QMatrix matrix;
    QRectF zoomedRect = QRectF(position(), size());
    if(converter)
176
        zoomedRect = converter->documentToView(zoomedRect);
Thomas Zander's avatar
Thomas Zander committed
177 178 179 180 181 182 183 184 185 186 187
    matrix.translate( zoomedRect.x(), zoomedRect.y() );

    // apply parents matrix to inherit any transformations done there.
    KoShapeContainer *container = m_parent;
    KoShape const *child = this;
    while(container) {
        if(container->childClipped(child))
            matrix *= container->transformationMatrix(0);
        else {
            QPointF containerPos =container->position();
            if(converter)
188
                containerPos = converter->documentToView(containerPos);
Thomas Zander's avatar
Thomas Zander committed
189 190 191 192 193 194 195 196 197
            matrix.translate(containerPos.x(), containerPos.y());
        }
        container = dynamic_cast<KoShapeContainer*>(container->parent());
        child = child->parent();
    }

    if ( m_angle != 0 )
    {
        matrix.translate( zoomedRect.width() / 2.0 * m_scaleX, zoomedRect.height() / 2.0 * m_scaleY );
198
        matrix.translate( zoomedRect.height() / 2.0 * m_shearX, zoomedRect.width() / 2.0 * m_shearY );
Thomas Zander's avatar
Thomas Zander committed
199
        matrix.rotate( m_angle );
200 201
        matrix.translate( -zoomedRect.width() / 2.0 * m_scaleX, -zoomedRect.height() / 2.0 * m_scaleY );
        matrix.translate( -zoomedRect.height() / 2.0 * m_shearX, -zoomedRect.width() / 2.0 * m_shearY );
Thomas Zander's avatar
Thomas Zander committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }
    matrix.shear( m_shearX, m_shearY );
    matrix.scale( m_scaleX, m_scaleY );
    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)
        m_parent = parent;
    else
        m_parent = 0;
    recalcMatrix();
219
    shapeChanged(ParentChanged);
Thomas Zander's avatar
Thomas Zander committed
220 221 222 223 224 225 226 227 228
}

int KoShape::zIndex() const {
    if(parent()) // we can't be under our parent...
        return qMax(m_zIndex, parent()->zIndex());
    return m_zIndex;
}

void KoShape::repaint() const {
229 230 231 232
    if ( !m_shapeManagers.empty() )
    {
        foreach( KoShapeManager * manager, m_shapeManagers )
        {
233
            QRectF rect(QPointF(0, 0), size() );
234 235 236 237 238 239 240 241
            if(m_border) {
                KoInsets insets(0, 0, 0, 0);
                m_border->borderInsets(this, insets);
                rect.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
            }
            rect = m_matrix.mapRect(rect);
            manager->repaint( rect, this, true );
        }
Thomas Zander's avatar
Thomas Zander committed
242 243 244
    }
}

245
void KoShape::repaint(const QRectF &shape) const {
246 247
    if ( !m_shapeManagers.empty() && isVisible() )
    {
248
        QRectF rect(m_matrix.mapRect(shape));
249 250 251 252 253
        foreach( KoShapeManager * manager, m_shapeManagers )
        {
            manager->repaint(rect);
        }
    }
Thomas Zander's avatar
Thomas Zander committed
254 255 256 257 258 259 260 261 262
}

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;
263
    path.addRect(QRectF( QPointF(0, 0), size() ));
Thomas Zander's avatar
Thomas Zander committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    return path;
}

QPointF KoShape::absolutePosition() const {
    return m_matrix.map(QPointF(size().width() / 2.0 , size().height() / 2.0));
}

void KoShape::setAbsolutePosition(QPointF newPosition) {
    QPointF zero(0, 0);
    QMatrix matrix;
    // apply parents matrix to inherit any transformations done there.
    KoShapeContainer *container = m_parent;
    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();
292 293 294 295 296 297 298 299
    if ( m_angle != 0 )
    {
        matrix.translate( size().width() / 2.0 * m_scaleX, size().height() / 2.0 * m_scaleY );
        matrix.translate( size().height() / 2.0 * m_shearX, size().width() / 2.0 * m_shearY );
        matrix.rotate( m_angle );
        matrix.translate( -size().width() / 2.0 * m_scaleX, -size().height() / 2.0 * m_scaleY );
        matrix.translate( -size().height() / 2.0 * m_shearX, -size().width() / 2.0 * m_shearY );
    }
Thomas Zander's avatar
Thomas Zander committed
300 301 302 303
    matrix.shear( m_shearX, m_shearY );
    matrix.scale( m_scaleX, m_scaleY );

    QPointF vector2 = matrix.map( QPointF(size().width() / 2.0, size().height() / 2.0) );
304
    //kDebug() << "vector1: " << vector1 << ", vector2: " << vector2 << endl;
Thomas Zander's avatar
Thomas Zander committed
305 306 307 308

    setPosition(newPosition + vector1 - vector2);
}

Thomas Zander's avatar
Thomas Zander committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
void KoShape::copySettings(const KoShape *shape) {
    m_pos = shape->position();
    m_scaleX = shape->scaleX();
    m_scaleY = shape->scaleY();
    m_angle = shape->rotation();
    m_shearX = shape->shearX();
    m_shearY = shape->shearY();
    m_size = shape->size();
    m_connectors.clear();
    foreach(QPointF point, shape->connectors())
        addConnectionPoint(point);
    m_zIndex = shape->zIndex();
    m_visible = shape->isVisible();
    m_locked = shape->isLocked();
    m_keepAspect = shape->keepAspectRatio();
}

Thomas Zander's avatar
Thomas Zander committed
326 327 328
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
329 330
}

Thomas Zander's avatar
Thomas Zander committed
331
void KoShape::notifyChanged()
332 333 334
{
    foreach( KoShapeManager * manager, m_shapeManagers )
    {
Thomas Zander's avatar
Thomas Zander committed
335
        manager->notifyShapeChanged( this );
336 337 338
    }
}

339 340 341 342 343 344 345 346 347 348
void KoShape::setUserData(KoShapeUserData *userData) {
    if(m_userData)
        delete m_userData;
    m_userData = userData;
}

KoShapeUserData *KoShape::userData() const {
    return m_userData;
}

349
void KoShape::setApplicationData(KoShapeApplicationData *appData) {
350
    delete m_appData;
351 352 353 354 355 356 357
    m_appData = appData;
}

KoShapeApplicationData *KoShape::applicationData() const {
    return m_appData;
}

358
bool KoShape::hasTransparency() {
Thomas Zander's avatar
Thomas Zander committed
359 360 361 362 363
    if(m_backgroundBrush.style() == Qt::NoBrush)
        return true;
    return !m_backgroundBrush.isOpaque();
}

364 365 366 367 368 369 370
KoInsets KoShape::borderInsets() const {
    KoInsets answer;
    if(m_border)
        m_border->borderInsets(this, answer);
    return answer;
}

Thomas Zander's avatar
Thomas Zander committed
371 372 373 374 375 376
// static
void KoShape::applyConversion(QPainter &painter, const KoViewConverter &converter) {
    double zoomX, zoomY;
    converter.zoom(&zoomX, &zoomY);
    painter.scale(zoomX, zoomY);
}
377