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

View file

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