game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
frame_metrics_widget.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// frame_metrics_widget.cpp
4// ==============================================================================
5// Implementation of the frame metrics display widget - shows frametime graph
6// ==============================================================================
7
9#include "theme_manager.h"
10
11#include <QFontMetrics>
12#include <QPainterPath>
13#include <algorithm>
14
16 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
17{
18 // Compact size - just for the graph
19 setMinimumSize(150, 50);
20 setFixedHeight(60);
21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
22
23 applyTheme();
24
25 connect(themeManager_, &ThemeManager::themeChanged, this, &FrameMetricsWidget::applyTheme);
26}
27
29{
30 // Skip update if metrics haven't meaningfully changed
31 // This prevents unnecessary repaints during idle periods
32 bool hasNewMetrics = metrics.sample_count > 0;
33 if (hasMetrics_ == hasNewMetrics && metrics_.sample_count == metrics.sample_count &&
34 metrics_.frametimeHistory.size() == metrics.frametimeHistory.size())
35 {
36 // Check if history data actually changed (just compare a few samples)
37 bool historyChanged = false;
38 if (!metrics.frametimeHistory.empty() && !metrics_.frametimeHistory.empty())
39 {
40 // Compare last few samples to detect actual changes
41 size_t checkCount = std::min(size_t(5), metrics.frametimeHistory.size());
42 for (size_t i = 0; i < checkCount; ++i)
43 {
44 size_t idx = metrics.frametimeHistory.size() - 1 - i;
45 if (std::abs(metrics.frametimeHistory[idx] - metrics_.frametimeHistory[idx]) > 0.1)
46 {
47 historyChanged = true;
48 break;
49 }
50 }
51 }
52 if (!historyChanged)
53 {
54 return; // No meaningful change, skip repaint
55 }
56 }
57
58 metrics_ = metrics;
59 hasMetrics_ = hasNewMetrics;
60 update();
61}
62
64{
65 metrics_ = FrameMetrics();
66 hasMetrics_ = false;
67 update();
68}
69
71{
72 targetFps_ = fps;
73 update();
74}
75
77{
78 showGraph_ = show;
79 updateGeometry();
80 update();
81}
82
84{
85 compactMode_ = compact;
86 updateGeometry();
87 update();
88}
89
91{
92 return QSize(250, 60);
93}
94
96{
97 return QSize(150, 50);
98}
99
100QColor FrameMetricsWidget::getFpsColor(double fps) const
101{
102 // Color based on FPS relative to target
103 if (fps >= targetFps_ * 0.95)
104 return goodColor_;
105 else if (fps >= targetFps_ * 0.5)
106 return warnColor_;
107 else
108 return badColor_;
109}
110
111void FrameMetricsWidget::paintEvent(QPaintEvent* /*event*/)
112{
113 QPainter painter(this);
114 painter.setRenderHint(QPainter::Antialiasing);
115
116 // Get theme colors
117 const auto& tc = themeManager_->colors();
118
119 QRect rect = this->rect();
120
121 // Use theme background
122 painter.fillRect(rect, ThemeManager::darkenColor(tc.cardBackground, 10));
123
124 // Subtle border
125 painter.setPen(QPen(tc.border, 1));
126 painter.drawRect(rect.adjusted(0, 0, -1, -1));
127
128 if (!hasMetrics_ || metrics_.frametimeHistory.empty())
129 {
130 // No data - show label
131 painter.setPen(tc.textMuted);
132 QFont font = painter.font();
133 font.setPointSize(9);
134 painter.setFont(font);
135 painter.drawText(rect, Qt::AlignCenter, "Frametime Graph");
136 return;
137 }
138
139 int padding = 6;
140
141 // Check data freshness
142 bool isStale = !metrics_.isValid(2000);
143 bool isVeryStale = !metrics_.isValid(5000);
144
145 // Draw label with staleness indicator
146 QFont labelFont = painter.font();
147 labelFont.setPointSize(8);
148 painter.setFont(labelFont);
149
150 QString labelText = "Frametime";
151 QColor labelTextColor = tc.textMuted;
152 if (isVeryStale)
153 {
154 labelText = "Frametime (stale)";
155 labelTextColor = staleColor_;
156 }
157 else if (isStale)
158 {
159 labelText = "Frametime";
160 labelTextColor = warnColor_;
161 }
162 painter.setPen(labelTextColor);
163 painter.drawText(padding, padding + 10, labelText);
164
165 // Draw graph in remaining space
166 QRect graphRect(padding, padding + 16, rect.width() - 2 * padding,
167 rect.height() - padding - 20);
168 drawFrametimeGraph(painter, graphRect, isStale, isVeryStale);
169}
170
171void FrameMetricsWidget::drawFrametimeGraph(QPainter& painter, const QRect& rect, bool isStale,
172 bool isVeryStale)
173{
174 const auto& tc = themeManager_->colors();
175
176 // Graph background
177 painter.fillRect(rect, ThemeManager::darkenColor(tc.cardBackground, 20));
178
179 if (metrics_.frametimeHistory.empty())
180 return;
181
182 const auto& history = metrics_.frametimeHistory;
183 size_t count = history.size();
184
185 // Calculate scale based on data
186 double maxFt = *std::max_element(history.begin(), history.end());
187
188 // Use a reasonable scale
189 double targetFrametime = 1000.0 / targetFps_;
190 double scaleMax = std::max(maxFt * 1.2, targetFrametime * 2.0);
191 scaleMax = std::min(scaleMax, FRAMETIME_SCALE_MAX);
192
193 // Build path for the graph
194 QPainterPath path;
195 QPainterPath fillPath;
196
197 double xStep =
198 static_cast<double>(rect.width()) / static_cast<double>(std::max(size_t(1), count - 1));
199 double yScale = static_cast<double>(rect.height()) / scaleMax;
200
201 for (size_t i = 0; i < count; ++i)
202 {
203 double x = rect.left() + static_cast<double>(i) * xStep;
204 double y = rect.bottom() - (history[i] * yScale);
205 y = std::clamp(y, static_cast<double>(rect.top()), static_cast<double>(rect.bottom()));
206
207 if (i == 0)
208 {
209 path.moveTo(x, y);
210 fillPath.moveTo(x, rect.bottom());
211 fillPath.lineTo(x, y);
212 }
213 else
214 {
215 path.lineTo(x, y);
216 fillPath.lineTo(x, y);
217 }
218 }
219
220 // Complete fill path
221 fillPath.lineTo(rect.right(), rect.bottom());
222 fillPath.closeSubpath();
223
224 // Choose colors based on staleness
225 QColor lineColor = tc.accent;
226 QColor fillColor = tc.accent;
227
228 if (isVeryStale)
229 {
230 // Very stale - gray out completely
231 lineColor = staleColor_;
232 fillColor = staleColor_;
233 }
234 else if (isStale)
235 {
236 // Moderately stale - use warning/amber color
237 lineColor = warnColor_;
238 fillColor = warnColor_;
239 }
240
241 fillColor.setAlpha(40);
242 painter.fillPath(fillPath, fillColor);
243
244 // Draw line
245 painter.setPen(QPen(lineColor, 1.5));
246 painter.drawPath(path);
247
248 // Draw target frametime reference line
249 double targetY = rect.bottom() - (targetFrametime * yScale);
250 if (targetY >= rect.top() && targetY <= rect.bottom())
251 {
252 painter.setPen(QPen(tc.textMuted, 1, Qt::DotLine));
253 painter.drawLine(rect.left(), static_cast<int>(targetY), rect.right(),
254 static_cast<int>(targetY));
255 }
256}
257
258void FrameMetricsWidget::drawStatsText(QPainter& /*painter*/, const QRect& /*rect*/)
259{
260 // Stats are now shown in HTML, not in the widget
261}
262
264{
265 const auto& c = themeManager_->colors();
266
267 // Update semantic colors from theme
268 goodColor_ = c.success;
269 warnColor_ = c.warning;
270 badColor_ = c.error;
271 staleColor_ = c.textMuted;
272
273 // Update display colors
274 textColor_ = c.textPrimary;
275 labelColor_ = c.textSecondary;
276 bgColor_ = ThemeManager::darkenColor(c.cardBackground, 10);
277 graphBgColor_ = ThemeManager::darkenColor(c.cardBackground, 20);
278 graphLineColor_ = c.accent;
279 graphFillColor_ = c.accent;
280 graphFillColor_.setAlpha(80);
281
282 // Trigger repaint
283 update();
284}
QSize sizeHint() const override
Get the preferred size based on current mode.
void setMetrics(const FrameMetrics &metrics)
Update the displayed metrics.
FrameMetricsWidget(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void clear()
Clear the display (no game running).
void setTargetFps(double fps)
Set the target FPS for color coding (e.g., 60, 144).
void setShowGraph(bool show)
Set whether to show the frametime graph.
void setCompactMode(bool compact)
Set compact mode (less detailed, smaller).
void applyTheme()
Apply current theme colors.
void paintEvent(QPaintEvent *event) override
QSize minimumSizeHint() const override
static QColor darkenColor(const QColor &color, int amount)
Darken a color by decreasing its lightness.
void themeChanged()
Emitted when theme or accent color changes.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
Real-time frame timing and performance metrics.
std::vector< double > frametimeHistory
Recent frametimes for graph.
uint32_t sample_count