[DeckLoader] Fix missing lastFileName when opened from outside deck editor

This commit is contained in:
RickyRister 2025-11-20 03:49:09 -08:00
parent 8788a7aada
commit c5e57deb0f
9 changed files with 46 additions and 34 deletions

View file

@ -331,7 +331,7 @@ void DeckViewContainer::loadFromWebsite()
void DeckViewContainer::deckSelectFinished(const Response &r) void DeckViewContainer::deckSelectFinished(const Response &r)
{ {
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext); const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
DeckLoader newDeck(this, new DeckList(QString::fromStdString(resp.deck()))); DeckLoader newDeck(this, new DeckList(QString::fromStdString(resp.deck())), {});
CardPictureLoader::cacheCardPixmaps( CardPictureLoader::cacheCardPixmaps(
CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList())); CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList()));
setDeck(newDeck); setDeck(newDeck);

View file

@ -265,7 +265,7 @@ void Player::deleteCard(CardItem *card)
void Player::setDeck(const DeckLoader &_deck) void Player::setDeck(const DeckLoader &_deck)
{ {
deck = new DeckLoader(this, _deck.getDeckList()); deck = new DeckLoader(this, _deck.getDeckList(), _deck.getLastLoadInfo());
emit deckChanged(); emit deckChanged();
} }

View file

