summaryrefslogtreecommitdiffstats
path: root/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.cpp
blob: f38b5c2fc8aced706ce7815c1ab4c463d1345113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************
**
** Copyright (C) 2013 Sandro S. Andrade <sandroandrade@kde.org>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtUml 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 "javascriptconsoleplugin.h"

#include "ui_javascriptconsole.h"

#include <duseinterfaces/iuicontroller.h>
#include <duseinterfaces/iprojectcontroller.h>

#include <QtModeling/QModelingObject>

#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValueIterator>

#include <QtGui/QKeyEvent>

#include <QtWidgets/QListView>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QFileDialog>

#include <QtCore/QTimer>
#include <QtCore/QTextStream>
#include <QtCore/QStringListModel>

template <class T>
QScriptValue qSetToScriptValue(QScriptEngine *engine, const QSet<T *> &elements)
{
    QScriptValue array = engine->newArray();
    foreach (T *element, elements)
        array.property(QString::fromLatin1("push")).call(array, QScriptValueList() << engine->newQObject(element));
    return array;
}

template <class T>
void scriptValueToQSet(const QScriptValue &obj, QSet<T *> &elements)
{
    QScriptValueIterator it(obj);
    while (it.hasNext()) {
        it.next();
        elements.insert(qobject_cast<T *>(it.value().toQObject()));
    }
}

template <class T>
QScriptValue qListToScriptValue(QScriptEngine *engine, const QList<T *> &elements)
{
    QScriptValue array = engine->newArray();
    foreach (T *element, elements)
        array.property(QString::fromLatin1("push")).call(array, QScriptValueList() << engine->newQObject(element));
    return array;
}

template <class T>
void scriptValueToQList(const QScriptValue &obj, QList<T *> &elements)
{
    QScriptValueIterator it(obj);
    while (it.hasNext()) {
        it.next();
        elements.append(qobject_cast<T *>(it.value().toQObject()));
    }
}

