mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-23 23:23:55 -07:00
[DeckLoader] Make save/load methods static (#6476)
* const * [DeckLoader] make methods static * use static methods * add docs * add docs
This commit is contained in:
parent
c7c7bf550a
commit
d579c82cb9
11 changed files with 209 additions and 166 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue