[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)
{
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(
CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList()));
setDeck(newDeck);

View file

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

View file

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

View file

@ -44,40 +44,53 @@ public:
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:
DeckList *deckList;
QString lastFileName;
FileFormat lastFileFormat;
int lastRemoteDeckId;
LoadInfo lastLoadInfo;
public:
DeckLoader(QObject *parent);
DeckLoader(QObject *parent, DeckList *_deckList);
DeckLoader(QObject *parent, DeckList *_deckList, const LoadInfo &_lastLoadInfo);
DeckLoader(const DeckLoader &) = delete;
DeckLoader &operator=(const DeckLoader &) = delete;
void setDeckList(DeckList *_deckList);
const LoadInfo &getLastLoadInfo() const
{
return lastLoadInfo;
}
const QString &getLastFileName() const
{
return lastFileName;
return lastLoadInfo.fileName;
}
void setLastFileName(const QString &_lastFileName)
{
lastFileName = _lastFileName;
lastLoadInfo.fileName = _lastFileName;
}
FileFormat getLastFileFormat() const
{
return lastFileFormat;
return lastLoadInfo.fileFormat;
}
int getLastRemoteDeckId() const
{
return lastRemoteDeckId;
return lastLoadInfo.remoteDeckId;
}
bool hasNotBeenLoaded() const
{
return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1;
return lastLoadInfo.fileName.isEmpty() && lastLoadInfo.remoteDeckId == -1;
}
static void clearSetNamesAndNumbers(const DeckList *deckList);

View file

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

View file

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

View file

@ -493,7 +493,7 @@ void TabDeckStorage::downloadFinished(const Response &r,
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
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);
}

View file

@ -754,7 +754,7 @@ void TabGame::loadDeckForLocalPlayer(Player *localPlayer, int playerId, ServerIn
{
TabbedDeckViewContainer *deckViewContainer = deckViewContainers.value(playerId);
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(
CardDatabaseManager::query()->getCards(newDeck.getDeckList()->getCardRefList()));
deckViewContainer->playerDeckView->setDeck(newDeck);

View file

@ -869,7 +869,7 @@ TabDeckEditor *TabSupervisor::addDeckEditorTab(DeckLoader *deckToOpen)
{
auto *tab = new TabDeckEditor(this);
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::openDeckEditor, this, &TabSupervisor::addDeckEditorTab);
myAddTab(tab);
@ -882,7 +882,7 @@ TabDeckEditorVisual *TabSupervisor::addVisualDeckEditorTab(DeckLoader *deckToOpe
{
auto *tab = new TabDeckEditorVisual(this);
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::openDeckEditor, this, &TabSupervisor::addVisualDeckEditorTab);
myAddTab(tab);