dynamic pixmap cache update

This commit is contained in:
Max-Wilhelm Bruker 2009-08-29 20:17:02 +02:00
parent 0f737e6155
commit 4beb990f38
6 changed files with 37 additions and 8 deletions

View file

@ -224,6 +224,8 @@ void CardInfo::updatePixmapCache()
qDebug(QString("Updating pixmap cache for %1").arg(name).toLatin1()); qDebug(QString("Updating pixmap cache for %1").arg(name).toLatin1());
clearPixmapCache(); clearPixmapCache();
loadPixmap(); loadPixmap();
emit pixmapUpdated();
} }
QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info) QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)

View file

@ -33,7 +33,7 @@ public:
void sortByKey(); void sortByKey();
}; };
class CardInfo : QObject { class CardInfo : public QObject {
Q_OBJECT Q_OBJECT
private: private:
CardDatabase *db; CardDatabase *db;
@ -86,6 +86,8 @@ public:
void updatePixmapCache(); void updatePixmapCache();
private slots: private slots:
void picDownloadFinished(int id, bool error); void picDownloadFinished(int id, bool error);
signals:
void pixmapUpdated();
}; };
class CardDatabase : public QObject { class CardDatabase : public QObject {

View file

@ -6,7 +6,7 @@
#include <QMessageBox> #include <QMessageBox>
CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent) CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent)
: QFrame(parent), db(_db), pixmapHeight(pixmapWidth) : QFrame(parent), db(_db), pixmapHeight(pixmapWidth), info(0)
{ {
cardPicture = new QLabel; cardPicture = new QLabel;
cardPicture->setAlignment(Qt::AlignCenter); cardPicture->setAlignment(Qt::AlignCenter);
@ -71,12 +71,12 @@ void CardInfoWidget::setCard(CardInfo *card)
if (!card) if (!card)
return; return;
QPixmap *resizedPixmap = card->getPixmap(QSize(pixmapWidth, pixmapHeight)); if (info)
if (resizedPixmap) disconnect(info, 0, this, 0);
cardPicture->setPixmap(*resizedPixmap); info = card;
else connect(info, SIGNAL(pixmapUpdated()), this, SLOT(updatePixmap()));
cardPicture->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapHeight))));
updatePixmap();
nameLabel2->setText(card->getName()); nameLabel2->setText(card->getName());
manacostLabel2->setText(card->getManaCost()); manacostLabel2->setText(card->getManaCost());
cardtypeLabel2->setText(card->getCardType()); cardtypeLabel2->setText(card->getCardType());
@ -89,6 +89,15 @@ void CardInfoWidget::setCard(const QString &cardName)
setCard(db->getCard(cardName)); setCard(db->getCard(cardName));
} }
void CardInfoWidget::updatePixmap()
{
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapHeight));
if (resizedPixmap)
cardPicture->setPixmap(*resizedPixmap);
else
cardPicture->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapHeight))));
}
void CardInfoWidget::retranslateUi() void CardInfoWidget::retranslateUi()
{ {
nameLabel1->setText(tr("Name:")); nameLabel1->setText(tr("Name:"));

View file

@ -20,12 +20,16 @@ private:
QLabel *cardtypeLabel1, *cardtypeLabel2; QLabel *cardtypeLabel1, *cardtypeLabel2;
QLabel *powtoughLabel1, *powtoughLabel2; QLabel *powtoughLabel1, *powtoughLabel2;
QTextEdit *textLabel; QTextEdit *textLabel;
CardInfo *info;
public: public:
CardInfoWidget(CardDatabase *_db, QWidget *parent = 0); CardInfoWidget(CardDatabase *_db, QWidget *parent = 0);
void retranslateUi(); void retranslateUi();
public slots: public slots:
void setCard(CardInfo *card); void setCard(CardInfo *card);
void setCard(const QString &cardName); void setCard(const QString &cardName);
private slots:
void updatePixmap();
}; };
#endif #endif

View file

@ -15,6 +15,8 @@ CardItem::CardItem(CardDatabase *_db, const QString &_name, int _cardid, QGraphi
setFlag(ItemIsSelectable); setFlag(ItemIsSelectable);
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated()));
} }
CardItem::~CardItem() CardItem::~CardItem()
@ -28,6 +30,11 @@ QRectF CardItem::boundingRect() const
return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT);
} }
void CardItem::pixmapUpdated()
{
update();
}
void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/) void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/)
{ {
painter->save(); painter->save();
@ -105,8 +112,10 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void CardItem::setName(const QString &_name) void CardItem::setName(const QString &_name)
{ {
disconnect(info, 0, this, 0);
name = _name; name = _name;
info = db->getCard(name); info = db->getCard(name);
connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated()));
update(); update();
} }

View file

@ -20,7 +20,8 @@ enum CardItemType {
typeOther = QGraphicsItem::UserType + 4 typeOther = QGraphicsItem::UserType + 4
}; };
class CardItem : public AbstractGraphicsItem { class CardItem : public QObject, public AbstractGraphicsItem {
Q_OBJECT
private: private:
CardDatabase *db; CardDatabase *db;
CardInfo *info; CardInfo *info;
@ -34,6 +35,8 @@ private:
bool doesntUntap; bool doesntUntap;
QPoint gridPoint; QPoint gridPoint;
CardDragItem *dragItem; CardDragItem *dragItem;
private slots:
void pixmapUpdated();
public: public:
enum { Type = typeCard }; enum { Type = typeCard };
int type() const { return Type; } int type() const { return Type; }