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
qohosiconengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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
5
6
7#include <QtCore/private/qcore_ohos_p.h>
8#include <QtGui/qfont.h>
9#include <QtGui/private/qfonticonengine_p.h>
10
11#include <QtCore/qhash.h>
12#include <QtCore/private/qnapi_p.h>
13#include <QtCore/private/qohoslogger_p.h>
14
15#include <optional>
16
17QT_BEGIN_NAMESPACE
18
19using namespace Qt::StringLiterals;
20
21namespace {
22
23std::optional<char32_t> tryGetOhosSymbolCodepoint(QOhosJsState &jsState, const char *ohosSymbolName)
24{
25 try {
26 return jsState.eval<QNapi::Number>(
27 "@ohos.resourceManager.getSysResourceManager().getSymbolByName(*)",
28 {ohosSymbolName}).Uint32Value();
29 } catch (const Napi::Error &error) {
30 qOhosPrintfWarning(
31 "%s: getSymbolByName('%s') failed: %s",
32 Q_FUNC_INFO, ohosSymbolName, error.what());
33 return std::nullopt;
34 }
35}
36
37const QHash<QString, char32_t> &getThemeIconCodepoints()
38{
39 // Note: Qt uses string based icon names, see QAbstractFileIconProviderPrivate::getIconThemeIcon
40 static constexpr struct {
41 QLatin1StringView qtThemeIconName;
42 const char *ohosSymbolName;
43 } themeIconToOhosSymbolMapping[] = {
44 {"computer"_L1, "computer"},
45 {"user-desktop"_L1, "desktop"},
46 {"user-trash"_L1, "trash"},
47 {"network-workgroup"_L1, "dot_radiowaves_left_and_right"},
48 {"drive-harddisk"_L1, "externaldrive"},
49 {"folder"_L1, "folder"},
50 {"text-x-generic"_L1, "doc"},
51 {"go-down"_L1, "chevron_down"},
52 {"go-up"_L1, "chevron_up"},
53 {"go-previous"_L1, "chevron_left"},
54 {"go-next"_L1, "chevron_right"},
55 };
56
57 static const QHash<QString, char32_t> codepoints = QOhosJsThreadGateway::eval(
58 [](QOhosJsState &jsState) {
59 QHash<QString, char32_t> codepoints;
60 for (const auto &entry : themeIconToOhosSymbolMapping) {
61 const auto optCodepoint = tryGetOhosSymbolCodepoint(jsState, entry.ohosSymbolName);
62 if (optCodepoint)
63 codepoints.insert(entry.qtThemeIconName, *optCodepoint);
64 }
65 return codepoints;
66 },
67 Q_FUNC_INFO);
68
69 return codepoints;
70}
71
72QFont ohosSymbolFont()
73{
74 QFont font("HM Symbol"_L1);
75 font.setStyleStrategy(QFont::NoFontMerging);
76 return font;
77}
78
79std::optional<QString> tryGetGlyphForThemeIcon(const QString &iconName)
80{
81 const QHash<QString, char32_t> &codepoints = getThemeIconCodepoints();
82 const auto codepointIt = codepoints.constFind(iconName);
83 if (codepointIt == codepoints.cend())
84 return std::nullopt;
85
86 const char32_t ucs4 = *codepointIt;
87
88 return QString::fromUcs4(&ucs4, 1);
89}
90
91class QOhosIconEngine : public QFontIconEngine
92{
93public:
94 explicit QOhosIconEngine(const QString &iconName, const QString &glyphString);
95
96 QIconEngine *clone() const override;
97 QString key() const override;
98
99protected:
100 QString string() const override;
101 glyph_t glyph() const override;
102
103private:
104 QString m_glyphString;
105};
106
107QOhosIconEngine::QOhosIconEngine(const QString &iconName, const QString &glyphString)
108 : QFontIconEngine(iconName, ohosSymbolFont())
109 , m_glyphString(glyphString)
110{
111}
112
113QIconEngine *QOhosIconEngine::clone() const
114{
115 auto *constThis = const_cast<QOhosIconEngine *>(this);
116 return new QOhosIconEngine(constThis->iconName(), m_glyphString);
117}
118
119QString QOhosIconEngine::key() const
120{
121 return "QOhosIconEngine"_L1;
122}
123
124QString QOhosIconEngine::string() const
125{
126 return m_glyphString;
127}
128
129glyph_t QOhosIconEngine::glyph() const
130{
131 return 0;
132}
133
134}
135
136QIconEngine *tryCreateQOhosIconEngine(const QString &iconName)
137{
138 auto optGlyphString = tryGetGlyphForThemeIcon(iconName);
139 if (!optGlyphString)
140 return nullptr;
141
142 return new QOhosIconEngine(iconName, *optGlyphString);
143}
144
145QT_END_NAMESPACE
QIconEngine * tryCreateQOhosIconEngine(const QString &iconName)