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

View file

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

View file

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

View file

@ -41,13 +41,14 @@ void CardInfoPicture::updatePixmap()
return;
}
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio));
if (resizedPixmap) {
setNoPicture(false);
this->setPixmap(*resizedPixmap);
}
else {
QPixmap resizedPixmap;
info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
if (resizedPixmap.isNull()) {
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)
return;
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio));
if (resizedPixmap)
cardPicture->setPixmap(*resizedPixmap);
else
cardPicture->setPixmap(*(getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio))));
QPixmap resizedPixmap;
info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
if (resizedPixmap.isNull())
getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio), resizedPixmap);
cardPicture->setPixmap(resizedPixmap);
}
void CardInfoWidget::retranslateUi()