@ -25,13 +25,12 @@ const QStringList DeckLoader::ACCEPTED_FILE_EXTENSIONS = {"*.cod", "*.dec", "*.d
const QStringList DeckLoader::FILE_NAME_FILTERS = { const QStringList DeckLoader::FILE_NAME_FILTERS = {
tr("Common deck formats (%1)").arg(ACCEPTED_FILE_EXTENSIONS.join(" ")), tr("All files (*.*)")}; tr("Common deck formats (%1)").arg(ACCEPTED_FILE_EXTENSIONS.join(" ")), tr("All files (*.*)")};
DeckLoader::DeckLoader(QObject *parent) DeckLoader::DeckLoader(QObject *parent) : QObject(parent), deckList(new DeckList())
: QObject(parent), deckList(new DeckList()), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1)
{ {
} }
DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList) DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList, const LoadInfo &_lastLoadInfo)
: QObject(parent), deckList(_deckList), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1) : QObject(parent), deckList(_deckList), lastLoadInfo(_lastLoadInfo)
{ {
deckList->setParent(this); deckList->setParent(this);
} }
@ -71,8 +70,8 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool user
} }
if (result) { if (result) {
lastFileName = fileName; lastLoadInfo.fileName = fileName;
lastFileFormat = fmt; lastLoadInfo.fileFormat = fmt;
if (userRequest) { if (userRequest) {
updateLastLoadedTimestamp(fileName, fmt); updateLastLoadedTimestamp(fileName, fmt);
} }
@ -93,8 +92,8 @@ bool DeckLoader::loadFromFileAsync(const QString &fileName, FileFormat fmt, bool
watcher->deleteLater(); watcher->deleteLater();
if (result) { if (result) {
lastFileName = fileName; lastLoadInfo.fileName = fileName;
lastFileFormat = fmt; lastLoadInfo.fileFormat = fmt;
if (userRequest) { if (userRequest) {
updateLastLoadedTimestamp(fileName, fmt); updateLastLoadedTimestamp(fileName, fmt);
} }
@ -136,9 +135,9 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
{ {
bool result = deckList->loadFromString_Native(nativeString); bool result = deckList->loadFromString_Native(nativeString);
if (result) { if (result) {
lastFileName = QString(); lastLoadInfo.fileName = QString();
lastFileFormat = CockatriceFormat; lastLoadInfo.fileFormat = CockatriceFormat;
lastRemoteDeckId = remoteDeckId; lastLoadInfo.remoteDeckId = remoteDeckId;
emit deckLoaded(); emit deckLoaded();
} }
@ -163,8 +162,8 @@ bool DeckLoader::saveToFile(const QString &fileName, FileFormat fmt)
} }
if (result) { if (result) {
lastFileName = fileName; lastLoadInfo.fileName = fileName;
lastFileFormat = fmt; lastLoadInfo.fileFormat = fmt;
} }
file.flush(); file.flush();
@ -206,8 +205,8 @@ bool DeckLoader::updateLastLoadedTimestamp(const QString &fileName, FileFormat f
file.close(); // Close the file to ensure changes are flushed file.close(); // Close the file to ensure changes are flushed
if (result) { if (result) {
lastFileName = fileName; lastLoadInfo.fileName = fileName;
lastFileFormat = fmt; lastLoadInfo.fileFormat = fmt;
// Re-open the file and set the original timestamp // Re-open the file and set the original timestamp
if (!file.open(QIODevice::ReadWrite)) { if (!file.open(QIODevice::ReadWrite)) {
@ -587,8 +586,8 @@ bool DeckLoader::convertToCockatriceFormat(QString fileName)
} else { } else {
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName; qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
} }
lastFileName = newFileName; lastLoadInfo.fileName = newFileName;
lastFileFormat = CockatriceFormat; lastLoadInfo.fileFormat = CockatriceFormat;
} }
return result; return result;

View file

@ -44,40 +44,53 @@ public:
DecklistXyz DecklistXyz
}; };
/**
* @brief Information about the file or source that the deck was loaded from
*/
struct LoadInfo
{
QString fileName;
FileFormat fileFormat = CockatriceFormat;
int remoteDeckId = -1;
};
private: private:
DeckList *deckList; DeckList *deckList;
QString lastFileName; LoadInfo lastLoadInfo;
FileFormat lastFileFormat;
int lastRemoteDeckId;
public: public:
DeckLoader(QObject *parent); DeckLoader(QObject *parent);
DeckLoader(QObject *parent, DeckList *_deckList); DeckLoader(QObject *parent, DeckList *_deckList, const LoadInfo &_lastLoadInfo);
DeckLoader(const DeckLoader &) = delete; DeckLoader(const DeckLoader &) = delete;
DeckLoader &operator=(const DeckLoader &) = delete; DeckLoader &operator=(const DeckLoader &) = delete;
void setDeckList(DeckList *_deckList); void setDeckList(DeckList *_deckList);
const LoadInfo &getLastLoadInfo() const
{
return lastLoadInfo;
}
const QString &getLastFileName() const const QString &getLastFileName() const
{ {
return lastFileName; return lastLoadInfo.fileName;
} }
void setLastFileName(const QString &_lastFileName) void setLastFileName(const QString &_lastFileName)
{ {
lastFileName = _lastFileName; lastLoadInfo.fileName = _lastFileName;
} }
FileFormat getLastFileFormat() const FileFormat getLastFileFormat() const
{ {
return lastFileFormat; return lastLoadInfo.fileFormat;
} }
int getLastRemoteDeckId() const int getLastRemoteDeckId() const
{ {
return lastRemoteDeckId; return lastLoadInfo.remoteDeckId;
} }
bool hasNotBeenLoaded() const bool hasNotBeenLoaded() const
{ {
return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1; return lastLoadInfo.fileName.isEmpty() && lastLoadInfo.remoteDeckId == -1;
} }
static void clearSetNamesAndNumbers(const DeckList *deckList); static void clearSetNamesAndNumbers(const DeckList *deckList);

View file

@ -32,7 +32,7 @@ void DeckEditorDeckDockWidget::createDeckDock()
deckModel->setObjectName("deckModel"); deckModel->setObjectName("deckModel");
connect(deckModel, &DeckListModel::deckHashChanged, this, &DeckEditorDeckDockWidget::updateHash); connect(deckModel, &DeckListModel::deckHashChanged, this, &DeckEditorDeckDockWidget::updateHash);
deckLoader = new DeckLoader(this, deckModel->getDeckList()); deckLoader = new DeckLoader(this, deckModel->getDeckList(), {});
proxy = new DeckListStyleProxy(this); proxy = new DeckListStyleProxy(this);
proxy->setSourceModel(deckModel); proxy->setSourceModel(deckModel);

View file

@ -142,7 +142,7 @@ DlgEditDeckInClipboard::DlgEditDeckInClipboard(const DeckLoader &deckList, bool
{ {
setWindowTitle(tr("Edit deck in clipboard")); setWindowTitle(tr("Edit deck in clipboard"));
deckLoader = new DeckLoader(this, deckList.getDeckList()); deckLoader = new DeckLoader(this, deckList.getDeckList(), deckList.getLastLoadInfo());
deckLoader->setParent(this); deckLoader->setParent(this);
DlgEditDeckInClipboard::actRefresh(); DlgEditDeckInClipboard::actRefresh();

View file

@ -493,7 +493,7 @@ void TabDeckStorage::downloadFinished(const Response &r,
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext); const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
QString filePath = extraData.toString(); QString filePath = extraData.toString();
DeckLoader deck(this, new DeckList(QString::fromStdString(resp.deck()))); DeckLoader deck(this, new DeckList(QString::fromStdString(resp.deck())), {});
deck.saveToFile(filePath, DeckLoader::CockatriceFormat); deck.saveToFile(filePath, DeckLoader::CockatriceFormat);
} }

View file

@ -754,7 +754,7 @@ void TabGame::loadDeckForLocalPlayer(Player *localPlayer, int playerId, ServerIn
{ {
TabbedDeckViewContainer *deckViewContainer = deckViewContainers.value(playerId); TabbedDeckViewContainer *deckViewContainer = deckViewContainers.value(playerId);
if (playerInfo.has_deck_list()) { if (playerInfo.has_deck_list()) {
DeckLoader newDeck(this, new DeckList(QString::fromStdString(playerInfo.deck_list()))); DeckLoader newDeck(this, new DeckList(QString::fromStdString(playerInfo.deck_list())), {});
CardPictureLoader::cacheCardPixmaps( CardPictureLoader::cacheCardPixmaps(
CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList())); CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList()));
deckViewContainer->playerDeckView->setDeck(newDeck); deckViewContainer->playerDeckView->setDeck(newDeck);

View file

@ -869,7 +869,7 @@ TabDeckEditor *TabSupervisor::addDeckEditorTab(DeckLoader *deckToOpen)
{ {
auto *tab = new TabDeckEditor(this); auto *tab = new TabDeckEditor(this);
if (deckToOpen) if (deckToOpen)
tab->openDeck(new DeckLoader(this, deckToOpen->getDeckList())); tab->openDeck(new DeckLoader(this, deckToOpen->getDeckList(), deckToOpen->getLastLoadInfo()));
connect(tab, &AbstractTabDeckEditor::deckEditorClosing, this, &TabSupervisor::deckEditorClosed); connect(tab, &AbstractTabDeckEditor::deckEditorClosing, this, &TabSupervisor::deckEditorClosed);
connect(tab, &AbstractTabDeckEditor::openDeckEditor, this, &TabSupervisor::addDeckEditorTab); connect(tab, &AbstractTabDeckEditor::openDeckEditor, this, &TabSupervisor::addDeckEditorTab);
myAddTab(tab); myAddTab(tab);
@ -882,7 +882,7 @@ TabDeckEditorVisual *TabSupervisor::addVisualDeckEditorTab(DeckLoader *deckToOpe
{ {
auto *tab = new TabDeckEditorVisual(this); auto *tab = new TabDeckEditorVisual(this);
if (deckToOpen) if (deckToOpen)
tab->openDeck(new DeckLoader(this, deckToOpen->getDeckList())); tab->openDeck(new DeckLoader(this, deckToOpen->getDeckList(), deckToOpen->getLastLoadInfo()));
connect(tab, &AbstractTabDeckEditor::deckEditorClosing, this, &TabSupervisor::deckEditorClosed); connect(tab, &AbstractTabDeckEditor::deckEditorClosing, this, &TabSupervisor::deckEditorClosed);
connect(tab, &AbstractTabDeckEditor::openDeckEditor, this, &TabSupervisor::addVisualDeckEditorTab); connect(tab, &AbstractTabDeckEditor::openDeckEditor, this, &TabSupervisor::addVisualDeckEditorTab);
myAddTab(tab); myAddTab(tab);