diff --git a/cockatrice/src/carddatabasemodel.cpp b/cockatrice/src/carddatabasemodel.cpp index 9cc6da8c..eeca8bf3 100644 --- a/cockatrice/src/carddatabasemodel.cpp +++ b/cockatrice/src/carddatabasemodel.cpp @@ -80,7 +80,7 @@ CardDatabaseDisplayModel::CardDatabaseDisplayModel(QObject *parent) bool CardDatabaseDisplayModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const { - CardInfo *info = static_cast(sourceModel())->getCard(sourceRow); + CardInfo const *info = static_cast(sourceModel())->getCard(sourceRow); if (((isToken == ShowTrue) && !info->getIsToken()) || (isToken == ShowFalse) && info->getIsToken()) return false; @@ -88,11 +88,15 @@ bool CardDatabaseDisplayModel::filterAcceptsRow(int sourceRow, const QModelIndex if (!cardNameBeginning.isEmpty()) if (!info->getName().startsWith(cardNameBeginning, Qt::CaseInsensitive)) return false; - + if (!cardName.isEmpty()) if (!info->getName().contains(cardName, Qt::CaseInsensitive)) return false; + if (!cardNameSet.isEmpty()) + if (!cardNameSet.contains(info->getName())) + return false; + if (!cardText.isEmpty()) if (!info->getText().contains(cardText, Qt::CaseInsensitive)) return false; diff --git a/cockatrice/src/carddatabasemodel.h b/cockatrice/src/carddatabasemodel.h index 40df3cf1..79475f4d 100644 --- a/cockatrice/src/carddatabasemodel.h +++ b/cockatrice/src/carddatabasemodel.h @@ -16,7 +16,7 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - CardInfo *getCard(int index) const { return cardList[index]; } + CardInfo const *getCard(int index) const { return cardList[index]; } private: QList cardList; CardDatabase *db; @@ -31,12 +31,13 @@ public: private: FilterBool isToken; QString cardNameBeginning, cardName, cardText; - QSet cardTypes, cardColors; + QSet cardNameSet, cardTypes, cardColors; public: CardDatabaseDisplayModel(QObject *parent = 0); void setIsToken(FilterBool _isToken) { isToken = _isToken; invalidate(); } void setCardNameBeginning(const QString &_beginning) { cardNameBeginning = _beginning; invalidate(); } void setCardName(const QString &_cardName) { cardName = _cardName; invalidate(); } + void setCardNameSet(const QSet &_cardNameSet) { cardNameSet = _cardNameSet; invalidate(); } void setCardText(const QString &_cardText) { cardText = _cardText; invalidate(); } void setCardTypes(const QSet &_cardTypes) { cardTypes = _cardTypes; invalidate(); } void setCardColors(const QSet &_cardColors) { cardColors = _cardColors; invalidate(); } diff --git a/cockatrice/src/dlg_create_token.cpp b/cockatrice/src/dlg_create_token.cpp index 6e731fc8..62ec4076 100644 --- a/cockatrice/src/dlg_create_token.cpp +++ b/cockatrice/src/dlg_create_token.cpp @@ -9,13 +9,14 @@ #include #include #include +#include #include "decklist.h" #include "dlg_create_token.h" #include "carddatabasemodel.h" #include "main.h" -DlgCreateToken::DlgCreateToken(DeckList *_deck, QWidget *parent) - : QDialog(parent) +DlgCreateToken::DlgCreateToken(const QStringList &_predefinedTokens, QWidget *parent) + : QDialog(parent), predefinedTokens(_predefinedTokens) { nameLabel = new QLabel(tr("&Name:")); nameEdit = new QLineEdit(tr("Token")); @@ -30,7 +31,7 @@ DlgCreateToken::DlgCreateToken(DeckList *_deck, QWidget *parent) colorEdit->addItem(tr("red"), "r"); colorEdit->addItem(tr("green"), "g"); colorEdit->addItem(tr("multicolor"), "m"); - colorEdit->addItem(tr("colorless"), ""); + colorEdit->addItem(tr("colorless"), QString()); colorLabel->setBuddy(colorEdit); ptLabel = new QLabel(tr("&P/T:")); @@ -63,10 +64,31 @@ DlgCreateToken::DlgCreateToken(DeckList *_deck, QWidget *parent) cardDatabaseDisplayModel->setSourceModel(cardDatabaseModel); cardDatabaseDisplayModel->setIsToken(CardDatabaseDisplayModel::ShowTrue); - QRadioButton *chooseTokenFromAllRadioButton = new QRadioButton(tr("Show &all tokens")); - QRadioButton *chooseTokenFromDeckRadioButton = new QRadioButton(tr("Show tokens from this &deck")); + chooseTokenFromAllRadioButton = new QRadioButton(tr("Show &all tokens")); + connect(chooseTokenFromAllRadioButton, SIGNAL(toggled(bool)), this, SLOT(actChooseTokenFromAll(bool))); + chooseTokenFromDeckRadioButton = new QRadioButton(tr("Show tokens from this &deck")); + connect(chooseTokenFromDeckRadioButton, SIGNAL(toggled(bool)), this, SLOT(actChooseTokenFromDeck(bool))); QTreeView *chooseTokenView = new QTreeView; chooseTokenView->setModel(cardDatabaseDisplayModel); + chooseTokenView->setUniformRowHeights(true); + chooseTokenView->setRootIsDecorated(false); + chooseTokenView->setAlternatingRowColors(true); + chooseTokenView->setSortingEnabled(true); + chooseTokenView->sortByColumn(0, Qt::AscendingOrder); + chooseTokenView->resizeColumnToContents(0); + chooseTokenView->header()->setStretchLastSection(false); + chooseTokenView->header()->hideSection(1); + chooseTokenView->header()->hideSection(2); + chooseTokenView->header()->setResizeMode(3, QHeaderView::ResizeToContents); + chooseTokenView->header()->setResizeMode(4, QHeaderView::ResizeToContents); + connect(chooseTokenView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), this, SLOT(tokenSelectionChanged(QModelIndex, QModelIndex))); + + if (predefinedTokens.isEmpty()) + chooseTokenFromAllRadioButton->setChecked(true); + else { + chooseTokenFromDeckRadioButton->setChecked(true); + cardDatabaseDisplayModel->setCardNameSet(QSet::fromList(predefinedTokens)); + } QVBoxLayout *tokenChooseLayout = new QVBoxLayout; tokenChooseLayout->addWidget(chooseTokenFromAllRadioButton); @@ -76,8 +98,12 @@ DlgCreateToken::DlgCreateToken(DeckList *_deck, QWidget *parent) QGroupBox *tokenChooseGroupBox = new QGroupBox(tr("Choose token from list")); tokenChooseGroupBox->setLayout(tokenChooseLayout); + QVBoxLayout *leftVBox = new QVBoxLayout; + leftVBox->addWidget(tokenDataGroupBox); + leftVBox->addStretch(); + QHBoxLayout *hbox = new QHBoxLayout; - hbox->addWidget(tokenDataGroupBox); + hbox->addLayout(leftVBox); hbox->addWidget(tokenChooseGroupBox); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); @@ -94,6 +120,30 @@ DlgCreateToken::DlgCreateToken(DeckList *_deck, QWidget *parent) setMinimumWidth(300); } +void DlgCreateToken::tokenSelectionChanged(const QModelIndex ¤t, const QModelIndex & /*previous*/) +{ + const QModelIndex realIndex = cardDatabaseDisplayModel->mapToSource(current); + const CardInfo *cardInfo = cardDatabaseModel->getCard(realIndex.row()); + + nameEdit->setText(cardInfo->getName()); + const QString cardColor = cardInfo->getColors().isEmpty() ? QString() : (cardInfo->getColors().size() > 1 ? QString("m") : cardInfo->getColors().first()); + colorEdit->setCurrentIndex(colorEdit->findData(cardColor, Qt::UserRole, Qt::MatchFixedString)); + ptEdit->setText(cardInfo->getPowTough()); + annotationEdit->setText(cardInfo->getText()); +} + +void DlgCreateToken::actChooseTokenFromAll(bool checked) +{ + if (checked) + cardDatabaseDisplayModel->setCardNameSet(QSet()); +} + +void DlgCreateToken::actChooseTokenFromDeck(bool checked) +{ + if (checked) + cardDatabaseDisplayModel->setCardNameSet(QSet::fromList(predefinedTokens)); +} + void DlgCreateToken::actOk() { accept(); diff --git a/cockatrice/src/dlg_create_token.h b/cockatrice/src/dlg_create_token.h index 1b943016..2bc371fe 100644 --- a/cockatrice/src/dlg_create_token.h +++ b/cockatrice/src/dlg_create_token.h @@ -2,12 +2,14 @@ #define DLG_CREATETOKEN_H #include -#include +#include class QLabel; +class QLineEdit; class QComboBox; class QCheckBox; class QPushButton; +class QRadioButton; class DeckList; class CardDatabaseModel; class CardDatabaseDisplayModel; @@ -15,21 +17,26 @@ class CardDatabaseDisplayModel; class DlgCreateToken : public QDialog { Q_OBJECT public: - DlgCreateToken(DeckList *_deck, QWidget *parent = 0); + DlgCreateToken(const QStringList &_predefinedTokens, QWidget *parent = 0); QString getName() const; QString getColor() const; QString getPT() const; QString getAnnotation() const; bool getDestroy() const; private slots: + void tokenSelectionChanged(const QModelIndex ¤t, const QModelIndex &previous); + void actChooseTokenFromAll(bool checked); + void actChooseTokenFromDeck(bool checked); void actOk(); private: CardDatabaseModel *cardDatabaseModel; CardDatabaseDisplayModel *cardDatabaseDisplayModel; + QStringList predefinedTokens; QLabel *nameLabel, *colorLabel, *ptLabel, *annotationLabel; QComboBox *colorEdit; QLineEdit *nameEdit, *ptEdit, *annotationEdit; QCheckBox *destroyCheckBox; + QRadioButton *chooseTokenFromAllRadioButton, *chooseTokenFromDeckRadioButton; }; #endif diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index ad479067..2b656161 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -18,6 +18,8 @@ #include "dlg_create_token.h" #include "carddatabase.h" #include "color.h" +#include "decklist.h" +#include "main.h" #include #include #include @@ -307,6 +309,8 @@ Player::Player(const ServerInfo_User &info, int _id, bool _local, TabGame *_pare aCreateAnotherToken = new QAction(this); connect(aCreateAnotherToken, SIGNAL(triggered()), this, SLOT(actCreateAnotherToken())); aCreateAnotherToken->setEnabled(false); + + createPredefinedTokenMenu = new QMenu(QString()); playerMenu->addSeparator(); countersMenu = playerMenu->addMenu(QString()); @@ -317,6 +321,7 @@ Player::Player(const ServerInfo_User &info, int _id, bool _local, TabGame *_pare playerMenu->addSeparator(); playerMenu->addAction(aCreateToken); playerMenu->addAction(aCreateAnotherToken); + playerMenu->addMenu(createPredefinedTokenMenu); playerMenu->addSeparator(); sayMenu = playerMenu->addMenu(QString()); initSayMenu(); @@ -332,11 +337,11 @@ Player::Player(const ServerInfo_User &info, int _id, bool _local, TabGame *_pare allPlayersActions.append(newAction); playerLists[i]->addSeparator(); } - } else { countersMenu = 0; sbMenu = 0; aCreateAnotherToken = 0; + createPredefinedTokenMenu = 0; aCardMenu = 0; } @@ -611,6 +616,7 @@ void Player::retranslateUi() aRollDie->setText(tr("R&oll die...")); aCreateToken->setText(tr("&Create token...")); aCreateAnotherToken->setText(tr("C&reate another token")); + createPredefinedTokenMenu->setTitle(tr("Cr&eate predefined token")); sayMenu->setTitle(tr("S&ay")); QMapIterator counterIterator(counters); @@ -735,6 +741,24 @@ void Player::initSayMenu() } } +void Player::setDeck(DeckList *_deck) +{ + deck = _deck; + + createPredefinedTokenMenu->clear(); + predefinedTokens.clear(); + InnerDecklistNode *tokenZone = dynamic_cast(deck->getRoot()->findChild("tokens")); + if (tokenZone) + for (int i = 0; i < tokenZone->size(); ++i) { + const QString tokenName = tokenZone->at(i)->getName(); + predefinedTokens.append(tokenName); + QAction *a = createPredefinedTokenMenu->addAction(tokenName); + if (i < 10) + a->setShortcut("Alt+" + QString::number((i + 1) % 10)); + connect(a, SIGNAL(triggered()), this, SLOT(actCreatePredefinedToken())); + } +} + void Player::actViewLibrary() { static_cast(scene())->toggleZoneView(this, "deck", -1); @@ -888,7 +912,7 @@ void Player::actRollDie() void Player::actCreateToken() { - DlgCreateToken dlg(deck); + DlgCreateToken dlg(predefinedTokens); if (!dlg.exec()) return; @@ -917,6 +941,21 @@ void Player::actCreateAnotherToken() sendGameCommand(cmd); } +void Player::actCreatePredefinedToken() +{ + QAction *action = static_cast(sender()); + CardInfo *cardInfo = db->getCard(action->text()); + + lastTokenName = cardInfo->getName(); + lastTokenColor = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().first(); + lastTokenPT = cardInfo->getPowTough(); + lastTokenAnnotation = cardInfo->getText(); + lastTokenDestroy = true; + aCreateAnotherToken->setEnabled(true); + + actCreateAnotherToken(); +} + void Player::actSayMessage() { QAction *a = qobject_cast(sender()); diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 79fb4ecf..b52b6b87 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -134,6 +134,7 @@ private slots: void updateBoundingRect(); void rearrangeZones(); + void actCreatePredefinedToken(); void cardMenuAction(); void actCardCounterTrigger(); void actAttach(); @@ -153,7 +154,7 @@ private slots: private: TabGame *game; - QMenu *playerMenu, *handMenu, *graveMenu, *rfgMenu, *libraryMenu, *sbMenu, *countersMenu, *sayMenu, + QMenu *playerMenu, *handMenu, *graveMenu, *rfgMenu, *libraryMenu, *sbMenu, *countersMenu, *sayMenu, *createPredefinedTokenMenu, *mRevealLibrary, *mRevealTopCard, *mRevealHand, *mRevealRandomHandCard; QList playerLists; QList allPlayersActions; @@ -189,6 +190,8 @@ private: QList cardsToDelete; DeckList *deck; + QStringList predefinedTokens; + PlayerArea *playerArea; QMap zones; StackZone *stack; @@ -258,8 +261,7 @@ public: void retranslateUi(); void clear(); TabGame *getGame() const { return game; } - DeckList *getDeck() const { return deck; } - void setDeck(DeckList *_deck) { deck = _deck; } + void setDeck(DeckList *_deck); QMenu *getPlayerMenu() const { return playerMenu; } int getId() const { return id; } QString getName() const; diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index f0ee0848..e322dcf7 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -935,6 +935,7 @@ void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*e DeckList *newDeck = new DeckList(QString::fromStdString(playerInfo.deck_list())); db->cacheCardPixmaps(newDeck->getCardList()); deckViewContainer->setDeck(newDeck); + player->setDeck(newDeck); } deckViewContainer->setReadyStart(prop.ready_start()); deckViewContainer->setSideboardLocked(prop.sideboard_locked());