DolphinQt: Add "Time Played" column to game list view

Shows minutes/hours in the list view and handles column visibility.
This commit is contained in:
Aneesh Maganti 2025-02-10 15:27:16 -05:00 committed by Admiral H. Curtiss
parent 276f043db8
commit 3c44fe592b
No known key found for this signature in database
GPG key ID: F051B4C4044F33FB
4 changed files with 43 additions and 0 deletions

View file

@ -208,6 +208,7 @@ void GameList::MakeListView()
SetResizeMode(Column::FileFormat, Mode::Fixed); SetResizeMode(Column::FileFormat, Mode::Fixed);
SetResizeMode(Column::BlockSize, Mode::Fixed); SetResizeMode(Column::BlockSize, Mode::Fixed);
SetResizeMode(Column::Compression, Mode::Fixed); SetResizeMode(Column::Compression, Mode::Fixed);
SetResizeMode(Column::TimePlayed, Mode::Interactive);
SetResizeMode(Column::Tags, Mode::Interactive); SetResizeMode(Column::Tags, Mode::Interactive);
// Cells have 3 pixels of padding, so the width of these needs to be image width + 6. Banners // Cells have 3 pixels of padding, so the width of these needs to be image width + 6. Banners
@ -273,6 +274,7 @@ void GameList::UpdateColumnVisibility()
SetVisiblity(Column::FileFormat, Config::Get(Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT)); SetVisiblity(Column::FileFormat, Config::Get(Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT));
SetVisiblity(Column::BlockSize, Config::Get(Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE)); SetVisiblity(Column::BlockSize, Config::Get(Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE));
SetVisiblity(Column::Compression, Config::Get(Config::MAIN_GAMELIST_COLUMN_COMPRESSION)); SetVisiblity(Column::Compression, Config::Get(Config::MAIN_GAMELIST_COLUMN_COMPRESSION));
SetVisiblity(Column::TimePlayed, Config::Get(Config::MAIN_GAMELIST_COLUMN_TIME_PLAYED));
SetVisiblity(Column::Tags, Config::Get(Config::MAIN_GAMELIST_COLUMN_TAGS)); SetVisiblity(Column::Tags, Config::Get(Config::MAIN_GAMELIST_COLUMN_TAGS));
} }
@ -1005,6 +1007,7 @@ void GameList::OnColumnVisibilityToggled(const QString& row, bool visible)
{tr("File Format"), Column::FileFormat}, {tr("File Format"), Column::FileFormat},
{tr("Block Size"), Column::BlockSize}, {tr("Block Size"), Column::BlockSize},
{tr("Compression"), Column::Compression}, {tr("Compression"), Column::Compression},
{tr("Time Played"), Column::TimePlayed},
{tr("Tags"), Column::Tags}, {tr("Tags"), Column::Tags},
}; };

View file

