Skip to content

Commit

Permalink
DolphinQT: Add "Time Played" column to game list view
Browse files Browse the repository at this point in the history
Shows minutes/hours in the list view and handles column visibility.
  • Loading branch information
aminoa committed Jan 23, 2025
1 parent 976318e commit e3b6e6d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/Core/DolphinQt/GameList/GameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ void GameList::MakeListView()
SetResizeMode(Column::FileFormat, Mode::Fixed);
SetResizeMode(Column::BlockSize, Mode::Fixed);
SetResizeMode(Column::Compression, Mode::Fixed);
SetResizeMode(Column::TimePlayed, 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
Expand Down Expand Up @@ -273,6 +274,7 @@ void GameList::UpdateColumnVisibility()
SetVisiblity(Column::FileFormat, Config::Get(Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT));
SetVisiblity(Column::BlockSize, Config::Get(Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE));
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));
}

Expand Down Expand Up @@ -1005,6 +1007,7 @@ void GameList::OnColumnVisibilityToggled(const QString& row, bool visible)
{tr("File Format"), Column::FileFormat},
{tr("Block Size"), Column::BlockSize},
{tr("Compression"), Column::Compression},
{tr("Time Played"), Column::TimePlayed},
{tr("Tags"), Column::Tags},
};

Expand Down
30 changes: 30 additions & 0 deletions Source/Core/DolphinQt/GameList/GameListModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QRegularExpression>

#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/TimePlayed.h"

#include "DiscIO/Enums.h"

Expand All @@ -32,6 +34,8 @@ GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
&GameTracker::RefreshAll);
connect(&Settings::Instance(), &Settings::TitleDBReloadRequested,
[this] { m_title_database = Core::TitleDatabase(); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
&GameListModel::OnEmulationStateChanged);

for (const QString& dir : Settings::Instance().GetPaths())
m_tracker.AddDirectory(dir);
Expand Down Expand Up @@ -187,6 +191,22 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
return compression.isEmpty() ? tr("No Compression") : compression;
}
break;
case Column::TimePlayed:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
std::string game_id = game.GetGameID();
std::chrono::milliseconds total_time = m_timer.GetTimePlayed(game_id);
std::chrono::minutes total_minutes =
std::chrono::duration_cast<std::chrono::minutes>(total_time);
std::chrono::hours 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() - total_hours.count() * 60);
return formatted_time;
}
break;
case Column::Tags:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
Expand Down Expand Up @@ -232,6 +252,8 @@ QVariant GameListModel::headerData(int section, Qt::Orientation orientation, int
return tr("Block Size");
case Column::Compression:
return tr("Compression");
case Column::TimePlayed:
return tr("Time Played");
case Column::Tags:
return tr("Tags");
default:
Expand Down Expand Up @@ -480,3 +502,11 @@ void GameListModel::PurgeCache()
{
m_tracker.PurgeCache();
}

void GameListModel::OnEmulationStateChanged(Core::State state)
{
if (state == Core::State::Uninitialized)
{
m_timer.Reload();
}
}
6 changes: 6 additions & 0 deletions Source/Core/DolphinQt/GameList/GameListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <QStringList>
#include <QVariant>

#include "Core/Core.h"
#include "Core/TimePlayed.h"
#include "Core/TitleDatabase.h"

#include "DolphinQt/GameList/GameTracker.h"
Expand Down Expand Up @@ -58,6 +60,7 @@ class GameListModel final : public QAbstractTableModel
FileFormat,
BlockSize,
Compression,
TimePlayed,
Tags,
Count,
};
Expand Down Expand Up @@ -87,12 +90,15 @@ class GameListModel final : public QAbstractTableModel
// Index in m_games, or -1 if it isn't found
int FindGameIndex(const std::string& path) const;

void OnEmulationStateChanged(Core::State state);

QStringList m_tag_list;
QMap<QString, QVariant> m_game_tags;

GameTracker m_tracker;
QList<std::shared_ptr<const UICommon::GameFile>> m_games;
Core::TitleDatabase m_title_database;
TimePlayed m_timer;
QString m_term;
float m_scale = 1.0;
};
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/MenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ void MenuBar::AddListColumnsMenu(QMenu* view_menu)
{tr("File Format"), &Config::MAIN_GAMELIST_COLUMN_FILE_FORMAT},
{tr("Block Size"), &Config::MAIN_GAMELIST_COLUMN_BLOCK_SIZE},
{tr("Compression"), &Config::MAIN_GAMELIST_COLUMN_COMPRESSION},
{tr("Time Played"), &Config::MAIN_GAMELIST_COLUMN_TIME_PLAYED},
{tr("Tags"), &Config::MAIN_GAMELIST_COLUMN_TAGS}};

QActionGroup* column_group = new QActionGroup(this);
Expand Down

0 comments on commit e3b6e6d

Please sign in to comment.