mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-01-13 13:37:36 -08: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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user