Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSelection.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2
3 SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
4 SPDX-FileCopyrightText: 2006 Thorsten Zachmann <zachmann@kde.org>
5 SPDX-FileCopyrightText: 2006 Jan Hambrecht <jaham@gmx.net>
6 SPDX-FileCopyrightText: 2006-2007, 2009 Thomas Zander <zander@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "KoSelection.h"
12#include "KoSelection_p.h"
13#include "KoShapeContainer.h"
14#include "KoShapeLayer.h"
15#include "KoShapeGroup.h"
16#include "KoPointerEvent.h"
17#include "kis_algebra_2d.h"
19
20#include <QPainter>
21
23{
25
26 if (type == KoShape::Deleted) {
27 m_parent->d->activeLayer = nullptr;
28 Q_EMIT m_parent->currentLayerChanged(m_parent->d->activeLayer);
29 } else if (type == KoShape::GeometryProtectedChanged) {
30 // we should reemit the signal with the same value
31 // to make sure all the clients see the geometry
32 // lock change
33 Q_EMIT m_parent->currentLayerChanged(m_parent->d->activeLayer);
34 }
35}
36
37#include "kis_debug.h"
39 : QObject(parent)
40 , KoShape()
41 , d(new Private(this))
42{
43 connect(d->selectionChangedCompressor, SIGNAL(timeout()), SIGNAL(selectionChanged()));
44}
45
47 : QObject()
48 , KoShape(rhs)
49 , d(new Private(*rhs.d, this))
50{
51}
52
56
57void KoSelection::paint(QPainter &painter) const
58{
59 Q_UNUSED(painter);
60}
61
62void KoSelection::setSize(const QSizeF &size)
63{
64 Q_UNUSED(size);
65 qWarning() << "WARNING: KoSelection::setSize() should never be used!";
66}
67
68QSizeF KoSelection::size() const
69{
70 return outlineRect().size();
71}
72
74{
75 const QTransform invertedTransform = transformation().inverted();
76 QRectF boundingRect;
77
78 Q_FOREACH (KoShape *shape, selectedVisibleShapes()) {
79 // it is cheaper to invert-transform each outline, than
80 // to group 300+ rotated rectangles into a polygon
82 invertedTransform.map(
83 shape->absoluteTransformation().map(
84 QPolygonF(shape->outlineRect()))).boundingRect();
85 }
86
87 return boundingRect;
88}
89
94
96{
97 KIS_SAFE_ASSERT_RECOVER_RETURN(shape != this);
99
100 if (!shape->isSelectable() || !shape->isVisible()) {
101 return;
102 }
103
104 // check recursively
105 if (isSelected(shape)) {
106 return;
107 }
108
109 // find the topmost parent to select
110 while (KoShapeGroup *parentGroup = dynamic_cast<KoShapeGroup*>(shape->parent())) {
111 if (parentGroup && parentGroup->isSelectable()) {
112 shape = parentGroup;
113 } else {
114 break;
115 }
116 }
117
118 d->selectedShapes << shape;
119 shape->addShapeChangeListener(this);
120
121 if (d->selectedShapes.size() == 1) {
123 } else {
124 setTransformation(QTransform());
125 }
126
127 d->selectionChangedCompressor->start();
128}
129
131{
132 if (!d->selectedShapes.contains(shape))
133 return;
134
135 d->selectedShapes.removeAll(shape);
136 shape->removeShapeChangeListener(this);
137
138 if (d->selectedShapes.size() == 1) {
139 setTransformation(d->selectedShapes.first()->absoluteTransformation());
140 }
141
142 d->selectionChangedCompressor->start();
143}
144
146{
147
148 if (d->selectedShapes.isEmpty())
149 return;
150
151 Q_FOREACH (KoShape *shape, d->selectedShapes) {
152 shape->removeShapeChangeListener(this);
153 }
154
155 // reset the transformation matrix of the selection
156 setTransformation(QTransform());
157
158 d->selectedShapes.clear();
159 d->selectionChangedCompressor->start();
160}
161
163{
164 return d->selectedShapes.size();
165}
166
167bool KoSelection::hitTest(const QPointF &position) const
168{
169
170 Q_FOREACH (KoShape *shape, d->selectedShapes) {
171 if (shape->isVisible()) continue;
172 if (shape->hitTest(position)) return true;
173 }
174
175 return false;
176}
177
179{
180 return d->selectedShapes;
181}
182
184{
186
187 KritaUtils::filterContainer (shapes, [](KoShape *shape) {
188 return shape->isVisible();
189 });
190
191 return shapes;
192}
193
195{
197
198 KritaUtils::filterContainer (shapes, [](KoShape *shape) {
199 return shape->isShapeEditable();
200 });
201
202 return shapes;
203}
204
206{
207 QList<KoShape*> shapes;
208 Q_FOREACH (KoShape *shape, selectedShapes()) {
209 QSet<KoShape *> delegates = shape->toolDelegates();
210 if (delegates.isEmpty()) {
211 shapes.append(shape);
212 } else {
213 Q_FOREACH (KoShape *delegatedShape, delegates) {
214 shapes.append(delegatedShape);
215 }
216 }
217 }
218 return shapes;
219}
220
221bool KoSelection::isSelected(const KoShape *shape) const
222{
223 if (shape == this)
224 return true;
225
226 const KoShape *tmpShape = shape;
227 while (tmpShape && std::find(d->selectedShapes.begin(), d->selectedShapes.end(), tmpShape) == d->selectedShapes.end()) {
228 tmpShape = tmpShape->parent();
229 }
230
231 return tmpShape;
232}
233
235{
236 return !d->selectedShapes.isEmpty() ? d->selectedShapes.first() : 0;
237}
238
240{
241 const bool layerChanged = d->activeLayer != layer;
242
243 if (d->activeLayer && layerChanged) {
244 d->activeLayer->removeShapeChangeListener(&d->activeLayerStateListener);
245 }
246
247 d->activeLayer = layer;
248
249 if (d->activeLayer && layerChanged) {
250 d->activeLayer->addShapeChangeListener(&d->activeLayerStateListener);
251 }
252
253 Q_EMIT currentLayerChanged(layer);
254}
255
257{
258 return d->activeLayer;
259}
260
262{
263 Q_UNUSED(shape);
264 if (type == KoShape::Deleted) {
265 deselect(shape);
266
267 // HACK ALERT: the caller will also remove the listener, which was
268 // removed in deselect(), so re-add it here
269 shape->addShapeChangeListener(this);
270 }
271}
void notifyShapeChanged(KoShape::ChangeType type, KoShape *shape) override
const QList< KoShape * > selectedVisibleShapes() const
QRectF outlineRect() const override
KoSelection(QObject *parent=0)
bool isSelected(const KoShape *shape) const
return true if the shape is selected
bool hitTest(const QPointF &position) const override
Check if the shape is hit on position.
void deselectAll()
clear the selections list
QSizeF size() const override
Get the size of the shape in pt.
QRectF boundingRect() const override
Get the bounding box of the shape.
QScopedPointer< Private > d
~KoSelection() override
void setActiveLayer(KoShapeLayer *layer)
void currentLayerChanged(const KoShapeLayer *layer)
emitted when the current layer is changed
void selectionChanged()
emitted when the selection is changed
void deselect(KoShape *shape)
void setSize(const QSizeF &size) override
Resize the shape.
const QList< KoShape * > selectedEditableShapesAndDelegates() const
void select(KoShape *shape)
int count() const
return the selection count, i.e. the number of all selected shapes
void paint(QPainter &painter) const override
Paint the shape fill The class extending this one is responsible for painting itself....
void notifyShapeChanged(ChangeType type, KoShape *shape) override
const QList< KoShape * > selectedEditableShapes() const
KoShape * firstSelectedShape() const
const QList< KoShape * > selectedShapes() const
KoShapeLayer * activeLayer() const
virtual QRectF outlineRect() const
Definition KoShape.cpp:561
void addShapeChangeListener(ShapeChangeListener *listener)
Definition KoShape.cpp:1155
bool isSelectable() const
Definition KoShape.cpp:832
void removeShapeChangeListener(ShapeChangeListener *listener)
Definition KoShape.cpp:1163
virtual bool isShapeEditable(bool recursive=true) const
checks recursively if the shape or one of its parents is not visible or locked
Definition KoShape.cpp:968
QSet< KoShape * > toolDelegates() const
Definition KoShape.cpp:1105
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:300
KoShapeContainer * parent() const
Definition KoShape.cpp:860
QTransform absoluteTransformation() const
Definition KoShape.cpp:330
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:369
ChangeType
Used by shapeChanged() to select which change was made.
Definition KoShape.h:92
@ GeometryProtectedChanged
used after a setPosition()
Definition KoShape.h:113
@ Deleted
the shape was deleted
Definition KoShape.h:101
QTransform transformation() const
Returns the shapes local transformation matrix.
Definition KoShape.cpp:378
virtual bool hitTest(const QPointF &position) const
Check if the shape is hit on position.
Definition KoShape.cpp:281
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:797
QPointF position() const
Get the position of the shape in pt.
Definition KoShape.cpp:740
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
auto filterContainer(C &container, KeepIfFunction keepIf) -> decltype(bool(keepIf(container[0])), void())