Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions qt_backend/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

Steps to build:

1: Install Qt development packages (built with scripting support). E.g. on Ubuntu 11.04:
sudo apt-get install qt4-dev-tools

2: Generate the makefile from the .pro file:
qmake -o Makefile workstation_js.pro

3: Build with make:
make

4: Start the resulting application:
./workstation_js
28 changes: 28 additions & 0 deletions qt_backend/Screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Screen.h"

#include <QLineEdit>
#include <QLabel>
#include <QPushButton>

Screen::Screen(QWidget* parent, const QString& title)
: QWidget(parent)
{
}

QWidget* Screen::addLabel()
{
QLabel* label = new QLabel("", this);
return label;
}

QWidget* Screen::addTextEdit()
{
QLineEdit* lineEdit = new QLineEdit("", this);
return lineEdit;
}

QWidget* Screen::addButton()
{
QPushButton* pushButton = new QPushButton("", this);
return pushButton;
}
20 changes: 20 additions & 0 deletions qt_backend/Screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SCREEN_H
#define SCREEN_H

#include <QWidget>

class Screen : public QWidget
{
Q_OBJECT

public:
Screen(QWidget* parent, const QString& title);
~Screen() {};

QWidget* addLabel();
QWidget* addTextEdit();
QWidget* addButton();
};

#endif

37 changes: 37 additions & 0 deletions qt_backend/ScreenPrototype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "ScreenPrototype.h"
#include "Screen.h"

#include <QtScript/QScriptValue>
#include <QtScript/QScriptEngine>

Q_DECLARE_METATYPE(Screen*)

ScreenPrototype::ScreenPrototype(QObject *parent)
: QObject(parent)
{
}

ScreenPrototype::~ScreenPrototype()
{
}

QScriptValue ScreenPrototype::addWidget(const QString& type)
{
Screen *item = qscriptvalue_cast<Screen*>(thisObject());
QWidget* widget = 0;
if (type == QString("label")) {
widget = item->addLabel();
}
else if (type == QString("textbox")) {
widget = item->addTextEdit();
}
else if (type == QString("button")) {
widget = item->addButton();
}
else {
qDebug("Unknown widget: %s", type.toStdString().c_str());
return QScriptValue();
}

return engine()->toScriptValue(widget);
}
20 changes: 20 additions & 0 deletions qt_backend/ScreenPrototype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SCREENPROTOTYPE_H
#define SCREENPROTOTYPE_H

#include <QtCore/QObject>
#include <QtScript/QScriptable>
#include <QtScript/QScriptValue>

class ScreenPrototype : public QObject, public QScriptable
{
Q_OBJECT
public:
ScreenPrototype(QObject *parent = 0);
~ScreenPrototype();

public slots:
QScriptValue addWidget(const QString& type);

};

#endif
32 changes: 32 additions & 0 deletions qt_backend/Workstation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "Workstation.h"
#include "Screen.h"

#include <cassert>

Workstation::Workstation(QWidget* parent)
: QWidget(parent)
{
}

void Workstation::addScreen(const QString& title)
{
screens.push_back(new Screen(this, title));
}

Screen* Workstation::getScreen(unsigned int index)
{
assert(index < getNScreens() && "Invalid index");
return screens[index];
}

unsigned int Workstation::getNScreens() const
{
return screens.size();
}

void Workstation::run()
{
// Resize to show the contents
if (getNScreens() > 0u)
resize(300, 300);
}
27 changes: 27 additions & 0 deletions qt_backend/Workstation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef WORKSTATION_H
#define WORKSTATION_H

#include <QWidget>
#include <QVector>

class Screen;

class Workstation : public QWidget
{
Q_OBJECT
public:
Workstation(QWidget* parent = 0);
~Workstation() {};

void addScreen(const QString& title);
Screen* getScreen(unsigned int index);
unsigned int getNScreens() const;
void run();

private:
QVector<Screen*> screens;

};

#endif

48 changes: 48 additions & 0 deletions qt_backend/WorkstationPrototype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "WorkstationPrototype.h"
#include "Workstation.h"

#include <QtScript/QScriptValue>
#include <QtScript/QScriptEngine>

Q_DECLARE_METATYPE(Workstation*)
Q_DECLARE_METATYPE(Screen*)

WorkstationPrototype::WorkstationPrototype(QObject *parent)
: QObject(parent)
{
}

WorkstationPrototype::~WorkstationPrototype()
{
}

Workstation* WorkstationPrototype::getWorkstation() const
{
return qscriptvalue_cast<Workstation*>(thisObject());
}

void WorkstationPrototype::addScreen(const QString &title)
{
Workstation* workstation = getWorkstation();
workstation->addScreen(title);
}


unsigned int WorkstationPrototype::numberOfScreens() const
{
Workstation* workstation = getWorkstation();
return workstation->getNScreens();
}

