src/declarative/qml/qdeclarativebinding.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "private/qdeclarativebinding_p.h"
       
    43 #include "private/qdeclarativebinding_p_p.h"
       
    44 
       
    45 #include "qdeclarative.h"
       
    46 #include "qdeclarativecontext.h"
       
    47 #include "qdeclarativeinfo.h"
       
    48 #include "private/qdeclarativecontext_p.h"
       
    49 #include "private/qdeclarativedata_p.h"
       
    50 #include "private/qdeclarativestringconverters_p.h"
       
    51 
       
    52 #include <QVariant>
       
    53 #include <QtCore/qdebug.h>
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 QDeclarativeBindingData::QDeclarativeBindingData()
       
    58 : updating(false), enabled(false)
       
    59 {
       
    60 }
       
    61 
       
    62 QDeclarativeBindingData::~QDeclarativeBindingData()
       
    63 {
       
    64     removeError();
       
    65 }
       
    66 
       
    67 void QDeclarativeBindingData::refresh()
       
    68 {
       
    69     if (enabled && !updating && q) {
       
    70         QDeclarativeBinding *b = static_cast<QDeclarativeBinding *>(QDeclarativeExpressionPrivate::get(q));
       
    71         b->update();
       
    72     }
       
    73 }
       
    74 
       
    75 QDeclarativeBindingPrivate::QDeclarativeBindingPrivate()
       
    76 : QDeclarativeExpressionPrivate(new QDeclarativeBindingData)
       
    77 {
       
    78 }
       
    79 
       
    80 QDeclarativeBinding::QDeclarativeBinding(void *data, QDeclarativeRefCount *rc, QObject *obj, 
       
    81                                          QDeclarativeContextData *ctxt, const QString &url, int lineNumber, 
       
    82                                          QObject *parent)
       
    83 : QDeclarativeExpression(ctxt, data, rc, obj, url, lineNumber, *new QDeclarativeBindingPrivate)
       
    84 {
       
    85     setParent(parent);
       
    86     setNotifyOnValueChanged(true);
       
    87 }
       
    88 
       
    89 QDeclarativeBinding::QDeclarativeBinding(const QString &str, QObject *obj, QDeclarativeContext *ctxt, 
       
    90                                          QObject *parent)
       
    91 : QDeclarativeExpression(QDeclarativeContextData::get(ctxt), obj, str, *new QDeclarativeBindingPrivate)
       
    92 {
       
    93     setParent(parent);
       
    94     setNotifyOnValueChanged(true);
       
    95 }
       
    96 
       
    97 QDeclarativeBinding::QDeclarativeBinding(const QString &str, QObject *obj, QDeclarativeContextData *ctxt, 
       
    98                                          QObject *parent)
       
    99 : QDeclarativeExpression(ctxt, obj, str, *new QDeclarativeBindingPrivate)
       
   100 {
       
   101     setParent(parent);
       
   102     setNotifyOnValueChanged(true);
       
   103 }
       
   104 
       
   105 QDeclarativeBinding::~QDeclarativeBinding()
       
   106 {
       
   107 }
       
   108 
       
   109 void QDeclarativeBinding::setTarget(const QDeclarativeProperty &prop)
       
   110 {
       
   111     Q_D(QDeclarativeBinding);
       
   112     d->bindingData()->property = prop;
       
   113 
       
   114     update();
       
   115 }
       
   116 
       
   117 QDeclarativeProperty QDeclarativeBinding::property() const 
       
   118 {
       
   119    Q_D(const QDeclarativeBinding);
       
   120    return d->bindingData()->property; 
       
   121 }
       
   122 
       
   123 void QDeclarativeBinding::update(QDeclarativePropertyPrivate::WriteFlags flags)
       
   124 {
       
   125     Q_D(QDeclarativeBinding);
       
   126 
       
   127     QDeclarativeBindingData *data = d->bindingData();
       
   128 
       
   129     if (!data->enabled || !data->context() || !data->context()->isValid())
       
   130         return;
       
   131 
       
   132     data->addref();
       
   133 
       
   134     if (!data->updating) {
       
   135         data->updating = true;
       
   136 
       
   137         if (data->property.propertyType() == qMetaTypeId<QDeclarativeBinding *>()) {
       
   138 
       
   139             int idx = data->property.index();
       
   140             Q_ASSERT(idx != -1);
       
   141 
       
   142 
       
   143             QDeclarativeBinding *t = this;
       
   144             int status = -1;
       
   145             void *a[] = { &t, 0, &status, &flags };
       
   146             QMetaObject::metacall(data->property.object(),
       
   147                                   QMetaObject::WriteProperty,
       
   148                                   idx, a);
       
   149 
       
   150         } else {
       
   151             QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(data->context()->engine);
       
   152 
       
   153             bool isUndefined = false;
       
   154             QVariant value;
       
   155 
       
   156             QScriptValue scriptValue = d->scriptValue(0, &isUndefined);
       
   157             if (data->property.propertyTypeCategory() == QDeclarativeProperty::List) {
       
   158                 value = ep->scriptValueToVariant(scriptValue, qMetaTypeId<QList<QObject *> >());
       
   159             } else if (scriptValue.isNull() && 
       
   160                        data->property.propertyTypeCategory() == QDeclarativeProperty::Object) {
       
   161                 value = QVariant::fromValue((QObject *)0);
       
   162             } else {
       
   163                 value = ep->scriptValueToVariant(scriptValue, data->property.propertyType());
       
   164                 if (value.userType() == QMetaType::QObjectStar && !qvariant_cast<QObject*>(value)) {
       
   165                     // If the object is null, we extract the predicted type.  While this isn't
       
   166                     // 100% reliable, in many cases it gives us better error messages if we
       
   167                     // assign this null-object to an incompatible property
       
   168                     int type = ep->objectClass->objectType(scriptValue);
       
   169                     QObject *o = 0;
       
   170                     value = QVariant(type, (void *)&o);
       
   171                 }
       
   172             }
       
   173 
       
   174 
       
   175             if (data->error.isValid()) {
       
   176 
       
   177             } else if (isUndefined && data->property.isResettable()) {
       
   178 
       
   179                 data->property.reset();
       
   180 
       
   181             } else if (isUndefined && data->property.propertyType() == qMetaTypeId<QVariant>()) {
       
   182 
       
   183                 QDeclarativePropertyPrivate::write(data->property, QVariant(), flags);
       
   184 
       
   185             } else if (isUndefined) {
       
   186 
       
   187                 QUrl url = QUrl(data->url);
       
   188                 int line = data->line;
       
   189                 if (url.isEmpty()) url = QUrl(QLatin1String("<Unknown File>"));
       
   190 
       
   191                 data->error.setUrl(url);
       
   192                 data->error.setLine(line);
       
   193                 data->error.setColumn(-1);
       
   194                 data->error.setDescription(QLatin1String("Unable to assign [undefined] to ")
       
   195                     + QLatin1String(QMetaType::typeName(data->property.propertyType()))
       
   196                     + QLatin1String(" ") + data->property.name());
       
   197 
       
   198             } else if (!scriptValue.isRegExp() && scriptValue.isFunction()) {
       
   199 
       
   200                 QUrl url = QUrl(data->url);
       
   201                 int line = data->line;
       
   202                 if (url.isEmpty()) url = QUrl(QLatin1String("<Unknown File>"));
       
   203 
       
   204                 data->error.setUrl(url);
       
   205                 data->error.setLine(line);
       
   206                 data->error.setColumn(-1);
       
   207                 data->error.setDescription(QLatin1String("Unable to assign a function to a property."));
       
   208 
       
   209             } else if (data->property.object() &&
       
   210                        !QDeclarativePropertyPrivate::write(data->property, value, flags)) {
       
   211 
       
   212                 QUrl url = QUrl(data->url);
       
   213                 int line = data->line;
       
   214                 if (url.isEmpty()) url = QUrl(QLatin1String("<Unknown File>"));
       
   215 
       
   216                 const char *valueType = 0;
       
   217                 if (value.userType() == QVariant::Invalid) valueType = "null";
       
   218                 else valueType = QMetaType::typeName(value.userType());
       
   219 
       
   220                 data->error.setUrl(url);
       
   221                 data->error.setLine(line);
       
   222                 data->error.setColumn(-1);
       
   223                 data->error.setDescription(QLatin1String("Unable to assign ") +
       
   224                                            QLatin1String(valueType) +
       
   225                                            QLatin1String(" to ") +
       
   226                                            QLatin1String(QMetaType::typeName(data->property.propertyType())));
       
   227             }
       
   228 
       
   229             if (data->error.isValid()) {
       
   230                if (!data->addError(ep)) ep->warning(this->error());
       
   231             } else {
       
   232                 data->removeError();
       
   233             }
       
   234         }
       
   235 
       
   236         data->updating = false;
       
   237     } else {
       
   238         qmlInfo(data->property.object()) << tr("Binding loop detected for property \"%1\"").arg(data->property.name());
       
   239     }
       
   240 
       
   241     data->release();
       
   242 }
       
   243 
       
   244 void QDeclarativeBindingPrivate::emitValueChanged()
       
   245 {
       
   246     Q_Q(QDeclarativeBinding);
       
   247     q->update();
       
   248 }
       
   249 
       
   250 void QDeclarativeBinding::setEnabled(bool e, QDeclarativePropertyPrivate::WriteFlags flags)
       
   251 {
       
   252     Q_D(QDeclarativeBinding);
       
   253     d->bindingData()->enabled = e;
       
   254     setNotifyOnValueChanged(e);
       
   255 
       
   256     QDeclarativeAbstractBinding::setEnabled(e, flags);
       
   257 
       
   258     if (e) {
       
   259         addToObject(d->bindingData()->property.object());
       
   260         update(flags);
       
   261     } else {
       
   262         removeFromObject();
       
   263     }
       
   264 }
       
   265 
       
   266 int QDeclarativeBinding::propertyIndex()
       
   267 {
       
   268     Q_D(QDeclarativeBinding);
       
   269     return QDeclarativePropertyPrivate::bindingIndex(d->bindingData()->property);
       
   270 }
       
   271 
       
   272 bool QDeclarativeBinding::enabled() const
       
   273 {
       
   274     Q_D(const QDeclarativeBinding);
       
   275 
       
   276     return d->bindingData()->enabled;
       
   277 }
       
   278 
       
   279 QString QDeclarativeBinding::expression() const
       
   280 {
       
   281     return QDeclarativeExpression::expression();
       
   282 }
       
   283 
       
   284 QDeclarativeAbstractBinding::QDeclarativeAbstractBinding()
       
   285 : m_object(0), m_mePtr(0), m_prevBinding(0), m_nextBinding(0)
       
   286 {
       
   287 }
       
   288 
       
   289 QDeclarativeAbstractBinding::~QDeclarativeAbstractBinding()
       
   290 {
       
   291     Q_ASSERT(m_prevBinding == 0);
       
   292     Q_ASSERT(m_mePtr == 0);
       
   293 }
       
   294 
       
   295 void QDeclarativeAbstractBinding::destroy()
       
   296 {
       
   297     removeFromObject();
       
   298     clear();
       
   299 
       
   300     delete this;
       
   301 }
       
   302 
       
   303 void QDeclarativeAbstractBinding::addToObject(QObject *object)
       
   304 {
       
   305     Q_ASSERT(object);
       
   306 
       
   307     if (m_object == object)
       
   308         return;
       
   309 
       
   310     int index = propertyIndex();
       
   311 
       
   312     removeFromObject();
       
   313 
       
   314     Q_ASSERT(!m_prevBinding);
       
   315 
       
   316     m_object = object;
       
   317     QDeclarativeData *data = QDeclarativeData::get(object, true);
       
   318 
       
   319     if (index & 0xFF000000) {
       
   320         // Value type
       
   321 
       
   322         int coreIndex = index & 0xFFFFFF;
       
   323 
       
   324         // Find the value type proxy (if there is one)
       
   325         QDeclarativeValueTypeProxyBinding *proxy = 0;
       
   326         if (data->hasBindingBit(coreIndex)) {
       
   327             QDeclarativeAbstractBinding *b = data->bindings;
       
   328             while (b && b->propertyIndex() != coreIndex)
       
   329                 b = b->m_nextBinding;
       
   330             Q_ASSERT(b && b->bindingType() == QDeclarativeAbstractBinding::ValueTypeProxy);
       
   331             proxy = static_cast<QDeclarativeValueTypeProxyBinding *>(b);
       
   332         }
       
   333 
       
   334         if (!proxy) 
       
   335             proxy = new QDeclarativeValueTypeProxyBinding(object, coreIndex);
       
   336         proxy->addToObject(object);
       
   337 
       
   338         m_nextBinding = proxy->m_bindings;
       
   339         if (m_nextBinding) m_nextBinding->m_prevBinding = &m_nextBinding;
       
   340         m_prevBinding = &proxy->m_bindings;
       
   341         proxy->m_bindings = this;
       
   342 
       
   343     } else {
       
   344         m_nextBinding = data->bindings;
       
   345         if (m_nextBinding) m_nextBinding->m_prevBinding = &m_nextBinding;
       
   346         m_prevBinding = &data->bindings;
       
   347         data->bindings = this;
       
   348 
       
   349         data->setBindingBit(m_object, index);
       
   350     }
       
   351 }
       
   352 
       
   353 void QDeclarativeAbstractBinding::removeFromObject()
       
   354 {
       
   355     if (m_prevBinding) {
       
   356         int index = propertyIndex();
       
   357 
       
   358         *m_prevBinding = m_nextBinding;
       
   359         if (m_nextBinding) m_nextBinding->m_prevBinding = m_prevBinding;
       
   360         m_prevBinding = 0;
       
   361         m_nextBinding = 0;
       
   362 
       
   363         if (index & 0xFF000000) {
       
   364             // Value type - we don't remove the proxy from the object.  It will sit their happily
       
   365             // doing nothing for ever more.
       
   366         } else if (m_object) {
       
   367             QDeclarativeData *data = QDeclarativeData::get(m_object, false);
       
   368             if (data) data->clearBindingBit(index);
       
   369         }
       
   370 
       
   371         m_object = 0;
       
   372     }
       
   373 }
       
   374 
       
   375 void QDeclarativeAbstractBinding::clear()
       
   376 {
       
   377     if (m_mePtr) {
       
   378         *m_mePtr = 0;
       
   379         m_mePtr = 0;
       
   380     }
       
   381 }
       
   382 
       
   383 QString QDeclarativeAbstractBinding::expression() const
       
   384 {
       
   385     return QLatin1String("<Unknown>");
       
   386 }
       
   387 
       
   388 void QDeclarativeAbstractBinding::setEnabled(bool e, QDeclarativePropertyPrivate::WriteFlags)
       
   389 {
       
   390     if (e) m_mePtr = 0;
       
   391 }
       
   392 
       
   393 QDeclarativeValueTypeProxyBinding::QDeclarativeValueTypeProxyBinding(QObject *o, int index)
       
   394 : m_object(o), m_index(index), m_bindings(0)
       
   395 {
       
   396 }
       
   397 
       
   398 QDeclarativeValueTypeProxyBinding::~QDeclarativeValueTypeProxyBinding()
       
   399 {
       
   400     while (m_bindings) {
       
   401         QDeclarativeAbstractBinding *binding = m_bindings;
       
   402         binding->setEnabled(false, 0);
       
   403         binding->destroy();
       
   404     }
       
   405 }
       
   406 
       
   407 void QDeclarativeValueTypeProxyBinding::setEnabled(bool e, QDeclarativePropertyPrivate::WriteFlags flags)
       
   408 {
       
   409     if (e) {
       
   410         addToObject(m_object);
       
   411 
       
   412         QDeclarativeAbstractBinding *bindings = m_bindings;
       
   413         m_bindings = 0;
       
   414         recursiveEnable(bindings, flags);
       
   415     } else {
       
   416         removeFromObject();
       
   417 
       
   418         QDeclarativeAbstractBinding *bindings = m_bindings;
       
   419         m_bindings = 0;
       
   420         recursiveDisable(bindings);
       
   421     }
       
   422 }
       
   423 
       
   424 void QDeclarativeValueTypeProxyBinding::recursiveEnable(QDeclarativeAbstractBinding *b, QDeclarativePropertyPrivate::WriteFlags flags)
       
   425 {
       
   426     if (!b)
       
   427         return;
       
   428 
       
   429     QDeclarativeAbstractBinding *next = b->m_nextBinding;
       
   430     b->m_prevBinding = 0;
       
   431     b->m_nextBinding = 0;
       
   432     Q_ASSERT(b->m_mePtr == 0);
       
   433     b->m_mePtr = &b;
       
   434 
       
   435     recursiveEnable(next, flags);
       
   436 
       
   437     if (b)
       
   438         b->setEnabled(true, flags);
       
   439 }
       
   440 
       
   441 void QDeclarativeValueTypeProxyBinding::recursiveDisable(QDeclarativeAbstractBinding *b)
       
   442 {
       
   443     if (!b)
       
   444         return;
       
   445 
       
   446     recursiveDisable(b->m_nextBinding);
       
   447 
       
   448     b->setEnabled(false, 0);
       
   449 
       
   450     Q_ASSERT(b->m_prevBinding == 0);
       
   451     Q_ASSERT(b->m_nextBinding == 0);
       
   452     b->m_nextBinding = m_bindings;
       
   453     if (b->m_nextBinding) b->m_nextBinding->m_prevBinding = &b->m_nextBinding;
       
   454     b->m_prevBinding = &m_bindings;
       
   455     m_bindings = b;
       
   456 }
       
   457 
       
   458 int QDeclarativeValueTypeProxyBinding::propertyIndex()
       
   459 {
       
   460     return m_index;
       
   461 }
       
   462 
       
   463 void QDeclarativeValueTypeProxyBinding::update(QDeclarativePropertyPrivate::WriteFlags)
       
   464 {
       
   465 }
       
   466 
       
   467 QDeclarativeAbstractBinding *QDeclarativeValueTypeProxyBinding::binding(int propertyIndex)
       
   468 {
       
   469     QDeclarativeAbstractBinding *binding = m_bindings;
       
   470     
       
   471     while (binding && binding->propertyIndex() != propertyIndex) 
       
   472         binding = binding->m_nextBinding;
       
   473 
       
   474     return binding;
       
   475 }
       
   476 
       
   477 QT_END_NAMESPACE