From bc52882ac4526a8091f99a13092f9ffc5fec6f21 Mon Sep 17 00:00:00 2001 From: Zach H Date: Fri, 19 Jan 2018 20:34:14 -0500 Subject: [PATCH] Fix Bad Gameplay (#3034) --- cockatrice/src/player.cpp | 4 +- cockatrice/src/player.h | 498 +++++++++++---------- cockatrice/src/tab_game.cpp | 688 +++++++++--------------------- cockatrice/src/tab_game.h | 382 ++++++++--------- cockatrice/src/tab_supervisor.cpp | 368 +++++----------- cockatrice/src/tab_supervisor.h | 167 ++++---- 6 files changed, 805 insertions(+), 1302 deletions(-) diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 201a25f2..b7cf0e33 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -2740,10 +2740,8 @@ qreal Player::getMinimumWidth() const void Player::setGameStarted() { - if (local && aAlwaysRevealTopCard) - { + if (local) aAlwaysRevealTopCard->setChecked(false); - } setConceded(false); } diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 2be1594d..e1f1d295 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -59,276 +59,268 @@ class PendingCommand; const int MAX_TOKENS_PER_DIALOG = 99; -class PlayerArea : public QObject, public QGraphicsItem -{ +class PlayerArea : public QObject, public QGraphicsItem { Q_OBJECT Q_INTERFACES(QGraphicsItem) - private: - QRectF bRect; - - private slots: - void updateBg(); - - public: - enum { Type = typeOther }; - int type() const override - { return Type; } - - explicit PlayerArea(QGraphicsItem *parent = nullptr); - QRectF boundingRect() const override { return bRect; } - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; - - void setSize(qreal width, qreal height); +private: + QRectF bRect; +private slots: + void updateBg(); +public: + enum { Type = typeOther }; + int type() const { return Type; } + + PlayerArea(QGraphicsItem *parent = 0); + QRectF boundingRect() const { return bRect; } + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void setSize(qreal width, qreal height); }; -class Player : public QObject, public QGraphicsItem -{ +class Player : public QObject, public QGraphicsItem { Q_OBJECT Q_INTERFACES(QGraphicsItem) +signals: + void openDeckEditor(const DeckLoader *deck); + void newCardAdded(AbstractCardItem *card); + // Log events + void logSay(Player *player, QString message); + void logShuffle(Player *player, CardZone *zone); + void logRollDie(Player *player, int sides, int roll); + void logCreateArrow(Player *player, Player *startPlayer, QString startCard, Player *targetPlayer, QString targetCard, bool _playerTarget); + void logCreateToken(Player *player, QString cardName, QString pt); + void logDrawCards(Player *player, int number); + void logUndoDraw(Player *player, QString cardName); + void logMoveCard(Player *player, CardItem *card, CardZone *startZone, int oldX, CardZone *targetZone, int newX); + void logFlipCard(Player *player, QString cardName, bool faceDown); + void logDestroyCard(Player *player, QString cardName); + void logAttachCard(Player *player, QString cardName, Player *targetPlayer, QString targetCardName); + void logUnattachCard(Player *player, QString cardName); + void logSetCardCounter(Player *player, QString cardName, int counterId, int value, int oldValue); + void logSetTapped(Player *player, CardItem *card, bool tapped); + void logSetCounter(Player *player, QString counterName, int value, int oldValue); + void logSetDoesntUntap(Player *player, CardItem *card, bool doesntUntap); + void logSetPT(Player *player, CardItem *card, QString newPT); + void logSetAnnotation(Player *player, CardItem *card, QString newAnnotation); + void logDumpZone(Player *player, CardZone *zone, int numberCards); + void logStopDumpZone(Player *player, CardZone *zone); + void logRevealCards(Player *player, CardZone *zone, int cardId, QString cardName, Player *otherPlayer, bool faceDown); + void logAlwaysRevealTopCard(Player *player, CardZone *zone, bool reveal); + + void sizeChanged(); + void gameConceded(); +public slots: + void actUntapAll(); + void actRollDie(); + void actCreateToken(); + void actCreateAnotherToken(); + void actShuffle(); + void actDrawCard(); + void actDrawCards(); + void actUndoDraw(); + void actMulligan(); + void actMoveTopCardToPlayFaceDown(); + void actMoveTopCardsToGrave(); + void actMoveTopCardsToExile(); + void actMoveTopCardToBottom(); + void actMoveBottomCardToGrave(); - signals: - void openDeckEditor(const DeckLoader *deck); - void newCardAdded(AbstractCardItem *card); - // Log events - void logSay(Player *player, QString message); - void logShuffle(Player *player, CardZone *zone); - void logRollDie(Player *player, int sides, int roll); - void logCreateArrow(Player *player, Player *startPlayer, QString startCard, Player *targetPlayer, QString targetCard, bool _playerTarget); - void logCreateToken(Player *player, QString cardName, QString pt); - void logDrawCards(Player *player, int number); - void logUndoDraw(Player *player, QString cardName); - void logMoveCard(Player *player, CardItem *card, CardZone *startZone, int oldX, CardZone *targetZone, int newX); - void logFlipCard(Player *player, QString cardName, bool faceDown); - void logDestroyCard(Player *player, QString cardName); - void logAttachCard(Player *player, QString cardName, Player *targetPlayer, QString targetCardName); - void logUnattachCard(Player *player, QString cardName); - void logSetCardCounter(Player *player, QString cardName, int counterId, int value, int oldValue); - void logSetTapped(Player *player, CardItem *card, bool tapped); - void logSetCounter(Player *player, QString counterName, int value, int oldValue); - void logSetDoesntUntap(Player *player, CardItem *card, bool doesntUntap); - void logSetPT(Player *player, CardItem *card, QString newPT); - void logSetAnnotation(Player *player, CardItem *card, QString newAnnotation); - void logDumpZone(Player *player, CardZone *zone, int numberCards); - void logStopDumpZone(Player *player, CardZone *zone); - void logRevealCards(Player *player, CardZone *zone, int cardId, QString cardName, Player *otherPlayer, bool faceDown); - void logAlwaysRevealTopCard(Player *player, CardZone *zone, bool reveal); + void actViewLibrary(); + void actViewTopCards(); + void actAlwaysRevealTopCard(); + void actViewGraveyard(); + void actRevealRandomGraveyardCard(); + void actViewRfg(); + void actViewSideboard(); + + void actSayMessage(); +private slots: + void addPlayer(Player *player); + void removePlayer(Player *player); + void playerListActionTriggered(); + + void updateBoundingRect(); + void rearrangeZones(); + + void actOpenDeckInDeckEditor(); + void actCreatePredefinedToken(); + void actCreateRelatedCard(); + void actCreateAllRelatedCards(); + void cardMenuAction(); + void actMoveCardXCardsFromTop(); + void actCardCounterTrigger(); + void actAttach(); + void actUnattach(); + void actDrawArrow(); + void actIncPT(int deltaP, int deltaT); + void actSetPT(); + void actIncP(); + void actDecP(); + void actIncT(); + void actDecT(); + void actIncPT(); + void actDecPT(); + void actSetAnnotation(); + void actPlay(); + void actHide(); + void actPlayFacedown(); + void refreshShortcuts(); - void sizeChanged(); - void gameConceded(); +private: + TabGame *game; + QMenu *playerMenu, *handMenu, *moveHandMenu, *graveMenu, *moveGraveMenu, *rfgMenu, *moveRfgMenu, *libraryMenu, *sbMenu, *countersMenu, *sayMenu, *createPredefinedTokenMenu, + *mRevealLibrary, *mRevealTopCard, *mRevealHand, *mRevealRandomHandCard, *mRevealRandomGraveyardCard; + QList playerLists; + QList allPlayersActions; + QAction *aMoveHandToTopLibrary, *aMoveHandToBottomLibrary, *aMoveHandToGrave, *aMoveHandToRfg, + *aMoveGraveToTopLibrary, *aMoveGraveToBottomLibrary, *aMoveGraveToHand, *aMoveGraveToRfg, + *aMoveRfgToTopLibrary, *aMoveRfgToBottomLibrary, *aMoveRfgToHand, *aMoveRfgToGrave, + *aViewLibrary, *aViewTopCards, *aAlwaysRevealTopCard, *aOpenDeckInDeckEditor, *aMoveTopCardsToGrave, *aMoveTopCardsToExile, *aMoveTopCardToBottom, + *aViewGraveyard, *aViewRfg, *aViewSideboard, + *aDrawCard, *aDrawCards, *aUndoDraw, *aMulligan, *aShuffle, *aMoveTopToPlayFaceDown, + *aUntapAll, *aRollDie, *aCreateToken, *aCreateAnotherToken, + *aCardMenu, *aMoveBottomCardToGrave; + + QList aAddCounter, aSetCounter, aRemoveCounter; + QAction *aPlay, *aPlayFacedown, + *aHide, + *aTap, *aDoesntUntap, *aAttach, *aUnattach, *aDrawArrow, *aSetPT, *aIncP, *aDecP, *aIncT, *aDecT, *aIncPT, *aDecPT, *aSetAnnotation, *aFlip, *aPeek, *aClone, + *aMoveToTopLibrary, *aMoveToBottomLibrary, *aMoveToHand, *aMoveToGraveyard, *aMoveToExile, *aMoveToXfromTopOfLibrary; - public slots: - void actUntapAll(); - void actRollDie(); - void actCreateToken(); - void actCreateAnotherToken(); - void actShuffle(); - void actDrawCard(); - void actDrawCards(); - void actUndoDraw(); - void actMulligan(); - void actMoveTopCardToPlayFaceDown(); - void actMoveTopCardsToGrave(); - void actMoveTopCardsToExile(); - void actMoveTopCardToBottom(); - void actMoveBottomCardToGrave(); + bool shortcutsActive; + int defaultNumberTopCards; + int defaultNumberTopCardsToPlaceBelow; + QString lastTokenName, lastTokenColor, lastTokenPT, lastTokenAnnotation; + bool lastTokenDestroy; + int lastTokenTableRow; + ServerInfo_User *userInfo; + int id; + bool active; + bool local; + bool mirrored; + bool handVisible; + bool conceded; + + bool dialogSemaphore; + bool clearCardsToDelete(); + QList cardsToDelete; + + DeckLoader *deck; + QStringList predefinedTokens; + + PlayerArea *playerArea; + QMap zones; + StackZone *stack; + TableZone *table; + HandZone *hand; + PlayerTarget *playerTarget; + + void setCardAttrHelper(const GameEventContext &context, CardItem *card, CardAttribute attribute, const QString &avalue, bool allCards); + void addRelatedCardActions(const CardItem *card, QMenu *cardMenu); + void createCard(const CardItem *sourceCard, const QString &dbCardName, bool attach = false); + void createAttachedCard(const CardItem *sourceCard, const QString &dbCardName); + bool createRelatedFromRelation(const CardItem *sourceCard, const CardRelation *cardRelation); + QString dbNameFromTokenDisplayName(const QString &tokenName); - void actViewLibrary(); - void actViewTopCards(); - void actAlwaysRevealTopCard(); - void actViewGraveyard(); - void actRevealRandomGraveyardCard(); - void actViewRfg(); - void actViewSideboard(); + QRectF bRect; - void actSayMessage(); + QMap counters; + QMap arrows; + void rearrangeCounters(); + + void initSayMenu(); + + void eventConnectionStateChanged(const Event_ConnectionStateChanged &event); + void eventGameSay(const Event_GameSay &event); + void eventShuffle(const Event_Shuffle &event); + void eventRollDie(const Event_RollDie &event); + void eventCreateArrow(const Event_CreateArrow &event); + void eventDeleteArrow(const Event_DeleteArrow &event); + void eventCreateToken(const Event_CreateToken &event); + void eventSetCardAttr(const Event_SetCardAttr &event, const GameEventContext &context); + void eventSetCardCounter(const Event_SetCardCounter &event); + void eventCreateCounter(const Event_CreateCounter &event); + void eventSetCounter(const Event_SetCounter &event); + void eventDelCounter(const Event_DelCounter &event); + void eventDumpZone(const Event_DumpZone &event); + void eventStopDumpZone(const Event_StopDumpZone &event); + void eventMoveCard(const Event_MoveCard &event, const GameEventContext &context); + void eventFlipCard(const Event_FlipCard &event); + void eventDestroyCard(const Event_DestroyCard &event); + void eventAttachCard(const Event_AttachCard &event); + void eventDrawCards(const Event_DrawCards &event); + void eventRevealCards(const Event_RevealCards &event); + void eventChangeZoneProperties(const Event_ChangeZoneProperties &event); +public: + static const int counterAreaWidth = 55; + enum CardMenuActionType { cmTap, cmUntap, cmDoesntUntap, cmFlip, cmPeek, cmClone, cmMoveToTopLibrary, cmMoveToBottomLibrary, cmMoveToHand, cmMoveToGraveyard, cmMoveToExile }; + enum CardsToReveal {RANDOM_CARD_FROM_ZONE = -2}; - private slots: - void addPlayer(Player *player); - void removePlayer(Player *player); - void playerListActionTriggered(); + enum { Type = typeOther }; + int type() const { return Type; } + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void playCard(CardItem *c, bool faceDown, bool tapped); + void addCard(CardItem *c); + void deleteCard(CardItem *c); + void addZone(CardZone *z); - void updateBoundingRect(); - void rearrangeZones(); + AbstractCounter *addCounter(const ServerInfo_Counter &counter); + AbstractCounter *addCounter(int counterId, const QString &name, QColor color, int radius, int value); + void delCounter(int counterId); + void clearCounters(); + + ArrowItem *addArrow(const ServerInfo_Arrow &arrow); + ArrowItem *addArrow(int arrowId, CardItem *startCard, ArrowTarget *targetItem, const QColor &color); + void delArrow(int arrowId); + void removeArrow(ArrowItem *arrow); + void clearArrows(); + PlayerTarget *getPlayerTarget() const { return playerTarget; } - void actOpenDeckInDeckEditor(); - void actCreatePredefinedToken(); - void actCreateRelatedCard(); - void actCreateAllRelatedCards(); - void cardMenuAction(); - void actMoveCardXCardsFromTop(); - void actCardCounterTrigger(); - void actAttach(); - void actUnattach(); - void actDrawArrow(); - void actIncPT(int deltaP, int deltaT); - void actSetPT(); - void actIncP(); - void actDecP(); - void actIncT(); - void actDecT(); - void actIncPT(); - void actDecPT(); - void actSetAnnotation(); - void actPlay(); - void actHide(); - void actPlayFacedown(); - void refreshShortcuts(); + Player(const ServerInfo_User &info, int _id, bool _local, TabGame *_parent); + ~Player(); + void retranslateUi(); + void clear(); + TabGame *getGame() const { return game; } + void setDeck(const DeckLoader &_deck); + QMenu *getPlayerMenu() const { return playerMenu; } + int getId() const { return id; } + QString getName() const; + ServerInfo_User *getUserInfo() const { return userInfo; } + bool getLocal() const { return local; } + bool getMirrored() const { return mirrored; } + const QMap &getZones() const { return zones; } + const QMap &getArrows() const { return arrows; } + void setCardMenu(QMenu *menu); + QMenu *getCardMenu() const; + void updateCardMenu(const CardItem *card); + bool getActive() const { return active; } + void setActive(bool _active); + void setShortcutsActive(); + void setShortcutsInactive(); + void updateZones(); + + void setConceded(bool _conceded); + bool getConceded() const { return conceded; } - private: - TabGame *game; - QMenu *playerMenu, *handMenu, *moveHandMenu, *graveMenu, *moveGraveMenu, *rfgMenu, *moveRfgMenu, *libraryMenu, *sbMenu, *countersMenu, *sayMenu, *createPredefinedTokenMenu, - *mRevealLibrary, *mRevealTopCard, *mRevealHand, *mRevealRandomHandCard, *mRevealRandomGraveyardCard; - QList playerLists; - QList allPlayersActions; - QAction *aMoveHandToTopLibrary, *aMoveHandToBottomLibrary, *aMoveHandToGrave, *aMoveHandToRfg, - *aMoveGraveToTopLibrary, *aMoveGraveToBottomLibrary, *aMoveGraveToHand, *aMoveGraveToRfg, - *aMoveRfgToTopLibrary, *aMoveRfgToBottomLibrary, *aMoveRfgToHand, *aMoveRfgToGrave, - *aViewLibrary, *aViewTopCards, *aAlwaysRevealTopCard, *aOpenDeckInDeckEditor, *aMoveTopCardsToGrave, *aMoveTopCardsToExile, *aMoveTopCardToBottom, - *aViewGraveyard, *aViewRfg, *aViewSideboard, - *aDrawCard, *aDrawCards, *aUndoDraw, *aMulligan, *aShuffle, *aMoveTopToPlayFaceDown, - *aUntapAll, *aRollDie, *aCreateToken, *aCreateAnotherToken, - *aCardMenu, *aMoveBottomCardToGrave; + void setGameStarted(); + + qreal getMinimumWidth() const; + void setMirrored(bool _mirrored); + void processSceneSizeChange(int newPlayerWidth); + + void processPlayerInfo(const ServerInfo_Player &info); + void processCardAttachment(const ServerInfo_Player &info); + + void processGameEvent(GameEvent::GameEventType type, const GameEvent &event, const GameEventContext &context); - QList aAddCounter, aSetCounter, aRemoveCounter; - QAction *aPlay, *aPlayFacedown, - *aHide, - *aTap, *aDoesntUntap, *aAttach, *aUnattach, *aDrawArrow, *aSetPT, *aIncP, *aDecP, *aIncT, *aDecT, *aIncPT, *aDecPT, *aSetAnnotation, *aFlip, *aPeek, *aClone, - *aMoveToTopLibrary, *aMoveToBottomLibrary, *aMoveToHand, *aMoveToGraveyard, *aMoveToExile, *aMoveToXfromTopOfLibrary; + PendingCommand *prepareGameCommand(const ::google::protobuf::Message &cmd); + PendingCommand *prepareGameCommand(const QList< const ::google::protobuf::Message * > &cmdList); + void sendGameCommand(PendingCommand *pend); + void sendGameCommand(const google::protobuf::Message &command); - bool shortcutsActive; - int defaultNumberTopCards; - int defaultNumberTopCardsToPlaceBelow; - QString lastTokenName, lastTokenColor, lastTokenPT, lastTokenAnnotation; - bool lastTokenDestroy; - int lastTokenTableRow; - ServerInfo_User *userInfo; - int id; - bool active; - bool local; - bool mirrored; - bool handVisible; - bool conceded; - - bool dialogSemaphore; - bool clearCardsToDelete(); - QList cardsToDelete; - - DeckLoader *deck; - QStringList predefinedTokens; - - PlayerArea *playerArea; - QMap zones; - StackZone *stack; - TableZone *table; - HandZone *hand; - PlayerTarget *playerTarget; - - void setCardAttrHelper(const GameEventContext &context, CardItem *card, CardAttribute attribute, const QString &avalue, bool allCards); - void addRelatedCardActions(const CardItem *card, QMenu *cardMenu); - void createCard(const CardItem *sourceCard, const QString &dbCardName, bool attach = false); - void createAttachedCard(const CardItem *sourceCard, const QString &dbCardName); - bool createRelatedFromRelation(const CardItem *sourceCard, const CardRelation *cardRelation); - QString dbNameFromTokenDisplayName(const QString &tokenName); - - QRectF bRect; - - QMap counters; - QMap arrows; - void rearrangeCounters(); - - void initSayMenu(); - - void eventGameSay(const Event_GameSay &event); - void eventShuffle(const Event_Shuffle &event); - void eventRollDie(const Event_RollDie &event); - void eventCreateArrow(const Event_CreateArrow &event); - void eventDeleteArrow(const Event_DeleteArrow &event); - void eventCreateToken(const Event_CreateToken &event); - void eventSetCardAttr(const Event_SetCardAttr &event, const GameEventContext &context); - void eventSetCardCounter(const Event_SetCardCounter &event); - void eventCreateCounter(const Event_CreateCounter &event); - void eventSetCounter(const Event_SetCounter &event); - void eventDelCounter(const Event_DelCounter &event); - void eventDumpZone(const Event_DumpZone &event); - void eventStopDumpZone(const Event_StopDumpZone &event); - void eventMoveCard(const Event_MoveCard &event, const GameEventContext &context); - void eventFlipCard(const Event_FlipCard &event); - void eventDestroyCard(const Event_DestroyCard &event); - void eventAttachCard(const Event_AttachCard &event); - void eventDrawCards(const Event_DrawCards &event); - void eventRevealCards(const Event_RevealCards &event); - void eventChangeZoneProperties(const Event_ChangeZoneProperties &event); - - public: - static const int counterAreaWidth = 55; - enum CardMenuActionType { cmTap, cmUntap, cmDoesntUntap, cmFlip, cmPeek, cmClone, cmMoveToTopLibrary, cmMoveToBottomLibrary, cmMoveToHand, cmMoveToGraveyard, cmMoveToExile }; - enum CardsToReveal {RANDOM_CARD_FROM_ZONE = -2}; - - enum { Type = typeOther }; - int type() const override { return Type; } - QRectF boundingRect() const override; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; - - void playCard(CardItem *c, bool faceDown, bool tapped); - void addCard(CardItem *c); - void deleteCard(CardItem *c); - void addZone(CardZone *z); - - AbstractCounter *addCounter(const ServerInfo_Counter &counter); - AbstractCounter *addCounter(int counterId, const QString &name, QColor color, int radius, int value); - void delCounter(int counterId); - void clearCounters(); - - ArrowItem *addArrow(const ServerInfo_Arrow &arrow); - ArrowItem *addArrow(int arrowId, CardItem *startCard, ArrowTarget *targetItem, const QColor &color); - void delArrow(int arrowId); - void removeArrow(ArrowItem *arrow); - void clearArrows(); - PlayerTarget *getPlayerTarget() const { return playerTarget; } - - Player(const ServerInfo_User &info, int _id, bool _local, TabGame *_parent); - ~Player() override; - void retranslateUi(); - void clear(); - TabGame *getGame() const { return game; } - void setDeck(const DeckLoader &_deck); - QMenu *getPlayerMenu() const { return playerMenu; } - int getId() const { return id; } - QString getName() const; - ServerInfo_User *getUserInfo() const { return userInfo; } - bool getLocal() const { return local; } - bool getMirrored() const { return mirrored; } - const QMap &getZones() const { return zones; } - const QMap &getArrows() const { return arrows; } - void setCardMenu(QMenu *menu); - QMenu *getCardMenu() const; - void updateCardMenu(const CardItem *card); - bool getActive() const { return active; } - void setActive(bool _active); - void setShortcutsActive(); - void setShortcutsInactive(); - void updateZones(); - - void setConceded(bool _conceded); - bool getConceded() const { return conceded; } - - void setGameStarted(); - - qreal getMinimumWidth() const; - void setMirrored(bool _mirrored); - void processSceneSizeChange(int newPlayerWidth); - - void processPlayerInfo(const ServerInfo_Player &info); - void processCardAttachment(const ServerInfo_Player &info); - - void processGameEvent(GameEvent::GameEventType type, const GameEvent &event, const GameEventContext &context); - - PendingCommand *prepareGameCommand(const ::google::protobuf::Message &cmd); - PendingCommand *prepareGameCommand(const QList< const ::google::protobuf::Message * > &cmdList); - void sendGameCommand(PendingCommand *pend); - void sendGameCommand(const google::protobuf::Message &command); - - void setLastToken(CardInfo *cardInfo); + void setLastToken(CardInfo *cardInfo); }; #endif diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index a77c3ce7..4ebd9441 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -70,9 +70,9 @@ #include "pb/context_ping_changed.pb.h" #include "get_pb_extension.h" -ToggleButton::ToggleButton(QWidget *parent) : QPushButton(parent), state(false) +ToggleButton::ToggleButton(QWidget *parent) + : QPushButton(parent), state(false) { - } void ToggleButton::paintEvent(QPaintEvent *event) @@ -95,15 +95,13 @@ void ToggleButton::setState(bool _state) update(); } -DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent) : QWidget(nullptr), parentGame(parent), playerId(_playerId) +DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent) + : QWidget(0), parentGame(parent), playerId(_playerId) { loadLocalButton = new QPushButton; loadRemoteButton = new QPushButton; if (parentGame->getIsLocalGame()) - { loadRemoteButton->setEnabled(false); - } - readyStartButton = new ToggleButton; readyStartButton->setEnabled(false); sideboardLockButton = new ToggleButton; @@ -115,19 +113,18 @@ DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent) : QWidget(n connect(sideboardLockButton, SIGNAL(clicked()), this, SLOT(sideboardLockButtonClicked())); connect(sideboardLockButton, SIGNAL(stateChanged()), this, SLOT(updateSideboardLockButtonText())); - auto *buttonHBox = new QHBoxLayout; + QHBoxLayout *buttonHBox = new QHBoxLayout; buttonHBox->addWidget(loadLocalButton); buttonHBox->addWidget(loadRemoteButton); buttonHBox->addWidget(readyStartButton); buttonHBox->addWidget(sideboardLockButton); buttonHBox->setContentsMargins(0, 0, 0, 0); buttonHBox->addStretch(); - deckView = new DeckView; connect(deckView, SIGNAL(newCardAdded(AbstractCardItem *)), this, SIGNAL(newCardAdded(AbstractCardItem *))); connect(deckView, SIGNAL(sideboardPlanChanged()), this, SLOT(sideboardPlanChanged())); - auto *deckViewLayout = new QVBoxLayout; + QVBoxLayout *deckViewLayout = new QVBoxLayout; deckViewLayout->addLayout(buttonHBox); deckViewLayout->addWidget(deckView); deckViewLayout->setContentsMargins(0, 0, 0, 0); @@ -146,16 +143,20 @@ void DeckViewContainer::retranslateUi() updateSideboardLockButtonText(); } +void DeckViewContainer::setButtonsVisible(bool _visible) +{ + loadLocalButton->setVisible(_visible); + loadRemoteButton->setVisible(_visible); + readyStartButton->setVisible(_visible); + sideboardLockButton->setVisible(_visible); +} + void DeckViewContainer::updateSideboardLockButtonText() { if (sideboardLockButton->getState()) - { sideboardLockButton->setText(tr("S&ideboard unlocked")); - } else - { sideboardLockButton->setText(tr("S&ideboard locked")); - } } void DeckViewContainer::refreshShortcuts() @@ -166,11 +167,9 @@ void DeckViewContainer::refreshShortcuts() void TabGame::refreshShortcuts() { - for (int i = 0; i < phaseActions.size(); i++) - { + for (int i = 0; i < phaseActions.size(); ++i) { QAction *temp = phaseActions.at(i); - switch (i) - { + switch (i) { case 0: temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase0")); break; case 1: temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase1")); break; case 2: temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase2")); break; @@ -182,52 +181,35 @@ void TabGame::refreshShortcuts() case 8: temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase8")); break; case 9: temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase9")); break; case 10:temp->setShortcuts(settingsCache->shortcuts().getShortcut("Player/phase10")); break; - default: break; + default: ; } } - if (aNextPhase) - { + if (aNextPhase) { aNextPhase->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aNextPhase")); } - - if (aNextTurn) - { + if (aNextTurn) { aNextTurn->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aNextTurn")); } - - if (aRemoveLocalArrows) - { + if (aRemoveLocalArrows) { aRemoveLocalArrows->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aRemoveLocalArrows")); } - - if (aRotateViewCW) - { + if (aRotateViewCW) { aRotateViewCW->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aRotateViewCW")); } - - if (aRotateViewCCW) - { + if (aRotateViewCCW) { aRotateViewCCW->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aRotateViewCCW")); } - - if (aConcede) - { + if (aConcede) { aConcede->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aConcede")); } - - if (aLeaveGame) - { + if (aLeaveGame) { aLeaveGame->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aLeaveGame")); } - - if (aCloseReplay) - { + if (aCloseReplay) { aCloseReplay->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aCloseReplay")); } - - if (aResetLayout) - { + if (aResetLayout) { aResetLayout->setShortcuts(settingsCache->shortcuts().getShortcut("Player/aResetLayout")); } } @@ -238,15 +220,12 @@ void DeckViewContainer::loadLocalDeck() dialog.setDirectory(settingsCache->getDeckPath()); dialog.setNameFilters(DeckLoader::fileNameFilters); if (!dialog.exec()) - { return; - } QString fileName = dialog.selectedFiles().at(0); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName); DeckLoader deck; - if (!deck.loadFromFile(fileName, fmt)) - { + if (!deck.loadFromFile(fileName, fmt)) { QMessageBox::critical(this, tr("Error"), tr("The selected file could not be loaded.")); return; } @@ -261,8 +240,7 @@ void DeckViewContainer::loadLocalDeck() void DeckViewContainer::loadRemoteDeck() { DlgLoadRemoteDeck dlg(parentGame->getClientForPlayer(playerId)); - if (dlg.exec()) - { + if (dlg.exec()) { Command_DeckSelect cmd; cmd.set_deck_id(dlg.getDeckId()); PendingCommand *pend = parentGame->prepareGameCommand(cmd); @@ -298,10 +276,8 @@ void DeckViewContainer::sideboardPlanChanged() { Command_SetSideboardPlan cmd; const QList &newPlan = deckView->getSideboardPlan(); - for (const auto &i : newPlan) - { - cmd.add_move_list()->CopyFrom(i); - } + for (int i = 0; i < newPlan.size(); ++i) + cmd.add_move_list()->CopyFrom(newPlan.at(i)); parentGame->sendGameCommand(cmd, playerId); } @@ -317,9 +293,7 @@ void DeckViewContainer::setSideboardLocked(bool locked) sideboardLockButton->setState(!locked); deckView->setLocked(readyStartButton->getState() || !sideboardLockButton->getState()); if (locked) - { deckView->resetSideboardPlan(); - } } void DeckViewContainer::setDeck(const DeckLoader &deck) @@ -340,12 +314,12 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, GameReplay *_replay) gameStateKnown(false), resuming(false), currentPhase(-1), - activeCard(nullptr), + activeCard(0), gameClosed(false), replay(_replay), currentReplayStep(0), - sayLabel(nullptr), - sayEdit(nullptr) + sayLabel(0), + sayEdit(0) { // THIS CTOR IS USED ON REPLAY gameInfo.CopyFrom(replay->game_info()); @@ -355,25 +329,17 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, GameReplay *_replay) // Distribute simultaneous events evenly across 1 second. unsigned int lastEventTimestamp = 0; const int eventCount = replay->event_list_size(); - for (int i = 0; i < eventCount; ++i) - { + for (int i = 0; i < eventCount; ++i) { int j = i + 1; while ((j < eventCount) && (replay->event_list(j).seconds_elapsed() == lastEventTimestamp)) - { ++j; - } const int numberEventsThisSecond = j - i; for (int k = 0; k < numberEventsThisSecond; ++k) - { - replayTimeline.append(replay->event_list(i + k).seconds_elapsed() * 1000 + static_cast((static_cast(k) / static_cast(numberEventsThisSecond) * 1000))); - } + replayTimeline.append(replay->event_list(i + k).seconds_elapsed() * 1000 + (int) ((qreal) k / (qreal) numberEventsThisSecond * 1000)); if (j < eventCount) - { lastEventTimestamp = replay->event_list(j).seconds_elapsed(); - } - i += numberEventsThisSecond - 1; } @@ -417,10 +383,10 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, QList &_client gameStateKnown(false), resuming(event.resuming()), currentPhase(-1), - activeCard(nullptr), + activeCard(0), gameClosed(false), - replay(nullptr), - replayDock(nullptr) + replay(0), + replayDock(0) { // THIS CTOR IS USED ON GAMES gameInfo.set_started(false); @@ -449,22 +415,18 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, QList &_client // append game to rooms game list for others to see for (int i = gameInfo.game_types_size() - 1; i >= 0; i--) - { gameTypes.append(roomGameTypes.find(gameInfo.game_types(i)).value()); - } this->installEventFilter(this); QTimer::singleShot(0, this, SLOT(loadLayout())); } -void TabGame::addMentionTag(QString value) -{ +void TabGame::addMentionTag(QString value) { sayEdit->insert(value + " "); sayEdit->setFocus(); } -void TabGame::emitUserEvent() -{ +void TabGame::emitUserEvent() { bool globalEvent = !spectator || settingsCache->getSpectatorNotificationsEnabled(); emit userEvent(globalEvent); updatePlayerListDockTitle(); @@ -472,13 +434,12 @@ void TabGame::emitUserEvent() TabGame::~TabGame() { - delete replay; + if(replay) + delete replay; QMapIterator i(players); while (i.hasNext()) - { delete i.next().value(); - } players.clear(); emit gameClosing(this); @@ -498,68 +459,43 @@ void TabGame::retranslateUi() updatePlayerListDockTitle(); cardInfoDock->setWindowTitle(tr("Card Info") + (cardInfoDock->isWindow() ? tabText : QString())); messageLayoutDock->setWindowTitle(tr("Messages") + (messageLayoutDock->isWindow() ? tabText : QString())); - if (replayDock) - { + if(replayDock) replayDock->setWindowTitle(tr("Replay Timeline") + (replayDock->isWindow() ? tabText : QString())); - } - if (phasesMenu) - { + if (phasesMenu) { for (int i = 0; i < phaseActions.size(); ++i) - { phaseActions[i]->setText(phasesToolbar->getLongPhaseName(i)); - } phasesMenu->setTitle(tr("&Phases")); } gameMenu->setTitle(tr("&Game")); - if (aNextPhase) - { + if (aNextPhase) { aNextPhase->setText(tr("Next &phase")); } - - if (aNextTurn) - { + if (aNextTurn) { aNextTurn->setText(tr("Next &turn")); } - - if (aRemoveLocalArrows) - { + if (aRemoveLocalArrows) { aRemoveLocalArrows->setText(tr("&Remove all local arrows")); } - - if (aRotateViewCW) - { + if (aRotateViewCW) { aRotateViewCW->setText(tr("Rotate View Cl&ockwise")); } - - if (aRotateViewCCW) - { + if (aRotateViewCCW) { aRotateViewCCW->setText(tr("Rotate View Co&unterclockwise")); } - if (aGameInfo) - { aGameInfo->setText(tr("Game &information")); - } - - if (aConcede) - { + if (aConcede) { aConcede->setText(tr("&Concede")); } - - if (aLeaveGame) - { + if (aLeaveGame) { aLeaveGame->setText(tr("&Leave game")); } - - if (aCloseReplay) - { + if (aCloseReplay) { aCloseReplay->setText(tr("C&lose replay")); } - - if (sayLabel) - { + if (sayLabel){ sayLabel->setText(tr("&Say:")); } @@ -577,7 +513,7 @@ void TabGame::retranslateUi() aPlayerListDockVisible->setText(tr("Visible")); aPlayerListDockFloating->setText(tr("Floating")); - if (replayDock) + if(replayDock) { replayDockMenu->setTitle(tr("Replay Timeline")); aReplayDockVisible->setText(tr("Visible")); @@ -590,15 +526,10 @@ void TabGame::retranslateUi() QMapIterator i(players); while (i.hasNext()) - { i.next().value()->retranslateUi(); - } - QMapIterator j(deckViewContainers); while (j.hasNext()) - { j.next().value()->retranslateUi(); - } scene->retranslateUi(); } @@ -610,7 +541,7 @@ void TabGame::closeRequest() void TabGame::replayNextEvent() { - processGameEventContainer(replay->event_list(timelineWidget->getCurrentEvent()), nullptr); + processGameEventContainer(replay->event_list(timelineWidget->getCurrentEvent()), 0); } void TabGame::replayFinished() @@ -676,29 +607,20 @@ void TabGame::actGameInfo() void TabGame::actConcede() { if (QMessageBox::question(this, tr("Concede"), tr("Are you sure you want to concede this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) - { return; - } sendGameCommand(Command_Concede()); } void TabGame::actLeaveGame() { - if (!gameClosed) - { + if (!gameClosed) { if (!spectator) - { - if (QMessageBox::question(this, tr("Leave game"), tr("Are you sure you want to leave this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) - { - return; - } + if (QMessageBox::question(this, tr("Leave game"), tr("Are you sure you want to leave this game?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) + return; - if (!replay) - { - sendGameCommand(Command_LeaveGame()); - } - } + if (!replay) + sendGameCommand(Command_LeaveGame()); } scene->clearViews(); deleteLater(); @@ -707,12 +629,9 @@ void TabGame::actLeaveGame() void TabGame::actSay() { if (completer->popup()->isVisible()) - { return; - } - if (!sayEdit->text().isEmpty()) - { + if (!sayEdit->text().isEmpty()) { Command_GameSay cmd; cmd.set_message(sayEdit->text().toStdString()); sendGameCommand(cmd); @@ -722,9 +641,9 @@ void TabGame::actSay() void TabGame::actPhaseAction() { - int phase = phaseActions.indexOf(dynamic_cast(sender())); + int phase = phaseActions.indexOf(static_cast(sender())); Command_SetActivePhase cmd; - cmd.set_phase(static_cast(phase)); + cmd.set_phase(phase); sendGameCommand(cmd); } @@ -732,12 +651,9 @@ void TabGame::actNextPhase() { int phase = currentPhase; if (++phase >= phasesToolbar->phaseCount()) - { phase = 0; - } - Command_SetActivePhase cmd; - cmd.set_phase(static_cast(phase)); + cmd.set_phase(phase); sendGameCommand(cmd); } @@ -749,17 +665,12 @@ void TabGame::actNextTurn() void TabGame::actRemoveLocalArrows() { QMapIterator playerIterator(players); - while (playerIterator.hasNext()) - { + while (playerIterator.hasNext()) { Player *player = playerIterator.next().value(); if (!player->getLocal()) - { continue; - } - QMapIterator arrowIterator(player->getArrows()); - while (arrowIterator.hasNext()) - { + while (arrowIterator.hasNext()) { ArrowItem *a = arrowIterator.next().value(); Command_DeleteArrow cmd; cmd.set_arrow_id(a->getId()); @@ -786,12 +697,10 @@ void TabGame::actCompleterChanged() Player *TabGame::addPlayer(int playerId, const ServerInfo_User &info) { bool local = ((clients.size() > 1) || (playerId == localPlayerId)); - auto *newPlayer = new Player(info, playerId, local, this); + Player *newPlayer = new Player(info, playerId, local, this); connect(newPlayer, SIGNAL(openDeckEditor(const DeckLoader *)), this, SIGNAL(openDeckEditor(const DeckLoader *))); QString newPlayerName = "@" + newPlayer->getName(); - - if (sayEdit && !autocompleteUserList.contains(newPlayerName)) - { + if (sayEdit && !autocompleteUserList.contains(newPlayerName)){ autocompleteUserList << newPlayerName; sayEdit->setCompletionList(autocompleteUserList); } @@ -800,14 +709,11 @@ Player *TabGame::addPlayer(int playerId, const ServerInfo_User &info) connect(newPlayer, SIGNAL(newCardAdded(AbstractCardItem *)), this, SLOT(newCardAdded(AbstractCardItem *))); messageLog->connectToPlayer(newPlayer); - if (local && !spectator) - { + if (local && !spectator) { if (clients.size() == 1) - { newPlayer->setShortcutsActive(); - } - auto *deckView = new DeckViewContainer(playerId, this); + DeckViewContainer *deckView = new DeckViewContainer(playerId, this); connect(deckView, SIGNAL(newCardAdded(AbstractCardItem *)), this, SLOT(newCardAdded(AbstractCardItem *))); deckViewContainers.insert(playerId, deckView); deckViewContainerLayout->addWidget(deckView); @@ -825,37 +731,25 @@ void TabGame::processGameEventContainer(const GameEventContainer &cont, Abstract const GameEventContext &context = cont.context(); messageLog->containerProcessingStarted(context); const int eventListSize = cont.event_list_size(); - for (int i = 0; i < eventListSize; ++i) - { + for (int i = 0; i < eventListSize; ++i) { const GameEvent &event = cont.event_list(i); const int playerId = event.player_id(); - const auto eventType = static_cast(getPbExtension(event)); - - if (spectators.contains(playerId)) - { - switch (eventType) - { + const GameEvent::GameEventType eventType = static_cast(getPbExtension(event)); + if (spectators.contains(playerId)) { + switch (eventType) { case GameEvent::GAME_SAY: eventSpectatorSay(event.GetExtension(Event_GameSay::ext), playerId, context); break; case GameEvent::LEAVE: eventSpectatorLeave(event.GetExtension(Event_Leave::ext), playerId, context); break; - default: - { + default: { qDebug() << "unhandled spectator game event" << eventType; break; } } - } - else - { + } else { if ((clients.size() > 1) && (playerId != -1)) - { - if (clients.at(playerId) != client) - { - continue; - } - } + if (clients.at(playerId) != client) + continue; - switch (eventType) - { + switch (eventType) { case GameEvent::GAME_STATE_CHANGED: eventGameStateChanged(event.GetExtension(Event_GameStateChanged::ext), playerId, context); break; case GameEvent::PLAYER_PROPERTIES_CHANGED: eventPlayerPropertiesChanged(event.GetExtension(Event_PlayerPropertiesChanged::ext), playerId, context); break; case GameEvent::JOIN: eventJoin(event.GetExtension(Event_Join::ext), playerId, context); break; @@ -865,38 +759,16 @@ void TabGame::processGameEventContainer(const GameEventContainer &cont, Abstract case GameEvent::GAME_CLOSED: eventGameClosed(event.GetExtension(Event_GameClosed::ext), playerId, context); break; case GameEvent::SET_ACTIVE_PLAYER: eventSetActivePlayer(event.GetExtension(Event_SetActivePlayer::ext), playerId, context); break; case GameEvent::SET_ACTIVE_PHASE: eventSetActivePhase(event.GetExtension(Event_SetActivePhase::ext), playerId, context); break; - default: - { + + default: { Player *player = players.value(playerId, 0); - if (!player) - { + if (!player) { qDebug() << "unhandled game event: invalid player id"; break; } player->processGameEvent(eventType, event, context); emitUserEvent(); - break; } - case GameEvent_GameEventType_GAME_SAY:break; - case GameEvent_GameEventType_CREATE_ARROW:break; - case GameEvent_GameEventType_DELETE_ARROW:break; - case GameEvent_GameEventType_CREATE_COUNTER:break; - case GameEvent_GameEventType_SET_COUNTER:break; - case GameEvent_GameEventType_DEL_COUNTER:break; - case GameEvent_GameEventType_DRAW_CARDS:break; - case GameEvent_GameEventType_REVEAL_CARDS:break; - case GameEvent_GameEventType_SHUFFLE:break; - case GameEvent_GameEventType_ROLL_DIE:break; - case GameEvent_GameEventType_MOVE_CARD:break; - case GameEvent_GameEventType_FLIP_CARD:break; - case GameEvent_GameEventType_DESTROY_CARD:break; - case GameEvent_GameEventType_ATTACH_CARD:break; - case GameEvent_GameEventType_CREATE_TOKEN:break; - case GameEvent_GameEventType_SET_CARD_ATTR:break; - case GameEvent_GameEventType_SET_CARD_COUNTER:break; - case GameEvent_GameEventType_DUMP_ZONE:break; - case GameEvent_GameEventType_STOP_DUMP_ZONE:break; - case GameEvent_GameEventType_CHANGE_ZONE_PROPERTIES:break; } } } @@ -905,32 +777,22 @@ void TabGame::processGameEventContainer(const GameEventContainer &cont, Abstract AbstractClient *TabGame::getClientForPlayer(int playerId) const { - if (clients.size() > 1) - { + if (clients.size() > 1) { if (playerId == -1) - { playerId = getActiveLocalPlayer()->getId(); - } return clients.at(playerId); - } - else if (clients.isEmpty()) - { - return nullptr; - } + } else if (clients.isEmpty()) + return 0; else - { return clients.first(); - } } void TabGame::sendGameCommand(PendingCommand *pend, int playerId) { AbstractClient *client = getClientForPlayer(playerId); - if (client == nullptr) - { + if (!client) return; - } connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(commandFinished(const Response &))); client->sendCommand(pend); @@ -939,10 +801,8 @@ void TabGame::sendGameCommand(PendingCommand *pend, int playerId) void TabGame::sendGameCommand(const google::protobuf::Message &command, int playerId) { AbstractClient *client = getClientForPlayer(playerId); - if (client == nullptr) - { + if (!client) return; - } PendingCommand *pend = prepareGameCommand(command); connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(commandFinished(const Response &))); @@ -952,15 +812,13 @@ void TabGame::sendGameCommand(const google::protobuf::Message &command, int play void TabGame::commandFinished(const Response &response) { if (response.response_code() == Response::RespChatFlood) - { messageLog->appendMessage(tr("You are flooding the game. Please wait a couple of seconds.")); - } } PendingCommand *TabGame::prepareGameCommand(const ::google::protobuf::Message &cmd) { CommandContainer cont; - cont.set_game_id(static_cast(gameInfo.game_id())); + cont.set_game_id(gameInfo.game_id()); GameCommand *c = cont.add_game_command(); c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd); return new PendingCommand(cont); @@ -969,13 +827,11 @@ PendingCommand *TabGame::prepareGameCommand(const ::google::protobuf::Message &c PendingCommand *TabGame::prepareGameCommand(const QList< const ::google::protobuf::Message * > &cmdList) { CommandContainer cont; - cont.set_game_id(static_cast(gameInfo.game_id())); - - for (auto i : cmdList) - { + cont.set_game_id(gameInfo.game_id()); + for (int i = 0; i < cmdList.size(); ++i) { GameCommand *c = cont.add_game_command(); - c->GetReflection()->MutableMessage(c, i->GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(*i); - delete i; + c->GetReflection()->MutableMessage(c, cmdList[i]->GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(*cmdList[i]); + delete cmdList[i]; } return new PendingCommand(cont); } @@ -985,35 +841,25 @@ void TabGame::startGame(bool resuming) currentPhase = -1; QMapIterator i(deckViewContainers); - while (i.hasNext()) - { + while (i.hasNext()) { i.next(); - if (i.value()) - { - i.value()->setReadyStart(false); - i.value()->hide(); - } + i.value()->setReadyStart(false); + i.value()->hide(); } mainWidget->setCurrentWidget(gamePlayAreaWidget); - if (!resuming) - { + if (!resuming) { QMapIterator playerIterator(players); while (playerIterator.hasNext()) - { playerIterator.next().value()->setGameStarted(); - } } playerListWidget->setGameStarted(true, resuming); gameInfo.set_started(true); - dynamic_cast(gameView->scene())->rearrange(); - - if (sayEdit && players.size() > 1) - { + static_cast(gameView->scene())->rearrange(); + if(sayEdit && players.size() > 1) sayEdit->setFocus(); - } } void TabGame::stopGame() @@ -1022,13 +868,9 @@ void TabGame::stopGame() activePlayer = -1; QMapIterator i(deckViewContainers); - while (i.hasNext()) - { + while (i.hasNext()) { i.next(); - if (i.value()) - { - i.value()->show(); - } + i.value()->show(); } mainWidget->setCurrentWidget(deckViewContainerWidget); @@ -1057,10 +899,7 @@ void TabGame::eventSpectatorLeave(const Event_Leave & event, int eventPlayerId, { QString playerName = "@" + QString::fromStdString(spectators.value(eventPlayerId).name()); if (sayEdit && autocompleteUserList.removeOne(playerName)) - { sayEdit->setCompletionList(autocompleteUserList); - } - messageLog->logLeaveSpectator(QString::fromStdString(spectators.value(eventPlayerId).name()), getLeaveReason(event.reason())); playerListWidget->removePlayer(eventPlayerId); spectators.remove(eventPlayerId); @@ -1071,42 +910,30 @@ void TabGame::eventSpectatorLeave(const Event_Leave & event, int eventPlayerId, void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*eventPlayerId*/, const GameEventContext & /*context*/) { const int playerListSize = event.player_list_size(); - for (int i = 0; i < playerListSize; ++i) - { + for (int i = 0; i < playerListSize; ++i) { const ServerInfo_Player &playerInfo = event.player_list(i); const ServerInfo_PlayerProperties &prop = playerInfo.properties(); const int playerId = prop.player_id(); QString playerName = "@" + QString::fromStdString(prop.user_info().name()); - - if (sayEdit && !autocompleteUserList.contains(playerName)) - { + if (sayEdit && !autocompleteUserList.contains(playerName)){ autocompleteUserList << playerName; sayEdit->setCompletionList(autocompleteUserList); } - - if (prop.spectator()) - { - if (!spectators.contains(playerId)) - { + if (prop.spectator()) { + if (!spectators.contains(playerId)) { spectators.insert(playerId, prop.user_info()); playerListWidget->addPlayer(prop); } - } - else - { + } else { Player *player = players.value(playerId, 0); - if (!player) - { + if (!player) { player = addPlayer(playerId, prop.user_info()); playerListWidget->addPlayer(prop); } player->processPlayerInfo(playerInfo); - - if (player->getLocal()) - { + if (player->getLocal()) { DeckViewContainer *deckViewContainer = deckViewContainers.value(playerId); - if (playerInfo.has_deck_list()) - { + if (playerInfo.has_deck_list()) { DeckLoader newDeck(QString::fromStdString(playerInfo.deck_list())); PictureLoader::cacheCardPixmaps(db->getCards(newDeck.getCardList())); deckViewContainer->setDeck(newDeck); @@ -1117,42 +944,29 @@ void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*e } } } - - for (int i = 0; i < playerListSize; ++i) - { + for (int i = 0; i < playerListSize; ++i) { const ServerInfo_Player &playerInfo = event.player_list(i); const ServerInfo_PlayerProperties &prop = playerInfo.properties(); - if (!prop.spectator()) - { + if (!prop.spectator()) { Player *player = players.value(prop.player_id(), 0); - if (player == nullptr) - { + if (!player) continue; - } - player->processCardAttachment(playerInfo); } } secondsElapsed = event.seconds_elapsed(); - if (event.game_started() && !gameInfo.started()) - { + if (event.game_started() && !gameInfo.started()) { startGame(!gameStateKnown); if (gameStateKnown) - { messageLog->logGameStart(); - } - setActivePlayer(event.active_player_id()); setActivePhase(event.active_phase()); - } - else if (!event.game_started() && gameInfo.started()) - { + } else if (!event.game_started() && gameInfo.started()) { stopGame(); scene->clearViews(); } - gameStateKnown = true; emitUserEvent(); } @@ -1160,71 +974,51 @@ void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*e void TabGame::eventPlayerPropertiesChanged(const Event_PlayerPropertiesChanged &event, int eventPlayerId, const GameEventContext &context) { Player *player = players.value(eventPlayerId, 0); - if (player == nullptr) - { + if (!player) return; - } - const ServerInfo_PlayerProperties &prop = event.player_properties(); playerListWidget->updatePlayerProperties(prop, eventPlayerId); - const auto contextType = static_cast(getPbExtension(context)); - switch (contextType) - { - case GameEventContext::READY_START: - { + const GameEventContext::ContextType contextType = static_cast(getPbExtension(context)); + switch (contextType) { + case GameEventContext::READY_START: { bool ready = prop.ready_start(); if (player->getLocal()) - { deckViewContainers.value(player->getId())->setReadyStart(ready); - } - if (ready) - { messageLog->logReadyStart(player); - } else - { messageLog->logNotReadyStart(player); - } - break; } - case GameEventContext::CONCEDE: - { + case GameEventContext::CONCEDE: { messageLog->logConcede(player); player->setConceded(true); QMapIterator playerIterator(players); while (playerIterator.hasNext()) - { playerIterator.next().value()->updateZones(); - } break; } - case GameEventContext::DECK_SELECT: - { + case GameEventContext::DECK_SELECT: { Context_DeckSelect deckSelect = context.GetExtension(Context_DeckSelect::ext); - messageLog->logDeckSelect(player, QString::fromStdString(deckSelect.deck_hash()), deckSelect.sideboard_size()); + messageLog->logDeckSelect(player, + QString::fromStdString(deckSelect.deck_hash()), + deckSelect.sideboard_size()); break; } - case GameEventContext::SET_SIDEBOARD_LOCK: - { + case GameEventContext::SET_SIDEBOARD_LOCK: { if (player->getLocal()) - { deckViewContainers.value(player->getId())->setSideboardLocked(prop.sideboard_locked()); - } - messageLog->logSetSideboardLock(player, prop.sideboard_locked()); break; } - case GameEventContext::CONNECTION_STATE_CHANGED: - { + case GameEventContext::CONNECTION_STATE_CHANGED: { messageLog->logConnectionStateChanged(player, prop.ping_seconds() != -1); break; } - default: break; + default: ; } } @@ -1233,28 +1027,21 @@ void TabGame::eventJoin(const Event_Join &event, int /*eventPlayerId*/, const Ga const ServerInfo_PlayerProperties &playerInfo = event.player_properties(); const int playerId = playerInfo.player_id(); QString playerName = QString::fromStdString(playerInfo.user_info().name()); - if (sayEdit && !autocompleteUserList.contains("@" + playerName)) - { + if (sayEdit && !autocompleteUserList.contains("@" + playerName)){ autocompleteUserList << "@" + playerName; sayEdit->setCompletionList(autocompleteUserList); } if (players.contains(playerId)) - { return; - } - if (playerInfo.spectator()) - { + if (playerInfo.spectator()) { spectators.insert(playerId, playerInfo.user_info()); messageLog->logJoinSpectator(playerName); - } - else - { + } else { Player *newPlayer = addPlayer(playerId, playerInfo.user_info()); messageLog->logJoin(newPlayer); } - playerListWidget->addPlayer(playerInfo); emitUserEvent(); } @@ -1281,16 +1068,12 @@ QString TabGame::getLeaveReason(Event_Leave::LeaveReason reason) void TabGame::eventLeave(const Event_Leave & event, int eventPlayerId, const GameEventContext & /*context*/) { Player *player = players.value(eventPlayerId, 0); - if (player == nullptr) - { + if (!player) return; - } QString playerName = "@" + player->getName(); - if (sayEdit && autocompleteUserList.removeOne(playerName)) - { + if(sayEdit && autocompleteUserList.removeOne(playerName)) sayEdit->setCompletionList(autocompleteUserList); - } messageLog->logLeave(player, getLeaveReason(event.reason())); playerListWidget->removePlayer(eventPlayerId); @@ -1302,9 +1085,7 @@ void TabGame::eventLeave(const Event_Leave & event, int eventPlayerId, const Gam // Rearrange all remaining zones so that attachment relationship updates take place QMapIterator playerIterator(players); while (playerIterator.hasNext()) - { playerIterator.next().value()->updateZones(); - } emitUserEvent(); } @@ -1339,34 +1120,23 @@ void TabGame::eventGameClosed(const Event_GameClosed & /*event*/, int /*eventPla Player *TabGame::setActivePlayer(int id) { Player *player = players.value(id, 0); - if (player == nullptr) - { - return nullptr; - } + if (!player) + return 0; activePlayer = id; playerListWidget->setActivePlayer(id); QMapIterator i(players); - while (i.hasNext()) - { + while (i.hasNext()) { i.next(); - if (i.value() == player) - { + if (i.value() == player) { i.value()->setActive(true); if (clients.size() > 1) - { i.value()->setShortcutsActive(); - } - } - else - { + } else { i.value()->setActive(false); if (clients.size() > 1) - { i.value()->setShortcutsInactive(); - } } } - currentPhase = -1; emitUserEvent(); return player; @@ -1375,19 +1145,15 @@ Player *TabGame::setActivePlayer(int id) void TabGame::eventSetActivePlayer(const Event_SetActivePlayer &event, int /*eventPlayerId*/, const GameEventContext & /*context*/) { Player *player = setActivePlayer(event.active_player_id()); - if (player == nullptr) - { + if (!player) return; - } - messageLog->logSetActivePlayer(player); emitUserEvent(); } void TabGame::setActivePhase(int phase) { - if (currentPhase != phase) - { + if (currentPhase != phase) { currentPhase = phase; phasesToolbar->setActivePhase(phase); } @@ -1397,10 +1163,7 @@ void TabGame::eventSetActivePhase(const Event_SetActivePhase &event, int /*event { const int phase = event.phase(); if (currentPhase != phase) - { messageLog->logSetActivePhase(phase); - } - setActivePhase(phase); emitUserEvent(); } @@ -1416,16 +1179,12 @@ void TabGame::newCardAdded(AbstractCardItem *card) CardItem *TabGame::getCard(int playerId, const QString &zoneName, int cardId) const { Player *player = players.value(playerId, 0); - if (player == nullptr) - { - return nullptr; - } + if (!player) + return 0; CardZone *zone = player->getZones().value(zoneName, 0); - if (zone == nullptr) - { - return nullptr; - } + if (!zone) + return 0; return zone->getCard(cardId, QString()); } @@ -1433,13 +1192,10 @@ CardItem *TabGame::getCard(int playerId, const QString &zoneName, int cardId) co QString TabGame::getTabText() const { QString gameTypeInfo; - if (!gameTypes.empty()) - { + if (gameTypes.size() != 0) { gameTypeInfo = gameTypes.at(0); if (gameTypes.size() > 1) - { gameTypeInfo.append("..."); - } } QString gameDesc(gameInfo.description().c_str()); @@ -1447,32 +1203,17 @@ QString TabGame::getTabText() const QString tabText; if (replay) - { tabText.append(tr("Replay") + " "); - } - if (!gameTypeInfo.isEmpty()) - { tabText.append(gameTypeInfo + " "); - } - - if (!gameDesc.isEmpty()) - { + if (!gameDesc.isEmpty()) { if (gameDesc.length() >= 15) - { tabText.append("| " + gameDesc.left(15) + "... "); - } else - { tabText.append("| " + gameDesc + " "); - } } - if (!tabText.isEmpty()) - { tabText.append("| "); - } - tabText.append("#" + gameId); return tabText; @@ -1481,37 +1222,28 @@ QString TabGame::getTabText() const Player *TabGame::getActiveLocalPlayer() const { Player *active = players.value(activePlayer, 0); - if (active && active->getLocal()) - { + if (active) + if (active->getLocal()) return active; - } QMapIterator playerIterator(players); - while (playerIterator.hasNext()) - { + while (playerIterator.hasNext()) { Player *temp = playerIterator.next().value(); if (temp->getLocal()) - { return temp; - } } - return nullptr; + return 0; } void TabGame::updateCardMenu(AbstractCardItem *card) { Player *p; if ((clients.size() > 1) || !players.contains(localPlayerId)) - { p = card->getOwner(); - } else - { p = players.value(localPlayerId); - } - - p->updateCardMenu(dynamic_cast(card)); + p->updateCardMenu(static_cast(card)); } void TabGame::createMenuItems() @@ -1532,11 +1264,10 @@ void TabGame::createMenuItems() connect(aConcede, SIGNAL(triggered()), this, SLOT(actConcede())); aLeaveGame = new QAction(this); connect(aLeaveGame, SIGNAL(triggered()), this, SLOT(actLeaveGame())); - aCloseReplay = nullptr; + aCloseReplay = 0; phasesMenu = new QMenu(this); - for (int i = 0; i < phasesToolbar->phaseCount(); ++i) - { + for (int i = 0; i < phasesToolbar->phaseCount(); ++i) { QAction *temp = new QAction(QString(), this); connect(temp, SIGNAL(triggered()), this, SLOT(actPhaseAction())); phasesMenu->addAction(temp); @@ -1563,19 +1294,19 @@ void TabGame::createMenuItems() void TabGame::createReplayMenuItems() { - aNextPhase = nullptr; - aNextTurn = nullptr; - aRemoveLocalArrows = nullptr; - aRotateViewCW = nullptr; - aRotateViewCCW = nullptr; - aResetLayout = nullptr; - aGameInfo = nullptr; - aConcede = nullptr; - aLeaveGame = nullptr; + aNextPhase = 0; + aNextTurn = 0; + aRemoveLocalArrows = 0; + aRotateViewCW = 0; + aRotateViewCCW = 0; + aResetLayout = 0; + aGameInfo = 0; + aConcede = 0; + aLeaveGame = 0; aCloseReplay = new QAction(this); connect(aCloseReplay, SIGNAL(triggered()), this, SLOT(actLeaveGame())); - phasesMenu = nullptr; + phasesMenu = 0; gameMenu = new QMenu(this); gameMenu->addAction(aCloseReplay); addTabMenu(gameMenu); @@ -1610,7 +1341,7 @@ void TabGame::createViewMenuItems() aPlayerListDockFloating->setCheckable(true); connect(aPlayerListDockFloating,SIGNAL(triggered()),this,SLOT(dockFloatingTriggered())); - if (replayDock) + if(replayDock) { replayDockMenu = viewMenu->addMenu(QString()); @@ -1633,7 +1364,7 @@ void TabGame::createViewMenuItems() void TabGame::loadLayout() { - if (replayDock) + if(replayDock) { restoreGeometry(settingsCache->layouts().getReplayPlayAreaGeometry()); restoreState(settingsCache->layouts().getReplayPlayAreaLayoutState()); @@ -1646,9 +1377,7 @@ void TabGame::loadLayout() playerListDock->setMaximumSize(settingsCache->layouts().getReplayPlayerListSize()); replayDock->setMinimumSize(settingsCache->layouts().getReplayReplaySize()); replayDock->setMaximumSize(settingsCache->layouts().getReplayReplaySize()); - } - else - { + } else { restoreGeometry(settingsCache->layouts().getGamePlayAreaGeometry()); restoreState(settingsCache->layouts().getGamePlayAreaLayoutState()); @@ -1672,7 +1401,7 @@ void TabGame::loadLayout() aMessageLayoutDockFloating->setChecked(messageLayoutDock->isFloating()); aPlayerListDockFloating->setChecked(playerListDock->isFloating()); - if (replayDock) + if(replayDock) { aReplayDockVisible->setChecked(replayDock->isVisible()); aReplayDockFloating->setEnabled(aReplayDockVisible->isChecked()); @@ -1693,7 +1422,7 @@ void TabGame::freeDocksSize() playerListDock->setMinimumSize(100,100); playerListDock->setMaximumSize(5000,5000); - if (replayDock) + if(replayDock) { replayDock->setMinimumSize(100,100); replayDock->setMaximumSize(5000,5000); @@ -1722,7 +1451,7 @@ void TabGame::actResetLayout() addDockWidget(Qt::RightDockWidgetArea, playerListDock); addDockWidget(Qt::RightDockWidgetArea, messageLayoutDock); - if (replayDock) + if(replayDock) { replayDock->setVisible(true); replayDock->setFloating(false); @@ -1738,9 +1467,7 @@ void TabGame::actResetLayout() playerListDock->setMaximumSize(250,50); replayDock->setMinimumSize(900,100); replayDock->setMaximumSize(900,100); - } - else - { + } else { cardInfoDock->setMinimumSize(250,360); cardInfoDock->setMaximumSize(250,360); messageLayoutDock->setMinimumSize(250,250); @@ -1755,11 +1482,8 @@ void TabGame::actResetLayout() void TabGame::createPlayAreaWidget(bool bReplay) { phasesToolbar = new PhasesToolbar; - if (!bReplay) - { + if(!bReplay) connect(phasesToolbar, SIGNAL(sendGameCommand(const ::google::protobuf::Message &, int)), this, SLOT(sendGameCommand(const ::google::protobuf::Message &, int))); - } - scene = new GameScene(phasesToolbar, this); gameView = new GameView(scene); @@ -1852,16 +1576,13 @@ void TabGame::createCardInfoDock(bool bReplay) void TabGame::createPlayerListDock(bool bReplay) { - if (bReplay) - { - playerListWidget = new PlayerListWidget(nullptr, nullptr, this); - } - else + if(bReplay) { + playerListWidget = new PlayerListWidget(0, 0, this); + } else { playerListWidget = new PlayerListWidget(tabSupervisor, clients.first(), this); connect(playerListWidget, SIGNAL(openMessageDialog(QString, bool)), this, SIGNAL(openMessageDialog(QString, bool))); } - playerListWidget->setFocusPolicy(Qt::NoFocus); playerListDock = new QDockWidget(this); @@ -1881,7 +1602,7 @@ void TabGame::createMessageDock(bool bReplay) connect(messageLog, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString))); connect(messageLog, SIGNAL(deleteCardInfoPopup(QString)), this, SLOT(deleteCardInfoPopup(QString))); - if (!bReplay) + if(!bReplay) { connect(messageLog, SIGNAL(openMessageDialog(QString, bool)), this, SIGNAL(openMessageDialog(QString, bool))); connect(messageLog, SIGNAL(addMentionTag(QString)), this, SLOT(addMentionTag(QString))); @@ -1905,12 +1626,10 @@ void TabGame::createMessageDock(bool bReplay) sayEdit->setCompleter(completer); actCompleterChanged(); - if (spectator && !gameInfo.spectators_can_chat() && tabSupervisor->getAdminLocked()) - { + if (spectator && !gameInfo.spectators_can_chat() && tabSupervisor->getAdminLocked()) { sayLabel->hide(); sayEdit->hide(); } - connect(tabSupervisor, SIGNAL(adminLockChanged(bool)), this, SLOT(adminLockChanged(bool))); connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay())); @@ -1922,17 +1641,11 @@ void TabGame::createMessageDock(bool bReplay) messageLogLayout = new QVBoxLayout; messageLogLayout->setContentsMargins(0, 0, 0, 0); - if (!bReplay) - { + if(!bReplay) messageLogLayout->addWidget(timeElapsedLabel); - } - messageLogLayout->addWidget(messageLog); - - if (!bReplay) - { + if(!bReplay) messageLogLayout->addLayout(sayHLayout); - } messageLogLayoutWidget = new QWidget; messageLogLayoutWidget->setLayout(messageLogLayout); @@ -1950,33 +1663,26 @@ void TabGame::createMessageDock(bool bReplay) // Method uses to sync docks state with menu items state bool TabGame::eventFilter(QObject * o, QEvent * e) { - if (e->type() == QEvent::Close) + if(e->type() == QEvent::Close) { - if (o == cardInfoDock) + if(o == cardInfoDock) { aCardInfoDockVisible->setChecked(false); aCardInfoDockFloating->setEnabled(false); - } - else if (o == messageLayoutDock) - { + } else if(o == messageLayoutDock) { aMessageLayoutDockVisible->setChecked(false); aMessageLayoutDockFloating->setEnabled(false); - } - else if(o == playerListDock) - { + } else if(o == playerListDock) { aPlayerListDockVisible->setChecked(false); aPlayerListDockFloating->setEnabled(false); - } - else if(o == replayDock) - { + } else if(o == replayDock) { aReplayDockVisible->setChecked(false); aReplayDockFloating->setEnabled(false); } } - if (o == this && e->type() == QEvent::Hide) - { - if (replay) + if( o == this && e->type() == QEvent::Hide){ + if(replay) { settingsCache->layouts().setReplayPlayAreaState(saveState()); settingsCache->layouts().setReplayPlayAreaGeometry(saveGeometry()); @@ -1984,9 +1690,7 @@ bool TabGame::eventFilter(QObject * o, QEvent * e) settingsCache->layouts().setReplayMessageLayoutSize(messageLayoutDock->size()); settingsCache->layouts().setReplayPlayerListSize(playerListDock->size()); settingsCache->layouts().setReplayReplaySize(replayDock->size()); - } - else - { + } else { settingsCache->layouts().setGamePlayAreaState(saveState()); settingsCache->layouts().setGamePlayAreaGeometry(saveGeometry()); settingsCache->layouts().setGameCardInfoSize(cardInfoDock->size()); @@ -2000,28 +1704,28 @@ bool TabGame::eventFilter(QObject * o, QEvent * e) void TabGame::dockVisibleTriggered() { QObject *o = sender(); - if (o == aCardInfoDockVisible) + if(o == aCardInfoDockVisible) { cardInfoDock->setVisible(aCardInfoDockVisible->isChecked()); aCardInfoDockFloating->setEnabled(aCardInfoDockVisible->isChecked()); return; } - if (o == aMessageLayoutDockVisible) + if(o == aMessageLayoutDockVisible) { messageLayoutDock->setVisible(aMessageLayoutDockVisible->isChecked()); aMessageLayoutDockFloating->setEnabled(aMessageLayoutDockVisible->isChecked()); return; } - if (o == aPlayerListDockVisible) + if(o == aPlayerListDockVisible) { playerListDock->setVisible(aPlayerListDockVisible->isChecked()); aPlayerListDockFloating->setEnabled(aPlayerListDockVisible->isChecked()); return; } - if (o == aReplayDockVisible) + if(o == aReplayDockVisible) { replayDock->setVisible(aReplayDockVisible->isChecked()); aReplayDockFloating->setEnabled(aReplayDockVisible->isChecked()); @@ -2032,25 +1736,25 @@ void TabGame::dockVisibleTriggered() void TabGame::dockFloatingTriggered() { QObject *o = sender(); - if (o == aCardInfoDockFloating) + if(o == aCardInfoDockFloating) { cardInfoDock->setFloating(aCardInfoDockFloating->isChecked()); return; } - if (o == aMessageLayoutDockFloating) + if(o == aMessageLayoutDockFloating) { messageLayoutDock->setFloating(aMessageLayoutDockFloating->isChecked()); return; } - if (o == aPlayerListDockFloating) + if(o == aPlayerListDockFloating) { playerListDock->setFloating(aPlayerListDockFloating->isChecked()); return; } - if (o == aReplayDockFloating) + if(o == aReplayDockFloating) { replayDock->setFloating(aReplayDockFloating->isChecked()); return; @@ -2062,27 +1766,27 @@ void TabGame::dockTopLevelChanged(bool topLevel) retranslateUi(); QObject *o = sender(); - if (o == cardInfoDock) + if(o == cardInfoDock) { aCardInfoDockFloating->setChecked(topLevel); return; } - if (o == messageLayoutDock) + if(o == messageLayoutDock) { aMessageLayoutDockFloating->setChecked(topLevel); return; } - if (o == playerListDock) + if(o == playerListDock) { aPlayerListDockFloating->setChecked(topLevel); return; } - if (o == replayDock) + if(o == replayDock) { aReplayDockFloating->setChecked(topLevel); return; } -} \ No newline at end of file +} diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index 6811ed71..ac4bcbc4 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -60,216 +60,208 @@ class LineEditCompleter; class QDockWidget; class QStackedWidget; -class ToggleButton : public QPushButton -{ +class ToggleButton : public QPushButton { Q_OBJECT - private: - bool state; - - signals: - void stateChanged(); - - public: - explicit ToggleButton(QWidget *parent = nullptr); - bool getState() const { return state; } - void setState(bool _state); - - protected: - void paintEvent(QPaintEvent *event) override; +private: + bool state; +signals: + void stateChanged(); +public: + ToggleButton(QWidget *parent = 0); + bool getState() const { return state; } + void setState(bool _state); +protected: + void paintEvent(QPaintEvent *event); }; -class DeckViewContainer : public QWidget -{ +class DeckViewContainer : public QWidget { Q_OBJECT - private: - QPushButton *loadLocalButton, *loadRemoteButton; - ToggleButton *readyStartButton, *sideboardLockButton; - DeckView *deckView; - TabGame *parentGame; - int playerId; - - private slots: - void loadLocalDeck(); - void loadRemoteDeck(); - void readyStart(); - void deckSelectFinished(const Response &r); - void sideboardPlanChanged(); - void sideboardLockButtonClicked(); - void updateSideboardLockButtonText(); - void refreshShortcuts(); - - signals: - void newCardAdded(AbstractCardItem *card); - - public: - DeckViewContainer(int _playerId, TabGame *parent); - void retranslateUi(); - void setReadyStart(bool ready); - void setSideboardLocked(bool locked); - void setDeck(const DeckLoader &deck); +private: + QPushButton *loadLocalButton, *loadRemoteButton; + ToggleButton *readyStartButton, *sideboardLockButton; + DeckView *deckView; + TabGame *parentGame; + int playerId; +private slots: + void loadLocalDeck(); + void loadRemoteDeck(); + void readyStart(); + void deckSelectFinished(const Response &r); + void sideboardPlanChanged(); + void sideboardLockButtonClicked(); + void updateSideboardLockButtonText(); + void refreshShortcuts(); +signals: + void newCardAdded(AbstractCardItem *card); + void notIdle(); +public: + DeckViewContainer(int _playerId, TabGame *parent); + void retranslateUi(); + void setButtonsVisible(bool _visible); + void setReadyStart(bool ready); + void setSideboardLocked(bool locked); + void setDeck(const DeckLoader &deck); }; -class TabGame : public Tab -{ +class TabGame : public Tab { Q_OBJECT - private: - QTimer *gameTimer; - int secondsElapsed; - QList clients; - ServerInfo_Game gameInfo; - QMap roomGameTypes; - int hostId; - int localPlayerId; - const bool isLocalGame; - bool spectator; - QMap players; - QMap spectators; - bool gameStateKnown; - bool resuming; - int currentPhase; - int activePlayer; - CardItem *activeCard; - bool gameClosed; - QStringList gameTypes; - QCompleter *completer; - QStringList autocompleteUserList; - QStackedWidget * mainWidget; +private: + QTimer *gameTimer; + int secondsElapsed; + QList clients; + ServerInfo_Game gameInfo; + QMap roomGameTypes; + int hostId; + int localPlayerId; + const bool isLocalGame; + bool spectator; + QMap players; + QMap spectators; + bool gameStateKnown; + bool resuming; + QStringList phasesList; + int currentPhase; + int activePlayer; + CardItem *activeCard; + bool gameClosed; + QStringList gameTypes; + QCompleter *completer; + QStringList autocompleteUserList; + QStackedWidget * mainWidget; + + // Replay related members + GameReplay *replay; + int currentReplayStep; + QList replayTimeline; + ReplayTimelineWidget *timelineWidget; + QToolButton *replayStartButton, *replayPauseButton, *replayFastForwardButton; - // Replay related members - GameReplay *replay; - int currentReplayStep; - QList replayTimeline; - ReplayTimelineWidget *timelineWidget; - QToolButton *replayStartButton, *replayPauseButton, *replayFastForwardButton; + CardFrame *cardInfo; + PlayerListWidget *playerListWidget; + QLabel *timeElapsedLabel; + MessageLogWidget *messageLog; + QLabel *sayLabel; + LineEditCompleter *sayEdit; + PhasesToolbar *phasesToolbar; + GameScene *scene; + GameView *gameView; + QMap deckViewContainers; + QVBoxLayout *cardVInfoLayout, *messageLogLayout, *gamePlayAreaVBox, *deckViewContainerLayout; + QHBoxLayout *cardHInfoLayout, *sayHLayout, *mainHLayout, *replayControlLayout; + QWidget *cardBoxLayoutWidget, *messageLogLayoutWidget, *gamePlayAreaWidget, *deckViewContainerWidget, *replayControlWidget; + QDockWidget *cardInfoDock, *messageLayoutDock, *playerListDock, *replayDock; + QAction *playersSeparator; + QMenu *gameMenu, *phasesMenu, *viewMenu, *cardInfoDockMenu, *messageLayoutDockMenu, *playerListDockMenu, *replayDockMenu; + QAction *aGameInfo, *aConcede, *aLeaveGame, *aCloseReplay, *aNextPhase, *aNextTurn, *aRemoveLocalArrows, *aRotateViewCW, *aRotateViewCCW, *aResetLayout, *aResetReplayLayout; + QAction *aCardInfoDockVisible, *aCardInfoDockFloating, *aMessageLayoutDockVisible, *aMessageLayoutDockFloating, *aPlayerListDockVisible, *aPlayerListDockFloating, *aReplayDockVisible, *aReplayDockFloating; + QList phaseActions; - CardFrame *cardInfo; - PlayerListWidget *playerListWidget; - QLabel *timeElapsedLabel; - MessageLogWidget *messageLog; - QLabel *sayLabel; - LineEditCompleter *sayEdit; - PhasesToolbar *phasesToolbar; - GameScene *scene; - GameView *gameView; - QMap deckViewContainers; - QVBoxLayout *cardVInfoLayout, *messageLogLayout, *gamePlayAreaVBox, *deckViewContainerLayout; - QHBoxLayout *cardHInfoLayout, *sayHLayout, *replayControlLayout; - QWidget *cardBoxLayoutWidget, *messageLogLayoutWidget, *gamePlayAreaWidget, *deckViewContainerWidget, *replayControlWidget; - QDockWidget *cardInfoDock, *messageLayoutDock, *playerListDock, *replayDock; - QAction *playersSeparator; - QMenu *gameMenu, *phasesMenu, *viewMenu, *cardInfoDockMenu, *messageLayoutDockMenu, *playerListDockMenu, *replayDockMenu; - QAction *aGameInfo, *aConcede, *aLeaveGame, *aCloseReplay, *aNextPhase, *aNextTurn, *aRemoveLocalArrows, *aRotateViewCW, *aRotateViewCCW, *aResetLayout; - QAction *aCardInfoDockVisible, *aCardInfoDockFloating, *aMessageLayoutDockVisible, *aMessageLayoutDockFloating, *aPlayerListDockVisible, *aPlayerListDockFloating, *aReplayDockVisible, *aReplayDockFloating; - QList phaseActions; + Player *addPlayer(int playerId, const ServerInfo_User &info); - Player *addPlayer(int playerId, const ServerInfo_User &info); + void startGame(bool resuming); + void stopGame(); + void closeGame(); - void startGame(bool resuming); - void stopGame(); - void closeGame(); + void eventSpectatorSay(const Event_GameSay &event, int eventPlayerId, const GameEventContext &context); + void eventSpectatorLeave(const Event_Leave &event, int eventPlayerId, const GameEventContext &context); + + void eventGameStateChanged(const Event_GameStateChanged &event, int eventPlayerId, const GameEventContext &context); + void eventPlayerPropertiesChanged(const Event_PlayerPropertiesChanged &event, int eventPlayerId, const GameEventContext &context); + void eventJoin(const Event_Join &event, int eventPlayerId, const GameEventContext &context); + void eventLeave(const Event_Leave &event, int eventPlayerId, const GameEventContext &context); + void eventKicked(const Event_Kicked &event, int eventPlayerId, const GameEventContext &context); + void eventGameHostChanged(const Event_GameHostChanged &event, int eventPlayerId, const GameEventContext &context); + void eventGameClosed(const Event_GameClosed &event, int eventPlayerId, const GameEventContext &context); + Player *setActivePlayer(int id); + void eventSetActivePlayer(const Event_SetActivePlayer &event, int eventPlayerId, const GameEventContext &context); + void setActivePhase(int phase); + void eventSetActivePhase(const Event_SetActivePhase &event, int eventPlayerId, const GameEventContext &context); + void eventPing(const Event_Ping &event, int eventPlayerId, const GameEventContext &context); + void emitUserEvent(); + void createMenuItems(); + void createReplayMenuItems(); + void createViewMenuItems(); + void createCardInfoDock(bool bReplay=false); + void createPlayerListDock(bool bReplay=false); + void createMessageDock(bool bReplay=false); + void createPlayAreaWidget(bool bReplay=false); + void createDeckViewContainerWidget(bool bReplay=false); + void createReplayDock(); + QString getLeaveReason(Event_Leave::LeaveReason reason); +signals: + void gameClosing(TabGame *tab); + void playerAdded(Player *player); + void playerRemoved(Player *player); + void containerProcessingStarted(const GameEventContext &context); + void containerProcessingDone(); + void openMessageDialog(const QString &userName, bool focus); + void openDeckEditor(const DeckLoader *deck); + void notIdle(); +private slots: + void replayNextEvent(); + void replayFinished(); + void replayStartButtonClicked(); + void replayPauseButtonClicked(); + void replayFastForwardButtonToggled(bool checked); + + void incrementGameTime(); + void adminLockChanged(bool lock); + void newCardAdded(AbstractCardItem *card); + void updateCardMenu(AbstractCardItem *card); + + void actGameInfo(); + void actConcede(); + void actLeaveGame(); + void actRemoveLocalArrows(); + void actRotateViewCW(); + void actRotateViewCCW(); + void actSay(); + void actPhaseAction(); + void actNextPhase(); + void actNextTurn(); - void eventSpectatorSay(const Event_GameSay &event, int eventPlayerId, const GameEventContext &context); - void eventSpectatorLeave(const Event_Leave &event, int eventPlayerId, const GameEventContext &context); + void addMentionTag(QString value); + void commandFinished(const Response &response); + + void refreshShortcuts(); + + void loadLayout(); + void actCompleterChanged(); + void actResetLayout(); + void freeDocksSize(); - void eventGameStateChanged(const Event_GameStateChanged &event, int eventPlayerId, const GameEventContext &context); - void eventPlayerPropertiesChanged(const Event_PlayerPropertiesChanged &event, int eventPlayerId, const GameEventContext &context); - void eventJoin(const Event_Join &event, int eventPlayerId, const GameEventContext &context); - void eventLeave(const Event_Leave &event, int eventPlayerId, const GameEventContext &context); - void eventKicked(const Event_Kicked &event, int eventPlayerId, const GameEventContext &context); - void eventGameHostChanged(const Event_GameHostChanged &event, int eventPlayerId, const GameEventContext &context); - void eventGameClosed(const Event_GameClosed &event, int eventPlayerId, const GameEventContext &context); - Player *setActivePlayer(int id); - void eventSetActivePlayer(const Event_SetActivePlayer &event, int eventPlayerId, const GameEventContext &context); - void setActivePhase(int phase); - void eventSetActivePhase(const Event_SetActivePhase &event, int eventPlayerId, const GameEventContext &context); - void emitUserEvent(); - void createMenuItems(); - void createReplayMenuItems(); - void createViewMenuItems(); - void createCardInfoDock(bool bReplay=false); - void createPlayerListDock(bool bReplay=false); - void createMessageDock(bool bReplay=false); - void createPlayAreaWidget(bool bReplay=false); - void createDeckViewContainerWidget(bool bReplay=false); - void createReplayDock(); - QString getLeaveReason(Event_Leave::LeaveReason reason); + bool eventFilter(QObject *o, QEvent *e); + void dockVisibleTriggered(); + void dockFloatingTriggered(); + void dockTopLevelChanged(bool topLevel); +public: + TabGame(TabSupervisor *_tabSupervisor, QList &_clients, const Event_GameJoined &event, const QMap &_roomGameTypes); + TabGame(TabSupervisor *_tabSupervisor, GameReplay *replay); + ~TabGame(); + void retranslateUi(); + void updatePlayerListDockTitle(); + void closeRequest(); + const QMap &getPlayers() const { return players; } + CardItem *getCard(int playerId, const QString &zoneName, int cardId) const; + bool isHost() const { return hostId == localPlayerId; } + bool getIsLocalGame() const { return isLocalGame; } + int getGameId() const { return gameInfo.game_id(); } + QString getTabText() const; + bool getSpectator() const { return spectator; } + bool getSpectatorsSeeEverything() const { return gameInfo.spectators_omniscient(); } + bool isSpectator(); + Player *getActiveLocalPlayer() const; + AbstractClient *getClientForPlayer(int playerId) const; - signals: - void gameClosing(TabGame *tab); - void playerAdded(Player *player); - void playerRemoved(Player *player); - void containerProcessingStarted(const GameEventContext &context); - void containerProcessingDone(); - void openMessageDialog(const QString &userName, bool focus); - void openDeckEditor(const DeckLoader *deck); + void setActiveCard(CardItem *_card) { activeCard = _card; } + CardItem *getActiveCard() const { return activeCard; } - private slots: - void replayNextEvent(); - void replayFinished(); - void replayStartButtonClicked(); - void replayPauseButtonClicked(); - void replayFastForwardButtonToggled(bool checked); - - void incrementGameTime(); - void adminLockChanged(bool lock); - void newCardAdded(AbstractCardItem *card); - void updateCardMenu(AbstractCardItem *card); - - void actGameInfo(); - void actConcede(); - void actLeaveGame(); - void actRemoveLocalArrows(); - void actRotateViewCW(); - void actRotateViewCCW(); - void actSay(); - void actPhaseAction(); - void actNextPhase(); - void actNextTurn(); - - void addMentionTag(QString value); - void commandFinished(const Response &response); - - void refreshShortcuts(); - - void loadLayout(); - void actCompleterChanged(); - void actResetLayout(); - void freeDocksSize(); - - bool eventFilter(QObject *o, QEvent *e) override; - void dockVisibleTriggered(); - void dockFloatingTriggered(); - void dockTopLevelChanged(bool topLevel); - - public: - TabGame(TabSupervisor *_tabSupervisor, QList &_clients, const Event_GameJoined &event, const QMap &_roomGameTypes); - TabGame(TabSupervisor *_tabSupervisor, GameReplay *replay); - ~TabGame() override; - void retranslateUi() override; - void updatePlayerListDockTitle(); - void closeRequest() override; - const QMap &getPlayers() const { return players; } - CardItem *getCard(int playerId, const QString &zoneName, int cardId) const; - bool isHost() const { return hostId == localPlayerId; } - bool getIsLocalGame() const { return isLocalGame; } - int getGameId() const { return gameInfo.game_id(); } - QString getTabText() const override; - bool getSpectator() const { return spectator; } - bool getSpectatorsSeeEverything() const { return gameInfo.spectators_omniscient(); } - bool isSpectator(); - Player *getActiveLocalPlayer() const; - AbstractClient *getClientForPlayer(int playerId) const; - - void setActiveCard(CardItem *_card) { activeCard = _card; } - CardItem *getActiveCard() const { return activeCard; } - - void processGameEventContainer(const GameEventContainer &cont, AbstractClient *client); - PendingCommand *prepareGameCommand(const ::google::protobuf::Message &cmd); - PendingCommand *prepareGameCommand(const QList< const ::google::protobuf::Message * > &cmdList); - - public slots: - void sendGameCommand(PendingCommand *pend, int playerId = -1); - void sendGameCommand(const ::google::protobuf::Message &command, int playerId = -1); + void processGameEventContainer(const GameEventContainer &cont, AbstractClient *client); + PendingCommand *prepareGameCommand(const ::google::protobuf::Message &cmd); + PendingCommand *prepareGameCommand(const QList< const ::google::protobuf::Message * > &cmdList); +public slots: + void sendGameCommand(PendingCommand *pend, int playerId = -1); + void sendGameCommand(const ::google::protobuf::Message &command, int playerId = -1); }; #endif diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 911c95e5..4a64bd89 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -28,7 +28,8 @@ #include "pb/serverinfo_room.pb.h" #include "pb/moderator_commands.pb.h" -CloseButton::CloseButton(QWidget *parent) : QAbstractButton(parent) +CloseButton::CloseButton(QWidget *parent) + : QAbstractButton(parent) { setFocusPolicy(Qt::NoFocus); setCursor(Qt::ArrowCursor); @@ -38,9 +39,9 @@ CloseButton::CloseButton(QWidget *parent) : QAbstractButton(parent) QSize CloseButton::sizeHint() const { ensurePolished(); - int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, nullptr, this); - int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, nullptr, this); - return {width, height}; + int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this); + int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this); + return QSize(width, height); } void CloseButton::enterEvent(QEvent *event) @@ -62,34 +63,24 @@ void CloseButton::paintEvent(QPaintEvent * /*event*/) opt.init(this); opt.state |= QStyle::State_AutoRaise; if (isEnabled() && underMouse() && !isChecked() && !isDown()) - { opt.state |= QStyle::State_Raised; - } - if (isChecked()) - { opt.state |= QStyle::State_On; - } - if (isDown()) - { opt.state |= QStyle::State_Sunken; - } - - if (const auto *tb = qobject_cast(parent())) - { + + if (const QTabBar *tb = qobject_cast(parent())) { int index = tb->currentIndex(); - auto position = static_cast(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb)); + QTabBar::ButtonPosition position = (QTabBar::ButtonPosition) style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb); if (tb->tabButton(index, position) == this) - { opt.state |= QStyle::State_Selected; - } } style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this); } -TabSupervisor::TabSupervisor(AbstractClient *_client, QWidget *parent) : QTabWidget(parent), userInfo(nullptr), client(_client), tabServer(nullptr), tabUserLists(nullptr), tabDeckStorage(nullptr), tabReplays(nullptr), tabAdmin(nullptr), tabLog(nullptr) +TabSupervisor::TabSupervisor(AbstractClient *_client, QWidget *parent) + : QTabWidget(parent), userInfo(0), client(_client), tabServer(0), tabUserLists(0), tabDeckStorage(0), tabReplays(0), tabAdmin(0), tabLog(0) { setElideMode(Qt::ElideRight); setMovable(true); @@ -122,63 +113,42 @@ void TabSupervisor::retranslateUi() tabs.append(tabLog); QMapIterator roomIterator(roomTabs); while (roomIterator.hasNext()) - { tabs.append(roomIterator.next().value()); - } - QMapIterator gameIterator(gameTabs); while (gameIterator.hasNext()) - { tabs.append(gameIterator.next().value()); - } - QListIterator replayIterator(replayTabs); while (replayIterator.hasNext()) - { tabs.append(replayIterator.next()); - } - QListIterator deckEditorIterator(deckEditorTabs); while (deckEditorIterator.hasNext()) - { tabs.append(deckEditorIterator.next()); - } - QMapIterator messageIterator(messageTabs); while (messageIterator.hasNext()) - { tabs.append(messageIterator.next().value()); - } - for (auto &tab : tabs) - { - if (tab) - { - int idx = indexOf(tab); - QString tabText = tab->getTabText(); + for (int i = 0; i < tabs.size(); ++i) + if (tabs[i]) { + int idx = indexOf(tabs[i]); + QString tabText = tabs[i]->getTabText(); setTabText(idx, sanitizeTabName(tabText)); setTabToolTip(idx, sanitizeHtml(tabText)); - tab->retranslateUi(); + tabs[i]->retranslateUi(); } - } } bool TabSupervisor::closeRequest() { - if (getGameCount()) - { - if (QMessageBox::question(this, tr("Are you sure?"), tr("There are still open games. Are you sure you want to quit?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) - { + if (getGameCount()) { + if (QMessageBox::question(this, tr("Are you sure?"), tr("There are still open games. Are you sure you want to quit?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) { return false; } } - for (TabDeckEditor *tab : deckEditorTabs) + foreach(TabDeckEditor *tab, deckEditorTabs) { - if (!tab->confirmClose()) - { + if(!tab->confirmClose()) return false; - } } return true; @@ -215,52 +185,53 @@ int TabSupervisor::myAddTab(Tab *tab) return idx; } -void TabSupervisor::start(const ServerInfo_User &_userInfo) -{ +void TabSupervisor::start(const ServerInfo_User &_userInfo) { isLocalGame = false; userInfo = new ServerInfo_User(_userInfo); tabServer = new TabServer(this, client); - connect(tabServer, SIGNAL(roomJoined(const ServerInfo_Room &, bool)), this, SLOT(addRoomTab(const ServerInfo_Room &, bool))); + connect(tabServer, SIGNAL(roomJoined( + const ServerInfo_Room &, bool)), this, SLOT(addRoomTab( + const ServerInfo_Room &, bool))); myAddTab(tabServer); tabUserLists = new TabUserLists(this, client, *userInfo); - connect(tabUserLists, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool))); + connect(tabUserLists, SIGNAL(openMessageDialog( + const QString &, bool)), this, SLOT(addMessageTab( + const QString &, bool))); connect(tabUserLists, SIGNAL(userJoined(ServerInfo_User)), this, SLOT(processUserJoined(ServerInfo_User))); - connect(tabUserLists, SIGNAL(userLeft(const QString &)), this, SLOT(processUserLeft(const QString &))); + connect(tabUserLists, SIGNAL(userLeft( + const QString &)), this, SLOT(processUserLeft( + const QString &))); myAddTab(tabUserLists); updatePingTime(0, -1); - if (userInfo->user_level() & ServerInfo_User::IsRegistered) - { + if (userInfo->user_level() & ServerInfo_User::IsRegistered) { tabDeckStorage = new TabDeckStorage(this, client); - connect(tabDeckStorage, SIGNAL(openDeckEditor(const DeckLoader *)), this, SLOT(addDeckEditorTab(const DeckLoader *))); + connect(tabDeckStorage, SIGNAL(openDeckEditor( + const DeckLoader *)), this, SLOT(addDeckEditorTab( + const DeckLoader *))); myAddTab(tabDeckStorage); tabReplays = new TabReplays(this, client); - connect(tabReplays, SIGNAL(openReplay(GameReplay* )), this, SLOT(openReplay(GameReplay* ))); + connect(tabReplays, SIGNAL(openReplay(GameReplay * )), this, SLOT(openReplay(GameReplay * ))); myAddTab(tabReplays); - } - else - { - tabDeckStorage = nullptr; - tabReplays = nullptr; + } else { + tabDeckStorage = 0; + tabReplays = 0; } - if (userInfo->user_level() & ServerInfo_User::IsModerator) - { - tabAdmin = new TabAdmin(this, client, static_cast(userInfo->user_level() & ServerInfo_User::IsAdmin)); + if (userInfo->user_level() & ServerInfo_User::IsModerator) { + tabAdmin = new TabAdmin(this, client, (userInfo->user_level() & ServerInfo_User::IsAdmin)); connect(tabAdmin, SIGNAL(adminLockChanged(bool)), this, SIGNAL(adminLockChanged(bool))); myAddTab(tabAdmin); tabLog = new TabLog(this, client); myAddTab(tabLog); - } - else - { - tabAdmin = nullptr; - tabLog = nullptr; + } else { + tabAdmin = 0; + tabLog = 0; } retranslateUi(); @@ -276,117 +247,82 @@ void TabSupervisor::startLocal(const QList &_clients) isLocalGame = true; userInfo = new ServerInfo_User; localClients = _clients; - for (auto &localClient : localClients) - { - connect(localClient, SIGNAL(gameEventContainerReceived(const GameEventContainer &)), this, SLOT(processGameEventContainer(const GameEventContainer &))); - } - + for (int i = 0; i < localClients.size(); ++i) + connect(localClients[i], SIGNAL(gameEventContainerReceived(const GameEventContainer &)), this, SLOT(processGameEventContainer(const GameEventContainer &))); connect(localClients.first(), SIGNAL(gameJoinedEventReceived(const Event_GameJoined &)), this, SLOT(localGameJoined(const Event_GameJoined &))); } void TabSupervisor::stop() { if ((!client) && localClients.isEmpty()) - { return; - } - if (!localClients.isEmpty()) - { - for (auto &localClient : localClients) - { - localClient->deleteLater(); - } - + if (!localClients.isEmpty()) { + for (int i = 0; i < localClients.size(); ++i) + localClients[i]->deleteLater(); localClients.clear(); emit localGameEnded(); - } - else - { + } else { if (tabUserLists) - { tabUserLists->deleteLater(); - } - if (tabServer) - { tabServer->deleteLater(); - } - if (tabDeckStorage) - { tabDeckStorage->deleteLater(); - } - if (tabReplays) - { tabReplays->deleteLater(); - } - if (tabAdmin) - { tabAdmin->deleteLater(); - } - if (tabLog) - { tabLog->deleteLater(); - } } - - tabUserLists = nullptr; - tabServer = nullptr; - tabDeckStorage = nullptr; - tabReplays = nullptr; - tabAdmin = nullptr; - tabLog = nullptr; + tabUserLists = 0; + tabServer = 0; + tabDeckStorage = 0; + tabReplays = 0; + tabAdmin = 0; + tabLog = 0; QMapIterator roomIterator(roomTabs); while (roomIterator.hasNext()) - { roomIterator.next().value()->deleteLater(); - } roomTabs.clear(); QMapIterator gameIterator(gameTabs); while (gameIterator.hasNext()) - { gameIterator.next().value()->deleteLater(); - } gameTabs.clear(); QListIterator replayIterator(replayTabs); while (replayIterator.hasNext()) - { replayIterator.next()->deleteLater(); - } replayTabs.clear(); delete userInfo; - userInfo = nullptr; + userInfo = 0; } void TabSupervisor::updatePingTime(int value, int max) { - if (!tabServer || tabServer->getContentsChanged()) - { + if (!tabServer) + return; + if (tabServer->getContentsChanged()) return; - } setTabIcon(indexOf(tabServer), QIcon(PingPixmapGenerator::generatePixmap(15, value, max))); } void TabSupervisor::closeButtonPressed() { - Tab *tab = dynamic_cast(dynamic_cast(sender())->property("tab").value()); + Tab *tab = static_cast(static_cast(sender())->property("tab").value()); tab->closeRequest(); } void TabSupervisor::addCloseButtonToTab(Tab *tab, int tabIndex) { - auto closeSide = static_cast(tabBar()->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tabBar())); - auto *closeButton = new CloseButton; + QTabBar::ButtonPosition closeSide = (QTabBar::ButtonPosition) tabBar()->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tabBar()); + CloseButton *closeButton = new CloseButton; connect(closeButton, SIGNAL(clicked()), this, SLOT(closeButtonPressed())); closeButton->setProperty("tab", qVariantFromValue((QObject *) tab)); tabBar()->setTabButton(tabIndex, closeSide, closeButton); @@ -397,16 +333,10 @@ void TabSupervisor::gameJoined(const Event_GameJoined &event) QMap roomGameTypes; TabRoom *room = roomTabs.value(event.game_info().room_id()); if (room) - { roomGameTypes = room->getGameTypes(); - } else - { for (int i = 0; i < event.game_types_size(); ++i) - { roomGameTypes.insert(event.game_types(i).game_type_id(), QString::fromStdString(event.game_types(i).description())); - } - } TabGame *tab = new TabGame(this, QList() << client, event, roomGameTypes); connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *))); @@ -428,8 +358,7 @@ void TabSupervisor::localGameJoined(const Event_GameJoined &event) gameTabs.insert(event.game_info().game_id(), tab); setCurrentWidget(tab); - for (int i = 1; i < localClients.size(); ++i) - { + for (int i = 1; i < localClients.size(); ++i) { Command_JoinGame cmd; cmd.set_game_id(event.game_info().game_id()); localClients[i]->sendCommand(localClients[i]->prepareRoomCommand(cmd, 0)); @@ -438,28 +367,19 @@ void TabSupervisor::localGameJoined(const Event_GameJoined &event) void TabSupervisor::gameLeft(TabGame *tab) { - if (tab == nullptr) - { - return; - } - if (tab == currentWidget()) - { emit setMenu(); - } gameTabs.remove(tab->getGameId()); removeTab(indexOf(tab)); if (!localClients.isEmpty()) - { stop(); - } } void TabSupervisor::addRoomTab(const ServerInfo_Room &info, bool setCurrent) { - auto *tab = new TabRoom(this, client, userInfo, info); + TabRoom *tab = new TabRoom(this, client, userInfo, info); connect(tab, SIGNAL(maximizeClient()), this, SLOT(maximizeMainWindow())); connect(tab, SIGNAL(roomClosing(TabRoom *)), this, SLOT(roomLeft(TabRoom *))); connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool))); @@ -467,22 +387,13 @@ void TabSupervisor::addRoomTab(const ServerInfo_Room &info, bool setCurrent) addCloseButtonToTab(tab, tabIndex); roomTabs.insert(info.room_id(), tab); if (setCurrent) - { setCurrentWidget(tab); - } } void TabSupervisor::roomLeft(TabRoom *tab) { - if (tab == nullptr) - { - return; - } - if (tab == currentWidget()) - { emit setMenu(); - } roomTabs.remove(tab->getRoomId()); removeTab(indexOf(tab)); @@ -490,7 +401,7 @@ void TabSupervisor::roomLeft(TabRoom *tab) void TabSupervisor::openReplay(GameReplay *replay) { - auto *replayTab = new TabGame(this, replay); + TabGame *replayTab = new TabGame(this, replay); connect(replayTab, SIGNAL(gameClosing(TabGame *)), this, SLOT(replayLeft(TabGame *))); int tabIndex = myAddTab(replayTab); addCloseButtonToTab(replayTab, tabIndex); @@ -501,9 +412,7 @@ void TabSupervisor::openReplay(GameReplay *replay) void TabSupervisor::replayLeft(TabGame *tab) { if (tab == currentWidget()) - { emit setMenu(); - } replayTabs.removeAt(replayTabs.indexOf(tab)); } @@ -511,30 +420,20 @@ void TabSupervisor::replayLeft(TabGame *tab) TabMessage *TabSupervisor::addMessageTab(const QString &receiverName, bool focus) { if (receiverName == QString::fromStdString(userInfo->name())) - { - return nullptr; - } + return 0; ServerInfo_User otherUser; UserListTWI *twi = tabUserLists->getAllUsersList()->getUsers().value(receiverName); if (twi) - { otherUser = twi->getUserInfo(); - } else - { otherUser.set_name(receiverName.toStdString()); - } TabMessage *tab; tab = messageTabs.value(QString::fromStdString(otherUser.name())); - if (tab) - { + if (tab) { if (focus) - { - setCurrentWidget(tab); - } - + setCurrentWidget(tab); return tab; } @@ -545,28 +444,18 @@ TabMessage *TabSupervisor::addMessageTab(const QString &receiverName, bool focus addCloseButtonToTab(tab, tabIndex); messageTabs.insert(receiverName, tab); if (focus) - { setCurrentWidget(tab); - } return tab; } -void TabSupervisor::maximizeMainWindow() -{ +void TabSupervisor::maximizeMainWindow() { emit showWindowIfHidden(); } void TabSupervisor::talkLeft(TabMessage *tab) { - if (tab == nullptr) - { - return; - } - if (tab == currentWidget()) - { emit setMenu(); - } messageTabs.remove(tab->getUserName()); removeTab(indexOf(tab)); @@ -574,13 +463,9 @@ void TabSupervisor::talkLeft(TabMessage *tab) TabDeckEditor *TabSupervisor::addDeckEditorTab(const DeckLoader *deckToOpen) { - auto *tab = new TabDeckEditor(this); - + TabDeckEditor *tab = new TabDeckEditor(this); if (deckToOpen) - { tab->setDeck(new DeckLoader(*deckToOpen)); - } - connect(tab, SIGNAL(deckEditorClosing(TabDeckEditor *)), this, SLOT(deckEditorClosed(TabDeckEditor *))); int tabIndex = myAddTab(tab); addCloseButtonToTab(tab, tabIndex); @@ -591,15 +476,8 @@ TabDeckEditor *TabSupervisor::addDeckEditorTab(const DeckLoader *deckToOpen) void TabSupervisor::deckEditorClosed(TabDeckEditor *tab) { - if (tab == nullptr) - { - return; - } - if (tab == currentWidget()) - { emit setMenu(); - } deckEditorTabs.removeAt(deckEditorTabs.indexOf(tab)); removeTab(indexOf(tab)); @@ -607,18 +485,13 @@ void TabSupervisor::deckEditorClosed(TabDeckEditor *tab) void TabSupervisor::tabUserEvent(bool globalEvent) { - auto *tab = dynamic_cast(sender()); - - if (tab != currentWidget()) - { + Tab *tab = static_cast(sender()); + if (tab != currentWidget()) { tab->setContentsChanged(true); setTabIcon(indexOf(tab), QPixmap("theme:icons/tab_changed")); } - if (globalEvent && settingsCache->getNotificationsEnabled()) - { QApplication::alert(this); - } } void TabSupervisor::updateTabText(Tab *tab, const QString &newTabText) @@ -631,129 +504,90 @@ void TabSupervisor::updateTabText(Tab *tab, const QString &newTabText) void TabSupervisor::processRoomEvent(const RoomEvent &event) { TabRoom *tab = roomTabs.value(event.room_id(), 0); - if (tab) - { tab->processRoomEvent(event); - } } void TabSupervisor::processGameEventContainer(const GameEventContainer &cont) { TabGame *tab = gameTabs.value(cont.game_id()); - if (tab) - { tab->processGameEventContainer(cont, qobject_cast(sender())); - } else - { qDebug() << "gameEvent: invalid gameId"; - } } void TabSupervisor::processUserMessageEvent(const Event_UserMessage &event) { QString senderName = QString::fromStdString(event.sender_name()); TabMessage *tab = messageTabs.value(senderName); - - if (tab == nullptr) - { + if (!tab) tab = messageTabs.value(QString::fromStdString(event.receiver_name())); - } - - if (tab == nullptr) - { + if (!tab) { UserListTWI *twi = tabUserLists->getAllUsersList()->getUsers().value(senderName); - - if (twi) - { + if (twi) { UserLevelFlags userLevel = UserLevelFlags(twi->getUserInfo().user_level()); - if (settingsCache->getIgnoreUnregisteredUserMessages() && !userLevel.testFlag(ServerInfo_User::IsRegistered)) - { + if (settingsCache->getIgnoreUnregisteredUserMessages() && + !userLevel.testFlag(ServerInfo_User::IsRegistered)) // Flags are additive, so reg/mod/admin are all IsRegistered return; - } } - tab = addMessageTab(QString::fromStdString(event.sender_name()), false); } - - if (tab == nullptr) - { + if (!tab) return; - } - tab->processUserMessageEvent(event); } void TabSupervisor::processUserLeft(const QString &userName) { TabMessage *tab = messageTabs.value(userName); - if (tab) - { tab->processUserLeft(); - } } void TabSupervisor::processUserJoined(const ServerInfo_User &userInfo) { TabMessage *tab = messageTabs.value(QString::fromStdString(userInfo.name())); if (tab) - { tab->processUserJoined(userInfo); - } } void TabSupervisor::updateCurrent(int index) { - if (index != -1) - { - auto *tab = dynamic_cast(widget(index)); - if (tab->getContentsChanged()) - { + if (index != -1) { + Tab *tab = static_cast(widget(index)); + if (tab->getContentsChanged()) { setTabIcon(index, QIcon()); tab->setContentsChanged(false); } - emit setMenu(dynamic_cast(widget(index))->getTabMenus()); + emit setMenu(static_cast(widget(index))->getTabMenus()); tab->tabActivated(); - } - else - { + } else emit setMenu(); - } } bool TabSupervisor::getAdminLocked() const { if (!tabAdmin) - { return true; - } return tabAdmin->getLocked(); } void TabSupervisor::processNotifyUserEvent(const Event_NotifyUser &event) { - switch ((Event_NotifyUser::NotificationType) event.type()) - { + switch ((Event_NotifyUser::NotificationType) event.type()) { case Event_NotifyUser::UNKNOWN: QMessageBox::information(this, tr("Unknown Event"), tr("The server has sent you a message that your client does not understand.\nThis message might mean there is a new version of Cockatrice available or this server is running a custom or pre-release version.\n\nTo update your client, go to Help -> Check for Updates.")); break; case Event_NotifyUser::IDLEWARNING: QMessageBox::information(this, tr("Idle Timeout"), tr("You are about to be logged out due to inactivity.")); break; case Event_NotifyUser::PROMOTED: QMessageBox::information(this, tr("Promotion"), tr("You have been promoted to moderator. Please log out and back in for changes to take effect.")); break; - case Event_NotifyUser::WARNING: - { + case Event_NotifyUser::WARNING: { if (!QString::fromStdString(event.warning_reason()).simplified().isEmpty()) - { QMessageBox::warning(this, tr("Warned"), tr("You have received a warning due to %1.\nPlease refrain from engaging in this activity or further actions may be taken against you. If you have any questions, please private message a moderator.").arg(QString::fromStdString(event.warning_reason()).simplified())); - } break; } - case Event_NotifyUser::CUSTOM: - { - if (!QString::fromStdString(event.custom_title()).simplified().isEmpty() && !QString::fromStdString(event.custom_content()).simplified().isEmpty()) - { + case Event_NotifyUser::CUSTOM: { + if (!QString::fromStdString(event.custom_title()).simplified().isEmpty() && !QString::fromStdString(event.custom_content()).simplified().isEmpty()) { QMessageBox msgBox; msgBox.setParent(this); msgBox.setWindowFlags(Qt::Dialog); @@ -766,7 +600,7 @@ void TabSupervisor::processNotifyUserEvent(const Event_NotifyUser &event) } break; } - default: break; + default: ; } } @@ -783,11 +617,8 @@ QString TabSupervisor::getOwnUsername() const bool TabSupervisor::isUserBuddy(const QString &userName) const { - if (!getUserListsTab() || !getUserListsTab()->getBuddyList()) - { - return false; - } - + if (!getUserListsTab()) return false; + if (!getUserListsTab()->getBuddyList()) return false; QMap buddyList = getUserListsTab()->getBuddyList()->getUsers(); bool senderIsBuddy = buddyList.contains(userName); return senderIsBuddy; @@ -795,33 +626,26 @@ bool TabSupervisor::isUserBuddy(const QString &userName) const bool TabSupervisor::isUserIgnored(const QString &userName) const { - if (!getUserListsTab() || !getUserListsTab()->getIgnoreList()) - { - return false; - } - + if (!getUserListsTab()) return false; + if (!getUserListsTab()->getIgnoreList()) return false; QMap buddyList = getUserListsTab()->getIgnoreList()->getUsers(); bool senderIsBuddy = buddyList.contains(userName); return senderIsBuddy; } -const ServerInfo_User* TabSupervisor::getOnlineUser(const QString &userName) const +const ServerInfo_User * TabSupervisor::getOnlineUser(const QString &userName) const { - if (!getUserListsTab() || !getUserListsTab()->getAllUsersList()) - { - return nullptr; - } - + if (!getUserListsTab()) return nullptr; + if (!getUserListsTab()->getAllUsersList()) return nullptr; QMap userList = getUserListsTab()->getAllUsersList()->getUsers(); const QString &userNameToMatchLower = userName.toLower(); QMap::iterator i; for (i = userList.begin(); i != userList.end(); ++i) - if (i.key().toLower() == userNameToMatchLower) - { + if (i.key().toLower() == userNameToMatchLower) { const ServerInfo_User &userInfo = i.value()->getUserInfo(); return &userInfo; } return nullptr; -}; \ No newline at end of file +}; diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index b256a68c..6a7a4a15 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -30,97 +30,90 @@ class ServerInfo_User; class GameReplay; class DeckList; -class CloseButton : public QAbstractButton -{ +class CloseButton : public QAbstractButton { Q_OBJECT - public: - explicit CloseButton(QWidget *parent = nullptr); - QSize sizeHint() const override; - inline QSize minimumSizeHint() const override { return sizeHint(); } - - protected: - void enterEvent(QEvent *event) override; - void leaveEvent(QEvent *event) override; - void paintEvent(QPaintEvent *event) override; +public: + CloseButton(QWidget *parent = 0); + QSize sizeHint() const; + inline QSize minimumSizeHint() const { return sizeHint(); } +protected: + void enterEvent(QEvent *event); + void leaveEvent(QEvent *event); + void paintEvent(QPaintEvent *event); }; -class TabSupervisor : public QTabWidget, public UserlistProxy -{ +class TabSupervisor : public QTabWidget, public UserlistProxy { Q_OBJECT - private: - ServerInfo_User *userInfo; - AbstractClient *client; - QList localClients; - TabServer *tabServer; - TabUserLists *tabUserLists; - TabDeckStorage *tabDeckStorage; - TabReplays *tabReplays; - TabAdmin *tabAdmin; - TabLog *tabLog; - QMap roomTabs; - QMap gameTabs; - QList replayTabs; - QMap messageTabs; - QList deckEditorTabs; - int myAddTab(Tab *tab); - void addCloseButtonToTab(Tab *tab, int tabIndex); - QString sanitizeTabName(QString dirty) const; - QString sanitizeHtml(QString dirty) const; - bool isLocalGame; - - public: - explicit TabSupervisor(AbstractClient *_client, QWidget *parent = nullptr); - ~TabSupervisor() override; - void retranslateUi(); - void start(const ServerInfo_User &userInfo); - void startLocal(const QList &_clients); - void stop(); - bool getIsLocalGame() const { return isLocalGame; } - int getGameCount() const { return gameTabs.size(); } - TabUserLists *getUserListsTab() const { return tabUserLists; } - ServerInfo_User *getUserInfo() const { return userInfo; } - AbstractClient *getClient() const; - const QMap &getRoomTabs() const { return roomTabs; } - bool getAdminLocked() const; - bool closeRequest(); - bool isOwnUserRegistered() const override; - QString getOwnUsername() const override; - bool isUserBuddy(const QString &userName) const override; - bool isUserIgnored(const QString &userName) const override; - const ServerInfo_User* getOnlineUser(const QString &userName) const override; - - signals: - void setMenu(const QList &newMenuList = QList()); - void localGameEnded(); - void adminLockChanged(bool lock); - void showWindowIfHidden(); - - public slots: - TabDeckEditor *addDeckEditorTab(const DeckLoader *deckToOpen); - void openReplay(GameReplay *replay); - void maximizeMainWindow(); - - private slots: - void closeButtonPressed(); - void updateCurrent(int index); - void updatePingTime(int value, int max); - void gameJoined(const Event_GameJoined &event); - void localGameJoined(const Event_GameJoined &event); - void gameLeft(TabGame *tab); - void addRoomTab(const ServerInfo_Room &info, bool setCurrent); - void roomLeft(TabRoom *tab); - TabMessage *addMessageTab(const QString &userName, bool focus); - void replayLeft(TabGame *tab); - void processUserLeft(const QString &userName); - void processUserJoined(const ServerInfo_User &userInfo); - void talkLeft(TabMessage *tab); - void deckEditorClosed(TabDeckEditor *tab); - void tabUserEvent(bool globalEvent); - void updateTabText(Tab *tab, const QString &newTabText); - void processRoomEvent(const RoomEvent &event); - void processGameEventContainer(const GameEventContainer &cont); - void processUserMessageEvent(const Event_UserMessage &event); - void processNotifyUserEvent(const Event_NotifyUser &event); +private: + ServerInfo_User *userInfo; + AbstractClient *client; + QList localClients; + TabServer *tabServer; + TabUserLists *tabUserLists; + TabDeckStorage *tabDeckStorage; + TabReplays *tabReplays; + TabAdmin *tabAdmin; + TabLog *tabLog; + QMap roomTabs; + QMap gameTabs; + QList replayTabs; + QMap messageTabs; + QList deckEditorTabs; + int myAddTab(Tab *tab); + void addCloseButtonToTab(Tab *tab, int tabIndex); + QString sanitizeTabName(QString dirty) const; + QString sanitizeHtml(QString dirty) const; + bool isLocalGame; +public: + TabSupervisor(AbstractClient *_client, QWidget *parent = 0); + ~TabSupervisor(); + void retranslateUi(); + void start(const ServerInfo_User &userInfo); + void startLocal(const QList &_clients); + void stop(); + bool getIsLocalGame() const { return isLocalGame; } + int getGameCount() const { return gameTabs.size(); } + TabUserLists *getUserListsTab() const { return tabUserLists; } + ServerInfo_User *getUserInfo() const { return userInfo; } + AbstractClient *getClient() const; + const QMap &getRoomTabs() const { return roomTabs; } + bool getAdminLocked() const; + bool closeRequest(); + bool isOwnUserRegistered() const; + QString getOwnUsername() const; + bool isUserBuddy(const QString &userName) const; + bool isUserIgnored(const QString &userName) const; + const ServerInfo_User* getOnlineUser(const QString &userName) const; +signals: + void setMenu(const QList &newMenuList = QList()); + void localGameEnded(); + void adminLockChanged(bool lock); + void showWindowIfHidden(); +public slots: + TabDeckEditor *addDeckEditorTab(const DeckLoader *deckToOpen); + void openReplay(GameReplay *replay); + void maximizeMainWindow(); +private slots: + void closeButtonPressed(); + void updateCurrent(int index); + void updatePingTime(int value, int max); + void gameJoined(const Event_GameJoined &event); + void localGameJoined(const Event_GameJoined &event); + void gameLeft(TabGame *tab); + void addRoomTab(const ServerInfo_Room &info, bool setCurrent); + void roomLeft(TabRoom *tab); + TabMessage *addMessageTab(const QString &userName, bool focus); + void replayLeft(TabGame *tab); + void processUserLeft(const QString &userName); + void processUserJoined(const ServerInfo_User &userInfo); + void talkLeft(TabMessage *tab); + void deckEditorClosed(TabDeckEditor *tab); + void tabUserEvent(bool globalEvent); + void updateTabText(Tab *tab, const QString &newTabText); + void processRoomEvent(const RoomEvent &event); + void processGameEventContainer(const GameEventContainer &cont); + void processUserMessageEvent(const Event_UserMessage &event); + void processNotifyUserEvent(const Event_NotifyUser &event); }; #endif \ No newline at end of file