[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
10 changed files with 80 additions and 65 deletions

View File

@@ -48,7 +48,7 @@ public:
}
loader->getDeckList()->loadFromStream_Plain(outStream, false);
loader->resolveSetNameAndNumberToProviderID();
DeckLoader::resolveSetNameAndNumberToProviderID(loader->getDeckList());
return loader;
}
@@ -95,7 +95,7 @@ public:
}
loader->getDeckList()->loadFromStream_Plain(outStream, false);
loader->resolveSetNameAndNumberToProviderID();
DeckLoader::resolveSetNameAndNumberToProviderID(loader->getDeckList());
QJsonObject commandersObj = obj.value("commanders").toObject();
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
*
* @param deckList The decklist to export
* @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
QString deckString = "https://" + getDomainForWebsite(website) + "/?";
@@ -345,8 +347,10 @@ struct SetProviderIdToPreferred
/**
* This function iterates through each card in the decklist and sets the providerId
* 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.
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.
*
* @param deckList The decklist to modify
*/
void DeckLoader::resolveSetNameAndNumberToProviderID()
void DeckLoader::resolveSetNameAndNumberToProviderID(const DeckList *deckList)
{
auto setProviderId = [](const auto node, const auto card) {
Q_UNUSED(node);
@@ -397,8 +403,10 @@ struct ClearSetNameNumberAndProviderId
/**
* 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) {
Q_UNUSED(node)
@@ -419,19 +427,22 @@ DeckLoader::FileFormat DeckLoader::getFormatFromName(const QString &fileName)
return PlainTextFormat;
}
void DeckLoader::saveToClipboard(bool addComments, bool addSetNameAndNumber) const
void DeckLoader::saveToClipboard(const DeckList *deckList, bool addComments, bool addSetNameAndNumber)
{
QString 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::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) {
saveToStream_DeckHeader(out);
saveToStream_DeckHeader(out, deckList);
}
// loop zones
@@ -447,7 +458,7 @@ bool DeckLoader::saveToStream_Plain(QTextStream &out, bool addComments, bool add
return true;
}
void DeckLoader::saveToStream_DeckHeader(QTextStream &out) const
void DeckLoader::saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList)
{
if (!deckList->getName().isEmpty()) {
out << "// " << deckList->getName() << "\n\n";
@@ -465,7 +476,7 @@ void DeckLoader::saveToStream_DeckHeader(QTextStream &out) const
void DeckLoader::saveToStream_DeckZone(QTextStream &out,
const InnerDecklistNode *zoneNode,
bool addComments,
bool addSetNameAndNumber) const
bool addSetNameAndNumber)
{
// group cards by card type and count the subtotals
QMultiMap<QString, DecklistCardNode *> cardsByType;
@@ -513,7 +524,7 @@ void DeckLoader::saveToStream_DeckZoneCards(QTextStream &out,
const InnerDecklistNode *zoneNode,
QList<DecklistCardNode *> cards,
bool addComments,
bool addSetNameAndNumber) const
bool addSetNameAndNumber)
{
// QMultiMap sorts values in reverse order
for (int i = cards.size() - 1; i >= 0; --i) {
@@ -589,7 +600,7 @@ bool DeckLoader::convertToCockatriceFormat(QString fileName)
return result;
}
QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneName)
QString DeckLoader::getCardZoneFromName(const QString &cardName, QString currentZoneName)
{
CardInfoPtr card = CardDatabaseManager::query()->getCardInfo(cardName);
@@ -600,7 +611,7 @@ QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneNam
return currentZoneName;
}
QString DeckLoader::getCompleteCardName(const QString &cardName) const
QString DeckLoader::getCompleteCardName(const QString &cardName)
{
if (CardDatabaseManager::getInstance()) {
ExactCard temp = CardDatabaseManager::query()->guessCard({cardName});
@@ -672,7 +683,7 @@ void DeckLoader::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node)
cursor->movePosition(QTextCursor::End);
}
void DeckLoader::printDeckList(QPrinter *printer)
void DeckLoader::printDeckList(QPrinter *printer, const DeckList *deckList)
{
QTextDocument doc;

View File

@@ -21,13 +21,6 @@ signals:
void deckLoaded();
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:
enum FileFormat
{
@@ -84,7 +77,7 @@ public:
return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1;
}
void clearSetNamesAndNumbers();
static void clearSetNamesAndNumbers(const DeckList *deckList);
static FileFormat getFormatFromName(const QString &fileName);
bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false);
@@ -92,15 +85,25 @@ public:
bool loadFromRemote(const QString &nativeString, int remoteDeckId);
bool saveToFile(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);
DeckList *getDeckList()
@@ -109,21 +112,21 @@ public:
}
private:
void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node);
static void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node);
static void saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList);
protected:
void saveToStream_DeckHeader(QTextStream &out) const;
void saveToStream_DeckZone(QTextStream &out,
const InnerDecklistNode *zoneNode,
bool addComments = true,
bool addSetNameAndNumber = true) const;
void saveToStream_DeckZoneCards(QTextStream &out,
const InnerDecklistNode *zoneNode,
QList<DecklistCardNode *> cards,
bool addComments = true,
bool addSetNameAndNumber = true) const;
[[nodiscard]] QString getCardZoneFromName(QString cardName, QString currentZoneName);
[[nodiscard]] QString getCompleteCardName(const QString &cardName) const;
static void saveToStream_DeckZone(QTextStream &out,
const InnerDecklistNode *zoneNode,
bool addComments = true,
bool addSetNameAndNumber = true);
static void saveToStream_DeckZoneCards(QTextStream &out,
const InnerDecklistNode *zoneNode,
QList<DecklistCardNode *> cards,
bool addComments = true,
bool addSetNameAndNumber = true);
[[nodiscard]] static QString getCardZoneFromName(const QString &cardName, QString currentZoneName);
[[nodiscard]] static QString getCompleteCardName(const QString &cardName);
};
#endif

View File

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

View File

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

View File

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

View File

@@ -510,32 +510,33 @@ void AbstractTabDeckEditor::actEditDeckInClipboardRaw()
/** @brief Saves deck to clipboard with set info and annotation. */
void AbstractTabDeckEditor::actSaveDeckToClipboard()
{
getDeckLoader()->saveToClipboard(true, true);
DeckLoader::saveToClipboard(getDeckList(), true, true);
}
/** @brief Saves deck to clipboard with annotation, without set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardNoSetInfo()
{
getDeckLoader()->saveToClipboard(true, false);
DeckLoader::saveToClipboard(getDeckList(), true, false);
}
/** @brief Saves deck to clipboard without annotations, with set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardRaw()
{
getDeckLoader()->saveToClipboard(false, true);
DeckLoader::saveToClipboard(getDeckList(), false, true);
}
/** @brief Saves deck to clipboard without annotations or set info. */
void AbstractTabDeckEditor::actSaveDeckToClipboardRawNoSetInfo()
{
getDeckLoader()->saveToClipboard(false, false);
DeckLoader::saveToClipboard(getDeckList(), false, false);
}
/** @brief Prints the deck using a QPrintPreviewDialog. */
void AbstractTabDeckEditor::actPrintDeck()
{
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();
}
@@ -569,7 +570,7 @@ void AbstractTabDeckEditor::actLoadDeckFromWebsite()
void AbstractTabDeckEditor::exportToDecklistWebsite(DeckLoader::DecklistWebsite website)
{
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.
if (decklistUrlString.isEmpty()) {
// 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"));
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,
[this] { deckLoader->saveToClipboard(true, false); });
[this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), true, false); });
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,
[this] { deckLoader->saveToClipboard(false, false); });
[this] { DeckLoader::saveToClipboard(deckLoader->getDeckList(), false, false); });
menu->addSeparator();

View File

@@ -722,7 +722,7 @@ void DeckList::refreshDeckHash()
/**
* 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
// doesn't get more complicated.

View File

@@ -315,7 +315,7 @@ public:
*
* @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