proper pixmap caching for hand counter
This commit is contained in:
parent
df7bcf179d
commit
f33abf14ca
2 changed files with 16 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QSvgRenderer>
|
#include <QSvgRenderer>
|
||||||
|
#include <QPixmapCache>
|
||||||
#include "handcounter.h"
|
#include "handcounter.h"
|
||||||
#include "cardzone.h"
|
#include "cardzone.h"
|
||||||
|
|
||||||
|
@ -7,17 +8,10 @@ HandCounter::HandCounter(QGraphicsItem *parent)
|
||||||
: AbstractGraphicsItem(parent), number(0)
|
: AbstractGraphicsItem(parent), number(0)
|
||||||
{
|
{
|
||||||
setCacheMode(DeviceCoordinateCache);
|
setCacheMode(DeviceCoordinateCache);
|
||||||
|
|
||||||
QSvgRenderer svg(QString(":/resources/hand.svg"));
|
|
||||||
handImage = new QPixmap(72, 72);
|
|
||||||
handImage->fill(Qt::transparent);
|
|
||||||
QPainter painter(handImage);
|
|
||||||
svg.render(&painter, QRectF(0, 0, 72, 72));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HandCounter::~HandCounter()
|
HandCounter::~HandCounter()
|
||||||
{
|
{
|
||||||
delete handImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandCounter::updateNumber()
|
void HandCounter::updateNumber()
|
||||||
|
@ -33,6 +27,20 @@ QRectF HandCounter::boundingRect() const
|
||||||
|
|
||||||
void HandCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
void HandCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
||||||
{
|
{
|
||||||
painter->drawPixmap(handImage->rect(), *handImage, handImage->rect());
|
painter->save();
|
||||||
|
QSize translatedSize = painter->combinedTransform().mapRect(boundingRect()).size().toSize();
|
||||||
|
QPixmap cachedPixmap;
|
||||||
|
if (!QPixmapCache::find("handCounter" + QString::number(translatedSize.width()), &cachedPixmap)) {
|
||||||
|
QSvgRenderer svg(QString(":/resources/hand.svg"));
|
||||||
|
cachedPixmap = QPixmap(translatedSize);
|
||||||
|
cachedPixmap.fill(Qt::transparent);
|
||||||
|
QPainter painter(&cachedPixmap);
|
||||||
|
svg.render(&painter, QRectF(0, 0, translatedSize.width(), translatedSize.height()));
|
||||||
|
QPixmapCache::insert("handCounter" + QString::number(translatedSize.width()), cachedPixmap);
|
||||||
|
}
|
||||||
|
painter->resetTransform();
|
||||||
|
painter->drawPixmap(cachedPixmap.rect(), cachedPixmap, cachedPixmap.rect());
|
||||||
|
painter->restore();
|
||||||
|
|
||||||
paintNumberEllipse(number, 24, Qt::white, -1, painter);
|
paintNumberEllipse(number, 24, Qt::white, -1, painter);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ class HandCounter : public QObject, public AbstractGraphicsItem {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
int number;
|
int number;
|
||||||
QPixmap *handImage;
|
|
||||||
public slots:
|
public slots:
|
||||||
void updateNumber();
|
void updateNumber();
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in a new issue