KoConnectionShape.h 3.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* This file is part of the KDE project
 * Copyright (C) 2007 Boudewijn Rempt <boud@kde.org>
 * Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
 *
 * 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.
 */

#ifndef KO_CONNECTION_SHAPE_H
#define KO_CONNECTION_SHAPE_H

#include <KoParameterShape.h>

#define KOCONNECTIONSHAPEID "KoConnectionShape"

/// A connection to a connection point of a shape
30
typedef QPair<KoShape*, int> KoConnection;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

class KoConnectionShape : public KoParameterShape
{
public:
    enum Type {
        Standard, ///< escapes connected shapes with straight lines, connects with perpendicular lines
        Lines,    ///< escapes connected shapes with straight lines, connects with straight line
        Straight, ///< one straight line between connected shapes
        Curve     ///< a single curved line between connected shapes
    };

    explicit KoConnectionShape();
    virtual ~KoConnectionShape();

    // reimplemented
46
    virtual void paint(QPainter& painter, const KoViewConverter& converter);
47 48

    // reimplemented
49
    virtual void saveOdf(KoShapeSavingContext & context) const;
50 51

    // reimplemented
52
    virtual bool loadOdf(const KoXmlElement & element, KoShapeLoadingContext &context);
53 54

    // reimplemented
55 56 57
    inline QString pathShapeId() const {
        return KOCONNECTIONSHAPEID;
    }
58

59 60 61
    // reimplemented
    virtual bool hitTest(const QPointF &position) const;

62
    /// Sets the first shape the connector is connected to
63
    bool setConnection1(KoShape * shape1, int connectionPointIndex);
64 65

    /// Sets the second shape the connector is connected to
66
    bool setConnection2(KoShape * shape2, int connectionPointIndex);
67 68 69 70 71 72 73 74 75 76 77 78 79 80

    /// Returns the connection to the first shape
    KoConnection connection1() const;

    /// Returns the connection to the second shape
    KoConnection connection2() const;

    /// Updates connections to shapes
    void updateConnections();

    /// Returns connection type
    Type connectionType() const;

    /// Sets the connection type
81
    void setConnectionType(Type connectionType);
82 83 84
protected:

    /// reimplemented
85
    void moveHandleAction(int handleId, const QPointF & point, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
86 87

    /// reimplemented
88
    void updatePath(const QSizeF &size);
89

90 91 92
    /// reimplemented
    virtual void shapeChanged(ChangeType type);

93
    /// Returns if given handle is connected to a shape
94
    bool handleConnected(int handleId) const;
95 96

    /// Returns escape direction of given handle
97
    QPointF escapeDirection(int handleId) const;
98 99

    /// Checks if rays from given points into given directions intersect
100
    bool intersects(const QPointF &p1, const QPointF &d1, const QPointF &p2, const QPointF &d2, QPointF &isect);
101 102

    /// Returns perpendicular direction from given point p1 and direction d1 toward point p2
103
    QPointF perpendicularDirection(const QPointF &p1, const QPointF &d1, const QPointF &p2);
104 105 106

private:

107
    // reimplemented
108
    virtual void notifyShapeChanged(KoShape * shape, ChangeType type);
109

110 111
    qreal scalarProd(const QPointF &v1, const QPointF &v2);
    qreal crossProd(const QPointF &v1, const QPointF &v2);
112 113 114 115 116 117 118

    struct Private;
    Private * const d;
};


#endif