Skip to content

Commit

Permalink
style nits and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ahigerd committed Jun 30, 2022
1 parent e98a572 commit 59a39da
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 77 deletions.
8 changes: 5 additions & 3 deletions src/platform/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ if(USE_SQLITE3)
list(APPEND SOURCE_FILES
ArchiveInspector.cpp
library/LibraryController.cpp
library/LibraryEntry.cpp
library/LibraryModel.cpp)
endif()

Expand Down Expand Up @@ -486,11 +487,12 @@ if(BUILD_SUITE)
find_package(${QT}Test)
if(${QT}Test_FOUND)
set(TEST_SRC
test/main.cpp
test/library.cpp
test/spanset.cpp
utils.cpp
library/LibraryEntry.cpp
library/LibraryModel.cpp
test/library.cpp
test/main.cpp
test/spanset.cpp
)
add_executable(test-platform-qt WIN32 ${TEST_SRC})
target_link_libraries(test-platform-qt ${PLATFORM_LIBRARY} ${BINARY_NAME} ${QT_LIBRARIES} ${QT}::Test)
Expand Down
31 changes: 3 additions & 28 deletions src/platform/qt/library/LibraryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,6 @@

using namespace QGBA;

LibraryEntry::LibraryEntry(const mLibraryEntry* entry)
: base(entry->base)
, filename(entry->filename)
, fullpath(QString("%1/%2").arg(entry->base, entry->filename))
, title(entry->title)
, internalTitle(entry->internalTitle)
, internalCode(entry->internalCode)
, platform(entry->platform)
, filesize(entry->filesize)
, crc32(entry->crc32)
{
}

void AbstractGameList::addEntry(const LibraryEntry& item) {
addEntries({item});
}

void AbstractGameList::updateEntry(const LibraryEntry& item) {
updateEntries({item});
}

void AbstractGameList::removeEntry(const QString& item) {
removeEntries({item});
}

