Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShapePainter Class Reference

#include <KoShapePainter.h>

+ Inheritance diagram for KoShapePainter:

Public Member Functions

QRectF contentRect () const
 Returns the bounding rect of the shapes to paint.
 
KoShapeManagerinternalShapeManager () const
 internalShapeManager KoShapePainter has an internal shape manager that is used to paint the shapes.
 
 KoShapePainter ()
 
void paint (QImage &image)
 
void paint (QPainter &painter)
 
void paint (QPainter &painter, const QRect &painterRect, const QRectF &documentRect)
 
 Private ()
 
void setShapes (const QList< KoShape * > &shapes)
 
void setUpdateFunction (std::function< void(const QRectF &)> function)
 
 ~KoShapePainter ()
 
 ~Private ()
 

Public Attributes

SimpleCanvascanvas
 

Private Attributes

QScopedPointer< Privated
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

A utility class to paint a subset of shapes onto a QPainter. Notice that using setShapes repeatedly is very expensive, as it populates the shapeManager and all its caching every time. If at all possible use a shapeManager directly and avoid losing the cache between usages.

Definition at line 110 of file KoShapePainter.cpp.

Constructor & Destructor Documentation

◆ ~Private()

KoShapePainter::~Private ( )
inline

Definition at line 118 of file KoShapePainter.cpp.

118{ delete canvas; }
SimpleCanvas * canvas

◆ KoShapePainter()

KoShapePainter::KoShapePainter ( )
explicit

Definition at line 122 of file KoShapePainter.cpp.

123 : d(new Private())
124{
125}
QScopedPointer< Private > d

◆ ~KoShapePainter()

KoShapePainter::~KoShapePainter ( )

Definition at line 127 of file KoShapePainter.cpp.

128{
129}

Member Function Documentation

◆ contentRect()

QRectF KoShapePainter::contentRect ( ) const

Returns the bounding rect of the shapes to paint.

Definition at line 195 of file KoShapePainter.cpp.

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}
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

References KoShape::boundingRect(), d, and KoShape::isVisible().

◆ internalShapeManager()

KoShapeManager * KoShapePainter::internalShapeManager ( ) const

internalShapeManager KoShapePainter has an internal shape manager that is used to paint the shapes.

Returns
the internal shape manager.

Definition at line 214 of file KoShapePainter.cpp.

215{
216 return d->canvas->shapeManager();
217}

References KoShapeManager::canvas, d, and KoCanvasBase::shapeManager().

◆ paint() [1/3]

void KoShapePainter::paint ( QImage & image)

Paints shapes to the given image, so that all shapes fit onto it.

Parameters
imagethe image to paint into
Returns
false if image is empty, else true

Definition at line 185 of file KoShapePainter.cpp.

186{
187 if (image.isNull())
188 return;
189
190 QPainter painter(&image);
191
192 paint(painter, image.rect(), contentRect());
193}
QRectF contentRect() const
Returns the bounding rect of the shapes to paint.
void paint(QPainter &painter)

References contentRect(), and paint().

◆ paint() [2/3]

void KoShapePainter::paint ( QPainter & painter)

Paints the shapes on the given painter and using the zoom handler.

Parameters
painterthe painter to paint on
converterthe view converter defining the zoom to use

Definition at line 136 of file KoShapePainter.cpp.

137{
138 foreach (KoShape *shape, d->canvas->shapeManager()->shapes()) {
139 shape->waitUntilReady(false);
140 }
141
142 d->canvas->shapeManager()->paint(painter);
143}
virtual void waitUntilReady(bool asynchronous=true) const
Definition KoShape.cpp:965

References d, and KoShape::waitUntilReady().

◆ paint() [3/3]

void KoShapePainter::paint ( QPainter & painter,
const QRect & painterRect,
const QRectF & documentRect )

Paints the shapes on the given painter. The given document rectangle is painted to fit into the given painter rectangle.

Parameters
painterthe painter to paint on
painterRectthe destination rectangle on the painter
documentRectthe document region to paint

Definition at line 145 of file KoShapePainter.cpp.

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}
virtual void setZoom(qreal zoom)
virtual QPointF viewToDocument(const QPointF &viewPoint) const
virtual QPointF documentToView(const QPointF &documentPoint) const
QAction * zoom(const QObject *recvr, const char *slot, QObject *parent)

References KoViewConverter::documentToView(), paint(), KoViewConverter::setZoom(), and KoViewConverter::viewToDocument().

◆ Private()

KoShapePainter::Private ( )
inline

Definition at line 113 of file KoShapePainter.cpp.

114 : canvas(new SimpleCanvas())
115 {
116 }

◆ setShapes()

void KoShapePainter::setShapes ( const QList< KoShape * > & shapes)

Sets the shapes to be painted.

Parameters
shapesthe shapes to paint

Definition at line 131 of file KoShapePainter.cpp.

132{
133 d->canvas->shapeManager()->setShapes(shapes, KoShapeManager::AddWithoutRepaint);
134}
@ AddWithoutRepaint
Avoids each shapes 'update()' to be called for faster addition when its possible.

References KoShapeManager::AddWithoutRepaint, and d.

◆ setUpdateFunction()

void KoShapePainter::setUpdateFunction ( std::function< void(const QRectF &)> function)

Definition at line 219 of file KoShapePainter.cpp.

220{
221 d->canvas->setUpdateFunction(function);
222}

References KoShapeManager::canvas, and d.

Member Data Documentation

◆ canvas

SimpleCanvas* KoShapePainter::canvas

Definition at line 119 of file KoShapePainter.cpp.

◆ d

QScopedPointer<Private> KoShapePainter::d
private

Definition at line 77 of file KoShapePainter.h.


The documentation for this class was generated from the following files: