JSConsole:

executeCommand works async now
This commit is contained in:
Thijs Wenker 2015-10-14 22:21:51 +02:00
parent 9d2ae661e1
commit 7df5ba01e6
2 changed files with 20 additions and 6 deletions

View file

@ -9,9 +9,11 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QFuture>
#include <QKeyEvent>
#include <QLabel>
#include <QScrollBar>
#include <QtConcurrent/QtConcurrentRun>
#include <PathUtils.h>
@ -55,6 +57,8 @@ JSConsole::JSConsole(QWidget* parent, ScriptEngine* scriptEngine) :
setScriptEngine(scriptEngine);
resizeTextInput();
connect(&_executeWatcher, SIGNAL(finished()), this, SLOT(commandFinished()));
}
JSConsole::~JSConsole() {
@ -96,9 +100,19 @@ void JSConsole::executeCommand(const QString& command) {
appendMessage(">", "<span style='" + COMMAND_STYLE + "'>" + command.toHtmlEscaped() + "</span>");
QFuture<QScriptValue> future = QtConcurrent::run(this, &JSConsole::executeCommandInWatcher, command);
_executeWatcher.setFuture(future);
}
QScriptValue JSConsole::executeCommandInWatcher(const QString& command) {
QScriptValue result;
QMetaObject::invokeMethod(_scriptEngine, "evaluate", Qt::ConnectionType::BlockingQueuedConnection,
Q_RETURN_ARG(QScriptValue, result), Q_ARG(const QString&, command));
return result;
}
void JSConsole::commandFinished() {
QScriptValue result = _executeWatcher.result();
_ui->promptTextEdit->setDisabled(false);
@ -106,11 +120,11 @@ void JSConsole::executeCommand(const QString& command) {
if (window()->isActiveWindow()) {
_ui->promptTextEdit->setFocus();
}
bool error = (_scriptEngine->hasUncaughtException() || result.isError());
QString gutter = error ? GUTTER_ERROR : GUTTER_PREVIOUS_COMMAND;
QString resultColor = error ? RESULT_ERROR_STYLE : RESULT_SUCCESS_STYLE;
QString resultStr = "<span style='" + resultColor + "'>" + result.toString().toHtmlEscaped() + "</span>";
QString resultStr = "<span style='" + resultColor + "'>" + result.toString().toHtmlEscaped() + "</span>";
appendMessage(gutter, resultStr);
resetCurrentCommandHistory();

View file

@ -14,6 +14,7 @@
#include <QDialog>
#include <QEvent>
#include <QFutureWatcher>
#include <QObject>
#include <QWidget>
@ -37,10 +38,6 @@ public:
public slots:
void executeCommand(const QString& command);
signals:
void commandExecuting(const QString& command);
void commandFinished(const QString& result);
protected:
void setAndSelectCommand(const QString& command);
virtual bool eventFilter(QObject* sender, QEvent* event);
@ -52,13 +49,16 @@ protected slots:
void resizeTextInput();
void handlePrint(const QString& message);
void handleError(const QString& message);
void commandFinished();
private:
void appendMessage(const QString& gutter, const QString& message);
void setToNextCommandInHistory();
void setToPreviousCommandInHistory();
void resetCurrentCommandHistory();
QScriptValue executeCommandInWatcher(const QString& command);
QFutureWatcher<QScriptValue> _executeWatcher;
Ui::Console* _ui;
int _currentCommandInHistory;
QList<QString> _commandHistory;