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
qqmljsmetatypes.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
7
8#include "QtQml/private/qqmltranslation_p.h"
9
11
12/*!
13 \class QQmlJSMetaPropertyBinding
14 \inmodule QtQmlCompiler
15 \internal
16
17 Represents a single QML binding of a specific type. Typically, when you
18 create a new binding, you know all the details of it already, so you should
19 just set all the data at once.
20*/
21
22/*!
23 A binding is valid when it has both a target (m_propertyName is set)
24 and some content set (m_bindingType != Invalid).
25*/
26bool QQmlJSMetaPropertyBinding::isValid() const
27{
28 return !m_propertyName.isEmpty() && bindingType() != QQmlSA::BindingType::Invalid;
29}
30
31bool QQmlJSMetaPropertyBinding::boolValue() const
32{
33 if (auto boolLit = std::get_if<Content::BoolLiteral>(&m_bindingContent))
34 return boolLit->value;
35 // warn
36 return false;
37}
38
39double QQmlJSMetaPropertyBinding::numberValue() const
40{
41 if (auto numberLit = std::get_if<Content::NumberLiteral>(&m_bindingContent))
42 return numberLit->value;
43 // warn
44 return 0;
45}
46
47QString QQmlJSMetaPropertyBinding::stringValue() const
48{
49 if (auto stringLiteral = std::get_if<Content::StringLiteral>(&m_bindingContent))
50 return stringLiteral->value;
51 // warn
52 return {};
53}
54
55QString QQmlJSMetaPropertyBinding::regExpValue() const
56{
57 if (auto regexpLiteral = std::get_if<Content::RegexpLiteral>(&m_bindingContent))
58 return regexpLiteral->value;
59 // warn
60 return {};
61}
62
63 /*!
64 * Extracts the information about translations from a binding.
65 * An additional context string is needed for text based translation (e.g. with qsTr())
66 * and can be obtained from the name of the qml file.
67 *
68 * \sa QQmlTranslation
69 */
70QQmlTranslation QQmlJSMetaPropertyBinding::translationDataValue(QString qmlFileNameForContext) const
71{
72 QQmlTranslation::Data data;
73 if (auto translation = std::get_if<Content::TranslationById>(&m_bindingContent)) {
74 data = QQmlTranslation::QsTrIdData(translation->id, translation->number);
75 } else if (auto translation = std::get_if<Content::TranslationString>(&m_bindingContent)) {
76 const QString context = translation->context.isEmpty()
77 ? QQmlTranslation::contextFromQmlFilename(qmlFileNameForContext)
78 : translation->context;
79 data = QQmlTranslation::QsTrData(context, translation->text, translation->comment,
80 translation->number);
81 }
82 return QQmlTranslation(data);
83}
84
85/*!
86 Uses \a resolver to return the correct type for the stored literal
87 and a null scope pointer if the binding does not contain a literal
88 */
89QSharedPointer<const QQmlJSScope> QQmlJSMetaPropertyBinding::literalType(const QQmlJSTypeResolver *resolver) const
90{
91 Q_ASSERT(resolver);
92 switch (bindingType()) {
93 case BindingType::BoolLiteral:
94 return resolver->boolType();
95 case BindingType::NumberLiteral:
96 return resolver->realType();
97 case BindingType::Translation: // translations are strings
98 case BindingType::TranslationById:
99 case BindingType::StringLiteral:
100 return resolver->stringType();
101 case BindingType::RegExpLiteral:
102 return resolver->regexpType();
103 case BindingType::Null:
104 return resolver->nullType();
105 case BindingType::Invalid:
106 case BindingType::Script:
107 case BindingType::Object:
108 case BindingType::Interceptor:
109 case BindingType::ValueSource:
110 case BindingType::AttachedProperty:
111 case BindingType::GroupProperty:
112 return {};
113 }
114 Q_UNREACHABLE_RETURN({});
115}
116
117QQmlJSMetaPropertyBinding::QQmlJSMetaPropertyBinding() = default;
118
119QT_END_NAMESPACE
Combined button and popup list for selecting options.