Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShapePainter.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 *
3 * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
4 * SPDX-FileCopyrightText: 2009 Thomas Zander <zander@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#include "KoShapePainter.h"
10
11#include "KoCanvasBase.h"
13#include "KoShapeManager.h"
14#include "KoShape.h"
15#include "KoShapeStrokeModel.h"
16#include "KoShapeGroup.h"
17#include "KoShapeContainer.h"
18#include <KoViewConverter.h>
19#include <KoUnit.h>
20
21#include <QPainter>
22#include <QImage>
23
25{
26public:
33
34 ~SimpleCanvas() override
35 {
36 }
37
38 void gridSize(QPointF *offset, QSizeF *spacing) const override
39 {
40 *offset = QPointF();
41 *spacing = QSizeF();
42 };
43
44 bool snapToGrid() const override
45 {
46 return false;
47 }
48
49 void addCommand(KUndo2Command *) override
50 {
51 }
52
53 KoShapeManager *shapeManager() const override
54 {
55 return m_shapeManager.data();
56 }
57
59 {
60 return m_selectedShapesProxy.data();
61 }
62
63 void updateCanvas(const QRectF&rect) override
64 {
66 }
67
68 KoToolProxy *toolProxy() const override
69 {
70 return 0;
71 }
72
73 const KoViewConverter *viewConverter() const override
74 {
75 return 0;
76 }
77
79 {
80 return 0;
81 }
82
83 QWidget *canvasWidget() override
84 {
85 return 0;
86 }
87
88 const QWidget *canvasWidget() const override
89 {
90 return 0;
91 }
92
93 KoUnit unit() const override
94 {
95 return KoUnit(KoUnit::Point);
96 }
97
98 void setCursor(const QCursor &) override {}
99
100 void setUpdateFunction(std::function<void(const QRectF&)> function) {
101 m_updateFunc = function;
102 }
103
104private:
105 QScopedPointer<KoShapeManager> m_shapeManager;
106 QScopedPointer<KoSelectedShapesProxySimple> m_selectedShapesProxy;
107 std::function<void(const QRectF&)> m_updateFunc;
108};
109
110class Q_DECL_HIDDEN KoShapePainter::Private
111{
112public:
114 : canvas(new SimpleCanvas())
115 {
116 }
117
118 ~Private() { delete canvas; }
120};
121
123 : d(new Private())
124{
125}
126
130
132{
133 d->canvas->shapeManager()->setShapes(shapes, KoShapeManager::AddWithoutRepaint);
134}
135
136void KoShapePainter::paint(QPainter &painter)
137{
138 foreach (KoShape *shape, d->canvas->shapeManager()->shapes()) {
139 shape->waitUntilReady(false);
140 }
141
142 d->canvas->shapeManager()->paint(painter);
143}
144
145void KoShapePainter::paint(QPainter &painter, const QRect &painterRect, const QRectF &documentRect)
146{
147 if (documentRect.width() == 0.0f || documentRect.height() == 0.0f)
148 return;
149
150 KoViewConverter converter;
151 // calculate the painter destination rectangle size in document coordinates
152 QRectF paintBox = converter.viewToDocument(QRectF(QPointF(), painterRect.size()));
153
154 // compute the zoom factor based on the bounding rects in document coordinates
155 // so that the content fits into the image
156 qreal zoomW = paintBox.width() / documentRect.width();
157 qreal zoomH = paintBox.height() / documentRect.height();
158 qreal zoom = qMin(zoomW, zoomH);
159
160 // now set the zoom into the zoom handler used for painting the shape
161 converter.setZoom(zoom);
162
163 painter.save();
164
165 // initialize painter
166 painter.setPen(QPen(Qt::NoPen));
167 painter.setBrush(Qt::NoBrush);
168 painter.setRenderHint(QPainter::Antialiasing);
169 painter.setClipRect(painterRect.adjusted(-1,-1,1,1));
170
171 // convert document rectangle to view coordinates
172 QRectF zoomedBound = converter.documentToView(documentRect);
173 // calculate offset between painter rectangle and converted document rectangle
174 QPointF offset = QRectF(painterRect).center() - zoomedBound.center();
175 // center content in painter rectangle
176 painter.translate(offset.x(), offset.y());
177 painter.setTransform(converter.documentToView(), true);
178
179 // finally paint the shapes
180 paint(painter);
181
182 painter.restore();
183}
184
185void KoShapePainter::paint(QImage &image)
186{
187 if (image.isNull())
188 return;
189
190 QPainter painter(&image);
191
192 paint(painter, image.rect(), contentRect());
193}
194
196{
197 QRectF bound;
198 foreach (KoShape *shape, d->canvas->shapeManager()->shapes()) {
199 if (!shape->isVisible())
200 continue;
201 if (dynamic_cast<KoShapeGroup*>(shape))
202 continue;
203
204 QRectF shapeRect = shape->boundingRect();
205
206 if (bound.isEmpty())
207 bound = shapeRect;
208 else
209 bound = bound.united(shapeRect);
210 }
211 return bound;
212}
213
218
219void KoShapePainter::setUpdateFunction(std::function<void (const QRectF &)> function)
220{
221 d->canvas->setUpdateFunction(function);
222}
virtual KoShapeManager * shapeManager() const =0
The KoSelectedShapesProxy class is a special interface of KoCanvasBase to have a stable connection to...
@ AddWithoutRepaint
Avoids each shapes 'update()' to be called for faster addition when its possible.
KoCanvasBase * canvas
void setShapes(const QList< KoShape * > &shapes)
QRectF contentRect() const
Returns the bounding rect of the shapes to paint.
KoShapeManager * internalShapeManager() const
internalShapeManager KoShapePainter has an internal shape manager that is used to paint the shapes.
void paint(QPainter &painter)
void setUpdateFunction(std::function< void(const QRectF &)> function)
QScopedPointer< Private > d
SimpleCanvas * canvas
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:299
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:802
virtual void waitUntilReady(bool asynchronous=true) const
Definition KoShape.cpp:965
@ Point
Postscript point, 1/72th of an Inco.
Definition KoUnit.h:76
virtual void setZoom(qreal zoom)
virtual QPointF viewToDocument(const QPointF &viewPoint) const
virtual QPointF documentToView(const QPointF &documentPoint) const
QWidget * canvasWidget() override
KoSelectedShapesProxy * selectedShapesProxy() const override
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
const KoViewConverter * viewConverter() const override
void gridSize(QPointF *offset, QSizeF *spacing) const override
bool snapToGrid() const override
void setCursor(const QCursor &) override
void addCommand(KUndo2Command *) override
void setUpdateFunction(std::function< void(const QRectF &)> function)
QScopedPointer< KoShapeManager > m_shapeManager
const QWidget * canvasWidget() const override
KoToolProxy * toolProxy() const override
KoUnit unit() const override
QScopedPointer< KoSelectedShapesProxySimple > m_selectedShapesProxy
~SimpleCanvas() override
void updateCanvas(const QRectF &rect) override
std::function< void(const QRectF &) m_updateFunc)
KoViewConverter * viewConverter() override
KoShapeManager * shapeManager() const override
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)