Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSnapGuide Class Reference

#include <KoSnapGuide.h>

+ Inheritance diagram for KoSnapGuide:

Public Types

typedef QSharedPointer< KoSnapStrategyKoSnapStrategySP
 
typedef QList< KoSnapStrategySPStrategiesList
 
enum  Strategy {
  OrthogonalSnapping = 1 , NodeSnapping = 2 , ExtensionSnapping = 4 , IntersectionSnapping = 8 ,
  GridSnapping = 0x10 , BoundingBoxSnapping = 0x20 , GuideLineSnapping = 0x40 , DocumentBoundsSnapping = 0x80 ,
  DocumentCenterSnapping = 0x100 , CustomSnapping = 0x200 , PixelSnapping = 0x400
}
 the different possible snap Strategies More...
 

Public Member Functions

bool addCustomSnapStrategy (KoSnapStrategy *customStrategy)
 
KoShapeadditionalEditedShape () const
 returns the extra shapes to use
 
QRectF boundingRect ()
 returns the bounding rect of the guide
 
KoCanvasBasecanvas () const
 returns the canvas the snap guide is working on
 
KoSnapGuide::Strategies enabledSnapStrategies () const
 returns the enabled snap strategies
 
void enableSnapping (bool on)
 enables the snapping guides
 
void enableSnapStrategies (Strategies strategies)
 enables the strategies used for snapping
 
void enableSnapStrategy (Strategy type, bool value)
 
QList< KoPathPoint * > ignoredPathPoints () const
 Returns list of ignored points.
 
QList< KoShape * > ignoredShapes () const
 Returns list of ignored shapes.
 
bool isSnapping () const
 returns if snapping is enabled
 
bool isStrategyEnabled (Strategy type) const
 
 KoSnapGuide (KoCanvasBase *canvas)
 Creates the snap guide to work on the given canvas.
 
void overrideSnapStrategy (Strategy type, KoSnapStrategy *strategy)
 
void paint (QPainter &painter, const KoViewConverter &converter)
 paints the guide
 
 Private (KoCanvasBase *parentCanvas)
 
void reset ()
 Resets the snap guide.
 
void setAdditionalEditedShape (KoShape *shape)
 Adds an additional shape to snap to (useful when creating a path)
 
void setIgnoredPathPoints (const QList< KoPathPoint * > &ignoredPoints)
 Sets a list of path points to ignore.
 
void setIgnoredShapes (const QList< KoShape * > &ignoredShapes)
 Sets list of ignored shapes.
 
void setSnapDistance (int distance)
 sets the snap distances in pixels
 
QPointF snap (const QPointF &mousePosition, const QPointF &dragOffset, Qt::KeyboardModifiers modifiers)
 
QPointF snap (const QPointF &mousePosition, Qt::KeyboardModifiers modifiers)
 snaps the mouse position, returns if mouse was snapped
 
int snapDistance () const
 returns the snap distance in pixels
 
virtual ~KoSnapGuide ()
 
 ~Private ()
 

Public Attributes

bool active
 
KoShapeadditionalEditedShape
 
KoCanvasBasecanvas
 
KoSnapStrategySP currentStrategy
 
QList< KoPathPoint * > ignoredPoints
 
QList< KoShape * > ignoredShapes
 
int snapDistance
 
StrategiesList strategies
 
KoSnapGuide::Strategies usedStrategies
 

Private Attributes

const QScopedPointer< Privated
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

This class is the place where all the snapping (i.e. snap to grid) is handled.

What this class does is snapping a given position (i.e. mouse position) to various snapping targets like grid, boundbox etc. The snap guide does not know anything about the specific snapping target. This is handled by the different snapping strategies which are derived from KoSnapStrategy. Snapping strategies can be enabled/disabled by passing a mask of corresponding snapping ids to KoSnapGuide::enableSnapStrategies. There can be one or more snapping strategies enabled at the same time. The best result (with the nearest distance to the original position) is then returned to the caller of KoSnapGuide::snap.

The snap guide is part of the KoCanvasBase class and thus can be accessed by any tool or application via the canvas pointer. For letting the user manage which snap strategies to enable, there is a snap guide config widget in guiutils.

Definition at line 23 of file KoSnapGuide.cpp.

Member Typedef Documentation

◆ KoSnapStrategySP

◆ StrategiesList

Member Enumeration Documentation

◆ Strategy

the different possible snap Strategies

Enumerator
OrthogonalSnapping 
NodeSnapping 
ExtensionSnapping 
IntersectionSnapping 
GridSnapping 
BoundingBoxSnapping 
GuideLineSnapping 
DocumentBoundsSnapping 
DocumentCenterSnapping 
CustomSnapping 
PixelSnapping 

Definition at line 48 of file KoSnapGuide.h.

49 {
51 NodeSnapping = 2,
54 GridSnapping = 0x10,
56 GuideLineSnapping = 0x40,
59 CustomSnapping = 0x200,
60 PixelSnapping = 0x400
61 };
@ DocumentBoundsSnapping
Definition KoSnapGuide.h:57
@ DocumentCenterSnapping
Definition KoSnapGuide.h:58
@ IntersectionSnapping
Definition KoSnapGuide.h:53
@ BoundingBoxSnapping
Definition KoSnapGuide.h:55

Constructor & Destructor Documentation

◆ ~Private()

KoSnapGuide::~Private ( )
inline

Definition at line 33 of file KoSnapGuide.cpp.

34 {
35 strategies.clear();
36 }
StrategiesList strategies

◆ KoSnapGuide()

KoSnapGuide::KoSnapGuide ( KoCanvasBase * canvas)
explicit

Creates the snap guide to work on the given canvas.

Definition at line 53 of file KoSnapGuide.cpp.

54 : d(new Private(canvas))
55{
56 d->strategies.append(toQShared(new GridSnapStrategy()));
57 d->strategies.append(toQShared(new NodeSnapStrategy()));
58 d->strategies.append(toQShared(new OrthogonalSnapStrategy()));
59 d->strategies.append(toQShared(new ExtensionSnapStrategy()));
60 d->strategies.append(toQShared(new IntersectionSnapStrategy()));
61 d->strategies.append(toQShared(new BoundingBoxSnapStrategy()));
62}
snaps to shape bounding boxes
snaps extension lines of path shapes
snaps to the canvas grid
snaps to intersections of shapes
const QScopedPointer< Private > d
KoCanvasBase * canvas
snaps to path points
snaps to x- or y-coordinates of path points
QSharedPointer< T > toQShared(T *ptr)

References d, and toQShared().

◆ ~KoSnapGuide()

KoSnapGuide::~KoSnapGuide ( )
virtual

Definition at line 64 of file KoSnapGuide.cpp.

65{
66}

Member Function Documentation

◆ addCustomSnapStrategy()

bool KoSnapGuide::addCustomSnapStrategy ( KoSnapStrategy * customStrategy)

Adds a custom snap strategy

The snap guide take ownership of the strategy. All custom strategies are destroyed when calling reset().

Definition at line 102 of file KoSnapGuide.cpp.

103{
104 if (!customStrategy || customStrategy->type() != CustomSnapping)
105 return false;
106
107 d->strategies.append(toQShared(customStrategy));
108 return true;
109}
KoSnapGuide::Strategy type() const
returns the strategies type

References CustomSnapping, d, toQShared(), and KoSnapStrategy::type().

◆ additionalEditedShape()

KoShape * KoSnapGuide::additionalEditedShape ( ) const

returns the extra shapes to use

◆ boundingRect()

QRectF KoSnapGuide::boundingRect ( )

returns the bounding rect of the guide

Definition at line 199 of file KoSnapGuide.cpp.

200{
201 QRectF rect;
202
203 if (d->currentStrategy) {
204 rect = d->currentStrategy->decoration(*d->canvas->viewConverter()).boundingRect();
205 return rect.adjusted(-2, -2, 2, 2);
206 } else {
207 return rect;
208 }
209}

References d.

◆ canvas()

KoCanvasBase * KoSnapGuide::canvas ( ) const

returns the canvas the snap guide is working on

◆ enabledSnapStrategies()

KoSnapGuide::Strategies KoSnapGuide::enabledSnapStrategies ( ) const

returns the enabled snap strategies

Definition at line 97 of file KoSnapGuide.cpp.

98{
99 return d->usedStrategies;
100}

References d.

◆ enableSnapping()

void KoSnapGuide::enableSnapping ( bool on)

enables the snapping guides

Definition at line 131 of file KoSnapGuide.cpp.

132{
133 d->active = on;
134}

References d.

◆ enableSnapStrategies()

void KoSnapGuide::enableSnapStrategies ( Strategies strategies)

enables the strategies used for snapping

Definition at line 92 of file KoSnapGuide.cpp.

93{
94 d->usedStrategies = strategies;
95}

References d, and strategies.

◆ enableSnapStrategy()

void KoSnapGuide::enableSnapStrategy ( Strategy type,
bool value )

Definition at line 78 of file KoSnapGuide.cpp.

