game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
loading_widgets.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// loading_widgets.cpp
4// ==============================================================================
5
7#include "theme_manager.h"
8
9#include <QHBoxLayout>
10#include <QHideEvent>
11#include <QLinearGradient>
12#include <QMouseEvent>
13#include <QPainter>
14#include <QPainterPath>
15#include <QPushButton>
16#include <QShowEvent>
17#include <QVBoxLayout>
18
19// ==============================================================================
20// SkeletonWidget Implementation
21// ==============================================================================
22
23SkeletonWidget::SkeletonWidget(int width, int height, QWidget* parent, ThemeManager* themeManager)
24 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
25{
26 setFixedSize(width, height);
27 initAnimation();
28
29 const auto& c = themeManager_->colors();
30 baseColor_ = c.inputBackground;
31 highlightColor_ = c.hoverBackground;
32
33 // Don't start animation in constructor - only start when widget becomes visible
34 // This prevents infinite animations from consuming CPU when hidden
35}
36
37SkeletonWidget::SkeletonWidget(const QSize& size, QWidget* parent, ThemeManager* themeManager)
38 : SkeletonWidget(size.width(), size.height(), parent, themeManager)
39{
40}
41
42void SkeletonWidget::initAnimation()
43{
44 animation_ = new QPropertyAnimation(this, "shimmerOffset", this);
45 animation_->setDuration(1500);
46 animation_->setStartValue(0.0);
47 animation_->setEndValue(1.0);
48 animation_->setEasingCurve(QEasingCurve::InOutSine);
49 animation_->setLoopCount(-1); // Infinite loop
50}
51
53{
54 if (animation_ && animation_->state() != QAbstractAnimation::Running)
55 {
56 animation_->start();
57 }
58}
59
61{
62 if (animation_)
63 {
64 animation_->stop();
65 }
66}
67
68void SkeletonWidget::showEvent(QShowEvent* event)
69{
70 QWidget::showEvent(event);
72}
73
74void SkeletonWidget::hideEvent(QHideEvent* event)
75{
76 QWidget::hideEvent(event);
78}
79
80void SkeletonWidget::paintEvent(QPaintEvent* /*event*/)
81{
82 QPainter painter(this);
83 painter.setRenderHint(QPainter::Antialiasing);
84
85 // Create rounded rectangle path
86 QPainterPath path;
87 path.addRoundedRect(rect(), cornerRadius_, cornerRadius_);
88 painter.setClipPath(path);
89
90 // Draw base color
91 painter.fillRect(rect(), baseColor_);
92
93 // Draw shimmer gradient
94 QLinearGradient gradient(0, 0, width(), 0);
95
96 // Calculate shimmer position
97 qreal shimmerWidth = 0.3;
98 qreal shimmerStart = shimmerOffset_ - shimmerWidth;
99 qreal shimmerEnd = shimmerOffset_ + shimmerWidth;
100
101 gradient.setColorAt(qMax(0.0, shimmerStart), baseColor_);
102 gradient.setColorAt(qMax(0.0, shimmerOffset_ - shimmerWidth / 2), highlightColor_);
103 gradient.setColorAt(qMin(1.0, shimmerOffset_ + shimmerWidth / 2), highlightColor_);
104 gradient.setColorAt(qMin(1.0, shimmerEnd), baseColor_);
105
106 painter.fillRect(rect(), gradient);
107}
108
109// ==============================================================================
110// GameCardSkeleton Implementation
111// ==============================================================================
112
113GameCardSkeleton::GameCardSkeleton(Style style, QWidget* parent, ThemeManager* themeManager)
114 : QWidget(parent), style_(style),
115 themeManager_(themeManager ? themeManager : &ThemeManager::instance())
116{
117 if (style == Style::Card)
118 {
119 setupCardStyle();
120 }
121 else
122 {
123 setupListItemStyle();
124 }
125}
126
127void GameCardSkeleton::setupCardStyle()
128{
129 auto* layout = new QVBoxLayout(this);
130 layout->setContentsMargins(0, 0, 0, 0);
131 layout->setSpacing(8);
132
133 // Image placeholder (2:3 aspect ratio for game covers)
134 auto* imageSkeleton = new SkeletonWidget(150, 225, this, themeManager_);
135 imageSkeleton->setCornerRadius(8);
136 layout->addWidget(imageSkeleton);
137 skeletons_.append(imageSkeleton);
138
139 // Title placeholder
140 auto* titleSkeleton = new SkeletonWidget(130, 16, this, themeManager_);
141 layout->addWidget(titleSkeleton);
142 skeletons_.append(titleSkeleton);
143
144 // Subtitle placeholder (shorter)
145 auto* subtitleSkeleton = new SkeletonWidget(80, 12, this, themeManager_);
146 layout->addWidget(subtitleSkeleton);
147 skeletons_.append(subtitleSkeleton);
148
149 setFixedSize(150, 280);
150}
151
152void GameCardSkeleton::setupListItemStyle()
153{
154 auto* layout = new QHBoxLayout(this);
155 layout->setContentsMargins(8, 8, 8, 8);
156 layout->setSpacing(12);
157
158 // Small image placeholder
159 auto* imageSkeleton = new SkeletonWidget(48, 48, this, themeManager_);
160 imageSkeleton->setCornerRadius(4);
161 layout->addWidget(imageSkeleton);
162 skeletons_.append(imageSkeleton);
163
164 // Text container
165 auto* textLayout = new QVBoxLayout();
166 textLayout->setSpacing(4);
167
168 auto* titleSkeleton = new SkeletonWidget(200, 16, this, themeManager_);
169 textLayout->addWidget(titleSkeleton);
170 skeletons_.append(titleSkeleton);
171
172 auto* subtitleSkeleton = new SkeletonWidget(120, 12, this, themeManager_);
173 textLayout->addWidget(subtitleSkeleton);
174 skeletons_.append(subtitleSkeleton);
175
176 layout->addLayout(textLayout, 1);
177
178 setFixedHeight(64);
179}
180
182{
183 for (auto* skeleton : skeletons_)
184 {
185 skeleton->startAnimation();
186 }
187}
188
190{
191 for (auto* skeleton : skeletons_)
192 {
193 skeleton->stopAnimation();
194 }
195}
196
197// ==============================================================================
198// LoadingOverlay Implementation
199// ==============================================================================
200
201LoadingOverlay::LoadingOverlay(QWidget* parent, ThemeManager* themeManager)
202 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
203{
204 setAttribute(Qt::WA_TransparentForMouseEvents, false);
205 setAutoFillBackground(false);
206
207 // Create cancel button (hidden by default)
208 cancelButton_ = new QPushButton("Cancel", this);
209 cancelButton_->setVisible(false);
210 cancelButton_->setCursor(Qt::PointingHandCursor);
211 connect(cancelButton_, &QPushButton::clicked, this, &LoadingOverlay::cancelled);
212
213 // Setup spinner animation
214 spinnerAnimation_ = new QPropertyAnimation(this, "spinnerAngle", this);
215 spinnerAnimation_->setDuration(1000);
216 spinnerAnimation_->setStartValue(0);
217 spinnerAnimation_->setEndValue(360);
218 spinnerAnimation_->setLoopCount(-1);
219
220 // Style cancel button
221 const auto& c = themeManager_->colors();
222 cancelButton_->setStyleSheet(QString("QPushButton {"
223 " background-color: %1;"
224 " color: %2;"
225 " border: 1px solid %3;"
226 " border-radius: 4px;"
227 " padding: 8px 16px;"
228 " font-weight: bold;"
229 "}"
230 "QPushButton:hover {"
231 " background-color: %4;"
232 "}")
233 .arg(c.cardBackground.name(), c.textPrimary.name(),
234 c.border.name(), c.hoverBackground.name()));
235
236 hide();
237}
238
239void LoadingOverlay::setMessage(const QString& message)
240{
241 message_ = message;
242 update();
243}
244
245void LoadingOverlay::setCancellable(bool cancellable)
246{
247 cancellable_ = cancellable;
248 cancelButton_->setVisible(cancellable && isVisible());
249 updateGeometry();
250}
251
252void LoadingOverlay::setProgress(int current, int total)
253{
254 progressCurrent_ = current;
255 progressTotal_ = total;
256 update();
257}
258
260{
261 progressCurrent_ = -1;
262 progressTotal_ = -1;
263 update();
264}
265
266void LoadingOverlay::startSpinner()
267{
268 if (spinnerAnimation_->state() != QAbstractAnimation::Running)
269 {
270 spinnerAnimation_->start();
271 }
272}
273
274void LoadingOverlay::stopSpinner()
275{
276 spinnerAnimation_->stop();
277}
278
279void LoadingOverlay::updateGeometry()
280{
281 if (parentWidget())
282 {
283 resize(parentWidget()->size());
284 }
285
286 // Position cancel button
287 if (cancelButton_->isVisible())
288 {
289 int x = (width() - cancelButton_->width()) / 2;
290 int y = height() / 2 + 60;
291 cancelButton_->move(x, y);
292 }
293}
294
295void LoadingOverlay::showEvent(QShowEvent* event)
296{
297 QWidget::showEvent(event);
298 startSpinner();
299 cancelButton_->setVisible(cancellable_);
300 updateGeometry();
301 raise();
302}
303
304void LoadingOverlay::hideEvent(QHideEvent* event)
305{
306 QWidget::hideEvent(event);
307 stopSpinner();
308}
309
310void LoadingOverlay::resizeEvent(QResizeEvent* event)
311{
312 QWidget::resizeEvent(event);
313 updateGeometry();
314}
315
316void LoadingOverlay::paintEvent(QPaintEvent* /*event*/)
317{
318 QPainter painter(this);
319 painter.setRenderHint(QPainter::Antialiasing);
320
321 const auto& c = themeManager_->colors();
322
323 // Semi-transparent background
324 QColor overlayColor = c.windowBackground;
325 overlayColor.setAlpha(200);
326 painter.fillRect(rect(), overlayColor);
327
328 // Spinner
329 int spinnerSize = 48;
330 int centerX = width() / 2;
331 int centerY = height() / 2;
332
333 painter.save();
334 painter.translate(centerX, centerY);
335 painter.rotate(spinnerAngle_);
336
337 QPen spinnerPen(c.accent, 4, Qt::SolidLine, Qt::RoundCap);
338 painter.setPen(spinnerPen);
339
340 // Draw arc (270 degrees)
341 QRectF spinnerRect(-spinnerSize / 2.0, -spinnerSize / 2.0, spinnerSize, spinnerSize);
342 painter.drawArc(spinnerRect, 0, 270 * 16);
343
344 painter.restore();
345
346 // Message text
347 if (!message_.isEmpty())
348 {
349 QFont font = painter.font();
350 font.setPointSize(12);
351 painter.setFont(font);
352 painter.setPen(c.textPrimary);
353
354 QRect textRect(0, centerY + spinnerSize / 2 + 16, width(), 30);
355 painter.drawText(textRect, Qt::AlignHCenter | Qt::AlignTop, message_);
356 }
357
358 // Progress text
359 if (progressCurrent_ >= 0 && progressTotal_ > 0)
360 {
361 QString progressText = QString("%1 / %2").arg(progressCurrent_).arg(progressTotal_);
362 QFont font = painter.font();
363 font.setPointSize(10);
364 painter.setFont(font);
365 painter.setPen(c.textSecondary);
366
367 QRect progressRect(0, centerY + spinnerSize / 2 + 44, width(), 20);
368 painter.drawText(progressRect, Qt::AlignHCenter | Qt::AlignTop, progressText);
369 }
370}
371
372// ==============================================================================
373// EmptyStateWidget Implementation
374// ==============================================================================
375
377 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
378{
379 auto* layout = new QVBoxLayout(this);
380 layout->setAlignment(Qt::AlignCenter);
381 layout->setSpacing(16);
382
383 // Icon
384 iconLabel_ = new QLabel(this);
385 iconLabel_->setAlignment(Qt::AlignCenter);
386 QFont iconFont = iconLabel_->font();
387 iconFont.setPointSize(48);
388 iconLabel_->setFont(iconFont);
389 layout->addWidget(iconLabel_);
390
391 // Title
392 titleLabel_ = new QLabel(this);
393 titleLabel_->setAlignment(Qt::AlignCenter);
394 titleLabel_->setWordWrap(true);
395 QFont titleFont = titleLabel_->font();
396 titleFont.setPointSize(16);
397 titleFont.setBold(true);
398 titleLabel_->setFont(titleFont);
399 layout->addWidget(titleLabel_);
400
401 // Description
402 descriptionLabel_ = new QLabel(this);
403 descriptionLabel_->setAlignment(Qt::AlignCenter);
404 descriptionLabel_->setWordWrap(true);
405 QFont descFont = descriptionLabel_->font();
406 descFont.setPointSize(11);
407 descriptionLabel_->setFont(descFont);
408 layout->addWidget(descriptionLabel_);
409
410 // Actions container
411 actionsContainer_ = new QWidget(this);
412 actionsLayout_ = new QHBoxLayout(actionsContainer_);
413 actionsLayout_->setAlignment(Qt::AlignCenter);
414 actionsLayout_->setSpacing(12);
415 layout->addWidget(actionsContainer_);
416
417 layout->addStretch();
418
419 applyTheme();
420}
421
422void EmptyStateWidget::setIcon(const QString& iconText)
423{
424 iconLabel_->setText(iconText);
425}
426
427void EmptyStateWidget::setTitle(const QString& title)
428{
429 titleLabel_->setText(title);
430}
431
432void EmptyStateWidget::setDescription(const QString& description)
433{
434 descriptionLabel_->setText(description);
435}
436
437void EmptyStateWidget::addAction(const QString& label, const std::function<void()>& callback)
438{
439 auto* button = new QPushButton(label, actionsContainer_);
440 button->setCursor(Qt::PointingHandCursor);
441
442 const auto& c = themeManager_->colors();
443 button->setStyleSheet(QString("QPushButton {"
444 " background-color: %1;"
445 " color: %2;"
446 " border: none;"
447 " border-radius: 6px;"
448 " padding: 10px 20px;"
449 " font-weight: bold;"
450 "}"
451 "QPushButton:hover {"
452 " background-color: %3;"
453 "}")
454 .arg(c.accent.name(), c.textOnAccent.name(), c.accentHover.name()));
455
456 connect(button, &QPushButton::clicked, this,
457 [callback]()
458 {
459 if (callback)
460 callback();
461 });
462
463 actionsLayout_->addWidget(button);
464}
465
467{
468 while (QLayoutItem* item = actionsLayout_->takeAt(0))
469 {
470 if (QWidget* widget = item->widget())
471 {
472 delete widget;
473 }
474 delete item;
475 }
476}
477
479{
480 const auto& c = themeManager_->colors();
481
482 // Use QPalette for foreground colour: Qt's style system applies it
483 // directly without the parsing/CSS cascade overhead of setStyleSheet,
484 // and it composes cleanly with parent widgets' stylesheets.
485 auto setLabelColor = [](QLabel* label, const QColor& color)
486 {
487 QPalette p = label->palette();
488 p.setColor(QPalette::WindowText, color);
489 label->setPalette(p);
490 };
491 setLabelColor(titleLabel_, c.textPrimary);
492 setLabelColor(descriptionLabel_, c.textSecondary);
493}
494
495void EmptyStateWidget::showEvent(QShowEvent* event)
496{
497 QWidget::showEvent(event);
498 applyTheme();
499}
500
501// ==============================================================================
502// PulsingDot Implementation
503// ==============================================================================
504
505PulsingDot::PulsingDot(int size, QWidget* parent, ThemeManager* themeManager)
506 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
507{
508 setFixedSize(size, size);
509
510 const auto& c = themeManager_->colors();
511 color_ = c.accent;
512
513 // Create pulsing animation
514 animation_ = new QSequentialAnimationGroup(this);
515
516 auto* fadeOut = new QPropertyAnimation(this, "opacity", this);
517 fadeOut->setDuration(600);
518 fadeOut->setStartValue(1.0);
519 fadeOut->setEndValue(0.3);
520 fadeOut->setEasingCurve(QEasingCurve::InOutSine);
521
522 auto* fadeIn = new QPropertyAnimation(this, "opacity", this);
523 fadeIn->setDuration(600);
524 fadeIn->setStartValue(0.3);
525 fadeIn->setEndValue(1.0);
526 fadeIn->setEasingCurve(QEasingCurve::InOutSine);
527
528 animation_->addAnimation(fadeOut);
529 animation_->addAnimation(fadeIn);
530 animation_->setLoopCount(-1);
531}
532
534{
535 if (animation_->state() != QAbstractAnimation::Running)
536 {
537 animation_->start();
538 }
539}
540
542{
543 animation_->stop();
544}
545
546void PulsingDot::showEvent(QShowEvent* event)
547{
548 QWidget::showEvent(event);
550}
551
552void PulsingDot::hideEvent(QHideEvent* event)
553{
554 QWidget::hideEvent(event);
556}
557
558void PulsingDot::paintEvent(QPaintEvent* /*event*/)
559{
560 QPainter painter(this);
561 painter.setRenderHint(QPainter::Antialiasing);
562
563 QColor dotColor = color_;
564 dotColor.setAlphaF(static_cast<float>(opacity_));
565
566 painter.setBrush(dotColor);
567 painter.setPen(Qt::NoPen);
568 painter.drawEllipse(rect());
569}
EmptyStateWidget(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void addAction(const QString &label, const std::function< void()> &callback)
void setIcon(const QString &iconText)
void setDescription(const QString &description)
void setTitle(const QString &title)
void showEvent(QShowEvent *event) override
GameCardSkeleton(Style style=Style::Card, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void hideEvent(QHideEvent *event) override
void setProgress(int current, int total)
void setCancellable(bool cancellable)
void resizeEvent(QResizeEvent *event) override
void paintEvent(QPaintEvent *event) override
void setMessage(const QString &message)
LoadingOverlay(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void showEvent(QShowEvent *event) override
PulsingDot(int size=8, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void hideEvent(QHideEvent *event) override
void showEvent(QShowEvent *event) override
void paintEvent(QPaintEvent *event) override
A pulsing skeleton placeholder widget.
void hideEvent(QHideEvent *event) override
void showEvent(QShowEvent *event) override
void paintEvent(QPaintEvent *event) override
SkeletonWidget(int width, int height, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)