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
qmlpropertynode.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include "classnode.h"
7#include "propertynode.h"
8#include "enumnode.h"
9
10#include <utility>
11#include "qdocdatabase.h"
12#include "utilities.h"
13
15
16/*!
17 Constructor for the QML property node.
18 */
19QmlPropertyNode::QmlPropertyNode(Aggregate *parent, const QString &name, QString type,
20 bool attached)
21 : Node(QmlProperty, parent, name),
22 m_type(std::move(type)),
23 m_attached(attached)
24{
25 if (m_type == "alias")
26 m_isAlias = true;
27 if (name.startsWith("__"))
28 setStatus(Internal);
29}
30
31/*!
32 \fn bool QmlPropertyNode::isReadOnly() const
33
34 Returns \c true if this QML property node is marked as a
35 read-only property.
36*/
37
38/*!
39 \fn const EnumNode *QmlPropertyNode::enumNode() const
40
41 Returns the node representing the C++ enumeration associated
42 with this property, or \nullptr.
43*/
44
45/*!
46 Returns the prefix to use for documentated enumerators from
47 the associated C++ enum for this property.
48*/
50{
51 return !m_enumNode.second.isEmpty() ?
52 m_enumNode.second : parent()->name();
53}
54
55/*!
56 Locates the node specified by \a path and sets it as the C++ enumeration
57 associated with this property.
58
59 \a registeredQmlName is used as the prefix in the generated enum value
60 documentation.
61
62 \note The target EnumNode is searched under the primary tree only.
63
64 Returns \c true on success.
65*/
66bool QmlPropertyNode::setEnumNode(const QString &path, const QString &registeredQmlName)
67{
68 m_enumNode.first = static_cast<EnumNode*>(
69 QDocDatabase::qdocDB()->primaryTree()->findNodeByNameAndType(path.split("::"), &Node::isEnumType)
70 );
71 m_enumNode.second = registeredQmlName;
72 return m_enumNode.first != nullptr;
73}
74
75/*!
76 Returns \c true if this QML property or attached property is
77 read-only. If the read-only status is not set explicitly
78 using the \\readonly command, QDoc attempts to resolve it
79 from the associated C++ class instantiated by the QML type
80 that this property belongs to.
81
82 \note Depending on how the QML type is implemented, this
83 information may not be available to QDoc. If so, add a debug
84 line but do not treat it as a warning.
85 */
87{
88 if (m_readOnly != FlagValueDefault)
89 return fromFlagValue(m_readOnly, false);
90
91 // Find the parent QML type node
92 auto *parent{this->parent()};
93 while (parent && !(parent->isQmlType()))
94 parent = parent->parent();
95
96 bool readonly{false};
97 if (auto qcn = static_cast<QmlTypeNode *>(parent); qcn && qcn->classNode()) {
98 if (auto propertyNode = findCorrespondingCppProperty(); propertyNode)
99 readonly = !propertyNode->isWritable();
100 else
101 qCDebug(lcQdoc).nospace()
102 << qPrintable(defLocation().toString())
103 << ": Automatic resolution of QML property attributes failed for "
104 << name()
105 << " (Q_PROPERTY not found in the C++ class hierarchy known to QDoc. "
106 << "Likely, the type is replaced with a private implementation.)";
107 }
108 markReadOnly(readonly);
109 return readonly;
110}
111
112/*!
113 Returns \c true if this QML property is marked with \required or the
114 corresponding C++ property uses the REQUIRED keyword.
115*/
117{
118 if (m_required != FlagValueDefault)
119 return fromFlagValue(m_required, false);
120
121 PropertyNode *pn = findCorrespondingCppProperty();
122 return pn != nullptr && pn->isRequired();
123}
124
125/*!
126 Returns a pointer this QML property's corresponding C++
127 property, if it has one.
128 */
129PropertyNode *QmlPropertyNode::findCorrespondingCppProperty()
130{
131 PropertyNode *pn;
132 Node *n = parent();
133 while (n && !(n->isQmlType()))
134 n = n->parent();
135 if (n) {
136 auto *qcn = static_cast<QmlTypeNode *>(n);
137 ClassNode *cn = qcn->classNode();
138 if (cn) {
139 /*
140 If there is a dot in the property name, first
141 find the C++ property corresponding to the QML
142 property group.
143 */
144 QStringList dotSplit = name().split(QChar('.'));
145 pn = cn->findPropertyNode(dotSplit[0]);
146 if (pn) {
147 /*
148 Now find the C++ property corresponding to
149 the QML property in the QML property group,
150 <group>.<property>.
151 */
152 if (dotSplit.size() > 1) {
153 QStringList path(extractClassName(pn->qualifiedDataType()));
154 Node *nn = QDocDatabase::qdocDB()->findClassNode(path);
155 if (nn) {
156 auto *cn = static_cast<ClassNode *>(nn);
157 PropertyNode *pn2 = cn->findPropertyNode(dotSplit[1]);
158 /*
159 If found, return the C++ property
160 corresponding to the QML property.
161 Otherwise, return the C++ property
162 corresponding to the QML property
163 group.
164 */
165 return (pn2 ? pn2 : pn);
166 }
167 } else
168 return pn;
169 }
170 }
171 }
172 return nullptr;
173}
174
175QT_END_NAMESPACE
The ClassNode represents a C++ class.
Definition classnode.h:21
bool isQmlType() const
Returns true if the node type is QmlType or QmlValueType.
Definition node.h:155
Aggregate * parent() const
Returns the node's parent pointer.
Definition node.h:244
static bool fromFlagValue(FlagValue fv, bool defaultValue)
Converts the enum fv back to a boolean value.
Definition node.cpp:748
@ FlagValueDefault
Definition node.h:114
LinkType
An unsigned char value that probably should be moved out of the Node base class.
Definition node.h:112
This class describes one instance of using the Q_PROPERTY macro.
bool isRequired() const
bool isWritable() const
This class provides exclusive access to the qdoc database, which consists of a forrest of trees and a...
static QDocDatabase * qdocDB()
Creates the singleton.
const QString & enumPrefix() const
Returns the prefix to use for documentated enumerators from the associated C++ enum for this property...
void markReadOnly(bool flag) override
If this node is a QmlPropertyNode, then the property's read-only flag is set to flag.
bool isRequired()
Returns true if this QML property is marked with \required or the corresponding C++ property uses the...
bool setEnumNode(const QString &path, const QString &registeredQmlName)
Locates the node specified by path and sets it as the C++ enumeration associated with this property.
bool isReadOnly()
Returns true if this QML property or attached property is read-only.
Combined button and popup list for selecting options.