Commit db6af3f1 authored by Tobias Hunger's avatar Tobias Hunger

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: default avatarEike Ziller <eike.ziller@digia.com>
parent 9e4cc131
...@@ -17,6 +17,7 @@ SOURCES += mainwindow.cpp \ ...@@ -17,6 +17,7 @@ SOURCES += mainwindow.cpp \
generalsettings.cpp \ generalsettings.cpp \
id.cpp \ id.cpp \
icontext.cpp \ icontext.cpp \
jsexpander.cpp \
messagemanager.cpp \ messagemanager.cpp \
messageoutputwindow.cpp \ messageoutputwindow.cpp \
outputpane.cpp \ outputpane.cpp \
...@@ -109,6 +110,7 @@ HEADERS += mainwindow.h \ ...@@ -109,6 +110,7 @@ HEADERS += mainwindow.h \
fancytabwidget.h \ fancytabwidget.h \
generalsettings.h \ generalsettings.h \
id.h \ id.h \
jsexpander.h \
messagemanager.h \ messagemanager.h \
messageoutputwindow.h \ messageoutputwindow.h \
outputpane.h \ outputpane.h \
......
...@@ -68,6 +68,7 @@ QtcPlugin { ...@@ -68,6 +68,7 @@ QtcPlugin {
"ioutputpane.cpp", "ioutputpane.h", "ioutputpane.cpp", "ioutputpane.h",
"iversioncontrol.cpp", "iversioncontrol.h", "iversioncontrol.cpp", "iversioncontrol.h",
"iwizardfactory.cpp", "iwizardfactory.h", "iwizardfactory.cpp", "iwizardfactory.h",
"jsexpander.cpp", "jsexpander.h",
"mainwindow.cpp", "mainwindow.h", "mainwindow.cpp", "mainwindow.h",
"manhattanstyle.cpp", "manhattanstyle.h", "manhattanstyle.cpp", "manhattanstyle.h",
"messagemanager.cpp", "messagemanager.h", "messagemanager.cpp", "messagemanager.h",
......
/****************************************************************************
**
** 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 <utils/qtcassert.h>
#include <QCoreApplication>
#include <QDebug>
#include <QScriptEngine>
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
/****************************************************************************
**
** 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 <functional>
#include <QFileInfo>
#include <QString>
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
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "icore.h" #include "icore.h"
#include "coreconstants.h" #include "coreconstants.h"
#include "jsexpander.h"
#include "toolsettings.h" #include "toolsettings.h"
#include "mimetypesettings.h" #include "mimetypesettings.h"
#include "fancytabwidget.h" #include "fancytabwidget.h"
...@@ -119,6 +120,7 @@ MainWindow::MainWindow() : ...@@ -119,6 +120,7 @@ MainWindow::MainWindow() :
m_externalToolManager(0), m_externalToolManager(0),
m_progressManager(new ProgressManagerPrivate), m_progressManager(new ProgressManagerPrivate),
m_variableManager(new VariableManager), m_variableManager(new VariableManager),
m_jsExpander(new JsExpander), // must be initialized after the VariableManager
m_vcsManager(new VcsManager), m_vcsManager(new VcsManager),
m_statusBarManager(0), m_statusBarManager(0),
m_modeManager(0), m_modeManager(0),
...@@ -304,6 +306,8 @@ MainWindow::~MainWindow() ...@@ -304,6 +306,8 @@ MainWindow::~MainWindow()
m_helpManager = 0; m_helpManager = 0;
delete m_variableManager; delete m_variableManager;
m_variableManager = 0; m_variableManager = 0;
delete m_jsExpander;
m_jsExpander = 0;
} }
bool MainWindow::init(QString *errorMessage) bool MainWindow::init(QString *errorMessage)
......
...@@ -55,6 +55,7 @@ class DocumentManager; ...@@ -55,6 +55,7 @@ class DocumentManager;
class HelpManager; class HelpManager;
class IDocument; class IDocument;
class IWizardFactory; class IWizardFactory;
class JsExpander;
class MessageManager; class MessageManager;
class MimeDatabase; class MimeDatabase;
class ModeManager; class ModeManager;
...@@ -172,6 +173,7 @@ private: ...@@ -172,6 +173,7 @@ private:
MessageManager *m_messageManager; MessageManager *m_messageManager;
ProgressManagerPrivate *m_progressManager; ProgressManagerPrivate *m_progressManager;
VariableManager *m_variableManager; VariableManager *m_variableManager;
JsExpander *m_jsExpander;
VcsManager *m_vcsManager; VcsManager *m_vcsManager;
StatusBarManager *m_statusBarManager; StatusBarManager *m_statusBarManager;
ModeManager *m_modeManager; ModeManager *m_modeManager;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment