Use QPixmapCache for card images

Drop CardInfo’s own pixmap caches; use QPixmapCache instead.
Use QPixmap references instead of pointers.
Being stored in QPixmapCache, all card images are now subjected to
QPixmapCache’s size limit
This commit is contained in:
Fabio Bas 2014-12-24 16:18:55 +01:00
parent b96104bed4
commit f88621fd97
5 changed files with 71 additions and 69 deletions

View file

@ -90,15 +90,11 @@ void AbstractCardItem::paintPicture(QPainter *painter, const QSizeF &translatedS
qreal scaleFactor = translatedSize.width() / boundingRect().width(); qreal scaleFactor = translatedSize.width() / boundingRect().width();
CardInfo *imageSource = facedown ? db->getCard() : info; CardInfo *imageSource = facedown ? db->getCard() : info;
QPixmap *translatedPixmap = imageSource->getPixmap(translatedSize.toSize()); QPixmap translatedPixmap;
imageSource->getPixmap(translatedSize.toSize(), translatedPixmap);
painter->save(); painter->save();
QColor bgColor = Qt::transparent; QColor bgColor = Qt::transparent;
if (translatedPixmap) { if (translatedPixmap.isNull()) {
painter->save();
transformPainter(painter, translatedSize, angle);
painter->drawPixmap(QPointF(0, 0), *translatedPixmap);
painter->restore();
} else {
QString colorStr; QString colorStr;
if (!color.isEmpty()) if (!color.isEmpty())
colorStr = color; colorStr = color;
@ -121,6 +117,11 @@ void AbstractCardItem::paintPicture(QPainter *painter, const QSizeF &translatedS
bgColor = QColor(250, 190, 30); bgColor = QColor(250, 190, 30);
else else
bgColor = QColor(230, 230, 230); bgColor = QColor(230, 230, 230);
} else {
painter->save();
transformPainter(painter, translatedSize, angle);
painter->drawPixmap(QPointF(0, 0), translatedPixmap);
painter->restore();
} }
painter->setBrush(bgColor); painter->setBrush(bgColor);
QPen pen(Qt::black); QPen pen(Qt::black);
@ -128,7 +129,7 @@ void AbstractCardItem::paintPicture(QPainter *painter, const QSizeF &translatedS
painter->setPen(pen); painter->setPen(pen);
painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2)); painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2));
if (!translatedPixmap || settingsCache->getDisplayCardNames() || facedown) { if (translatedPixmap.isNull() || settingsCache->getDisplayCardNames() || facedown) {
painter->save(); painter->save();
transformPainter(painter, translatedSize, angle); transformPainter(painter, translatedSize, angle);
painter->setPen(Qt::white); painter->setPen(Qt::white);

View file

@ -390,9 +390,10 @@ CardInfo::CardInfo(CardDatabase *_db,
customPicURLsHq(_customPicURLsHq), customPicURLsHq(_customPicURLsHq),
muIds(_muIds), muIds(_muIds),
cipt(_cipt), cipt(_cipt),
tableRow(_tableRow), tableRow(_tableRow)
pixmap(NULL)
{ {
pixmapCacheKey = QLatin1String("card_") + name;
for (int i = 0; i < sets.size(); i++) for (int i = 0; i < sets.size(); i++)
sets[i]->append(this); sets[i]->append(this);
} }
@ -446,72 +447,67 @@ void CardInfo::addToSet(CardSet *set)
sets << set; sets << set;
} }
QPixmap *CardInfo::loadPixmap() void CardInfo::loadPixmap(QPixmap &pixmap)
{ {
if (pixmap) if(QPixmapCache::find(pixmapCacheKey, &pixmap))
return pixmap; return;
pixmap = new QPixmap();
pixmap = QPixmap();
if (getName().isEmpty()) { if (getName().isEmpty()) {
pixmap->load(settingsCache->getCardBackPicturePath()); pixmap.load(settingsCache->getCardBackPicturePath());
return pixmap; return;
} }
db->loadImage(this); db->loadImage(this);
return pixmap;
} }
void CardInfo::imageLoaded(const QImage &image) void CardInfo::imageLoaded(const QImage &image)
{ {
if (!image.isNull()) { if (!image.isNull()) {
*pixmap = QPixmap::fromImage(image); QPixmapCache::insert(pixmapCacheKey, QPixmap::fromImage(image));
emit pixmapUpdated(); emit pixmapUpdated();
} }
} }
QPixmap *CardInfo::getPixmap(QSize size) void CardInfo::getPixmap(QSize size, QPixmap &pixmap)
{ {
QPixmap *cachedPixmap = scaledPixmapCache.value(size.width()); QString key = QLatin1String("card_") + name + QLatin1Char('_') + QString::number(size.width());
if (cachedPixmap) if(QPixmapCache::find(key, &pixmap))
return cachedPixmap; return;
QPixmap *bigPixmap = loadPixmap();
QPixmap *result; QPixmap bigPixmap;
if (bigPixmap->isNull()) { loadPixmap(bigPixmap);
if (!getName().isEmpty()) if (bigPixmap.isNull()) {
return 0; if (!getName().isEmpty()) {
else { pixmap = QPixmap(); // null
result = new QPixmap(size); return;
result->fill(Qt::transparent); } else {
pixmap = QPixmap(size);
pixmap.fill(Qt::transparent);
QSvgRenderer svg(QString(":/back.svg")); QSvgRenderer svg(QString(":/back.svg"));
QPainter painter(result); QPainter painter(&pixmap);
svg.render(&painter, QRectF(0, 0, size.width(), size.height())); svg.render(&painter, QRectF(0, 0, size.width(), size.height()));
} }
} else } else {
result = new QPixmap(bigPixmap->scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); pixmap = bigPixmap.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
scaledPixmapCache.insert(size.width(), result); }
return result; QPixmapCache::insert(key, pixmap);
} }
void CardInfo::clearPixmapCache() void CardInfo::clearPixmapCache()
{ {
if (pixmap) {
qDebug() << "Deleting pixmap for" << name; qDebug() << "Deleting pixmap for" << name;
delete pixmap; QPixmapCache::remove(pixmapCacheKey);
pixmap = 0;
QMapIterator<int, QPixmap *> i(scaledPixmapCache);
while (i.hasNext()) {
i.next();
qDebug() << " Deleting cached pixmap for width" << i.key();
delete i.value();
}
scaledPixmapCache.clear();
}
} }
void CardInfo::clearPixmapCacheMiss() void CardInfo::clearPixmapCacheMiss()
{ {
if (!pixmap) QPixmap pixmap;
if(!QPixmapCache::find(pixmapCacheKey, &pixmap))
return; return;
if (pixmap->isNull())
if (pixmap.isNull())
clearPixmapCache(); clearPixmapCache();
} }
@ -519,7 +515,8 @@ void CardInfo::updatePixmapCache()
{ {
qDebug() << "Updating pixmap cache for" << name; qDebug() << "Updating pixmap cache for" << name;
clearPixmapCache(); clearPixmapCache();
loadPixmap(); QPixmap tmp;
loadPixmap(tmp);
emit pixmapUpdated(); emit pixmapUpdated();
} }
@ -609,7 +606,8 @@ CardDatabase::CardDatabase(QObject *parent)
pictureLoaderThread->start(QThread::LowPriority); pictureLoaderThread->start(QThread::LowPriority);
noCard = new CardInfo(this); noCard = new CardInfo(this);
noCard->loadPixmap(); // cache pixmap for card back QPixmap tmp;
noCard->loadPixmap(tmp); // cache pixmap for card back
connect(settingsCache, SIGNAL(cardBackPicturePathChanged()), noCard, SLOT(updatePixmapCache())); connect(settingsCache, SIGNAL(cardBackPicturePathChanged()), noCard, SLOT(updatePixmapCache()));
} }
@ -954,8 +952,9 @@ QStringList CardDatabase::getAllMainCardTypes() const
void CardDatabase::cacheCardPixmaps(const QStringList &cardNames) void CardDatabase::cacheCardPixmaps(const QStringList &cardNames)
{ {
QPixmap tmp;
for (int i = 0; i < cardNames.size(); ++i) for (int i = 0; i < cardNames.size(); ++i)
getCard(cardNames[i])->loadPixmap(); getCard(cardNames[i])->loadPixmap(tmp);
} }
void CardDatabase::loadImage(CardInfo *card) void CardDatabase::loadImage(CardInfo *card)

View file

@ -12,6 +12,7 @@
#include <QThread> #include <QThread>
#include <QMutex> #include <QMutex>
#include <QWaitCondition> #include <QWaitCondition>
#include <QPixmapCache>
class CardDatabase; class CardDatabase;
class CardInfo; class CardInfo;
@ -119,8 +120,7 @@ private:
MuidMap muIds; MuidMap muIds;
bool cipt; bool cipt;
int tableRow; int tableRow;
QPixmap *pixmap; QString pixmapCacheKey;
QMap<int, QPixmap *> scaledPixmapCache;
public: public:
CardInfo(CardDatabase *_db, CardInfo(CardDatabase *_db,
const QString &_name = QString(), const QString &_name = QString(),
@ -165,8 +165,8 @@ public:
void setCustomPicURLHq(const QString &_set, const QString &_customPicURL) { customPicURLsHq.insert(_set, _customPicURL); } void setCustomPicURLHq(const QString &_set, const QString &_customPicURL) { customPicURLsHq.insert(_set, _customPicURL); }
void setMuId(const QString &_set, const int &_muId) { muIds.insert(_set, _muId); } void setMuId(const QString &_set, const int &_muId) { muIds.insert(_set, _muId); }
void addToSet(CardSet *set); void addToSet(CardSet *set);
QPixmap *loadPixmap(); void loadPixmap(QPixmap &pixmap);
QPixmap *getPixmap(QSize size); void getPixmap(QSize size, QPixmap &pixmap);
void clearPixmapCache(); void clearPixmapCache();
void clearPixmapCacheMiss(); void clearPixmapCacheMiss();
void imageLoaded(const QImage &image); void imageLoaded(const QImage &image);

View file

@ -41,13 +41,14 @@ void CardInfoPicture::updatePixmap()
return; return;
} }
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)); QPixmap resizedPixmap;
if (resizedPixmap) { info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
setNoPicture(false);
this->setPixmap(*resizedPixmap); if (resizedPixmap.isNull()) {
}
else {
setNoPicture(true); setNoPicture(true);
this->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)))); db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
} else {
setNoPicture(false);
} }
this->setPixmap(resizedPixmap);
} }

View file

@ -188,11 +188,12 @@ void CardInfoWidget::updatePixmap()
if (pixmapWidth == 0) if (pixmapWidth == 0)
return; return;
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)); QPixmap resizedPixmap;
if (resizedPixmap) info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
cardPicture->setPixmap(*resizedPixmap);
else if (resizedPixmap.isNull())
cardPicture->setPixmap(*(getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)))); getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
cardPicture->setPixmap(resizedPixmap);
} }
void CardInfoWidget::retranslateUi() void CardInfoWidget::retranslateUi()