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
|
/*
qmlcontextpropertyadaptor.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "qmlcontextpropertyadaptor.h"
#include <core/propertydata.h>
#include <QQmlContext>
#include <private/qqmlcontext_p.h>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <private/qqmlcontextdata_p.h>
#include <private/qv4identifierhashdata_p.h>
#else
#include <private/qv4identifier_p.h>
#endif
#include <QDebug>
using namespace GammaRay;
QmlContextPropertyAdaptor::QmlContextPropertyAdaptor(QObject *parent)
: PropertyAdaptor(parent)
{
}
QmlContextPropertyAdaptor::~QmlContextPropertyAdaptor() = default;
int QmlContextPropertyAdaptor::count() const
{
return ( int )m_contextPropertyNames.size();
}
PropertyData QmlContextPropertyAdaptor::propertyData(int index) const
{
PropertyData pd;
if (!object().isValid())
return pd;
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_contextPropertyNames.size());
auto context = qobject_cast<QQmlContext *>(object().qtObject());
if (!context)
return pd;
pd.setName(m_contextPropertyNames.at(index));
pd.setValue(context->contextProperty(m_contextPropertyNames.at(index)));
pd.setClassName(tr("QML Context Property"));
pd.setAccessFlags(PropertyData::Writable);
return pd;
}
void QmlContextPropertyAdaptor::writeProperty(int index, const QVariant &value)
{
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_contextPropertyNames.size());
const auto &name = m_contextPropertyNames.at(index);
auto context = qobject_cast<QQmlContext *>(object().qtObject());
if (name.isEmpty() || !context)
return;
context->setContextProperty(name, value);
}
void QmlContextPropertyAdaptor::doSetObject(const ObjectInstance &oi)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto context = qobject_cast<QQmlContext *>(oi.qtObject());
Q_ASSERT(context);
auto contextData = QQmlContextData::get(context);
Q_ASSERT(contextData);
const auto &propNames = contextData->propertyNames();
m_contextPropertyNames.clear();
m_contextPropertyNames.reserve(propNames.count());
QV4::IdentifierHashEntry *e = propNames.d->entries;
QV4::IdentifierHashEntry *end = e + propNames.d->alloc;
while (e < end) {
if (e->identifier.isValid())
m_contextPropertyNames.push_back(e->identifier.toQString());
++e;
}
#else
auto context = qobject_cast<QQmlContext *>(oi.qtObject());
Q_ASSERT(context);
auto contextData = QQmlContextData::get(context);
Q_ASSERT(contextData);
auto priv = contextData->asQQmlContextPrivate();
Q_ASSERT(priv);
const int numProps = priv->numPropertyValues();
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
const auto propNames = contextData->propertyNames();
#endif
for (int i = 0; i < numProps; ++i) {
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
const auto prop = propNames.findId(i);
#else
const auto prop = contextData->propertyName(i);
#endif
if (!prop.isEmpty()) {
m_contextPropertyNames.push_back(prop);
}
}
#endif
}
QmlContextPropertyAdaptorFactory *QmlContextPropertyAdaptorFactory::s_instance = nullptr;
PropertyAdaptor *QmlContextPropertyAdaptorFactory::create(const ObjectInstance &oi,
QObject *parent) const
{
if (oi.type() != ObjectInstance::QtObject || !oi.qtObject())
return nullptr;
if (qobject_cast<QQmlContext *>(oi.qtObject()))
return new QmlContextPropertyAdaptor(parent);
return nullptr;
}
QmlContextPropertyAdaptorFactory *QmlContextPropertyAdaptorFactory::instance()
{
if (!s_instance)
s_instance = new QmlContextPropertyAdaptorFactory;
return s_instance;
}
|