[DeckLoader] Refactor to make some methods static (#6336)

* Make forEachCard const

* Make some DeckLoader methods static

* Update usages

* Update method param documentation in deck_loader.cpp

---------

Co-authored-by: BruebachL <44814898+BruebachL@users.noreply.github.com>
This commit is contained in:
RickyRister 2025-11-17 03:49:45 -08:00 committed by GitHub
parent a8ee0d7648
commit 16392c28c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 80 additions and 65 deletions

View file

@ -48,7 +48,7 @@ public:
} }
loader->getDeckList()->loadFromStream_Plain(outStream, false); loader->getDeckList()->loadFromStream_Plain(outStream, false);
loader->resolveSetNameAndNumberToProviderID(); DeckLoader::resolveSetNameAndNumberToProviderID(loader->getDeckList());
return loader; return loader;
} }
@ -95,7 +95,7 @@ public:
} }
loader->getDeckList()->loadFromStream_Plain(outStream, false); loader->getDeckList()->loadFromStream_Plain(outStream, false);
loader->resolveSetNameAndNumberToProviderID(); DeckLoader::resolveSetNameAndNumberToProviderID(loader->getDeckList());
QJsonObject commandersObj = obj.value("commanders").toObject(); QJsonObject commandersObj = obj.value("commanders").toObject();
if (!commandersObj.isEmpty()) { if (!commandersObj.isEmpty()) {

View file

@ -276,9 +276,11 @@ static QString toDecklistExportString(const DecklistCardNode *card)
/** /**
* Export deck to decklist function, called to format the deck in a way to be sent to a server * Export deck to decklist function, called to format the deck in a way to be sent to a server
*
* @param deckList The decklist to export
* @param website The website we're sending the deck to * @param website The website we're sending the deck to
*/ */
QString DeckLoader::exportDeckToDecklist(DecklistWebsite website) QString DeckLoader::exportDeckToDecklist(const DeckList *deckList, DecklistWebsite website)
{ {
// Add the base url // Add the base url
QString deckString = "https://" + getDomainForWebsite(website) + "/?"; QString deckString = "https://" + getDomainForWebsite(website) + "/?";
@ -345,8 +347,10 @@ struct SetProviderIdToPreferred
/** /**
* This function iterates through each card in the decklist and sets the providerId * This function iterates through each card in the decklist and sets the providerId
* on each card based on its set name and collector number. * on each card based on its set name and collector number.
*
* @param deckList The decklist to modify
*/ */
void DeckLoader::setProviderIdToPreferredPrinting() void DeckLoader::setProviderIdToPreferredPrinting(const DeckList *deckList)
{ {
// Set up the struct to call. // Set up the struct to call.
SetProviderIdToPreferred setProviderIdToPreferred; SetProviderIdToPreferred setProviderIdToPreferred;
@ -357,8 +361,10 @@ void DeckLoader::setProviderIdToPreferredPrinting()
/** /**
* Sets the providerId on each card in the decklist based on its set name and collector number. * Sets the providerId on each card in the decklist based on its set name and collector number.
*
* @param deckList The decklist to modify
*/ */
void DeckLoader::resolveSetNameAndNumberToProviderID() void DeckLoader::resolveSetNameAndNumberToProviderID(const DeckList *deckList)
{ {
auto setProviderId = [](const auto node, const auto card) { auto setProviderId = [](const auto node, const auto card) {
Q_UNUSED(node); Q_UNUSED(node);
@ -397,8 +403,10 @@ struct ClearSetNameNumberAndProviderId
/** /**
* Clears the set name and numbers on each card in the decklist. * Clears the set name and numbers on each card in the decklist.
*
* @param deckList The decklist to modify
*/ */
void DeckLoader::clearSetNamesAndNumbers() void DeckLoader::clearSetNamesAndNumbers(const DeckList *deckList)
{ {
auto clearSetNameAndNumber = [](const auto node, auto card) { auto clearSetNameAndNumber = [](const auto node, auto card) {
Q_UNUSED(node) Q_UNUSED(node)
@ -419,19 +427,22 @@ DeckLoader::FileFormat DeckLoader::getFormatFromName(const QString &fileName)
return PlainTextFormat; return PlainTextFormat;
} }
void DeckLoader::saveToClipboard(bool addComments, bool addSetNameAndNumber) const void DeckLoader::saveToClipboard(const DeckList *deckList, bool addComments, bool addSetNameAndNumber)
{ {
QString buffer; QString buffer;
QTextStream stream(&buffer); QTextStream stream(&buffer);
saveToStream_Plain(stream, addComments, addSetNameAndNumber); saveToStream_Plain(stream, deckList, addComments, addSetNameAndNumber);
QApplication::clipboard()->setText(buffer, QClipboard::Clipboard); QApplication::clipboard()->setText(buffer, QClipboard::Clipboard);
QApplication::clipboard()->setText(buffer, QClipboard::Selection); QApplication::clipboard()->setText(buffer, QClipboard::Selection);
} }
bool DeckLoader::saveToStream_Plain(QTextStream &out, bool addComments, bool addSetNameAndNumber) const bool DeckLoader::saveToStream_Plain(QTextStream &out,
const DeckList *deckList,
bool addComments,
bool addSetNameAndNumber)
{ {
if (addComments) { if (addComments) {
saveToStream_DeckHeader(out); saveToStream_DeckHeader(out, deckList);
} }
// loop zones // loop zones
@ -447,7 +458,7 @@ bool DeckLoader::saveToStream_Plain(QTextStream &out, bool addComments, bool add
return true; return true;
} }
void DeckLoader::saveToStream_DeckHeader(QTextStream &out) const void DeckLoader::saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList)
{ {
if (!deckList->getName().isEmpty()) { if (!deckList->getName().isEmpty()) {
out << "// " << deckList->getName() << "\n\n"; out << "// " << deckList->getName() << "\n\n";
@ -465,7 +476,7 @@ void DeckLoader::saveToStream_DeckHeader(QTextStream &out) const
void DeckLoader::saveToStream_DeckZone(QTextStream &out, void DeckLoader::saveToStream_DeckZone(QTextStream &out,
const InnerDecklistNode *zoneNode, const InnerDecklistNode *zoneNode,
bool addComments, bool addComments,
bool addSetNameAndNumber) const bool addSetNameAndNumber)
{ {
// group cards by card type and count the subtotals // group cards by card type and count the subtotals
QMultiMap<QString, DecklistCardNode *> cardsByType; QMultiMap<QString, DecklistCardNode *> cardsByType;
@ -513,7 +524,7 @@ void DeckLoader::saveToStream_DeckZoneCards(QTextStream &out,
const InnerDecklistNode *zoneNode, const InnerDecklistNode *zoneNode,
QList<DecklistCardNode *> cards, QList<DecklistCardNode *> cards,
bool addComments, bool addComments,
bool addSetNameAndNumber) const bool addSetNameAndNumber)
{ {
// QMultiMap sorts values in reverse order // QMultiMap sorts values in reverse order
for (int i = cards.size() - 1; i >= 0; --i) { for (int i = cards.size() - 1; i >= 0; --i) {
@ -589,7 +600,7 @@ bool DeckLoader::convertToCockatriceFormat(QString fileName)
return result; return result;
} }
QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneName) QString DeckLoader::getCardZoneFromName(const QString &cardName, QString currentZoneName)
{ {
CardInfoPtr card = CardDatabaseManager::query()->getCardInfo(cardName); CardInfoPtr card = CardDatabaseManager::query()->getCardInfo(cardName);
@ -600,7 +611,7 @@ QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneNam
return currentZoneName; return currentZoneName;
} }
QString DeckLoader::getCompleteCardName(const QString &cardName) const QString DeckLoader::getCompleteCardName(const QString &cardName)
{ {
if (CardDatabaseManager::getInstance()) { if (CardDatabaseManager::getInstance()) {
ExactCard temp = CardDatabaseManager::query()->guessCard({cardName}); ExactCard temp = CardDatabaseManager::query()->guessCard({cardName});
@ -672,7 +683,7 @@ void DeckLoader::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node)
cursor->movePosition(QTextCursor::End); cursor->movePosition(QTextCursor::End);
} }
void DeckLoader::printDeckList(QPrinter *printer) void DeckLoader::printDeckList(QPrinter *printer, const DeckList *deckList)
{ {
QTextDocument doc; QTextDocument doc;

View file

@ -21,13 +21,6 @@ signals:
void deckLoaded(); void deckLoaded();
void loadFinished(bool success); void loadFinished(bool success);
public slots:
/**
* @brief Prints the decklist to the provided QPrinter.
* @param printer The printer to render the decklist to.
*/
void printDeckList(QPrinter *printer);
public: public:
enum FileFormat enum FileFormat
{ {
@ -84,7 +77,7 @@ public:
return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1; return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1;
} }
void clearSetNamesAndNumbers(); static void clearSetNamesAndNumbers(const DeckList *deckList);
static FileFormat getFormatFromName(const QString &fileName); static FileFormat getFormatFromName(const QString &fileName);
bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false); bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false);
@ -92,15 +85,25 @@ public:
bool loadFromRemote(const QString &nativeString, int remoteDeckId); bool loadFromRemote(const QString &nativeString, int remoteDeckId);
bool saveToFile(const QString &fileName, FileFormat fmt); bool saveToFile(const QString &fileName, FileFormat fmt);
bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt); bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt);
QString exportDeckToDecklist(DecklistWebsite website);
void setProviderIdToPreferredPrinting();
void resolveSetNameAndNumberToProviderID(); static QString exportDeckToDecklist(const DeckList *deckList, DecklistWebsite website);
void saveToClipboard(bool addComments = true, bool addSetNameAndNumber = true) const; static void setProviderIdToPreferredPrinting(const DeckList *deckList);
static void resolveSetNameAndNumberToProviderID(const DeckList *deckList);
static void saveToClipboard(const DeckList *deckList, bool addComments = true, bool addSetNameAndNumber = true);
static bool saveToStream_Plain(QTextStream &out,
const DeckList *deckList,
bool addComments = true,
bool addSetNameAndNumber = true);
/**
* @brief Prints the decklist to the provided QPrinter.
* @param printer The printer to render the decklist to.
* @param deckList
*/
static void printDeckList(QPrinter *printer, const DeckList *deckList);
// overload
bool saveToStream_Plain(QTextStream &out, bool addComments = true, bool addSetNameAndNumber = true) const;
bool convertToCockatriceFormat(QString fileName); bool convertToCockatriceFormat(QString fileName);
DeckList *getDeckList() DeckList *getDeckList()
@ -109,21 +112,21 @@ public:
} }
private: private:
void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); static void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node);
static void saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList);
protected: static void saveToStream_DeckZone(QTextStream &out,
void saveToStream_DeckHeader(QTextStream &out) const; const InnerDecklistNode *zoneNode,
void saveToStream_DeckZone(QTextStream &out, bool addComments = true,
const InnerDecklistNode *zoneNode, bool addSetNameAndNumber = true);
bool addComments = true, static void saveToStream_DeckZoneCards(QTextStream &out,
bool addSetNameAndNumber = true) const; const InnerDecklistNode *zoneNode,
void saveToStream_DeckZoneCards(QTextStream &out, QList<DecklistCardNode *> cards,
const InnerDecklistNode *zoneNode, bool addComments = true,
QList<DecklistCardNode *> cards, bool addSetNameAndNumber = true);
bool addComments = true,
bool addSetNameAndNumber = true) const; [[nodiscard]] static QString getCardZoneFromName(const QString &cardName, QString currentZoneName);
[[nodiscard]] QString getCardZoneFromName(QString cardName, QString currentZoneName); [[nodiscard]] static QString getCompleteCardName(const QString &cardName);
[[nodiscard]] QString getCompleteCardName(const QString &cardName) const;
}; };
#endif #endif

View file

@ -82,9 +82,9 @@ bool AbstractDlgDeckTextEdit::loadIntoDeck(DeckLoader *deckLoader) const
if (deckLoader->getDeckList()->loadFromStream_Plain(stream, true)) { if (deckLoader->getDeckList()->loadFromStream_Plain(stream, true)) {
if (loadSetNameAndNumberCheckBox->isChecked()) { if (loadSetNameAndNumberCheckBox->isChecked()) {
deckLoader->resolveSetNameAndNumberToProviderID(); DeckLoader::resolveSetNameAndNumberToProviderID(deckLoader->getDeckList());
} else { } else {
deckLoader->clearSetNamesAndNumbers(); DeckLoader::clearSetNamesAndNumbers(deckLoader->getDeckList());
} }
return true; return true;
} }
@ -154,17 +154,17 @@ DlgEditDeckInClipboard::DlgEditDeckInClipboard(const DeckLoader &deckList, bool
* @param addComments Whether to add annotations * @param addComments Whether to add annotations
* @return A QString * @return A QString
*/ */
static QString deckListToString(const DeckLoader *deckList, bool addComments) static QString deckListToString(const DeckList *deckList, bool addComments)
{ {
QString buffer; QString buffer;
QTextStream stream(&buffer); QTextStream stream(&buffer);
deckList->saveToStream_Plain(stream, addComments); DeckLoader::saveToStream_Plain(stream, deckList, addComments);
return buffer; return buffer;
} }
void DlgEditDeckInClipboard::actRefresh() void DlgEditDeckInClipboard::actRefresh()
{ {
setText(deckListToString(deckLoader, annotated)); setText(deckListToString(deckLoader->getDeckList(), annotated));
} }
void DlgEditDeckInClipboard::actOK() void DlgEditDeckInClipboard::actOK()

View file

@ -98,7 +98,7 @@ void DlgLoadDeckFromWebsite::accept()
DeckLoader *loader = new DeckLoader(this); DeckLoader *loader = new DeckLoader(this);
QTextStream stream(&deckText); QTextStream stream(&deckText);
loader->getDeckList()->loadFromStream_Plain(stream, false); loader->getDeckList()->loadFromStream_Plain(stream, false);
loader->resolveSetNameAndNumberToProviderID(); DeckLoader::resolveSetNameAndNumberToProviderID(loader->getDeckList());
deck = loader; deck = loader;
QDialog::accept(); QDialog::accept();

View file

@ -162,14 +162,14 @@ void DlgSelectSetForCards::actOK()
void DlgSelectSetForCards::actClear() void DlgSelectSetForCards::actClear()
{ {
qobject_cast<DeckLoader *>(model->getDeckList())->clearSetNamesAndNumbers(); DeckLoader::clearSetNamesAndNumbers(model->getDeckList());
accept(); accept();
} }
void DlgSelectSetForCards::actSetAllToPreferred() void DlgSelectSetForCards::actSetAllToPreferred()
{ {
qobject_cast<DeckLoader *>(model->getDeckList())->clearSetNamesAndNumbers(); DeckLoader::clearSetNamesAndNumbers(model->getDeckList());
qobject_cast<DeckLoader *>(model->getDeckList())->setProviderIdToPreferredPrinting(); DeckLoader::setProviderIdToPreferredPrinting(model->getDeckList());
accept(); accept();
} }

View file

@ -510,32 +510,33 @@ void AbstractTabDeckEditor::actEditDeckInClipboardRaw()
/** @brief Saves deck to clipboard with set info and annotation. */ /** @brief Saves deck to clipboard with set info and annotation. */
void AbstractTabDeckEditor::actSaveDeckToClipboard() void AbstractTabDeckEditor::actSaveDeckToClipboard()
{ {
getDeckLoader()->saveToClipboard(true, true); DeckLoader::saveToClipboard(getDeckList(), true, true);
} }
/** @brief Saves deck to clipboard with annotation, without set info. */ /** @brief Saves deck to clipboard with annotation, without set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardNoSetInfo() void AbstractTabDeckEditor::actSaveDeckToClipboardNoSetInfo()
{ {
getDeckLoader()->saveToClipboard(true, false); DeckLoader::saveToClipboard(getDeckList(), true, false);
} }
/** @brief Saves deck to clipboard without annotations, with set info. */ /** @brief Saves deck to clipboard without annotations, with set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardRaw() void AbstractTabDeckEditor::actSaveDeckToClipboardRaw()
{ {
getDeckLoader()->saveToClipboard(false, true); DeckLoader::saveToClipboard(getDeckList(), false, true);
} }
/** @brief Saves deck to clipboard without annotations or set info. */ /** @brief Saves deck to clipboard without annotations or set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardRawNoSetInfo() void AbstractTabDeckEditor::actSaveDeckToClipboardRawNoSetInfo()
{ {
getDeckLoader()->saveToClipboard(false, false); DeckLoader::saveToClipboard(getDeckList(), false, false);
} }
/** @brief Prints the deck using a QPrintPreviewDialog. */ /** @brief Prints the deck using a QPrintPreviewDialog. */
void AbstractTabDeckEditor::actPrintDeck() void AbstractTabDeckEditor::actPrintDeck()
{ {
auto *dlg = new QPrintPreviewDialog(this); auto *dlg = new QPrintPreviewDialog(this);
connect(dlg, &QPrintPreviewDialog::paintRequested, deckDockWidget->getDeckLoader(), &DeckLoader::printDeckList); connect(dlg, &QPrintPreviewDialog::paintRequested, this,
[this](QPrinter *printer) { DeckLoader::printDeckList(printer, getDeckList()); });
dlg->exec(); dlg->exec();
} }
@ -569,7 +570,7 @@ void AbstractTabDeckEditor::actLoadDeckFromWebsite()
void AbstractTabDeckEditor::exportToDecklistWebsite(DeckLoader::DecklistWebsite website) void AbstractTabDeckEditor::exportToDecklistWebsite(DeckLoader::DecklistWebsite website)
{ {
if (DeckLoader *const deck = getDeckLoader()) { if (DeckLoader *const deck = getDeckLoader()) {
QString decklistUrlString = deck->exportDeckToDecklist(website); QString decklistUrlString = deck->exportDeckToDecklist(getDeckList(), website);
// Check to make sure the string isn't empty. // Check to make sure the string isn't empty.
if (decklistUrlString.isEmpty()) { if (decklistUrlString.isEmpty()) {
// Show an error if the deck is empty, and return. // Show an error if the deck is empty, and return.

View file

@ -326,13 +326,13 @@ QMenu *DeckPreviewWidget::createRightClickMenu()
auto saveToClipboardMenu = menu->addMenu(tr("Save Deck to Clipboard")); auto saveToClipboardMenu = menu->addMenu(tr("Save Deck to Clipboard"));
connect(saveToClipboardMenu->addAction(tr("Annotated")), &QAction::triggered, this, connect(saveToClipboardMenu->addAction(tr("Annotated")), &QAction::triggered, this,
[this] { deckLoader->saveToClipboard(true, true); }); [this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), true, true); });
connect(saveToClipboardMenu->addAction(tr("Annotated (No set info)")), &QAction::triggered, this, connect(saveToClipboardMenu->addAction(tr("Annotated (No set info)")), &QAction::triggered, this,
[this] { deckLoader->saveToClipboard(true, false); }); [this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), true, false); });
connect(saveToClipboardMenu->addAction(tr("Not Annotated")), &QAction::triggered, this, connect(saveToClipboardMenu->addAction(tr("Not Annotated")), &QAction::triggered, this,
[this] { deckLoader->saveToClipboard(false, true); }); [this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), false, true); });
connect(saveToClipboardMenu->addAction(tr("Not Annotated (No set info)")), &QAction::triggered, this, connect(saveToClipboardMenu->addAction(tr("Not Annotated (No set info)")), &QAction::triggered, this,
[this] { deckLoader->saveToClipboard(false, false); }); [this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), false, false); });
menu->addSeparator(); menu->addSeparator();

View file

@ -722,7 +722,7 @@ void DeckList::refreshDeckHash()
/** /**
* Calls a given function on each card in the deck. * Calls a given function on each card in the deck.
*/ */
void DeckList::forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func) void DeckList::forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func) const
{ {
// Support for this is only possible if the internal structure // Support for this is only possible if the internal structure
// doesn't get more complicated. // doesn't get more complicated.

View file

@ -315,7 +315,7 @@ public:
* *
* @param func Function taking (zone node, card node). * @param func Function taking (zone node, card node).
*/ */
void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func); void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func) const;
}; };
#endif #endif