[Refactor] Pass around LoadedDeck instead of DeckLoader (#6422)

This commit is contained in:
RickyRister 2025-12-20 04:39:00 -08:00 committed by GitHub
parent 367507e054
commit d6db21419c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 253 additions and 264 deletions

View file

@ -66,26 +66,26 @@ void AbstractDlgDeckTextEdit::setText(const QString &text)
}
/**
* Tries to load the current contents of the contentsEdit into the DeckLoader
* Tries to load the current contents of the contentsEdit into the deckList
*
* @param deckLoader The DeckLoader to load the deck into
* @param deckList The deckList to load the deck into
* @return Whether the loading was successful
*/
bool AbstractDlgDeckTextEdit::loadIntoDeck(DeckLoader *deckLoader) const
bool AbstractDlgDeckTextEdit::loadIntoDeck(DeckList &deckList) const
{
QString buffer = contentsEdit->toPlainText();
if (buffer.contains("<cockatrice_deck version=\"1\">")) {
return deckLoader->getDeckList()->loadFromString_Native(buffer);
return deckList.loadFromString_Native(buffer);
}
QTextStream stream(&buffer);
if (deckLoader->getDeckList()->loadFromStream_Plain(stream, true)) {
if (deckList.loadFromStream_Plain(stream, true)) {
if (loadSetNameAndNumberCheckBox->isChecked()) {
deckLoader->getDeckList()->forEachCard(CardNodeFunction::ResolveProviderId());
deckList.forEachCard(CardNodeFunction::ResolveProviderId());
} else {
deckLoader->getDeckList()->forEachCard(CardNodeFunction::ClearPrintingData());
deckList.forEachCard(CardNodeFunction::ClearPrintingData());
}
return true;
}
@ -108,7 +108,7 @@ void AbstractDlgDeckTextEdit::keyPressEvent(QKeyEvent *event)
*
* @param parent The parent widget
*/
DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent) : AbstractDlgDeckTextEdit(parent), deckList(nullptr)
DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent) : AbstractDlgDeckTextEdit(parent)
{
setWindowTitle(tr("Load deck from clipboard"));
@ -122,8 +122,6 @@ void DlgLoadDeckFromClipboard::actRefresh()
void DlgLoadDeckFromClipboard::actOK()
{
deckList = new DeckLoader(this);
if (loadIntoDeck(deckList)) {
accept();
} else {
@ -134,18 +132,15 @@ void DlgLoadDeckFromClipboard::actOK()
/**
* Creates the dialog window for the "Edit deck in clipboard" action
*
* @param _deckLoader The existing deck in the deck editor. Copies the instance
* @param _deckList The existing deck in the deck editor.
* @param _annotated Whether to add annotations to the text that is loaded from the deck
* @param parent The parent widget
*/
DlgEditDeckInClipboard::DlgEditDeckInClipboard(DeckLoader *_deckLoader, bool _annotated, QWidget *parent)
: AbstractDlgDeckTextEdit(parent), annotated(_annotated)
DlgEditDeckInClipboard::DlgEditDeckInClipboard(const DeckList &_deckList, bool _annotated, QWidget *parent)
: AbstractDlgDeckTextEdit(parent), deckList(_deckList), annotated(_annotated)
{
setWindowTitle(tr("Edit deck in clipboard"));
deckLoader = new DeckLoader(this, _deckLoader->getDeckList());
deckLoader->setParent(this);
DlgEditDeckInClipboard::actRefresh();
}
@ -165,12 +160,12 @@ static QString deckListToString(const DeckList *deckList, bool addComments)
void DlgEditDeckInClipboard::actRefresh()
{
setText(deckListToString(deckLoader->getDeckList(), annotated));
setText(deckListToString(&deckList, annotated));
}
void DlgEditDeckInClipboard::actOK()
{
if (loadIntoDeck(deckLoader)) {
if (loadIntoDeck(deckList)) {
accept();
} else {
QMessageBox::critical(this, tr("Error"), tr("Invalid deck list."));

View file

@ -8,10 +8,11 @@
#ifndef DLG_LOAD_DECK_FROM_CLIPBOARD_H
#define DLG_LOAD_DECK_FROM_CLIPBOARD_H
#include "../../deck_loader/loaded_deck.h"
#include <QCheckBox>
#include <QDialog>
class DeckLoader;
class QPlainTextEdit;
class QPushButton;
@ -35,15 +36,13 @@ public:
/**
* Gets the loaded deck. Only call this method after this dialog window has been successfully exec'd.
*
* The returned DeckLoader is parented to this object; make sure to take ownership of the DeckLoader if you intend
* to use it, since otherwise it will get destroyed once this dlg is destroyed
* @return The DeckLoader
* @return The loaded decklist
*/
[[nodiscard]] virtual DeckLoader *getDeckList() const = 0;
[[nodiscard]] virtual const DeckList &getDeckList() = 0;
protected:
void setText(const QString &text);
bool loadIntoDeck(DeckLoader *deckLoader) const;
bool loadIntoDeck(DeckList &deckList) const;
void keyPressEvent(QKeyEvent *event) override;
protected slots:
@ -62,12 +61,12 @@ protected slots:
void actRefresh() override;
private:
DeckLoader *deckList;
DeckList deckList;
public:
explicit DlgLoadDeckFromClipboard(QWidget *parent = nullptr);
[[nodiscard]] DeckLoader *getDeckList() const override
[[nodiscard]] const DeckList &getDeckList() override
{
return deckList;
}
@ -84,15 +83,15 @@ protected slots:
void actRefresh() override;
private:
DeckLoader *deckLoader;
DeckList deckList;
bool annotated;
public:
explicit DlgEditDeckInClipboard(DeckLoader *_deckLoader, bool _annotated, QWidget *parent = nullptr);
explicit DlgEditDeckInClipboard(const DeckList &_deckList, bool _annotated, QWidget *parent = nullptr);
[[nodiscard]] DeckLoader *getDeckList() const override
[[nodiscard]] const DeckList &getDeckList() override
{
return deckLoader;
return deckList;
}
};

View file

@ -97,11 +97,11 @@ void DlgLoadDeckFromWebsite::accept()
}
// Parse the plain text deck here
DeckLoader *loader = new DeckLoader(this);
DeckList deckList;
QTextStream stream(&deckText);
loader->getDeckList()->loadFromStream_Plain(stream, false);
loader->getDeckList()->forEachCard(CardNodeFunction::ResolveProviderId());
deck = loader;
deckList.loadFromStream_Plain(stream, false);
deckList.forEachCard(CardNodeFunction::ResolveProviderId());
deck = deckList;
QDialog::accept();
return;

View file

@ -26,9 +26,9 @@ public:
explicit DlgLoadDeckFromWebsite(QWidget *parent);
void retranslateUi();
bool testValidUrl();
DeckLoader *deck;
DeckList deck;
DeckLoader *getDeck()
const DeckList &getDeck() const
{
return deck;
}