2 ******************************************************************************
4 * @file interface_wrap_helpers.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef INTERFACE_WRAP_HELPERS_H
30 #define INTERFACE_WRAP_HELPERS_H
32 #include <QtScript/QScriptEngine>
34 namespace SharedTools
{
35 // Convert a QObjectInterface to Scriptvalue
36 // To be registered as a magic creation function with qScriptRegisterMetaType().
37 // (see registerQObjectInterface)
39 template <class QObjectInterface
>
40 static QScriptValue
qObjectInterfaceToScriptValue(QScriptEngine
*engine
, QObjectInterface
*const &qoif
)
43 return QScriptValue(engine
, QScriptValue::NullValue
);
46 QObject
*qObject
= const_cast<QObjectInterface
*>(qoif
);
48 const QScriptEngine::QObjectWrapOptions wrapOptions
=
49 QScriptEngine::ExcludeChildObjects
| QScriptEngine::ExcludeSuperClassMethods
| QScriptEngine::ExcludeSuperClassProperties
;
50 return engine
->newQObject(qObject
, QScriptEngine::QtOwnership
, wrapOptions
);
53 // Convert Scriptvalue back to QObjectInterface
54 // To be registered as a magic conversion function with qScriptRegisterMetaType().
55 // (see registerQObjectInterface)
57 template <class QObjectInterface
>
58 static void scriptValueToQObjectInterface(const QScriptValue
&sv
, QObjectInterface
* &p
)
60 QObject
*qObject
= sv
.toQObject();
62 p
= qobject_cast
<QObjectInterface
*>(qObject
);
65 // Magically register a Workbench interface derived from
66 // ExtensionSystem::QObjectInterface class with the engine.
67 // To avoid lifecycle issues, the script value is created on the QObject returned
68 // by ExtensionSystem::QObjectInterface::qObject() and given the specified
69 // prototype. By convention, ExtensionSystem::QObjectInterface::qObject() returns an
70 // QObject that implements the interface, so it can be casted to it.
72 template <class QObjectInterface
, class Prototype
>
73 static void registerQObjectInterface(QScriptEngine
*engine
)
75 Prototype
*protoType
= new Prototype(engine
);
76 const QScriptValue scriptProtoType
= engine
->newQObject(protoType
);
78 const int metaTypeId
= qScriptRegisterMetaType
<QObjectInterface
*>(
80 qObjectInterfaceToScriptValue
<QObjectInterface
>,
81 scriptValueToQObjectInterface
<QObjectInterface
>,
86 } // namespace SharedTools
88 #endif // INTERFACE_WRAP_HELPERS_H