2015-05-19 16:11:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 14:59:14 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-05-19 16:11:35 +02:00
|
|
|
**
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
**
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:59:14 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-05-19 16:11:35 +02:00
|
|
|
**
|
2015-09-18 11:34:48 +02:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
2016-01-15 14:59:14 +01:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-05-19 16:11:35 +02:00
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "designercustomobjectdata.h"
|
|
|
|
|
|
|
|
#include <qmlprivategate.h>
|
|
|
|
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
|
|
|
|
#include <private/qqmlbinding_p.h>
|
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
namespace QmlPrivateGate {
|
|
|
|
|
|
|
|
QHash<QObject *, DesignerCustomObjectData*> m_objectToDataHash;
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
DesignerCustomObjectData::DesignerCustomObjectData(QObject *object)
|
2015-05-19 16:11:35 +02:00
|
|
|
: m_object(object)
|
|
|
|
{
|
|
|
|
if (object)
|
|
|
|
populateResetHashes();
|
|
|
|
m_objectToDataHash.insert(object, this);
|
|
|
|
QObject::connect(object, &QObject::destroyed, [=] {
|
|
|
|
m_objectToDataHash.remove(object);
|
|
|
|
delete this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
void DesignerCustomObjectData::registerData(QObject *object)
|
2015-05-19 16:11:35 +02:00
|
|
|
{
|
2015-05-19 16:37:55 +02:00
|
|
|
new DesignerCustomObjectData(object);
|
2015-05-19 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DesignerCustomObjectData *DesignerCustomObjectData::get(QObject *object)
|
|
|
|
{
|
|
|
|
return m_objectToDataHash.value(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DesignerCustomObjectData::getResetValue(QObject *object, const PropertyName &propertyName)
|
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
return data->getResetValue(propertyName);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
void DesignerCustomObjectData::doResetProperty(QObject *object, QQmlContext *context, const PropertyName &propertyName)
|
2015-05-19 16:11:35 +02:00
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
2015-05-19 16:37:55 +02:00
|
|
|
data->doResetProperty(context, propertyName);
|
2015-05-19 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DesignerCustomObjectData::hasValidResetBinding(QObject *object, const PropertyName &propertyName)
|
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
return data->hasValidResetBinding(propertyName);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
bool DesignerCustomObjectData::hasBindingForProperty(QObject *object, QQmlContext *context, const PropertyName &propertyName, bool *hasChanged)
|
2015-05-19 16:11:35 +02:00
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
2015-05-19 16:37:55 +02:00
|
|
|
return data->hasBindingForProperty(context, propertyName, hasChanged);
|
2015-05-19 16:11:35 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
void DesignerCustomObjectData::setPropertyBinding(QObject *object, QQmlContext *context, const PropertyName &propertyName, const QString &expression)
|
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
data->setPropertyBinding(context, propertyName, expression);
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:48:47 +02:00
|
|
|
void DesignerCustomObjectData::keepBindingFromGettingDeleted(QObject *object, QQmlContext *context, const PropertyName &propertyName)
|
|
|
|
{
|
|
|
|
DesignerCustomObjectData* data = get(object);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
data->keepBindingFromGettingDeleted(context, propertyName);
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:11:35 +02:00
|
|
|
void DesignerCustomObjectData::populateResetHashes()
|
|
|
|
{
|
|
|
|
PropertyNameList propertyNameList = QmlPrivateGate::propertyNameListForWritableProperties(object());
|
|
|
|
|
|
|
|
foreach (const PropertyName &propertyName, propertyNameList) {
|
2016-03-22 14:07:59 +01:00
|
|
|
QQmlProperty property(object(), QString::fromUtf8(propertyName), QQmlEngine::contextForObject(object()));
|
2015-05-19 16:11:35 +02:00
|
|
|
|
2015-07-01 13:48:04 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
QQmlAbstractBinding::Ptr binding = QQmlAbstractBinding::Ptr(QQmlPropertyPrivate::binding(property));
|
|
|
|
#else
|
2015-05-19 16:11:35 +02:00
|
|
|
QQmlAbstractBinding::Pointer binding = QQmlAbstractBinding::getPointer(QQmlPropertyPrivate::binding(property));
|
2015-07-01 13:48:04 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 16:11:35 +02:00
|
|
|
if (binding) {
|
|
|
|
m_resetBindingHash.insert(propertyName, binding);
|
|
|
|
} else if (property.isWritable()) {
|
|
|
|
m_resetValueHash.insert(propertyName, property.read());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_resetValueHash.insert("Layout.rowSpan", 1);
|
|
|
|
m_resetValueHash.insert("Layout.columnSpan", 1);
|
|
|
|
m_resetValueHash.insert("Layout.fillHeight", false);
|
|
|
|
m_resetValueHash.insert("Layout.fillWidth", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *DesignerCustomObjectData::object() const
|
|
|
|
{
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DesignerCustomObjectData::getResetValue(const PropertyName &propertyName) const
|
|
|
|
{
|
|
|
|
return m_resetValueHash.value(propertyName);
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
void DesignerCustomObjectData::doResetProperty(QQmlContext *context, const PropertyName &propertyName)
|
2015-05-19 16:11:35 +02:00
|
|
|
{
|
2016-03-22 14:07:59 +01:00
|
|
|
QQmlProperty property(object(), QString::fromUtf8(propertyName), context);
|
2015-05-19 16:11:35 +02:00
|
|
|
|
|
|
|
if (!property.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant oldValue = property.read();
|
|
|
|
if (oldValue.type() == QVariant::Url) {
|
|
|
|
QUrl url = oldValue.toUrl();
|
|
|
|
QString path = url.toLocalFile();
|
|
|
|
/* ### TODO
|
|
|
|
if (QFileInfo(path).exists() && nodeInstanceServer())
|
|
|
|
nodeInstanceServer()->removeFilePropertyFromFileSystemWatcher(object(), propertyName, path);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QQmlAbstractBinding *binding = QQmlPropertyPrivate::binding(property);
|
|
|
|
if (binding && !(hasValidResetBinding(propertyName) && getResetBinding(propertyName) == binding)) {
|
|
|
|
binding->setEnabled(false, 0);
|
2015-07-01 13:48:04 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
//Refcounting is taking care
|
|
|
|
#else
|
2015-05-19 16:11:35 +02:00
|
|
|
binding->destroy();
|
2015-07-01 13:48:04 +02:00
|
|
|
#endif
|
2015-05-19 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (hasValidResetBinding(propertyName)) {
|
|
|
|
QQmlAbstractBinding *binding = getResetBinding(propertyName);
|
2015-07-01 13:48:04 +02:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
QQmlBinding *qmlBinding = dynamic_cast<QQmlBinding*>(binding);
|
|
|
|
if (qmlBinding)
|
|
|
|
qmlBinding->setTarget(property);
|
|
|
|
QQmlPropertyPrivate::setBinding(binding, QQmlPropertyPrivate::None, QQmlPropertyPrivate::DontRemoveBinding);
|
|
|
|
if (qmlBinding)
|
|
|
|
qmlBinding->update();
|
|
|
|
#else
|
2015-05-19 16:11:35 +02:00
|
|
|
QQmlPropertyPrivate::setBinding(property, binding, QQmlPropertyPrivate::DontRemoveBinding);
|
|
|
|
binding->update();
|
2015-07-01 13:48:04 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 16:11:35 +02:00
|
|
|
} else if (property.isResettable()) {
|
|
|
|
property.reset();
|
|
|
|
} else if (property.propertyTypeCategory() == QQmlProperty::List) {
|
|
|
|
QQmlListReference list = qvariant_cast<QQmlListReference>(property.read());
|
|
|
|
|
|
|
|
if (!QmlPrivateGate::hasFullImplementedListInterface(list)) {
|
|
|
|
qWarning() << "Property list interface not fully implemented for Class " << property.property().typeName() << " in property " << property.name() << "!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.clear();
|
|
|
|
} else if (property.isWritable()) {
|
|
|
|
if (property.read() == getResetValue(propertyName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
property.write(getResetValue(propertyName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DesignerCustomObjectData::hasValidResetBinding(const PropertyName &propertyName) const
|
|
|
|
{
|
|
|
|
return m_resetBindingHash.contains(propertyName) && m_resetBindingHash.value(propertyName).data();
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlAbstractBinding *DesignerCustomObjectData::getResetBinding(const PropertyName &propertyName) const
|
|
|
|
{
|
|
|
|
return m_resetBindingHash.value(propertyName).data();
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
bool DesignerCustomObjectData::hasBindingForProperty(QQmlContext *context, const PropertyName &propertyName, bool *hasChanged) const
|
2015-05-19 16:11:35 +02:00
|
|
|
{
|
|
|
|
if (QmlPrivateGate::isPropertyBlackListed(propertyName))
|
|
|
|
return false;
|
|
|
|
|
2016-03-22 14:07:59 +01:00
|
|
|
QQmlProperty property(object(), QString::fromUtf8(propertyName), context);
|
2015-05-19 16:11:35 +02:00
|
|
|
|
|
|
|
bool hasBinding = QQmlPropertyPrivate::binding(property);
|
|
|
|
|
|
|
|
if (hasChanged) {
|
|
|
|
*hasChanged = hasBinding != m_hasBindingHash.value(propertyName, false);
|
|
|
|
if (*hasChanged)
|
|
|
|
m_hasBindingHash.insert(propertyName, hasBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QQmlPropertyPrivate::binding(property);
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:55 +02:00
|
|
|
void DesignerCustomObjectData::setPropertyBinding(QQmlContext *context, const PropertyName &propertyName, const QString &expression)
|
|
|
|
{
|
2016-03-22 14:07:59 +01:00
|
|
|
QQmlProperty property(object(), QString::fromUtf8(propertyName), context);
|
2015-05-19 16:37:55 +02:00
|
|
|
|
|
|
|
if (!property.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (property.isProperty()) {
|
|
|
|
QQmlBinding *binding = new QQmlBinding(expression, object(), context);
|
|
|
|
binding->setTarget(property);
|
|
|
|
binding->setNotifyOnValueChanged(true);
|
2015-07-01 13:48:04 +02:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
QQmlPropertyPrivate::setBinding(binding, QQmlPropertyPrivate::None, QQmlPropertyPrivate::DontRemoveBinding);
|
|
|
|
//Refcounting is taking take care of deletion
|
|
|
|
#else
|
2015-05-19 16:37:55 +02:00
|
|
|
QQmlAbstractBinding *oldBinding = QQmlPropertyPrivate::setBinding(property, binding);
|
|
|
|
if (oldBinding && !hasValidResetBinding(propertyName))
|
|
|
|
oldBinding->destroy();
|
2015-07-01 13:48:04 +02:00
|
|
|
#endif
|
2015-05-19 16:37:55 +02:00
|
|
|
binding->update();
|
|
|
|
if (binding->hasError()) {
|
|
|
|
if (property.property().userType() == QVariant::String)
|
|
|
|
property.write(QVariant(QString("#%1#").arg(expression)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qWarning() << Q_FUNC_INFO << ": Cannot set binding for property" << propertyName << ": property is unknown for type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:48:47 +02:00
|
|
|
void DesignerCustomObjectData::keepBindingFromGettingDeleted(QQmlContext *context, const PropertyName &propertyName)
|
|
|
|
{
|
2015-07-01 13:48:04 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
//Refcounting is taking care
|
|
|
|
Q_UNUSED(context)
|
|
|
|
Q_UNUSED(propertyName)
|
|
|
|
#else
|
2016-03-22 14:07:59 +01:00
|
|
|
QQmlProperty property(object(), QString::fromUtf8(propertyName), context);
|
2015-05-19 16:48:47 +02:00
|
|
|
QQmlPropertyPrivate::setBinding(property, 0, QQmlPropertyPrivate::BypassInterceptor
|
|
|
|
| QQmlPropertyPrivate::DontRemoveBinding);
|
2015-07-01 13:48:04 +02:00
|
|
|
#endif
|
2015-05-19 16:48:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-19 16:11:35 +02:00
|
|
|
|
|
|
|
} // namespace QmlPrivateGate
|
|
|
|
} // namespace Internal
|
|
|
|
} // namespace QmlDesigner
|
|
|
|
|