1 /*
2   qjsvaluepropertyadaptor.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2015-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include "qjsvaluepropertyadaptor.h"
30 
31 #include <core/propertydata.h>
32 
33 #include <QDebug>
34 #include <QJSValue>
35 
36 using namespace GammaRay;
37 
QJSValuePropertyAdaptor(QObject * parent)38 QJSValuePropertyAdaptor::QJSValuePropertyAdaptor(QObject *parent)
39     : PropertyAdaptor(parent)
40 {
41 }
42 
43 QJSValuePropertyAdaptor::~QJSValuePropertyAdaptor() = default;
44 
count() const45 int QJSValuePropertyAdaptor::count() const
46 {
47     auto jsValue = object().variant().value<QJSValue>();
48     if (jsValue.isArray())
49         return jsValue.toVariant().value<QVariantList>().size();
50     return 0;
51 }
52 
propertyData(int index) const53 PropertyData QJSValuePropertyAdaptor::propertyData(int index) const
54 {
55     PropertyData pd;
56     auto jsValue = object().variant().value<QJSValue>();
57 
58     if (jsValue.isArray()) {
59         auto list = jsValue.toVariant().value<QVariantList>();
60         if (index < 0 || index >= list.size())
61             return pd;
62         pd.setName(QString::number(index));
63         pd.setValue(list.at(index));
64         pd.setClassName(QStringLiteral("QJSValue"));
65     }
66 
67     return pd;
68 }
69 
70 QJSValuePropertyAdaptorFactory *QJSValuePropertyAdaptorFactory::s_instance = nullptr;
71 
create(const ObjectInstance & oi,QObject * parent) const72 PropertyAdaptor *QJSValuePropertyAdaptorFactory::create(const ObjectInstance &oi,
73                                                         QObject *parent) const
74 {
75     if (oi.type() != ObjectInstance::QtVariant)
76         return nullptr;
77 
78     if (!oi.variant().isValid() || !oi.variant().canConvert<QJSValue>())
79         return nullptr;
80 
81     return new QJSValuePropertyAdaptor(parent);
82 }
83 
instance()84 QJSValuePropertyAdaptorFactory *QJSValuePropertyAdaptorFactory::instance()
85 {
86     if (!s_instance)
87         s_instance = new QJSValuePropertyAdaptorFactory;
88     return s_instance;
89 }
90