diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 6dfd50bfa..ded280dd5 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -23,9 +23,16 @@ CardInfo::CardInfo(QDataStream &stream) CardInfo::~CardInfo() { - if (pixmap) + if (pixmap) { qDebug(QString("Deleting pixmap for %1").arg(name).toLatin1()); - delete pixmap; + delete pixmap; + QMapIterator 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 @@ -63,7 +70,7 @@ void CardInfo::addEdition(const QString &edition) editions << edition; } -QPixmap *CardInfo::getPixmap() +QPixmap *CardInfo::loadPixmap() { if (pixmap) return pixmap; @@ -72,7 +79,7 @@ QPixmap *CardInfo::getPixmap() pixmap->load("../pics/back.jpg"); 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++) { // Fire // Ice, Circle of Protection: Red QString correctedName = getName().remove(" // ").remove(":"); @@ -85,6 +92,19 @@ QPixmap *CardInfo::getPixmap() 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) { stream << name @@ -192,7 +212,7 @@ void CardDatabase::importOracle() qDebug(QString("CardDatabase: %1 cards imported").arg(hash.size()).toLatin1()); CardInfo *empty = new CardInfo(); - empty->getPixmap(); // cache pixmap for card back + empty->loadPixmap(); // cache pixmap for card back hash.insert("", empty); } diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index bfa25524d..13f97d862 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -3,6 +3,7 @@ #include #include +#include #include class CardInfo { @@ -14,6 +15,7 @@ private: QString powtough; QStringList text; QPixmap *pixmap; + QMap scaledPixmapCache; public: CardInfo(const QString &_name = QString(), const QString &_manacost = QString(), @@ -30,7 +32,8 @@ public: QStringList getText() const { return text; } QString getMainCardType() const; void addEdition(const QString &edition); - QPixmap *getPixmap(); + QPixmap *loadPixmap(); + QPixmap *getPixmap(QSize size); void saveToStream(QDataStream &stream); }; diff --git a/cockatrice/src/carddragitem.cpp b/cockatrice/src/carddragitem.cpp index bb4a29608..cde021aea 100644 --- a/cockatrice/src/carddragitem.cpp +++ b/cockatrice/src/carddragitem.cpp @@ -1,9 +1,10 @@ #include "carddragitem.h" #include "cardzone.h" +#include "carddatabase.h" #include -CardDragItem::CardDragItem(QGraphicsScene *scene, CardZone *_startZone, QPixmap *_image, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent) - : QGraphicsItem(parent), image(_image), id(_id), hotSpot(_hotSpot), startZone(_startZone), faceDown(_faceDown) +CardDragItem::CardDragItem(QGraphicsScene *scene, CardZone *_startZone, CardInfo *_info, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent) + : QGraphicsItem(parent), id(_id), info(_info), hotSpot(_hotSpot), startZone(_startZone), faceDown(_faceDown) { if ((hotSpot.x() < 0) || (hotSpot.y() < 0)) { qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1()); @@ -31,16 +32,11 @@ QRectF CardDragItem::boundingRect() const 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); - Q_UNUSED(widget); - - 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())); + QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); + QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize()); + painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect()); } void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) diff --git a/cockatrice/src/carddragitem.h b/cockatrice/src/carddragitem.h index a2c6b9850..1eb3df795 100644 --- a/cockatrice/src/carddragitem.h +++ b/cockatrice/src/carddragitem.h @@ -5,18 +5,19 @@ class QGraphicsScene; class CardZone; +class CardInfo; class CardDragItem : public QGraphicsItem { private: - QPixmap *image; int id; + CardInfo *info; QPointF hotSpot; CardZone *startZone; bool faceDown; public: enum { Type = typeCardDrag }; 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(); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); diff --git a/cockatrice/src/cardinfowidget.cpp b/cockatrice/src/cardinfowidget.cpp index 7a4517d8b..591d710ee 100644 --- a/cockatrice/src/cardinfowidget.cpp +++ b/cockatrice/src/cardinfowidget.cpp @@ -42,12 +42,13 @@ void CardInfoWidget::setCard(CardInfo *card) if (!card) return; - QPixmap *pixmap = card->getPixmap(); - if (aspectratio == 0) - aspectratio = (double) pixmap->height() / pixmap->width(); + if (aspectratio == 0) { + QPixmap *bigPixmap = card->loadPixmap(); + aspectratio = (double) bigPixmap->height() / bigPixmap->width(); + } 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()); manacostLabel2->setText(card->getManacost()); cardtypeLabel2->setText(card->getCardType()); diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 4a5321e2d..51f3d3078 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -10,7 +10,6 @@ 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) { - image = db->getCard(name)->getPixmap(); setCursor(Qt::OpenHandCursor); setFlag(ItemIsSelectable); setAcceptsHoverEvents(true); @@ -31,15 +30,13 @@ QRectF CardItem::boundingRect() const void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/) { painter->save(); - QRectF foo = option->matrix.mapRect(boundingRect()); - qDebug(QString("%1: w=%2,h=%3").arg(name).arg(foo.width()).arg(foo.height()).toLatin1()); - QPixmap bar; + + QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); if (tapped) - bar = image->scaled((int) foo.height(), (int) foo.width(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - else - bar = image->scaled((int) foo.width(), (int) foo.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); -// painter->drawPixmap(boundingRect(), *image, QRectF(0, 0, image->width(), image->height())); - painter->drawPixmap(boundingRect(), bar, bar.rect()); + translatedSize.transpose(); + QPixmap *translatedPixmap = db->getCard(name)->getPixmap(translatedSize.toSize()); + painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect()); + if (isSelected()) { painter->setPen(QPen(QColor("red"))); painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2)); @@ -57,7 +54,6 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void CardItem::setName(const QString &_name) { name = _name; - image = db->getCard(name)->getPixmap(); update(boundingRect()); } @@ -114,7 +110,7 @@ void CardItem::resetState() CardDragItem *CardItem::createDragItem(CardZone *startZone, int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown) { 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()); return dragItem; @@ -154,7 +150,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) CardItem *c = (CardItem *) sel.at(i); if (c == this) 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()); } setCursor(Qt::OpenHandCursor); diff --git a/cockatrice/src/carditem.h b/cockatrice/src/carditem.h index 8a1252adc..94ac3fc53 100644 --- a/cockatrice/src/carditem.h +++ b/cockatrice/src/carditem.h @@ -34,7 +34,6 @@ private: CardDatabase *db; QString name; int id; - QPixmap *image; bool tapped; bool attacking; bool facedown; @@ -53,7 +52,6 @@ public: void setId(int _id) { id = _id; } QString getName() const { return name; } void setName(const QString &_name = QString()); - QPixmap *getImage() const { return image; } bool getTapped() const { return tapped; } void setTapped(bool _tapped); bool getAttacking() const { return attacking; } diff --git a/cockatrice/src/cardzone.h b/cockatrice/src/cardzone.h index df33636b5..9fd55915f 100644 --- a/cockatrice/src/cardzone.h +++ b/cockatrice/src/cardzone.h @@ -11,7 +11,6 @@ class QPainter; class CardZone : public QGraphicsItem { protected: - QPixmap *image; Player *player; QString name; CardList *cards; diff --git a/cockatrice/src/decklist.cpp b/cockatrice/src/decklist.cpp index 258ad3c27..b5ba557c1 100644 --- a/cockatrice/src/decklist.cpp +++ b/cockatrice/src/decklist.cpp @@ -343,7 +343,7 @@ void DeckList::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog for (int i = 0; i < item->size(); i++) { DecklistCardNode *node = dynamic_cast(item->at(i)); if (node) { - db->getCard(node->getName())->getPixmap(); + db->getCard(node->getName())->loadPixmap(); progress->setValue(progress->value() + 1); } else cacheCardPicturesHelper(dynamic_cast(item->at(i)), progress); diff --git a/cockatrice/src/libraryzone.cpp b/cockatrice/src/libraryzone.cpp index 14a29f53c..03546c2ed 100644 --- a/cockatrice/src/libraryzone.cpp +++ b/cockatrice/src/libraryzone.cpp @@ -12,8 +12,6 @@ LibraryZone::LibraryZone(Player *_p, QGraphicsItem *parent) cards = new CardList(false); setCacheMode(DeviceCoordinateCache); // Do not move this line to the parent constructor! setCursor(Qt::OpenHandCursor); - - image = player->getDb()->getCard()->getPixmap(); } QRectF LibraryZone::boundingRect() const @@ -25,9 +23,9 @@ void LibraryZone::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio { painter->save(); - QRectF foo = option->matrix.mapRect(boundingRect()); - QPixmap bar = image->scaled((int) foo.width(), (int) foo.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - painter->drawPixmap(boundingRect(), bar, bar.rect()); + QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); + QPixmap *translatedPixmap = player->getDb()->getCard()->getPixmap(translatedSize.toSize()); + painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect()); paintCardNumberEllipse(painter);