mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 17:14:52 -07:00
Add loadFromFileAsync to deckLoader and connect VisualDeckStorageWidget to it. (#5456)
* Add loadFromFileAsync to deckLoader and connect VisualDeckStorageWidget to it. * Address comments. * Lint. * Unlint something. --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
6072df3522
commit
497e4f1be0
6 changed files with 77 additions and 15 deletions
|
|
@ -116,7 +116,7 @@ QChar DeckPreviewColorCircleWidget::getColorChar() const
|
||||||
return colorChar;
|
return colorChar;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckPreviewColorIdentityWidget::DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent)
|
DeckPreviewColorIdentityWidget::DeckPreviewColorIdentityWidget(QWidget *parent, const QString &colorIdentity)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
QHBoxLayout *layout = new QHBoxLayout(this);
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class DeckPreviewColorCircleWidget : public QWidget
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DeckPreviewColorCircleWidget(QChar color, QWidget *parent = nullptr);
|
explicit DeckPreviewColorCircleWidget(QChar color, QWidget *parent);
|
||||||
|
|
||||||
void setColorActive(bool active);
|
void setColorActive(bool active);
|
||||||
QChar getColorChar() const;
|
QChar getColorChar() const;
|
||||||
|
|
@ -33,7 +33,7 @@ class DeckPreviewColorIdentityWidget : public QWidget
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent = nullptr);
|
explicit DeckPreviewColorIdentityWidget(QWidget *parent, const QString &colorIdentity);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
|
||||||
|
|
@ -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,16 +26,28 @@ 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) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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() ? QFileInfo(deckLoader->getLastFileName()).fileName() : deckLoader->getName());
|
deckLoader->getName().isEmpty() ? 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(this, 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,11 +8,10 @@
|
||||||
#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 <QtConcurrentRun>
|
||||||
|
|
||||||
Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
|
|
||||||
|
|
||||||
const QStringList DeckLoader::fileNameFilters = QStringList()
|
const QStringList DeckLoader::fileNameFilters = QStringList()
|
||||||
<< QObject::tr("Common deck formats (*.cod *.dec *.dek *.txt *.mwDeck)")
|
<< QObject::tr("Common deck formats (*.cod *.dec *.dek *.txt *.mwDeck)")
|
||||||
|
|
@ -80,6 +79,54 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool user
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DeckLoader::loadFromFileAsync(const QString &fileName, FileFormat fmt, bool userRequest)
|
||||||
|
{
|
||||||
|
auto *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;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (fmt) {
|
||||||
|
case PlainTextFormat:
|
||||||
|
return loadFromFile_Plain(&file);
|
||||||
|
case CockatriceFormat: {
|
||||||
|
bool result = false;
|
||||||
|
result = loadFromFile_Native(&file);
|
||||||
|
if (!result) {
|
||||||
|
file.seek(0);
|
||||||
|
return loadFromFile_Plain(&file);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,16 @@
|
||||||
|
|
||||||
#include "decklist.h"
|
#include "decklist.h"
|
||||||
|
|
||||||
class DeckLoader : public DeckList
|
#include <QLoggingCategory>
|
||||||
|
|
||||||
|
inline Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
|
||||||
|
|
||||||
|
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 +48,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