[DeckLoader] Make save/load methods static (#6476)

* const

* [DeckLoader] make methods static

* use static methods

* add docs

* add docs
This commit is contained in:
RickyRister 2026-01-16 10:20:36 -08:00 committed by GitHub
parent c7c7bf550a
commit d579c82cb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 209 additions and 166 deletions

View file

@ -78,10 +78,9 @@ void HomeWidget::initializeBackgroundFromSource()
void HomeWidget::loadBackgroundSourceDeck()
{
DeckLoader deckLoader = DeckLoader(this);
deckLoader.loadFromFile(SettingsCache::instance().getDeckPath() + "background.cod", DeckFileFormat::Cockatrice,
false);
backgroundSourceDeck = deckLoader.getDeck().deckList;
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(
SettingsCache::instance().getDeckPath() + "background.cod", DeckFileFormat::Cockatrice, false);
backgroundSourceDeck = deckOpt.has_value() ? deckOpt.value().deckList : DeckList();
}
void HomeWidget::updateRandomCard()

View file

@ -313,13 +313,13 @@ void AbstractTabDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLo
{
DeckFileFormat::Format fmt = DeckFileFormat::getFormatFromName(fileName);
auto l = DeckLoader(this);
if (l.loadFromFile(fileName, fmt, true)) {
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(fileName, fmt, true);
if (deckOpt) {
if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(l.getDeck());
emit openDeckEditor(deckOpt.value());
} else {
deckMenu->setSaveStatus(false);
openDeck(l.getDeck());
openDeck(deckOpt.value());
}
} else {
QMessageBox::critical(this, tr("Error"), tr("Could not open deck at %1").arg(fileName));
@ -355,9 +355,7 @@ bool AbstractTabDeckEditor::actSaveDeck()
if (loadedDeck.lastLoadInfo.fileName.isEmpty())
return actSaveDeckAs();
auto deckLoader = DeckLoader(this);
deckLoader.setDeck(loadedDeck);
if (deckLoader.saveToFile(loadedDeck.lastLoadInfo.fileName, loadedDeck.lastLoadInfo.fileFormat)) {
if (DeckLoader::saveToFile(loadedDeck)) {
deckStateManager->setModified(false);
return true;
}
@ -374,14 +372,14 @@ bool AbstractTabDeckEditor::actSaveDeck()
*/
bool AbstractTabDeckEditor::actSaveDeckAs()
{
LoadedDeck loadedDeck = deckStateManager->toLoadedDeck();
DeckList deckList = deckStateManager->getDeckList();
QFileDialog dialog(this, tr("Save deck"));
dialog.setDirectory(SettingsCache::instance().getDeckPath());
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDefaultSuffix("cod");
dialog.setNameFilters(DeckLoader::FILE_NAME_FILTERS);
dialog.selectFile(loadedDeck.deckList.getName().trimmed());
dialog.selectFile(deckList.getName().trimmed());
if (!dialog.exec())
return false;
@ -389,16 +387,15 @@ bool AbstractTabDeckEditor::actSaveDeckAs()
QString fileName = dialog.selectedFiles().at(0);
DeckFileFormat::Format fmt = DeckFileFormat::getFormatFromName(fileName);
DeckLoader deckLoader = DeckLoader(this);
deckLoader.setDeck(loadedDeck);
if (!deckLoader.saveToFile(fileName, fmt)) {
std::optional<LoadedDeck::LoadInfo> infoOpt = DeckLoader::saveToFile(deckList, fileName, fmt);
if (!infoOpt) {
QMessageBox::critical(
this, tr("Error"),
tr("The deck could not be saved.\nPlease check that the directory is writable and try again."));
return false;
}
deckStateManager->setLastLoadInfo({.fileName = fileName, .fileFormat = fmt});
deckStateManager->setLastLoadInfo(infoOpt.value());
deckStateManager->setModified(false);
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(fileName);

View file

@ -241,11 +241,11 @@ void TabDeckStorage::actOpenLocalDeck()
continue;
QString filePath = localDirModel->filePath(curLeft);
auto deckLoader = new DeckLoader(this);
if (!deckLoader->loadFromFile(filePath, DeckFileFormat::Cockatrice, true))
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(filePath, DeckFileFormat::Cockatrice, true);
if (!deckOpt)
continue;
emit openDeckEditor(deckLoader->getDeck());
emit openDeckEditor(deckOpt.value());
}
}
@ -307,13 +307,13 @@ void TabDeckStorage::uploadDeck(const QString &filePath, const QString &targetPa
QFile deckFile(filePath);
QFileInfo deckFileInfo(deckFile);
DeckLoader deckLoader(this);
if (!deckLoader.loadFromFile(filePath, DeckFileFormat::Cockatrice)) {
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(filePath, DeckFileFormat::Cockatrice, true);
if (!deckOpt) {
QMessageBox::critical(this, tr("Error"), tr("Invalid deck file"));
return;
}
DeckList deck = deckLoader.getDeck().deckList;
DeckList deck = deckOpt.value().deckList;
if (deck.getName().isEmpty()) {
bool ok;
@ -434,11 +434,11 @@ void TabDeckStorage::openRemoteDeckFinished(const Response &r, const CommandCont
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
const Command_DeckDownload &cmd = commandContainer.session_command(0).GetExtension(Command_DeckDownload::ext);
DeckLoader loader(this);
if (!loader.loadFromRemote(QString::fromStdString(resp.deck()), cmd.deck_id()))
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromRemote(QString::fromStdString(resp.deck()), cmd.deck_id());
if (!deckOpt)
return;
emit openDeckEditor(loader.getDeck());
emit openDeckEditor(deckOpt.value());
}
void TabDeckStorage::actDownload()
@ -496,10 +496,7 @@ void TabDeckStorage::downloadFinished(const Response &r,
DeckList deckList = DeckList(QString::fromStdString(resp.deck()));
DeckLoader deckLoader(this);
deckLoader.setDeck({deckList, {}});
deckLoader.saveToFile(filePath, DeckFileFormat::Cockatrice);
DeckLoader::saveToFile(deckList, filePath, DeckFileFormat::Cockatrice);
}
void TabDeckStorage::actNewFolder()

View file

@ -24,11 +24,12 @@ TabDeckStorageVisual::TabDeckStorageVisual(TabSupervisor *_tabSupervisor)
void TabDeckStorageVisual::actOpenLocalDeck(const QString &filePath)
{
auto deckLoader = DeckLoader(this);
if (!deckLoader.loadFromFile(filePath, DeckFileFormat::getFormatFromName(filePath), true)) {
std::optional<LoadedDeck> deckOpt =
DeckLoader::loadFromFile(filePath, DeckFileFormat::getFormatFromName(filePath), true);
if (!deckOpt) {
QMessageBox::critical(this, tr("Error"), tr("Could not open deck at %1").arg(filePath));
return;
}
emit openDeckEditor(deckLoader.getDeck());
emit openDeckEditor(deckOpt.value());
}

View file

@ -77,10 +77,10 @@ static QStringList findAllKnownTags()
QStringList allFiles = getAllFiles(SettingsCache::instance().getDeckPath());
QStringList knownTags;
auto loader = DeckLoader(nullptr);
for (const QString &file : allFiles) {
loader.loadFromFile(file, DeckFileFormat::getFormatFromName(file), false);
QStringList tags = loader.getDeck().deckList.getTags();
std::optional<LoadedDeck> deckOpt =
DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), false);
QStringList tags = deckOpt.has_value() ? deckOpt->deckList.getTags() : QStringList();
knownTags.append(tags);
knownTags.removeDuplicates();
}
@ -124,7 +124,7 @@ static bool confirmOverwriteIfExists(QWidget *parent, const QString &filePath)
static void convertFileToCockatriceFormat(DeckPreviewWidget *deckPreviewWidget)
{
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath);
DeckLoader::convertToCockatriceFormat(deckPreviewWidget->deckLoader->getDeck());
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getDeck().lastLoadInfo.fileName;
deckPreviewWidget->refreshBannerCardText();
}

View file

@ -288,7 +288,7 @@ void DeckPreviewWidget::setBannerCard(int /* changedIndex */)
auto [name, id] = bannerCardComboBox->currentData().value<QPair<QString, QString>>();
CardRef cardRef = {name, id};
deckLoader->getDeck().deckList.setBannerCard(cardRef);
deckLoader->saveToFile(filePath, DeckFileFormat::getFormatFromName(filePath));
DeckLoader::saveToFile(deckLoader->getDeck());
bannerCardDisplayWidget->setCard(CardDatabaseManager::query()->getCard(cardRef));
}
@ -311,7 +311,7 @@ void DeckPreviewWidget::imageDoubleClickedEvent(QMouseEvent *event, DeckPreviewC
void DeckPreviewWidget::setTags(const QStringList &tags)
{
deckLoader->getDeck().deckList.setTags(tags);
deckLoader->saveToFile(filePath, DeckFileFormat::Cockatrice);
DeckLoader::saveToFile(deckLoader->getDeck());
}
QMenu *DeckPreviewWidget::createRightClickMenu()
@ -386,7 +386,7 @@ void DeckPreviewWidget::actRenameDeck()
// write change
deckLoader->getDeck().deckList.setName(newName);
deckLoader->saveToFile(filePath, DeckFileFormat::getFormatFromName(filePath));
DeckLoader::saveToFile(deckLoader->getDeck());
// update VDS
refreshBannerCardText();