mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 09:04:53 -07:00
[VDS] Reload deck on hover if file has been modified since last load (#6507)
* add reload to DeckLoader * [VDS] Reload deck on hover if file has been modified since last load * fix version incompatibility
This commit is contained in:
parent
d9b9c79112
commit
39ddaa0c35
7 changed files with 124 additions and 20 deletions
|
|
@ -91,6 +91,22 @@ void DeckLoader::loadFromFileAsync(const QString &fileName, DeckFileFormat::Form
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DeckLoader::reload()
|
||||||
|
{
|
||||||
|
QString lastFileName = loadedDeck.lastLoadInfo.fileName;
|
||||||
|
if (lastFileName.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::optional<LoadedDeck> deck = loadFromFile(lastFileName, loadedDeck.lastLoadInfo.fileFormat, false);
|
||||||
|
|
||||||
|
if (!deck) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedDeck = *deck;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<LoadedDeck> DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
|
std::optional<LoadedDeck> DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
|
||||||
{
|
{
|
||||||
DeckList deckList;
|
DeckList deckList;
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,13 @@ public:
|
||||||
*/
|
*/
|
||||||
void loadFromFileAsync(const QString &fileName, DeckFileFormat::Format fmt, bool userRequest);
|
void loadFromFileAsync(const QString &fileName, DeckFileFormat::Format fmt, bool userRequest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Loads the file that the lastLoadInfo currently points to into this instance.
|
||||||
|
* No-ops if the lastLoadInfo is missing the required info or the load fails.
|
||||||
|
* @return Whether the loaded succeeded.
|
||||||
|
*/
|
||||||
|
bool reload();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Loads a deck from a local file.
|
* @brief Loads a deck from a local file.
|
||||||
* @param fileName The file to load
|
* @param fileName The file to load
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,16 @@ void ColorIdentityWidget::populateManaSymbolWidgets()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ColorIdentityWidget::setColorIdentity(const QString &_colorIdentity)
|
||||||
|
{
|
||||||
|
if (colorIdentity == _colorIdentity) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
colorIdentity = _colorIdentity;
|
||||||
|
populateManaSymbolWidgets();
|
||||||
|
}
|
||||||
|
|
||||||
void ColorIdentityWidget::toggleUnusedVisibility()
|
void ColorIdentityWidget::toggleUnusedVisibility()
|
||||||
{
|
{
|
||||||
populateManaSymbolWidgets();
|
populateManaSymbolWidgets();
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public:
|
||||||
static QStringList parseColorIdentity(const QString &manaString);
|
static QStringList parseColorIdentity(const QString &manaString);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void setColorIdentity(const QString &_colorIdentity);
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
void toggleUnusedVisibility();
|
void toggleUnusedVisibility();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class DeckPreviewDeckTagsDisplayWidget : public QWidget
|
||||||
FlowWidget *flowWidget;
|
FlowWidget *flowWidget;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DeckPreviewDeckTagsDisplayWidget(QWidget *_parent, const QStringList &_tags);
|
explicit DeckPreviewDeckTagsDisplayWidget(QWidget *_parent, const QStringList &_tags = {});
|
||||||
void setTags(const QStringList &_tags);
|
void setTags(const QStringList &_tags);
|
||||||
void refreshTags();
|
void refreshTags();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,21 +71,48 @@ void DeckPreviewWidget::resizeEvent(QResizeEvent *event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
void DeckPreviewWidget::enterEvent(QEnterEvent *event)
|
||||||
|
#else
|
||||||
|
void DeckPreviewWidget::enterEvent(QEvent *event)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
QWidget::enterEvent(event);
|
||||||
|
reloadIfModified();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the lastModifiedTime to the value given by the file.
|
||||||
|
*/
|
||||||
|
void DeckPreviewWidget::updateLastModifiedTime()
|
||||||
|
{
|
||||||
|
QFileInfo fileInfo(filePath);
|
||||||
|
lastModifiedTime = fileInfo.lastModified();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Writes the current contents of the deck to file. Updates the lastModifiedTime afterward.
|
||||||
|
*/
|
||||||
|
void DeckPreviewWidget::writeDeckToFile()
|
||||||
|
{
|
||||||
|
DeckLoader::saveToFile(deckLoader->getDeck());
|
||||||
|
updateLastModifiedTime();
|
||||||
|
}
|
||||||
|
|
||||||
void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
||||||
{
|
{
|
||||||
if (!deckLoadSuccess) {
|
if (!deckLoadSuccess) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto bannerCard = deckLoader->getDeck().deckList.getBannerCard().name.isEmpty()
|
|
||||||
? ExactCard()
|
|
||||||
: CardDatabaseManager::query()->getCard(deckLoader->getDeck().deckList.getBannerCard());
|
|
||||||
|
|
||||||
bannerCardDisplayWidget->setCard(bannerCard);
|
QFileInfo fileInfo(filePath);
|
||||||
|
lastModifiedTime = fileInfo.lastModified();
|
||||||
|
|
||||||
bannerCardDisplayWidget->setFontSize(24);
|
bannerCardDisplayWidget->setFontSize(24);
|
||||||
setFilePath(deckLoader->getDeck().lastLoadInfo.fileName);
|
setFilePath(deckLoader->getDeck().lastLoadInfo.fileName);
|
||||||
|
|
||||||
colorIdentityWidget = new ColorIdentityWidget(this, getColorIdentity());
|
colorIdentityWidget = new ColorIdentityWidget(this);
|
||||||
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader->getDeck().deckList.getTags());
|
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this);
|
||||||
connect(deckTagsDisplayWidget, &DeckPreviewDeckTagsDisplayWidget::tagsChanged, this, &DeckPreviewWidget::setTags);
|
connect(deckTagsDisplayWidget, &DeckPreviewDeckTagsDisplayWidget::tagsChanged, this, &DeckPreviewWidget::setTags);
|
||||||
|
|
||||||
bannerCardLabel = new QLabel(this);
|
bannerCardLabel = new QLabel(this);
|
||||||
|
|
@ -93,13 +120,11 @@ void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
||||||
bannerCardComboBox = new QComboBox(this);
|
bannerCardComboBox = new QComboBox(this);
|
||||||
bannerCardComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
bannerCardComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
||||||
bannerCardComboBox->setObjectName("bannerCardComboBox");
|
bannerCardComboBox->setObjectName("bannerCardComboBox");
|
||||||
bannerCardComboBox->setCurrentText(deckLoader->getDeck().deckList.getBannerCard().name);
|
|
||||||
bannerCardComboBox->installEventFilter(new NoScrollFilter(bannerCardComboBox));
|
bannerCardComboBox->installEventFilter(new NoScrollFilter(bannerCardComboBox));
|
||||||
connect(bannerCardComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
connect(bannerCardComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
||||||
&DeckPreviewWidget::setBannerCard);
|
&DeckPreviewWidget::setBannerCard);
|
||||||
|
|
||||||
updateColorIdentityVisibility(SettingsCache::instance().getVisualDeckStorageShowColorIdentity());
|
updateColorIdentityVisibility(SettingsCache::instance().getVisualDeckStorageShowColorIdentity());
|
||||||
updateBannerCardComboBox();
|
|
||||||
updateBannerCardComboBoxVisibility(SettingsCache::instance().getVisualDeckStorageShowBannerCardComboBox());
|
updateBannerCardComboBoxVisibility(SettingsCache::instance().getVisualDeckStorageShowBannerCardComboBox());
|
||||||
updateTagsVisibility(SettingsCache::instance().getVisualDeckStorageShowTagsOnDeckPreviews());
|
updateTagsVisibility(SettingsCache::instance().getVisualDeckStorageShowTagsOnDeckPreviews());
|
||||||
|
|
||||||
|
|
@ -108,9 +133,44 @@ void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
||||||
layout->addWidget(bannerCardLabel);
|
layout->addWidget(bannerCardLabel);
|
||||||
layout->addWidget(bannerCardComboBox);
|
layout->addWidget(bannerCardComboBox);
|
||||||
|
|
||||||
refreshBannerCardText();
|
|
||||||
|
|
||||||
retranslateUi();
|
retranslateUi();
|
||||||
|
resyncWidgets();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Syncs the contents of the child widgets with the current deck.
|
||||||
|
*/
|
||||||
|
void DeckPreviewWidget::resyncWidgets()
|
||||||
|
{
|
||||||
|
auto bannerCardRef = deckLoader->getDeck().deckList.getBannerCard();
|
||||||
|
auto bannerCard = bannerCardRef.name.isEmpty() ? ExactCard() : CardDatabaseManager::query()->getCard(bannerCardRef);
|
||||||
|
|
||||||
|
bannerCardDisplayWidget->setCard(bannerCard);
|
||||||
|
refreshBannerCardText();
|
||||||
|
updateBannerCardComboBox(bannerCardRef.name);
|
||||||
|
colorIdentityWidget->setColorIdentity(getColorIdentity());
|
||||||
|
deckTagsDisplayWidget->setTags(deckLoader->getDeck().deckList.getTags());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reloads the deck if the file's last modified time has increased since we last checked.
|
||||||
|
*/
|
||||||
|
void DeckPreviewWidget::reloadIfModified()
|
||||||
|
{
|
||||||
|
QFileInfo fileInfo(filePath);
|
||||||
|
QDateTime newLastModifiedTime = fileInfo.lastModified();
|
||||||
|
|
||||||
|
if (!newLastModifiedTime.isValid() || newLastModifiedTime <= lastModifiedTime) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool success = deckLoader->reload();
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
fileInfo.refresh();
|
||||||
|
lastModifiedTime = fileInfo.lastModified();
|
||||||
|
resyncWidgets();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckPreviewWidget::updateVisibility()
|
void DeckPreviewWidget::updateVisibility()
|
||||||
|
|
@ -232,11 +292,8 @@ void DeckPreviewWidget::refreshBannerCardToolTip()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckPreviewWidget::updateBannerCardComboBox()
|
void DeckPreviewWidget::updateBannerCardComboBox(const QString ¤tText)
|
||||||
{
|
{
|
||||||
// Store the current text of the combo box
|
|
||||||
QString currentText = bannerCardComboBox->currentText();
|
|
||||||
|
|
||||||
// Block signals temporarily
|
// Block signals temporarily
|
||||||
bool wasBlocked = bannerCardComboBox->blockSignals(true);
|
bool wasBlocked = bannerCardComboBox->blockSignals(true);
|
||||||
bannerCardComboBox->setUpdatesEnabled(false);
|
bannerCardComboBox->setUpdatesEnabled(false);
|
||||||
|
|
@ -300,7 +357,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(deckLoader->getDeck());
|
writeDeckToFile();
|
||||||
bannerCardDisplayWidget->setCard(CardDatabaseManager::query()->getCard(cardRef));
|
bannerCardDisplayWidget->setCard(CardDatabaseManager::query()->getCard(cardRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -323,7 +380,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(deckLoader->getDeck());
|
writeDeckToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
QMenu *DeckPreviewWidget::createRightClickMenu()
|
QMenu *DeckPreviewWidget::createRightClickMenu()
|
||||||
|
|
@ -398,7 +455,7 @@ void DeckPreviewWidget::actRenameDeck()
|
||||||
|
|
||||||
// write change
|
// write change
|
||||||
deckLoader->getDeck().deckList.setName(newName);
|
deckLoader->getDeck().deckList.setName(newName);
|
||||||
DeckLoader::saveToFile(deckLoader->getDeck());
|
writeDeckToFile();
|
||||||
|
|
||||||
// update VDS
|
// update VDS
|
||||||
refreshBannerCardText();
|
refreshBannerCardText();
|
||||||
|
|
@ -429,9 +486,10 @@ void DeckPreviewWidget::actRenameFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
deckLoader->getDeck().lastLoadInfo.fileName = newFilePath;
|
deckLoader->getDeck().lastLoadInfo.fileName = newFilePath;
|
||||||
|
setFilePath(newFilePath);
|
||||||
|
|
||||||
// update VDS
|
// update VDS
|
||||||
setFilePath(newFilePath);
|
updateLastModifiedTime();
|
||||||
refreshBannerCardText();
|
refreshBannerCardText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ public:
|
||||||
VisualDeckStorageWidget *visualDeckStorageWidget;
|
VisualDeckStorageWidget *visualDeckStorageWidget;
|
||||||
QVBoxLayout *layout;
|
QVBoxLayout *layout;
|
||||||
QString filePath;
|
QString filePath;
|
||||||
|
QDateTime lastModifiedTime;
|
||||||
DeckLoader *deckLoader;
|
DeckLoader *deckLoader;
|
||||||
DeckPreviewCardPictureWidget *bannerCardDisplayWidget = nullptr;
|
DeckPreviewCardPictureWidget *bannerCardDisplayWidget = nullptr;
|
||||||
ColorIdentityWidget *colorIdentityWidget = nullptr;
|
ColorIdentityWidget *colorIdentityWidget = nullptr;
|
||||||
|
|
@ -57,18 +58,29 @@ public slots:
|
||||||
void setFilePath(const QString &filePath);
|
void setFilePath(const QString &filePath);
|
||||||
void refreshBannerCardText();
|
void refreshBannerCardText();
|
||||||
void refreshBannerCardToolTip();
|
void refreshBannerCardToolTip();
|
||||||
void updateBannerCardComboBox();
|
void updateBannerCardComboBox(const QString ¤tText);
|
||||||
void setBannerCard(int);
|
void setBannerCard(int);
|
||||||
void imageClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
void imageClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
||||||
void imageDoubleClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
void imageDoubleClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
|
||||||
void initializeUi(bool deckLoadSuccess);
|
void initializeUi(bool deckLoadSuccess);
|
||||||
|
void resyncWidgets();
|
||||||
|
void reloadIfModified();
|
||||||
void updateVisibility();
|
void updateVisibility();
|
||||||
void updateColorIdentityVisibility(bool visible);
|
void updateColorIdentityVisibility(bool visible);
|
||||||
void updateBannerCardComboBoxVisibility(bool visible);
|
void updateBannerCardComboBoxVisibility(bool visible);
|
||||||
void updateTagsVisibility(bool visible);
|
void updateTagsVisibility(bool visible);
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
void enterEvent(QEnterEvent *event) override; // Qt6 signature
|
||||||
|
#else
|
||||||
|
void enterEvent(QEvent *event) override; // Qt5 signature
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void updateLastModifiedTime();
|
||||||
|
void writeDeckToFile();
|
||||||
QMenu *createRightClickMenu();
|
QMenu *createRightClickMenu();
|
||||||
void addSetBannerCardMenu(QMenu *menu);
|
void addSetBannerCardMenu(QMenu *menu);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue