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 \internal
14 A binding is valid when it has both a target (m_propertyName is set)
15 and some content set (m_bindingType != Invalid).
16*/
17bool QQmlJSMetaPropertyBinding::isValid() const
18{
19 return !m_propertyName.isEmpty() && bindingType() != QQmlSA::BindingType::Invalid;
20}
21
22bool QQmlJSMetaPropertyBinding::boolValue() const
23{
24 if (auto boolLit = std::get_if<Content::BoolLiteral>(&m_bindingContent))
25 return boolLit->value;
26 // warn
27 return false;
28}
29
30double QQmlJSMetaPropertyBinding::numberValue() const
31{
32 if (auto numberLit = std::get_if<Content::NumberLiteral>(&m_bindingContent))
33 return numberLit->value;
34 // warn
35 return 0;
36}
37
38QString QQmlJSMetaPropertyBinding::stringValue() const
39{
40 if (auto stringLiteral = std::get_if<Content::StringLiteral>(&m_bindingContent))
41 return stringLiteral->value;
42 // warn
43 return {};
44}
45
46QString QQmlJSMetaPropertyBinding::regExpValue() const
47{
48 if (auto regexpLiteral = std::get_if<Content::RegexpLiteral>(&m_bindingContent))
49 return regexpLiteral->value;
50 // warn
51 return {};
52}
53
54 /*!
55 * Extracts the information about translations from a binding.
56 * An additional context string is needed for text based translation (e.g. with qsTr())
57 * and can be obtained from the name of the qml file.
58 *
59 * \sa QQmlTranslation
60 */
61QQmlTranslation QQmlJSMetaPropertyBinding::translationDataValue(QString qmlFileNameForContext) const
62{
63 QQmlTranslation::Data data;
64 if (auto translation = std::get_if<Content::TranslationById>(&m_bindingContent)) {
65 data = QQmlTranslation::QsTrIdData(translation->id, translation->number);
66 } else if (auto translation = std::get_if<Content::TranslationString>(&m_bindingContent)) {
67 const QString context = translation->context.isEmpty()
68 ? QQmlTranslation::contextFromQmlFilename(qmlFileNameForContext)
69 : translation->context;
70 data = QQmlTranslation::QsTrData(context, translation->text, translation->comment,
71 translation->number);
72 }
73 return QQmlTranslation(data);
74}
75
76/*!
77 \internal
78 Uses \a resolver to return the correct type for the stored literal
79 and a null scope pointer if the binding does not contain a literal
80 */
81QSharedPointer<const QQmlJSScope> QQmlJSMetaPropertyBinding::literalType(const QQmlJSTypeResolver *resolver) const
82{
83 Q_ASSERT(resolver);
84 switch (bindingType()) {
85 case BindingType::BoolLiteral:
86 return resolver->boolType();
87 case BindingType::NumberLiteral:
88 return resolver->realType();
89 case BindingType::Translation: // translations are strings
90 case BindingType::TranslationById:
91 case BindingType::StringLiteral:
92 return resolver->stringType();
93 case BindingType::RegExpLiteral:
94 return resolver->regexpType();
95 case BindingType::Null:
96 return resolver->nullType();
97 case BindingType::Invalid:
98 case BindingType::Script:
99 case BindingType::Object:
100 case BindingType::Interceptor:
101 case BindingType::ValueSource:
102 case BindingType::AttachedProperty:
103 case BindingType::GroupProperty:
104 return {};
105 }
106 Q_UNREACHABLE_RETURN({});
107}
108
109QQmlJSMetaPropertyBinding::QQmlJSMetaPropertyBinding() = default;
110
111QT_END_NAMESPACE