From f77054f20d9cf963fea09f8f7458656a24e97edb Mon Sep 17 00:00:00 2001 From: sylvanbasilisk Date: Wed, 29 Jan 2014 00:19:34 +0000 Subject: [PATCH] refactor cardframe as a parent of a text card frame and a picture card frame cardframe is now a stacked widget that prefers to show the card picture only so that the screen can be more space efficient and the card pixel map can be displayed as a larger size. however if the card pixel map can not be loaded for some reason, the cardframe class will automatically switch to the text version of the card. a menu item was added under the database menu to allow for users to prefer card text only. --- cockatrice/CMakeLists.txt | 4 ++ cockatrice/src/cardframe.cpp | 44 ++++++++++---------- cockatrice/src/cardframe.h | 22 ++++++---- cockatrice/src/cardinfopicture.cpp | 53 +++++++++++++++++++++++ cockatrice/src/cardinfopicture.h | 34 +++++++++++++++ cockatrice/src/cardinfotext.cpp | 67 ++++++++++++++++++++++++++++++ cockatrice/src/cardinfotext.h | 31 ++++++++++++++ cockatrice/src/tab_deck_editor.cpp | 8 +++- cockatrice/src/tab_deck_editor.h | 2 +- 9 files changed, 231 insertions(+), 34 deletions(-) create mode 100644 cockatrice/src/cardinfopicture.cpp create mode 100644 cockatrice/src/cardinfopicture.h create mode 100644 cockatrice/src/cardinfotext.cpp create mode 100644 cockatrice/src/cardinfotext.h diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index a1e8683a..4012795f 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -34,6 +34,8 @@ SET(cockatrice_SOURCES src/dlg_load_remote_deck.cpp src/cardinfowidget.cpp src/cardframe.cpp + src/cardinfopicture.cpp + src/cardinfotext.cpp src/filterbuilder.cpp src/cardfilter.cpp src/filtertreemodel.cpp @@ -116,6 +118,8 @@ SET(cockatrice_HEADERS src/dlg_load_remote_deck.h src/cardinfowidget.h src/cardframe.h + src/cardinfopicture.h + src/cardinfotext.h src/filterbuilder.h src/cardfilter.h src/filtertreemodel.h diff --git a/cockatrice/src/cardframe.cpp b/cockatrice/src/cardframe.cpp index dd4191ef..89b3cb6b 100644 --- a/cockatrice/src/cardframe.cpp +++ b/cockatrice/src/cardframe.cpp @@ -1,16 +1,27 @@ #include "cardframe.h" -#include #include "carditem.h" #include "carddatabase.h" #include "main.h" +#include "cardinfopicture.h" +#include "cardinfotext.h" -CardFrame::CardFrame(const QString &cardName, QWidget *parent) - : QLabel(parent) +CardFrame::CardFrame(int width, int height, + const QString &cardName, QWidget *parent) + : QStackedWidget(parent) , info(0) + , cardTextOnly(false) { setFrameStyle(QFrame::Panel | QFrame::Raised); - setMaximumWidth(250); + setMaximumWidth(width); + setMinimumWidth(width); + setMaximumHeight(height); + setMinimumHeight(height); + pic = new CardInfoPicture(width); + addWidget(pic); + text = new CardInfoText(); + addWidget(text); + connect(pic, SIGNAL(hasPictureChanged()), this, SLOT(hasPictureChanged())); setCard(db->getCard(cardName)); } @@ -19,10 +30,9 @@ void CardFrame::setCard(CardInfo *card) if (info) disconnect(info, 0, this, 0); info = card; - connect(info, SIGNAL(pixmapUpdated()), this, SLOT(updatePixmap())); connect(info, SIGNAL(destroyed()), this, SLOT(clear())); - - updatePixmap(); + text->setCard(info); + pic->setCard(info); } void CardFrame::setCard(const QString &cardName) @@ -40,22 +50,10 @@ void CardFrame::clear() setCard(db->getCard()); } -void CardFrame::updatePixmap() +void CardFrame::hasPictureChanged() { - qreal aspectRatio = (qreal) CARD_HEIGHT / (qreal) CARD_WIDTH; - qreal pixmapWidth = this->width(); - - if (pixmapWidth == 0) - return; - - QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)); - if (resizedPixmap) - this->setPixmap(*resizedPixmap); + if (pic->hasPicture() && !cardTextOnly) + setCurrentWidget(pic); else - this->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)))); -} - -QString CardFrame::getCardName() const -{ - return info->getName(); + setCurrentWidget(text); } diff --git a/cockatrice/src/cardframe.h b/cockatrice/src/cardframe.h index 1dccf821..c241c51a 100644 --- a/cockatrice/src/cardframe.h +++ b/cockatrice/src/cardframe.h @@ -1,32 +1,36 @@ #ifndef CARDFRAME_H #define CARDFRAME_H -#include -#include +#include class AbstractCardItem; class CardInfo; -class QResizeEvent; +class CardInfoPicture; +class CardInfoText; -class CardFrame : public QLabel { +class CardFrame : public QStackedWidget { Q_OBJECT private: CardInfo *info; + CardInfoPicture *pic; + CardInfoText *text; + bool cardTextOnly; public: - CardFrame(const QString &cardName = QString(), QWidget *parent = 0); - QString getCardName() const; + CardFrame(int width, int height, const QString &cardName = QString(), + QWidget *parent = 0); + void setCardTextOnly(bool status) { cardTextOnly = status; hasPictureChanged(); } public slots: void setCard(CardInfo *card); void setCard(const QString &cardName); void setCard(AbstractCardItem *card); + void clear(); private slots: - void clear(); - void updatePixmap(); - + void hasPictureChanged(); + void toggleCardTextOnly() { setCardTextOnly(!cardTextOnly); } }; #endif diff --git a/cockatrice/src/cardinfopicture.cpp b/cockatrice/src/cardinfopicture.cpp new file mode 100644 index 00000000..7a7d6195 --- /dev/null +++ b/cockatrice/src/cardinfopicture.cpp @@ -0,0 +1,53 @@ +#include "cardinfopicture.h" + +#include +#include "carditem.h" +#include "carddatabase.h" +#include "main.h" + +CardInfoPicture::CardInfoPicture(int maximumWidth, QWidget *parent) + : QLabel(parent) + , info(0) + , noPicture(true) +{ + setMaximumWidth(maximumWidth); +} + +void CardInfoPicture::setNoPicture(bool status) +{ + if (noPicture != status) { + noPicture = status; + emit hasPictureChanged(); + } +} + +void CardInfoPicture::setCard(CardInfo *card) +{ + if (info) + disconnect(info, 0, this, 0); + info = card; + connect(info, SIGNAL(pixmapUpdated()), this, SLOT(updatePixmap())); + + updatePixmap(); +} + +void CardInfoPicture::updatePixmap() +{ + qreal aspectRatio = (qreal) CARD_HEIGHT / (qreal) CARD_WIDTH; + qreal pixmapWidth = this->width(); + + if (pixmapWidth == 0) { + setNoPicture(true); + return; + } + + QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)); + if (resizedPixmap) { + setNoPicture(false); + this->setPixmap(*resizedPixmap); + } + else { + setNoPicture(true); + this->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)))); + } +} diff --git a/cockatrice/src/cardinfopicture.h b/cockatrice/src/cardinfopicture.h new file mode 100644 index 00000000..9f34da94 --- /dev/null +++ b/cockatrice/src/cardinfopicture.h @@ -0,0 +1,34 @@ +#ifndef CARDINFOPICTURE_H +#define CARDINFOPICTURE_H + +#include + +class AbstractCardItem; +class CardInfo; + +class CardInfoPicture : public QLabel { + Q_OBJECT + +signals: + void hasPictureChanged(); + +private: + CardInfo *info; + bool noPicture; + +public: + CardInfoPicture(int maximumWidth, QWidget *parent = 0); + bool hasPicture() const { return !noPicture; } + +private: + void setNoPicture(bool status); + +public slots: + void setCard(CardInfo *card); + +private slots: + void updatePixmap(); + +}; + +#endif diff --git a/cockatrice/src/cardinfotext.cpp b/cockatrice/src/cardinfotext.cpp new file mode 100644 index 00000000..9e59f33b --- /dev/null +++ b/cockatrice/src/cardinfotext.cpp @@ -0,0 +1,67 @@ +#include "cardinfotext.h" + +#include +#include +#include +#include "carditem.h" +#include "carddatabase.h" +#include "main.h" + +CardInfoText::CardInfoText(QWidget *parent) + : QFrame(parent) + , info(0) +{ + nameLabel1 = new QLabel; + nameLabel2 = new QLabel; + nameLabel2->setWordWrap(true); + manacostLabel1 = new QLabel; + manacostLabel2 = new QLabel; + manacostLabel2->setWordWrap(true); + cardtypeLabel1 = new QLabel; + cardtypeLabel2 = new QLabel; + cardtypeLabel2->setWordWrap(true); + powtoughLabel1 = new QLabel; + powtoughLabel2 = new QLabel; + loyaltyLabel1 = new QLabel; + loyaltyLabel2 = new QLabel; + + textLabel = new QTextEdit(); + textLabel->setReadOnly(true); + + QGridLayout *grid = new QGridLayout(this); + int row = 0; + grid->addWidget(nameLabel1, row, 0); + grid->addWidget(nameLabel2, row++, 1); + grid->addWidget(manacostLabel1, row, 0); + grid->addWidget(manacostLabel2, row++, 1); + grid->addWidget(cardtypeLabel1, row, 0); + grid->addWidget(cardtypeLabel2, row++, 1); + grid->addWidget(powtoughLabel1, row, 0); + grid->addWidget(powtoughLabel2, row++, 1); + grid->addWidget(loyaltyLabel1, row, 0); + grid->addWidget(loyaltyLabel2, row++, 1); + grid->addWidget(textLabel, row, 0, -1, 2); + grid->setRowStretch(row, 1); + grid->setColumnStretch(1, 1); + + retranslateUi(); +} + +void CardInfoText::setCard(CardInfo *card) +{ + nameLabel2->setText(card->getName()); + manacostLabel2->setText(card->getManaCost()); + cardtypeLabel2->setText(card->getCardType()); + powtoughLabel2->setText(card->getPowTough()); + loyaltyLabel2->setText(card->getLoyalty() > 0 ? QString::number(card->getLoyalty()) : QString()); + textLabel->setText(card->getText()); +} + +void CardInfoText::retranslateUi() +{ + nameLabel1->setText(tr("Name:")); + manacostLabel1->setText(tr("Mana cost:")); + cardtypeLabel1->setText(tr("Card type:")); + powtoughLabel1->setText(tr("P / T:")); + loyaltyLabel1->setText(tr("Loyalty:")); +} diff --git a/cockatrice/src/cardinfotext.h b/cockatrice/src/cardinfotext.h new file mode 100644 index 00000000..0b555230 --- /dev/null +++ b/cockatrice/src/cardinfotext.h @@ -0,0 +1,31 @@ +#ifndef CARDINFOTEXT_H +#define CARDINFOTEXT_H + +#include + +class QLabel; +class QTextEdit; +class CardInfo; + +class CardInfoText : public QFrame { + Q_OBJECT + +private: + QLabel *nameLabel1, *nameLabel2; + QLabel *manacostLabel1, *manacostLabel2; + QLabel *cardtypeLabel1, *cardtypeLabel2; + QLabel *powtoughLabel1, *powtoughLabel2; + QLabel *loyaltyLabel1, *loyaltyLabel2; + QTextEdit *textLabel; + + CardInfo *info; + +public: + CardInfoText(QWidget *parent = 0); + void retranslateUi(); + +public slots: + void setCard(CardInfo *card); +}; + +#endif diff --git a/cockatrice/src/tab_deck_editor.cpp b/cockatrice/src/tab_deck_editor.cpp index 2de449f4..38179197 100644 --- a/cockatrice/src/tab_deck_editor.cpp +++ b/cockatrice/src/tab_deck_editor.cpp @@ -87,7 +87,11 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) leftFrame->addLayout(searchLayout); leftFrame->addWidget(databaseView); - cardInfo = new CardFrame(); + cardInfo = new CardFrame(250, 356); + aCardTextOnly = new QAction(QString(), this); + aCardTextOnly->setCheckable(true); + connect(aCardTextOnly, SIGNAL(triggered()), cardInfo, SLOT(toggleCardTextOnly())); + filterModel = new FilterTreeModel(); databaseDisplayModel->setFilterTree(filterModel->filterTree()); filterView = new QTreeView; @@ -221,6 +225,7 @@ TabDeckEditor::TabDeckEditor(TabSupervisor *_tabSupervisor, QWidget *parent) dbMenu->addAction(aEditTokens); dbMenu->addSeparator(); dbMenu->addAction(aClearSearch); + dbMenu->addAction(aCardTextOnly); addTabMenu(dbMenu); aAddCard = new QAction(QString(), this); @@ -258,6 +263,7 @@ TabDeckEditor::~TabDeckEditor() void TabDeckEditor::retranslateUi() { + aCardTextOnly->setText(tr("&Show card text only")); aClearSearch->setText(tr("&Clear search")); searchLabel->setText(tr("&Search for:")); diff --git a/cockatrice/src/tab_deck_editor.h b/cockatrice/src/tab_deck_editor.h index 3554df82..0821e605 100644 --- a/cockatrice/src/tab_deck_editor.h +++ b/cockatrice/src/tab_deck_editor.h @@ -86,7 +86,7 @@ private: QMenu *deckMenu, *dbMenu; QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs, *aLoadDeckFromClipboard, *aSaveDeckToClipboard, *aPrintDeck, *aAnalyzeDeck, *aClose; - QAction *aEditSets, *aEditTokens, *aClearSearch; + QAction *aEditSets, *aEditTokens, *aClearSearch, *aCardTextOnly; QAction *aAddCard, *aAddCardToSideboard, *aRemoveCard, *aIncrement, *aDecrement, *aUpdatePrices; bool modified;