From 5e6b2bf68b2338ad9424b48193b860a70addd022 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 14 Jan 2013 18:12:50 +0100 Subject: [PATCH] Add QQmlPlatform (exposed as Qt.platform) Change-Id: If34603fd8fbd907bc063b52ea31848eabdb49f61 Reviewed-by: Gabriel de Dietrich Reviewed-by: Alan Alpert --- src/qml/qml/qml.pri | 6 ++-- src/qml/qml/qqmlengine.cpp | 31 +++++++++++++++++ src/qml/qml/qqmlplatform.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++ src/qml/qml/qqmlplatform_p.h | 74 +++++++++++++++++++++++++++++++++++++++ src/qml/qml/v8/qv8engine.cpp | 13 +++++++ src/qml/qml/v8/qv8engine_p.h | 2 ++ 6 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 src/qml/qml/qqmlplatform.cpp create mode 100644 src/qml/qml/qqmlplatform_p.h diff --git a/src/qml/qml/qml.pri b/src/qml/qml/qml.pri index 87fe477..53094db 100644 --- a/src/qml/qml/qml.pri +++ b/src/qml/qml/qml.pri @@ -55,7 +55,8 @@ SOURCES += \ $$PWD/qqmlmemoryprofiler.cpp \ $$PWD/qqmlconnections.cpp \ $$PWD/qqmltimer.cpp \ - $$PWD/qqmlbind.cpp + $$PWD/qqmlbind.cpp \ + $$PWD/qqmlplatform.cpp HEADERS += \ $$PWD/qqmlglobal_p.h \ @@ -131,7 +132,8 @@ HEADERS += \ $$PWD/qqmlmemoryprofiler_p.h \ $$PWD/qqmlconnections_p.h \ $$PWD/qqmltimer_p.h \ - $$PWD/qqmlbind_p.h + $$PWD/qqmlbind_p.h \ + $$PWD/qqmlplatform_p.h include(parser/parser.pri) diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 5657cac..074ea51 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -92,6 +92,7 @@ #include "qqmlbind_p.h" #include "qqmlconnections_p.h" #include "qqmltimer_p.h" +#include "qqmlplatform_p.h" #include #include #include @@ -367,6 +368,36 @@ The following functions are also on the Qt object. */ /*! + \qmlproperty object Qt::platform + \since QtQml 2.1 + + The \c platform object provides info about the underlying platform. + + Its properties are: + + \table + \row + \li \c platform.os + \li + + This read-only property contains the name of the operating system. + + Possible values are: + + \list + \li \c "android" - Android + \li \c "blackberry" - BlackBerry OS + \li \c "ios" - Apple iOS + \li \c "linux" - Linux + \li \c "mac" - Mac OS X + \li \c "unix" - Other Unix-based OS + \li \c "windows" - Windows + \li \c "wince" - Windows CE + \endlist + \endtable +*/ + +/*! \qmlproperty object Qt::application \since QtQuick 1.1 diff --git a/src/qml/qml/qqmlplatform.cpp b/src/qml/qml/qqmlplatform.cpp new file mode 100644 index 0000000..21145be --- /dev/null +++ b/src/qml/qml/qqmlplatform.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qqmlplatform_p.h" + +QT_BEGIN_NAMESPACE + +/* + This object and its properties are documented as part of the Qt object, + in qqmlengine.cpp +*/ + +QQmlPlatform::QQmlPlatform(QObject *parent) + : QObject(parent) +{ +} + +QQmlPlatform::~QQmlPlatform() +{ +} + +QString QQmlPlatform::os() +{ +#if defined(Q_OS_LINUX_ANDROID) + return QLatin1String("android"); +#elif defined(Q_OS_BLACKBERRY) + return QLatin1String("blackberry"); +#elif defined(Q_OS_IOS) + return QLatin1String("ios"); +#elif defined(Q_OS_MAC) + return QLatin1String("mac"); +#elif defined(Q_OS_WINCE) + return QLatin1String("wince"); +#elif defined(Q_OS_WIN) + return QLatin1String("windows"); +#elif defined(Q_OS_LINUX) + return QLatin1String("linux"); +#elif defined(Q_OS_UNIX) + return QLatin1String("unix"); +#else + return QLatin1String("unknown"); +#endif +} + +QT_END_NAMESPACE diff --git a/src/qml/qml/qqmlplatform_p.h b/src/qml/qml/qqmlplatform_p.h new file mode 100644 index 0000000..a70e21f --- /dev/null +++ b/src/qml/qml/qqmlplatform_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQMLPLATFORM_P_H +#define QQMLPLATFORM_P_H + +#include +#include +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +class Q_QML_PRIVATE_EXPORT QQmlPlatform : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString os READ os CONSTANT) + +public: + explicit QQmlPlatform(QObject *parent = 0); + virtual ~QQmlPlatform(); + + static QString os(); + +private: + Q_DISABLE_COPY(QQmlPlatform) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQmlPlatform) + +QT_END_HEADER + +#endif // QQMLPLATFORM_P_H diff --git a/src/qml/qml/v8/qv8engine.cpp b/src/qml/qml/v8/qv8engine.cpp index 2619c1a..15611f3 100644 --- a/src/qml/qml/v8/qv8engine.cpp +++ b/src/qml/qml/v8/qv8engine.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include "qscript_impl_p.h" #include "qv8domerrors_p.h" @@ -125,6 +126,7 @@ QV8Engine::QV8Engine(QJSEngine* qq, ContextOwnership ownership) , m_ownsV8Context(ownership == CreateNewContext) , m_xmlHttpRequestData(0) , m_listModelData(0) + , m_platform(0) , m_application(0) { QML_MEMORY_SCOPE_STRING("QV8Engine::QV8Engine"); @@ -629,6 +631,7 @@ void QV8Engine::initializeGlobal(v8::Handle global) qt->Set(v8::String::New("binding"), V8FUNCTION(binding, this)); if (m_engine) { + qt->SetAccessor(v8::String::New("platform"), getPlatform, 0, v8::External::New(this)); qt->SetAccessor(v8::String::New("application"), getApplication, 0, v8::External::New(this)); #ifndef QT_NO_IM qt->SetAccessor(v8::String::New("inputMethod"), getInputMethod, 0, v8::External::New(this)); @@ -1446,6 +1449,16 @@ int QV8Engine::consoleCountHelper(const QString &file, quint16 line, quint16 col return number; } +v8::Handle QV8Engine::getPlatform(v8::Local, const v8::AccessorInfo &info) +{ + QV8Engine *engine = reinterpret_cast(v8::External::Unwrap(info.Data())); + if (!engine->m_platform) { + // Only allocate a platform object once + engine->m_platform = new QQmlPlatform(engine->m_engine); + } + return engine->newQObject(engine->m_platform); +} + v8::Handle QV8Engine::getApplication(v8::Local, const v8::AccessorInfo &info) { QV8Engine *engine = reinterpret_cast(v8::External::Unwrap(info.Data())); diff --git a/src/qml/qml/v8/qv8engine_p.h b/src/qml/qml/v8/qv8engine_p.h index 43c978c..9c3eb22 100644 --- a/src/qml/qml/v8/qv8engine_p.h +++ b/src/qml/qml/v8/qv8engine_p.h @@ -427,6 +427,7 @@ public: void addRelationshipForGC(QObject *object, v8::Persistent handle); void addRelationshipForGC(QObject *object, QObject *other); + static v8::Handle getPlatform(v8::Local property, const v8::AccessorInfo &info); static v8::Handle getApplication(v8::Local property, const v8::AccessorInfo &info); #ifndef QT_NO_IM static v8::Handle getInputMethod(v8::Local property, const v8::AccessorInfo &info); @@ -480,6 +481,7 @@ protected: QHash m_consoleCount; + QObject *m_platform; QObject *m_application; QVariant toBasicVariant(v8::Handle); -- 2.7.4