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;
86void CanvasPainter::paintCached(
const QRect& blitRect)
88 if (!mPreLayersPixmapCacheValid)
92 renderPreLayers(preLayerPainter, blitRect);
93 preLayerPainter.
end();
94 mPreLayersPixmapCacheValid =
true;
100 mainPainter.
drawPixmap(mPointZero, mPreLayersPixmap);
103 paintCurrentFrame(mainPainter, blitRect, mCurrentLayerIndex, mCurrentLayerIndex);
105 if (!mPostLayersPixmapCacheValid)
109 renderPostLayers(postLayerPainter, blitRect);
110 postLayerPainter.
end();
111 mPostLayersPixmapCacheValid =
true;
115 mainPainter.
drawPixmap(mPointZero, mPostLayersPixmap);
119void CanvasPainter::resetLayerCache()
121 mPreLayersPixmapCacheValid =
false;
122 mPostLayersPixmapCacheValid =
false;
127 painter.
begin(&device);
143void CanvasPainter::renderPreLayers(
QPainter& painter,
const QRect& blitRect)
145 if (mOptions.eLayerVisibility != LayerVisibility::CURRENTONLY || mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA)
150 paintOnionSkin(painter, blitRect);
154void CanvasPainter::renderPostLayers(
QPainter& painter,
const QRect& blitRect)
156 if (mOptions.eLayerVisibility != LayerVisibility::CURRENTONLY || mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA)
158 paintCurrentFrame(painter, blitRect, mCurrentLayerIndex + 1, mObject->getLayerCount() - 1);
162void CanvasPainter::setPaintSettings(
const Object*
object,
int currentLayer,
int frame,
TiledBuffer* tiledBuffer)
167 CANVASPAINTER_LOG(
"Set CurrentLayerIndex = %d", currentLayer);
168 mCurrentLayerIndex = currentLayer;
169 mFrameNumber = frame;
170 mTiledBuffer = tiledBuffer;
173void CanvasPainter::paint(
const QRect& blitRect)
182 renderPreLayers(preLayerPainter, blitRect);
183 preLayerPainter.
end();
186 mainPainter.
drawPixmap(mPointZero, mPreLayersPixmap);
189 paintCurrentFrame(mainPainter, blitRect, mCurrentLayerIndex, mCurrentLayerIndex);
192 renderPostLayers(postLayerPainter, blitRect);
193 postLayerPainter.
end();
196 mainPainter.
drawPixmap(mPointZero, mPostLayersPixmap);
199 mPreLayersPixmapCacheValid =
true;
200 mPostLayersPixmapCacheValid =
true;
203void CanvasPainter::paintOnionSkinOnLayer(
QPainter& painter,
const QRect& blitRect,
Layer* layer)
205 mOnionSkinSubPainter.paint(painter, layer, mOnionSkinPainterOptions, mFrameNumber, [&] (OnionSkinPaintState state,
int onionFrameNumber) {
206 if (state == OnionSkinPaintState::PREV) {
207 switch (layer->type())
209 case Layer::BITMAP: { paintBitmapOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizePrevFrames); break; }
210 case Layer::VECTOR: { paintVectorOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizePrevFrames); break; }
214 if (state == OnionSkinPaintState::NEXT) {
215 switch (layer->type())
217 case Layer::BITMAP: { paintBitmapOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizeNextFrames); break; }
218 case Layer::VECTOR: { paintVectorOnionSkinFrame(painter, blitRect, layer, onionFrameNumber, mOnionSkinPainterOptions.colorizeNextFrames); break; }
225void CanvasPainter::paintOnionSkin(
QPainter& painter,
const QRect& blitRect)
227 if (!mOptions.bOnionSkinMultiLayer || mOptions.eLayerVisibility == LayerVisibility::CURRENTONLY) {
228 Layer* layer = mObject->getLayer(mCurrentLayerIndex);
229 paintOnionSkinOnLayer(painter, blitRect, layer);
231 for (
int i = 0; i < mObject->getLayerCount(); i++) {
232 Layer* layer = mObject->getLayer(i);
233 if (layer ==
nullptr) {
continue; }
235 paintOnionSkinOnLayer(painter, blitRect, layer);
240void CanvasPainter::paintBitmapOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
244 BitmapImage* bitmapImage = bitmapLayer->getBitmapImageAtFrame(nFrame);
246 if (bitmapImage ==
nullptr) {
return; }
247 bitmapImage->loadFile();
252 onionSkinPainter.
drawImage(bitmapImage->topLeft(), *bitmapImage->image());
253 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, bitmapImage->getOpacity());
256void CanvasPainter::paintVectorOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
260 CANVASPAINTER_LOG(
"Paint Onion skin vector, Frame = %d", nFrame);
261 VectorImage* vectorImage = vectorLayer->getVectorImageAtFrame(nFrame);
262 if (vectorImage ==
nullptr) {
return; }
267 vectorImage->
paintImage(onionSkinPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
268 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, vectorImage->getOpacity());
271void CanvasPainter::paintOnionSkinFrame(
QPainter& painter,
QPainter& onionSkinPainter,
int nFrame,
bool colorize, qreal frameOpacity)
281 if (nFrame < mFrameNumber)
285 else if (nFrame > mFrameNumber)
292 onionSkinPainter.
setBrush(colorBrush);
295 painter.
drawPixmap(mPointZero, mOnionSkinPixmap);
298void CanvasPainter::paintCurrentBitmapFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
301 BitmapImage* paintedImage = bitmapLayer->getLastBitmapImageAtFrame(mFrameNumber);
303 if (paintedImage ==
nullptr) {
return; }
304 paintedImage->loadFile();
306 const bool isDrawing = mTiledBuffer && !mTiledBuffer->bounds().
isEmpty();
314 currentBitmapPainter.
drawImage(paintedImage->topLeft(), *paintedImage->image());
316 if (isCurrentLayer && isDrawing)
319 const auto tiles = mTiledBuffer->tiles();
320 for (
const Tile* tile : tiles) {
321 currentBitmapPainter.
drawPixmap(tile->posF(), tile->pixmap());
326 Q_ASSERT(!isDrawing || mSelectionTransform.
isIdentity());
327 if (isCurrentLayer && mRenderTransform && !isDrawing) {
328 paintTransformedSelection(currentBitmapPainter, paintedImage, mSelection);
331 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
334void CanvasPainter::paintCurrentVectorFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
337 VectorImage* vectorImage = vectorLayer->getLastVectorImageAtFrame(mFrameNumber, 0);
338 if (vectorImage ==
nullptr)
346 const bool isDrawing = mTiledBuffer->
isValid();
348 if (mRenderTransform) {
353 vectorImage->
paintImage(currentVectorPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
355 if (isCurrentLayer && isDrawing) {
358 const auto tiles = mTiledBuffer->tiles();
359 for (
const Tile* tile : tiles) {
360 currentVectorPainter.
drawPixmap(tile->posF(), tile->pixmap());
370 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
373void CanvasPainter::paintTransformedSelection(
QPainter& painter,
BitmapImage* bitmapImage,
const QRect& selection)
const
376 if (selection.
width() == 0 && selection.
height() == 0)
382 QPainter imagePainter(&transformedPixmap);
383 imagePainter.translate(-selection.
topLeft());
384 imagePainter.drawImage(bitmapImage->topLeft(), *bitmapImage->image());
399 painter.
setTransform(mSelectionTransform*mViewTransform);
402 painter.
drawPixmap(selection, transformedPixmap);
416 bool isCameraLayer = mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA;
418 for (
int i = startLayer; i <= endLayer; ++i)
420 Layer* layer = mObject->getLayer(i);
422 if (!layer->visible())
425 if (mOptions.eLayerVisibility == LayerVisibility::RELATED && !isCameraLayer)
427 painter.
setOpacity(calculateRelativeOpacityForLayer(mCurrentLayerIndex, i, mOptions.fLayerVisibilityThreshold));
429 bool isCurrentLayer = mCurrentLayerIndex == i;
431 CANVASPAINTER_LOG(
" Render Layer[%d] %s", i, layer->name());
432 switch (layer->type())
434 case Layer::BITMAP: { paintCurrentBitmapFrame(painter, blitRect, layer, isCurrentLayer);
break; }
435 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)