diff --git a/src/platform/qt/ArchiveInspector.cpp b/src/platform/qt/ArchiveInspector.cpp index e32281e8c..acb887967 100644 --- a/src/platform/qt/ArchiveInspector.cpp +++ b/src/platform/qt/ArchiveInspector.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2016 Jeffrey Pfau +/* Copyright (c) 2013-2017 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 @@ -7,27 +7,19 @@ #include -#include "ConfigController.h" - using namespace QGBA; ArchiveInspector::ArchiveInspector(const QString& filename, QWidget* parent) : QDialog(parent) - , m_model(ConfigController::configDir() + "/library.sqlite3") { m_ui.setupUi(this); - connect(&m_model, &LibraryModel::doneLoading, [this]() { + connect(m_ui.archiveView, &LibraryView::doneLoading, [this]() { m_ui.loading->hide(); }); - m_model.loadDirectory(filename); - m_model.constrainBase(filename); - m_ui.archiveListing->setModel(&m_model); + connect(m_ui.archiveView, SIGNAL(accepted()), this, SIGNAL(accepted())); + m_ui.archiveView->setDirectory(filename); } VFile* ArchiveInspector::selectedVFile() const { - QModelIndex index = m_ui.archiveListing->selectionModel()->currentIndex(); - if (!index.isValid()) { - return nullptr; - } - return m_model.openVFile(index); + return m_ui.archiveView->selectedVFile(); } diff --git a/src/platform/qt/ArchiveInspector.h b/src/platform/qt/ArchiveInspector.h index fc393e535..658a47963 100644 --- a/src/platform/qt/ArchiveInspector.h +++ b/src/platform/qt/ArchiveInspector.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2016 Jeffrey Pfau +/* Copyright (c) 2013-2017 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 @@ -6,8 +6,6 @@ #ifndef QGBA_ARCHIVE_INSPECTOR #define QGBA_ARCHIVE_INSPECTOR -#include "LibraryModel.h" - #include "ui_ArchiveInspector.h" struct VFile; @@ -24,8 +22,6 @@ public: private: Ui::ArchiveInspector m_ui; - - LibraryModel m_model; }; } diff --git a/src/platform/qt/ArchiveInspector.ui b/src/platform/qt/ArchiveInspector.ui index 95edcf864..55e0cbe6e 100644 --- a/src/platform/qt/ArchiveInspector.ui +++ b/src/platform/qt/ArchiveInspector.ui @@ -6,33 +6,41 @@ 0 0 - 400 - 300 + 600 + 400 Open in archive... - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Open - - - - + Loading... + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Open + + + - + + + + QGBA::LibraryView + QWidget +
LibraryView.h
+ 1 +
+
@@ -67,21 +75,5 @@ - - archiveListing - doubleClicked(QModelIndex) - ArchiveInspector - accept() - - - 199 - 129 - - - 199 - 149 - - - diff --git a/src/platform/qt/CMakeLists.txt b/src/platform/qt/CMakeLists.txt index 856cce2e6..bde726ede 100644 --- a/src/platform/qt/CMakeLists.txt +++ b/src/platform/qt/CMakeLists.txt @@ -120,6 +120,7 @@ set(UI_FILES DebuggerConsole.ui GIFView.ui IOViewer.ui + LibraryView.ui LoadSaveState.ui LogView.ui MemoryView.ui @@ -191,7 +192,8 @@ endif() if(USE_SQLITE3) list(APPEND SOURCE_FILES ArchiveInspector.cpp - LibraryModel.cpp) + LibraryModel.cpp + LibraryView.cpp) endif() qt5_add_resources(RESOURCES resources.qrc) diff --git a/src/platform/qt/LibraryModel.cpp b/src/platform/qt/LibraryModel.cpp index fd250324a..c317cc77b 100644 --- a/src/platform/qt/LibraryModel.cpp +++ b/src/platform/qt/LibraryModel.cpp @@ -5,15 +5,61 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "LibraryModel.h" +#include + #include using namespace QGBA; Q_DECLARE_METATYPE(mLibraryEntry); +QMap LibraryModel::s_columns; + LibraryModel::LibraryModel(const QString& path, QObject* parent) : QAbstractItemModel(parent) { + if (s_columns.empty()) { + s_columns["filename"] = { + tr("Filename"), + [](const mLibraryEntry& e) -> QString { + return e.filename; + } + }; + s_columns["size"] = { + tr("Size"), + [](const mLibraryEntry& e) -> QString { + double size = e.filesize; + QString unit = "B"; + if (size > 1024.0) { + size /= 1024.0; + unit = "kiB"; + } + if (size > 1024.0) { + size /= 1024.0; + unit = "MiB"; + } + return QString("%0 %1").arg(size, 0, 'f', 1).arg(unit); + } + }; + s_columns["platform"] = { + tr("Platform"), + [](const mLibraryEntry& e) -> QString { + int platform = e.platform; + switch (platform) { +#ifdef M_CORE_GBA + case PLATFORM_GBA: + return tr("GBA"); +#endif +#ifdef M_CORE_GB + case PLATFORM_GB: + return tr("GB"); +#endif + default: + return tr("?"); + } + } + }; + } if (!path.isNull()) { m_library = mLibraryLoad(path.toUtf8().constData()); } else { @@ -21,6 +67,9 @@ LibraryModel::LibraryModel(const QString& path, QObject* parent) } memset(&m_constraints, 0, sizeof(m_constraints)); m_constraints.platform = PLATFORM_NONE; + m_columns.append(s_columns["filename"]); + m_columns.append(s_columns["platform"]); + m_columns.append(s_columns["size"]); if (!m_library) { return; @@ -75,16 +124,19 @@ QVariant LibraryModel::data(const QModelIndex& index, int role) const { if (role == Qt::UserRole) { return QVariant::fromValue(entry); } - if (role != Qt::DisplayRole) { + if (index.column() >= m_columns.count()) { return QVariant(); } - switch (index.column()) { - case 0: - return entry.filename; - case 1: - return (unsigned long long) entry.filesize; + switch (role) { + case Qt::DisplayRole: + return m_columns[index.column()].value(entry); + case Qt::SizeHintRole: { + QFontMetrics fm((QFont())); + return fm.size(Qt::TextSingleLine, m_columns[index.column()].value(entry)); + } + default: + return QVariant(); } - return QVariant(); } QVariant LibraryModel::headerData(int section, Qt::Orientation orientation, int role) const { @@ -92,12 +144,10 @@ QVariant LibraryModel::headerData(int section, Qt::Orientation orientation, int return QAbstractItemModel::headerData(section, orientation, role); } if (orientation == Qt::Horizontal) { - switch (section) { - case 0: - return tr("Filename"); - case 1: - return tr("Size"); + if (section >= m_columns.count()) { + return QVariant(); } + return m_columns[section].name; } return section; } @@ -117,7 +167,7 @@ int LibraryModel::columnCount(const QModelIndex& parent) const { if (parent.isValid()) { return 0; } - return 2; + return m_columns.count(); } int LibraryModel::rowCount(const QModelIndex& parent) const { diff --git a/src/platform/qt/LibraryModel.h b/src/platform/qt/LibraryModel.h index a1b354b5c..cdeafe61d 100644 --- a/src/platform/qt/LibraryModel.h +++ b/src/platform/qt/LibraryModel.h @@ -50,11 +50,19 @@ private slots: void directoryLoaded(const QString& path); private: + struct LibraryColumn { + QString name; + std::function value; + }; + mLibrary* m_library; mLibraryEntry m_constraints; LibraryLoader* m_loader; QThread m_loaderThread; QStringList m_queue; + + QList m_columns; + static QMap s_columns; }; class LibraryLoader : public QObject { diff --git a/src/platform/qt/LibraryView.cpp b/src/platform/qt/LibraryView.cpp new file mode 100644 index 000000000..1741d9c34 --- /dev/null +++ b/src/platform/qt/LibraryView.cpp @@ -0,0 +1,42 @@ +/* Copyright (c) 2013-2017 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 "LibraryView.h" + +#include + +#include "ConfigController.h" + +using namespace QGBA; + +LibraryView::LibraryView(QWidget* parent) + : QWidget(parent) + , m_model(ConfigController::configDir() + "/library.sqlite3") +{ + m_ui.setupUi(this); + connect(&m_model, SIGNAL(doneLoading()), this, SIGNAL(doneLoading())); + connect(&m_model, SIGNAL(doneLoading()), this, SLOT(resizeColumns())); + connect(m_ui.listing, SIGNAL(activated(const QModelIndex&)), this, SIGNAL(accepted())); + m_ui.listing->horizontalHeader()->setSectionsMovable(true); + m_ui.listing->setModel(&m_model); + resizeColumns(); +} + +void LibraryView::setDirectory(const QString& filename) { + m_model.loadDirectory(filename); + m_model.constrainBase(filename); +} + +VFile* LibraryView::selectedVFile() const { + QModelIndex index = m_ui.listing->selectionModel()->currentIndex(); + if (!index.isValid()) { + return nullptr; + } + return m_model.openVFile(index); +} + +void LibraryView::resizeColumns() { + m_ui.listing->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents); +} diff --git a/src/platform/qt/LibraryView.h b/src/platform/qt/LibraryView.h new file mode 100644 index 000000000..c0591973a --- /dev/null +++ b/src/platform/qt/LibraryView.h @@ -0,0 +1,43 @@ +/* Copyright (c) 2013-2017 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/. */ +#ifndef QGBA_LIBRARY_VIEW +#define QGBA_LIBRARY_VIEW + +#include "LibraryModel.h" + +#include "ui_LibraryView.h" + +struct VFile; + +namespace QGBA { + +class LibraryView : public QWidget { +Q_OBJECT + +public: + LibraryView(QWidget* parent = nullptr); + + VFile* selectedVFile() const; + +signals: + void doneLoading(); + void accepted(); + +public slots: + void setDirectory(const QString&); + +private slots: + void resizeColumns(); + +private: + Ui::LibraryView m_ui; + + LibraryModel m_model; +}; + +} + +#endif diff --git a/src/platform/qt/LibraryView.ui b/src/platform/qt/LibraryView.ui new file mode 100644 index 000000000..2d7d697ab --- /dev/null +++ b/src/platform/qt/LibraryView.ui @@ -0,0 +1,46 @@ + + + LibraryView + + + + 0 + 0 + 400 + 300 + + + + Library + + + + + + QAbstractItemView::NoEditTriggers + + + true + + + QAbstractItemView::SelectRows + + + false + + + true + + + false + + + 0 + + + + + + + +