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, const KoColorDisplayRendererInterface *displayRenderer)
 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 24 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 49 of file KoSnapGuide.h.

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

Constructor & Destructor Documentation

◆ ~Private()

KoSnapGuide::~Private ( )
inline

Definition at line 34 of file KoSnapGuide.cpp.

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

◆ KoSnapGuide()

KoSnapGuide::KoSnapGuide ( KoCanvasBase * canvas)
explicit

Creates the snap guide to work on the given canvas.

Definition at line 54 of file KoSnapGuide.cpp.

55 : d(new Private(canvas))
56{
57 d->strategies.append(toQShared(new GridSnapStrategy()));
58 d->strategies.append(toQShared(new NodeSnapStrategy()));
59 d->strategies.append(toQShared(new OrthogonalSnapStrategy()));
60 d->strategies.append(toQShared(new ExtensionSnapStrategy()));
61 d->strategies.append(toQShared(new IntersectionSnapStrategy()));
62 d->strategies.append(toQShared(new BoundingBoxSnapStrategy()));
63}
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 65 of file KoSnapGuide.cpp.

66{
67}

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 103 of file KoSnapGuide.cpp.

104{
105 if (!customStrategy || customStrategy->type() != CustomSnapping)
106 return false;
107
108 d->strategies.append(toQShared(customStrategy));
109 return true;
110}
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 200 of file KoSnapGuide.cpp.

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

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 98 of file KoSnapGuide.cpp.

99{
100 return d->usedStrategies;
101}

References d.

◆ enableSnapping()

void KoSnapGuide::enableSnapping ( bool on)

enables the snapping guides

Definition at line 132 of file KoSnapGuide.cpp.

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

References d.

◆ enableSnapStrategies()

void KoSnapGuide::enableSnapStrategies ( Strategies strategies)

enables the strategies used for snapping

Definition at line 93 of file KoSnapGuide.cpp.

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

References d, and strategies.

◆ enableSnapStrategy()

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

Definition at line 79 of file KoSnapGuide.cpp.

80{
81 if (value) {
82 d->usedStrategies |= type;
83 } else {
84 d->usedStrategies &= ~type;
85 }
86}
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 250 of file KoSnapGuide.cpp.

251{
252 return d->ignoredPoints;
253}

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 137 of file KoSnapGuide.cpp.

138{
139 return d->active;
140}

References d.

◆ isStrategyEnabled()

bool KoSnapGuide::isStrategyEnabled ( Strategy type) const

Definition at line 88 of file KoSnapGuide.cpp.

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

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 112 of file KoSnapGuide.cpp.

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

References d, and toQShared().

◆ paint()

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

paints the guide

Definition at line 212 of file KoSnapGuide.cpp.

213{
214 if (! d->currentStrategy || ! d->active)
215 return;
216
217 QPainterPath decoration = d->currentStrategy->decoration(converter);
218
219 int thickness = d->canvas->resourceManager()? d->canvas->resourceManager()->decorationThickness(): 1;
220
221 painter.setBrush(Qt::NoBrush);
222
223 KoColor c;
224 c.fromQColor(Qt::white);
225
226 QPen whitePen(displayRenderer->convertColorToDisplayColorSpace(c), thickness);
227 whitePen.setCosmetic(true);
228 whitePen.setStyle(Qt::SolidLine);
229 painter.setPen(whitePen);
230 painter.drawPath(decoration);
231
232 c.fromQColor(Qt::red);
233 QPen redPen(displayRenderer->convertColorToDisplayColorSpace(c), thickness);
234 redPen.setCosmetic(true);
235 redPen.setStyle(Qt::DotLine);
236 painter.setPen(redPen);
237 painter.drawPath(decoration);
238}
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213

References KoColorDisplayRendererInterface::convertColorToDisplayColorSpace(), d, and KoColor::fromQColor().

◆ Private()

KoSnapGuide::Private ( KoCanvasBase * parentCanvas)
inline

Definition at line 27 of file KoSnapGuide.cpp.

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

◆ reset()

void KoSnapGuide::reset ( )

Resets the snap guide.

Definition at line 265 of file KoSnapGuide.cpp.

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

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 69 of file KoSnapGuide.cpp.

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

References d.

◆ setIgnoredPathPoints()

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

Sets a list of path points to ignore.

Definition at line 245 of file KoSnapGuide.cpp.

246{
247 d->ignoredPoints = ignoredPoints;
248}
QList< KoPathPoint * > ignoredPoints

References d, and ignoredPoints.

◆ setIgnoredShapes()

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

Sets list of ignored shapes.

Definition at line 255 of file KoSnapGuide.cpp.

256{
257 d->ignoredShapes = ignoredShapes;
258}
QList< KoShape * > ignoredShapes

References d, and ignoredShapes.

◆ setSnapDistance()

void KoSnapGuide::setSnapDistance ( int distance)

sets the snap distances in pixels

Definition at line 142 of file KoSnapGuide.cpp.

143{
144 d->snapDistance = qAbs(distance);
145}
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 152 of file KoSnapGuide.cpp.

153{
154 QPointF pos = mousePosition + dragOffset;
155 pos = snap(pos, modifiers);
156 return pos - dragOffset;
157}
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 159 of file KoSnapGuide.cpp.

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

◆ additionalEditedShape

KoShape * KoSnapGuide::additionalEditedShape

Definition at line 40 of file KoSnapGuide.cpp.

◆ canvas

KoCanvasBase * KoSnapGuide::canvas

Definition at line 39 of file KoSnapGuide.cpp.

◆ currentStrategy

KoSnapStrategySP KoSnapGuide::currentStrategy

Definition at line 45 of file KoSnapGuide.cpp.

◆ d

const QScopedPointer<Private> KoSnapGuide::d
private

Definition at line 143 of file KoSnapGuide.h.

◆ ignoredPoints

QList<KoPathPoint*> KoSnapGuide::ignoredPoints

Definition at line 50 of file KoSnapGuide.cpp.

◆ ignoredShapes

QList< KoShape * > KoSnapGuide::ignoredShapes

Definition at line 51 of file KoSnapGuide.cpp.

◆ snapDistance

int KoSnapGuide::snapDistance

Definition at line 49 of file KoSnapGuide.cpp.

◆ strategies

StrategiesList KoSnapGuide::strategies

Definition at line 44 of file KoSnapGuide.cpp.

◆ usedStrategies

KoSnapGuide::Strategies KoSnapGuide::usedStrategies

Definition at line 47 of file KoSnapGuide.cpp.


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