Qt: Start cleaning up library code

This commit is contained in:
Vicki Pfau 2021-04-24 23:58:43 -07:00
parent 967cc0886e
commit 73a1416565
6 changed files with 71 additions and 96 deletions

View File

@ -13,20 +13,14 @@
using namespace QGBA; using namespace QGBA;
LibraryEntry::LibraryEntry(mLibraryEntry* entry) void AbstractGameList::addEntries(QList<mLibraryEntry*> items) {
: entry(entry) for (auto item : items) {
, m_fullpath(QString("%1/%2").arg(entry->base, entry->filename)) addEntry(item);
{
}
void AbstractGameList::addEntries(QList<LibraryEntryRef> items) {
for (LibraryEntryRef o : items) {
addEntry(o);
} }
} }
void AbstractGameList::removeEntries(QList<LibraryEntryRef> items) { void AbstractGameList::removeEntries(QList<mLibraryEntry*> items) {
for (LibraryEntryRef o : items) { for (auto item : items) {
removeEntry(o); removeEntry(item);
} }
} }
@ -80,39 +74,42 @@ void LibraryController::setViewStyle(LibraryStyle newStyle) {
m_currentList = newCurrentList; m_currentList = newCurrentList;
} }
void LibraryController::selectEntry(LibraryEntryRef entry) { void LibraryController::selectEntry(mLibraryEntry* entry) {
if (!m_currentList) { if (!m_currentList) {
return; return;
} }
m_currentList->selectEntry(entry); m_currentList->selectEntry(entry);
} }
LibraryEntryRef LibraryController::selectedEntry() { mLibraryEntry* LibraryController::selectedEntry() {
if (!m_currentList) { if (!m_currentList) {
return LibraryEntryRef(); return nullptr;
} }
return m_currentList->selectedEntry(); return m_currentList->selectedEntry();
} }
VFile* LibraryController::selectedVFile() { VFile* LibraryController::selectedVFile() {
LibraryEntryRef entry = selectedEntry(); mLibraryEntry* entry = selectedEntry();
if (entry) { if (entry) {
return mLibraryOpenVFile(m_library.get(), entry->entry); return mLibraryOpenVFile(m_library.get(), entry);
} else { } else {
return nullptr; return nullptr;
} }
} }
QPair<QString, QString> LibraryController::selectedPath() { QPair<QString, QString> LibraryController::selectedPath() {
LibraryEntryRef e = selectedEntry(); mLibraryEntry* entry = selectedEntry();
return e ? qMakePair(e->base(), e->filename()) : qMakePair<QString, QString>("", ""); if (entry) {
return qMakePair(QString(entry->base), QString(entry->filename));
} else {
return qMakePair(QString(), QString());
}
} }
void LibraryController::addDirectory(const QString& dir, bool recursive) { void LibraryController::addDirectory(const QString& dir, bool recursive) {
// The worker thread temporarily owns the library // The worker thread temporarily owns the library
std::shared_ptr<mLibrary> library = m_library; std::shared_ptr<mLibrary> library = m_library;
m_libraryJob = GBAApp::app()->submitWorkerJob(std::bind(&LibraryController::loadDirectory, this, dir, recursive), this, [this, library]() { m_libraryJob = GBAApp::app()->submitWorkerJob(std::bind(&LibraryController::loadDirectory, this, dir, recursive), this, [this, library]() {
m_libraryJob = -1;
refresh(); refresh();
}); });
} }
@ -133,26 +130,23 @@ void LibraryController::refresh() {
setDisabled(true); setDisabled(true);
QStringList allEntries; QSet<QString> allEntries;
QList<LibraryEntryRef> newEntries; QList<mLibraryEntry*> newEntries;
freeLibrary(); freeLibrary();
mLibraryGetEntries(m_library.get(), &m_listing, 0, 0, nullptr); mLibraryGetEntries(m_library.get(), &m_listing, 0, 0, nullptr);
for (size_t i = 0; i < mLibraryListingSize(&m_listing); i++) { for (size_t i = 0; i < mLibraryListingSize(&m_listing); i++) {
mLibraryEntry* entry = mLibraryListingGetPointer(&m_listing, i); mLibraryEntry* entry = mLibraryListingGetPointer(&m_listing, i);
QString fullpath = QString("%1/%2").arg(entry->base, entry->filename); QString fullpath = QString("%1/%2").arg(entry->base, entry->filename);
if (m_entries.contains(fullpath)) { if (!m_entries.contains(fullpath)) {
m_entries.value(fullpath)->entry = entry; newEntries.append(entry);
} else {
LibraryEntryRef libentry = std::make_shared<LibraryEntry>(entry);
m_entries.insert(fullpath, libentry);
newEntries.append(libentry);
} }
allEntries.append(fullpath); m_entries[fullpath] = entry;
allEntries.insert(fullpath);
} }
// Check for entries that were removed // Check for entries that were removed
QList<LibraryEntryRef> removedEntries; QList<mLibraryEntry*> removedEntries;
for (QString& path : m_entries.keys()) { for (QString& path : m_entries.keys()) {
if (!allEntries.contains(path)) { if (!allEntries.contains(path)) {
removedEntries.append(m_entries.value(path)); removedEntries.append(m_entries.value(path));
@ -184,7 +178,9 @@ void LibraryController::selectLastBootedGame() {
void LibraryController::loadDirectory(const QString& dir, bool recursive) { void LibraryController::loadDirectory(const QString& dir, bool recursive) {
// This class can get deleted during this function (sigh) so we need to hold onto this // This class can get deleted during this function (sigh) so we need to hold onto this
std::shared_ptr<mLibrary> library = m_library; std::shared_ptr<mLibrary> library = m_library;
qint64 libraryJob = m_libraryJob;
mLibraryLoadDirectory(library.get(), dir.toUtf8().constData(), recursive); mLibraryLoadDirectory(library.get(), dir.toUtf8().constData(), recursive);
m_libraryJob.testAndSetOrdered(libraryJob, -1);
} }
void LibraryController::freeLibrary() { void LibraryController::freeLibrary() {

View File

@ -1,5 +1,5 @@
/* Copyright (c) 2014-2017 waddlesplash /* Copyright (c) 2014-2017 waddlesplash
* Copyright (c) 2014-2020 Jeffrey Pfau * Copyright (c) 2014-2021 Jeffrey Pfau
* *
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -8,8 +8,9 @@
#include <memory> #include <memory>
#include <QAtomicInteger>
#include <QHash>
#include <QList> #include <QList>
#include <QMap>
#include <QStackedWidget> #include <QStackedWidget>
#include <mgba/core/library.h> #include <mgba/core/library.h>
@ -28,40 +29,18 @@ enum class LibraryStyle {
STYLE_ICON STYLE_ICON
}; };
class LibraryEntry final {
public:
LibraryEntry(mLibraryEntry* entry);
QString displayTitle() const { return title().isNull() ? filename() : title(); }
QString base() const { return QString(entry->base); }
QString filename() const { return QString(entry->filename); }
QString fullpath() const { return m_fullpath; }
QString title() const { return QString(entry->title); }
QByteArray internalTitle() const { return QByteArray(entry->internalTitle); }
QByteArray internalCode() const { return QByteArray(entry->internalCode); }
mPlatform platform() const { return entry->platform; }
size_t filesize() const { return entry->filesize; }
uint32_t crc32() const { return entry->crc32; }
const mLibraryEntry* entry;
private:
const QString m_fullpath;
};
typedef std::shared_ptr<LibraryEntry> LibraryEntryRef;
class AbstractGameList { class AbstractGameList {
public: public:
virtual LibraryEntryRef selectedEntry() = 0; virtual mLibraryEntry* selectedEntry() = 0;
virtual void selectEntry(LibraryEntryRef game) = 0; virtual void selectEntry(mLibraryEntry* game) = 0;
virtual void setViewStyle(LibraryStyle newStyle) = 0; virtual void setViewStyle(LibraryStyle newStyle) = 0;
virtual void addEntry(LibraryEntryRef item) = 0; virtual void addEntry(mLibraryEntry* item) = 0;
virtual void addEntries(QList<LibraryEntryRef> items); virtual void addEntries(QList<mLibraryEntry*> items);
virtual void removeEntry(LibraryEntryRef item) = 0; virtual void removeEntry(mLibraryEntry* item) = 0;
virtual void removeEntries(QList<LibraryEntryRef> items); virtual void removeEntries(QList<mLibraryEntry*> items);
virtual QWidget* widget() = 0; virtual QWidget* widget() = 0;
}; };
@ -77,8 +56,8 @@ public:
LibraryStyle viewStyle() const { return m_currentStyle; } LibraryStyle viewStyle() const { return m_currentStyle; }
void setViewStyle(LibraryStyle newStyle); void setViewStyle(LibraryStyle newStyle);
void selectEntry(LibraryEntryRef entry); void selectEntry(mLibraryEntry* entry);
LibraryEntryRef selectedEntry(); mLibraryEntry* selectedEntry();
VFile* selectedVFile(); VFile* selectedVFile();
QPair<QString, QString> selectedPath(); QPair<QString, QString> selectedPath();
@ -102,9 +81,9 @@ private:
ConfigController* m_config = nullptr; ConfigController* m_config = nullptr;
std::shared_ptr<mLibrary> m_library; std::shared_ptr<mLibrary> m_library;
qint64 m_libraryJob = -1; QAtomicInteger<qint64> m_libraryJob = -1;
mLibraryListing m_listing; mLibraryListing m_listing;
QMap<QString, LibraryEntryRef> m_entries; QHash<QString, mLibraryEntry*> m_entries;
LibraryStyle m_currentStyle; LibraryStyle m_currentStyle;
AbstractGameList* m_currentList = nullptr; AbstractGameList* m_currentList = nullptr;

View File

@ -23,15 +23,15 @@ LibraryGrid::~LibraryGrid() {
delete m_widget; delete m_widget;
} }
LibraryEntryRef LibraryGrid::selectedEntry() { mLibraryEntry* LibraryGrid::selectedEntry() {
if (!m_widget->selectedItems().empty()) { if (!m_widget->selectedItems().empty()) {
return m_items.key(m_widget->selectedItems().at(0)); return m_items.key(m_widget->selectedItems().at(0));
} else { } else {
return LibraryEntryRef(); return nullptr;
} }
} }
void LibraryGrid::selectEntry(LibraryEntryRef game) { void LibraryGrid::selectEntry(mLibraryEntry* game) {
if (!game) { if (!game) {
return; return;
} }
@ -56,19 +56,19 @@ void LibraryGrid::setViewStyle(LibraryStyle newStyle) {
m_widget->setDragEnabled(false); m_widget->setDragEnabled(false);
} }
void LibraryGrid::addEntry(LibraryEntryRef item) { void LibraryGrid::addEntry(mLibraryEntry* item) {
if (m_items.contains(item)) { if (m_items.contains(item)) {
return; return;
} }
QListWidgetItem* i = new QListWidgetItem; QListWidgetItem* i = new QListWidgetItem;
i->setText(item->displayTitle()); i->setText(item->title ? item->title : item->filename);
m_widget->addItem(i); m_widget->addItem(i);
m_items.insert(item, i); m_items.insert(item, i);
} }
void LibraryGrid::removeEntry(LibraryEntryRef entry) { void LibraryGrid::removeEntry(mLibraryEntry* entry) {
if (!m_items.contains(entry)) { if (!m_items.contains(entry)) {
return; return;
} }

View File

@ -17,13 +17,13 @@ public:
~LibraryGrid(); ~LibraryGrid();
// AbstractGameList stuff // AbstractGameList stuff
virtual LibraryEntryRef selectedEntry() override; virtual mLibraryEntry* selectedEntry() override;
virtual void selectEntry(LibraryEntryRef game) override; virtual void selectEntry(mLibraryEntry* game) override;
virtual void setViewStyle(LibraryStyle newStyle) override; virtual void setViewStyle(LibraryStyle newStyle) override;
virtual void addEntry(LibraryEntryRef item) override; virtual void addEntry(mLibraryEntry* item) override;
virtual void removeEntry(LibraryEntryRef entry) override; virtual void removeEntry(mLibraryEntry* entry) override;
virtual QWidget* widget() override { return m_widget; } virtual QWidget* widget() override { return m_widget; }
@ -40,7 +40,7 @@ private:
const quint32 ICON_BANNER_WIDTH = 64; const quint32 ICON_BANNER_WIDTH = 64;
const quint32 ICON_BANNER_HEIGHT = 64; const quint32 ICON_BANNER_HEIGHT = 64;
QMap<LibraryEntryRef, QListWidgetItem*> m_items; QHash<mLibraryEntry*, QListWidgetItem*> m_items;
LibraryStyle m_currentStyle; LibraryStyle m_currentStyle;
}; };

View File

@ -74,15 +74,15 @@ void LibraryTree::resizeAllCols() {
} }
} }
LibraryEntryRef LibraryTree::selectedEntry() { mLibraryEntry* LibraryTree::selectedEntry() {
if (!m_widget->selectedItems().empty()) { if (!m_widget->selectedItems().empty()) {
return m_items.key(m_widget->selectedItems().at(0)); return m_items.key(m_widget->selectedItems().at(0));
} else { } else {
return LibraryEntryRef(); return nullptr;
} }
} }
void LibraryTree::selectEntry(LibraryEntryRef game) { void LibraryTree::selectEntry(mLibraryEntry* game) {
if (!game) { if (!game) {
return; return;
} }
@ -104,19 +104,19 @@ void LibraryTree::setViewStyle(LibraryStyle newStyle) {
} }
} }
void LibraryTree::addEntries(QList<LibraryEntryRef> items) { void LibraryTree::addEntries(QList<mLibraryEntry*> items) {
m_deferredTreeRebuild = true; m_deferredTreeRebuild = true;
AbstractGameList::addEntries(items); AbstractGameList::addEntries(items);
m_deferredTreeRebuild = false; m_deferredTreeRebuild = false;
rebuildTree(); rebuildTree();
} }
void LibraryTree::addEntry(LibraryEntryRef item) { void LibraryTree::addEntry(mLibraryEntry* item) {
if (m_items.contains(item)) { if (m_items.contains(item)) {
return; return;
} }
QString folder = item->base(); QString folder = item->base;
if (!m_pathNodes.contains(folder)) { if (!m_pathNodes.contains(folder)) {
QTreeWidgetItem* i = new TreeWidgetItem; QTreeWidgetItem* i = new TreeWidgetItem;
i->setText(0, folder.section("/", -1)); i->setText(0, folder.section("/", -1));
@ -127,18 +127,18 @@ void LibraryTree::addEntry(LibraryEntryRef item) {
} }
TreeWidgetItem* i = new TreeWidgetItem; TreeWidgetItem* i = new TreeWidgetItem;
i->setText(COL_NAME, item->displayTitle()); i->setText(COL_NAME, item->title ? item->title : item->filename);
i->setText(COL_LOCATION, QDir::toNativeSeparators(item->base())); i->setText(COL_LOCATION, QDir::toNativeSeparators(item->base));
i->setText(COL_PLATFORM, nicePlatformFormat(item->platform())); i->setText(COL_PLATFORM, nicePlatformFormat(item->platform));
i->setFilesize(item->filesize()); i->setFilesize(item->filesize);
i->setTextAlignment(COL_SIZE, Qt::AlignRight); i->setTextAlignment(COL_SIZE, Qt::AlignRight);
i->setText(COL_CRC32, QString("%0").arg(item->crc32(), 8, 16, QChar('0'))); i->setText(COL_CRC32, QString("%0").arg(item->crc32, 8, 16, QChar('0')));
m_items.insert(item, i); m_items.insert(item, i);
rebuildTree(); rebuildTree();
} }
void LibraryTree::removeEntry(LibraryEntryRef item) { void LibraryTree::removeEntry(mLibraryEntry* item) {
if (!m_items.contains(item)) { if (!m_items.contains(item)) {
return; return;
} }
@ -150,7 +150,7 @@ void LibraryTree::rebuildTree() {
return; return;
} }
LibraryEntryRef currentGame = selectedEntry(); mLibraryEntry* currentGame = selectedEntry();
int count = m_widget->topLevelItemCount(); int count = m_widget->topLevelItemCount();
for (int a = count - 1; a >= 0; --a) { for (int a = count - 1; a >= 0; --a) {
@ -166,7 +166,7 @@ void LibraryTree::rebuildTree() {
m_widget->addTopLevelItem(i); m_widget->addTopLevelItem(i);
} }
for (QTreeWidgetItem* i : m_items.values()) { for (QTreeWidgetItem* i : m_items.values()) {
m_pathNodes.value(m_items.key(i)->base())->addChild(i); m_pathNodes.value(m_items.key(i)->base)->addChild(i);
} }
} else { } else {
for (QTreeWidgetItem* i : m_items.values()) { for (QTreeWidgetItem* i : m_items.values()) {

View File

@ -26,14 +26,14 @@ public:
~LibraryTree(); ~LibraryTree();
// AbstractGameList stuff // AbstractGameList stuff
virtual LibraryEntryRef selectedEntry() override; virtual mLibraryEntry* selectedEntry() override;
virtual void selectEntry(LibraryEntryRef game) override; virtual void selectEntry(mLibraryEntry* game) override;
virtual void setViewStyle(LibraryStyle newStyle) override; virtual void setViewStyle(LibraryStyle newStyle) override;
virtual void addEntries(QList<LibraryEntryRef> items) override; virtual void addEntries(QList<mLibraryEntry*> items) override;
virtual void addEntry(LibraryEntryRef item) override; virtual void addEntry(mLibraryEntry* item) override;
virtual void removeEntry(LibraryEntryRef item) override; virtual void removeEntry(mLibraryEntry* item) override;
virtual QWidget* widget() override { return m_widget; } virtual QWidget* widget() override { return m_widget; }
@ -44,8 +44,8 @@ private:
LibraryController* m_controller; LibraryController* m_controller;
bool m_deferredTreeRebuild = false; bool m_deferredTreeRebuild = false;
QMap<LibraryEntryRef, QTreeWidgetItem*> m_items; QHash<mLibraryEntry*, QTreeWidgetItem*> m_items;
QMap<QString, QTreeWidgetItem*> m_pathNodes; QHash<QString, QTreeWidgetItem*> m_pathNodes;
void rebuildTree(); void rebuildTree();
void resizeAllCols(); void resizeAllCols();