File indexing completed on 2024-05-12 15:56:51

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2008-2009 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KOSNAPSTRATEGY_H
0008 #define KOSNAPSTRATEGY_H
0009 
0010 #include "KoSnapGuide.h"
0011 
0012 #include <QLineF>
0013 
0014 class TestSnapStrategy;
0015 class KoPathPoint;
0016 class KoSnapProxy;
0017 class KoViewConverter;
0018 
0019 class QTransform;
0020 class QPainterPath;
0021 
0022 class KRITAFLAKE_EXPORT KoSnapStrategy
0023 {
0024 public:
0025     KoSnapStrategy(KoSnapGuide::Strategy type);
0026     virtual ~KoSnapStrategy() {};
0027 
0028     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) = 0;
0029 
0030     /// returns the strategies type
0031     KoSnapGuide::Strategy type() const;
0032 
0033     static qreal squareDistance(const QPointF &p1, const QPointF &p2);
0034     static qreal scalarProduct(const QPointF &p1, const QPointF &p2);
0035 
0036     /// returns the snapped position form the last call to snapToPoints
0037     QPointF snappedPosition() const;
0038 
0039     /// returns the current snap strategy decoration
0040     virtual QPainterPath decoration(const KoViewConverter &converter) const = 0;
0041 
0042 protected:
0043     /// sets the current snapped position
0044     void setSnappedPosition(const QPointF &position);
0045 
0046 private:
0047     KoSnapGuide::Strategy m_snapType;
0048     QPointF m_snappedPosition;
0049 };
0050 
0051 /// snaps to x- or y-coordinates of path points
0052 class KRITAFLAKE_EXPORT OrthogonalSnapStrategy : public KoSnapStrategy
0053 {
0054 public:
0055     OrthogonalSnapStrategy();
0056     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0057     QPainterPath decoration(const KoViewConverter &converter) const override;
0058 private:
0059     QLineF m_hLine;
0060     QLineF m_vLine;
0061 };
0062 
0063 /// snaps to path points
0064 class KRITAFLAKE_EXPORT NodeSnapStrategy : public KoSnapStrategy
0065 {
0066 public:
0067     NodeSnapStrategy();
0068     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0069     QPainterPath decoration(const KoViewConverter &converter) const override;
0070 };
0071 
0072 /// snaps extension lines of path shapes
0073 class KRITAFLAKE_EXPORT ExtensionSnapStrategy : public KoSnapStrategy
0074 {
0075     friend class TestSnapStrategy;
0076 public:
0077     ExtensionSnapStrategy();
0078     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0079     QPainterPath decoration(const KoViewConverter &converter) const override;
0080 private:
0081     qreal project(const QPointF &lineStart , const QPointF &lineEnd, const QPointF &point);
0082     QPointF extensionDirection(KoPathPoint * point, const QTransform &matrix);
0083     bool snapToExtension(QPointF &position, KoPathPoint * point, const QTransform &matrix);
0084     QList<QLineF> m_lines;
0085 };
0086 
0087 /// snaps to intersections of shapes
0088 class KRITAFLAKE_EXPORT IntersectionSnapStrategy : public KoSnapStrategy
0089 {
0090 public:
0091     IntersectionSnapStrategy();
0092     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0093     QPainterPath decoration(const KoViewConverter &converter) const override;
0094 };
0095 
0096 /// snaps to the canvas grid
0097 class KRITAFLAKE_EXPORT GridSnapStrategy : public KoSnapStrategy
0098 {
0099 public:
0100     GridSnapStrategy();
0101     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0102     QPainterPath decoration(const KoViewConverter &converter) const override;
0103 };
0104 
0105 /// snaps to shape bounding boxes
0106 class KRITAFLAKE_EXPORT BoundingBoxSnapStrategy : public KoSnapStrategy
0107 {
0108     friend class TestSnapStrategy;
0109 public:
0110     BoundingBoxSnapStrategy();
0111     bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance) override;
0112     QPainterPath decoration(const KoViewConverter &converter) const override;
0113 private:
0114     qreal squareDistanceToLine(const QPointF &lineA, const QPointF &lineB, const QPointF &point, QPointF &pointOnLine);
0115     QPointF m_boxPoints[5];
0116 };
0117 
0118 // KoGuidesData has been moved into Krita. Please port this class!
0119 //
0120 /// snaps to line guides
0121 // class KRITAFLAKE_EXPORT LineGuideSnapStrategy : public KoSnapStrategy
0122 // {
0123 // public:
0124 //     LineGuideSnapStrategy();
0125 //     virtual bool snap(const QPointF &mousePosition, KoSnapProxy * proxy, qreal maxSnapDistance);
0126 //     virtual QPainterPath decoration(const KoViewConverter &converter) const;
0127 // private:
0128 //     int m_orientation;
0129 // };
0130 
0131 #endif // KOSNAPSTRATEGY_H