| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickdesignersupportproperties_p.h" |
| 5 | |
| 6 | #include "qqmldesignermetaobject_p.h" |
| 7 | #include "qquickdesignercustomobjectdata_p.h" |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | static void addToPropertyNameListIfNotBlackListed(QQuickDesignerSupport::PropertyNameList *propertyNameList, |
| 12 | const QQuickDesignerSupport::PropertyName &propertyName) |
| 13 | { |
| 14 | if (!QQuickDesignerSupportProperties::isPropertyBlackListed(propertyName)) |
| 15 | propertyNameList->append(t: propertyName); |
| 16 | } |
| 17 | |
| 18 | void QQuickDesignerSupportProperties::createNewDynamicProperty(QObject *object, QQmlEngine *engine, const QString &name) |
| 19 | { |
| 20 | QQmlDesignerMetaObject::getNodeInstanceMetaObject(object, engine)->createNewDynamicProperty(name); |
| 21 | } |
| 22 | |
| 23 | void QQuickDesignerSupportProperties::registerNodeInstanceMetaObject(QObject *object, QQmlEngine *engine) |
| 24 | { |
| 25 | // we just create one and the ownership goes automatically to the object in nodeinstance see init method |
| 26 | QQmlDesignerMetaObject::getNodeInstanceMetaObject(object, engine); |
| 27 | } |
| 28 | |
| 29 | bool QQuickDesignerSupportProperties::hasFullImplementedListInterface(const QQmlListReference &list) |
| 30 | { |
| 31 | return list.isValid() && list.canCount() && list.canAt() && list.canAppend() && list.canClear(); |
| 32 | } |
| 33 | |
| 34 | void QQuickDesignerSupportProperties::registerCustomData(QObject *object) |
| 35 | { |
| 36 | QQuickDesignerCustomObjectData::registerData(object); |
| 37 | } |
| 38 | |
| 39 | QVariant QQuickDesignerSupportProperties::getResetValue(QObject *object, const QQuickDesignerSupport::PropertyName &propertyName) |
| 40 | { |
| 41 | return QQuickDesignerCustomObjectData::getResetValue(object, propertyName); |
| 42 | } |
| 43 | |
| 44 | void QQuickDesignerSupportProperties::doResetProperty(QObject *object, QQmlContext *context, const QQuickDesignerSupport::PropertyName &propertyName) |
| 45 | { |
| 46 | QQuickDesignerCustomObjectData::doResetProperty(object, context, propertyName); |
| 47 | } |
| 48 | |
| 49 | bool QQuickDesignerSupportProperties::hasValidResetBinding(QObject *object, const QQuickDesignerSupport::PropertyName &propertyName) |
| 50 | { |
| 51 | return QQuickDesignerCustomObjectData::hasValidResetBinding(object, propertyName); |
| 52 | } |
| 53 | |
| 54 | bool QQuickDesignerSupportProperties::hasBindingForProperty(QObject *object, |
| 55 | QQmlContext *context, |
| 56 | const QQuickDesignerSupport::PropertyName &propertyName, |
| 57 | bool *hasChanged) |
| 58 | { |
| 59 | return QQuickDesignerCustomObjectData::hasBindingForProperty(object, context, propertyName, hasChanged); |
| 60 | } |
| 61 | |
| 62 | void QQuickDesignerSupportProperties::setPropertyBinding(QObject *object, |
| 63 | QQmlContext *context, |
| 64 | const QQuickDesignerSupport::PropertyName &propertyName, |
| 65 | const QString &expression) |
| 66 | { |
| 67 | QQuickDesignerCustomObjectData::setPropertyBinding(object, context, propertyName, expression); |
| 68 | } |
| 69 | |
| 70 | void QQuickDesignerSupportProperties::keepBindingFromGettingDeleted(QObject *object, |
| 71 | QQmlContext *context, |
| 72 | const QQuickDesignerSupport::PropertyName &propertyName) |
| 73 | { |
| 74 | QQuickDesignerCustomObjectData::keepBindingFromGettingDeleted(object, context, propertyName); |
| 75 | } |
| 76 | |
| 77 | bool QQuickDesignerSupportProperties::isPropertyQObject(const QMetaProperty &metaProperty) |
| 78 | { |
| 79 | return metaProperty.metaType().flags().testFlag(flag: QMetaType::PointerToQObject); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | QObject *QQuickDesignerSupportProperties::readQObjectProperty(const QMetaProperty &metaProperty, QObject *object) |
| 84 | { |
| 85 | return QQmlMetaType::toQObject(metaProperty.read(obj: object)); |
| 86 | } |
| 87 | |
| 88 | void QQuickDesignerSupportProperties::getPropertyCache(QObject *object) |
| 89 | { |
| 90 | QQmlMetaType::propertyCache(metaObject: object->metaObject()); |
| 91 | } |
| 92 | |
| 93 | static QQuickDesignerSupport::PropertyNameList propertyNameListForWritableProperties(QObject *object, |
| 94 | const QQuickDesignerSupport::PropertyName &baseName, |
| 95 | QObjectList *inspectedObjects, |
| 96 | int depth = 0) |
| 97 | { |
| 98 | QQuickDesignerSupport::PropertyNameList propertyNameList; |
| 99 | |
| 100 | if (depth > 2) |
| 101 | return propertyNameList; |
| 102 | |
| 103 | if (!inspectedObjects->contains(t: object)) |
| 104 | inspectedObjects->append(t: object); |
| 105 | |
| 106 | const QMetaObject *metaObject = object->metaObject(); |
| 107 | for (int index = 0; index < metaObject->propertyCount(); ++index) { |
| 108 | QMetaProperty metaProperty = metaObject->property(index); |
| 109 | QQmlProperty declarativeProperty(object, QString::fromUtf8(utf8: metaProperty.name())); |
| 110 | if (declarativeProperty.isValid() && !declarativeProperty.isWritable() && declarativeProperty.propertyTypeCategory() == QQmlProperty::Object) { |
| 111 | if (declarativeProperty.name() != QLatin1String("parent" )) { |
| 112 | QObject *childObject = QQmlMetaType::toQObject(declarativeProperty.read()); |
| 113 | if (childObject) |
| 114 | propertyNameList.append(other: propertyNameListForWritableProperties(object: childObject, |
| 115 | baseName: baseName + QQuickDesignerSupport::PropertyName(metaProperty.name()) |
| 116 | + '.', inspectedObjects, |
| 117 | depth: depth + 1)); |
| 118 | } |
| 119 | } else if (QQmlGadgetPtrWrapper *valueType |
| 120 | = QQmlGadgetPtrWrapper::instance(engine: qmlEngine(object), type: metaProperty.metaType())) { |
| 121 | valueType->setValue(metaProperty.read(obj: object)); |
| 122 | propertyNameList.append(other: propertyNameListForWritableProperties(object: valueType, |
| 123 | baseName: baseName + QQuickDesignerSupport::PropertyName(metaProperty.name()) |
| 124 | + '.', inspectedObjects, |
| 125 | depth: depth + 1)); |
| 126 | } |
| 127 | |
| 128 | if (metaProperty.isReadable() && metaProperty.isWritable()) { |
| 129 | addToPropertyNameListIfNotBlackListed(propertyNameList: &propertyNameList, |
| 130 | propertyName: baseName + QQuickDesignerSupport::PropertyName(metaProperty.name())); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return propertyNameList; |
| 135 | } |
| 136 | |
| 137 | QQuickDesignerSupport::PropertyNameList QQuickDesignerSupportProperties::propertyNameListForWritableProperties(QObject *object) |
| 138 | { |
| 139 | QObjectList localObjectList; |
| 140 | return ::propertyNameListForWritableProperties(object, baseName: {}, inspectedObjects: &localObjectList); |
| 141 | } |
| 142 | |
| 143 | bool QQuickDesignerSupportProperties::isPropertyBlackListed(const QQuickDesignerSupport::PropertyName &propertyName) |
| 144 | { |
| 145 | if (propertyName.contains(bv: "." ) && propertyName.contains(bv: "__" )) |
| 146 | return true; |
| 147 | |
| 148 | if (propertyName.count(bv: "." ) > 1) |
| 149 | return true; |
| 150 | |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | QQuickDesignerSupport::PropertyNameList QQuickDesignerSupportProperties::allPropertyNames(QObject *object, |
| 155 | const QQuickDesignerSupport::PropertyName &baseName, |
| 156 | QObjectList *inspectedObjects, |
| 157 | int depth) |
| 158 | { |
| 159 | QQuickDesignerSupport::PropertyNameList propertyNameList; |
| 160 | |
| 161 | QObjectList localObjectList; |
| 162 | |
| 163 | if (inspectedObjects == nullptr) |
| 164 | inspectedObjects = &localObjectList; |
| 165 | |
| 166 | if (depth > 2) |
| 167 | return propertyNameList; |
| 168 | |
| 169 | if (!inspectedObjects->contains(t: object)) |
| 170 | inspectedObjects->append(t: object); |
| 171 | |
| 172 | const QMetaObject *metaObject = object->metaObject(); |
| 173 | |
| 174 | QStringList deferredPropertyNames; |
| 175 | const int namesIndex = metaObject->indexOfClassInfo(name: "DeferredPropertyNames" ); |
| 176 | if (namesIndex != -1) { |
| 177 | QMetaClassInfo classInfo = metaObject->classInfo(index: namesIndex); |
| 178 | deferredPropertyNames = QString::fromUtf8(utf8: classInfo.value()).split(sep: QLatin1Char(',')); |
| 179 | } |
| 180 | |
| 181 | for (int index = 0; index < metaObject->propertyCount(); ++index) { |
| 182 | QMetaProperty metaProperty = metaObject->property(index); |
| 183 | QQmlProperty declarativeProperty(object, QString::fromUtf8(utf8: metaProperty.name())); |
| 184 | if (declarativeProperty.isValid() && declarativeProperty.propertyTypeCategory() == QQmlProperty::Object) { |
| 185 | if (declarativeProperty.name() != QLatin1String("parent" ) |
| 186 | && !deferredPropertyNames.contains(str: declarativeProperty.name())) { |
| 187 | QObject *childObject = QQmlMetaType::toQObject(declarativeProperty.read()); |
| 188 | if (childObject) |
| 189 | propertyNameList.append(other: allPropertyNames(object: childObject, |
| 190 | baseName: baseName |
| 191 | + QQuickDesignerSupport::PropertyName(metaProperty.name()) |
| 192 | + '.', inspectedObjects, |
| 193 | depth: depth + 1)); |
| 194 | } |
| 195 | } else if (QQmlGadgetPtrWrapper *valueType |
| 196 | = QQmlGadgetPtrWrapper::instance(engine: qmlEngine(object), type: metaProperty.metaType())) { |
| 197 | valueType->setValue(metaProperty.read(obj: object)); |
| 198 | propertyNameList.append(t: baseName + QQuickDesignerSupport::PropertyName(metaProperty.name())); |
| 199 | propertyNameList.append(other: allPropertyNames(object: valueType, |
| 200 | baseName: baseName |
| 201 | + QQuickDesignerSupport::PropertyName(metaProperty.name()) |
| 202 | + '.', inspectedObjects, |
| 203 | depth: depth + 1)); |
| 204 | } else { |
| 205 | addToPropertyNameListIfNotBlackListed(propertyNameList: &propertyNameList, |
| 206 | propertyName: baseName + QQuickDesignerSupport::PropertyName(metaProperty.name())); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return propertyNameList; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | QT_END_NAMESPACE |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |