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::paintOnionSkin(
QPainter& painter,
const QRect& blitRect)
205 Layer* layer = mObject->getLayer(mCurrentLayerIndex);
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::paintBitmapOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
231 BitmapImage* bitmapImage = bitmapLayer->getBitmapImageAtFrame(nFrame);
233 if (bitmapImage ==
nullptr) {
return; }
234 bitmapImage->loadFile();
239 onionSkinPainter.
drawImage(bitmapImage->topLeft(), *bitmapImage->image());
240 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, bitmapImage->getOpacity());
243void CanvasPainter::paintVectorOnionSkinFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
int nFrame,
bool colorize)
247 CANVASPAINTER_LOG(
"Paint Onion skin vector, Frame = %d", nFrame);
248 VectorImage* vectorImage = vectorLayer->getVectorImageAtFrame(nFrame);
249 if (vectorImage ==
nullptr) {
return; }
254 vectorImage->
paintImage(onionSkinPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
255 paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, vectorImage->getOpacity());
258void CanvasPainter::paintOnionSkinFrame(
QPainter& painter,
QPainter& onionSkinPainter,
int nFrame,
bool colorize, qreal frameOpacity)
268 if (nFrame < mFrameNumber)
272 else if (nFrame > mFrameNumber)
279 onionSkinPainter.
setBrush(colorBrush);
282 painter.
drawPixmap(mPointZero, mOnionSkinPixmap);
285void CanvasPainter::paintCurrentBitmapFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
288 BitmapImage* paintedImage = bitmapLayer->getLastBitmapImageAtFrame(mFrameNumber);
290 if (paintedImage ==
nullptr) {
return; }
291 paintedImage->loadFile();
293 const bool isDrawing = mTiledBuffer && !mTiledBuffer->bounds().
isEmpty();
301 currentBitmapPainter.
drawImage(paintedImage->topLeft(), *paintedImage->image());
303 if (isCurrentLayer && isDrawing)
306 const auto tiles = mTiledBuffer->tiles();
307 for (
const Tile* tile : tiles) {
308 currentBitmapPainter.
drawPixmap(tile->posF(), tile->pixmap());
313 Q_ASSERT(!isDrawing || mSelectionTransform.
isIdentity());
314 if (isCurrentLayer && mRenderTransform && !isDrawing) {
315 paintTransformedSelection(currentBitmapPainter, paintedImage, mSelection);
318 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
321void CanvasPainter::paintCurrentVectorFrame(
QPainter& painter,
const QRect& blitRect,
Layer* layer,
bool isCurrentLayer)
324 VectorImage* vectorImage = vectorLayer->getLastVectorImageAtFrame(mFrameNumber, 0);
325 if (vectorImage ==
nullptr)
333 const bool isDrawing = mTiledBuffer->
isValid();
335 if (mRenderTransform) {
340 vectorImage->
paintImage(currentVectorPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
342 if (isCurrentLayer && isDrawing) {
345 const auto tiles = mTiledBuffer->tiles();
346 for (
const Tile* tile : tiles) {
347 currentVectorPainter.
drawPixmap(tile->posF(), tile->pixmap());
357 painter.
drawPixmap(mPointZero, mCurrentLayerPixmap);
360void CanvasPainter::paintTransformedSelection(
QPainter& painter,
BitmapImage* bitmapImage,
const QRect& selection)
const
363 if (selection.
width() == 0 && selection.
height() == 0)
369 QPainter imagePainter(&transformedPixmap);
370 imagePainter.translate(-selection.
topLeft());
371 imagePainter.drawImage(bitmapImage->topLeft(), *bitmapImage->image());
386 painter.
setTransform(mSelectionTransform*mViewTransform);
389 painter.
drawPixmap(selection, transformedPixmap);
403 bool isCameraLayer = mObject->getLayer(mCurrentLayerIndex)->type() == Layer::CAMERA;
405 for (
int i = startLayer; i <= endLayer; ++i)
407 Layer* layer = mObject->getLayer(i);
409 if (!layer->visible())
412 if (mOptions.eLayerVisibility == LayerVisibility::RELATED && !isCameraLayer)
414 painter.
setOpacity(calculateRelativeOpacityForLayer(mCurrentLayerIndex, i, mOptions.fLayerVisibilityThreshold));
416 bool isCurrentLayer = mCurrentLayerIndex == i;
418 CANVASPAINTER_LOG(
" Render Layer[%d] %s", i, layer->name());
419 switch (layer->type())
421 case Layer::BITMAP: { paintCurrentBitmapFrame(painter, blitRect, layer, isCurrentLayer);
break; }
422 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)