diff --git a/src/plugins/coreplugin/coreplugin.pro b/src/plugins/coreplugin/coreplugin.pro index 40d2bfaaf34786b3c182dd2c143c950c41fb7b97..662bc50534df71ba8c13bb4c13ca225b45ae78d7 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 e4937d98b089a736a1ee560993231fd3b51b4b11..d3c019bb268ddd4a439979cefcf0cc34cc113dcb 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 0000000000000000000000000000000000000000..0ddbfbeb50d05f775c80b8fd2249d3f4a52c8142 --- /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 0000000000000000000000000000000000000000..7829f5c951853669fad1f703eb7dd514388d9373 --- /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 f16b10e35151989558b037ce4abf81aa6163bcfb..8c6cb9c1f031936c9b39980cae113bed21423ca1 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 f0ba5ac2de97f9463076702473694023ab1b1000..7c901b3fb4101abe0496ed9e2ffa0f63ff35110f 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;