libs/flake

KoSnapStrategy.h

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2008-2009 Jan Hambrecht <jaham@gmx.net>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #ifndef KOSNAPSTRATEGY_H
00021 #define KOSNAPSTRATEGY_H
00022 
00023 #include "flake_export.h"
00024 
00025 #include <QtCore/QPointF>
00026 #include <QtGui/QPainterPath>
00027 
00028 class KoPathPoint;
00029 class KoSnapProxy;
00030 class KoViewConverter;
00031 
00032 class FLAKE_EXPORT KoSnapStrategy
00033 {
00034 public:
00036     enum SnapType {
00037         Orthogonal = 1,
00038         Node = 2,
00039         Extension = 4,
00040         Intersection = 8,
00041         Grid = 16,
00042         BoundingBox = 32,
00043         GuideLine = 64,
00044         Custom = 128
00045     };
00046 
00047     KoSnapStrategy(SnapType type);
00048     virtual ~KoSnapStrategy() {};
00049 
00050     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) = 0;
00051 
00053     SnapType type() const;
00054 
00055     static qreal squareDistance(const QPointF &p1, const QPointF &p2);
00056     static qreal scalarProduct(const QPointF &p1, const QPointF &p2);
00057 
00059     QPointF snappedPosition() const;
00060 
00062     virtual QPainterPath decoration(const KoViewConverter &converter) const = 0;
00063 
00064 protected:
00066     void setSnappedPosition(const QPointF &position);
00067 
00068 private:
00069     SnapType m_snapType;
00070     QPointF m_snappedPosition;
00071 };
00072 
00074 class OrthogonalSnapStrategy : public KoSnapStrategy
00075 {
00076 public:
00077     OrthogonalSnapStrategy();
00078     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00079     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00080 private:
00081     QLineF m_hLine;
00082     QLineF m_vLine;
00083 };
00084 
00086 class NodeSnapStrategy : public KoSnapStrategy
00087 {
00088 public:
00089     NodeSnapStrategy();
00090     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00091     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00092 };
00093 
00095 class ExtensionSnapStrategy : public KoSnapStrategy
00096 {
00097 public:
00098     ExtensionSnapStrategy();
00099     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00100     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00101 private:
00102     qreal project(const QPointF &lineStart , const QPointF &lineEnd, const QPointF &point);
00103     QPointF extensionDirection(KoPathPoint * point, const QMatrix &matrix);
00104     bool snapToExtension(QPointF &position, KoPathPoint * point, const QMatrix &matrix);
00105     QList<QLineF> m_lines;
00106 };
00107 
00109 class IntersectionSnapStrategy : public KoSnapStrategy
00110 {
00111 public:
00112     IntersectionSnapStrategy();
00113     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00114     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00115 };
00116 
00118 class GridSnapStrategy : public KoSnapStrategy
00119 {
00120 public:
00121     GridSnapStrategy();
00122     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00123     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00124 };
00125 
00127 class BoundingBoxSnapStrategy : public KoSnapStrategy
00128 {
00129 public:
00130     BoundingBoxSnapStrategy();
00131     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00132     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00133 private:
00134     qreal squareDistanceToLine(const QPointF &lineA, const QPointF &lineB, const QPointF &point, QPointF &pointOnLine);
00135     QPointF m_boxPoints[5];
00136 };
00137 
00139 class LineGuideSnapStrategy : public KoSnapStrategy
00140 {
00141 public:
00142     LineGuideSnapStrategy();
00143     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
00144     virtual QPainterPath decoration(const KoViewConverter &converter) const;
00145 private:
00146     int m_orientation;
00147 };
00148 
00149 #endif // KOSNAPSTRATEGY_H