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
7QT_BEGIN_NAMESPACE
8
9namespace
10{
11
12constexpr double mapPixelsToMillimeters(double pixels, double dpi)
13{
14 constexpr double millimetersPerInch = 25.4;
15 return (pixels / dpi) * millimetersPerInch;
16}
17
18}
19
20QOhosDisplayInfo QOhosDisplayInfo::makeFromOhosDisplayObject(QtOhos::JsState &jsState, QNapi::Object displayObject)
21{
22 constexpr auto forceEmptyTopLevelOffsetPixels = false;
23
24 auto sourceMode = std::optional(
25 jsState.mapOhosEnumFromJs<DisplaySourceMode>(
26 displayObject.get<QNapi::Number>("sourceMode")));
27
28 QOhosDisplayInfo result = {
29 .id = JsDisplayId(displayObject.get<QNapi::Number>("id")),
30 .name = QString::fromStdString(displayObject.get<QNapi::String>("name")),
31 .sizePixels = QSizeF(
32 displayObject.get<QNapi::Number>("width"),
33 displayObject.get<QNapi::Number>("height")),
34 .densityDPI = displayObject.get<QNapi::Number>("densityDPI"),
35 .densityPixels = {},
36 .densityScaled = {},
37 .refreshRate = displayObject.get<QNapi::Number>("refreshRate"),
38 .dpi = QDpi(
39 displayObject.get<QNapi::Number>("xDPI"),
40 displayObject.get<QNapi::Number>("yDPI")),
41 .orientation = jsState.tryMapOhosEnumFromJs<JsDisplayOrientation>(
42 displayObject.get<QNapi::Number>("orientation")),
43 .sourceMode = sourceMode,
44 .topLeftOffsetPixels = !forceEmptyTopLevelOffsetPixels
45 ? qAndThen(sourceMode, [&](DisplaySourceMode mode) {
46 return mode == DisplaySourceMode::MAIN || mode == DisplaySourceMode::EXTEND
47 ? std::optional(
48 QPoint(
49 displayObject.get<QNapi::Number>("x"),
50 displayObject.get<QNapi::Number>("y")))
51 : std::nullopt;
52 })
53 : std::nullopt
54 };
55
56 // The value obtained via displayObject.get<QNapi::Number>("densityDPI") has only float precision.
57 // However, since all high-DPI related calculations use double precision, this may introduce
58 // rounding errors. Therefore, we calculate the DPI directly using the formula provided
59 // in the documentation below.
60 // https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-display#display
61 result.densityPixels = result.densityScaled = result.densityDPI / 160.;
62
63 return result;
64}
65
66std::optional<QNapi::Object> QOhosDisplayInfo::tryGetDisplayById(QtOhos::JsState &jsState, QOhosDisplayInfo::JsDisplayId displayId)
67{
68 std::optional<QNapi::Object> result;
69 try {
70 result = jsState.eval<QNapi::Object>(
71 "@ohos.display.getDisplayByIdSync(*)", { displayId.value() });
72 } catch (const Napi::Error &error) {
73 qOhosPrintfError(
74 "%s: Failed to retrieve display with id: %f", Q_FUNC_INFO,
75 displayId.value());
76 }
77 return result;
78}
79
81{
82 return QRect(topLeftOffsetPixels.value_or(QPoint()), sizePixels.toSize());
83}
84
86{
87 return QSizeF(
88 mapPixelsToMillimeters(sizePixels.width(), dpi.first),
89 mapPixelsToMillimeters(sizePixels.height(), dpi.second));
90}
91
93{
94 constexpr int virtualDisplayBaseId = 1000;
95 static const QOhosDisplayInfo::DisplaySourceMode sourceModesToIgnore[] = {
96 QOhosDisplayInfo::DisplaySourceMode::NONE,
97 QOhosDisplayInfo::DisplaySourceMode::MIRROR,
98 QOhosDisplayInfo::DisplaySourceMode::ALONE,
99 };
100
101 const auto *sourceModeToIgnoreIter = std::find(
102 std::begin(sourceModesToIgnore), std::end(sourceModesToIgnore),
103 sourceMode);
104 bool ignoreBySoureMode = sourceModeToIgnoreIter != std::end(sourceModesToIgnore);
105
106 return sourceMode.has_value()
107 ? ignoreBySoureMode
108 : id.value() >= virtualDisplayBaseId;
109}
110
112{
113 return sourceMode == QOhosDisplayInfo::DisplaySourceMode::MAIN
114 || sourceMode == QOhosDisplayInfo::DisplaySourceMode::EXTEND;
115}
116
117QT_END_NAMESPACE
constexpr double mapPixelsToMillimeters(double pixels, double dpi)
bool isDisplayMainOrExtended() const
QSizeF physicalSize() const
QRect displayGeometryPixels() const
bool shouldIgnoreDisplay() const