Skip to content

Commit

Permalink
implement js console history
Browse files Browse the repository at this point in the history
  • Loading branch information
svalaskevicius committed Mar 14, 2012
1 parent 1832167 commit c129dff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
44 changes: 40 additions & 4 deletions ionCore/ionPulse.js
Expand Up @@ -26,6 +26,10 @@ function to_string(input)
if (is_string(input)) { if (is_string(input)) {
return input; return input;
} }

if (undefined === input) {
return "undefined";
}
if (null === input) { if (null === input) {
return "null"; return "null";
} }
Expand All @@ -50,9 +54,12 @@ function alert(text)
} }




function JsConsoleWidget(parent) { function JsConsoleWidget(parent)
{
QWidget.call(this, parent); QWidget.call(this, parent);


this.history = [];
this.historyIdx = null;
this.textEdit = new QTextEdit(this); this.textEdit = new QTextEdit(this);
this.textEdit.readOnly = true; this.textEdit.readOnly = true;
this.htmlPrefix = "<html><head> \ this.htmlPrefix = "<html><head> \
Expand All @@ -71,6 +78,8 @@ function JsConsoleWidget(parent) {
var line = trim(this.lineInput.text); var line = trim(this.lineInput.text);
this.lineInput.text = ""; this.lineInput.text = "";
if (line) { if (line) {
this.history.push(line);
this.historyIdx = null;
// wrap to catch thrown exceptions // wrap to catch thrown exceptions
line = "try {ret = "+line+";console.log(typeof(ret)+ret?ret:\"\");} catch (err) {" line = "try {ret = "+line+";console.log(typeof(ret)+ret?ret:\"\");} catch (err) {"
+ "console.error('An error has occurred: '+err.message);" + "console.error('An error has occurred: '+err.message);"
Expand All @@ -82,8 +91,6 @@ function JsConsoleWidget(parent) {
console.error('An error has occurred: '+err.message); console.error('An error has occurred: '+err.message);
} }
} }
var scrollBar = this.textEdit.verticalScrollBar();
scrollBar.value = scrollBar.maximum;
} }
); );


Expand All @@ -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 = new QWidget();
JsConsoleWidget.prototype.log = function (text) JsConsoleWidget.prototype.log = function (text)
{ {
this.htmlContent += "<div class='line'><span class='log'>&gt; </span>"+escape(text)+"</div>"; 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) JsConsoleWidget.prototype.error = function (text)
{ {
this.htmlContent += "<div class='line'><span class='error'>&gt; </span>"+escape(text)+"</div>"; 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; this.textEdit.html = this.htmlPrefix + this.htmlContent + this.htmlSuffix;
var scrollBar = this.textEdit.verticalScrollBar();
scrollBar.value = scrollBar.maximum;
} }




Expand Down
7 changes: 5 additions & 2 deletions ionCore/jsengine.cpp
Expand Up @@ -93,7 +93,7 @@ QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine)
QScriptValue installAppShortcut(QScriptContext *context, QScriptEngine *engine) QScriptValue installAppShortcut(QScriptContext *context, QScriptEngine *engine)
{ {
return engine->newQObject( 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 QScriptEngine::ScriptOwnership
); );
} }
Expand Down Expand Up @@ -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); qApp->installEventFilter(this);
} }


bool AppShortcut::eventFilter(QObject *obj, QEvent *event) bool AppShortcut::eventFilter(QObject *obj, QEvent *event)
{ {
if (owner && (owner != obj)) {
return false;
}
if (QEvent::KeyPress == event->type()) { if (QEvent::KeyPress == event->type()) {
QKeyEvent *kev = static_cast<QKeyEvent*>(event); QKeyEvent *kev = static_cast<QKeyEvent*>(event);
if (kev->key() == key) { if (kev->key() == key) {
Expand Down
3 changes: 2 additions & 1 deletion ionCore/jsengine.h
Expand Up @@ -29,8 +29,9 @@ class AppShortcut : public QObject {


private: private:
Qt::Key key; Qt::Key key;
QObject *owner;
public: public:
AppShortcut(Qt::Key key); AppShortcut(Qt::Key key, QObject *owner);


protected: protected:
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
Expand Down

0 comments on commit c129dff

Please sign in to comment.