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

View File

@ -1,5 +1,5 @@
/* 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
* License, v. 2.0. If a copy of the MPL was not distributed with this
@ -8,8 +8,9 @@
#include <memory>
#include <QAtomicInteger>
#include <QHash>
#include <QList>
#include <QMap>
#include <QStackedWidget>
#include <mgba/core/library.h>
@ -28,40 +29,18 @@ enum class LibraryStyle {
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 {
public:
virtual LibraryEntryRef selectedEntry() = 0;
virtual void selectEntry(LibraryEntryRef game) = 0;
virtual mLibraryEntry* selectedEntry() = 0;
virtual void selectEntry(mLibraryEntry* game) = 0;
virtual void setViewStyle(LibraryStyle newStyle) = 0;
virtual void addEntry(LibraryEntryRef item) = 0;
virtual void addEntries(QList<LibraryEntryRef> items);
virtual void addEntry(mLibraryEntry* item) = 0;
virtual void addEntries(QList<mLibraryEntry*> items);
virtual void removeEntry(LibraryEntryRef item) = 0;
virtual void removeEntries(QList<LibraryEntryRef> items);
virtual void removeEntry(mLibraryEntry* item) = 0;
virtual void removeEntries(QList<mLibraryEntry*> items);
virtual QWidget* widget() = 0;
};
@ -77,8 +56,8 @@ public:
LibraryStyle viewStyle() const { return m_currentStyle; }
void setViewStyle(LibraryStyle newStyle);
void selectEntry(LibraryEntryRef entry);
LibraryEntryRef selectedEntry();
void selectEntry(mLibraryEntry* entry);
mLibraryEntry* selectedEntry();
VFile* selectedVFile();
QPair<QString, QString> selectedPath();
@ -102,9 +81,9 @@ private:
ConfigController* m_config = nullptr;
std::shared_ptr<mLibrary> m_library;
qint64 m_libraryJob = -1;
QAtomicInteger<qint64> m_libraryJob = -1;
mLibraryListing m_listing;
QMap<QString, LibraryEntryRef> m_entries;
QHash<QString, mLibraryEntry*> m_entries;
LibraryStyle m_currentStyle;
AbstractGameList* m_currentList = nullptr;

View File

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

View File

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

View File

@ -74,15 +74,15 @@ void LibraryTree::resizeAllCols() {
}
}
LibraryEntryRef LibraryTree::selectedEntry() {
mLibraryEntry* LibraryTree::selectedEntry() {
if (!m_widget->selectedItems().empty()) {
return m_items.key(m_widget->selectedItems().at(0));
} else {
return LibraryEntryRef();
return nullptr;
}
}
void LibraryTree::selectEntry(LibraryEntryRef game) {
void LibraryTree::selectEntry(mLibraryEntry* game) {
if (!game) {
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;
AbstractGameList::addEntries(items);
m_deferredTreeRebuild = false;
rebuildTree();
}
void LibraryTree::addEntry(LibraryEntryRef item) {
void LibraryTree::addEntry(mLibraryEntry* item) {
if (m_items.contains(item)) {
return;
}
QString folder = item->base();
QString folder = item->base;
if (!m_pathNodes.contains(folder)) {
QTreeWidgetItem* i = new TreeWidgetItem;
i->setText(0, folder.section("/", -1));
@ -127,18 +127,18 @@ void LibraryTree::addEntry(LibraryEntryRef item) {
}
TreeWidgetItem* i = new TreeWidgetItem;
i->setText(COL_NAME, item->displayTitle());
i->setText(COL_LOCATION, QDir::toNativeSeparators(item->base()));
i->setText(COL_PLATFORM, nicePlatformFormat(item->platform()));
i->setFilesize(item->filesize());
i->setText(COL_NAME, item->title ? item->title : item->filename);
i->setText(COL_LOCATION, QDir::toNativeSeparators(item->base));
i->setText(COL_PLATFORM, nicePlatformFormat(item->platform));
i->setFilesize(item->filesize);
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);
rebuildTree();
}
void LibraryTree::removeEntry(LibraryEntryRef item) {
void LibraryTree::removeEntry(mLibraryEntry* item) {
if (!m_items.contains(item)) {
return;
}
@ -150,7 +150,7 @@ void LibraryTree::rebuildTree() {
return;
}
LibraryEntryRef currentGame = selectedEntry();
mLibraryEntry* currentGame = selectedEntry();
int count = m_widget->topLevelItemCount();
for (int a = count - 1; a >= 0; --a) {
@ -166,7 +166,7 @@ void LibraryTree::rebuildTree() {
m_widget->addTopLevelItem(i);
}
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 {
for (QTreeWidgetItem* i : m_items.values()) {

View File

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