Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_shape_controller.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2007 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9
10#include <klocalizedstring.h>
11
12#include <KoShape.h>
13#include <KoShapeContainer.h>
14#include <KoShapeManager.h>
15#include <KoCanvasBase.h>
16#include <KoToolManager.h>
17#include <KisView.h>
18#include <KoSelection.h>
19#include <KoShapeLayer.h>
20#include <KoPathShape.h>
22#include <KoCanvasController.h>
23
24#include "kis_node_manager.h"
25#include "kis_shape_selection.h"
26#include "kis_selection.h"
29#include "kis_clone_layer.h"
30#include "canvas/kis_canvas2.h"
31#include "KisDocument.h"
32#include "kis_image.h"
33#include "kis_group_layer.h"
34#include "kis_node_shape.h"
36#include "kis_name_server.h"
37#include "kis_mask.h"
38#include "kis_shape_layer.h"
39#include "KisViewManager.h"
40#include "kis_node.h"
41#include <kis_selection_mask.h>
42#include <KoShapeStroke.h>
43
46#include <kis_undo_adapter.h>
49
51
52
61
62KisShapeController::KisShapeController(KisNameServer *nameServer, KUndo2Stack *undoStack, QObject *parent)
63 : KisDummiesFacadeBase(parent)
64 , m_d(new Private())
65{
66 m_d->nameServer = nameServer;
67 resourceManager()->setUndoStack(undoStack);
68}
69
70
72{
74 if (node) {
76 }
77
78 delete m_d;
79}
80
90
99
101{
102 KisNodeShape *newShape =
103 m_d->shapesGraph.addNode(node, parent, aboveThis);
104 // XXX: what are we going to do with this shape?
105 Q_UNUSED(newShape);
106
107 KisShapeLayer *shapeLayer = dynamic_cast<KisShapeLayer*>(node.data());
108 if (shapeLayer) {
113 connect(shapeLayer, SIGNAL(selectionChanged()),
114 SIGNAL(selectionChanged()));
115 connect(shapeLayer->shapeManager(), SIGNAL(selectionContentChanged()),
116 SIGNAL(selectionContentChanged()));
117 connect(shapeLayer, SIGNAL(currentLayerChanged(const KoShapeLayer*)),
118 SIGNAL(currentLayerChanged(const KoShapeLayer*)));
119 }
120}
121
123{
124 KisShapeLayer *shapeLayer = dynamic_cast<KisShapeLayer*>(node.data());
125 if (shapeLayer) {
126 shapeLayer->disconnect(this);
127 }
128
130}
131
133{
134 return m_d->shapesGraph.containsNode(node);
135}
136
141
146
148{
149 return m_d->shapesGraph.shapesCount();
150}
151static inline bool belongsToShapeSelection(KoShape* shape) {
152 return dynamic_cast<KisShapeSelectionMarker*>(shape->userData());
153}
154
156{
157 KoShapeContainer *resultParent = 0;
158 KisCommandUtils::CompositeCommand *resultCommand =
159 new KisCommandUtils::CompositeCommand(parentCommand);
160
161 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!shapes.isEmpty(), resultParent);
162 Q_FOREACH (KoShape *shape, shapes) {
164 }
165
166 // TODO: think about moving selection to the canvas resources and pass resources instead
167 KisCanvas2 *kisCanvas = dynamic_cast<KisCanvas2*>(canvas);
168 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(kisCanvas, resultParent);
169 KisView *view = kisCanvas ? kisCanvas->imageView() : 0;
170
171 const bool baseBelongsToSelection = belongsToShapeSelection(shapes.first());
172 bool allSameBelongsToShapeSelection = true;
173
174 Q_FOREACH (KoShape *shape, shapes) {
175 allSameBelongsToShapeSelection &= belongsToShapeSelection(shape) == baseBelongsToSelection;
176 }
177
178 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!baseBelongsToSelection || allSameBelongsToShapeSelection, resultParent);
179
180 KisSelectionSP parentSelection;
181
182 if (baseBelongsToSelection && allSameBelongsToShapeSelection) {
183 parentSelection = view->selection();
184 }
185
186 if (KisNodeSP activeNode = view->currentNode()) {
187 if (KisSelectionMask *selectionMask = qobject_cast<KisSelectionMask *>(activeNode.data())) {
188 if (KisSelectionSP selection = selectionMask->selection()) {
189 parentSelection = selection;
190 }
191 }
192 }
193
194 if (parentSelection) {
195 KisSelectionComponent *shapeSelectionComponent = parentSelection->shapeSelection();
196
197 Q_FOREACH (KoShape *shape, shapes) {
198 if (!belongsToShapeSelection(shape)) {
200 }
201 }
202
203 if (!shapeSelectionComponent) {
204 shapeSelectionComponent = new KisShapeSelection(this, parentSelection);
205 resultCommand->addCommand(parentSelection->convertToVectorSelection(shapeSelectionComponent));
206 }
207
208 KisShapeSelection *shapeSelection = static_cast<KisShapeSelection *>(shapeSelectionComponent);
209 resultParent = shapeSelection;
210 }
211
212 if (!resultParent) {
213 Q_FOREACH (KoShape *shape, shapes) {
214 if (belongsToShapeSelection(shape)) {
215 shape->setUserData(nullptr);
216 }
217
225 if ((!shape->stroke() || !shape->stroke()->isVisible()) && !shape->background()) {
226 shape->setStroke(toQShared(new KoShapeStroke(1, Qt::black)));
227 }
228 }
229
230
231 KisShapeLayer *shapeLayer =
232 dynamic_cast<KisShapeLayer*>(
234
235 if (!shapeLayer || forceNewLayer) {
236 shapeLayer = new KisShapeLayer(this, image(),
237 i18n("Vector Layer %1", m_d->nameServer->number()),
239
240 resultCommand->addCommand(
242 shapeLayer,
243 image()->rootLayer(),
244 image()->rootLayer()->childCount()));
245 }
246
247 resultParent = shapeLayer;
248 }
249
250 return resultParent;
251}
252
254{
255 KisImageSP image = this->image();
256 return image ? image->bounds() : QRect(0, 0, 666, 777);
257}
258
260{
261 KisImageSP image = this->image();
262 return image ? image->xRes() * 72.0 : 72.0;
263}
264
266{
267 if (!image()) return;
268
269 KisNodeSP rootNode = image()->root();
270
271 if (m_d->shapesGraph.containsNode(rootNode)) {
272 Q_ASSERT(canvas);
273 Q_ASSERT(canvas->shapeManager());
274 KoSelection *selection = canvas->shapeManager()->selection();
275 if (selection && m_d->shapesGraph.nodeToShape(rootNode)) {
276 selection->select(m_d->shapesGraph.nodeToShape(rootNode));
277 KoToolManager *toolManager = canvas->canvasController()->toolManager();
278 toolManager->switchToolRequested(toolManager->preferredToolForSelection(selection->selectedShapes()));
279 }
280 }
281}
282
284{
286
287 if (image) {
288 m_d->imageConnections.addConnection(image, SIGNAL(sigResolutionChanged(double, double)), this, SLOT(slotUpdateDocumentResolution()));
289 m_d->imageConnections.addConnection(image, SIGNAL(sigSizeChanged(QPointF, QPointF)), this, SLOT(slotUpdateDocumentSize()));
290 }
291
293
296}
297
299{
300 if (node) {
301 return m_d->shapesGraph.nodeToShape(node);
302 }
303 return 0;
304}
305
const quint8 OPACITY_OPAQUE_U8
KoShapeManager shapeManager
QPointer< KisView > imageView() const
void setImage(KisImageWSP image)
The command for adding a layer.
double xRes() const
QRect bounds() const override
KisNodeSP node() const
bool containsNode(KisNodeSP node) const
KisNodeDummy * nodeToDummy(KisNodeSP node)
KisNodeDummy * rootDummy() const
KisNodeShape * addNode(KisNodeSP node, KisNodeSP parent, KisNodeSP aboveThis)
KisNodeShape * nodeToShape(KisNodeSP node)
void removeNode(KisNodeSP node)
void setImage(KisImageWSP image, KisNodeSP activeNode) override
qreal pixelsPerInch() const override
bool hasDummyForNode(KisNodeSP node) const override
KoShapeContainer * createParentForShapes(const QList< KoShape * > shapes, bool forceNewLayer, KoCanvasBase *canvas, KUndo2Command *parentCommand) override
int dummiesCount() const override
void addNodeImpl(KisNodeSP node, KisNodeSP parent, KisNodeSP aboveThis) override
KisNodeDummy * rootDummy() const override
KisShapeController(KisNameServer *nameServer, KUndo2Stack *undoStack, QObject *parent=0)
void setInitialShapeForCanvas(KisCanvas2 *canvas)
void currentLayerChanged(const KoShapeLayer *)
void selectionContentChanged()
QRectF documentRectInPixels() const override
void removeNodeImpl(KisNodeSP node) override
KoShapeLayer * shapeForNode(KisNodeSP layer) const
KisNodeDummy * dummyForNode(KisNodeSP layer) const override
KoShapeManager * shapeManager() const
void addConnection(Sender sender, Signal signal, Receiver receiver, Method method, Qt::ConnectionType type=Qt::AutoConnection)
KisNodeSP currentNode
Definition KisView.cpp:130
KisSelectionSP selection()
Definition KisView.cpp:1349
KoCanvasController * canvasController() const
virtual KoSelectedShapesProxy * selectedShapesProxy() const =0
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
QPointer< KoToolManager > toolManager
void setUndoStack(KUndo2Stack *undoStack)
void setResource(int key, const QVariant &value)
@ DocumentRectInPixels
Bounds of the document in pixels.
@ DocumentResolution
Pixels-per-inch resolution of the document.
virtual KoSelection * selection()=0
void select(KoShape *shape)
const QList< KoShape * > selectedShapes() const
KoShapeLayer * activeLayer() const
virtual KoDocumentResourceManager * resourceManager() const
KoSelection * selection
KoShapeUserData * userData() const
Definition KoShape.cpp:625
virtual KoShapeStrokeModelSP stroke() const
Definition KoShape.cpp:888
KoShapeContainer * parent() const
Definition KoShape.cpp:860
virtual void setStroke(KoShapeStrokeModelSP stroke)
Definition KoShape.cpp:902
void setUserData(KoShapeUserData *userData)
Definition KoShape.cpp:620
virtual QSharedPointer< KoShapeBackground > background() const
Definition KoShape.cpp:754
void switchToolRequested(const QString &id)
QString preferredToolForSelection(const QList< KoShape * > &shapes)
#define KIS_SAFE_ASSERT_RECOVER_BREAK(cond)
Definition kis_assert.h:127
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
QSharedPointer< T > toQShared(T *ptr)
static bool belongsToShapeSelection(KoShape *shape)
KUndo2Command * convertToVectorSelection(KisSelectionComponent *shapeSelection)
converts shape selection into the vector state
KisSelectionComponent * shapeSelection
KisSignalAutoConnectionsStore imageConnections