aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlabstractbinding.cpp
blob: 1e5ce8d64a62ebcd93860885e357c83a00111a76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qqmlabstractbinding_p.h"

#include <QtQml/qqmlinfo.h>
#include <private/qqmlbinding_p.h>
#include <private/qqmlvaluetypeproxybinding_p.h>

QT_BEGIN_NAMESPACE

extern QQmlAbstractBinding::VTable QQmlBinding_vtable;
extern QQmlAbstractBinding::VTable QQmlValueTypeProxyBinding_vtable;
extern QQmlAbstractBinding::VTable QV4Bindings_Binding_vtable;
extern QQmlAbstractBinding::VTable QV8Bindings_Binding_vtable;

QQmlAbstractBinding::VTable *QQmlAbstractBinding::vTables[] = {
    &QQmlBinding_vtable,
    &QV4Bindings_Binding_vtable,
    &QV8Bindings_Binding_vtable,
    &QQmlValueTypeProxyBinding_vtable
};

QQmlAbstractBinding::QQmlAbstractBinding(BindingType bt)
: m_nextBindingPtr(bt)
{
    Q_ASSERT(bt <= 0x03);
}

QQmlAbstractBinding::~QQmlAbstractBinding()
{
    Q_ASSERT(isAddedToObject() == false);
    Q_ASSERT(nextBinding() == 0);
    Q_ASSERT(*m_mePtr == 0);
}

/*!
Add this binding to \a object.

This transfers ownership of the binding to the object, marks the object's property as
being bound.

However, it does not enable the binding itself or call update() on it.
*/
void QQmlAbstractBinding::addToObject()
{
    Q_ASSERT(!nextBinding());
    Q_ASSERT(isAddedToObject() == false);

    QObject *obj = object();
    Q_ASSERT(obj);

    int index = propertyIndex();

    QQmlData *data = QQmlData::get(obj, true);

    if (index & 0xFF000000) {
        // Value type

        int coreIndex = index & 0xFFFFFF;

        // Find the value type proxy (if there is one)
        QQmlValueTypeProxyBinding *proxy = 0;
        if (data->hasBindingBit(coreIndex)) {
            QQmlAbstractBinding *b = data->bindings;
            while (b && b->propertyIndex() != coreIndex)
                b = b->nextBinding();
            Q_ASSERT(b && b->bindingType() == QQmlAbstractBinding::ValueTypeProxy);
            proxy = static_cast<QQmlValueTypeProxyBinding *>(b);
        }

        if (!proxy) {
            proxy = new QQmlValueTypeProxyBinding(obj, coreIndex);

            Q_ASSERT(proxy->propertyIndex() == coreIndex);
            Q_ASSERT(proxy->object() == obj);

            proxy->addToObject();
        }

        setNextBinding(proxy->m_bindings);
        proxy->m_bindings = this;

    } else {
        setNextBinding(data->bindings);
        data->bindings = this;

        data->setBindingBit(obj, index);
    }

    setAddedToObject(true);
}

/*!
Remove the binding from the object.
*/
void QQmlAbstractBinding::removeFromObject()
{
    if (isAddedToObject()) {
        QObject *obj = object();
        int index = propertyIndex();

        QQmlData *data = QQmlData::get(obj, false);
        Q_ASSERT(data);

        if (index & 0xFF000000) {

            // Find the value type binding
            QQmlAbstractBinding *vtbinding = data->bindings;
            while (vtbinding->propertyIndex() != (index & 0xFFFFFF)) {
                vtbinding = vtbinding->nextBinding();
                Q_ASSERT(vtbinding);
            }
            Q_ASSERT(vtbinding->bindingType() == QQmlAbstractBinding::ValueTypeProxy);

            QQmlValueTypeProxyBinding *vtproxybinding =
                static_cast<QQmlValueTypeProxyBinding *>(vtbinding);

            QQmlAbstractBinding *binding = vtproxybinding->m_bindings;
            if (binding == this) {
                vtproxybinding->m_bindings = nextBinding();
            } else {
               while (binding->nextBinding() != this) {
                  binding = binding->nextBinding();
                  Q_ASSERT(binding);
               }
               binding->setNextBinding(nextBinding());
            }

            // Value type - we don't remove the proxy from the object.  It will sit their happily
            // doing nothing until it is removed by a write, a binding change or it is reused
            // to hold more sub-bindings.

        } else {

            if (data->bindings == this) {
                data->bindings = nextBinding();
            } else {
                QQmlAbstractBinding *binding = data->bindings;
                while (binding->nextBinding() != this) {
                    binding = binding->nextBinding();
                    Q_ASSERT(binding);
                }
                binding->setNextBinding(nextBinding());
            }

            data->clearBindingBit(index);
        }

        setNextBinding(0);
        setAddedToObject(false);
    }
}

void QQmlAbstractBinding::printBindingLoopError(QQmlProperty &prop)
{
    qmlInfo(prop.object()) << QString(QLatin1String("Binding loop detected for property \"%1\"")).arg(prop.name());
}


static void bindingDummyDeleter(QQmlAbstractBinding *)
{
}

QQmlAbstractBinding::Pointer QQmlAbstractBinding::weakPointer()
{
    if (m_mePtr.value().isNull())
        m_mePtr.value() = QSharedPointer<QQmlAbstractBinding>(this, bindingDummyDeleter);

    return m_mePtr.value().toWeakRef();
}

void QQmlAbstractBinding::clear()
{
    if (!m_mePtr.isNull()) {
        **m_mePtr = 0;
        m_mePtr = 0;
    }
}

void QQmlAbstractBinding::default_retargetBinding(QQmlAbstractBinding *, QObject *, int)
{
    qFatal("QQmlAbstractBinding::retargetBinding() called on illegal binding.");
}

QString QQmlAbstractBinding::default_expression(const QQmlAbstractBinding *)
{
    return QLatin1String("<Unknown>");
}

QT_END_NAMESPACE