@ -9,6 +9,8 @@
#include <QRegularExpression> #include <QRegularExpression>
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/TimePlayed.h"
#include "DiscIO/Enums.h" #include "DiscIO/Enums.h"
@ -32,6 +34,8 @@ GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
&GameTracker::RefreshAll); &GameTracker::RefreshAll);
connect(&Settings::Instance(), &Settings::TitleDBReloadRequested, connect(&Settings::Instance(), &Settings::TitleDBReloadRequested,
[this] { m_title_database = Core::TitleDatabase(); }); [this] { m_title_database = Core::TitleDatabase(); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
&GameListModel::OnEmulationStateChanged);
for (const QString& dir : Settings::Instance().GetPaths()) for (const QString& dir : Settings::Instance().GetPaths())
m_tracker.AddDirectory(dir); m_tracker.AddDirectory(dir);
@ -187,6 +191,25 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
return compression.isEmpty() ? tr("No Compression") : compression; return compression.isEmpty() ? tr("No Compression") : compression;
} }
break; break;
case Column::TimePlayed:
if (role == Qt::DisplayRole)
{
const std::string game_id = game.GetGameID();
const std::chrono::milliseconds total_time = m_timer.GetTimePlayed(game_id);
const auto total_minutes = std::chrono::duration_cast<std::chrono::minutes>(total_time);
const auto total_hours = std::chrono::duration_cast<std::chrono::hours>(total_time);
// i18n: A time displayed as hours and minutes
QString formatted_time =
tr("%1h %2m").arg(total_hours.count()).arg(total_minutes.count() % 60);
return formatted_time;
}
if (role == SORT_ROLE)
{
const std::string game_id = game.GetGameID();
return static_cast<qlonglong>(m_timer.GetTimePlayed(game_id).count());
}
break;
case Column::Tags: case Column::Tags:
if (role == Qt::DisplayRole || role == SORT_ROLE) if (role == Qt::DisplayRole || role == SORT_ROLE)
{ {
@ -232,6 +255,8 @@ QVariant GameListModel::headerData(int section, Qt::Orientation orientation, int
return tr("Block Size"); return tr("Block Size");
case Column::Compression: case Column::Compression:
return tr("Compression"); return tr("Compression");
case Column::TimePlayed:
return tr("Time Played");
case Column::Tags: case Column::Tags:
return tr("Tags"); return tr("Tags");
default: default:
@ -480,3 +505,11 @@ void GameListModel::PurgeCache()
{ {
m_tracker.PurgeCache(); m_tracker.PurgeCache();
} }
void GameListModel::OnEmulationStateChanged(Core::State state)
{
if (state == Core::State::Uninitialized)
{
m_timer.Reload();
}
}

View file

@ -12,6 +12,8 @@
#include <QStringList> #include <QStringList>
#include <QVariant> #include <QVariant>
#include "Core/Core.h"
#include "Core/TimePlayed.h"
#include "Core/TitleDatabase.h" #include "Core/TitleDatabase.h"
#include "DolphinQt/GameList/GameTracker.h" #include "DolphinQt/GameList/GameTracker.h"
@ -58,6 +60,7 @@ public:
FileFormat, FileFormat,
BlockSize, BlockSize,
Compression, Compression,
TimePlayed,
Tags, Tags,
Count, Count,
}; };
@ -87,12 +90,15 @@ private:
// Index in m_games, or -1 if it isn't found // Index in m_games, or -1 if it isn't found
int FindGameIndex(const std::string& path) const; int FindGameIndex(const std::string& path) const;
void OnEmulationStateChanged(Core::State state);
QStringList m_tag_list; QStringList m_tag_list;
QMap<QString, QVariant> m_game_tags; QMap<QString, QVariant> m_game_tags;
GameTracker m_tracker; GameTracker m_tracker;
QList<std::shared_ptr<const UICommon::GameFile>> m_games; QList<std::shared_ptr<const UICommon::GameFile>> m_games;
Core::TitleDatabase m_title_database; Core::TitleDatabase m_title_database;
TimePlayed m_timer;
QString m_term; QString m_term;
float m_scale = 1.0; float m_scale = 1.0;
}; };

View file

@ -701,6 +701,7 @@ void MenuBar::AddListColumnsMenu(QMenu* view_menu)
{tr("File Format"), &Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT}, {tr("File Format"), &Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT},
{tr("Block Size"), &Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE}, {tr("Block Size"), &Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE},
{tr("Compression"), &Config::MAIN_GAMELIST_COLUMN_COMPRESSION}, {tr("Compression"), &Config::MAIN_GAMELIST_COLUMN_COMPRESSION},
{tr("Time Played"), &Config::MAIN_GAMELIST_COLUMN_TIME_PLAYED},
{tr("Tags"), &Config::MAIN_GAMELIST_COLUMN_TAGS}}; {tr("Tags"), &Config::MAIN_GAMELIST_COLUMN_TAGS}};
QActionGroup* column_group = new QActionGroup(this); QActionGroup* column_group = new QActionGroup(this);