17#include "canvaspainter.h"
22#include "layerbitmap.h"
23#include "layervector.h"
24#include "bitmapimage.h"
26#include "tiledbuffer.h"
27#include "vectorimage.h"
29#include "painterutils.h"
31CanvasPainter::CanvasPainter(
QPixmap& canvas) : mCanvas(canvas)
36CanvasPainter::~CanvasPainter()
40void CanvasPainter::reset()
59 if (mViewTransform != view || mViewInverse != viewInverse) {
60 mViewTransform = view;
61 mViewInverse = viewInverse;
65void CanvasPainter::setTransformedSelection(
QRect selection,
QTransform transform)
68 if (selection.
width() > 0 && selection.
height() > 0)
70 mSelection = selection;
71 mSelectionTransform = transform;
72 mRenderTransform =
true;
77 ignoreTransformedSelection();
81void CanvasPainter::ignoreTransformedSelection()
83 mRenderTransform =
false;
84 mSelectionTransform.
reset();
88void CanvasPainter::paintCached(
const QRect& blitRect)
90 if (!mPreLayersPixmapCacheValid)
94 renderPreLayers(preLayerPainter, blitRect);
95 preLayerPainter.
end();
96 mPreLayersPixmapCacheValid =
true;
102 mainPainter.
drawPixmap(mPointZero, mPreLayersPixmap);
105 paintCurrentFrame(mainPainter, blitRect, mCurrentLayerIndex, mCurrentLayerIndex);
107 if (!mPostLayersPixmapCacheValid)
111 renderPostLayers(postLayerPainter, blitRect);
112 postLayerPainter.
end();
113 mPostLayersPixmapCacheValid =
true;
117 mainPainter.
drawPixmap(mPointZero, mPostLayersPixmap);
121void CanvasPainter::resetLayerCache()
123 mPreLayersPixmapCacheValid =
false;
124 mPostLayersPixmapCacheValid =
false;
129 painter.
begin(&device);
145void CanvasPainter::renderPreLayers(
QPainter& painter,
const QRect& blitRect)
147 if (mOptions.eLayerVisibility != LayerVisibility::CURRENTONLY || mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA)
152 paintOnionSkin(painter, blitRect);
156void CanvasPainter::renderPostLayers(
QPainter& painter,
const QRect& blitRect)
158 if (mOptions.eLayerVisibility != LayerVisibility::CURRENTONLY || mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA)
160 paintCurrentFrame(painter, blitRect, mCurrentLayerIndex + 1, mObject->getLayerCount() - 1);
164void CanvasPainter::setPaintSettings(
const Object*
object,
int currentLayer,
int frame,
TiledBuffer* tiledBuffer)
169 CANVASPAINTER_LOG(
"Set CurrentLayerIndex = %d", currentLayer);
170 mCurrentLayerIndex = currentLayer;
171 mFrameNumber = frame;
172 mTiledBuffer = tiledBuffer;
175void CanvasPainter::paint(
const QRect& blitRect)
184 renderPreLayers(preLayerPainter, blitRect);
185 preLayerPainter.
end();
188 mainPainter.
drawPixmap(mPointZero, mPreLayersPixmap);
191 paintCurrentFrame(mainPainter, blitRect, mCurrentLayerIndex, mCurrentLayerIndex);
194 renderPostLayers(postLayerPainter, blitRect);
195 postLayerPainter.
end();
198 mainPainter.
drawPixmap(mPointZero, mPostLayersPixmap);
201 mPreLayersPixmapCacheValid =
true;
202 mPostLayersPixmapCacheValid =
true;
205void CanvasPainter::paintOnionSkinOnLayer(
QPainter& painter,
const QRect& blitRect,
Layer* layer)
207 mOnionSkinSubPainter.paint(painter, layer, mOnionSkinPainterOptions, mFrameNumber, [&] (OnionSkinPaintState state,
int onionFrameNumber) {
208 if (state == OnionSkinPaintState::PREV) {
209 switch (layer->type())
211 case Layer::BITMAP: { paintBitmapOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizePrevFrames); break; }
212 case Layer::VECTOR: { paintVectorOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizePrevFrames); break; }
216 if (state == OnionSkinPaintState::NEXT) {
217 switch (layer->type())
219 case Layer::BITMAP: { paintBitmapOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizeNextFrames); break; }
220 case Layer::VECTOR: { paintVectorOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizeNextFrames); break; }
227void CanvasPainter::paintOnionSkin(
QPainter& painter,
const QRect& blitRect)
229 if (!mOptions.bOnionSkinMultiLayer || mOptions.eLayerVisibility == LayerVisibility::CURRENTONLY) {
230 Layer* layer = mObject->getLayer(mCurrentLayerIndex);
231 paintOnionSkinOnLayer(painter, blitRect, layer);
233 for (
int i = 0; i < mObject->getLayerCount(); i++) {
234 Layer* layer = mObject->getLayer(i);
235 if (layer ==
nullptr) {
continue; }
237 paintOnionSkinOnLayer(painter, blitRect, layer);
242void CanvasPainter::paintBitmapOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
246 BitmapImage* bitmapImage = bitmapLayer->getBitmapImageAtFrame(nFrame);
248 if (bitmapImage ==
nullptr) {
return; }
249 bitmapImage->loadFile();
254 onionSkinPainter.
drawImage(bitmapImage->topLeft(), *bitmapImage->image());
255 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, bitmapImage->getOpacity());
258void CanvasPainter::paintVectorOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
262 CANVASPAINTER_LOG(
"Paint Onion skin vector, Frame = %d", nFrame);
263 VectorImage* vectorImage = vectorLayer->getVectorImageAtFrame(nFrame);
264 if (vectorImage ==
nullptr) {
return; }
269 vectorImage->
paintImage(onionSkinPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
270 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, vectorImage->getOpacity());
273void CanvasPainter::paintOnionSkinFrame(
QPainter& painter,
QPainter& onionSkinPainter,
int nFrame,
bool colorize, qreal frameOpacity)
283 if (nFrame < mFrameNumber)
287 else if (nFrame > mFrameNumber)
294 onionSkinPainter.
setBrush(colorBrush);
297 painter.
drawPixmap(mPointZero, mOnionSkinPixmap);
300void CanvasPainter::paintCurrentBitmapFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
303 BitmapImage* paintedImage = bitmapLayer->getLastBitmapImageAtFrame(mFrameNumber);
305 if (paintedImage ==
nullptr) {
return; }
306 paintedImage->loadFile();
308 const bool isDrawing = mTiledBuffer && !mTiledBuffer->bounds().
isEmpty();
315 currentBitmapPainter.
setOpacity(paintedImage->getOpacity() - (1.0-painter.
opacity()));
316 currentBitmapPainter.
drawImage(paintedImage->topLeft(), *paintedImage->image());
318 if (isCurrentLayer && isDrawing)
321 const auto tiles = mTiledBuffer->tiles();
322 for (
const Tile* tile : tiles) {
323 currentBitmapPainter.
drawPixmap(tile->posF(), tile->pixmap());
328 Q_ASSERT(!isDrawing || mSelectionTransform.
isIdentity());
329 if (isCurrentLayer && mRenderTransform && !isDrawing) {
330 paintTransformedSelection(currentBitmapPainter, paintedImage, mSelection);
333 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
336void CanvasPainter::paintCurrentVectorFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
339 VectorImage* vectorImage = vectorLayer->getLastVectorImageAtFrame(mFrameNumber);
340 if (vectorImage ==
nullptr)
348 const bool isDrawing = mTiledBuffer->
isValid();
350 if (mRenderTransform) {
356 currentVectorPainter.
setOpacity(vectorImage->getOpacity() - (1.0-painter.
opacity()));
357 vectorImage->
paintImage(currentVectorPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
359 if (isCurrentLayer && isDrawing) {
362 const auto tiles = mTiledBuffer->tiles();
363 for (
const Tile* tile : tiles) {
364 currentVectorPainter.
drawPixmap(tile->posF(), tile->pixmap());
372 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
375void CanvasPainter::paintTransformedSelection(
QPainter& painter,
BitmapImage* bitmapImage,
const QRect& selection)
const
378 if (selection.
width() == 0 && selection.
height() == 0)
384 QPainter imagePainter(&transformedPixmap);
385 imagePainter.translate(-selection.
topLeft());
386 imagePainter.drawImage(bitmapImage->topLeft(), *bitmapImage->image());
401 painter.
setTransform(mSelectionTransform*mViewTransform);
404 painter.
drawPixmap(selection, transformedPixmap);
418 bool isCameraLayer = mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA;
420 for (
int i = startLayer; i <= endLayer; ++i)
422 Layer* layer = mObject->getLayer(i);
424 if (!layer->visible())
427 if (mOptions.eLayerVisibility == LayerVisibility::RELATED && !isCameraLayer)
429 painter.
setOpacity(calculateRelativeOpacityForLayer(mCurrentLayerIndex, i, mOptions.fLayerVisibilityThreshold));
431 bool isCurrentLayer = mCurrentLayerIndex == i;
433 CANVASPAINTER_LOG(
" Render Layer[%d] %s", i, layer->name());
434 switch (layer->type())
436 case Layer::BITMAP: { paintCurrentBitmapFrame(painter, blitRect, layer, isCurrentLayer);
break; }
437 case Layer::VECTOR: { paintCurrentVectorFrame(painter, blitRect, layer, isCurrentLayer);
break; }
void initializePainter(QPainter &painter, QPaintDevice &device, const QRect &blitRect)
CanvasPainter::initializePainter Enriches the painter with a context and sets it's initial matrix.
void paintCurrentFrame(QPainter &painter, const QRect &blitRect, int startLayer, int endLayer)
Paints layers within the specified range for the current frame.
bool isValid() const
Returns true if there are any tiles, otherwise false.
void paintImage(QPainter &painter, const Object &object, bool simplified, bool showThinCurves, bool antialiasing)
VectorImage::paintImage.
void setSelectionTransformation(QTransform transform)
VectorImage::setSelectionTransformation.
qreal devicePixelRatioF() const const
bool begin(QPaintDevice *device)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void drawRect(const QRectF &rectangle)
void fillRect(const QRectF &rectangle, const QBrush &brush)
qreal opacity() const const
void setBrush(const QBrush &brush)
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
void setCompositionMode(QPainter::CompositionMode mode)
void setOpacity(qreal opacity)
void setWorldMatrixEnabled(bool enable)
QRect viewport() const const
void fill(const QColor &color)
void setDevicePixelRatio(qreal scaleFactor)
bool isEmpty() const const
QPoint topLeft() const const
QTextStream & reset(QTextStream &stream)