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