fix: caching scaled image to avoid lagging when panning large image
This commit is contained in:
parent
c9dadcbcad
commit
6270910e12
|
@ -21,6 +21,18 @@ public:
|
||||||
m_scaleHint = scaleHint;
|
m_scaleHint = scaleHint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QPixmap & scaledPixmap(float scaleHint) {
|
||||||
|
if (qFuzzyCompare(scaleHint, m_cachedScaleHint)) return m_cachedPixmap;
|
||||||
|
QSizeF resizedScale(boundingRect().size());
|
||||||
|
resizedScale *= scaleHint;
|
||||||
|
m_cachedPixmap = pixmap().scaled(
|
||||||
|
resizedScale.toSize(),
|
||||||
|
Qt::KeepAspectRatio,
|
||||||
|
Qt::SmoothTransformation);
|
||||||
|
m_cachedScaleHint = scaleHint;
|
||||||
|
return m_cachedPixmap;
|
||||||
|
}
|
||||||
|
|
||||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
||||||
QWidget *widget) override
|
QWidget *widget) override
|
||||||
{
|
{
|
||||||
|
@ -28,18 +40,15 @@ public:
|
||||||
return QGraphicsPixmapItem::paint(painter, option, widget);
|
return QGraphicsPixmapItem::paint(painter, option, widget);
|
||||||
} else {
|
} else {
|
||||||
// painter->setRenderHints(QPainter::Antialiasing);
|
// painter->setRenderHints(QPainter::Antialiasing);
|
||||||
QSizeF resizedScale(boundingRect().size());
|
|
||||||
resizedScale *= m_scaleHint;
|
|
||||||
painter->drawPixmap(QRectF(offset(), boundingRect().size()).toRect(),
|
painter->drawPixmap(QRectF(offset(), boundingRect().size()).toRect(),
|
||||||
pixmap().scaled(resizedScale.toSize(),
|
scaledPixmap(m_scaleHint));
|
||||||
Qt::KeepAspectRatio,
|
|
||||||
Qt::SmoothTransformation)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float m_scaleHint = 1;
|
float m_scaleHint = 1;
|
||||||
|
float m_cachedScaleHint = -1;
|
||||||
|
QPixmap m_cachedPixmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphicsScene::GraphicsScene(QObject *parent)
|
GraphicsScene::GraphicsScene(QObject *parent)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user