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