79{
80 if (value) {
81 d->usedStrategies |= type;
82 } else {
83 d->usedStrategies &= ~type;
84 }
85}
float value(const T *src, size_t ch)

References d, and value().

◆ ignoredPathPoints()

QList< KoPathPoint * > KoSnapGuide::ignoredPathPoints ( ) const

Returns list of ignored points.

Definition at line 245 of file KoSnapGuide.cpp.

246{
247 return d->ignoredPoints;
248}

References d.

◆ ignoredShapes()

QList< KoShape * > KoSnapGuide::ignoredShapes ( ) const

Returns list of ignored shapes.

◆ isSnapping()

bool KoSnapGuide::isSnapping ( ) const

returns if snapping is enabled

Definition at line 136 of file KoSnapGuide.cpp.

137{
138 return d->active;
139}

References d.

◆ isStrategyEnabled()

bool KoSnapGuide::isStrategyEnabled ( Strategy type) const

Definition at line 87 of file KoSnapGuide.cpp.

88{
89 return d->usedStrategies & type;
90}

References d.

◆ overrideSnapStrategy()

void KoSnapGuide::overrideSnapStrategy ( Strategy type,
KoSnapStrategy * strategy )

Overrides the first entry of a strategy type with a strategy strategy. Note that basically strategy->type() may not be equal to type and that is ok. strategy may also be null.

Definition at line 111 of file KoSnapGuide.cpp.

112{
113 for (auto it = d->strategies.begin(); it != d->strategies.end(); /*noop*/) {
114 if ((*it)->type() == type) {
115 if (strategy) {
116 *it = toQShared(strategy);
117 } else {
118 it = d->strategies.erase(it);
119 }
120 return;
121 } else {
122 ++it;
123 }
124 }
125
126 if (strategy) {
127 d->strategies.append(toQShared(strategy));
128 }
129}

References d, and toQShared().

◆ paint()

void KoSnapGuide::paint ( QPainter & painter,
const KoViewConverter & converter )

paints the guide

Definition at line 211 of file KoSnapGuide.cpp.

212{
213 if (! d->currentStrategy || ! d->active)
214 return;
215
216 QPainterPath decoration = d->currentStrategy->decoration(converter);
217
218 int thickness = d->canvas->resourceManager()? d->canvas->resourceManager()->decorationThickness(): 1;
219
220 painter.setBrush(Qt::NoBrush);
221
222 QPen whitePen(Qt::white, thickness);
223 whitePen.setCosmetic(true);
224 whitePen.setStyle(Qt::SolidLine);
225 painter.setPen(whitePen);
226 painter.drawPath(decoration);
227
228 QPen redPen(Qt::red, thickness);
229 redPen.setCosmetic(true);
230 redPen.setStyle(Qt::DotLine);
231 painter.setPen(redPen);
232 painter.drawPath(decoration);
233}

References d.

◆ Private()

KoSnapGuide::Private ( KoCanvasBase * parentCanvas)
inline

Definition at line 26 of file KoSnapGuide.cpp.

27 : canvas(parentCanvas), additionalEditedShape(0), currentStrategy(0),
28 active(true),
29 snapDistance(10)
30 {
31 }
KoShape * additionalEditedShape
KoSnapStrategySP currentStrategy

◆ reset()

void KoSnapGuide::reset ( )

Resets the snap guide.

Definition at line 260 of file KoSnapGuide.cpp.

261{
262 d->currentStrategy.clear();
263 d->additionalEditedShape = 0;
264 d->ignoredPoints.clear();
265 d->ignoredShapes.clear();
266 // remove all custom strategies
267 int strategyCount = d->strategies.count();
268 for (int i = strategyCount-1; i >= 0; --i) {
269 if (d->strategies[i]->type() == CustomSnapping) {
270 d->strategies.removeAt(i);
271 }
272 }
273}

References CustomSnapping, and d.

◆ setAdditionalEditedShape()

void KoSnapGuide::setAdditionalEditedShape ( KoShape * shape)

Adds an additional shape to snap to (useful when creating a path)

Definition at line 68 of file KoSnapGuide.cpp.

69{
70 d->additionalEditedShape = shape;
71}

References d.

◆ setIgnoredPathPoints()

void KoSnapGuide::setIgnoredPathPoints ( const QList< KoPathPoint * > & ignoredPoints)

Sets a list of path points to ignore.

Definition at line 240 of file KoSnapGuide.cpp.

241{
242 d->ignoredPoints = ignoredPoints;
243}
QList< KoPathPoint * > ignoredPoints

References d, and ignoredPoints.

◆ setIgnoredShapes()

void KoSnapGuide::setIgnoredShapes ( const QList< KoShape * > & ignoredShapes)

