game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
dpr_utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/dpr_utils.h
4// ==============================================================================
5// Device-pixel-ratio-aware sizing helpers.
6// ==============================================================================
7// Qt 6 already handles DPR transparently for most drawing, but there are two
8// places where we need to be explicit:
9//
10// 1. Raster assets (PNGs) that need high-DPI versions: call
11// QIcon::pixmap(size, devicePixelRatio, mode, state) so Qt produces an
12// image with the correct logical-vs-physical pixel density.
13//
14// 2. Hard-coded pixel dimensions in stylesheets or geometry code: scale by
15// the current device pixel ratio so layouts feel consistent across
16// 1.0x (traditional), 1.5x (Plasma fractional), 2.0x (HiDPI laptop),
17// etc.
18//
19// All helpers here are `inline` so they can be included from any translation
20// unit without linkage overhead, and are safe to call after QApplication
21// construction (they read QGuiApplication::primaryScreen()).
22// ==============================================================================
23
24#pragma once
25
26#include <QGuiApplication>
27#include <QScreen>
28#include <QSize>
29#include <QWidget>
30
31namespace dpr
32{
33
34/// The primary screen's device pixel ratio. Returns 1.0 if Qt hasn't yet
35/// established a primary screen (edge cases during very-early initialisation).
36inline qreal primaryRatio()
37{
38 if (QScreen* s = QGuiApplication::primaryScreen())
39 return s->devicePixelRatio();
40 return 1.0;
41}
42
43/// Device pixel ratio of the screen a given widget is currently on. Falls
44/// back to the primary screen's DPR if `w` has no screen assignment yet.
45inline qreal widgetRatio(const QWidget* w)
46{
47 if (!w)
48 return primaryRatio();
49 if (const QScreen* s = w->screen())
50 return s->devicePixelRatio();
51 return primaryRatio();
52}
53
54/// Scale a logical pixel count into a physical pixel count using the primary
55/// screen's DPR. Intended for cases where we draw into backing pixmaps that
56/// don't get DPR-aware treatment automatically (e.g. QPixmap caches keyed by
57/// physical size).
58inline int scaleToPhysical(int logicalPixels)
59{
60 return static_cast<int>(qRound(logicalPixels * primaryRatio()));
61}
62
63/// Scale a QSize by the primary screen's DPR. Same rationale as above.
64inline QSize scaleToPhysical(const QSize& logical)
65{
66 const qreal r = primaryRatio();
67 return QSize(static_cast<int>(qRound(logical.width() * r)),
68 static_cast<int>(qRound(logical.height() * r)));
69}
70
71/// Per-widget version of scaleToPhysical — preferred when the widget is on a
72/// non-primary monitor that might have a different DPR (mixed-DPI setups,
73/// common on Wayland).
74inline int scaleToPhysical(const QWidget* w, int logicalPixels)
75{
76 return static_cast<int>(qRound(logicalPixels * widgetRatio(w)));
77}
78
79} // namespace dpr
qreal widgetRatio(const QWidget *w)
Device pixel ratio of the screen a given widget is currently on.
Definition dpr_utils.h:45
qreal primaryRatio()
The primary screen's device pixel ratio.
Definition dpr_utils.h:36
int scaleToPhysical(int logicalPixels)
Scale a logical pixel count into a physical pixel count using the primary screen's DPR.
Definition dpr_utils.h:58