mirror of https://github.com/mgba-emu/mgba.git
Qt: Move library loading to a separate thread
This commit is contained in:
parent
ac2097f0b6
commit
8dde1b5654
|
@ -16,6 +16,9 @@ ArchiveInspector::ArchiveInspector(const QString& filename, QWidget* parent)
|
||||||
, m_model(ConfigController::configDir() + "/library.sqlite3")
|
, m_model(ConfigController::configDir() + "/library.sqlite3")
|
||||||
{
|
{
|
||||||
m_ui.setupUi(this);
|
m_ui.setupUi(this);
|
||||||
|
connect(&m_model, &LibraryModel::doneLoading, [this]() {
|
||||||
|
m_ui.loading->hide();
|
||||||
|
});
|
||||||
m_model.loadDirectory(filename);
|
m_model.loadDirectory(filename);
|
||||||
m_model.constrainBase(filename);
|
m_model.constrainBase(filename);
|
||||||
m_ui.archiveListing->setModel(&m_model);
|
m_ui.archiveListing->setModel(&m_model);
|
||||||
|
|
|
@ -14,14 +14,21 @@
|
||||||
<string>Open in archive...</string>
|
<string>Open in archive...</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="1" column="0">
|
<item row="2" column="1">
|
||||||
<widget class="QDialogButtonBox" name="buttonBox">
|
<widget class="QDialogButtonBox" name="buttonBox">
|
||||||
<property name="standardButtons">
|
<property name="standardButtons">
|
||||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Open</set>
|
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Open</set>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="0">
|
<item row="2" column="0">
|
||||||
|
<widget class="QLabel" name="loading">
|
||||||
|
<property name="text">
|
||||||
|
<string>Loading...</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="0" colspan="2">
|
||||||
<widget class="QListView" name="archiveListing"/>
|
<widget class="QListView" name="archiveListing"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
|
@ -21,17 +21,27 @@ LibraryModel::LibraryModel(const QString& path, QObject* parent)
|
||||||
}
|
}
|
||||||
memset(&m_constraints, 0, sizeof(m_constraints));
|
memset(&m_constraints, 0, sizeof(m_constraints));
|
||||||
m_constraints.platform = PLATFORM_NONE;
|
m_constraints.platform = PLATFORM_NONE;
|
||||||
|
|
||||||
|
if (!m_library) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_loader = new LibraryLoader(m_library);
|
||||||
|
connect(m_loader, SIGNAL(directoryLoaded(const QString&)), this, SLOT(directoryLoaded(const QString&)));
|
||||||
|
m_loader->moveToThread(&m_loaderThread);
|
||||||
|
m_loaderThread.setObjectName("Library Loader Thread");
|
||||||
|
m_loaderThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
LibraryModel::~LibraryModel() {
|
LibraryModel::~LibraryModel() {
|
||||||
clearConstraints();
|
clearConstraints();
|
||||||
mLibraryDestroy(m_library);
|
mLibraryDestroy(m_library);
|
||||||
|
m_loaderThread.quit();
|
||||||
|
m_loaderThread.wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LibraryModel::loadDirectory(const QString& path) {
|
void LibraryModel::loadDirectory(const QString& path) {
|
||||||
beginResetModel();
|
m_queue.append(path);
|
||||||
mLibraryLoadDirectory(m_library, path.toUtf8().constData());
|
QMetaObject::invokeMethod(m_loader, "loadDirectory", Q_ARG(const QString&, path));
|
||||||
endResetModel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LibraryModel::entryAt(int row, mLibraryEntry* out) const {
|
bool LibraryModel::entryAt(int row, mLibraryEntry* out) const {
|
||||||
|
@ -136,3 +146,23 @@ void LibraryModel::clearConstraints() {
|
||||||
}
|
}
|
||||||
memset(&m_constraints, 0, sizeof(m_constraints));
|
memset(&m_constraints, 0, sizeof(m_constraints));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LibraryModel::directoryLoaded(const QString& path) {
|
||||||
|
m_queue.removeOne(path);
|
||||||
|
beginResetModel();
|
||||||
|
endResetModel();
|
||||||
|
if (m_queue.empty()) {
|
||||||
|
emit doneLoading();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LibraryLoader::LibraryLoader(mLibrary* library, QObject* parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_library(library)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void LibraryLoader::loadDirectory(const QString& path) {
|
||||||
|
mLibraryLoadDirectory(m_library, path.toUtf8().constData());
|
||||||
|
emit directoryLoaded(path);
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
#define QGBA_LIBRARY_MODEL
|
#define QGBA_LIBRARY_MODEL
|
||||||
|
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
|
#include <QStringList>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
#include <mgba/core/library.h>
|
#include <mgba/core/library.h>
|
||||||
|
|
||||||
|
@ -15,6 +17,7 @@ struct VFile;
|
||||||
|
|
||||||
namespace QGBA {
|
namespace QGBA {
|
||||||
|
|
||||||
|
class LibraryLoader;
|
||||||
class LibraryModel : public QAbstractItemModel {
|
class LibraryModel : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
@ -22,8 +25,6 @@ public:
|
||||||
LibraryModel(const QString& path, QObject* parent = nullptr);
|
LibraryModel(const QString& path, QObject* parent = nullptr);
|
||||||
virtual ~LibraryModel();
|
virtual ~LibraryModel();
|
||||||
|
|
||||||
void loadDirectory(const QString& path);
|
|
||||||
|
|
||||||
bool entryAt(int row, mLibraryEntry* out) const;
|
bool entryAt(int row, mLibraryEntry* out) const;
|
||||||
VFile* openVFile(const QModelIndex& index) const;
|
VFile* openVFile(const QModelIndex& index) const;
|
||||||
|
|
||||||
|
@ -36,13 +37,40 @@ public:
|
||||||
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void doneLoading();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void loadDirectory(const QString& path);
|
||||||
|
|
||||||
void constrainBase(const QString& path);
|
void constrainBase(const QString& path);
|
||||||
void clearConstraints();
|
void clearConstraints();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void directoryLoaded(const QString& path);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mLibrary* m_library;
|
mLibrary* m_library;
|
||||||
mLibraryEntry m_constraints;
|
mLibraryEntry m_constraints;
|
||||||
|
LibraryLoader* m_loader;
|
||||||
|
QThread m_loaderThread;
|
||||||
|
QStringList m_queue;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LibraryLoader : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
LibraryLoader(mLibrary* library, QObject* parent = nullptr);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void loadDirectory(const QString& path);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void directoryLoaded(const QString& path);
|
||||||
|
|
||||||
|
private:
|
||||||
|
mLibrary* m_library;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue