Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSnapProxy.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008-2009 Jan Hambrecht <jaham@gmx.net>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "KoSnapProxy.h"
8#include "KoSnapGuide.h"
9#include "KoCanvasBase.h"
10#include "KoShapeManager.h"
11#include "KoPathShape.h"
12#include "KoPathPoint.h"
13#include <KoSnapData.h>
14#include <KoShapeLayer.h>
15
17 : m_snapGuide(snapGuide)
18{
19}
20
21QList<QPointF> KoSnapProxy::pointsInRect(const QRectF &rect, bool omitEditedShape)
22{
23 QList<QPointF> points;
24 QList<KoShape*> shapes = shapesInRect(rect, omitEditedShape);
25 Q_FOREACH (KoShape * shape, shapes) {
26 Q_FOREACH (const QPointF & point, pointsFromShape(shape)) {
27 if (rect.contains(point))
28 points.append(point);
29 }
30 }
31
32 return points;
33}
34
35QList<KoShape*> KoSnapProxy::shapesInRect(const QRectF &rect, bool omitEditedShape)
36{
38 Q_FOREACH (KoShape * shape, m_snapGuide->ignoredShapes()) {
39 const int index = shapes.indexOf(shape);
40 if (index >= 0) {
41 shapes.removeAt(index);
42 }
43 }
44
45
46 if (omitEditedShape) {
47 Q_FOREACH (KoPathPoint *point, m_snapGuide->ignoredPathPoints()) {
48 const int index = shapes.indexOf(point->parent());
49 if (index >= 0) {
50 shapes.removeAt(index);
51 }
52 }
53 }
54
55 if (!omitEditedShape && m_snapGuide->additionalEditedShape()) {
57 if (rect.intersects(bound) || rect.contains(bound))
59 }
60 return shapes;
61}
62
64{
65 QList<QPointF> snapPoints;
66 // no snapping to hidden shapes
67 if (! shape->isVisible())
68 return snapPoints;
69
70 // return the special snap points of the shape
71 snapPoints += shape->snapData().snapPoints();
72
73 KoPathShape * path = dynamic_cast<KoPathShape*>(shape);
74 if (path) {
75 QTransform m = path->absoluteTransformation();
76
78
79 int subpathCount = path->subpathCount();
80 for (int subpathIndex = 0; subpathIndex < subpathCount; ++subpathIndex) {
81 int pointCount = path->subpathPointCount(subpathIndex);
82 for (int pointIndex = 0; pointIndex < pointCount; ++pointIndex) {
83 KoPathPoint * p = path->pointByIndex(KoPathPointIndex(subpathIndex, pointIndex));
84 if (! p || ignoredPoints.contains(p))
85 continue;
86
87 snapPoints.append(m.map(p->point()));
88 }
89 }
90 }
91 else
92 {
93 // add the bounding box corners as default snap points
94 QRectF bbox = shape->boundingRect();
95 snapPoints.append(bbox.topLeft());
96 snapPoints.append(bbox.topRight());
97 snapPoints.append(bbox.bottomRight());
98 snapPoints.append(bbox.bottomLeft());
99 }
100
101 return snapPoints;
102}
103
104QList<KoPathSegment> KoSnapProxy::segmentsInRect(const QRectF &rect, bool omitEditedShape)
105{
106
107 QList<KoShape*> shapes = shapesInRect(rect, omitEditedShape);
109
110 QList<KoPathSegment> segments;
111 Q_FOREACH (KoShape * shape, shapes) {
112 QList<KoPathSegment> shapeSegments;
113 QRectF rectOnShape = shape->documentToShape(rect);
114 KoPathShape * path = dynamic_cast<KoPathShape*>(shape);
115 if (path) {
116 shapeSegments = path->segmentsAt(rectOnShape);
117 } else {
118 Q_FOREACH (const KoPathSegment & s, shape->snapData().snapSegments()) {
119 QRectF controlRect = s.controlPointRect();
120 if (! rect.intersects(controlRect) && ! controlRect.contains(rect))
121 continue;
122 QRectF bound = s.boundingRect();
123 if (! rect.intersects(bound) && ! bound.contains(rect))
124 continue;
125 shapeSegments.append(s);
126 }
127 }
128
129 QTransform m = shape->absoluteTransformation();
130 // transform segments to document coordinates
131 Q_FOREACH (const KoPathSegment & s, shapeSegments) {
132 if (ignoredPoints.contains(s.first()) || ignoredPoints.contains(s.second()))
133 continue;
134 segments.append(s.mapped(m));
135 }
136 }
137 return segments;
138}
139
141{
143 QList<KoShape*> filteredShapes;
144 QList<KoShape*> ignoredShapes = m_snapGuide->ignoredShapes();
145
146 // filter all hidden and ignored shapes
147 Q_FOREACH (KoShape * shape, allShapes) {
148 if (shape->isVisible() &&
149 !ignoredShapes.contains(shape) &&
150 !dynamic_cast<KoShapeLayer*>(shape)) {
151
152 filteredShapes.append(shape);
153 }
154 }
155
156 if (omitEditedShape) {
157 Q_FOREACH (KoPathPoint *point, m_snapGuide->ignoredPathPoints()) {
158 const int index = filteredShapes.indexOf(point->parent());
159 if (index >= 0) {
160 filteredShapes.removeAt(index);
161 }
162 }
163 }
164
165 if (!omitEditedShape && m_snapGuide->additionalEditedShape()) {
166 filteredShapes.append(m_snapGuide->additionalEditedShape());
167 }
168
169 return filteredShapes;
170}
171
176
const Params2D p
QPair< int, int > KoPathPointIndex
Definition KoPathShape.h:28
virtual KoShapeManager * shapeManager() const =0
A KoPathPoint represents a point in a path.
KoPathShape * parent() const
Get the path shape the point belongs to.
A KoPathSegment consist of two neighboring KoPathPoints.
KoPathSegment mapped(const QTransform &matrix) const
Returns transformed segment.
QRectF controlPointRect() const
Returns the control point bounding rect.
KoPathPoint * first
KoPathPoint * second
QRectF boundingRect() const
Returns the axis aligned tight bounding rect.
The position of a path point within a path shape.
Definition KoPathShape.h:63
QList< KoShape * > shapes
QList< KoShape * > shapesAt(const QRectF &rect, bool omitHiddenShapes=true, bool containedMode=false)
virtual KoSnapData snapData() const
Returns additional snap data the shape wants to have snapping to.
Definition KoShape.cpp:1259
QPointF documentToShape(const QPointF &point) const
Transforms point from document coordinates to shape coordinates.
Definition KoShape.cpp:1211
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335
QTransform absoluteTransformation() const
Definition KoShape.cpp:382
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:979
QList< KoPathSegment > snapSegments() const
Returns list of segments to snap to.
QList< QPointF > snapPoints() const
Returns list of points to snap to.
QList< KoShape * > ignoredShapes
QList< KoPathPoint * > ignoredPathPoints() const
Returns list of ignored points.
KoShape * additionalEditedShape
KoCanvasBase * canvas
KoSnapGuide * m_snapGuide
Definition KoSnapProxy.h:46
QList< QPointF > pointsFromShape(KoShape *shape)
returns list of points from given shape
QList< KoShape * > shapesInRect(const QRectF &rect, bool omitEditedShape=false)
returns list of shape in given rectangle in document coordinates
KoSnapProxy(KoSnapGuide *snapGuide)
KoCanvasBase * canvas()
returns canvas we are working on
QList< KoPathSegment > segmentsInRect(const QRectF &rect, bool omitEditedShape)
returns list of points in given rectangle in document coordinates
QList< QPointF > pointsInRect(const QRectF &rect, bool omitEditedShape)
returns list of points in given rectangle in document coordinates
QList< KoShape * > shapes(bool omitEditedShape=false)
returns list of all shapes