namespace DuSE
{

JavaScriptConsolePlugin::JavaScriptConsolePlugin(QObject *parent) :
    IPlugin(parent),
    _javaScriptConsole(new Ui::JavaScriptConsole),
    _codeCompletionView(new QListView),
    _engine(0)
{
}

JavaScriptConsolePlugin::~JavaScriptConsolePlugin()
{
    delete _javaScriptConsole;
    delete _engine;
}

bool JavaScriptConsolePlugin::initialize()
{
    QWidget *javaScriptConsoleWidget = new QWidget;
    _javaScriptConsole->setupUi(javaScriptConsoleWidget);
    ICore::self()->uiController()->addDockWidget(Qt::BottomDockWidgetArea, tr("JavaScript Console"), javaScriptConsoleWidget);

    _javaScriptConsole->txeJavaScript->installEventFilter(this);
    _codeCompletionView->installEventFilter(this);

    _codeCompletionView->setParent(_javaScriptConsole->txeJavaScript);
    _codeCompletionView->hide();

    connect(_javaScriptConsole->tbtJSEvaluate, &QToolButton::clicked, this, &JavaScriptConsolePlugin::evaluate);
    connect(_javaScriptConsole->tbtJSEvaluate, SIGNAL(clicked()), ICore::self()->uiController(), SIGNAL(updateCurrentModelingObject()));

    connect(_javaScriptConsole->tbtSaveScript, &QToolButton::clicked, this, &JavaScriptConsolePlugin::saveScript);
    connect(_javaScriptConsole->tbtOpenScript, &QToolButton::clicked, this, &JavaScriptConsolePlugin::openScript);

    connect(ICore::self()->uiController(), &IUiController::currentModelingObjectChanged, this, &JavaScriptConsolePlugin::setSelfProperty);
    connect(ICore::self()->projectController(), SIGNAL(modelOpened(QList<QModelingObject*>)), this, SLOT(initializeEngine(QList<QModelingObject*>)));
    connect(ICore::self()->projectController(), SIGNAL(modelAboutToBeClosed(QList<QModelingObject*>)), this, SLOT(destroyEngine()));

    return true;
}

void JavaScriptConsolePlugin::setSelfProperty(QModelingObject *modelingObject)
{
    _engine->globalObject().setProperty("self", _engine->newQObject(modelingObject));
}

void JavaScriptConsolePlugin::initializeEngine(QList<QModelingObject *> modelingObjects)
{
    if (modelingObjects.size() == 0)
        return;

    _engine = new QScriptEngine;
    qScriptRegisterMetaType(_engine, qSetToScriptValue<QObject>, scriptValueToQSet<QObject>);
    qScriptRegisterMetaType(_engine, qListToScriptValue<QObject>, scriptValueToQList<QObject>);

    QModelingObject *modelingObject = modelingObjects.at(0);
    _engine->globalObject().setProperty(modelingObject->objectName(), _engine->newQObject(modelingObject));

    QScriptValue array = _engine->newArray();
    foreach (QModelingObject *modelingObject, modelingObjects)
        array.property(QString::fromLatin1("push")).call(array, QScriptValueList() << _engine->newQObject(modelingObject));
    _engine->globalObject().setProperty("input", array);

    _javaScriptConsole->txeJavaScript->setText("self");
    _javaScriptConsole->tbtJSEvaluate->setEnabled(true);
    _javaScriptConsole->tbtSaveScript->setEnabled(true);
    _javaScriptConsole->tbtOpenScript->setEnabled(true);
    QTimer::singleShot(0, this, SLOT(evaluate()));
}

void JavaScriptConsolePlugin::destroyEngine()
{
    delete _engine;
    _engine = 0;
    _javaScriptConsole->txeJavaScript->clear();
    _javaScriptConsole->txeJavaScriptEvaluation->clear();
    _javaScriptConsole->tbtJSEvaluate->setEnabled(false);
    _javaScriptConsole->tbtSaveScript->setEnabled(false);
    _javaScriptConsole->tbtOpenScript->setEnabled(false);
}

bool JavaScriptConsolePlugin::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress && obj == _javaScriptConsole->txeJavaScript) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == 46) {
            QModelingObject *modelingObject = dynamic_cast<QModelingObject *>(_engine->evaluate(_javaScriptConsole->txeJavaScript->toPlainText()).toQObject());
            if (modelingObject) {
                const QMetaObject *metaObject = modelingObject->metaObject();
                int propertyCount = metaObject->propertyCount();
                QStringList propertyList;
                for (int i = 0; i < propertyCount; ++i)
                    propertyList << metaObject->property(i).name();
                _codeCompletionView->setModel(new QStringListModel(propertyList));
                QFont font;
                QFontMetrics fm(font);
                _codeCompletionView->setGeometry(_javaScriptConsole->txeJavaScript->cursorRect().x(), _javaScriptConsole->txeJavaScript->cursorRect().y()+fm.height(), 200, 100);
                _codeCompletionView->show();
                _codeCompletionView->setFocus();
            }
        }
        return QObject::eventFilter(obj, event);
    } else if (event->type() == QEvent::KeyPress && obj == _codeCompletionView) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
         if (keyEvent->key() == 16777220 || keyEvent->key() == 32) { // spacebar or enter
            _javaScriptConsole->txeJavaScript->insertPlainText(_codeCompletionView->model()->data(_codeCompletionView->selectionModel()->selectedIndexes().first()).toString());
            _codeCompletionView->hide();
            _javaScriptConsole->txeJavaScript->setFocus();
            return true;
        }
        else if (keyEvent->key() == 16777235 || keyEvent->key() == 16777237 || keyEvent->key() == 16777239 || keyEvent->key() == 16777238) { // uparrow and downarrow, pageup, pagedown
            return QObject::eventFilter(obj, event);
        }
        else {
            _codeCompletionView->hide();
            _javaScriptConsole->txeJavaScript->setFocus();
            return true;
        }
    }
    // standard event processing
    return QObject::eventFilter(obj, event);
}

void JavaScriptConsolePlugin::evaluate()
{
    _javaScriptConsole->txeJavaScriptEvaluation->setText(_engine->evaluate(_javaScriptConsole->txeJavaScript->toPlainText()).toString());
}

void JavaScriptConsolePlugin::saveScript()
{
    QString fileName = QFileDialog::getSaveFileName(0, tr("Save script"), QDir::currentPath(), "*.js");
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QMessageBox::critical(0, tr("Save script"), tr("Error when saving script as %1").arg(fileName));
            return;
        }
        QTextStream out(&file);
        out << _javaScriptConsole->txeJavaScript->toPlainText();
        file.close();
    }
}

void JavaScriptConsolePlugin::openScript()
{
    QString fileName = QFileDialog::getOpenFileName(0, tr("Open script"), QDir::currentPath(), "*.js");
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QMessageBox::critical(0, tr("Open script"), tr("Error when opening script %1").arg(fileName));
            return;
        }
        QTextStream in(&file);
        _javaScriptConsole->txeJavaScript->setPlainText(in.readAll());
        file.close();
    }
}

}