diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2017-07-12 14:39:45 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2017-07-14 10:29:49 +0000 |
commit | 61887379b0c823953b61120532055fcbd881aadd (patch) | |
tree | 979ce7e32537acbdd96fa4a5ab9b95db6f765a91 /src/qml/qml/qqmlbinding.cpp | |
parent | 22a2cc43387ec3b9f74a6c01f8665378a4541147 (diff) |
Add support for QEvent::LanguageChange
Respond to the language change event by refreshing all binding
expressions. For constant string translation bindings we must now create
special QQmlBinding instances instead of a one-time property write
meta-call upon instantiation. Those however are more lightweight than an
entire JavaScript expression.
In addition this provides a slot to explicitly trigger a re-evaluation
of bindings, to make it a little easier to discover for the developer.
[ChangeLog][QtQml][QQmlEngine] Added retranslate() slot and
QEvent::LanguageChange support to refresh bindings when changing the
language at run-time.
Task-number: QTBUG-15602
Change-Id: Ide174648e1d8a5738acb88e15495018d0869d7bc
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Diffstat (limited to 'src/qml/qml/qqmlbinding.cpp')
-rw-r--r-- | src/qml/qml/qqmlbinding.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlbinding.cpp b/src/qml/qml/qqmlbinding.cpp index 62288a5845..0ccb4e8e4e 100644 --- a/src/qml/qml/qqmlbinding.cpp +++ b/src/qml/qml/qqmlbinding.cpp @@ -195,7 +195,7 @@ class QQmlNonbindingBinding: public QQmlBinding { protected: void doUpdate(const DeleteWatcher &watcher, - QQmlPropertyData::WriteFlags flags, QV4::Scope &scope) Q_DECL_OVERRIDE Q_DECL_FINAL + QQmlPropertyData::WriteFlags flags, QV4::Scope &scope) Q_DECL_OVERRIDE { auto ep = QQmlEnginePrivate::get(scope.engine); ep->referenceScarceResources(); @@ -296,6 +296,50 @@ protected: } }; +class QQmlTranslationBinding : public GenericBinding<QMetaType::QString> { +public: + QQmlTranslationBinding(QV4::CompiledData::CompilationUnit *compilationUnit, const QV4::CompiledData::Binding *binding) + { + setCompilationUnit(compilationUnit); + m_binding = binding; + setSourceLocation(QQmlSourceLocation(compilationUnit->fileName(), binding->valueLocation.line, binding->valueLocation.column)); + } + + void doUpdate(const DeleteWatcher &watcher, + QQmlPropertyData::WriteFlags flags, QV4::Scope &) Q_DECL_OVERRIDE Q_DECL_FINAL + { + if (watcher.wasDeleted()) + return; + + if (!isAddedToObject() || hasError()) + return; + + const QString result = m_binding->valueAsString(m_compilationUnit->data); + + Q_ASSERT(targetObject()); + + QQmlPropertyData *pd; + QQmlPropertyData vpd; + getPropertyData(&pd, &vpd); + Q_ASSERT(pd); + doStore(result, pd, flags); + } + +private: + const QV4::CompiledData::Binding *m_binding; +}; + +QQmlBinding *QQmlBinding::createTranslationBinding(QV4::CompiledData::CompilationUnit *unit, const QV4::CompiledData::Binding *binding, QObject *obj, QQmlContextData *ctxt) +{ + QQmlTranslationBinding *b = new QQmlTranslationBinding(unit, binding); + + b->setNotifyOnValueChanged(true); + b->QQmlJavaScriptExpression::setContext(ctxt); + b->setScopeObject(obj); + + return b; +} + Q_NEVER_INLINE bool QQmlBinding::slowWrite(const QQmlPropertyData &core, const QQmlPropertyData &valueTypeData, const QV4::Value &result, |