Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosdisplayinfo.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qohosdisplayinfo.h>
5#include <QtCore/private/qnapi_p.h>
6#include <qohosdeviceinfo_p.h>
7
9
10namespace
11{
12
13constexpr double mapPixelsToMillimeters(double pixels, double dpi)
14{
15 constexpr double millimetersPerInch = 25.4;
16 return (pixels / dpi) * millimetersPerInch;
17}
18
19}
20
21QOhosDisplayInfo QOhosDisplayInfo::makeFromOhosDisplayObject(QtOhos::JsState &jsState, QNapi::Object displayObject)
22{
23 constexpr auto forceEmptyTopLevelOffsetPixels = true;
24 constexpr auto minSupportedSdkVersionForSourceMode = 19;
25
26 auto sourceMode = QOhosDeviceInfo::sdkApiVersion() >= minSupportedSdkVersionForSourceMode
27 ? makeQOhosOptional(
28 jsState.mapOhosEnumFromJs<DisplaySourceMode>(
29 displayObject.get<QNapi::Number>("sourceMode")))
31
32 QOhosDisplayInfo result = {
33 .id = JsDisplayId(displayObject.get<QNapi::Number>("id")),
34 .name = QString::fromStdString(displayObject.get<QNapi::String>("name")),
35 .sizePixels = QSizeF(
36 displayObject.get<QNapi::Number>("width"),
37 displayObject.get<QNapi::Number>("height")),
38 .densityDPI = displayObject.get<QNapi::Number>("densityDPI"),
39 .dpi = QDpi(
40 displayObject.get<QNapi::Number>("xDPI"),
41 displayObject.get<QNapi::Number>("yDPI")),
42 .orientation = jsState.tryMapOhosEnumFromJs<JsDisplayOrientation>(
43 displayObject.get<QNapi::Number>("orientation")),
44 .sourceMode = sourceMode,
45 .topLeftOffsetPixels = !forceEmptyTopLevelOffsetPixels
46 ? sourceMode.andThen([&](DisplaySourceMode mode) {
47 return mode == DisplaySourceMode::MAIN || mode == DisplaySourceMode::EXTEND
48 ? makeQOhosOptional(
49 QPoint(
50 displayObject.get<QNapi::Number>("x"),
51 displayObject.get<QNapi::Number>("y")))
52 : makeEmptyQOhosOptional();
53 })
54 : makeEmptyQOhosOptional()
55 };
56
57 // The value obtained via displayObject.get<QNapi::Number>("densityDPI") has only float precision.
58 // However, since all high-DPI related calculations use double precision, this may introduce
59 // rounding errors. Therefore, we calculate the DPI directly using the formula provided
60 // in the documentation below.
61 // https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-display#display
62 result.densityPixels = result.densityScaled = result.densityDPI / 160.;
63
64 return result;
65}
66
68{
69 return QRect(topLeftOffsetPixels.valueOr(QPoint()), sizePixels.toSize());
70}
71
73{
74 return QSizeF(
75 mapPixelsToMillimeters(sizePixels.width(), dpi.first),
76 mapPixelsToMillimeters(sizePixels.height(), dpi.second));
77}
78
79QT_END_NAMESPACE
Combined button and popup list for selecting options.
constexpr double mapPixelsToMillimeters(double pixels, double dpi)
QOhosOptional< void > makeEmptyQOhosOptional()
QSizeF physicalSize() const
QRect displayGeometryPixels() const