Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShapeController.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 *
3 * SPDX-FileCopyrightText: 2006-2007, 2010 Thomas Zander <zander@kde.org>
4 * SPDX-FileCopyrightText: 2006-2008 Thorsten Zachmann <zachmann@kde.org>
5 * SPDX-FileCopyrightText: 2011 Jan Hambrecht <jaham@gmx.net>
6 *
7 * SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10#include "KoShapeController.h"
12#include "KoShapeRegistry.h"
14#include "KoShapeManager.h"
15#include "KoShapeLayer.h"
16#include "KoSelection.h"
19#include "KoCanvasBase.h"
21#include "KoShapeFactoryBase.h"
22#include "KoShape.h"
23#include <KoUnit.h>
24
25#include <QObject>
26
27#include <kpagedialog.h>
28#include <klocalizedstring.h>
29
31{
32public:
34 : canvas(0),
36 {
37 }
38
41
42 KUndo2Command* addShape(KoShape *shape, bool showDialog, KoShapeContainer *parentShape, KUndo2Command *parent) {
43
44 if (canvas) {
45 if (showDialog && !shape->shapeId().isEmpty()) {
47 Q_ASSERT(factory);
48 qint16 z = 0;
49 Q_FOREACH (KoShape *sh, canvas->shapeManager()->shapes()) {
50 z = qMax(z, sh->zIndex());
51 }
52 shape->setZIndex(z + 1);
53
54 // show config dialog.
55 KPageDialog *dialog = new KPageDialog(canvas->canvasWidget());
56 dialog->setWindowTitle(i18n("%1 Options", factory->name()));
57
58 int pageCount = 0;
60 Q_FOREACH (KoShapeConfigWidgetBase* panel, factory->createShapeOptionPanels()) {
61 if (! panel->showOnShapeCreate())
62 continue;
63 panel->open(shape);
64 panel->connect(panel, SIGNAL(accept()), dialog, SLOT(accept()));
65 widgets.append(panel);
67 panel->setUnit(canvas->unit());
68 QString title = panel->windowTitle().isEmpty() ? panel->objectName() : panel->windowTitle();
69 dialog->addPage(panel, title);
70 pageCount ++;
71 }
72
73 if (pageCount > 0) {
74 if (pageCount > 1)
75 dialog->setFaceType(KPageDialog::Tabbed);
76 if (dialog->exec() != KPageDialog::Accepted) {
77 delete dialog;
78 return 0;
79 }
80 Q_FOREACH (KoShapeConfigWidgetBase *widget, widgets)
81 widget->save();
82 }
83 delete dialog;
84 }
85 }
86
87 return addShapesDirect({shape}, parentShape, parent);
88 }
89
91 {
92 KUndo2Command *resultCommand = 0;
93
94 if (!parentShape) {
95 resultCommand = new KUndo2Command(parent);
96 parentShape = shapeController->createParentForShapes(shapes, false, resultCommand);
97 KUndo2Command *addShapeCommand = new KoShapeCreateCommand(shapeController, shapes, parentShape, resultCommand);
98 resultCommand->setText(addShapeCommand->text());
99 } else {
100 resultCommand = new KoShapeCreateCommand(shapeController, shapes, parentShape, parent);
101 }
102
103 return resultCommand;
104 }
105};
106
108 : d(new Private())
109{
110 d->canvas = canvas;
111 d->shapeController = shapeController;
112}
113
118
120{
121 d->canvas = 0;
122 d->shapeController = 0;
123}
124
126{
127 return d->addShape(shape, true, parentShape, parent);
128}
129
131{
132 return d->addShapesDirect({shape}, parentShape, parent);
133}
134
136{
137 return d->addShapesDirect(shapes, parentShape, parent);
138}
139
141{
142 return removeShapes({shape}, parent);
143}
144
146{
147 KUndo2Command *cmd = new KoShapeDeleteCommand(d->shapeController, shapes, parent);
148 return cmd;
149}
150
152{
153 d->shapeController = shapeController;
154}
155
157{
158 return d->shapeController ? d->shapeController->documentRectInPixels() : QRectF(0,0,1920,1080);
159}
160
162{
163 return d->shapeController ? d->shapeController->pixelsPerInch() : 72.0;
164}
165
170
172{
173 if (!d->shapeController) {
174 qWarning() << "THIS IS NOT GOOD!";
175 return 0;
176 }
178}
179
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
virtual KoShapeManager * shapeManager() const =0
virtual KoUnit unit() const =0
virtual QWidget * canvasWidget()=0
QPointer< KoCanvasResourceProvider > resourceManager
const T value(const QString &id) const
virtual void open(KoShape *shape)=0
void setResourceManager(KoCanvasResourceProvider *rm)
called to set the canvas resource manager of the canvas the user used to insert the new shape.
virtual void setUnit(const KoUnit &unit)
virtual bool showOnShapeCreate()
Return true if the shape config panel should be shown after the shape is created.
virtual void save()=0
virtual qreal pixelsPerInch() const =0
virtual KoShapeContainer * createParentForShapes(const QList< KoShape * > shapes, bool forceNewLayer, KUndo2Command *parentCommand)
virtual QRectF documentRectInPixels() const =0
virtual KoDocumentResourceManager * resourceManager() const
KUndo2Command * addShapesDirect(const QList< KoShape * > shapes, KoShapeContainer *parentShape, KUndo2Command *parent)
KoShapeControllerBase * shapeController
KUndo2Command * addShape(KoShape *shape, bool showDialog, KoShapeContainer *parentShape, KUndo2Command *parent)
KUndo2Command * addShape(KoShape *shape, KoShapeContainer *parentShape, KUndo2Command *parent=0)
Add a shape to the document. If the shape has no parent, the active layer will become its parent.
QRectF documentRect() const
KoDocumentResourceManager * resourceManager() const
void setShapeControllerBase(KoShapeControllerBase *shapeController)
Set the KoShapeControllerBase used to add/remove shapes.
KoShapeController(KoCanvasBase *canvas, KoShapeControllerBase *shapeController)
KUndo2Command * addShapeDirect(KoShape *shape, KoShapeContainer *parentShape, KUndo2Command *parent=0)
Add a shape to the document, skipping any dialogs or other user interaction.
void reset()
reset sets the canvas and shapebased document to 0.
~KoShapeController() override
destructor
KUndo2Command * removeShape(KoShape *shape, KUndo2Command *parent=0)
Remove a shape from the document.
KoShapeControllerBase * documentBase() const
Returns the KoShapeControllerBase used to add/remove shapes.
qreal pixelsPerInch() const
QRectF documentRectInPixels() const
KUndo2Command * addShapesDirect(const QList< KoShape * > shape, KoShapeContainer *parentShape, KUndo2Command *parent=0)
Add shapes to the document, skipping any dialogs or other user interaction.
KUndo2Command * removeShapes(const QList< KoShape * > &shapes, KUndo2Command *parent=0)
The undo / redo command for creating shapes.
The undo / redo command for deleting shapes.
virtual QList< KoShapeConfigWidgetBase * > createShapeOptionPanels()
QList< KoShape * > shapes
static KoShapeRegistry * instance()
void setZIndex(qint16 zIndex)
Definition KoShape.cpp:954
QString shapeId() const
Definition KoShape.cpp:1057
qint16 zIndex() const
Definition KoShape.cpp:600