Qt: Implement game list sorting
This commit is contained in:
parent
64426a0be2
commit
2611e64459
|
@ -2,10 +2,11 @@
|
||||||
#include "core/settings.h"
|
#include "core/settings.h"
|
||||||
#include "qthostinterface.h"
|
#include "qthostinterface.h"
|
||||||
#include "qtutils.h"
|
#include "qtutils.h"
|
||||||
|
#include <QtCore/QSortFilterProxyModel>
|
||||||
#include <QtGui/QPixmap>
|
#include <QtGui/QPixmap>
|
||||||
#include <QtWidgets/QHeaderView>
|
#include <QtWidgets/QHeaderView>
|
||||||
|
|
||||||
class GameListModel : public QAbstractTableModel
|
class GameListModel final : public QAbstractTableModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Column : int
|
enum Column : int
|
||||||
|
@ -73,6 +74,30 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case Qt::InitialSortOrderRole:
|
||||||
|
{
|
||||||
|
switch (index.column())
|
||||||
|
{
|
||||||
|
case Column_Type:
|
||||||
|
return static_cast<int>(ge.type);
|
||||||
|
|
||||||
|
case Column_Code:
|
||||||
|
return QString::fromStdString(ge.code);
|
||||||
|
|
||||||
|
case Column_Title:
|
||||||
|
return QString::fromStdString(ge.title);
|
||||||
|
|
||||||
|
case Column_Region:
|
||||||
|
return static_cast<int>(ge.region);
|
||||||
|
|
||||||
|
case Column_Size:
|
||||||
|
return ge.total_size;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case Qt::DecorationRole:
|
case Qt::DecorationRole:
|
||||||
{
|
{
|
||||||
switch (index.column())
|
switch (index.column())
|
||||||
|
@ -153,6 +178,19 @@ public:
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool titlesLessThan(int left_row, int right_row, bool ascending) const
|
||||||
|
{
|
||||||
|
if (left_row < 0 || left_row >= static_cast<int>(m_game_list->GetEntryCount()) || right_row < 0 ||
|
||||||
|
right_row >= static_cast<int>(m_game_list->GetEntryCount()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GameList::GameListEntry& left = m_game_list->GetEntries().at(left_row);
|
||||||
|
const GameList::GameListEntry& right = m_game_list->GetEntries().at(right_row);
|
||||||
|
return ascending ? (left.title < right.title) : (right.title < left.title);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadCommonImages()
|
void loadCommonImages()
|
||||||
{
|
{
|
||||||
|
@ -176,6 +214,33 @@ private:
|
||||||
QPixmap m_region_us_pixmap;
|
QPixmap m_region_us_pixmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class GameListSortModel final : public QSortFilterProxyModel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GameListSortModel(GameListModel* parent) : QSortFilterProxyModel(parent), m_model(parent) {}
|
||||||
|
|
||||||
|
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override
|
||||||
|
{
|
||||||
|
// TODO: Search
|
||||||
|
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override
|
||||||
|
{
|
||||||
|
const bool ascending = sortOrder() == Qt::AscendingOrder;
|
||||||
|
const QVariant left = source_left.data(Qt::InitialSortOrderRole);
|
||||||
|
const QVariant right = source_right.data(Qt::InitialSortOrderRole);
|
||||||
|
if (left != right)
|
||||||
|
return ascending ? (left < right) : (right < left);
|
||||||
|
|
||||||
|
// fallback to sorting by title for equal items
|
||||||
|
return m_model->titlesLessThan(source_left.row(), source_right.row(), ascending);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
GameListModel* m_model;
|
||||||
|
};
|
||||||
|
|
||||||
GameListWidget::GameListWidget(QWidget* parent /* = nullptr */) : QStackedWidget(parent) {}
|
GameListWidget::GameListWidget(QWidget* parent /* = nullptr */) : QStackedWidget(parent) {}
|
||||||
|
|
||||||
GameListWidget::~GameListWidget() = default;
|
GameListWidget::~GameListWidget() = default;
|
||||||
|
@ -188,8 +253,11 @@ void GameListWidget::initialize(QtHostInterface* host_interface)
|
||||||
connect(m_host_interface, &QtHostInterface::gameListRefreshed, this, &GameListWidget::onGameListRefreshed);
|
connect(m_host_interface, &QtHostInterface::gameListRefreshed, this, &GameListWidget::onGameListRefreshed);
|
||||||
|
|
||||||
m_table_model = new GameListModel(m_game_list, this);
|
m_table_model = new GameListModel(m_game_list, this);
|
||||||
|
m_table_sort_model = new GameListSortModel(m_table_model);
|
||||||
|
m_table_sort_model->setSourceModel(m_table_model);
|
||||||
m_table_view = new QTableView(this);
|
m_table_view = new QTableView(this);
|
||||||
m_table_view->setModel(m_table_model);
|
m_table_view->setModel(m_table_sort_model);
|
||||||
|
m_table_view->setSortingEnabled(true);
|
||||||
m_table_view->setSelectionMode(QAbstractItemView::SingleSelection);
|
m_table_view->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||||
m_table_view->setAlternatingRowColors(true);
|
m_table_view->setAlternatingRowColors(true);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <QtWidgets/QTableView>
|
#include <QtWidgets/QTableView>
|
||||||
|
|
||||||
class GameListModel;
|
class GameListModel;
|
||||||
|
class GameListSortModel;
|
||||||
|
|
||||||
class QtHostInterface;
|
class QtHostInterface;
|
||||||
|
|
||||||
|
@ -32,5 +33,6 @@ private:
|
||||||
GameList* m_game_list = nullptr;
|
GameList* m_game_list = nullptr;
|
||||||
|
|
||||||
GameListModel* m_table_model = nullptr;
|
GameListModel* m_table_model = nullptr;
|
||||||
|
GameListSortModel* m_table_sort_model = nullptr;
|
||||||
QTableView* m_table_view = nullptr;
|
QTableView* m_table_view = nullptr;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue