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

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtdeclarative/src/qmlcompiler/qqmljsmetatypes.cpp