New visual deck storage (#5290)

* Add TabDeckStorageVisual

* Visual Deck Storage

* Add BannerCard to .cod format, use it in the visual deck storage widget.

* Show filename instead of deckname if deck name is empty.

* Lint.

* Don't delint cmake list through hooks.

* Add deck loading functionality.

* Open Decks on double click, not single click.

* Void event for now.

* Fix build issue with overload?

* Fix build issue with overload?

* Include QDebug.

* Turn the tab into a widget.

* Move the signals down to the widget, move the connections and slots up to the parent widgets.

* No banner card equals an empty CardInfoPtr.

* Add an option to sort by filename or last modified.

* Flip last modified comparison.

* Lint.

* Don't open decks twice in the storage tab.

* Fix unload deck not working by showing/hiding widgets instead of adding/removing to layout.

* Add a search bar.

* Add a card size slider.

* Lint.

* Lint.

* Lint.

* Fix settings mocks.

* No need to QDebug.

* No need to QDebug.

* Member variable.

* Member variable.

* Non-lambda.

* Change set to list conversion.

* Specify overload.

* Include MouseEvent

* Adjust font size dynamically.

* Add an option to show the visual deck storage on database load.

* Fix the close button not working on the tab, add an option to launch the visual deck storage tab to Cockatrice menu.

* Override virtual functions.

* Correct tab text.

* Add a setting to remember last used sorting order for visual deck storage widget.

* Update banner card combo box correctly.

* Fix mocks.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
Co-authored-by: Zach H <zahalpern+github@gmail.com>
This commit is contained in:
BruebachL 2025-01-06 00:12:20 +01:00 committed by GitHub
parent 7496e79e8c
commit 62f7c7f9ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 834 additions and 31 deletions

View file

@ -26,6 +26,8 @@
#include <QApplication>
#include <QClipboard>
#include <QCloseEvent>
#include <QComboBox>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QDockWidget>
@ -95,6 +97,16 @@ void TabDeckEditor::createDeckDock()
commentsEdit->setObjectName("commentsEdit");
commentsLabel->setBuddy(commentsEdit);
connect(commentsEdit, SIGNAL(textChanged()), this, SLOT(updateComments()));
bannerCardLabel = new QLabel();
bannerCardLabel->setObjectName("bannerCardLabel");
bannerCardLabel->setText(tr("Banner Card"));
bannerCardComboBox = new QComboBox(this);
connect(deckModel, &DeckListModel::dataChanged, this, [this]() {
// Delay the update to avoid race conditions
QTimer::singleShot(100, this, &TabDeckEditor::updateBannerCardComboBox);
});
connect(bannerCardComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
&TabDeckEditor::setBannerCard);
aIncrement = new QAction(QString(), this);
aIncrement->setIcon(QPixmap("theme:icons/increment"));
@ -128,6 +140,9 @@ void TabDeckEditor::createDeckDock()
upperLayout->addWidget(commentsLabel, 1, 0);
upperLayout->addWidget(commentsEdit, 1, 1);
upperLayout->addWidget(bannerCardLabel, 2, 0);
upperLayout->addWidget(bannerCardComboBox, 2, 1);
hashLabel1 = new QLabel();
hashLabel1->setObjectName("hashLabel1");
auto *hashSizePolicy = new QSizePolicy();
@ -802,6 +817,71 @@ void TabDeckEditor::updateComments()
setSaveStatus(true);
}
void TabDeckEditor::updateBannerCardComboBox()
{
// Store the current text of the combo box
QString currentText = bannerCardComboBox->currentText();
// Block signals temporarily
bool wasBlocked = bannerCardComboBox->blockSignals(true);
// Clear the existing items in the combo box
bannerCardComboBox->clear();
// Prepare the new items with deduplication
QSet<QString> bannerCardSet;
InnerDecklistNode *listRoot = deckModel->getDeckList()->getRoot();
for (int i = 0; i < listRoot->size(); i++) {
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
for (int j = 0; j < currentZone->size(); j++) {
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
if (!currentCard)
continue;
for (int k = 0; k < currentCard->getNumber(); ++k) {
CardInfoPtr info = CardDatabaseManager::getInstance()->getCard(currentCard->getName());
if (info) {
bannerCardSet.insert(currentCard->getName());
}
}
}
}
// Convert the QSet to a sorted QStringList
QStringList bannerCardChoices;
for (const QString &entry : bannerCardSet) {
bannerCardChoices.append(entry);
}
bannerCardChoices.sort(Qt::CaseInsensitive);
// Populate the combo box with new items
bannerCardComboBox->addItems(bannerCardChoices);
// Try to restore the previous selection by finding the currentText
int restoredIndex = bannerCardComboBox->findText(currentText);
if (restoredIndex != -1) {
bannerCardComboBox->setCurrentIndex(restoredIndex);
} else {
// Add a placeholder "-" and set it as the current selection
int bannerIndex = bannerCardComboBox->findText(deckModel->getDeckList()->getBannerCard());
if (bannerIndex != -1) {
bannerCardComboBox->setCurrentIndex(bannerIndex);
} else {
bannerCardComboBox->insertItem(0, "-");
bannerCardComboBox->setCurrentIndex(0);
}
}
// Restore the previous signal blocking state
bannerCardComboBox->blockSignals(wasBlocked);
}
void TabDeckEditor::setBannerCard(int /* changedIndex */)
{
qDebug() << "Banner card was set to: " << bannerCardComboBox->currentText();
deckModel->getDeckList()->setBannerCard(bannerCardComboBox->currentText());
}
void TabDeckEditor::updateCardInfo(CardInfoPtr _card)
{
cardInfo->setCard(_card);
@ -941,6 +1021,7 @@ void TabDeckEditor::actLoadDeck()
QString fileName = dialog.selectedFiles().at(0);
openDeckFromFile(fileName, deckOpenLocation);
updateBannerCardComboBox();
}
void TabDeckEditor::actOpenRecent(const QString &fileName)
@ -966,6 +1047,10 @@ void TabDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLocation d
auto *l = new DeckLoader;
if (l->loadFromFile(fileName, fmt)) {
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(fileName);
updateBannerCardComboBox();
if (!l->getBannerCard().isEmpty()) {
bannerCardComboBox->setCurrentIndex(bannerCardComboBox->findText(l->getBannerCard()));
}
if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(l);
} else {
@ -1429,10 +1514,14 @@ void TabDeckEditor::actDecrement()
void TabDeckEditor::setDeck(DeckLoader *_deck)
{
qDebug() << " ORIGINAL BANNER CARD " << _deck->getBannerCard();
deckModel->setDeckList(_deck);
nameEdit->setText(deckModel->getDeckList()->getName());
commentsEdit->setText(deckModel->getDeckList()->getComments());
qDebug() << deckModel->getDeckList()->getBannerCard() << " was the banner card";
bannerCardComboBox->setCurrentText(deckModel->getDeckList()->getBannerCard());
updateBannerCardComboBox();
updateHash();
deckModel->sort(deckView->header()->sortIndicatorSection(), deckView->header()->sortIndicatorOrder());
deckView->expandAll();

View file

@ -22,6 +22,7 @@ class DeckLoader;
class Response;
class FilterTreeModel;
class FilterBuilder;
class QComboBox;
class QGroupBox;
class QMessageBox;
class QHBoxLayout;
@ -35,6 +36,8 @@ class TabDeckEditor : public Tab
private slots:
void updateName(const QString &name);
void updateComments();
void updateBannerCardComboBox();
void setBannerCard(int);
void updateHash();
void updateCardInfoLeft(const QModelIndex &current, const QModelIndex &previous);
void updateCardInfoRight(const QModelIndex &current, const QModelIndex &previous);
@ -129,6 +132,8 @@ private:
LineEditUnfocusable *nameEdit;
QLabel *commentsLabel;
QTextEdit *commentsEdit;
QLabel *bannerCardLabel;
QComboBox *bannerCardComboBox;
QLabel *hashLabel1;
LineEditUnfocusable *hashLabel;
FilterTreeModel *filterModel;

View file

@ -105,6 +105,8 @@ DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent)
{
loadLocalButton = new QPushButton;
loadRemoteButton = new QPushButton;
unloadDeckButton = new QPushButton;
unloadDeckButton->setEnabled(false);
readyStartButton = new ToggleButton;
readyStartButton->setEnabled(false);
forceStartGameButton = new QPushButton;
@ -114,6 +116,7 @@ DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent)
connect(loadLocalButton, SIGNAL(clicked()), this, SLOT(loadLocalDeck()));
connect(readyStartButton, SIGNAL(clicked()), this, SLOT(readyStart()));
connect(unloadDeckButton, &QPushButton::clicked, this, &DeckViewContainer::unloadDeck);
connect(forceStartGameButton, &QPushButton::clicked, this, &DeckViewContainer::forceStart);
connect(sideboardLockButton, SIGNAL(clicked()), this, SLOT(sideboardLockButtonClicked()));
connect(sideboardLockButton, SIGNAL(stateChanged()), this, SLOT(updateSideboardLockButtonText()));
@ -127,6 +130,7 @@ DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent)
auto *buttonHBox = new QHBoxLayout;
buttonHBox->addWidget(loadLocalButton);
buttonHBox->addWidget(loadRemoteButton);
buttonHBox->addWidget(unloadDeckButton);
buttonHBox->addWidget(readyStartButton);
buttonHBox->addWidget(sideboardLockButton);
if (forceStartGameButton->isEnabled()) {
@ -134,13 +138,20 @@ DeckViewContainer::DeckViewContainer(int _playerId, TabGame *parent)
}
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()));
deckView->setVisible(false);
auto *deckViewLayout = new QVBoxLayout;
visualDeckStorageWidget = new VisualDeckStorageWidget(this);
connect(visualDeckStorageWidget, &VisualDeckStorageWidget::imageDoubleClicked, this,
&DeckViewContainer::replaceDeckStorageWithDeckView);
deckViewLayout = new QVBoxLayout;
deckViewLayout->addLayout(buttonHBox);
deckViewLayout->addWidget(deckView);
deckViewLayout->addWidget(visualDeckStorageWidget);
deckViewLayout->setContentsMargins(0, 0, 0, 0);
setLayout(deckViewLayout);
@ -153,6 +164,7 @@ void DeckViewContainer::retranslateUi()
{
loadLocalButton->setText(tr("Load deck..."));
loadRemoteButton->setText(tr("Load remote deck..."));
unloadDeckButton->setText(tr("Unload deck..."));
readyStartButton->setText(tr("Ready to start"));
forceStartGameButton->setText(tr("Force start"));
updateSideboardLockButtonText();
@ -287,6 +299,44 @@ void TabGame::refreshShortcuts()
}
}
void DeckViewContainer::replaceDeckStorageWithDeckView(QMouseEvent *event, DeckPreviewCardPictureWidget *instance)
{
Q_UNUSED(event);
QString fileName = instance->filePath;
DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName);
QString deckString;
DeckLoader deck;
bool error = !deck.loadFromFile(fileName, fmt);
if (!error) {
deckString = deck.writeToString_Native();
error = deckString.length() > MAX_FILE_LENGTH;
}
if (error) {
QMessageBox::critical(this, tr("Error"), tr("The selected file could not be loaded."));
return;
}
Command_DeckSelect cmd;
cmd.set_deck(deckString.toStdString());
PendingCommand *pend = parentGame->prepareGameCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this,
SLOT(deckSelectFinished(const Response &)));
parentGame->sendGameCommand(pend, playerId);
visualDeckStorageWidget->setVisible(false);
deckView->setVisible(true);
deckViewLayout->update();
unloadDeckButton->setEnabled(true);
}
void DeckViewContainer::unloadDeck()
{
deckView->setVisible(false);
visualDeckStorageWidget->setVisible(true);
deckViewLayout->update();
unloadDeckButton->setEnabled(false);
}
void DeckViewContainer::loadLocalDeck()
{
DlgLoadDeck dialog(this);

View file

@ -3,6 +3,7 @@
#include "../../client/tearoff_menu.h"
#include "../../game/player/player.h"
#include "../ui/widgets/visual_deck_storage/visual_deck_storage_widget.h"
#include "pb/event_leave.pb.h"
#include "pb/serverinfo_game.pb.h"
#include "tab.h"
@ -86,14 +87,18 @@ class DeckViewContainer : public QWidget
{
Q_OBJECT
private:
QPushButton *loadLocalButton, *loadRemoteButton, *forceStartGameButton;
QVBoxLayout *deckViewLayout;
QPushButton *loadLocalButton, *loadRemoteButton, *unloadDeckButton, *forceStartGameButton;
ToggleButton *readyStartButton, *sideboardLockButton;
DeckView *deckView;
VisualDeckStorageWidget *visualDeckStorageWidget;
TabGame *parentGame;
int playerId;
private slots:
void replaceDeckStorageWithDeckView(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
void loadLocalDeck();
void loadRemoteDeck();
void unloadDeck();
void readyStart();
void forceStart();
void deckSelectFinished(const Response &r);

View file

@ -24,6 +24,7 @@
#include "tab_replays.h"
#include "tab_room.h"
#include "tab_server.h"
#include "visual_deck_storage/tab_deck_storage_visual.h"
#include <QApplication>
#include <QDebug>
@ -504,6 +505,15 @@ TabDeckEditor *TabSupervisor::addDeckEditorTab(const DeckLoader *deckToOpen)
return tab;
}
TabDeckStorageVisual *TabSupervisor::addVisualDeckStorageTab()
{
TabDeckStorageVisual *tab = new TabDeckStorageVisual(this, client);
int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex);
setCurrentWidget(tab);
return tab;
}
void TabSupervisor::deckEditorClosed(TabDeckEditor *tab)
{
if (tab == currentWidget())

View file

@ -3,6 +3,7 @@
#include "../../deck/deck_loader.h"
#include "../../server/chat_view/user_list_proxy.h"
#include "visual_deck_storage/tab_deck_storage_visual.h"
#include <QAbstractButton>
#include <QCommonStyle>
@ -130,6 +131,7 @@ signals:
public slots:
TabDeckEditor *addDeckEditorTab(const DeckLoader *deckToOpen);
TabDeckStorageVisual *addVisualDeckStorageTab();
void openReplay(GameReplay *replay);
void maximizeMainWindow();
private slots:

View file

@ -0,0 +1,104 @@
#include "tab_deck_storage_visual.h"
#include "../../../game/cards/card_database_model.h"
#include "../tab_supervisor.h"
#include "pb/command_deck_del.pb.h"
#include <QAction>
#include <QDebug>
#include <QDirIterator>
#include <QFileSystemModel>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QMessageBox>
#include <QMouseEvent>
#include <QScreen>
#include <QToolBar>
#include <QTreeView>
class FlowLayout;
TabDeckStorageVisual::TabDeckStorageVisual(TabSupervisor *_tabSupervisor, AbstractClient *_client)
: Tab(_tabSupervisor), client(_client)
{
deck_list_model = new DeckListModel(this);
deck_list_model->setObjectName("visualDeckModel");
QWidget *container = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(container);
container->setLayout(layout);
this->setCentralWidget(container);
leftToolBar = new QToolBar;
leftToolBar->setOrientation(Qt::Horizontal);
leftToolBar->setIconSize(QSize(32, 32));
QHBoxLayout *leftToolBarLayout = new QHBoxLayout(this);
leftToolBarLayout->addStretch();
leftToolBarLayout->addWidget(leftToolBar);
leftToolBarLayout->addStretch();
aOpenLocalDeck = new QAction(this);
aOpenLocalDeck->setIcon(QPixmap("theme:icons/pencil"));
connect(aOpenLocalDeck, SIGNAL(triggered()), this, SLOT(actOpenLocalDeck()));
aDeleteLocalDeck = new QAction(this);
aDeleteLocalDeck->setIcon(QPixmap("theme:icons/remove_row"));
connect(aDeleteLocalDeck, SIGNAL(triggered()), this, SLOT(actDeleteLocalDeck()));
connect(this, &TabDeckStorageVisual::openDeckEditor, tabSupervisor, &TabSupervisor::addDeckEditorTab);
leftToolBar->addAction(aOpenLocalDeck);
leftToolBar->addAction(aDeleteLocalDeck);
visualDeckStorageWidget = new VisualDeckStorageWidget(this);
connect(visualDeckStorageWidget, &VisualDeckStorageWidget::imageDoubleClicked, this,
&TabDeckStorageVisual::actOpenLocalDeck);
// layout->addWidget(leftToolBar);
layout->addWidget(visualDeckStorageWidget);
retranslateUi();
}
void TabDeckStorageVisual::closeRequest()
{
this->close();
}
void TabDeckStorageVisual::retranslateUi()
{
aOpenLocalDeck->setText(tr("Open in deck editor"));
aDeleteLocalDeck->setText(tr("Delete"));
}
QString TabDeckStorageVisual::getTargetPath() const
{
return {};
}
void TabDeckStorageVisual::actOpenLocalDeck(QMouseEvent *event, DeckPreviewCardPictureWidget *instance)
{
(void)event;
DeckLoader deckLoader;
if (!deckLoader.loadFromFile(instance->filePath, DeckLoader::CockatriceFormat))
return;
emit openDeckEditor(&deckLoader);
}
void TabDeckStorageVisual::actDeleteLocalDeck()
{
QModelIndex curLeft = localDirView->selectionModel()->currentIndex();
if (localDirModel->isDir(curLeft))
return;
if (QMessageBox::warning(this, tr("Delete local file"),
tr("Are you sure you want to delete \"%1\"?").arg(localDirModel->fileName(curLeft)),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
localDirModel->remove(curLeft);
}
void TabDeckStorageVisual::cardUpdateFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "Card update process finished with exit code:" << exitCode << "and exit status:" << exitStatus;
}

View file

@ -0,0 +1,56 @@
#ifndef TAB_DECK_STORAGE_VISUAL_H
#define TAB_DECK_STORAGE_VISUAL_H
#include "../../../deck/deck_list_model.h"
#include "../../../deck/deck_view.h"
#include "../../ui/widgets/cards/deck_preview_card_picture_widget.h"
#include "../../ui/widgets/visual_deck_storage/visual_deck_storage_widget.h"
#include "../tab.h"
#include <QProcess>
class AbstractClient;
class QTreeView;
class QFileSystemModel;
class QToolBar;
class QTreeWidget;
class QTreeWidgetItem;
class QGroupBox;
class CommandContainer;
class Response;
class DeckLoader;
class TabDeckStorageVisual final : public Tab
{
Q_OBJECT
public:
TabDeckStorageVisual(TabSupervisor *_tabSupervisor, AbstractClient *_client);
void retranslateUi() override;
QString getTabText() const override
{
return tr("Visual Deck storage");
}
public slots:
void cardUpdateFinished(int exitCode, QProcess::ExitStatus exitStatus);
void closeRequest() override;
void actOpenLocalDeck(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
void actDeleteLocalDeck();
signals:
void openDeckEditor(const DeckLoader *deckLoader);
private:
QWidget *container;
QHBoxLayout *layout;
AbstractClient *client;
QTreeView *localDirView;
QFileSystemModel *localDirModel;
QToolBar *leftToolBar;
QGroupBox *leftGroupBox;
VisualDeckStorageWidget *visualDeckStorageWidget;
DeckListModel *deck_list_model;
QAction *aOpenLocalDeck, *aDeleteLocalDeck;
QString getTargetPath() const;
};
#endif