Permalink
Browse files

implement js console history

  • Loading branch information...
1 parent 1832167 commit c129dffa2d8c2017e7079f4eb677094e93ed69ae @svalaskevicius committed Mar 14, 2012
Showing with 47 additions and 7 deletions.
  1. +40 −4 ionCore/ionPulse.js
  2. +5 −2 ionCore/jsengine.cpp
  3. +2 −1 ionCore/jsengine.h
View
@@ -26,6 +26,10 @@ function to_string(input)
if (is_string(input)) {
return input;
}
+
+ if (undefined === input) {
+ return "undefined";
+ }
if (null === input) {
return "null";
}
@@ -50,9 +54,12 @@ function alert(text)
}
-function JsConsoleWidget(parent) {
+function JsConsoleWidget(parent)
+{
QWidget.call(this, parent);
+ this.history = [];
+ this.historyIdx = null;
this.textEdit = new QTextEdit(this);
this.textEdit.readOnly = true;
this.htmlPrefix = "<html><head> \
@@ -71,6 +78,8 @@ function JsConsoleWidget(parent) {
var line = trim(this.lineInput.text);
this.lineInput.text = "";
if (line) {
+ this.history.push(line);
+ this.historyIdx = null;
// wrap to catch thrown exceptions
line = "try {ret = "+line+";console.log(typeof(ret)+ret?ret:\"\");} catch (err) {"
+ "console.error('An error has occurred: '+err.message);"
@@ -82,8 +91,6 @@ function JsConsoleWidget(parent) {
console.error('An error has occurred: '+err.message);
}
}
- var scrollBar = this.textEdit.verticalScrollBar();
- scrollBar.value = scrollBar.maximum;
}
);
@@ -105,19 +112,48 @@ function JsConsoleWidget(parent) {
}
}
);
+ qs.system.installAppShortcut(Qt.Key_Up, this.lineInput).callback.connect(
+ this,
+ function() {
+ if (null === this.historyIdx) {
+ this.historyIdx = this.history.length;
+ }
+ if (this.historyIdx > 0) {
+ this.lineInput.text = this.history[--this.historyIdx];
+ }
+ }
+ );
+ qs.system.installAppShortcut(Qt.Key_Down, this.lineInput).callback.connect(
+ this,
+ function() {
+ if (null === this.historyIdx) {
+ return;
+ }
+ if (this.historyIdx < this.history.length) {
+ this.lineInput.text = this.history[++this.historyIdx];
+ }
+ }
+ );
+
}
JsConsoleWidget.prototype = new QWidget();
JsConsoleWidget.prototype.log = function (text)
{
this.htmlContent += "<div class='line'><span class='log'>&gt; </span>"+escape(text)+"</div>";
- this.textEdit.html = this.htmlPrefix + this.htmlContent + this.htmlSuffix;
+ this.updateHtml();
}
JsConsoleWidget.prototype.error = function (text)
{
this.htmlContent += "<div class='line'><span class='error'>&gt; </span>"+escape(text)+"</div>";
+ this.updateHtml();
+}
+JsConsoleWidget.prototype.updateHtml = function()
+{
this.textEdit.html = this.htmlPrefix + this.htmlContent + this.htmlSuffix;
+ var scrollBar = this.textEdit.verticalScrollBar();
+ scrollBar.value = scrollBar.maximum;
}
@@ -93,7 +93,7 @@ QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine)
QScriptValue installAppShortcut(QScriptContext *context, QScriptEngine *engine)
{
return engine->newQObject(
- new AppShortcut((Qt::Key)context->argument(0).toInt32()),
+ new AppShortcut((Qt::Key)context->argument(0).toInt32(), (context->argumentCount()==2)?context->argument(1).toQObject():NULL),
QScriptEngine::ScriptOwnership
);
}
@@ -152,13 +152,16 @@ void JsEngine::initialiseJsFramework()
}
-AppShortcut::AppShortcut(Qt::Key key) : key(key)
+AppShortcut::AppShortcut(Qt::Key key, QObject *owner) : key(key), owner(owner)
{
qApp->installEventFilter(this);
}
bool AppShortcut::eventFilter(QObject *obj, QEvent *event)
{
+ if (owner && (owner != obj)) {
+ return false;
+ }
if (QEvent::KeyPress == event->type()) {
QKeyEvent *kev = static_cast<QKeyEvent*>(event);
if (kev->key() == key) {
View
@@ -29,8 +29,9 @@ class AppShortcut : public QObject {
private:
Qt::Key key;
+ QObject *owner;
public:
- AppShortcut(Qt::Key key);
+ AppShortcut(Qt::Key key, QObject *owner);
protected:
bool eventFilter(QObject *obj, QEvent *event);

0 comments on commit c129dff

Please sign in to comment.