Cockatrice/cockatrice/src/game_graphics/deckview/tabbed_deck_view_container.cpp
BruebachL 3fa8d78786
[Tests] Play card interactions through a Cockatrice event player (#7697)
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-11-04 06:23:45 +01:00

75 lines
2.1 KiB
C++

#include "tabbed_deck_view_container.h"
#include "../board/abstract_card_item.h"
#include "deck_view.h"
#include "deck_view_container.h"
#include <QSharedPointer>
#include <QTabBar>
#include <QWidget>
class DeckList;
class TabGame;
TabbedDeckViewContainer::TabbedDeckViewContainer(int _playerId, TabGame *parent)
: QTabWidget(nullptr), playerId(_playerId), parentGame(parent)
{
setObjectName(QString("tabbedDeckView_%1").arg(_playerId));
setTabsClosable(true);
connect(this, &QTabWidget::tabCloseRequested, this, &TabbedDeckViewContainer::closeTab);
playerDeckView = new DeckViewContainer(playerId, parentGame);
int playerTabIndex = addTab(playerDeckView, "Your Deck");
tabBar()->setTabButton(playerTabIndex, QTabBar::RightSide, nullptr);
updateTabBarVisibility();
}
void TabbedDeckViewContainer::addOpponentDeckView(const DeckList &opponentDeck, int opponentId, QString opponentName)
{
if (opponentDeckViews.contains(opponentId)) {
opponentDeckViews[opponentId]->setDeck(opponentDeck);
} else {
auto *opponentDeckView = new DeckView(this);
connect(opponentDeckView, &DeckView::newCardAdded, playerDeckView, &DeckViewContainer::newCardAdded);
opponentDeckView->setDeck(opponentDeck);
addTab(opponentDeckView, QString("%1's Deck").arg(opponentName));
opponentDeckViews.insert(opponentId, opponentDeckView);
}
updateTabBarVisibility();
}
void TabbedDeckViewContainer::closeTab(int index)
{
QWidget *widgetToClose = widget(index);
// Prevent removing the player tab
if (widgetToClose == playerDeckView) {
return;
}
// Remove it from map if it's an opponent
auto it = opponentDeckViews.begin();
while (it != opponentDeckViews.end()) {
if (it.value() == widgetToClose) {
it = opponentDeckViews.erase(it);
} else {
++it;
}
}
removeTab(index);
widgetToClose->deleteLater();
updateTabBarVisibility();
}
void TabbedDeckViewContainer::updateTabBarVisibility()
{
if (tabBar()->count() <= 1) {
tabBar()->hide();
} else {
tabBar()->show();
}
}