Sets list of ignored shapes.

Definition at line 250 of file KoSnapGuide.cpp.

251{
252 d->ignoredShapes = ignoredShapes;
253}
QList< KoShape * > ignoredShapes

References d, and ignoredShapes.

◆ setSnapDistance()

void KoSnapGuide::setSnapDistance ( int distance)

sets the snap distances in pixels

Definition at line 141 of file KoSnapGuide.cpp.

142{
143 d->snapDistance = qAbs(distance);
144}
qreal distance(const QPointF &p1, const QPointF &p2)

References d, and distance().

◆ snap() [1/2]

QPointF KoSnapGuide::snap ( const QPointF & mousePosition,
const QPointF & dragOffset,
Qt::KeyboardModifiers modifiers )

Definition at line 151 of file KoSnapGuide.cpp.

152{
153 QPointF pos = mousePosition + dragOffset;
154 pos = snap(pos, modifiers);
155 return pos - dragOffset;
156}
QPointF snap(const QPointF &mousePosition, Qt::KeyboardModifiers modifiers)
snaps the mouse position, returns if mouse was snapped

References snap().

◆ snap() [2/2]

QPointF KoSnapGuide::snap ( const QPointF & mousePosition,
Qt::KeyboardModifiers modifiers )

snaps the mouse position, returns if mouse was snapped

Definition at line 158 of file KoSnapGuide.cpp.

159{
160 d->currentStrategy.clear();
161
162 if (! d->active || (modifiers & Qt::ShiftModifier))
163 return mousePosition;
164
165 KoSnapProxy proxy(this);
166
167 using PriorityTuple = std::tuple<KoSnapStrategy::SnapType, qreal>;
168 PriorityTuple minPriority(KoSnapStrategy::ToLine, HUGE_VAL);
169
170 const qreal maxSnapDistance = d->canvas->viewConverter()->
171 viewToDocument(QSizeF(d->snapDistance,
172 d->snapDistance)).width();
173
174 foreach (Private::KoSnapStrategySP strategy, d->strategies) {
175 if (d->usedStrategies & strategy->type() ||
176 strategy->type() == GridSnapping ||
177 strategy->type() == CustomSnapping) {
178
179 if (! strategy->snap(mousePosition, &proxy, maxSnapDistance))
180 continue;
181
182 QPointF snapCandidate = strategy->snappedPosition();
183 qreal distance = KoSnapStrategy::squareDistance(snapCandidate, mousePosition);
184
185 const PriorityTuple priority(strategy->snappedType(), distance);
186 if (priority < minPriority) {
187 d->currentStrategy = strategy;
188 minPriority = priority;
189 }
190 }
191 }
192
193 if (! d->currentStrategy)
194 return mousePosition;
195
196 return d->currentStrategy->snappedPosition();
197}
static qreal squareDistance(const QPointF &p1, const QPointF &p2)

References CustomSnapping, d, distance(), GridSnapping, KoSnapStrategy::squareDistance(), and KoSnapStrategy::ToLine.

◆ snapDistance()

int KoSnapGuide::snapDistance ( ) const

returns the snap distance in pixels

Member Data Documentation

◆ active

bool KoSnapGuide::active

Definition at line 47 of file KoSnapGuide.cpp.

◆ additionalEditedShape

KoShape * KoSnapGuide::additionalEditedShape

Definition at line 39 of file KoSnapGuide.cpp.

◆ canvas

KoCanvasBase * KoSnapGuide::canvas

Definition at line 38 of file KoSnapGuide.cpp.

◆ currentStrategy

KoSnapStrategySP KoSnapGuide::currentStrategy

Definition at line 44 of file KoSnapGuide.cpp.

◆ d

const QScopedPointer<Private> KoSnapGuide::d
private

Definition at line 142 of file KoSnapGuide.h.

◆ ignoredPoints

QList<KoPathPoint*> KoSnapGuide::ignoredPoints

Definition at line 49 of file KoSnapGuide.cpp.

◆ ignoredShapes

QList< KoShape * > KoSnapGuide::ignoredShapes

Definition at line 50 of file KoSnapGuide.cpp.

◆ snapDistance

int KoSnapGuide::snapDistance

Definition at line 48 of file KoSnapGuide.cpp.

◆ strategies

StrategiesList KoSnapGuide::strategies

Definition at line 43 of file KoSnapGuide.cpp.

◆ usedStrategies

KoSnapGuide::Strategies KoSnapGuide::usedStrategies

Definition at line 46 of file KoSnapGuide.cpp.


The documentation for this class was generated from the following files: