use static methods

This commit is contained in:
RickyRister 2026-01-01 03:05:23 -08:00
parent 44ed3411d0
commit 7da585c597
7 changed files with 37 additions and 44 deletions

View file

@ -260,16 +260,15 @@ void DeckViewContainer::loadLocalDeck()
void DeckViewContainer::loadDeckFromFile(const QString &filePath) void DeckViewContainer::loadDeckFromFile(const QString &filePath)
{ {
DeckFileFormat::Format fmt = DeckFileFormat::getFormatFromName(filePath); DeckFileFormat::Format fmt = DeckFileFormat::getFormatFromName(filePath);
DeckLoader deck(this);
bool success = deck.loadFromFile(filePath, fmt, true); std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(filePath, fmt, true);
if (!success) { if (!deckOpt) {
QMessageBox::critical(this, tr("Error"), tr("The selected file could not be loaded.")); QMessageBox::critical(this, tr("Error"), tr("The selected file could not be loaded."));
return; return;
} }
loadDeckFromDeckList(deck.getDeck().deckList); loadDeckFromDeckList(deckOpt.value().deckList);
} }
void DeckViewContainer::loadDeckFromDeckList(const DeckList &deck) void DeckViewContainer::loadDeckFromDeckList(const DeckList &deck)

View file

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

View file

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

View file

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

View file

@ -24,11 +24,12 @@ TabDeckStorageVisual::TabDeckStorageVisual(TabSupervisor *_tabSupervisor)
void TabDeckStorageVisual::actOpenLocalDeck(const QString &filePath) void TabDeckStorageVisual::actOpenLocalDeck(const QString &filePath)
{ {
auto deckLoader = DeckLoader(this); std::optional<LoadedDeck> deckOpt =
if (!deckLoader.loadFromFile(filePath, DeckFileFormat::getFormatFromName(filePath), true)) { DeckLoader::loadFromFile(filePath, DeckFileFormat::getFormatFromName(filePath), true);
if (!deckOpt) {
QMessageBox::critical(this, tr("Error"), tr("Could not open deck at %1").arg(filePath)); QMessageBox::critical(this, tr("Error"), tr("Could not open deck at %1").arg(filePath));
return; 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 allFiles = getAllFiles(SettingsCache::instance().getDeckPath());
QStringList knownTags; QStringList knownTags;
auto loader = DeckLoader(nullptr);
for (const QString &file : allFiles) { for (const QString &file : allFiles) {
loader.loadFromFile(file, DeckFileFormat::getFormatFromName(file), false); std::optional<LoadedDeck> deckOpt =
QStringList tags = loader.getDeck().deckList.getTags(); DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), false);
QStringList tags = deckOpt.has_value() ? deckOpt->deckList.getTags() : QStringList();
knownTags.append(tags); knownTags.append(tags);
knownTags.removeDuplicates(); knownTags.removeDuplicates();
} }
@ -124,7 +124,7 @@ static bool confirmOverwriteIfExists(QWidget *parent, const QString &filePath)
static void convertFileToCockatriceFormat(DeckPreviewWidget *deckPreviewWidget) static void convertFileToCockatriceFormat(DeckPreviewWidget *deckPreviewWidget)
{ {
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath); DeckLoader::convertToCockatriceFormat(deckPreviewWidget->deckLoader->getDeck());
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getDeck().lastLoadInfo.fileName; deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getDeck().lastLoadInfo.fileName;
deckPreviewWidget->refreshBannerCardText(); deckPreviewWidget->refreshBannerCardText();
} }

View file

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