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
document.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "document.h"
5
7
8#include <QJsonArray>
9
11
12using namespace Qt::Literals::StringLiterals;
13
14namespace IR {
15
16/*!
17 \struct IR::Document
18 \brief Intermediate representation for a documentation topic.
19
20 Document contains all information needed to render a single documentation
21 page using templates. All links are pre-resolved, sections are pre-organized,
22 and file paths are pre-computed. The template engine receives only this IR
23 and performs no lookups or resolution itself.
24
25 The struct includes classification metadata (nodeType, genus, status, access)
26 that allows templates to conditionally render content based on the type and
27 visibility of the documented entity.
28*/
29
30/*!
31 Converts the QmlTypeInfo to a QJsonObject for template rendering.
32
33 Always emits \c isSingleton and \c isValueType. The \c importStatement,
34 \c inherits, \c inheritedBy, and \c nativeType fields are omitted when
35 they don't have values. Templates should use \c{existsIn()} to guard
36 access to optional fields.
37*/
39{
40 QJsonObject json;
41
42 if (!importStatement.isEmpty())
43 json["importStatement"_L1] = importStatement;
44 json["isSingleton"_L1] = isSingleton;
45 json["isValueType"_L1] = isValueType;
46
47 if (inherits) {
48 QJsonObject obj;
49 obj["name"_L1] = inherits->name;
50 obj["href"_L1] = inherits->href;
51 obj["moduleName"_L1] = inherits->moduleName;
52 json["inherits"_L1] = obj;
53 }
54
55 if (!inheritedBy.isEmpty()) {
56 QJsonArray arr;
57 for (const auto &entry : inheritedBy) {
58 QJsonObject obj;
59 obj["name"_L1] = entry.name;
60 obj["href"_L1] = entry.href;
61 arr.append(obj);
62 }
63 json["inheritedBy"_L1] = arr;
64 }
65
66 if (nativeType) {
67 QJsonObject obj;
68 obj["name"_L1] = nativeType->name;
69 obj["href"_L1] = nativeType->href;
70 json["nativeType"_L1] = obj;
71 }
72
73 return json;
74}
75
76static QJsonArray memberEntriesToJson(const QList<CollectionInfo::MemberEntry> &entries)
77{
78 QJsonArray arr;
79 for (const auto &entry : entries) {
80 QJsonObject obj;
81 obj["name"_L1] = entry.name;
82 obj["href"_L1] = entry.href;
83 obj["brief"_L1] = entry.brief;
84 arr.append(obj);
85 }
86 return arr;
87}
88
89/*!
90 Converts the CollectionInfo to a QJsonObject for template rendering.
91
92 Type flags (\c isModule, \c isQmlModule, \c isGroup) and \c noAutoList are
93 always emitted so templates can use unconditional checks. CMake/qmake build
94 variables are always emitted as empty strings when absent, for template
95 safety. Module metadata (\c logicalModuleName, \c logicalModuleVersion,
96 \c state) is conditionally emitted when non-empty. Member arrays are always
97 emitted (empty arrays when no entries) so Inja can iterate without guards.
98*/
100{
101 QJsonObject json;
102
103 json["isModule"_L1] = isModule;
104 json["isQmlModule"_L1] = isQmlModule;
105 json["isGroup"_L1] = isGroup;
106 json["noAutoList"_L1] = noAutoList;
107
108 if (!logicalModuleName.isEmpty())
109 json["logicalModuleName"_L1] = logicalModuleName;
110 if (!logicalModuleVersion.isEmpty())
111 json["logicalModuleVersion"_L1] = logicalModuleVersion;
112 if (!state.isEmpty())
113 json["state"_L1] = state;
114
115 json["qtVariable"_L1] = qtVariable;
116 json["cmakePackage"_L1] = cmakePackage;
117 json["cmakeComponent"_L1] = cmakeComponent;
118 json["cmakeTargetItem"_L1] = cmakeTargetItem;
119
120 json["namespaces"_L1] = memberEntriesToJson(namespaces);
121 json["classes"_L1] = memberEntriesToJson(classes);
122 json["members"_L1] = memberEntriesToJson(members);
123
124 return json;
125}
126
127/*!
128 Converts the Document to a QJsonObject for template rendering.
129
130 The JSON structure follows a convention where field names use camelCase
131 and match template variable names. Classification fields (nodeType, genus,
132 status, access) use a two-part structure with "id" (stable kebab-case
133 identifier for conditionals) and "label" (human-readable display name).
134
135 The \c contentJson field is nested under a 'content' key to provide better
136 structure and namespace separation in templates.
137
138 Returns a QJsonObject containing all IR data in a format suitable for
139 passing to the Inja template engine via InjaBridge.
140*/
142{
143 QJsonObject json;
144
145 // Classification (as {id, label} objects for template convenience)
146 // nodeType and genus are omitted when unclassified (NoType/DontCare),
147 // allowing templates to use `if defined` checks.
148 if (const auto t = nodeTypeToJson(nodeType))
149 json["nodeType"_L1] = *t;
150 if (const auto g = genusToJson(genus))
151 json["genus"_L1] = *g;
152 json["status"_L1] = statusToJson(status);
153 json["access"_L1] = accessToJson(access);
154
155 // Identity
156 json["title"_L1] = title;
157 json["fullTitle"_L1] = fullTitle;
158 json["url"_L1] = url;
159 if (!since.isEmpty())
160 json["since"_L1] = since;
161 if (!deprecatedSince.isEmpty())
162 json["deprecatedSince"_L1] = deprecatedSince;
163 if (!brief.isEmpty())
164 json["brief"_L1] = brief;
165
166 Q_ASSERT(!contentJson.contains("blocks"_L1));
167 QJsonObject content = contentJson;
168
169 QJsonArray blocks;
170 for (const auto &block : body)
171 blocks.append(block.toJson());
172 content["blocks"_L1] = blocks;
173
174 json["content"_L1] = content;
175
176 // QML type metadata (omitted for non-QML pages)
177 json["hasQmlType"_L1] = qmlTypeInfo.has_value();
178 if (qmlTypeInfo)
179 json["qmlType"_L1] = qmlTypeInfo->toJson();
180
181 // Collection metadata (module, QML module, and group pages).
182 json["hasCollection"_L1] = collectionInfo.has_value();
183 if (collectionInfo)
184 json["collection"_L1] = collectionInfo->toJson();
185
186 // Members sub-page URL (always emitted for Inja root-level variable safety;
187 // empty string when no members sub-page was generated)
188 json["membersPageUrl"_L1] = membersPageUrl;
189
190 // Sections (for aggregate pages with member listings).
191 // Always emitted (even when empty) so templates can iterate safely
192 // without existsIn() guards on the root data object.
193 QJsonArray sectionsArray;
194 for (const auto &section : summarySections)
195 sectionsArray.append(section.toJson());
196 json["sections"_L1] = sectionsArray;
197
198 QJsonArray detailSectionsArray;
199 for (const auto &section : detailSections)
200 detailSectionsArray.append(section.toJson());
201 json["detailSections"_L1] = detailSectionsArray;
202
203 return json;
204}
205
206} // namespace IR
207
208QT_END_NAMESPACE
Definition builder.cpp:14
static QJsonArray memberEntriesToJson(const QList< CollectionInfo::MemberEntry > &entries)
Definition document.cpp:76
Combined button and popup list for selecting options.
QJsonObject toJson() const
Converts the CollectionInfo to a QJsonObject for template rendering.
Definition document.cpp:99
Intermediate representation for a documentation topic.
Definition document.h:81
QJsonObject toJson() const
Converts the Document to a QJsonObject for template rendering.
Definition document.cpp:141
QJsonObject toJson() const
Converts the QmlTypeInfo to a QJsonObject for template rendering.
Definition document.cpp:38