Add loadFromFileAsync to deckLoader and connect VisualDeckStorageWidget to it. (#5456)
Some checks are pending
Build Desktop / Configure (push) Waiting to run
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Debian, DEB, 12) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Debian, DEB, skip, 11) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Fedora, RPM, 41) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Fedora, RPM, skip, 40) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, 24.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, skip, 20.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, skip, 22.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (yes, Arch, skip) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (3, 1, macos-14, Apple, 14, Release, 15.4) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (3, 1, macos-15, Apple, 15, Release, 16.2) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (3, macos-15, Apple, 15, Debug, 16.2) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (4, 1, macos-13, Intel, 13, Release, 14.3.1) (push) Blocked by required conditions
Build Desktop / Windows ${{matrix.target}} (msvc2019_64, 5.15.*, 7) (push) Blocked by required conditions
Build Desktop / Windows ${{matrix.target}} (msvc2019_64, qtimageformats qtmultimedia qtwebsockets, 6.6.*, 10) (push) Blocked by required conditions

* 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:
BruebachL 2025-01-14 04:02:33 +01:00 committed by GitHub
parent 6072df3522
commit 497e4f1be0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 77 additions and 15 deletions

View File

@ -116,7 +116,7 @@ QChar DeckPreviewColorCircleWidget::getColorChar() const
return colorChar;
}
DeckPreviewColorIdentityWidget::DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent)
DeckPreviewColorIdentityWidget::DeckPreviewColorIdentityWidget(QWidget *parent, const QString &colorIdentity)
: QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout(this);

View File

@ -11,7 +11,7 @@ class DeckPreviewColorCircleWidget : public QWidget
Q_OBJECT
public:
explicit DeckPreviewColorCircleWidget(QChar color, QWidget *parent = nullptr);
explicit DeckPreviewColorCircleWidget(QChar color, QWidget *parent);
void setColorActive(bool active);
QChar getColorChar() const;
@ -33,7 +33,7 @@ class DeckPreviewColorIdentityWidget : public QWidget
Q_OBJECT
public:
explicit DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent = nullptr);
explicit DeckPreviewColorIdentityWidget(QWidget *parent, const QString &colorIdentity);
protected:
void resizeEvent(QResizeEvent *event) override;

View File

@ -16,12 +16,8 @@ DeckPreviewWidget::DeckPreviewWidget(VisualDeckStorageWidget *_parent, const QSt
setLayout(layout);
deckLoader = new DeckLoader();
deckLoader->loadFromFile(filePath, DeckLoader::CockatriceFormat);
auto bannerCard = deckLoader->getBannerCard().first.isEmpty()
? CardInfoPtr()
: CardDatabaseManager::getInstance()->getCardByNameAndProviderId(
deckLoader->getBannerCard().first, deckLoader->getBannerCard().second);
connect(deckLoader, &DeckLoader::loadFinished, this, &DeckPreviewWidget::initializeUi);
deckLoader->loadFromFileAsync(filePath, DeckLoader::CockatriceFormat, false);
bannerCardDisplayWidget = new DeckPreviewCardPictureWidget(this);
@ -30,16 +26,28 @@ DeckPreviewWidget::DeckPreviewWidget(VisualDeckStorageWidget *_parent, const QSt
connect(bannerCardDisplayWidget, &DeckPreviewCardPictureWidget::imageDoubleClicked, this,
&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->setOverlayText(
deckLoader->getName().isEmpty() ? QFileInfo(deckLoader->getLastFileName()).fileName() : deckLoader->getName());
bannerCardDisplayWidget->setFontSize(24);
setFilePath(deckLoader->getLastFileName());
colorIdentityWidget = new DeckPreviewColorIdentityWidget(getColorIdentity());
colorIdentityWidget = new DeckPreviewColorIdentityWidget(this, getColorIdentity());
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader);
layout->addWidget(bannerCardDisplayWidget);
layout->addWidget(colorIdentityWidget);
layout->addWidget(deckTagsDisplayWidget);
}

View File

@ -35,6 +35,7 @@ public slots:
void setFilePath(const QString &filePath);
void imageClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
void imageDoubleClickedEvent(QMouseEvent *event, DeckPreviewCardPictureWidget *instance);
void initializeUi(bool deckLoadSuccess);
};
#endif // DECK_PREVIEW_WIDGET_H

View File

@ -8,11 +8,10 @@
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QFutureWatcher>
#include <QRegularExpression>
#include <QStringList>
#include <qloggingcategory.h>
Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
#include <QtConcurrentRun>
const QStringList DeckLoader::fileNameFilters = QStringList()
<< 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;
}
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 result = loadFromString_Native(nativeString);

View File

@ -3,11 +3,16 @@
#include "decklist.h"
class DeckLoader : public DeckList
#include <QLoggingCategory>
inline Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader")
class DeckLoader : public DeckList
{
Q_OBJECT
signals:
void deckLoaded();
void loadFinished(bool success);
public:
enum FileFormat
@ -43,6 +48,7 @@ public:
static FileFormat getFormatFromName(const QString &fileName);
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 saveToFile(const QString &fileName, FileFormat fmt);
bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt);