11#include <QFontMetrics>
12#include <QPainterPath>
16 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
19 setMinimumSize(150, 50);
21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
33 if (hasMetrics_ == hasNewMetrics && metrics_.sample_count == metrics.
sample_count &&
37 bool historyChanged =
false;
42 for (
size_t i = 0; i < checkCount; ++i)
45 if (std::abs(metrics.
frametimeHistory[idx] - metrics_.frametimeHistory[idx]) > 0.1)
47 historyChanged =
true;
59 hasMetrics_ = hasNewMetrics;
85 compactMode_ = compact;
92 return QSize(250, 60);
97 return QSize(150, 50);
100QColor FrameMetricsWidget::getFpsColor(
double fps)
const
103 if (fps >= targetFps_ * 0.95)
105 else if (fps >= targetFps_ * 0.5)
113 QPainter painter(
this);
114 painter.setRenderHint(QPainter::Antialiasing);
117 const auto& tc = themeManager_->colors();
119 QRect rect = this->rect();
125 painter.setPen(QPen(tc.border, 1));
126 painter.drawRect(rect.adjusted(0, 0, -1, -1));
128 if (!hasMetrics_ || metrics_.frametimeHistory.empty())
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");
142 bool isStale = !metrics_.isValid(2000);
143 bool isVeryStale = !metrics_.isValid(5000);
146 QFont labelFont = painter.font();
147 labelFont.setPointSize(8);
148 painter.setFont(labelFont);
150 QString labelText =
"Frametime";
151 QColor labelTextColor = tc.textMuted;
154 labelText =
"Frametime (stale)";
155 labelTextColor = staleColor_;
159 labelText =
"Frametime";
160 labelTextColor = warnColor_;
162 painter.setPen(labelTextColor);
163 painter.drawText(padding, padding + 10, labelText);
166 QRect graphRect(padding, padding + 16, rect.width() - 2 * padding,
167 rect.height() - padding - 20);
168 drawFrametimeGraph(painter, graphRect, isStale, isVeryStale);
171void FrameMetricsWidget::drawFrametimeGraph(QPainter& painter,
const QRect& rect,
bool isStale,
174 const auto& tc = themeManager_->
colors();
183 size_t count = history.size();
186 double maxFt = *std::max_element(history.begin(), history.end());
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);
195 QPainterPath fillPath;
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;
201 for (
size_t i = 0; i < count; ++i)
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()));
210 fillPath.moveTo(x, rect.bottom());
211 fillPath.lineTo(x, y);
216 fillPath.lineTo(x, y);
221 fillPath.lineTo(rect.right(), rect.bottom());
222 fillPath.closeSubpath();
225 QColor lineColor = tc.accent;
226 QColor fillColor = tc.accent;
231 lineColor = staleColor_;
232 fillColor = staleColor_;
237 lineColor = warnColor_;
238 fillColor = warnColor_;
241 fillColor.setAlpha(40);
242 painter.fillPath(fillPath, fillColor);
245 painter.setPen(QPen(lineColor, 1.5));
246 painter.drawPath(path);
249 double targetY = rect.bottom() - (targetFrametime * yScale);
250 if (targetY >= rect.top() && targetY <= rect.bottom())
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));
258void FrameMetricsWidget::drawStatsText(QPainter& ,
const QRect& )
265 const auto& c = themeManager_->colors();
268 goodColor_ = c.success;
269 warnColor_ = c.warning;
271 staleColor_ = c.textMuted;
274 textColor_ = c.textPrimary;
275 labelColor_ = c.textSecondary;
278 graphLineColor_ = c.accent;
279 graphFillColor_ = c.accent;
280 graphFillColor_.setAlpha(80);
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.