mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-19 00:42:14 -07:00
Add loadFromFileAsync to deckLoader and connect VisualDeckStorageWidget to it.
This commit is contained in:
parent
ba89495dc0
commit
d53995699e
4 changed files with 79 additions and 17 deletions
|
|
@ -16,12 +16,8 @@ DeckPreviewWidget::DeckPreviewWidget(VisualDeckStorageWidget *_parent, const QSt
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
||||||
deckLoader = new DeckLoader();
|
deckLoader = new DeckLoader();
|
||||||
deckLoader->loadFromFile(filePath, DeckLoader::CockatriceFormat);
|
connect(deckLoader, &DeckLoader::loadFinished, this, &DeckPreviewWidget::initializeUi);
|
||||||
|
deckLoader->loadFromFileAsync(filePath, DeckLoader::CockatriceFormat, false);
|
||||||
auto bannerCard = deckLoader->getBannerCard().first.isEmpty()
|
|
||||||
? CardInfoPtr()
|
|
||||||
: CardDatabaseManager::getInstance()->getCardByNameAndProviderId(
|
|
||||||
deckLoader->getBannerCard().first, deckLoader->getBannerCard().second);
|
|
||||||
|
|
||||||
bannerCardDisplayWidget = new DeckPreviewCardPictureWidget(this);
|
bannerCardDisplayWidget = new DeckPreviewCardPictureWidget(this);
|
||||||
|
|
||||||
|
|
@ -30,19 +26,31 @@ DeckPreviewWidget::DeckPreviewWidget(VisualDeckStorageWidget *_parent, const QSt
|
||||||
connect(bannerCardDisplayWidget, &DeckPreviewCardPictureWidget::imageDoubleClicked, this,
|
connect(bannerCardDisplayWidget, &DeckPreviewCardPictureWidget::imageDoubleClicked, this,
|
||||||
&DeckPreviewWidget::imageDoubleClickedEvent);
|
&DeckPreviewWidget::imageDoubleClickedEvent);
|
||||||
|
|
||||||
|
layout->addWidget(bannerCardDisplayWidget);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
||||||
|
{
|
||||||
|
if (deckLoadSuccess) {
|
||||||
|
auto bannerCard = deckLoader->getBannerCard().first.isEmpty()
|
||||||
|
? CardInfoPtr()
|
||||||
|
: CardDatabaseManager::getInstance()->getCardByNameAndProviderId(
|
||||||
|
deckLoader->getBannerCard().first, deckLoader->getBannerCard().second);
|
||||||
|
|
||||||
bannerCardDisplayWidget->setCard(bannerCard);
|
bannerCardDisplayWidget->setCard(bannerCard);
|
||||||
bannerCardDisplayWidget->setOverlayText(
|
bannerCardDisplayWidget->setOverlayText(deckLoader->getName().isEmpty()
|
||||||
deckLoader->getName().isEmpty() ? QFileInfo(deckLoader->getLastFileName()).fileName() : deckLoader->getName());
|
? QFileInfo(deckLoader->getLastFileName()).fileName()
|
||||||
|
: deckLoader->getName());
|
||||||
bannerCardDisplayWidget->setFontSize(24);
|
bannerCardDisplayWidget->setFontSize(24);
|
||||||
setFilePath(deckLoader->getLastFileName());
|
setFilePath(deckLoader->getLastFileName());
|
||||||
|
|
||||||
colorIdentityWidget = new DeckPreviewColorIdentityWidget(getColorIdentity());
|
colorIdentityWidget = new DeckPreviewColorIdentityWidget(getColorIdentity());
|
||||||
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader);
|
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader);
|
||||||
|
|
||||||
layout->addWidget(bannerCardDisplayWidget);
|
|
||||||
layout->addWidget(colorIdentityWidget);
|
layout->addWidget(colorIdentityWidget);
|
||||||
layout->addWidget(deckTagsDisplayWidget);
|
layout->addWidget(deckTagsDisplayWidget);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QString DeckPreviewWidget::getColorIdentity()
|
QString DeckPreviewWidget::getColorIdentity()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ public slots:
|
||||||
void setFilePath(const QString &filePath);
|
void setFilePath(const QString &filePath);
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DECK_PREVIEW_WIDGET_H
|
#endif // DECK_PREVIEW_WIDGET_H
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,11 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QFutureWatcher>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <qloggingcategory.h>
|
#include <qloggingcategory.h>
|
||||||
|
#include <qtconcurrentrun.h>
|
||||||
|
|
||||||
Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
|
Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
|
||||||
|
|
||||||
|
|
@ -80,6 +82,55 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool user
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DeckLoader::loadFromFileAsync(const QString &fileName, FileFormat fmt, bool userRequest)
|
||||||
|
{
|
||||||
|
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>(this);
|
||||||
|
|
||||||
|
connect(watcher, &QFutureWatcher<bool>::finished, this, [this, watcher, fileName, fmt, userRequest]() {
|
||||||
|
const bool result = watcher->result();
|
||||||
|
watcher->deleteLater();
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
lastFileName = fileName;
|
||||||
|
lastFileFormat = fmt;
|
||||||
|
if (userRequest) {
|
||||||
|
updateLastLoadedTimestamp(fileName, fmt);
|
||||||
|
}
|
||||||
|
emit deckLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
|
emit loadFinished(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
QFuture<bool> future = QtConcurrent::run([=, this]() {
|
||||||
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool result = false;
|
||||||
|
switch (fmt) {
|
||||||
|
case PlainTextFormat:
|
||||||
|
result = loadFromFile_Plain(&file);
|
||||||
|
break;
|
||||||
|
case CockatriceFormat: {
|
||||||
|
result = loadFromFile_Native(&file);
|
||||||
|
if (!result) {
|
||||||
|
file.seek(0);
|
||||||
|
result = loadFromFile_Plain(&file);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
watcher->setFuture(future);
|
||||||
|
return true; // Return immediately to indicate the async task was started
|
||||||
|
}
|
||||||
|
|
||||||
bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
|
bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
|
||||||
{
|
{
|
||||||
bool result = loadFromString_Native(nativeString);
|
bool result = loadFromString_Native(nativeString);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ class DeckLoader : public DeckList
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
signals:
|
signals:
|
||||||
void deckLoaded();
|
void deckLoaded();
|
||||||
|
void loadFinished(bool success);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum FileFormat
|
enum FileFormat
|
||||||
|
|
@ -43,6 +44,7 @@ public:
|
||||||
static FileFormat getFormatFromName(const QString &fileName);
|
static FileFormat getFormatFromName(const QString &fileName);
|
||||||
|
|
||||||
bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false);
|
bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false);
|
||||||
|
bool loadFromFileAsync(const QString &fileName, FileFormat fmt, bool userRequest);
|
||||||
bool loadFromRemote(const QString &nativeString, int remoteDeckId);
|
bool loadFromRemote(const QString &nativeString, int remoteDeckId);
|
||||||
bool saveToFile(const QString &fileName, FileFormat fmt);
|
bool saveToFile(const QString &fileName, FileFormat fmt);
|
||||||
bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt);
|
bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue