game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_list_delegate.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/game_list_delegate.cpp
4// ==============================================================================
5// Custom delegate for rendering game list items with rich visual information
6// ==============================================================================
7
9#include "icon_font.h"
10#include "mainwindow.h" // For ViewMode enum
11
12#include <QDateTime>
13#include <QListWidget>
14#include <QMetaObject>
15#include <QStyle>
16
17// Returns black or white depending on background luminance for readable text
18static QColor contrastTextColor(const QColor& bg)
19{
20 double lum = 0.299 * bg.redF() + 0.587 * bg.greenF() + 0.114 * bg.blueF();
21 return lum > 0.5 ? QColor(0, 0, 0) : QColor(255, 255, 255);
22}
23
24// ==============================================================================
25// Construction
26// ==============================================================================
27
29 : QStyledItemDelegate(parent), viewMode_(ViewMode::List), zoomLevel_(100)
30{
33}
34
36{
37 if (zoomLevel_ != level)
38 {
39 zoomLevel_ = level;
41 }
42}
43
45{
46 double zoom = zoomFactor();
47
48 nameFont_ = QFont();
49 nameFont_.setBold(true);
50 nameFont_.setPointSize(static_cast<int>(11 * zoom));
51 // nameFontSelected_ removed — uniform row height, single font for all states
52
53 infoFont_ = QFont();
54 infoFont_.setPointSize(static_cast<int>(9 * zoom));
55
56 smallFont_ = QFont();
57 smallFont_.setPointSize(static_cast<int>(9 * zoom));
58}
59
60// ==============================================================================
61// Size Hint
62// ==============================================================================
63
64QSize GameListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
65{
66 Q_UNUSED(index);
67 bool isSelected = (option.state & QStyle::State_Selected);
68 double zoom = zoomFactor();
69
70 switch (viewMode_)
71 {
72 case ViewMode::List:
73 {
74 Q_UNUSED(isSelected);
75 int height = static_cast<int>(56 * zoom);
76 return QSize(400, height);
77 }
79 {
80 Q_UNUSED(isSelected);
81 int height = static_cast<int>(36 * zoom);
82 return QSize(300, height);
83 }
84 case ViewMode::Grid:
85 default:
86 return QStyledItemDelegate::sizeHint(option, index);
87 }
88}
89
90// ==============================================================================
91// Paint
92// ==============================================================================
93
94void GameListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
95 const QModelIndex& index) const
96{
97 painter->save();
98 painter->setRenderHint(QPainter::Antialiasing);
99
100 if (viewMode_ == ViewMode::Grid)
101 {
102 paintGridMode(painter, option, index);
103 painter->restore();
104 return;
105 }
106
107 const ThemeColors& c = colors();
108 bool isSelected = (option.state & QStyle::State_Selected);
109
110 if (isSelected)
111 {
112 QRect bgRect = option.rect.adjusted(2, 2, -2, -2);
113 QColor glowColor = c.accent;
114 glowColor.setAlpha(40);
115 painter->fillRect(option.rect, glowColor);
116
117 QPainterPath bgPath;
118 bgPath.addRoundedRect(bgRect, 6, 6);
119 painter->fillPath(bgPath, c.accent);
120
121 painter->setPen(QPen(c.accent.lighter(130), 2));
122 painter->drawPath(bgPath);
123 }
124 else if (option.state & QStyle::State_MouseOver)
125 {
126 QRect bgRect = option.rect.adjusted(2, 2, -2, -2);
127 QPainterPath bgPath;
128 bgPath.addRoundedRect(bgRect, 4, 4);
129 painter->fillPath(bgPath, c.hoverBackground);
130 }
131
132 QRect rect = option.rect.adjusted(4, 4, -4, -4);
133
134 QString name = index.data(Qt::DisplayRole).toString();
135 QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
136 QString source = index.data(GameSourceRole).toString();
137 QString runner = index.data(GameRunnerRole).toString();
138 int playTimeSeconds = index.data(GamePlayTimeRole).toInt();
139 qint64 lastPlayed = index.data(GameLastPlayedRole).toLongLong();
140 int launchCount = index.data(GameLaunchCountRole).toInt();
141 bool isRunning = index.data(GameIsRunningRole).toBool();
142 bool isFavorite = index.data(GameIsFavoriteRole).toBool();
143 bool isInstalled = index.data(GameIsInstalledRole).toBool();
144 bool isSteam = index.data(GameIsSteamRole).toBool();
145 bool needsUpdate = index.data(GameNeedsUpdateRole).toBool();
146 bool isDownloading = index.data(GameIsDownloadingRole).toBool();
147 double downloadProgress = index.data(GameDownloadProgressRole).toDouble();
148 bool hasCloudSave = index.data(GameCloudSaveEnabledRole).toBool();
149 qint64 installSize = index.data(GameInstallSizeRole).toLongLong();
150 QString protondbTier = index.data(GameProtonDBTierRole).toString();
151
152 if (viewMode_ == ViewMode::List)
153 {
154 paintListMode(painter, rect, icon, name, source, runner, playTimeSeconds, lastPlayed,
155 launchCount, isRunning, isFavorite, isInstalled, isSteam, isSelected,
156 needsUpdate, isDownloading, downloadProgress, hasCloudSave, installSize,
157 protondbTier);
158 }
159 else if (viewMode_ == ViewMode::Compact)
160 {
161 paintCompactMode(painter, rect, icon, name, source, runner, playTimeSeconds, isRunning,
162 isFavorite, isInstalled, isSteam, isSelected, needsUpdate, isDownloading,
163 downloadProgress, hasCloudSave, protondbTier);
164 }
165
166 painter->restore();
167}
168
169// ==============================================================================
170// List Mode
171// ==============================================================================
172
173void GameListDelegate::paintListMode(QPainter* painter, const QRect& rect, const QIcon& icon,
174 const QString& name, const QString& source,
175 const QString& runner, int playTimeSeconds, qint64 lastPlayed,
176 int launchCount, bool isRunning, bool isFavorite,
177 bool isInstalled, bool isSteam, bool isSelected,
178 bool needsUpdate, bool isDownloading, double downloadProgress,
179 bool hasCloudSave, qint64 installSize,
180 const QString& protondbTier) const
181{
182 Q_UNUSED(installSize)
183 Q_UNUSED(launchCount)
184 const ThemeColors& c = colors();
185 double zoom = zoomFactor();
186
187 QColor textPrimary = isSelected ? c.textOnAccent : c.textPrimary;
188 QColor textMuted = isSelected ? c.textOnAccent.darker(110) : c.textMuted;
189
190 int iconWidth = static_cast<int>(64 * zoom);
191 int iconHeight = static_cast<int>(36 * zoom);
192 int padding = static_cast<int>(8 * zoom);
193 int textX = rect.left() + iconWidth + padding * 2;
194
195 // Draw horizontal thumbnail with rounded corners
196 if (!icon.isNull())
197 {
198 QRect iconRect(rect.left() + padding, rect.top() + (rect.height() - iconHeight) / 2,
199 iconWidth, iconHeight);
200
201 QPixmap pixmap = icon.pixmap(iconRect.size());
202 if (!pixmap.isNull())
203 {
204 int cornerRadius = static_cast<int>(4 * zoom);
205 QPainterPath path;
206 path.addRoundedRect(iconRect, cornerRadius, cornerRadius);
207 painter->setClipPath(path);
208 painter->drawPixmap(iconRect, pixmap);
209 painter->setClipping(false);
210
211 painter->setPen(QPen(isSelected ? c.accent : c.border, 1));
212 painter->setBrush(Qt::NoBrush);
213 painter->drawRoundedRect(iconRect, cornerRadius, cornerRadius);
214 }
215 }
216
217 // Row 1: Name + favorite star + running indicator
218 painter->setFont(nameFont_);
219
220 int row1Y = rect.top() + static_cast<int>(6 * zoom);
221 int textWidth = rect.width() - textX - padding;
222
223 if (isFavorite)
224 {
225 painter->setPen(c.favorite);
226 painter->drawText(textX, row1Y, 20, 20, Qt::AlignLeft | Qt::AlignVCenter, "★");
227 textX += 18;
228 textWidth -= 18;
229 }
230
231 painter->setPen(textPrimary);
232 QString elidedName = painter->fontMetrics().elidedText(name, Qt::ElideRight, textWidth - 100);
233 painter->drawText(textX, row1Y, textWidth - 100, 20, Qt::AlignLeft | Qt::AlignVCenter,
234 elidedName);
235
236 // Status badges (right side of row 1)
237 int statusX = rect.right() - padding;
238 QFont badgeFont = smallFont_;
239 badgeFont.setBold(true);
240 badgeFont.setPointSize(static_cast<int>(8 * zoom));
241 painter->setFont(badgeFont);
242
243 int statusBadgeH = static_cast<int>(18 * zoom);
244 double statusBadgeR = statusBadgeH / 2.0;
245 int badgeY = row1Y + (20 - statusBadgeH) / 2;
246
247 if (isRunning)
248 {
249 QString text = "▶ RUNNING";
250 int badgeWidth =
251 painter->fontMetrics().horizontalAdvance(text) + static_cast<int>(10 * zoom);
252 QRect badgeRect(statusX - badgeWidth, badgeY, badgeWidth, statusBadgeH);
253 painter->setPen(Qt::NoPen);
254 painter->setBrush(isSelected ? c.success.lighter(120) : c.success);
255 painter->drawRoundedRect(badgeRect, statusBadgeR, statusBadgeR);
256 painter->setPen(c.textOnAccent);
257 painter->drawText(badgeRect, Qt::AlignCenter, text);
258 statusX = badgeRect.left() - static_cast<int>(4 * zoom);
259 }
260 else if (isDownloading)
261 {
262 QString text = QStringLiteral("⬇ %1%").arg(static_cast<int>(downloadProgress * 100));
263 int badgeWidth =
264 painter->fontMetrics().horizontalAdvance(text) + static_cast<int>(10 * zoom);
265 QRect badgeRect(statusX - badgeWidth, badgeY, badgeWidth, statusBadgeH);
266 painter->setPen(Qt::NoPen);
267 painter->setBrush(isSelected ? c.accent.lighter(120) : c.accent);
268 painter->drawRoundedRect(badgeRect, statusBadgeR, statusBadgeR);
269 painter->setPen(c.textOnAccent);
270 painter->drawText(badgeRect, Qt::AlignCenter, text);
271 statusX = badgeRect.left() - static_cast<int>(4 * zoom);
272 }
273 else if (needsUpdate)
274 {
275 QString text = QStringLiteral("⬆ UPDATE");
276 int badgeWidth =
277 painter->fontMetrics().horizontalAdvance(text) + static_cast<int>(10 * zoom);
278 QRect badgeRect(statusX - badgeWidth, badgeY, badgeWidth, statusBadgeH);
279 painter->setPen(Qt::NoPen);
280 painter->setBrush(isSelected ? c.warning.lighter(120) : c.warning);
281 painter->drawRoundedRect(badgeRect, statusBadgeR, statusBadgeR);
282 painter->setPen(c.textOnAccent);
283 painter->drawText(badgeRect, Qt::AlignCenter, text);
284 statusX = badgeRect.left() - static_cast<int>(4 * zoom);
285 }
286
287 if (hasCloudSave)
288 {
289 painter->setFont(smallFont_);
290 painter->setPen(isSelected ? c.textOnAccent : c.textMuted);
291 painter->drawText(statusX - 20, row1Y, 20, 20, Qt::AlignCenter, "☁");
292 }
293
294 // Row 2: Platform | Source | Runner | Installed status | Play time | Last played
295 painter->setFont(infoFont_);
296
297 int row2Y = row1Y + static_cast<int>(22 * zoom);
298 textX = rect.left() + iconWidth + padding * 2;
299 if (isFavorite)
300 textX += static_cast<int>(18 * zoom);
301
302 // Draw metadata segments with visual hierarchy
303 QColor textSecondary = isSelected ? c.textOnAccent : c.textSecondary;
304 int segX = textX;
305 QString separator = QStringLiteral(" \u2022 ");
306 int sepWidth = QFontMetrics(infoFont_).horizontalAdvance(separator);
307
308 // Source + runner in muted color
309 QString sourceRunner;
310 if (!source.isEmpty())
311 sourceRunner = source;
312 if (!runner.isEmpty() && runner != source)
313 {
314 if (!sourceRunner.isEmpty())
315 sourceRunner += " \u2022 ";
316 sourceRunner += runner;
317 }
318
319 if (!sourceRunner.isEmpty())
320 {
321 painter->setPen(textSecondary);
322 painter->drawText(segX, row2Y, textWidth, 18, Qt::AlignLeft | Qt::AlignVCenter,
323 sourceRunner);
324 segX += painter->fontMetrics().horizontalAdvance(sourceRunner);
325 }
326
327 // Playtime in brighter secondary color
328 if (!(isSteam && !isInstalled) && playTimeSeconds > 0)
329 {
330 int hours = playTimeSeconds / 3600;
331 int mins = (playTimeSeconds % 3600) / 60;
332 QString timeStr = hours > 0 ? QStringLiteral("%1h %2m").arg(hours).arg(mins)
333 : QStringLiteral("%1m").arg(mins);
334 if (segX > textX)
335 {
336 painter->setPen(textMuted);
337 painter->drawText(segX, row2Y, sepWidth, 18, Qt::AlignCenter, separator);
338 segX += sepWidth;
339 }
340 painter->setPen(textSecondary);
341 painter->drawText(segX, row2Y, textWidth - (segX - textX), 18,
342 Qt::AlignLeft | Qt::AlignVCenter, timeStr);
343 segX += painter->fontMetrics().horizontalAdvance(timeStr);
344 }
345
346 // Last played in muted color
347 if (lastPlayed > 0)
348 {
349 QDateTime dt = QDateTime::fromSecsSinceEpoch(lastPlayed);
350 QDateTime now = QDateTime::currentDateTime();
351 auto daysAgo = dt.daysTo(now);
352 QString dateStr;
353 if (daysAgo == 0)
354 dateStr = QStringLiteral("Today");
355 else if (daysAgo == 1)
356 dateStr = QStringLiteral("Yesterday");
357 else if (daysAgo < 7)
358 dateStr = QStringLiteral("%1d ago").arg(daysAgo);
359 else
360 dateStr = dt.toString(QStringLiteral("MMM d"));
361 if (segX > textX)
362 {
363 painter->setPen(textMuted);
364 painter->drawText(segX, row2Y, sepWidth, 18, Qt::AlignCenter, separator);
365 segX += sepWidth;
366 }
367 painter->setPen(textSecondary);
368 painter->drawText(segX, row2Y, textWidth - (segX - textX), 18,
369 Qt::AlignLeft | Qt::AlignVCenter, dateStr);
370 segX += painter->fontMetrics().horizontalAdvance(dateStr);
371 }
372
373 if (isSteam && !isInstalled)
374 {
375 if (segX > textX)
376 {
377 painter->setPen(textMuted);
378 painter->drawText(segX, row2Y, sepWidth, 18, Qt::AlignCenter, separator);
379 segX += sepWidth;
380 }
381 int notInstW =
382 painter->fontMetrics().horizontalAdvance("Not Installed") + static_cast<int>(8 * zoom);
383 int notInstH = static_cast<int>(16 * zoom);
384 QRect notInstRect(segX, row2Y + (18 - notInstH) / 2, notInstW, notInstH);
385 QColor badgeBg = isSelected ? c.warning.lighter(130) : c.warning;
386 badgeBg.setAlpha(isSelected ? 180 : 40);
387 painter->setPen(Qt::NoPen);
388 painter->setBrush(badgeBg);
389 painter->drawRoundedRect(notInstRect, notInstH / 2.0, notInstH / 2.0);
390 painter->setPen(isSelected ? c.textOnAccent : c.warning);
391 painter->drawText(notInstRect, Qt::AlignCenter, "Not Installed");
392 }
393
394 // ProtonDB tier badge (right side of row 2)
395 if (!protondbTier.isEmpty() && protondbTier != "native")
396 {
397 QColor tierColor = getProtonDBTierColor(protondbTier);
398 QString tierText = protondbTier.left(1).toUpper() + protondbTier.mid(1);
399
400 QFont tierFont = painter->font();
401 tierFont.setPointSize(static_cast<int>(7 * zoom));
402 tierFont.setBold(true);
403 painter->setFont(tierFont);
404 int badgeWidth =
405 painter->fontMetrics().horizontalAdvance(tierText) + static_cast<int>(8 * zoom);
406 int badgeHeight = static_cast<int>(16 * zoom);
407
408 QRect tierRect(rect.right() - badgeWidth - padding, row2Y + (18 - badgeHeight) / 2,
409 badgeWidth, badgeHeight);
410 painter->setPen(Qt::NoPen);
411 painter->setBrush(tierColor);
412 painter->drawRoundedRect(tierRect, 3, 3);
413 painter->setPen(contrastTextColor(tierColor));
414 painter->drawText(tierRect, Qt::AlignCenter, tierText);
415 }
416}
417
418// ==============================================================================
419// Compact Mode
420// ==============================================================================
421
422void GameListDelegate::paintCompactMode(QPainter* painter, const QRect& rect, const QIcon& icon,
423 const QString& name, const QString& source,
424 const QString& runner, int playTimeSeconds, bool isRunning,
425 bool isFavorite, bool isInstalled, bool isSteam,
426 bool isSelected, bool needsUpdate, bool isDownloading,
427 double downloadProgress, bool hasCloudSave,
428 const QString& protondbTier) const
429{
430 const ThemeColors& c = colors();
431 double zoom = zoomFactor();
432
433 QColor textPrimary = isSelected ? c.textOnAccent : c.textPrimary;
434 QColor textSecondary = isSelected ? c.textOnAccent : c.textSecondary;
435
436 int iconWidth = static_cast<int>(44 * zoom);
437 int iconHeight = static_cast<int>(25 * zoom);
438 int padding = static_cast<int>(6 * zoom);
439 int textX = rect.left() + iconWidth + padding * 2;
440
441 if (!icon.isNull())
442 {
443 QRect iconRect(rect.left() + padding, rect.top() + (rect.height() - iconHeight) / 2,
444 iconWidth, iconHeight);
445
446 if (isSelected)
447 {
448 QPixmap pixmap = icon.pixmap(iconRect.size());
449 if (!pixmap.isNull())
450 {
451 QPainterPath path;
452 path.addRoundedRect(iconRect, 4, 4);
453 painter->setClipPath(path);
454 painter->drawPixmap(iconRect, pixmap);
455 painter->setClipping(false);
456 }
457 }
458 else
459 {
460 icon.paint(painter, iconRect);
461 }
462 }
463
464 if (isFavorite)
465 {
466 painter->setPen(c.favorite);
467 QFont starFont = painter->font();
468 starFont.setPointSize(static_cast<int>(9 * zoom));
469 painter->setFont(starFont);
470 int starWidth = static_cast<int>(16 * zoom);
471 painter->drawText(textX, rect.top(), starWidth, rect.height(),
472 Qt::AlignLeft | Qt::AlignVCenter, "★");
473 textX += static_cast<int>(14 * zoom);
474 }
475
476 QFont nameFont = painter->font();
477 nameFont.setPointSize(static_cast<int>(10 * zoom));
478 painter->setFont(nameFont);
479 painter->setPen(textPrimary);
480
481 int nameWidth = static_cast<int>((rect.width() - textX) * 0.4);
482 QString elidedName = painter->fontMetrics().elidedText(name, Qt::ElideRight, nameWidth);
483 painter->drawText(textX, rect.top(), nameWidth, rect.height(), Qt::AlignLeft | Qt::AlignVCenter,
484 elidedName);
485
486 int midX = textX + nameWidth + padding;
487 int midWidth = static_cast<int>((rect.width() - textX) * 0.25);
488 painter->setPen(textSecondary);
489 QFont smallFont = painter->font();
490 smallFont.setPointSize(static_cast<int>(9 * zoom));
491 painter->setFont(smallFont);
492
493 QString sourceRunner = source;
494 if (!runner.isEmpty() && runner != source)
495 sourceRunner += " • " + runner;
496 sourceRunner = painter->fontMetrics().elidedText(sourceRunner, Qt::ElideRight, midWidth);
497 painter->drawText(midX, rect.top(), midWidth, rect.height(), Qt::AlignLeft | Qt::AlignVCenter,
498 sourceRunner);
499
500 // ProtonDB tier badge
501 if (!protondbTier.isEmpty() && protondbTier != "native")
502 {
503 QColor tierColor = getProtonDBTierColor(protondbTier);
504 QString tierText = protondbTier.left(1).toUpper() + protondbTier.mid(1);
505
506 int srWidth = painter->fontMetrics().horizontalAdvance(sourceRunner);
507 QFont tierFont = painter->font();
508 tierFont.setPointSize(static_cast<int>(7 * zoom));
509 tierFont.setBold(true);
510 painter->setFont(tierFont);
511 int badgeWidth =
512 painter->fontMetrics().horizontalAdvance(tierText) + static_cast<int>(6 * zoom);
513 int badgeHeight = static_cast<int>(14 * zoom);
514 int badgeX = midX + srWidth + static_cast<int>(4 * zoom);
515
516 if (badgeX + badgeWidth < midX + midWidth)
517 {
518 QRect tierRect(badgeX, rect.top() + (rect.height() - badgeHeight) / 2, badgeWidth,
519 badgeHeight);
520 painter->setPen(Qt::NoPen);
521 painter->setBrush(tierColor);
522 painter->drawRoundedRect(tierRect, 3, 3);
523 painter->setPen(contrastTextColor(tierColor));
524 painter->drawText(tierRect, Qt::AlignCenter, tierText);
525 }
526 }
527
528 int rightX = midX + midWidth + padding;
529 int rightWidth = rect.right() - rightX - padding;
530
531 if (hasCloudSave)
532 {
533 painter->setPen(isSelected ? c.textOnAccent : c.textMuted);
534 painter->drawText(rightX, rect.top(), 16, rect.height(), Qt::AlignCenter, "☁");
535 rightX += 18;
536 rightWidth -= 18;
537 }
538
539 if (isRunning)
540 {
541 painter->setPen(isSelected ? c.textOnAccent : c.success);
542 painter->drawText(rightX, rect.top(), rightWidth, rect.height(),
543 Qt::AlignRight | Qt::AlignVCenter, QStringLiteral("▶ RUNNING"));
544 }
545 else if (isDownloading)
546 {
547 painter->setPen(isSelected ? c.textOnAccent : c.accent);
548 QString progressText =
549 QStringLiteral("⬇ %1%").arg(static_cast<int>(downloadProgress * 100));
550 painter->drawText(rightX, rect.top(), rightWidth, rect.height(),
551 Qt::AlignRight | Qt::AlignVCenter, progressText);
552 }
553 else if (needsUpdate)
554 {
555 painter->setPen(isSelected ? c.textOnAccent : c.warning);
556 painter->drawText(rightX, rect.top(), rightWidth, rect.height(),
557 Qt::AlignRight | Qt::AlignVCenter, QStringLiteral("⬆ UPDATE"));
558 }
559 else if (isSteam && !isInstalled)
560 {
561 painter->setPen(isSelected ? c.textOnAccent : c.warning);
562 painter->drawText(rightX, rect.top(), rightWidth, rect.height(),
563 Qt::AlignRight | Qt::AlignVCenter, QStringLiteral("Not Installed"));
564 }
565 else if (playTimeSeconds > 0)
566 {
567 int hours = playTimeSeconds / 3600;
568 int mins = (playTimeSeconds % 3600) / 60;
569 QString timeStr = hours > 0 ? QStringLiteral("%1h %2m").arg(hours).arg(mins)
570 : QStringLiteral("%1m").arg(mins);
571 painter->setPen(isSelected ? c.textOnAccent : c.info);
572 painter->drawText(rightX, rect.top(), rightWidth, rect.height(),
573 Qt::AlignRight | Qt::AlignVCenter, timeStr);
574 }
575}
576
577// ==============================================================================
578// Grid Mode
579// ==============================================================================
580
581void GameListDelegate::paintGridMode(QPainter* painter, const QStyleOptionViewItem& option,
582 const QModelIndex& index) const
583{
584 const ThemeColors& c = colors();
585 QRect rect = option.rect;
586 bool isSelected = (option.state & QStyle::State_Selected);
587 bool isHovered = (option.state & QStyle::State_MouseOver);
588
589 QString name = index.data(Qt::DisplayRole).toString();
590 QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
591 bool isRunning = index.data(GameIsRunningRole).toBool();
592 bool isFavorite = index.data(GameIsFavoriteRole).toBool();
593 bool isInstalled = index.data(GameIsInstalledRole).toBool();
594 bool isSteam = index.data(GameIsSteamRole).toBool();
595 QString protondbTier = index.data(GameProtonDBTierRole).toString();
596 QString source = index.data(GameSourceRole).toString();
597 bool needsUpdate = index.data(GameNeedsUpdateRole).toBool();
598 bool isDownloading = index.data(GameIsDownloadingRole).toBool();
599 double downloadProgress = index.data(GameDownloadProgressRole).toDouble();
600 bool hasCloudSave = index.data(GameCloudSaveEnabledRole).toBool();
601
602 if (isSelected)
603 {
604 QColor glowColor = c.accent;
605 glowColor.setAlpha(60);
606 painter->fillRect(rect, glowColor);
607 }
608 else if (isHovered)
609 {
610 painter->fillRect(rect, c.hoverBackground);
611 }
612
613 QRect cardRect = rect.adjusted(4, 4, -4, -4);
614 painter->setPen(Qt::NoPen);
615 painter->setBrush(c.cardBackground);
616 painter->drawRoundedRect(cardRect, 8, 8);
617
618 QRect imageRect = cardRect.adjusted(4, 4, -4, -30);
619 if (!icon.isNull())
620 {
621 QPixmap pixmap = icon.pixmap(imageRect.size());
622 int x = imageRect.x() + (imageRect.width() - pixmap.width()) / 2;
623 int y = imageRect.y() + (imageRect.height() - pixmap.height()) / 2;
624 painter->drawPixmap(x, y, pixmap);
625 }
626 else
627 {
628 painter->fillRect(imageRect, c.inputBackground);
629 // Large gamepad icon centered
630 QFont iconFont;
631 iconFont.setFamily(IconFont::solidFamily());
632 iconFont.setPointSize(28);
633 painter->setFont(iconFont);
634 painter->setPen(c.textMuted);
635 QRect iconArea = imageRect.adjusted(0, 0, 0, -16);
636 painter->drawText(iconArea, Qt::AlignCenter, IconFont::text(FA::Gamepad));
637 // Small "No Cover Art" text below
638 QFont labelFont = painter->font();
639 labelFont.setFamily(QString());
640 labelFont.setPointSize(8);
641 painter->setFont(labelFont);
642 QRect labelArea(imageRect.left(), iconArea.bottom() - 4, imageRect.width(), 20);
643 painter->drawText(labelArea, Qt::AlignHCenter | Qt::AlignTop,
644 QStringLiteral("No Cover Art"));
645 }
646
647 // Status indicator (top-right)
648 if (isRunning)
649 {
650 QRect runningRect(imageRect.right() - 70, imageRect.top() + 4, 66, 20);
651 painter->setPen(Qt::NoPen);
652 painter->setBrush(c.success);
653 painter->drawRoundedRect(runningRect, 4, 4);
654 painter->setPen(c.textOnAccent);
655 QFont runningFont = painter->font();
656 runningFont.setPointSize(8);
657 runningFont.setBold(true);
658 painter->setFont(runningFont);
659 painter->drawText(runningRect, Qt::AlignCenter, QStringLiteral("▶ RUNNING"));
660 }
661 else if (isDownloading)
662 {
663 QString progressText =
664 QStringLiteral("⬇ %1%").arg(static_cast<int>(downloadProgress * 100));
665 QFont dlFont = painter->font();
666 dlFont.setPointSize(8);
667 dlFont.setBold(true);
668 painter->setFont(dlFont);
669 int textWidth = painter->fontMetrics().horizontalAdvance(progressText) + 12;
670
671 QRect downloadRect(imageRect.right() - textWidth - 4, imageRect.top() + 4, textWidth, 20);
672 painter->setPen(Qt::NoPen);
673 painter->setBrush(c.accent);
674 painter->drawRoundedRect(downloadRect, 4, 4);
675 painter->setPen(c.textOnAccent);
676 painter->drawText(downloadRect, Qt::AlignCenter, progressText);
677 }
678 else if (needsUpdate)
679 {
680 QFont updateFont = painter->font();
681 updateFont.setPointSize(8);
682 updateFont.setBold(true);
683 painter->setFont(updateFont);
684 int upWidth = painter->fontMetrics().horizontalAdvance("⬆ UPDATE") + 12;
685 QRect updateRect(imageRect.right() - upWidth - 4, imageRect.top() + 4, upWidth, 20);
686 painter->setPen(Qt::NoPen);
687 painter->setBrush(c.warning);
688 painter->drawRoundedRect(updateRect, 4, 4);
689 painter->setPen(contrastTextColor(c.warning));
690 painter->drawText(updateRect, Qt::AlignCenter, "⬆ UPDATE");
691 }
692 else if (isSteam && !isInstalled)
693 {
694 QFont notInstalledFont = painter->font();
695 notInstalledFont.setPointSize(8);
696 notInstalledFont.setBold(true);
697 painter->setFont(notInstalledFont);
698 int niWidth = painter->fontMetrics().horizontalAdvance("Not Installed") + 12;
699 QRect notInstalledRect(imageRect.right() - niWidth - 4, imageRect.top() + 4, niWidth, 20);
700 painter->setPen(Qt::NoPen);
701 painter->setBrush(c.warning);
702 painter->drawRoundedRect(notInstalledRect, 4, 4);
703 painter->setPen(contrastTextColor(c.warning));
704 painter->drawText(notInstalledRect, Qt::AlignCenter, "Not Installed");
705 }
706
707 // Cloud save icon
708 if (hasCloudSave)
709 {
710 int cloudX = isFavorite ? imageRect.left() + 30 : imageRect.left() + 4;
711 QRect cloudRect(cloudX, imageRect.top() + 4, 24, 24);
712 painter->setPen(Qt::NoPen);
713 QColor badgeBg = c.cardBackground;
714 badgeBg.setAlpha(200);
715 painter->setBrush(badgeBg);
716 painter->drawRoundedRect(cloudRect, 4, 4);
717 painter->setPen(c.textPrimary);
718 QFont cloudFont = painter->font();
719 cloudFont.setPointSize(12);
720 painter->setFont(cloudFont);
721 painter->drawText(cloudRect, Qt::AlignCenter, "☁");
722 }
723
724 // Favorite star
725 if (isFavorite)
726 {
727 QRect starRect(imageRect.left() + 4, imageRect.top() + 4, 24, 24);
728 painter->setPen(Qt::NoPen);
729 QColor starBg = c.cardBackground;
730 starBg.setAlpha(200);
731 painter->setBrush(starBg);
732 painter->drawRoundedRect(starRect, 4, 4);
733 painter->setPen(c.favorite);
734 QFont starFont = painter->font();
735 starFont.setPointSize(14);
736 painter->setFont(starFont);
737 painter->drawText(starRect, Qt::AlignCenter, "★");
738 }
739
740 // ProtonDB tier badge
741 if (!protondbTier.isEmpty() && protondbTier != "native")
742 {
743 QColor tierColor = getProtonDBTierColor(protondbTier);
744 QString tierText = protondbTier.left(1).toUpper() + protondbTier.mid(1);
745
746 QFont tierFont = painter->font();
747 tierFont.setPointSize(8);
748 tierFont.setBold(true);
749 painter->setFont(tierFont);
750 int textWidth = painter->fontMetrics().horizontalAdvance(tierText) + 10;
751
752 QRect tierRect(imageRect.left() + 4, imageRect.bottom() - 22, textWidth, 18);
753 painter->setPen(Qt::NoPen);
754 painter->setBrush(tierColor);
755 painter->drawRoundedRect(tierRect, 4, 4);
756 painter->setPen(contrastTextColor(tierColor));
757 painter->drawText(tierRect, Qt::AlignCenter, tierText);
758 }
759
760 // Source badge
761 if (source != "Steam" || protondbTier.isEmpty())
762 {
763 QFont sourceFont = painter->font();
764 sourceFont.setPointSize(7);
765 painter->setFont(sourceFont);
766 int textWidth = painter->fontMetrics().horizontalAdvance(source) + 8;
767
768 QRect sourceRect(imageRect.right() - textWidth - 4, imageRect.bottom() - 20, textWidth, 16);
769 painter->setPen(Qt::NoPen);
770 QColor srcBg = c.cardBackground;
771 srcBg.setAlpha(210);
772 painter->setBrush(srcBg);
773 painter->drawRoundedRect(sourceRect, 3, 3);
774 painter->setPen(c.textSecondary);
775 painter->drawText(sourceRect, Qt::AlignCenter, source);
776 }
777
778 // Game name below image
779 QRect nameRect = cardRect.adjusted(6, cardRect.height() - 28, -6, -4);
780 painter->setPen(isSelected ? c.textOnAccent : c.textPrimary);
781 QFont nameFont = painter->font();
782 nameFont.setPointSize(isSelected ? 10 : 9);
783 nameFont.setBold(isSelected);
784 painter->setFont(nameFont);
785 QString elidedName = painter->fontMetrics().elidedText(name, Qt::ElideRight, nameRect.width());
786 painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, elidedName);
787
788 // Kebab menu button on hover
789 if (isHovered)
790 {
791 int kebabSize = 24;
792 QRect kebabRect(cardRect.right() - kebabSize - 4, cardRect.top() + 4, kebabSize, kebabSize);
793 painter->setPen(Qt::NoPen);
794 QColor kebabBg = c.cardBackground;
795 kebabBg.setAlpha(210);
796 painter->setBrush(kebabBg);
797 painter->drawRoundedRect(kebabRect, 4, 4);
798 painter->setBrush(c.textOnAccent);
799 int dotRadius = 2;
800 int centerX = kebabRect.center().x();
801 int centerY = kebabRect.center().y();
802 painter->drawEllipse(QPoint(centerX - 6, centerY), dotRadius, dotRadius);
803 painter->drawEllipse(QPoint(centerX, centerY), dotRadius, dotRadius);
804 painter->drawEllipse(QPoint(centerX + 6, centerY), dotRadius, dotRadius);
805 }
806
807 // Selection border
808 if (isSelected)
809 {
810 painter->setPen(QPen(c.accent.lighter(120), 3));
811 painter->setBrush(Qt::NoBrush);
812 painter->drawRoundedRect(cardRect, 8, 8);
813 }
814}
815
816// ==============================================================================
817// Editor Event (Grid mode kebab menu)
818// ==============================================================================
819
820bool GameListDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
821 const QStyleOptionViewItem& option, const QModelIndex& index)
822{
823 if (viewMode_ != ViewMode::Grid)
824 return QStyledItemDelegate::editorEvent(event, model, option, index);
825
826 if (event->type() == QEvent::MouseButtonPress)
827 {
828 auto* mouseEvent = static_cast<QMouseEvent*>(event);
829 if (mouseEvent->button() == Qt::LeftButton)
830 {
831 QRect cardRect = option.rect.adjusted(4, 4, -4, -4);
832 int kebabSize = 24;
833 QRect kebabRect(cardRect.right() - kebabSize - 4, cardRect.top() + 4, kebabSize,
834 kebabSize);
835 if (kebabRect.contains(mouseEvent->pos()))
836 {
837 if (auto* listWidget =
838 qobject_cast<QListWidget*>(const_cast<QWidget*>(option.widget)))
839 {
840 listWidget->setCurrentIndex(index);
841 QPoint globalPos = listWidget->viewport()->mapToGlobal(mouseEvent->pos());
842 QPoint localPos = listWidget->mapFromGlobal(globalPos);
843 QMetaObject::invokeMethod(listWidget, "customContextMenuRequested",
844 Qt::QueuedConnection, Q_ARG(QPoint, localPos));
845 }
846 return true;
847 }
848 }
849 }
850 return QStyledItemDelegate::editorEvent(event, model, option, index);
851}
852
853// ==============================================================================
854// ProtonDB Tier Colors
855// ==============================================================================
856
857QColor GameListDelegate::getProtonDBTierColor(const QString& tier)
858{
859 if (tier == "platinum")
860 return QColor(180, 199, 220);
861 if (tier == "gold")
862 return QColor(207, 181, 59);
863 if (tier == "silver")
864 return QColor(166, 166, 166);
865 if (tier == "bronze")
866 return QColor(205, 127, 50);
867 if (tier == "borked")
868 return QColor(255, 0, 0);
869 return QColor(100, 100, 100);
870}
static QColor getProtonDBTierColor(const QString &tier)
void setZoomLevel(int level)
GameListDelegate(QObject *parent=nullptr)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
double zoomFactor() const
const ThemeColors & colors() const
static const QString & solidFamily()
Get the loaded Solid font family name.
Definition icon_font.h:136
static QString text(char32_t codepoint)
Convert a codepoint to a QString glyph.
Definition icon_font.h:113
static QColor contrastTextColor(const QColor &bg)
@ GameRunnerRole
@ GameProtonDBTierRole
@ GameNeedsUpdateRole
@ GameCloudSaveEnabledRole
@ GameIsSteamRole
@ GameSourceRole
@ GameInstallSizeRole
@ GameIsDownloadingRole
@ GameIsRunningRole
@ GameLastPlayedRole
@ GameDownloadProgressRole
@ GameIsInstalledRole
@ GameLaunchCountRole
@ GamePlayTimeRole
@ GameIsFavoriteRole
ViewMode
Definition mainwindow.h:75
constexpr char32_t Gamepad
Definition icon_font.h:40
bool needsUpdate(uint32_t flags)
Check if a game needs an update.
bool isDownloading(uint32_t flags)
Check if a game is currently downloading or updating.
QColor favorite
QColor inputBackground
QColor cardBackground
QColor textSecondary
QColor textPrimary
QColor textMuted
QColor hoverBackground
QColor textOnAccent