QScriptValue WorkstationPrototype::lastScreen()
{
Workstation* workstation = getWorkstation();
unsigned int nScreens = workstation->getNScreens();
return engine()->toScriptValue(workstation->getScreen(nScreens - 1));
}

void WorkstationPrototype::run()
{
Workstation* workstation = getWorkstation();
workstation->run();
}
28 changes: 28 additions & 0 deletions qt_backend/WorkstationPrototype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef WORKSTATIONPROTOTYPE_H
#define WORKSTATIONPROTOTYPE_H

#include <QtCore/QObject>
#include <QtCore/QVariantMap>
#include <QtScript/QScriptable>
#include <QtScript/QScriptValue>

class Workstation;

class WorkstationPrototype : public QObject, public QScriptable
{
Q_OBJECT
public:
WorkstationPrototype(QObject *parent = 0);
~WorkstationPrototype();

public slots:
void addScreen(const QString& text);
unsigned int numberOfScreens() const;
QScriptValue lastScreen();
void run();

private:
Workstation* getWorkstation() const;
};

#endif
88 changes: 88 additions & 0 deletions qt_backend/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <QtGui>
#include <QtScript>

#include "Workstation.h"
#include "WorkstationPrototype.h"
#include "Screen.h"
#include "ScreenPrototype.h"

Q_DECLARE_METATYPE(Workstation*)
Q_DECLARE_METATYPE(Screen*)

// Constructor for QPoint (used to move the widget)
QScriptValue QPoint_ctor(QScriptContext *context, QScriptEngine *engine)
{
int x = context->argument(0).toInt32();
int y = context->argument(1).toInt32();
return engine->toScriptValue(QPoint(x, y));
}

QScriptValue alert(QScriptContext* context, QScriptEngine*)
{
QScriptValue arg = context->argument(0);
if (!arg.isString()) {
return context->throwError(QScriptContext::TypeError,
"alert(): expected string argument");
}

// Get the top level widget to use as parent to center the message box.
QWidget* widget = 0;
QWidgetList widgets = QApplication::topLevelWidgets();
if (widgets.size() == 1)
widget = widgets[0];

QString message = arg.toString();
QMessageBox::information(widget, "Alert", message);
return QScriptValue();
}


int main(int argc, char **argv)
{
QApplication application(argc, argv);
QScriptEngine engine;

// Add the two prototypes which we need to access from javascript:
// workstation and screen
WorkstationPrototype workstationProto;
engine.setDefaultPrototype(qMetaTypeId<Workstation*>(),
engine.newQObject(&workstationProto));

ScreenPrototype screenProto;
engine.setDefaultPrototype(qMetaTypeId<Screen*>(),
engine.newQObject(&screenProto));

// Make the constructor for QPoint accessible from the script.
// See http://doc.trolltech.com/4.5/qtscript.html#implementing-constructors-for-value-based-types
engine.globalObject().setProperty("QPoint", engine.newFunction(QPoint_ctor));

engine.globalObject().setProperty("alert", engine.newFunction(alert));

// Add a workstation object to the script engine's global object
Workstation workstation;
engine.globalObject().setProperty("workstationQt",
engine.newQObject(&workstation));


QStringList files;
files << "../lib/workstation.js"
<< "./workstation_qt.js"
<< "./playground.js";

foreach (const QString& fileName, files) {
qDebug() << "Reading javascript from: " << fileName;
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QScriptValue result = engine.evaluate(file.readAll());
file.close();
if (engine.hasUncaughtException()) {
int lineNo = engine.uncaughtExceptionLineNumber();
qWarning() << "Error in file: " << fileName
<< " Line" << lineNo << ":" << result.toString();
}
}

workstation.run();
workstation.show();
return application.exec();
}
27 changes: 27 additions & 0 deletions qt_backend/playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function run() {
app("hello world", function() {
screen("screen 123", function() {
label({ text: "Username", style: { top: 10, left: 20 } });
textbox({ id: "txtUsername", style: { top: 8, left: 100 } });

label({ text: "Password", style: { top: 40, left: 20 } });
textbox({ id: "txtPassword", style: { top: 38, left: 100 } });

button({
text: "Login",
style: { top: 70, left: 178 },
onclick: function() {
alert("hello world");
} });
button({
text: "Cancel",
style: { top: 70, left: 120 },
onclick: function() {
that.txtUsername.runtime.text("");
that.txtPassword.runtime.text("");
} });
});
});
};

run();
3 changes: 3 additions & 0 deletions qt_backend/workstation_js.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
QT += script
SOURCES += main.cpp Workstation.cpp Screen.cpp WorkstationPrototype.cpp ScreenPrototype.cpp
HEADERS += Workstation.h Screen.h WorkstationPrototype.h ScreenPrototype.h
Loading