KoParameterShape.cpp 3.91 KB
Newer Older
1 2
/* This file is part of the KDE project
   Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
3
   Copyright (C) 2007 Thomas Zander <zander@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   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 "KoParameterShape.h"

#include <QPainter>
24
#include <KDebug>
25

26 27 28 29 30 31
class KoParameterShape::Private {
public:
    Private() : modified(false) {}
    bool modified;
};

32
KoParameterShape::KoParameterShape()
33
    : d(new Private())
34 35 36 37 38 39 40 41 42 43 44
{
}

KoParameterShape::~KoParameterShape()
{
}

void KoParameterShape::moveHandle( int handleId, const QPointF & point, Qt::KeyboardModifiers modifiers )
{
    if ( handleId >= m_handles.size() )
    {
45
        kWarning(30006) << "handleId out of bounds" << endl;
46 47
        return;
    }
48 49

    repaint();
50
    // function to do special stuff
51
    moveHandleAction( handleId, documentToShape( point ), modifiers );
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

    updatePath( size() );
    repaint();
}


int KoParameterShape::handleIdAt( const QRectF & rect ) const
{
    int handle = -1;

    for ( int i = 0; i < m_handles.size(); ++i )
    {
        if ( rect.contains( m_handles.at( i ) ) )
        {
            handle = i;
            break;
        }
    }
    return handle;
}

QPointF KoParameterShape::handlePosition( int handleId )
{
    return m_handles[handleId];
}

78
void KoParameterShape::paintHandles( QPainter & painter, const KoViewConverter & converter, int handleRadius )
79 80 81
{
    applyConversion( painter, converter );

Jan Hambrecht's avatar
Jan Hambrecht committed
82 83 84
    QMatrix worldMatrix = painter.worldMatrix();
    painter.setMatrix( QMatrix() );

David Faure's avatar
David Faure committed
85
    QMatrix matrix;
86
    matrix.rotate( 45.0 );
87
    QPolygonF poly( handleRect( QPointF( 0, 0 ), handleRadius ) );
88 89 90 91 92
    poly = matrix.map( poly );

    QList<QPointF>::const_iterator it( m_handles.begin() );
    for ( ; it != m_handles.end(); ++it ) 
    {
Jan Hambrecht's avatar
Jan Hambrecht committed
93 94 95 96
        QPointF moveVector = worldMatrix.map( *it );
        poly.translate( moveVector.x(), moveVector.y() );
        painter.drawPolygon( poly );
        poly.translate( -moveVector.x(), -moveVector.y() );
97 98 99
    }
}

100
void KoParameterShape::paintHandle( QPainter & painter, const KoViewConverter & converter, int handleId, int handleRadius )
101 102 103
{
    applyConversion( painter, converter );

Jan Hambrecht's avatar
Jan Hambrecht committed
104 105 106
    QMatrix worldMatrix = painter.worldMatrix();
    painter.setMatrix( QMatrix() );

David Faure's avatar
David Faure committed
107
    QMatrix matrix;
108
    matrix.rotate( 45.0 );
109
    QPolygonF poly( handleRect( QPointF( 0, 0 ), handleRadius ) );
110
    poly = matrix.map( poly );
Jan Hambrecht's avatar
Jan Hambrecht committed
111
    poly.translate( worldMatrix.map( m_handles[handleId] ) );
112 113 114
    painter.drawPolygon( poly );
}

115
void KoParameterShape::setSize( const QSizeF &newSize )
116 117 118 119 120 121 122 123 124
{
    QSizeF oldSize = size();
    QMatrix matrix( newSize.width() / oldSize.width(), 0, 0, newSize.height() / oldSize.height(), 0, 0 );

    for( int i = 0; i < m_handles.size(); ++i )
    {
        m_handles[i] = matrix.map( m_handles[i] );
    }

125
    KoPathShape::setSize( newSize );
126
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

QPointF KoParameterShape::normalize()
{
    QPointF offset( KoPathShape::normalize() );
    QMatrix matrix;
    matrix.translate( -offset.x(), -offset.y() );

    for( int i = 0; i < m_handles.size(); ++i )
    {
        m_handles[i] = matrix.map( m_handles[i] );
    }

    return offset;
}

142 143 144 145 146 147 148
bool KoParameterShape::isParametricShape() const {
    return !d->modified;
}

void KoParameterShape::setModified( bool modified ) {
    d->modified = modified;
}