12#include <QDialogButtonBox>
26StatCard::StatCard(
const QString& label,
const QString& value,
const QString& icon, QWidget* parent,
28 : QFrame(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
30 setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
31 setMinimumSize(150, 100);
33 auto* layout =
new QVBoxLayout(
this);
34 layout->setContentsMargins(16, 12, 16, 12);
37 auto* topLayout =
new QHBoxLayout;
38 topLayout->setSpacing(8);
42 iconLabel_ =
new QLabel(icon);
43 iconLabel_->setStyleSheet(
"font-size: 24px;");
44 topLayout->addWidget(iconLabel_);
47 valueLabel_ =
new QLabel(value);
48 valueLabel_->setStyleSheet(
"font-size: 28px; font-weight: bold;");
49 topLayout->addWidget(valueLabel_);
50 topLayout->addStretch();
52 layout->addLayout(topLayout);
55 labelLabel_ =
new QLabel(label);
56 labelLabel_->setStyleSheet(
"font-size: 13px;");
57 layout->addWidget(labelLabel_);
60 subtextLabel_ =
new QLabel;
61 subtextLabel_->setStyleSheet(
"font-size: 11px;");
62 subtextLabel_->hide();
63 layout->addWidget(subtextLabel_);
73 valueLabel_->setText(value);
78 subtextLabel_->setText(subtext);
79 subtextLabel_->setVisible(!subtext.isEmpty());
84 const auto& c = themeManager_->colors();
86 setStyleSheet(QString(
"StatCard {"
87 " background-color: %1;"
88 " border: 1px solid %2;"
89 " border-radius: 8px;"
91 .arg(c.cardBackground.name())
92 .arg(c.border.name()));
96 valueLabel_->setStyleSheet(
97 QString(
"font-size: 28px; font-weight: bold; color: %1;").arg(c.textPrimary.name()));
101 labelLabel_->setStyleSheet(
102 QString(
"font-size: 13px; color: %1;").arg(c.textSecondary.name()));
106 subtextLabel_->setStyleSheet(
107 QString(
"font-size: 11px; color: %1;").arg(c.textSecondary.name()));
116 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
118 setMinimumSize(200, 200);
137 const auto& c = themeManager_->colors();
138 textColor_ = c.textPrimary;
139 backgroundColor_ = c.cardBackground;
145 QPainter painter(
this);
146 painter.setRenderHint(QPainter::Antialiasing);
149 painter.fillRect(rect(), backgroundColor_);
153 for (
const auto& slice : slices_)
155 total += slice.value;
158 if (total <= 0 || slices_.empty())
160 painter.setPen(textColor_);
161 painter.drawText(rect(), Qt::AlignCenter,
"No data");
167 if (!title_.isEmpty())
169 painter.setPen(textColor_);
170 painter.setFont(QFont(font().family(), 12, QFont::Bold));
171 painter.drawText(QRect(0, 5, width(), 20), Qt::AlignCenter, title_);
176 int pieSize = qMin(width(), height() - yOffset - 80);
177 int pieX = (width() - pieSize) / 2;
178 int pieY = yOffset + 10;
179 QRectF pieRect(pieX, pieY, pieSize, pieSize);
182 int startAngle = 90 * 16;
183 for (
const auto& slice : slices_)
185 if (slice.value <= 0)
188 int spanAngle =
static_cast<int>((slice.value / total) * 360 * 16);
189 painter.setBrush(slice.color);
190 painter.setPen(QPen(backgroundColor_, 2));
191 painter.drawPie(pieRect, startAngle, -spanAngle);
192 startAngle -= spanAngle;
196 painter.setFont(QFont(font().family(), 10));
197 int legendY = pieY + pieSize + 10;
199 int legendItemWidth = width() / 2 - 15;
202 for (
const auto& slice : slices_)
204 if (slice.value <= 0)
207 int x = legendX + (col % 2) * legendItemWidth;
208 int y = legendY + (col / 2) * 18;
211 painter.fillRect(x, y + 2, 12, 12, slice.color);
212 painter.setPen(textColor_);
215 double percent = (slice.value / total) * 100;
216 QString text = QString(
"%1 (%2%)").arg(slice.label).arg(percent, 0,
'f', 1);
217 painter.drawText(x + 16, y, legendItemWidth - 20, 16, Qt::AlignLeft | Qt::AlignVCenter,
229 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
231 setMinimumSize(300, 200);
250 const auto& c = themeManager_->colors();
251 textColor_ = c.textPrimary;
252 barColor_ = c.accent;
253 backgroundColor_ = c.cardBackground;
259 int barCount = qMin(
static_cast<int>(bars_.size()), maxBars_);
260 return QSize(400, qMax(200, barCount * 35 + 50));
265 return QSize(300, 150);
270 QPainter painter(
this);
271 painter.setRenderHint(QPainter::Antialiasing);
274 painter.fillRect(rect(), backgroundColor_);
278 if (!title_.isEmpty())
280 painter.setPen(textColor_);
281 painter.setFont(QFont(font().family(), 12, QFont::Bold));
282 painter.drawText(QRect(0, 5, width(), 20), Qt::AlignCenter, title_);
288 painter.setPen(textColor_);
289 painter.drawText(rect(), Qt::AlignCenter,
"No data");
295 for (
const auto& bar : bars_)
297 maxValue = qMax(maxValue, bar.value);
306 int barCount = qMin(
static_cast<int>(bars_.size()), maxBars_);
307 int labelWidth = 120;
309 int barAreaWidth = width() - labelWidth - valueWidth - 20;
313 painter.setFont(QFont(font().family(), 10));
315 for (
int i = 0; i < barCount; ++i)
317 const auto& bar = bars_[
static_cast<size_t>(i)];
318 int y = yOffset + i * (barHeight + barSpacing);
321 painter.setPen(textColor_);
322 QRect labelRect(5, y, labelWidth - 10, barHeight);
323 QString elidedLabel =
324 painter.fontMetrics().elidedText(bar.label, Qt::ElideRight, labelWidth - 15);
325 painter.drawText(labelRect, Qt::AlignLeft | Qt::AlignVCenter, elidedLabel);
328 int barWidth =
static_cast<int>((bar.value / maxValue) * barAreaWidth);
329 QRect barRect(labelWidth, y + 2, barWidth, barHeight - 4);
330 painter.fillRect(barRect, barColor_);
333 painter.setPen(textColor_);
334 QRect valueRect(labelWidth + barAreaWidth + 5, y, valueWidth, barHeight);
335 painter.drawText(valueRect, Qt::AlignLeft | Qt::AlignVCenter, bar.formattedValue);
344 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
346 setMinimumSize(500, 100);
353 activityData_ = dailyHours;
355 for (
const auto& [day, hours] : activityData_)
357 maxActivity_ = qMax(maxActivity_, hours);
370 const auto& c = themeManager_->colors();
371 textColor_ = c.textPrimary;
372 backgroundColor_ = c.cardBackground;
373 lowActivityColor_ = c.inputBackground;
374 highActivityColor_ = c.accent;
380 QPainter painter(
this);
381 painter.setRenderHint(QPainter::Antialiasing);
384 painter.fillRect(rect(), backgroundColor_);
388 if (!title_.isEmpty())
390 painter.setPen(textColor_);
391 painter.setFont(QFont(font().family(), 12, QFont::Bold));
392 painter.drawText(QRect(0, 5, width(), 20), Qt::AlignCenter, title_);
398 int cellSize = qMin((width() - 50) / weeks, 15);
399 int startX = (width() - weeks * cellSize) / 2;
400 int startY = yOffset + 5;
403 painter.setFont(QFont(font().family(), 8));
404 painter.setPen(textColor_);
405 QStringList days = {
"Mon",
"Wed",
"Fri",
"Sun"};
406 for (
int i = 0; i < 4; ++i)
408 painter.drawText(5, startY + i * 2 * cellSize + cellSize, days[i]);
412 QDate today = QDate::currentDate();
414 for (
int week = 0; week < weeks; ++week)
416 for (
int day = 0; day < 7; ++day)
418 int daysAgo = (weeks - 1 - week) * 7 + (6 - day);
419 QDate cellDate = today.addDays(-daysAgo);
422 if (cellDate > today)
426 auto it = activityData_.find(daysAgo);
427 if (it != activityData_.end())
436 cellColor = lowActivityColor_;
438 else if (maxActivity_ > 0)
440 double intensity = qMin(hours / maxActivity_, 1.0);
441 int r =
static_cast<int>(lowActivityColor_.red() +
443 (highActivityColor_.red() - lowActivityColor_.red()));
444 int g =
static_cast<int>(
445 lowActivityColor_.green() +
446 intensity * (highActivityColor_.green() - lowActivityColor_.green()));
447 int b =
static_cast<int>(
448 lowActivityColor_.blue() +
449 intensity * (highActivityColor_.blue() - lowActivityColor_.blue()));
450 cellColor = QColor(r, g, b);
454 cellColor = lowActivityColor_;
457 int x = startX + week * cellSize;
458 int y = startY + day * cellSize;
460 painter.fillRect(x + 1, y + 1, cellSize - 2, cellSize - 2, cellColor);
465 painter.setPen(textColor_);
466 int labelY = startY + 7 * cellSize + 12;
467 QDate monthStart = today.addMonths(-11);
468 monthStart = QDate(monthStart.year(), monthStart.month(), 1);
470 for (
int m = 0; m < 12; ++m)
472 QDate month = monthStart.addMonths(m);
473 int daysFromToday =
static_cast<int>(today.toJulianDay() - month.toJulianDay());
474 int weekIndex = weeks - 1 - daysFromToday / 7;
476 if (weekIndex >= 0 && weekIndex < weeks)
478 int x = startX + weekIndex * cellSize;
479 painter.drawText(x, labelY, month.toString(
"MMM"));
484 int legendX = width() - 150;
485 int legendY = labelY;
486 painter.drawText(legendX, legendY,
"Less");
487 for (
int i = 0; i < 5; ++i)
489 double intensity = i / 4.0;
490 int r =
static_cast<int>(lowActivityColor_.red() +
491 intensity * (highActivityColor_.red() - lowActivityColor_.red()));
493 static_cast<int>(lowActivityColor_.green() +
494 intensity * (highActivityColor_.green() - lowActivityColor_.green()));
495 int b =
static_cast<int>(lowActivityColor_.blue() + intensity * (highActivityColor_.blue() -
496 lowActivityColor_.blue()));
497 painter.fillRect(legendX + 30 + i * 14, legendY - 10, 12, 12, QColor(r, g, b));
499 painter.drawText(legendX + 105, legendY,
"More");
507 : QDialog(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
509 setWindowTitle(
"Library Statistics");
510 setMinimumSize(900, 700);
525void GameStatisticsDialog::setupUI()
527 auto* mainLayout =
new QVBoxLayout(
this);
528 mainLayout->setSpacing(12);
531 auto* filterLayout =
new QHBoxLayout;
532 filterLayout->addWidget(
new QLabel(
"Time Range:"));
534 timeRangeCombo_ =
new QComboBox;
535 timeRangeCombo_->addItem(
"All Time", 0);
536 timeRangeCombo_->addItem(
"Last 7 Days", 7);
537 timeRangeCombo_->addItem(
"Last 30 Days", 30);
538 timeRangeCombo_->addItem(
"Last 90 Days", 90);
539 timeRangeCombo_->addItem(
"Last Year", 365);
540 filterLayout->addWidget(timeRangeCombo_);
541 filterLayout->addStretch();
543 connect(timeRangeCombo_, QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
544 &GameStatisticsDialog::onTimeRangeChanged);
546 mainLayout->addLayout(filterLayout);
549 tabWidget_ =
new QTabWidget;
552 auto* overviewTab =
new QWidget;
553 setupOverviewTab(overviewTab);
554 tabWidget_->addTab(overviewTab,
"Overview");
557 auto* playtimeTab =
new QWidget;
558 setupPlaytimeTab(playtimeTab);
559 tabWidget_->addTab(playtimeTab,
"Playtime");
562 auto* libraryTab =
new QWidget;
563 setupLibraryTab(libraryTab);
564 tabWidget_->addTab(libraryTab,
"Library");
566 mainLayout->addWidget(tabWidget_, 1);
569 auto* buttonBox =
new QDialogButtonBox(QDialogButtonBox::Close);
570 connect(buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject);
571 mainLayout->addWidget(buttonBox);
574void GameStatisticsDialog::setupOverviewTab(QWidget* tab)
576 auto* layout =
new QVBoxLayout(tab);
579 auto* cardsLayout =
new QHBoxLayout;
581 totalGamesCard_ =
new StatCard(
"Total Games",
"0",
"\u25B6",
this, themeManager_);
582 cardsLayout->addWidget(totalGamesCard_);
584 totalPlaytimeCard_ =
new StatCard(
"Total Playtime",
"0h",
"\u23F1",
this, themeManager_);
585 cardsLayout->addWidget(totalPlaytimeCard_);
587 avgSessionCard_ =
new StatCard(
"Avg Session",
"0m",
"\u25B2",
this, themeManager_);
588 cardsLayout->addWidget(avgSessionCard_);
590 mostPlayedCard_ =
new StatCard(
"Most Played",
"-",
"\u2605",
this, themeManager_);
591 cardsLayout->addWidget(mostPlayedCard_);
593 layout->addLayout(cardsLayout);
596 auto* chartsLayout =
new QHBoxLayout;
598 auto* genreGroup =
new QGroupBox(
"By Genre");
599 auto* genreLayout =
new QVBoxLayout(genreGroup);
600 genreChart_ =
new PieChartWidget(
this, themeManager_);
601 genreLayout->addWidget(genreChart_);
602 chartsLayout->addWidget(genreGroup);
604 auto* sourceGroup =
new QGroupBox(
"By Source");
605 auto* sourceLayout =
new QVBoxLayout(sourceGroup);
606 sourceChart_ =
new PieChartWidget(
this, themeManager_);
607 sourceLayout->addWidget(sourceChart_);
608 chartsLayout->addWidget(sourceGroup);
610 layout->addLayout(chartsLayout, 1);
613void GameStatisticsDialog::setupPlaytimeTab(QWidget* tab)
615 auto* layout =
new QVBoxLayout(tab);
618 auto* cardsLayout =
new QHBoxLayout;
620 weeklyPlaytimeCard_ =
new StatCard(
"This Week",
"0h",
"\u25A0",
this, themeManager_);
621 cardsLayout->addWidget(weeklyPlaytimeCard_);
623 monthlyPlaytimeCard_ =
new StatCard(
"This Month",
"0h",
"\u25A1",
this, themeManager_);
624 cardsLayout->addWidget(monthlyPlaytimeCard_);
626 longestSessionCard_ =
new StatCard(
"Longest Session",
"0h",
"\u2605",
this, themeManager_);
627 cardsLayout->addWidget(longestSessionCard_);
629 layout->addLayout(cardsLayout);
632 auto* heatmapGroup =
new QGroupBox(
"Activity (Last 12 Months)");
633 auto* heatmapLayout =
new QVBoxLayout(heatmapGroup);
634 activityHeatmap_ =
new ActivityHeatmapWidget(
this, themeManager_);
635 heatmapLayout->addWidget(activityHeatmap_);
636 layout->addWidget(heatmapGroup);
639 auto* topGamesGroup =
new QGroupBox(
"Top 10 Most Played");
640 auto* topGamesLayout =
new QVBoxLayout(topGamesGroup);
641 topGamesChart_ =
new BarChartWidget(
this, themeManager_);
642 topGamesChart_->setMaxBars(10);
643 topGamesLayout->addWidget(topGamesChart_);
644 layout->addWidget(topGamesGroup, 1);
647void GameStatisticsDialog::setupLibraryTab(QWidget* tab)
649 auto* layout =
new QVBoxLayout(tab);
652 auto* cardsLayout =
new QHBoxLayout;
654 installedGamesCard_ =
new StatCard(
"Installed",
"0",
"\u25CF",
this, themeManager_);
655 cardsLayout->addWidget(installedGamesCard_);
657 favoriteGamesCard_ =
new StatCard(
"Favorites",
"0",
"\u2605",
this, themeManager_);
658 cardsLayout->addWidget(favoriteGamesCard_);
660 completedGamesCard_ =
new StatCard(
"Completed",
"0",
"\xE2\x9C\x93");
661 cardsLayout->addWidget(completedGamesCard_);
663 notPlayedCard_ =
new StatCard(
"Never Played",
"0",
"\u25CB",
this, themeManager_);
664 cardsLayout->addWidget(notPlayedCard_);
666 layout->addLayout(cardsLayout);
669 auto* bottomLayout =
new QHBoxLayout;
672 auto* chartsColumn =
new QVBoxLayout;
674 auto* completionGroup =
new QGroupBox(
"Completion Status");
675 auto* completionLayout =
new QVBoxLayout(completionGroup);
676 completionChart_ =
new PieChartWidget(
this, themeManager_);
677 completionLayout->addWidget(completionChart_);
678 chartsColumn->addWidget(completionGroup);
680 auto* installGroup =
new QGroupBox(
"Installation Status");
681 auto* installLayout =
new QVBoxLayout(installGroup);
682 installStatusChart_ =
new PieChartWidget(
this, themeManager_);
683 installLayout->addWidget(installStatusChart_);
684 chartsColumn->addWidget(installGroup);
686 bottomLayout->addLayout(chartsColumn);
689 auto* listsColumn =
new QVBoxLayout;
691 auto* recentlyAddedGroup =
new QGroupBox(
"Recently Added");
692 auto* recentlyAddedLayout =
new QVBoxLayout(recentlyAddedGroup);
693 recentlyAddedList_ =
new QListWidget;
694 recentlyAddedList_->setMaximumHeight(150);
695 connect(recentlyAddedList_, &QListWidget::itemDoubleClicked,
this,
696 &GameStatisticsDialog::onGameDoubleClicked);
697 recentlyAddedLayout->addWidget(recentlyAddedList_);
698 listsColumn->addWidget(recentlyAddedGroup);
700 auto* recentlyPlayedGroup =
new QGroupBox(
"Recently Played");
701 auto* recentlyPlayedLayout =
new QVBoxLayout(recentlyPlayedGroup);
702 recentlyPlayedList_ =
new QListWidget;
703 recentlyPlayedList_->setMaximumHeight(150);
704 connect(recentlyPlayedList_, &QListWidget::itemDoubleClicked,
this,
705 &GameStatisticsDialog::onGameDoubleClicked);
706 recentlyPlayedLayout->addWidget(recentlyPlayedList_);
707 listsColumn->addWidget(recentlyPlayedGroup);
709 bottomLayout->addLayout(listsColumn);
711 layout->addLayout(bottomLayout, 1);
714void GameStatisticsDialog::onTimeRangeChanged(
int index)
716 daysFilter_ = timeRangeCombo_->itemData(index).toInt();
720void GameStatisticsDialog::onGameDoubleClicked(QListWidgetItem* item)
726void GameStatisticsDialog::refreshStatistics()
728 calculateOverviewStats();
729 calculatePlaytimeStats();
730 calculateLibraryStats();
733void GameStatisticsDialog::calculateOverviewStats()
735 time_t now = time(
nullptr);
736 time_t cutoff = (daysFilter_ > 0) ? now - daysFilter_ * 86400 : 0;
739 double totalPlaytime = 0;
740 int totalSessions = 0;
741 std::map<std::string, int> genreCounts;
742 std::map<GameSource, int> sourceCounts;
743 std::string mostPlayedName;
744 double mostPlayedTime = 0;
746 for (
const auto* game : games_)
752 if (daysFilter_ > 0 && game->statistics.lastPlayed > 0 &&
753 game->statistics.lastPlayed < cutoff)
759 totalPlaytime += game->statistics.totalPlayTimeSeconds;
760 totalSessions += game->statistics.timesLaunched;
762 if (game->statistics.totalPlayTimeSeconds > mostPlayedTime)
764 mostPlayedTime = game->statistics.totalPlayTimeSeconds;
765 mostPlayedName = game->name;
769 for (
const auto& genre : game->metadata.genres)
771 genreCounts[genre]++;
775 sourceCounts[game->source]++;
779 totalGamesCard_->setValue(QString::number(totalGames));
781 totalPlaytimeCard_->setValue(formatPlaytime(totalPlaytime));
783 if (totalSessions > 0)
785 double avgSession = totalPlaytime / totalSessions;
786 avgSessionCard_->setValue(formatPlaytime(avgSession));
790 avgSessionCard_->setValue(
"0m");
793 if (!mostPlayedName.empty())
795 QString truncated = QString::fromStdString(mostPlayedName);
796 if (truncated.length() > 15)
798 truncated = truncated.left(12) +
"...";
800 mostPlayedCard_->setValue(truncated);
801 mostPlayedCard_->setSubtext(formatPlaytime(mostPlayedTime));
805 mostPlayedCard_->setValue(
"-");
809 std::vector<PieChartWidget::Slice> genreSlices;
810 std::vector<std::pair<std::string, int>> sortedGenres(genreCounts.begin(), genreCounts.end());
811 std::sort(sortedGenres.begin(), sortedGenres.end(),
812 [](
const auto& a,
const auto& b) { return a.second > b.second; });
814 QList<QColor> colors = {QColor(
"#6366f1"), QColor(
"#8b5cf6"), QColor(
"#a855f7"),
815 QColor(
"#d946ef"), QColor(
"#ec4899"), QColor(
"#f43f5e"),
816 QColor(
"#ef4444"), QColor(
"#f97316"), QColor(
"#f59e0b")};
819 for (
size_t i = 0; i < qMin(sortedGenres.size(),
size_t(8)); ++i)
821 genreSlices.push_back({QString::fromStdString(sortedGenres[i].first),
822 static_cast<double>(sortedGenres[i].second),
823 colors[colorIdx++ % colors.size()]});
825 genreChart_->setData(genreSlices);
828 std::vector<PieChartWidget::Slice> sourceSlices;
829 sourceSlices.reserve(sourceCounts.size());
830 auto sourceColor = [](
GameSource src) -> QColor
835 return QColor(
"#1b2838");
837 return QColor(
"#86328a");
839 return QColor(
"#2a2a2a");
841 return QColor(
"#fa5c5c");
843 return QColor(
"#ff9900");
845 return QColor(
"#4ade80");
847 return QColor(
"#4a86cf");
849 return QColor(
"#6b7280");
852 auto sourceName = [](
GameSource src) -> QString
875 for (
const auto& [source, count] : sourceCounts)
877 sourceSlices.push_back(
878 {sourceName(source),
static_cast<double>(count), sourceColor(source)});
880 sourceChart_->setData(sourceSlices);
883void GameStatisticsDialog::calculatePlaytimeStats()
885 time_t now = time(
nullptr);
886 time_t weekAgo = now - 7 * 86400;
887 time_t monthAgo = now - 30 * 86400;
889 double weeklyPlaytime = 0;
890 double monthlyPlaytime = 0;
891 double longestSession = 0;
892 std::map<int, double> dailyActivity;
895 std::vector<std::pair<std::string, double>> gamePlaytimes;
897 for (
const auto* game : games_)
902 gamePlaytimes.push_back({game->name, game->statistics.totalPlayTimeSeconds});
905 if (game->statistics.lastPlayed >= weekAgo)
907 weeklyPlaytime += game->statistics.totalPlayTimeSeconds / 10;
909 if (game->statistics.lastPlayed >= monthAgo)
911 monthlyPlaytime += game->statistics.totalPlayTimeSeconds / 30;
915 if (game->statistics.timesLaunched > 0)
917 double avgSession = game->statistics.totalPlayTimeSeconds /
918 static_cast<double>(game->statistics.timesLaunched);
919 if (avgSession > longestSession)
920 longestSession = avgSession;
924 if (game->statistics.lastPlayed > 0)
926 int daysAgo =
static_cast<int>((now - game->statistics.lastPlayed) / 86400);
929 double estimatedDaily = game->statistics.totalPlayTimeSeconds / 3600.0 /
930 qMax(1, game->statistics.timesLaunched);
931 dailyActivity[daysAgo] += estimatedDaily;
937 weeklyPlaytimeCard_->setValue(formatPlaytime(weeklyPlaytime));
938 monthlyPlaytimeCard_->setValue(formatPlaytime(monthlyPlaytime));
939 longestSessionCard_->setValue(formatPlaytime(longestSession));
942 activityHeatmap_->setActivityData(dailyActivity);
945 std::sort(gamePlaytimes.begin(), gamePlaytimes.end(),
946 [](
const auto& a,
const auto& b) { return a.second > b.second; });
948 std::vector<BarChartWidget::Bar> bars;
949 for (
size_t i = 0; i < qMin(gamePlaytimes.size(),
size_t(10)); ++i)
951 if (gamePlaytimes[i].second > 0)
953 bars.push_back({QString::fromStdString(gamePlaytimes[i].first), gamePlaytimes[i].second,
954 formatPlaytime(gamePlaytimes[i].second)});
957 topGamesChart_->setData(bars);
960void GameStatisticsDialog::calculateLibraryStats()
967 std::map<CompletionStatus, int> completionCounts;
968 std::map<GameStatus, int> statusCounts;
970 std::vector<std::pair<time_t, const Game*>> recentlyAdded;
971 std::vector<std::pair<time_t, const Game*>> recentlyPlayed;
973 for (
const auto* game : games_)
978 if (game->isInstalled())
982 if (game->isFavorite)
990 if (game->statistics.timesLaunched == 0)
995 completionCounts[game->completionStatus]++;
996 statusCounts[game->status]++;
998 if (game->statistics.dateAdded > 0)
1000 recentlyAdded.push_back({game->statistics.dateAdded, game});
1002 if (game->statistics.lastPlayed > 0)
1004 recentlyPlayed.push_back({game->statistics.lastPlayed, game});
1009 installedGamesCard_->setValue(QString::number(installed));
1010 favoriteGamesCard_->setValue(QString::number(favorites));
1011 completedGamesCard_->setValue(QString::number(completed));
1012 notPlayedCard_->setValue(QString::number(notPlayed));
1020 return QColor(
"#6b7280");
1022 return QColor(
"#3b82f6");
1024 return QColor(
"#10b981");
1026 return QColor(
"#22c55e");
1028 return QColor(
"#f59e0b");
1030 return QColor(
"#ef4444");
1032 return QColor(
"#8b5cf6");
1034 return QColor(
"#9ca3af");
1042 return "Not Played";
1060 std::vector<PieChartWidget::Slice> completionSlices;
1061 completionSlices.reserve(completionCounts.size());
1062 for (
const auto& [status, count] : completionCounts)
1064 completionSlices.push_back(
1065 {completionName(status),
static_cast<double>(count), completionColor(status)});
1067 completionChart_->setData(completionSlices);
1070 auto statusColor = [](
GameStatus status) -> QColor
1075 return QColor(
"#6b7280");
1077 return QColor(
"#22c55e");
1079 return QColor(
"#fbbf24");
1081 return QColor(
"#3b82f6");
1083 return QColor(
"#f59e0b");
1085 return QColor(
"#8b5cf6");
1087 return QColor(
"#ec4899");
1089 return QColor(
"#9ca3af");
1092 auto statusName = [](
GameStatus status) -> QString
1097 return "Not Installed";
1107 return "Installing";
1115 std::vector<PieChartWidget::Slice> statusSlices;
1116 statusSlices.reserve(statusCounts.size());
1117 for (
const auto& [status, count] : statusCounts)
1119 statusSlices.push_back(
1120 {statusName(status),
static_cast<double>(count), statusColor(status)});
1122 installStatusChart_->setData(statusSlices);
1125 std::sort(recentlyAdded.begin(), recentlyAdded.end(),
1126 [](
const auto& a,
const auto& b) { return a.first > b.first; });
1127 recentlyAddedList_->clear();
1128 for (
size_t i = 0; i < qMin(recentlyAdded.size(),
size_t(10)); ++i)
1130 auto* item =
new QListWidgetItem(QString::fromStdString(recentlyAdded[i].second->name));
1131 item->setData(Qt::UserRole, QString::fromStdString(recentlyAdded[i].second->id));
1132 item->setToolTip(QString(
"Added: %1").arg(formatDate(recentlyAdded[i].first)));
1133 recentlyAddedList_->addItem(item);
1137 std::sort(recentlyPlayed.begin(), recentlyPlayed.end(),
1138 [](
const auto& a,
const auto& b) { return a.first > b.first; });
1139 recentlyPlayedList_->clear();
1140 for (
size_t i = 0; i < qMin(recentlyPlayed.size(),
size_t(10)); ++i)
1142 auto* item =
new QListWidgetItem(QString::fromStdString(recentlyPlayed[i].second->name));
1143 item->setData(Qt::UserRole, QString::fromStdString(recentlyPlayed[i].second->id));
1144 item->setToolTip(QString(
"Last played: %1").arg(formatDate(recentlyPlayed[i].first)));
1145 recentlyPlayedList_->addItem(item);
1149QString GameStatisticsDialog::formatPlaytime(
double seconds)
const
1153 return QString(
"%1s").arg(
static_cast<int>(seconds));
1155 else if (seconds < 3600)
1157 return QString(
"%1m").arg(
static_cast<int>(seconds / 60));
1161 double hours = seconds / 3600;
1164 return QString(
"%1h").arg(
static_cast<int>(hours));
1166 return QString(
"%1h").arg(hours, 0,
'f', 1);
1170QString GameStatisticsDialog::formatDate(time_t timestamp)
const
1174 return QDateTime::fromSecsSinceEpoch(timestamp).toString(
"yyyy-MM-dd");
1177void GameStatisticsDialog::applyTheme()
1179 const auto& c = themeManager_->colors();
1181 setStyleSheet(QString(
"QDialog { background-color: %1; }").arg(c.windowBackground.name()));
1184 tabWidget_->setStyleSheet(QString(
"QTabWidget::pane {"
1185 " border: 1px solid %1;"
1186 " border-radius: 6px;"
1192 " padding: 8px 20px;"
1193 " border: 1px solid %1;"
1194 " border-bottom: none;"
1195 " border-top-left-radius: 4px;"
1196 " border-top-right-radius: 4px;"
1198 "QTabBar::tab:selected {"
1202 "QTabBar::tab:hover:!selected {"
1205 .arg(c.border.name())
1206 .arg(c.panelBackground.name())
1207 .arg(c.inputBackground.name())
1208 .arg(c.textSecondary.name())
1209 .arg(c.textPrimary.name())
1210 .arg(c.hoverBackground.name()));
1213 QString groupStyle = QString(
"QGroupBox {"
1214 " font-weight: bold;"
1216 " border: 1px solid %2;"
1217 " border-radius: 6px;"
1219 " padding-top: 10px;"
1221 "QGroupBox::title {"
1222 " subcontrol-origin: margin;"
1226 .arg(c.textPrimary.name())
1227 .arg(c.border.name());
1229 for (
auto* groupBox : findChildren<QGroupBox*>())
1231 groupBox->setStyleSheet(groupStyle);
1235 QString listStyle = QString(
"QListWidget {"
1236 " background-color: %1;"
1237 " border: 1px solid %2;"
1238 " border-radius: 4px;"
1241 "QListWidget::item {"
1244 "QListWidget::item:selected {"
1245 " background-color: %4;"
1248 "QListWidget::item:hover:!selected {"
1249 " background-color: %6;"
1251 .arg(c.inputBackground.name())
1252 .arg(c.border.name())
1253 .arg(c.textPrimary.name())
1254 .arg(c.accent.name())
1255 .arg(c.textOnAccent.name())
1256 .arg(c.hoverBackground.name());
1258 recentlyAddedList_->setStyleSheet(listStyle);
1259 recentlyPlayedList_->setStyleSheet(listStyle);
1262 timeRangeCombo_->setStyleSheet(QString(
"QComboBox {"
1263 " background-color: %1;"
1265 " border: 1px solid %3;"
1266 " border-radius: 4px;"
1267 " padding: 5px 10px;"
1270 " border-color: %4;"
1272 "QComboBox::drop-down {"
1275 "QComboBox QAbstractItemView {"
1276 " background-color: %1;"
1278 " selection-background-color: %4;"
1280 .arg(c.inputBackground.name())
1281 .arg(c.textPrimary.name())
1282 .arg(c.border.name())
1283 .arg(c.accent.name()));
void setGames(const std::vector< Game * > &games)
GameStatisticsDialog(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void setValue(const QString &value)
void setSubtext(const QString &subtext)
StatCard(const QString &label, const QString &value, const QString &icon="", QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void themeChanged()
Emitted when theme or accent color changes.
GameSource
Where the game was acquired from (store/service/local).
@ Steam
Steam store/platform.
@ Local
Manually added game (not from a store).
@ Itch
itch.io indie games
@ Lutris
Lutris community scripts.
@ GOG
GOG.com (DRM-free games).
@ Flatpak
Flatpak package.
GameStatus
Current installation and execution status of the game.
@ Installing
Game is being installed.
@ NotInstalled
Game is in library but not installed.
@ Running
Game is currently running.
@ Closing
Game is shutting down (waiting for process to terminate).
@ Updating
Game is being updated.
@ Installed
Game is installed and ready to play.
@ Launching
Game is being launched (waiting for process to start).
CompletionStatus
User's completion/play status for tracking backlog.
@ Played
Played but no clear completion criteria (e.g., multiplayer).
@ Completed
100% completion (achievements, side content, etc.)
@ Abandoned
Stopped playing, unlikely to return.
@ Beaten
Main story/campaign completed.
@ OnHold
Temporarily paused.
@ Playing
Currently playing.
@ NotPlayed
Never played (backlog).