LibraryController::LibraryController(QWidget* parent, const QString& path, ConfigController* config)
: QStackedWidget(parent)
, m_config(config)
Expand Down Expand Up @@ -96,10 +71,10 @@ LibraryController::LibraryController(QWidget* parent, const QString& path, Confi

QVariant librarySort = m_config->getQtOption("librarySort");
QVariant librarySortOrder = m_config->getQtOption("librarySortOrder");
if (librarySort.isNull()) {
if (librarySort.isNull() || !librarySort.canConvert<int>()) {
librarySort = 0;
}
if (librarySortOrder.isNull()) {
if (librarySortOrder.isNull() || !librarySortOrder.canConvert<Qt::SortOrder>()) {
librarySortOrder = Qt::AscendingOrder;
}
m_treeModel->sort(librarySort.toInt(), librarySortOrder.value<Qt::SortOrder>());
Expand Down Expand Up @@ -219,7 +194,7 @@ void LibraryController::refresh() {
mLibraryGetEntries(m_library.get(), &listing, 0, 0, nullptr);
for (size_t i = 0; i < mLibraryListingSize(&listing); i++) {
const mLibraryEntry* entry = mLibraryListingGetConstPointer(&listing, i);
uint64_t checkHash = LibraryEntry::checkHash(entry->filesize, entry->crc32);
uint64_t checkHash = LibraryEntry::checkHash(entry);
QString fullpath = QStringLiteral("%1/%2").arg(entry->base, entry->filename);
if (!m_knownGames.contains(fullpath)) {
newEntries.append(entry);
Expand Down
45 changes: 2 additions & 43 deletions src/platform/qt/library/LibraryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <mgba/core/library.h>

#include "LibraryEntry.h"

class QAbstractItemView;
class QListView;
class QSortFilterProxyModel;
Expand All @@ -33,49 +35,6 @@ enum class LibraryStyle {
STYLE_ICON
};

struct LibraryEntry {
LibraryEntry() {}
LibraryEntry(const mLibraryEntry* entry);
LibraryEntry(const LibraryEntry&) = default;
LibraryEntry(LibraryEntry&&) = default;

bool isNull() const { return fullpath.isNull(); }

QString displayTitle() const { return title.isNull() ? filename : title; }

QString base;
QString filename;
QString fullpath;
QString title;
QByteArray internalTitle;
QByteArray internalCode;
mPlatform platform;
size_t filesize;
uint32_t crc32;

LibraryEntry& operator=(const LibraryEntry&) = default;
LibraryEntry& operator=(LibraryEntry&&) = default;
bool operator==(const LibraryEntry& other) const { return other.fullpath == fullpath; }

static inline uint64_t checkHash(size_t filesize, uint32_t crc32) {
return (uint64_t(filesize) << 32) ^ ((crc32 + 1ULL) * (uint32_t(filesize) + 1ULL));
}
inline uint64_t checkHash() const { return checkHash(filesize, crc32); }
};

class AbstractGameList {
public:
virtual void resetEntries(const QList<LibraryEntry>&) = 0;
virtual void addEntries(const QList<LibraryEntry>&) = 0;
virtual void updateEntries(const QList<LibraryEntry>&) = 0;
virtual void removeEntries(const QList<QString>&) = 0;

virtual void addEntry(const LibraryEntry&);
virtual void updateEntry(const LibraryEntry&);
virtual void removeEntry(const QString&);
virtual void setShowFilename(bool showFilename) = 0;
};

class LibraryController final : public QStackedWidget {
Q_OBJECT

Expand Down
51 changes: 51 additions & 0 deletions src/platform/qt/library/LibraryEntry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (c) 2014-2017 waddlesplash
* Copyright (c) 2013-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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LibraryEntry.h"

#include <mgba/core/library.h>

using namespace QGBA;

static inline uint64_t checkHash(size_t filesize, uint32_t crc32) {
return (uint64_t(filesize) << 32) ^ ((crc32 + 1ULL) * (uint32_t(filesize) + 1ULL));
}

LibraryEntry::LibraryEntry(const mLibraryEntry* entry)
: base(entry->base)
, filename(entry->filename)
, fullpath(QString("%1/%2").arg(entry->base, entry->filename))
, title(entry->title)
, internalTitle(entry->internalTitle)
, internalCode(entry->internalCode)
, platform(entry->platform)
, filesize(entry->filesize)
, crc32(entry->crc32)
{
}

bool LibraryEntry::isNull() const {
return fullpath.isNull();
}

QString LibraryEntry::displayTitle() const {
if (title.isNull()) {
return filename;
}
return title;
}

bool LibraryEntry::operator==(const LibraryEntry& other) const {
return other.fullpath == fullpath;
}

uint64_t LibraryEntry::checkHash() const {
return ::checkHash(filesize, crc32);
}

uint64_t LibraryEntry::checkHash(const mLibraryEntry* entry) {
return ::checkHash(entry->filesize, entry->crc32);
}
47 changes: 47 additions & 0 deletions src/platform/qt/library/LibraryEntry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2014-2017 waddlesplash
* Copyright (c) 2013-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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once

#include <QByteArray>
#include <QList>
#include <QString>

#include <mgba/core/core.h>

struct mLibraryEntry;

namespace QGBA {

struct LibraryEntry {
LibraryEntry() = default;
LibraryEntry(const LibraryEntry&) = default;
LibraryEntry(LibraryEntry&&) = default;
LibraryEntry(const mLibraryEntry* entry);

bool isNull() const;

QString displayTitle() const;

QString base;
QString filename;
QString fullpath;
QString title;
QByteArray internalTitle;
QByteArray internalCode;
mPlatform platform;
size_t filesize;
uint32_t crc32;

LibraryEntry& operator=(const LibraryEntry&) = default;
LibraryEntry& operator=(LibraryEntry&&) = default;
bool operator==(const LibraryEntry& other) const;

uint64_t checkHash() const;
static uint64_t checkHash(const mLibraryEntry* entry);
};

};
2 changes: 1 addition & 1 deletion src/platform/qt/library/LibraryModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <mgba/core/library.h>

#include "LibraryController.h"
#include "LibraryEntry.h"

class QTreeView;

Expand Down
4 changes: 2 additions & 2 deletions src/platform/qt/test/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <QSignalSpy>

#define FIND_GBA_ROW(gba, gb) \
int gba = findGbaRow(); \
int gba = findGBARow(); \
if (gba < 0) QFAIL("Could not find gba row"); \
int gb = 1 - gba;

Expand All @@ -25,7 +25,7 @@ Q_OBJECT
private:
LibraryModel* model = nullptr;

int findGbaRow() {
int findGBARow() {
for (int i = 0; i < model->rowCount(); i++) {
if (model->index(i, 0).data() == "gba") {
return i;
Expand Down

0 comments on commit 59a39da

Please sign in to comment.