bind types for dataengines in the palsmacore import

This commit is contained in:
Marco Martin 2011-11-01 19:18:41 +01:00
parent 2bb756cfe7
commit 43a069c681
4 changed files with 154 additions and 1 deletions

View File

@ -11,6 +11,7 @@ set(corebindings_SRCS
framesvgitem.cpp framesvgitem.cpp
dialog.cpp dialog.cpp
tooltip.cpp tooltip.cpp
dataenginebindings.cpp
) )
INCLUDE_DIRECTORIES( INCLUDE_DIRECTORIES(

View File

@ -37,6 +37,7 @@
#include "theme_p.h" #include "theme_p.h"
#include "dialog.h" #include "dialog.h"
#include "tooltip.h" #include "tooltip.h"
#include "dataenginebindings_p.h"
void CoreBindingsPlugin::initializeEngine(QDeclarativeEngine *engine, const char *uri) void CoreBindingsPlugin::initializeEngine(QDeclarativeEngine *engine, const char *uri)
{ {
@ -51,12 +52,14 @@ void CoreBindingsPlugin::initializeEngine(QDeclarativeEngine *engine, const char
kdeclarative.setDeclarativeEngine(engine); kdeclarative.setDeclarativeEngine(engine);
kdeclarative.initialize(); kdeclarative.initialize();
QScriptEngine *scriptEngine = kdeclarative.scriptEngine(); QScriptEngine *scriptEngine = kdeclarative.scriptEngine();
kWarning()<<"AAAAA"<<scriptEngine->globalObject().property("i18n").isValid();
//inject the hack only if wasn't injected already //inject the hack only if wasn't injected already
if (!scriptEngine->globalObject().property("i18n").isValid()) { if (!scriptEngine->globalObject().property("i18n").isValid()) {
//binds things like kconfig and icons //binds things like kconfig and icons
kdeclarative.setupBindings(); kdeclarative.setupBindings();
} }
registerDataEngineMetaTypes(scriptEngine);
} }
void CoreBindingsPlugin::registerTypes(const char *uri) void CoreBindingsPlugin::registerTypes(const char *uri)

View File

@ -0,0 +1,66 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "dataenginebindings_p.h"
typedef Plasma::Service *ServicePtr;
QScriptValue qScriptValueFromService(QScriptEngine *engine, const ServicePtr &service)
{
return engine->newQObject(const_cast<Plasma::Service *>(service), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void serviceFromQScriptValue(const QScriptValue &scriptValue, ServicePtr &service)
{
QObject *obj = scriptValue.toQObject();
service = static_cast<Plasma::Service *>(obj);
}
typedef Plasma::DataEngine *DataEnginePtr;
QScriptValue qScriptValueFromDataEngine(QScriptEngine *engine, const DataEnginePtr &dataEngine)
{
return engine->newQObject(const_cast<Plasma::DataEngine *>(dataEngine), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void dataEngineFromQScriptValue(const QScriptValue &scriptValue, DataEnginePtr &dataEngine)
{
QObject *obj = scriptValue.toQObject();
dataEngine = static_cast<Plasma::DataEngine *>(obj);
}
typedef Plasma::ServiceJob *ServiceJobPtr;
QScriptValue qScriptValueFromServiceJob(QScriptEngine *engine, const ServiceJobPtr &serviceJob)
{
return engine->newQObject(const_cast<Plasma::ServiceJob *>(serviceJob), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void serviceJobFromQScriptValue(const QScriptValue &scriptValue, ServiceJobPtr &serviceJob)
{
QObject *obj = scriptValue.toQObject();
serviceJob = static_cast<Plasma::ServiceJob *>(obj);
}
void registerDataEngineMetaTypes(QScriptEngine *engine)
{
qRegisterMetaType<Plasma::DataEngine::Data>("Plasma::DataEngine::Data");
qRegisterMetaType<Plasma::DataEngine::Data>("DataEngine::Data");
qScriptRegisterMapMetaType<Plasma::DataEngine::Data>(engine);
qScriptRegisterMetaType<Plasma::Service *>(engine, qScriptValueFromService, serviceFromQScriptValue);
qScriptRegisterMetaType<Plasma::DataEngine *>(engine, qScriptValueFromDataEngine, dataEngineFromQScriptValue);
qScriptRegisterMetaType<Plasma::ServiceJob *>(engine, qScriptValueFromServiceJob, serviceJobFromQScriptValue);
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef DATAENGINEBIND_H
#define DATAENGINEBIND_H
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#include <kdebug.h>
#include <plasma/dataengine.h>
#include <plasma/service.h>
#include <plasma/servicejob.h>
using namespace Plasma;
Q_DECLARE_METATYPE(DataEngine::Dict)
Q_DECLARE_METATYPE(DataEngine::Data)
template <class M>
QScriptValue qScriptValueFromMap(QScriptEngine *eng, const M &map)
{
//kDebug() << "qScriptValueFromMap called";
QScriptValue obj = eng->newObject();
typename M::const_iterator begin = map.constBegin();
typename M::const_iterator end = map.constEnd();
typename M::const_iterator it;
for (it = begin; it != end; ++it) {
if (it.value().type() == QVariant::Hash) {
obj.setProperty(it.key(), qScriptValueFromMap(eng, it.value().toHash()));
} else if (it.value().type() == QVariant::Map) {
obj.setProperty(it.key(), qScriptValueFromMap(eng, it.value().toMap()));
} else {
obj.setProperty(it.key(), qScriptValueFromValue(eng, it.value()));
}
}
return obj;
}
template <class M>
void qScriptValueToMap(const QScriptValue &value, M &map)
{
//kDebug() << "qScriptValueToMap called";
QScriptValueIterator it(value);
while (it.hasNext()) {
it.next();
map[it.name()] = qscriptvalue_cast<typename M::mapped_type>(it.value());
}
}
template<typename T>
int qScriptRegisterMapMetaType(
QScriptEngine *engine,
const QScriptValue &prototype = QScriptValue()
#ifndef qdoc
, T * /* dummy */ = 0
#endif
)
{
return qScriptRegisterMetaType<T>(engine, qScriptValueFromMap, qScriptValueToMap, prototype);
}
void registerDataEngineMetaTypes(QScriptEngine *engine);
#endif // DATAENGINE_H