Refactor proxy models and toolbar.

Remove the ugly LARGE_ICON column hack from the table proxy and use a
list proxy. Move the toolbar into its own file.
This commit is contained in:
spxtr
2015-12-03 20:41:17 -08:00
parent 8322040c47
commit a06b0d87a7
12 changed files with 252 additions and 104 deletions

View File

@ -6,14 +6,16 @@
#include "Core/ConfigManager.h"
#include "DolphinQt2/GameList/GameList.h"
#include "DolphinQt2/GameList/GameListProxyModel.h"
#include "DolphinQt2/GameList/ListProxyModel.h"
#include "DolphinQt2/GameList/TableProxyModel.h"
GameList::GameList(QWidget* parent): QStackedWidget(parent)
{
m_model = new GameListModel(this);
m_proxy = new GameListProxyModel(this);
m_proxy->setSourceModel(m_model);
m_proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
m_table_proxy = new TableProxyModel(this);
m_table_proxy->setSourceModel(m_model);
m_list_proxy = new ListProxyModel(this);
m_list_proxy->setSourceModel(m_model);
MakeTableView();
MakeListView();
@ -30,7 +32,7 @@ GameList::GameList(QWidget* parent): QStackedWidget(parent)
void GameList::MakeTableView()
{
m_table = new QTableView(this);
m_table->setModel(m_proxy);
m_table->setModel(m_table_proxy);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setAlternatingRowColors(true);
@ -44,9 +46,6 @@ void GameList::MakeTableView()
m_table->setColumnWidth(GameListModel::COL_COUNTRY, 38);
m_table->setColumnWidth(GameListModel::COL_RATING, 52);
// This column is for the icon view. Hide it.
m_table->setColumnHidden(GameListModel::COL_LARGE_ICON, true);
QHeaderView* header = m_table->horizontalHeader();
header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::Fixed);
header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::Fixed);
@ -62,23 +61,31 @@ void GameList::MakeTableView()
void GameList::MakeListView()
{
m_list = new QListView(this);
m_list->setModel(m_proxy);
m_list->setModel(m_list_proxy);
m_list->setViewMode(QListView::IconMode);
m_list->setModelColumn(GameListModel::COL_LARGE_ICON);
m_list->setResizeMode(QListView::Adjust);
m_list->setUniformItemSizes(true);
}
QString GameList::GetSelectedGame() const
{
QItemSelectionModel* sel_model;
QAbstractItemView* view;
QSortFilterProxyModel* proxy;
if (currentWidget() == m_table)
sel_model = m_table->selectionModel();
{
view = m_table;
proxy = m_table_proxy;
}
else
sel_model = m_list->selectionModel();
{
view = m_list;
proxy = m_list_proxy;
}
QItemSelectionModel* sel_model = view->selectionModel();
if (sel_model->hasSelection())
return m_model->GetPath(m_proxy->mapToSource(sel_model->selectedIndexes()[0]).row());
else
return QString();
{
QModelIndex model_index = proxy->mapToSource(sel_model->selectedIndexes()[0]);
return m_model->GetPath(model_index.row());
}
return QStringLiteral();
}

View File

@ -35,7 +35,9 @@ private:
void MakeListView();
GameListModel* m_model;
QSortFilterProxyModel* m_proxy;
QSortFilterProxyModel* m_table_proxy;
QSortFilterProxyModel* m_list_proxy;
QListView* m_list;
QTableView* m_table;
};

View File

@ -37,7 +37,6 @@ public:
COL_SIZE,
COL_COUNTRY,
COL_RATING,
COL_LARGE_ICON,
NUM_COLS
};

View File

@ -0,0 +1,38 @@
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QSize>
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/GameList/ListProxyModel.h"
static constexpr QSize LARGE_BANNER_SIZE(144, 48);
ListProxyModel::ListProxyModel(QObject* parent)
: QSortFilterProxyModel(parent)
{
setSortCaseSensitivity(Qt::CaseInsensitive);
sort(GameListModel::COL_TITLE);
}
QVariant ListProxyModel::data(const QModelIndex& i, int role) const
{
QModelIndex source_index = mapToSource(i);
if (role == Qt::DisplayRole)
{
return sourceModel()->data(
sourceModel()->index(source_index.row(), GameListModel::COL_TITLE),
Qt::DisplayRole);
}
else if (role == Qt::DecorationRole)
{
return sourceModel()->data(
sourceModel()->index(source_index.row(), GameListModel::COL_BANNER),
Qt::DisplayRole).value<QPixmap>().scaled(
LARGE_BANNER_SIZE,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
return QVariant();
}

View File

@ -0,0 +1,14 @@
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QSortFilterProxyModel>
class ListProxyModel final : public QSortFilterProxyModel
{
Q_OBJECT
public:
ListProxyModel(QObject* parent = nullptr);
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
};

View File

@ -4,10 +4,9 @@
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/GameList/GameListProxyModel.h"
#include "DolphinQt2/GameList/TableProxyModel.h"
static constexpr QSize NORMAL_BANNER_SIZE(96, 32);
static constexpr QSize LARGE_BANNER_SIZE(144, 48);
// Convert an integer size to a friendly string representation.
static QString FormatSize(qint64 size)
@ -29,12 +28,13 @@ static QString FormatSize(qint64 size)
return QStringLiteral("%1 %2").arg(QString::number(num, 'f', 1)).arg(unit);
}
GameListProxyModel::GameListProxyModel(QObject* parent)
TableProxyModel::TableProxyModel(QObject* parent)
: QSortFilterProxyModel(parent)
{
setSortCaseSensitivity(Qt::CaseInsensitive);
}
QVariant GameListProxyModel::data(const QModelIndex& i, int role) const
QVariant TableProxyModel::data(const QModelIndex& i, int role) const
{
QModelIndex source_index = mapToSource(i);
QVariant source_data = sourceModel()->data(source_index, Qt::DisplayRole);
@ -51,9 +51,6 @@ QVariant GameListProxyModel::data(const QModelIndex& i, int role) const
case GameListModel::COL_DESCRIPTION:
case GameListModel::COL_MAKER:
return source_data;
// Show the title in the display role of the icon view.
case GameListModel::COL_LARGE_ICON:
return data(index(i.row(), GameListModel::COL_TITLE), Qt::DisplayRole);
}
}
else if (role == Qt::DecorationRole)
@ -74,13 +71,6 @@ QVariant GameListProxyModel::data(const QModelIndex& i, int role) const
return Resources::GetCountry(source_data.toInt());
case GameListModel::COL_RATING:
return Resources::GetRating(source_data.toInt());
// Show a scaled icon in the decoration role of the icon view.
case GameListModel::COL_LARGE_ICON:
return data(index(i.row(), GameListModel::COL_BANNER), Qt::DecorationRole)
.value<QPixmap>().scaled(
LARGE_BANNER_SIZE,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
}
return QVariant();

View File

@ -9,11 +9,11 @@
// For instance, the GameListModel exposes country as an integer, so this
// class converts that into a flag, while still allowing sorting on the
// underlying integer.
class GameListProxyModel final : public QSortFilterProxyModel
class TableProxyModel final : public QSortFilterProxyModel
{
Q_OBJECT
public:
GameListProxyModel(QObject* parent = nullptr);
TableProxyModel(QObject* parent = nullptr);
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
};