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

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 KOSNAPGUIDE_H
0008 #define KOSNAPGUIDE_H
0009 
0010 #include "kritaflake_export.h"
0011 
0012 #include <QScopedPointer>
0013 #include <QList>
0014 #include <Qt>
0015 
0016 class KoSnapStrategy;
0017 class KoShape;
0018 class KoPathPoint;
0019 class KoViewConverter;
0020 class KoCanvasBase;
0021 class QPainter;
0022 class QPointF;
0023 class QRectF;
0024 
0025 /**
0026  * This class is the place where all the snapping (i.e. snap to grid) is handled.
0027  *
0028  * What this class does is snapping a given position (i.e. mouse position) to various
0029  * snapping targets like grid, boundbox etc.
0030  * The snap guide does not know anything about the specific snapping target. This
0031  * is handled by the different snapping strategies which are derived from KoSnapStrategy.
0032  * Snapping strategies can be enabled/disabled by passing a mask of corresponding
0033  * snapping ids to KoSnapGuide::enableSnapStrategies. There can be one or more snapping
0034  * strategies enabled at the same time. The best result (with the nearest distance to the
0035  * original position) is then returned to the caller of KoSnapGuide::snap.
0036  *
0037  * The snap guide is part of the KoCanvasBase class and thus can be accessed by any tool
0038  * or application via the canvas pointer.
0039  * For letting the user manage which snap stratgies to enable, there is a snap guide config
0040  * widget in guiutils.
0041  *
0042  */
0043 
0044 class KRITAFLAKE_EXPORT KoSnapGuide
0045 {
0046 public:
0047     /// the different possible snap Strategies
0048     enum Strategy
0049     {
0050         OrthogonalSnapping = 1,
0051         NodeSnapping = 2,
0052         ExtensionSnapping = 4,
0053         IntersectionSnapping = 8,
0054         GridSnapping = 0x10,
0055         BoundingBoxSnapping = 0x20,
0056         GuideLineSnapping = 0x40,
0057         DocumentBoundsSnapping = 0x80,
0058         DocumentCenterSnapping = 0x100,
0059         CustomSnapping = 0x200,
0060         PixelSnapping = 0x400
0061     };
0062     Q_DECLARE_FLAGS(Strategies, Strategy)
0063 
0064     /// Creates the snap guide to work on the given canvas
0065     explicit KoSnapGuide(KoCanvasBase *canvas);
0066 
0067     virtual ~KoSnapGuide();
0068 
0069     /// snaps the mouse position, returns if mouse was snapped
0070     QPointF snap(const QPointF &mousePosition, Qt::KeyboardModifiers modifiers);
0071 
0072     QPointF snap(const QPointF &mousePosition, const QPointF &dragOffset, Qt::KeyboardModifiers modifiers);
0073 
0074     /// paints the guide
0075     void paint(QPainter &painter, const KoViewConverter &converter);
0076 
0077     /// returns the bounding rect of the guide
0078     QRectF boundingRect();
0079 
0080     /// Adds an additional shape to snap to (useful when creating a path)
0081     void setAdditionalEditedShape(KoShape *shape);
0082 
0083     /// returns the extra shapes to use
0084     KoShape *additionalEditedShape() const;
0085 
0086     void enableSnapStrategy(Strategy type, bool value);
0087     bool isStrategyEnabled(Strategy type) const;
0088 
0089     /// enables the strategies used for snapping
0090     void enableSnapStrategies(Strategies strategies);
0091 
0092     /// returns the enabled snap strategies
0093     KoSnapGuide::Strategies enabledSnapStrategies() const;
0094 
0095     /**
0096      * Adds a custom snap strategy
0097      *
0098      * The snap guide take ownership of the strategy. All custom strategies
0099      * are destroyed when calling reset().
0100      */
0101     bool addCustomSnapStrategy(KoSnapStrategy *customStrategy);
0102 
0103     /**
0104      * Overrides the first entry of a strategy \p type with a strategy
0105      * \p strategy. Note that basically strategy->type() may not be equal
0106      * to type and that is ok. \p strategy may also be null.
0107      */
0108     void overrideSnapStrategy(Strategy type, KoSnapStrategy *strategy);
0109 
0110     /// enables the snapping guides
0111     void enableSnapping(bool on);
0112 
0113     /// returns if snapping is enabled
0114     bool isSnapping() const;
0115 
0116     /// sets the snap distances in pixels
0117     void setSnapDistance(int distance);
0118 
0119     /// returns the snap distance in pixels
0120     int snapDistance() const;
0121 
0122     /// returns the canvas the snap guide is working on
0123     KoCanvasBase *canvas() const;
0124 
0125     /// Sets a list of path points to ignore
0126     void setIgnoredPathPoints(const QList<KoPathPoint*> &ignoredPoints);
0127 
0128     /// Returns list of ignored points
0129     QList<KoPathPoint*> ignoredPathPoints() const;
0130 
0131     /// Sets list of ignored shapes
0132     void setIgnoredShapes(const QList<KoShape*> &ignoredShapes);
0133 
0134     /// Returns list of ignored shapes
0135     QList<KoShape*> ignoredShapes() const;
0136 
0137     /// Resets the snap guide
0138     void reset();
0139 
0140 private:
0141     class Private;
0142     const QScopedPointer<Private> d;
0143 };
0144 
0145 Q_DECLARE_OPERATORS_FOR_FLAGS(KoSnapGuide::Strategies)
0146 
0147 #endif // KOSNAPGUIDE_H