Skip to content

Commit

Permalink
add platform icons to library view
Browse files Browse the repository at this point in the history
  • Loading branch information
ahigerd committed Jul 2, 2022
1 parent b490002 commit ffc6a2f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/platform/qt/library/LibraryEntry.cpp
Expand Up @@ -31,8 +31,8 @@ bool LibraryEntry::isNull() const {
return fullpath.isNull();
}

QString LibraryEntry::displayTitle() const {
if (title.isNull()) {
QString LibraryEntry::displayTitle(bool showFilename) const {
if (showFilename || title.isNull()) {
return filename;
}
return title;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/qt/library/LibraryEntry.h
Expand Up @@ -24,7 +24,7 @@ struct LibraryEntry {

bool isNull() const;

QString displayTitle() const;
QString displayTitle(bool showFilename = false) const;

QString base;
QString filename;
Expand Down
93 changes: 72 additions & 21 deletions src/platform/qt/library/LibraryModel.cpp
Expand Up @@ -11,6 +11,7 @@
#include <QDir>
#include <QItemSelectionModel>
#include <QSortFilterProxyModel>
#include <QStyle>

#include <algorithm>

Expand All @@ -21,6 +22,32 @@ LibraryModel::LibraryModel(QObject* parent)
, m_treeMode(false)
, m_showFilename(false)
{
QIcon iconGBA;
iconGBA.addFile(":/res/gba-icon-256.png", QSize(256, 256));
iconGBA.addFile(":/res/gba-icon-128.png");

QIcon iconGBC;
iconGBC.addFile(":/res/gbc-icon-128.png", QSize(128, 128));
iconGBC.addFile(":/res/gbc-icon-256.png", QSize(256, 256));

QIcon iconGB;
iconGB.addFile(":/res/gb-icon-128.png", QSize(128, 128));
iconGB.addFile(":/res/gb-icon-256.png", QSize(256, 256));

// QIcon iconNDS;
// iconNDS.addFile(":/res/gb-icon-128.png", QSize(128, 128));
// iconNDS.addFile(":/res/gb-icon-256.png", QSize(256, 256));

// These will silently and harmlessly fail if QSvgIconEngine isn't compiled in.
iconGBA.addFile(":/res/gba-icon.svg");
iconGBA.addFile(":/res/gbc-icon.svg");
iconGB.addFile(":/res/gb-icon.svg");
// iconNDS.addFile(":/res/nds-icon.svg");

m_icons[mPLATFORM_GBA] = iconGBA;
// m_icons[mPLATFORM_GBC] = iconGBC;
m_icons[mPLATFORM_GB] = iconGB;
// m_icons["NDS"] = iconNDS;
}

bool LibraryModel::treeMode() const {
Expand Down Expand Up @@ -274,27 +301,51 @@ int LibraryModel::rowCount(const QModelIndex& parent) const {
return m_games.size();
}

QVariant LibraryModel::folderData(const QModelIndex& index, int role) const
{
// Precondition: index and role must have already been validated
if (role == Qt::DecorationRole) {
return qApp->style()->standardIcon(QStyle::SP_DirOpenIcon);
}
if (role == FullPathRole || (index.column() == COL_LOCATION && role != Qt::DisplayRole)) {
return m_pathOrder[index.row()];
}
if (index.column() == COL_NAME) {
QString path = m_pathOrder[index.row()];
return path.section('/', -1);
}
return QVariant();
}

QVariant LibraryModel::data(const QModelIndex& index, int role) const {
if (role != Qt::DisplayRole &&
role != Qt::EditRole &&
role != Qt::ToolTipRole &&
role != Qt::DecorationRole &&
role != Qt::TextAlignmentRole &&
role != FullPathRole) {
return QVariant();
}
if (!checkIndex(index)) {
return QVariant();
}
// TODO: Qt::DecorationRole
if (role == Qt::DisplayRole || role == Qt::EditRole || (role == Qt::ToolTipRole && index.column() <= COL_LOCATION) || role == FullPathRole) {
const LibraryEntry* entry = nullptr;
if (m_treeMode) {
if (!index.parent().isValid()) {
if (role == FullPathRole || (index.column() == COL_LOCATION && role != Qt::DisplayRole)) {
return m_pathOrder[index.row()];
}
if (index.column() == COL_NAME) {
QString path = m_pathOrder[index.row()];
return path.section('/', -1);
}
return QVariant();
}
QString path = m_pathOrder[index.parent().row()];
entry = m_pathIndex[path][index.row()];
} else if (!index.parent().isValid() && index.row() < m_games.size()) {
if (role == Qt::ToolTipRole && index.column() > COL_LOCATION) {
return QVariant();
}
if (role == Qt::DecorationRole && index.column() != COL_NAME) {
return QVariant();
}
if (role == Qt::TextAlignmentRole) {
return index.column() == COL_SIZE ? (int)(Qt::AlignTrailing | Qt::AlignVCenter) : (int)(Qt::AlignLeading | Qt::AlignVCenter);
}
const LibraryEntry* entry = nullptr;
if (m_treeMode) {
if (!index.parent().isValid()) {
return folderData(index, role);
}
QString path = m_pathOrder[index.parent().row()];
entry = m_pathIndex[path][index.row()];
} else if (!index.parent().isValid() && index.row() < m_games.size()) {
entry = &m_games[index.row()];
}
if (entry) {
Expand All @@ -303,7 +354,10 @@ QVariant LibraryModel::data(const QModelIndex& index, int role) const {
}
switch (index.column()) {
case COL_NAME:
return m_showFilename ? entry->filename : entry->displayTitle();
if (role == Qt::DecorationRole) {
return m_icons.value(entry->platform, qApp->style()->standardIcon(QStyle::SP_FileIcon));
}
return entry->displayTitle(m_showFilename);
case COL_LOCATION:
return QDir::toNativeSeparators(entry->base);
case COL_PLATFORM:
Expand All @@ -314,9 +368,6 @@ QVariant LibraryModel::data(const QModelIndex& index, int role) const {
return (role == Qt::DisplayRole) ? QVariant(QStringLiteral("%0").arg(entry->crc32, 8, 16, QChar('0'))) : QVariant(entry->crc32);
}
}
} else if (role == Qt::TextAlignmentRole) {
return index.column() == COL_SIZE ? (int)(Qt::AlignTrailing | Qt::AlignVCenter) : (int)(Qt::AlignLeading | Qt::AlignVCenter);
}
return QVariant();
}

Expand Down
4 changes: 4 additions & 0 deletions src/platform/qt/library/LibraryModel.h
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include <QAbstractItemModel>
#include <QIcon>
#include <QTreeView>

#include <mgba/core/library.h>
Expand Down Expand Up @@ -61,6 +62,8 @@ Q_OBJECT
QModelIndex indexForPath(const QString& path);
QModelIndex indexForPath(const QString& path) const;

QVariant folderData(const QModelIndex& index, int role = Qt::DisplayRole) const;

void addEntriesList(const QList<LibraryEntry>& items);
void addEntriesTree(const QList<LibraryEntry>& items);
void addEntryInternal(const LibraryEntry& item);
Expand All @@ -72,6 +75,7 @@ Q_OBJECT
QStringList m_pathOrder;
QHash<QString, QList<const LibraryEntry*>> m_pathIndex;
QHash<QString, int> m_gameIndex;
QHash<int, QIcon> m_icons;
};

}
9 changes: 9 additions & 0 deletions src/platform/qt/resources.qrc
Expand Up @@ -6,6 +6,15 @@
<file>../../../res/keymap.qpic</file>
<file>../../../res/patrons.txt</file>
<file>../../../res/no-cam.png</file>
<file>../../../res/gb-icon-256.png</file>
<file>../../../res/gb-icon-128.png</file>
<file>../../../res/gb-icon.svg</file>
<file>../../../res/gbc-icon-256.png</file>
<file>../../../res/gbc-icon-128.png</file>
<file>../../../res/gbc-icon.svg</file>
<file>../../../res/gba-icon-256.png</file>
<file>../../../res/gba-icon-128.png</file>
<file>../../../res/gba-icon.svg</file>
</qresource>
<qresource prefix="/exe">
<file alias="exe4/chip-names.txt">../../../res/exe4/chip-names.txt</file>
Expand Down

0 comments on commit ffc6a2f

Please sign in to comment.