1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QQMLBINDING_P_H
41 #define QQMLBINDING_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include "qqml.h"
55 #include "qqmlpropertyvaluesource.h"
56 #include "qqmlexpression.h"
57 #include "qqmlproperty.h"
58 #include "qqmlscriptstring.h"
59 #include "qqmlproperty_p.h"
60 
61 #include <QtCore/QObject>
62 #include <QtCore/QMetaProperty>
63 
64 #include <private/qqmlabstractbinding_p.h>
65 #include <private/qqmljavascriptexpression_p.h>
66 #include <private/qv4functionobject_p.h>
67 
68 QT_BEGIN_NAMESPACE
69 
70 class QQmlContext;
71 class Q_QML_PRIVATE_EXPORT QQmlBinding : public QQmlJavaScriptExpression,
72                                          public QQmlAbstractBinding
73 {
74     friend class QQmlAbstractBinding;
75 public:
76     typedef QExplicitlySharedDataPointer<QQmlBinding> Ptr;
77 
78     static QQmlBinding *create(const QQmlPropertyData *, const QQmlScriptString &, QObject *, QQmlContext *);
79     static QQmlBinding *create(const QQmlPropertyData *, const QString &, QObject *, QQmlContextData *,
80                                const QString &url = QString(), quint16 lineNumber = 0);
81     static QQmlBinding *create(const QQmlPropertyData *property, QV4::Function *function,
82                                QObject *obj, QQmlContextData *ctxt, QV4::ExecutionContext *scope);
83     static QQmlBinding *createTranslationBinding(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit, const QV4::CompiledData::Binding *binding,
84                                                  QObject *obj, QQmlContextData *ctxt);
85     ~QQmlBinding() override;
86 
87     void setTarget(const QQmlProperty &);
88     bool setTarget(QObject *, const QQmlPropertyData &, const QQmlPropertyData *valueType);
89 
90     void setNotifyOnValueChanged(bool);
91 
92     void refresh() override;
93 
94     void setEnabled(bool, QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding) override;
95     QString expression() const override;
96     void update(QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding);
97 
98     typedef int Identifier;
99     enum {
100         Invalid = -1
101     };
102 
103     QVariant evaluate();
104 
105     QString expressionIdentifier() const override;
106     void expressionChanged() override;
107 
108     QQmlSourceLocation sourceLocation() const override;
109     void setSourceLocation(const QQmlSourceLocation &location);
setBoundFunction(QV4::BoundFunction * boundFunction)110     void setBoundFunction(QV4::BoundFunction *boundFunction) {
111         m_boundFunction.set(boundFunction->engine(), *boundFunction);
112     }
113 
114     /**
115      * This method returns a snapshot of the currently tracked dependencies of
116      * this binding. The dependencies can change upon reevaluation. This method is
117      * used in GammaRay to visualize binding hierarchies.
118      *
119      * Call this method from the UI thread.
120      */
121     QVector<QQmlProperty> dependencies() const;
122     virtual bool hasDependencies() const;
123 
124 protected:
125     virtual void doUpdate(const DeleteWatcher &watcher,
126                           QQmlPropertyData::WriteFlags flags, QV4::Scope &scope) = 0;
127 
128     void getPropertyData(QQmlPropertyData **propertyData, QQmlPropertyData *valueTypeData) const;
129     int getPropertyType() const;
130 
131     bool slowWrite(const QQmlPropertyData &core, const QQmlPropertyData &valueTypeData,
132                    const QV4::Value &result, bool isUndefined, QQmlPropertyData::WriteFlags flags);
133 
134     QV4::ReturnedValue evaluate(bool *isUndefined);
135 
136 private:
137     inline bool updatingFlag() const;
138     inline void setUpdatingFlag(bool);
139     inline bool enabledFlag() const;
140     inline void setEnabledFlag(bool);
141 
142     static QQmlBinding *newBinding(QQmlEnginePrivate *engine, const QQmlPropertyData *property);
143 
144     QQmlSourceLocation *m_sourceLocation = nullptr; // used for Qt.binding() created functions
145     QV4::PersistentValue m_boundFunction; // used for Qt.binding() that are created from a bound function object
146 };
147 
updatingFlag()148 bool QQmlBinding::updatingFlag() const
149 {
150     return m_target.flag();
151 }
152 
setUpdatingFlag(bool v)153 void QQmlBinding::setUpdatingFlag(bool v)
154 {
155     m_target.setFlagValue(v);
156 }
157 
enabledFlag()158 bool QQmlBinding::enabledFlag() const
159 {
160     return m_target.flag2();
161 }
162 
setEnabledFlag(bool v)163 void QQmlBinding::setEnabledFlag(bool v)
164 {
165     m_target.setFlag2Value(v);
166 }
167 
168 QT_END_NAMESPACE
169 
170 Q_DECLARE_METATYPE(QQmlBinding*)
171 
172 #endif // QQMLBINDING_P_H
173