Qt: fix gamelist sort by adding a sort_role to custom_list_widget_item

This commit is contained in:
Megamouse 2018-04-07 19:52:42 +02:00 committed by Ivan
parent 28e19a1d14
commit 3ba133f485
2 changed files with 25 additions and 9 deletions

View file

@ -4,9 +4,20 @@
class custom_table_widget_item : public QTableWidgetItem
{
private:
int m_sort_role = Qt::DisplayRole;
public:
bool operator <(const QTableWidgetItem &other) const
{
return data(Qt::UserRole) < other.data(Qt::UserRole);
return data(m_sort_role) < other.data(m_sort_role);
}
void setData(int role, const QVariant &value, bool assign_sort_role = false)
{
if (assign_sort_role)
{
m_sort_role = role;
}
QTableWidgetItem::setData(role, value);
}
};

View file

@ -510,7 +510,6 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
QTableWidgetItem* item = m_xgrid->item(row, col);
if (item == nullptr) return; // null happens if you are double clicking in dockwidget area on nothing.
index = item->data(Qt::ItemDataRole::UserRole).toInt();
if (index == -1) return; // empty item shouldn't have context menu
}
ShowSpecifiedContextMenu(pos, index);
}
@ -890,11 +889,15 @@ int game_list_frame::PopulateGameList()
m_gameList->clearContents();
m_gameList->setRowCount((int)m_game_data.size());
auto l_GetItem = [](const std::string& text)
auto l_GetItem = [](const std::string& text, int sort_role = Qt::DisplayRole, int sort_index = 0)
{
QTableWidgetItem* curr = new QTableWidgetItem;
custom_table_widget_item* curr = new custom_table_widget_item;
curr->setFlags(curr->flags() & ~Qt::ItemIsEditable);
curr->setText(qstr(text).simplified()); // simplified() forces single line text
if (sort_role != Qt::DisplayRole)
{
curr->setData(sort_role, sort_index, true);
}
return curr;
};
@ -907,12 +910,13 @@ int game_list_frame::PopulateGameList()
continue;
// Icon
QTableWidgetItem* icon_item = new QTableWidgetItem;
custom_table_widget_item* icon_item = new custom_table_widget_item;
icon_item->setFlags(icon_item->flags() & ~Qt::ItemIsEditable);
icon_item->setData(Qt::DecorationRole, game.pxmap);
icon_item->setData(Qt::UserRole, index);
icon_item->setData(Qt::UserRole, index, true);
QTableWidgetItem* title_item = l_GetItem(game.info.name);
// Title
custom_table_widget_item* title_item = l_GetItem(game.info.name);
if (game.hasCustomConfig)
{
title_item->setIcon(QIcon(":/Icons/cog_black.png"));
@ -922,12 +926,13 @@ int game_list_frame::PopulateGameList()
custom_table_widget_item* compat_item = new custom_table_widget_item;
compat_item->setFlags(compat_item->flags() & ~Qt::ItemIsEditable);
compat_item->setText(game.compat.text + (game.compat.date.isEmpty() ? "" : " (" + game.compat.date + ")"));
compat_item->setData(Qt::UserRole, game.compat.index);
compat_item->setData(Qt::UserRole, game.compat.index, true);
compat_item->setToolTip(game.compat.tooltip);
if (!game.compat.color.isEmpty())
{
compat_item->setData(Qt::DecorationRole, compat_pixmap(game.compat.color));
}
m_gameList->setItem(row, gui::column_icon, icon_item);
m_gameList->setItem(row, gui::column_name, title_item);
m_gameList->setItem(row, gui::column_serial, l_GetItem(game.info.serial));
@ -937,7 +942,7 @@ int game_list_frame::PopulateGameList()
m_gameList->setItem(row, gui::column_path, l_GetItem(game.info.path));
m_gameList->setItem(row, gui::column_resolution, l_GetItem(GetStringFromU32(game.info.resolution, resolution::mode, true)));
m_gameList->setItem(row, gui::column_sound, l_GetItem(GetStringFromU32(game.info.sound_format, sound::format, true)));
m_gameList->setItem(row, gui::column_parental, l_GetItem(GetStringFromU32(game.info.parental_lvl, parental::level)));
m_gameList->setItem(row, gui::column_parental, l_GetItem(GetStringFromU32(game.info.parental_lvl, parental::level), Qt::UserRole, game.info.parental_lvl));
m_gameList->setItem(row, gui::column_compat, compat_item);
if (selected_item == game.info.icon_path)