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#include <qohosplugincore.h>
7
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
18
19using namespace Qt::StringLiterals;
20
21namespace {
22
23std::optional<char32_t> tryGetOhosSymbolCodepoint(QtOhos::JsState &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 };
52
53 static const QHash<QString, char32_t> codepoints = QtOhos::evalInJsThread(
54 [](QtOhos::JsState &jsState) {
55 QHash<QString, char32_t> codepoints;
56 for (const auto &entry : themeIconToOhosSymbolMapping) {
57 const auto optCodepoint = tryGetOhosSymbolCodepoint(jsState, entry.ohosSymbolName);
58 if (optCodepoint)
59 codepoints.insert(entry.qtThemeIconName, *optCodepoint);
60 }
61 return codepoints;
62 },
63 Q_FUNC_INFO);
64
65 return codepoints;
66}
67
68QFont ohosSymbolFont()
69{
70 QFont font("HM Symbol"_L1);
71 font.setStyleStrategy(QFont::NoFontMerging);
72 return font;
73}
74
75std::optional<QString> tryGetGlyphForThemeIcon(const QString &iconName)
76{
77 const QHash<QString, char32_t> &codepoints = getThemeIconCodepoints();
78 const auto codepointIt = codepoints.constFind(iconName);
79 if (codepointIt == codepoints.cend())
80 return std::nullopt;
81
82 const char32_t ucs4 = *codepointIt;
83
84 return QString::fromUcs4(&ucs4, 1);
85}
86
87class QOhosIconEngine : public QFontIconEngine
88{
89public:
90 explicit QOhosIconEngine(const QString &iconName, const QString &glyphString);
91
92 QIconEngine *clone() const override;
93 QString key() const override;
94
95protected:
96 QString string() const override;
97 glyph_t glyph() const override;
98
99private:
100 QString m_glyphString;
101};
102
103QOhosIconEngine::QOhosIconEngine(const QString &iconName, const QString &glyphString)
104 : QFontIconEngine(iconName, ohosSymbolFont())
105 , m_glyphString(glyphString)
106{
107}
108
109QIconEngine *QOhosIconEngine::clone() const
110{
111 auto *constThis = const_cast<QOhosIconEngine *>(this);
112 return new QOhosIconEngine(constThis->iconName(), m_glyphString);
113}
114
115QString QOhosIconEngine::key() const
116{
117 return "QOhosIconEngine"_L1;
118}
119
120QString QOhosIconEngine::string() const
121{
122 return m_glyphString;
123}
124
125glyph_t QOhosIconEngine::glyph() const
126{
127 return 0;
128}
129
130}
131
132QIconEngine *tryCreateQOhosIconEngine(const QString &iconName)
133{
134 auto optGlyphString = tryGetGlyphForThemeIcon(iconName);
135 if (!optGlyphString)
136 return nullptr;
137
138 return new QOhosIconEngine(iconName, *optGlyphString);
139}
140
141QT_END_NAMESPACE
Combined button and popup list for selecting options.
QIconEngine * tryCreateQOhosIconEngine(const QString &iconName)