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&) override
64 {
65 }
66
67 KoToolProxy *toolProxy() const override
68 {
69 return 0;
70 }
71
72 const KoViewConverter *viewConverter() const override
73 {
74 return 0;
75 }
76
78 {
79 return 0;
80 }
81
82 QWidget *canvasWidget() override
83 {
84 return 0;
85 }
86
87 const QWidget *canvasWidget() const override
88 {
89 return 0;
90 }
91
92 KoUnit unit() const override
93 {
94 return KoUnit(KoUnit::Point);
95 }
96
97 void setCursor(const QCursor &) override {}
98
99private:
100 QScopedPointer<KoShapeManager> m_shapeManager;
101 QScopedPointer<KoSelectedShapesProxySimple> m_selectedShapesProxy;
102};
103
104class Q_DECL_HIDDEN KoShapePainter::Private
105{
106public:
108 : canvas(new SimpleCanvas())
109 {
110 }
111
112 ~Private() { delete canvas; }
114};
115
117 : d(new Private())
118{
119}
120
122{
123 delete d;
124}
125
130
131void KoShapePainter::paint(QPainter &painter)
132{
133 foreach (KoShape *shape, d->canvas->shapeManager()->shapes()) {
134 shape->waitUntilReady(false);
135 }
136
137 d->canvas->shapeManager()->paint(painter);
138}
139
140void KoShapePainter::paint(QPainter &painter, const QRect &painterRect, const QRectF &documentRect)
141{
142 if (documentRect.width() == 0.0f || documentRect.height() == 0.0f)
143 return;
144
145 KoViewConverter converter;
146 // calculate the painter destination rectangle size in document coordinates
147 QRectF paintBox = converter.viewToDocument(QRectF(QPointF(), painterRect.size()));
148
149 // compute the zoom factor based on the bounding rects in document coordinates
150 // so that the content fits into the image
151 qreal zoomW = paintBox.width() / documentRect.width();
152 qreal zoomH = paintBox.height() / documentRect.height();
153 qreal zoom = qMin(zoomW, zoomH);
154
155 // now set the zoom into the zoom handler used for painting the shape
156 converter.setZoom(zoom);
157
158 painter.save();
159
160 // initialize painter
161 painter.setPen(QPen(Qt::NoPen));
162 painter.setBrush(Qt::NoBrush);
163 painter.setRenderHint(QPainter::Antialiasing);
164 painter.setClipRect(painterRect.adjusted(-1,-1,1,1));
165
166 // convert document rectangle to view coordinates
167 QRectF zoomedBound = converter.documentToView(documentRect);
168 // calculate offset between painter rectangle and converted document rectangle
169 QPointF offset = QRectF(painterRect).center() - zoomedBound.center();
170 // center content in painter rectangle
171 painter.translate(offset.x(), offset.y());
172 painter.setTransform(converter.documentToView(), true);
173
174 // finally paint the shapes
175 paint(painter);
176
177 painter.restore();
178}
179
180void KoShapePainter::paint(QImage &image)
181{
182 if (image.isNull())
183 return;
184
185 QPainter painter(&image);
186
187 paint(painter, image.rect(), contentRect());
188}
189
191{
192 QRectF bound;
193 foreach (KoShape *shape, d->canvas->shapeManager()->shapes()) {
194 if (!shape->isVisible())
195 continue;
196 if (dynamic_cast<KoShapeGroup*>(shape))
197 continue;
198
199 QRectF shapeRect = shape->boundingRect();
200
201 if (bound.isEmpty())
202 bound = shapeRect;
203 else
204 bound = bound.united(shapeRect);
205 }
206 return bound;
207}
KoShapeManager shapeManager
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.
QList< KoShape * > shapes
void setShapes(const QList< KoShape * > &shapes, Repaint repaint=PaintShapeOnAdd)
void paint(QPainter &painter)
void setShapes(const QList< KoShape * > &shapes)
QRectF contentRect() const
Returns the bounding rect of the shapes to paint.
Private *const d
void paint(QPainter &painter)
SimpleCanvas * canvas
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:979
virtual void waitUntilReady(bool asynchronous=true) const
Definition KoShape.cpp:1160
@ 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
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 &) override
KoViewConverter * viewConverter() override
KoShapeManager * shapeManager() const override
KisCanvas2 * canvas