From db6af3f152495c613f66e2fad41c305a1d23c55d Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 12 Sep 2014 14:25:51 +0200 Subject: [PATCH] VariableManager: Add support for javascript varibales Add JsExpander that can be used to register additional functionality in its qscriptengine and to evaluate expressions. Register that with the VariableManager, using the "JS" prefix. Change-Id: I7d8f1ddc3484104f3943685dbac09e786d60212e Reviewed-by: Eike Ziller --- src/plugins/coreplugin/coreplugin.pro | 2 + src/plugins/coreplugin/coreplugin.qbs | 1 + src/plugins/coreplugin/jsexpander.cpp | 109 ++++++++++++++++++++++++++ src/plugins/coreplugin/jsexpander.h | 60 ++++++++++++++ src/plugins/coreplugin/mainwindow.cpp | 4 + src/plugins/coreplugin/mainwindow.h | 2 + 6 files changed, 178 insertions(+) create mode 100644 src/plugins/coreplugin/jsexpander.cpp create mode 100644 src/plugins/coreplugin/jsexpander.h diff --git a/src/plugins/coreplugin/coreplugin.pro b/src/plugins/coreplugin/coreplugin.pro index 40d2bfaaf34..662bc50534d 100644 --- a/src/plugins/coreplugin/coreplugin.pro +++ b/src/plugins/coreplugin/coreplugin.pro @@ -17,6 +17,7 @@ SOURCES += mainwindow.cpp \ generalsettings.cpp \ id.cpp \ icontext.cpp \ + jsexpander.cpp \ messagemanager.cpp \ messageoutputwindow.cpp \ outputpane.cpp \ @@ -109,6 +110,7 @@ HEADERS += mainwindow.h \ fancytabwidget.h \ generalsettings.h \ id.h \ + jsexpander.h \ messagemanager.h \ messageoutputwindow.h \ outputpane.h \ diff --git a/src/plugins/coreplugin/coreplugin.qbs b/src/plugins/coreplugin/coreplugin.qbs index e4937d98b08..d3c019bb268 100644 --- a/src/plugins/coreplugin/coreplugin.qbs +++ b/src/plugins/coreplugin/coreplugin.qbs @@ -68,6 +68,7 @@ QtcPlugin { "ioutputpane.cpp", "ioutputpane.h", "iversioncontrol.cpp", "iversioncontrol.h", "iwizardfactory.cpp", "iwizardfactory.h", + "jsexpander.cpp", "jsexpander.h", "mainwindow.cpp", "mainwindow.h", "manhattanstyle.cpp", "manhattanstyle.h", "messagemanager.cpp", "messagemanager.h", diff --git a/src/plugins/coreplugin/jsexpander.cpp b/src/plugins/coreplugin/jsexpander.cpp new file mode 100644 index 00000000000..0ddbfbeb50d --- /dev/null +++ b/src/plugins/coreplugin/jsexpander.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "jsexpander.h" + +#include "variablemanager.h" + +#include + +#include +#include +#include + +namespace Core { + +namespace Internal { + +class JsExpanderPrivate { +public: + QScriptEngine m_engine; +}; + +} // namespace Internal + +static Internal::JsExpanderPrivate *d; + +void JsExpander::registerQObjectForJs(const QString &name, QObject *obj) +{ + QScriptValue jsObj = d->m_engine.newQObject(obj, QScriptEngine::QtOwnership); + d->m_engine.globalObject().setProperty(name, jsObj); +} + +QString JsExpander::evaluate(const QString &expression, QString *errorMessage) +{ + d->m_engine.clearExceptions(); + QScriptValue value = d->m_engine.evaluate(expression); + if (d->m_engine.hasUncaughtException()) { + const QString msg = QCoreApplication::translate("Core::JsExpander", "Error in \"%1\": %2") + .arg(expression, d->m_engine.uncaughtException().toString()); + if (errorMessage) + *errorMessage = msg; + return QString(); + } + // Try to convert to bool, be that an int or whatever. + if (value.isBool()) + return value.toBool() ? QStringLiteral("true") : QStringLiteral("false"); + if (value.isNumber()) + return QString::number(value.toNumber()); + if (value.isString()) + return value.toString(); + QString msg = QCoreApplication::translate("Core::JsExpander", + "Cannot convert result of \"%1\" to string.").arg(expression); + if (errorMessage) + *errorMessage = msg; + return QString(); +} + +JsExpander::JsExpander() +{ + d = new Internal::JsExpanderPrivate; + VariableManager::registerPrefix("JS", + QCoreApplication::translate("Core::JsExpander", + "Evaluate simple Javascript statements.\n" + "The statements may not contain '{' nor '}' characters."), + [](QString in) -> QString { + QString errorMessage; + QString result = JsExpander::evaluate(in, &errorMessage); + if (!errorMessage.isEmpty()) { + qWarning() << errorMessage; + return errorMessage; + } else { + return result; + } + }); +} + +JsExpander::~JsExpander() +{ + delete d; + d = 0; +} + +} // namespace Core diff --git a/src/plugins/coreplugin/jsexpander.h b/src/plugins/coreplugin/jsexpander.h new file mode 100644 index 00000000000..7829f5c9518 --- /dev/null +++ b/src/plugins/coreplugin/jsexpander.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#ifndef JSEXPANDER_H +#define JSEXPANDER_H + +#include "core_global.h" + +#include + +#include +#include + +namespace Core { + +namespace Internal { class MainWindow; } + +class CORE_EXPORT JsExpander +{ +public: + static void registerQObjectForJs(const QString &name, QObject *obj); + + static QString evaluate(const QString &expression, QString *errorMessage = 0); + +private: + JsExpander(); + ~JsExpander(); + + friend class Core::Internal::MainWindow; +}; + +} // namespace Core + +#endif // JSEXPANDER_H diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index f16b10e3515..8c6cb9c1f03 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -30,6 +30,7 @@ #include "mainwindow.h" #include "icore.h" #include "coreconstants.h" +#include "jsexpander.h" #include "toolsettings.h" #include "mimetypesettings.h" #include "fancytabwidget.h" @@ -119,6 +120,7 @@ MainWindow::MainWindow() : m_externalToolManager(0), m_progressManager(new ProgressManagerPrivate), m_variableManager(new VariableManager), + m_jsExpander(new JsExpander), // must be initialized after the VariableManager m_vcsManager(new VcsManager), m_statusBarManager(0), m_modeManager(0), @@ -304,6 +306,8 @@ MainWindow::~MainWindow() m_helpManager = 0; delete m_variableManager; m_variableManager = 0; + delete m_jsExpander; + m_jsExpander = 0; } bool MainWindow::init(QString *errorMessage) diff --git a/src/plugins/coreplugin/mainwindow.h b/src/plugins/coreplugin/mainwindow.h index f0ba5ac2de9..7c901b3fb41 100644 --- a/src/plugins/coreplugin/mainwindow.h +++ b/src/plugins/coreplugin/mainwindow.h @@ -55,6 +55,7 @@ class DocumentManager; class HelpManager; class IDocument; class IWizardFactory; +class JsExpander; class MessageManager; class MimeDatabase; class ModeManager; @@ -172,6 +173,7 @@ private: MessageManager *m_messageManager; ProgressManagerPrivate *m_progressManager; VariableManager *m_variableManager; + JsExpander *m_jsExpander; VcsManager *m_vcsManager; StatusBarManager *m_statusBarManager; ModeManager *m_modeManager;