Commit 04072b02 authored by brukie's avatar brukie
Browse files

scaled pixmap cache. major speed improvement

parent 1da5c637
...@@ -23,9 +23,16 @@ CardInfo::CardInfo(QDataStream &stream) ...@@ -23,9 +23,16 @@ CardInfo::CardInfo(QDataStream &stream)
CardInfo::~CardInfo() CardInfo::~CardInfo()
{ {
if (pixmap) if (pixmap) {
qDebug(QString("Deleting pixmap for %1").arg(name).toLatin1()); qDebug(QString("Deleting pixmap for %1").arg(name).toLatin1());
delete pixmap; delete pixmap;
QMapIterator<int, QPixmap *> i(scaledPixmapCache);
while (i.hasNext()) {
i.next();
qDebug(QString(" Deleting cached pixmap for width %1").arg(i.key()).toLatin1());
delete i.value();
}
}
} }
QString CardInfo::getMainCardType() const QString CardInfo::getMainCardType() const
...@@ -63,7 +70,7 @@ void CardInfo::addEdition(const QString &edition) ...@@ -63,7 +70,7 @@ void CardInfo::addEdition(const QString &edition)
editions << edition; editions << edition;
} }
QPixmap *CardInfo::getPixmap() QPixmap *CardInfo::loadPixmap()
{ {
if (pixmap) if (pixmap)
return pixmap; return pixmap;
...@@ -72,7 +79,7 @@ QPixmap *CardInfo::getPixmap() ...@@ -72,7 +79,7 @@ QPixmap *CardInfo::getPixmap()
pixmap->load("../pics/back.jpg"); pixmap->load("../pics/back.jpg");
return pixmap; return pixmap;
} }
qDebug(QString("CardDatabase: loading pixmap for %1").arg(getName()).toLatin1()); qDebug(QString("CardDatabase: loading pixmap for '%1'").arg(getName()).toLatin1());
for (int i = 0; i < editions.size(); i++) { for (int i = 0; i < editions.size(); i++) {
// Fire // Ice, Circle of Protection: Red // Fire // Ice, Circle of Protection: Red
QString correctedName = getName().remove(" // ").remove(":"); QString correctedName = getName().remove(" // ").remove(":");
...@@ -85,6 +92,19 @@ QPixmap *CardInfo::getPixmap() ...@@ -85,6 +92,19 @@ QPixmap *CardInfo::getPixmap()
return pixmap; return pixmap;
} }
QPixmap *CardInfo::getPixmap(QSize size)
{
qDebug(QString("CardInfo::getPixmap(%1, %2) for %3").arg(size.width()).arg(size.height()).arg(getName()).toLatin1());
if (QPixmap *result = scaledPixmapCache.value(size.width())) {
qDebug("cache HIT");
return result;
}
qDebug("cache MISS");
QPixmap *result = new QPixmap(loadPixmap()->scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
scaledPixmapCache.insert(size.width(), result);
return result;
}
void CardInfo::saveToStream(QDataStream &stream) void CardInfo::saveToStream(QDataStream &stream)
{ {
stream << name stream << name
...@@ -192,7 +212,7 @@ void CardDatabase::importOracle() ...@@ -192,7 +212,7 @@ void CardDatabase::importOracle()
qDebug(QString("CardDatabase: %1 cards imported").arg(hash.size()).toLatin1()); qDebug(QString("CardDatabase: %1 cards imported").arg(hash.size()).toLatin1());
CardInfo *empty = new CardInfo(); CardInfo *empty = new CardInfo();
empty->getPixmap(); // cache pixmap for card back empty->loadPixmap(); // cache pixmap for card back
hash.insert("", empty); hash.insert("", empty);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <QHash> #include <QHash>
#include <QPixmap> #include <QPixmap>
#include <QMap>
#include <QDataStream> #include <QDataStream>
class CardInfo { class CardInfo {
...@@ -14,6 +15,7 @@ private: ...@@ -14,6 +15,7 @@ private:
QString powtough; QString powtough;
QStringList text; QStringList text;
QPixmap *pixmap; QPixmap *pixmap;
QMap<int, QPixmap *> scaledPixmapCache;
public: public:
CardInfo(const QString &_name = QString(), CardInfo(const QString &_name = QString(),
const QString &_manacost = QString(), const QString &_manacost = QString(),
...@@ -30,7 +32,8 @@ public: ...@@ -30,7 +32,8 @@ public:
QStringList getText() const { return text; } QStringList getText() const { return text; }
QString getMainCardType() const; QString getMainCardType() const;
void addEdition(const QString &edition); void addEdition(const QString &edition);
QPixmap *getPixmap(); QPixmap *loadPixmap();
QPixmap *getPixmap(QSize size);
void saveToStream(QDataStream &stream); void saveToStream(QDataStream &stream);
}; };
......
#include "carddragitem.h" #include "carddragitem.h"
#include "cardzone.h" #include "cardzone.h"
#include "carddatabase.h"
#include <QtGui> #include <QtGui>
CardDragItem::CardDragItem(QGraphicsScene *scene, CardZone *_startZone, QPixmap *_image, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent) CardDragItem::CardDragItem(QGraphicsScene *scene, CardZone *_startZone, CardInfo *_info, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent)
: QGraphicsItem(parent), image(_image), id(_id), hotSpot(_hotSpot), startZone(_startZone), faceDown(_faceDown) : QGraphicsItem(parent), id(_id), info(_info), hotSpot(_hotSpot), startZone(_startZone), faceDown(_faceDown)
{ {
if ((hotSpot.x() < 0) || (hotSpot.y() < 0)) { if ((hotSpot.x() < 0) || (hotSpot.y() < 0)) {
qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1()); qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1());
...@@ -31,16 +32,11 @@ QRectF CardDragItem::boundingRect() const ...@@ -31,16 +32,11 @@ QRectF CardDragItem::boundingRect() const
return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT);
} }
void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/)
{ {
// Q_UNUSED(option); QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size();
Q_UNUSED(widget); QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize());
painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect());
QRectF foo = option->matrix.mapRect(boundingRect());
QPixmap bar = image->scaled(foo.width(), foo.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
painter->drawPixmap(boundingRect(), bar, bar.rect());
// painter->drawPixmap(boundingRect(), *image, QRectF(0, 0, image->width(), image->height()));
} }
void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
......
...@@ -5,18 +5,19 @@ ...@@ -5,18 +5,19 @@
class QGraphicsScene; class QGraphicsScene;
class CardZone; class CardZone;
class CardInfo;
class CardDragItem : public QGraphicsItem { class CardDragItem : public QGraphicsItem {
private: private:
QPixmap *image;
int id; int id;
CardInfo *info;
QPointF hotSpot; QPointF hotSpot;
CardZone *startZone; CardZone *startZone;
bool faceDown; bool faceDown;
public: public:
enum { Type = typeCardDrag }; enum { Type = typeCardDrag };
int type() const { return Type; } int type() const { return Type; }
CardDragItem(QGraphicsScene *scene, CardZone *_startZone, QPixmap *_image, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent = 0); CardDragItem(QGraphicsScene *scene, CardZone *_startZone, CardInfo *_info, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent = 0);
~CardDragItem(); ~CardDragItem();
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
......
...@@ -42,12 +42,13 @@ void CardInfoWidget::setCard(CardInfo *card) ...@@ -42,12 +42,13 @@ void CardInfoWidget::setCard(CardInfo *card)
if (!card) if (!card)
return; return;
QPixmap *pixmap = card->getPixmap(); if (aspectratio == 0) {
if (aspectratio == 0) QPixmap *bigPixmap = card->loadPixmap();
aspectratio = (double) pixmap->height() / pixmap->width(); aspectratio = (double) bigPixmap->height() / bigPixmap->width();
}
double w = 180; double w = 180;
cardPicture->setPixmap(pixmap->scaled((int) w, (int) (w * aspectratio), Qt::KeepAspectRatio, Qt::SmoothTransformation)); cardPicture->setPixmap(*card->getPixmap(QSize(w, w * aspectratio)));
nameLabel2->setText(card->getName()); nameLabel2->setText(card->getName());
manacostLabel2->setText(card->getManacost()); manacostLabel2->setText(card->getManacost());
cardtypeLabel2->setText(card->getCardType()); cardtypeLabel2->setText(card->getCardType());
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
CardItem::CardItem(CardDatabase *_db, const QString &_name, int _cardid, QGraphicsItem *parent) CardItem::CardItem(CardDatabase *_db, const QString &_name, int _cardid, QGraphicsItem *parent)
: QGraphicsItem(parent), db(_db), name(_name), id(_cardid), tapped(false), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL) : QGraphicsItem(parent), db(_db), name(_name), id(_cardid), tapped(false), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL)
{ {
image = db->getCard(name)->getPixmap();
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
setFlag(ItemIsSelectable); setFlag(ItemIsSelectable);
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
...@@ -31,15 +30,13 @@ QRectF CardItem::boundingRect() const ...@@ -31,15 +30,13 @@ QRectF CardItem::boundingRect() const
void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/) void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/)
{ {
painter->save(); painter->save();
QRectF foo = option->matrix.mapRect(boundingRect());
qDebug(QString("%1: w=%2,h=%3").arg(name).arg(foo.width()).arg(foo.height()).toLatin1()); QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size();
QPixmap bar;
if (tapped) if (tapped)
bar = image->scaled((int) foo.height(), (int) foo.width(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); translatedSize.transpose();
else QPixmap *translatedPixmap = db->getCard(name)->getPixmap(translatedSize.toSize());
bar = image->scaled((int) foo.width(), (int) foo.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect());
// painter->drawPixmap(boundingRect(), *image, QRectF(0, 0, image->width(), image->height()));
painter->drawPixmap(boundingRect(), bar, bar.rect());
if (isSelected()) { if (isSelected()) {
painter->setPen(QPen(QColor("red"))); painter->setPen(QPen(QColor("red")));
painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2)); painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2));
...@@ -57,7 +54,6 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, ...@@ -57,7 +54,6 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void CardItem::setName(const QString &_name) void CardItem::setName(const QString &_name)
{ {
name = _name; name = _name;
image = db->getCard(name)->getPixmap();
update(boundingRect()); update(boundingRect());
} }
...@@ -114,7 +110,7 @@ void CardItem::resetState() ...@@ -114,7 +110,7 @@ void CardItem::resetState()
CardDragItem *CardItem::createDragItem(CardZone *startZone, int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown) CardDragItem *CardItem::createDragItem(CardZone *startZone, int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown)
{ {
deleteDragItem(); deleteDragItem();
dragItem = new CardDragItem(scene(), startZone, image, _id, _pos, faceDown); dragItem = new CardDragItem(scene(), startZone, db->getCard(name), _id, _pos, faceDown);
dragItem->setPos(_scenePos - dragItem->getHotSpot()); dragItem->setPos(_scenePos - dragItem->getHotSpot());
return dragItem; return dragItem;
...@@ -154,7 +150,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -154,7 +150,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
CardItem *c = (CardItem *) sel.at(i); CardItem *c = (CardItem *) sel.at(i);
if (c == this) if (c == this)
continue; continue;
CardDragItem *drag = new CardDragItem(scene(), (CardZone *) parentItem(), c->getImage(), c->getId(), QPointF(), false, dragItem); CardDragItem *drag = new CardDragItem(scene(), (CardZone *) parentItem(), db->getCard(c->getName()), c->getId(), QPointF(), false, dragItem);
drag->setPos(c->pos() - pos()); drag->setPos(c->pos() - pos());
} }
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
......
...@@ -34,7 +34,6 @@ private: ...@@ -34,7 +34,6 @@ private:
CardDatabase *db; CardDatabase *db;
QString name; QString name;
int id; int id;
QPixmap *image;
bool tapped; bool tapped;
bool attacking; bool attacking;
bool facedown; bool facedown;
...@@ -53,7 +52,6 @@ public: ...@@ -53,7 +52,6 @@ public:
void setId(int _id) { id = _id; } void setId(int _id) { id = _id; }
QString getName() const { return name; } QString getName() const { return name; }
void setName(const QString &_name = QString()); void setName(const QString &_name = QString());
QPixmap *getImage() const { return image; }
bool getTapped() const { return tapped; } bool getTapped() const { return tapped; }
void setTapped(bool _tapped); void setTapped(bool _tapped);
bool getAttacking() const { return attacking; } bool getAttacking() const { return attacking; }
......
...@@ -11,7 +11,6 @@ class QPainter; ...@@ -11,7 +11,6 @@ class QPainter;
class CardZone : public QGraphicsItem { class CardZone : public QGraphicsItem {
protected: protected:
QPixmap *image;
Player *player; Player *player;
QString name; QString name;
CardList *cards; CardList *cards;
......
...@@ -343,7 +343,7 @@ void DeckList::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog ...@@ -343,7 +343,7 @@ void DeckList::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog
for (int i = 0; i < item->size(); i++) { for (int i = 0; i < item->size(); i++) {
DecklistCardNode *node = dynamic_cast<DecklistCardNode *>(item->at(i)); DecklistCardNode *node = dynamic_cast<DecklistCardNode *>(item->at(i));
if (node) { if (node) {
db->getCard(node->getName())->getPixmap(); db->getCard(node->getName())->loadPixmap();
progress->setValue(progress->value() + 1); progress->setValue(progress->value() + 1);
} else } else
cacheCardPicturesHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), progress); cacheCardPicturesHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), progress);
......
...@@ -12,8 +12,6 @@ LibraryZone::LibraryZone(Player *_p, QGraphicsItem *parent) ...@@ -12,8 +12,6 @@ LibraryZone::LibraryZone(Player *_p, QGraphicsItem *parent)
cards = new CardList(false); cards = new CardList(false);
setCacheMode(DeviceCoordinateCache); // Do not move this line to the parent constructor! setCacheMode(DeviceCoordinateCache); // Do not move this line to the parent constructor!
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
image = player->getDb()->getCard()->getPixmap();
} }
QRectF LibraryZone::boundingRect() const QRectF LibraryZone::boundingRect() const
...@@ -25,9 +23,9 @@ void LibraryZone::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio ...@@ -25,9 +23,9 @@ void LibraryZone::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
{ {
painter->save(); painter->save();
QRectF foo = option->matrix.mapRect(boundingRect()); QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size();
QPixmap bar = image->scaled((int) foo.width(), (int) foo.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *translatedPixmap = player->getDb()->getCard()->getPixmap(translatedSize.toSize());
painter->drawPixmap(boundingRect(), bar, bar.rect()); painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect());
paintCardNumberEllipse(painter); paintCardNumberEllipse(painter);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment