Difference between revisions of "Development/Tutorials/Kross/Call Functions in Kross"

Jump to: navigation, search
(New Kross Tutorial)
 
Line 1: Line 1:
This tutorial shows how to deal with functions provided by a script. It builds upon the [[Development/Tutorials/Kross/Hello World|Kross Hello World]] tutorial and extends that tutorial where needed.
+
This tutorial shows how to deal with functions provided by a script. It builds upon the [[Development/Tutorials/Kross/Hello World|Kross Hello World]] tutorial.
  
 
== The source files ==
 
== The source files ==
  
=== CMakeList.txt ===
+
This tutorial is based on the [[Development/Tutorials/Kross/Hello_World|Hello World]] tutorial and extends the codebase we wrote there with new functionality.
  
 +
=== CMakeLists.txt ===
 +
 +
We are using cmake to build our small sample project. The CMakeLists.txt file looks like;
 +
 +
<code>
 
project (krosshello)
 
project (krosshello)
 
find_package(KDE4 REQUIRED)
 
find_package(KDE4 REQUIRED)
Line 11: Line 16:
 
kde4_add_executable(krosshello ${krosshello_SRCS})
 
kde4_add_executable(krosshello ${krosshello_SRCS})
 
target_link_libraries(krosshello ${KDE4_KDEUI_LIBS} ${KDE4_KROSSUI_LIBS})
 
target_link_libraries(krosshello ${KDE4_KDEUI_LIBS} ${KDE4_KROSSUI_LIBS})
 +
</code>
  
 
=== main.cpp ===
 
=== main.cpp ===
 +
 +
The main.cpp does create the sample application and shows the mainwindow instance.
  
 
<code cpp>
 
<code cpp>
Line 35: Line 43:
 
"http://kross.dipe.org",
 
"http://kross.dipe.org",
 
"submit@bugs.kde.org");
 
"submit@bugs.kde.org");
 
 
// Access to the command-line arguments.
 
// Access to the command-line arguments.
 
KCmdLineArgs::init( argc, argv, &aboutData );
 
KCmdLineArgs::init( argc, argv, &aboutData );
 
// Initialize the application.
 
// Initialize the application.
 
KApplication app;
 
KApplication app;
 
 
// Create and show the main window.
 
// Create and show the main window.
 
MainWindow* window = new MainWindow();
 
MainWindow* window = new MainWindow();
 
window->show();
 
window->show();
 
 
// Finally execute the application.
 
// Finally execute the application.
 
return app.exec();
 
return app.exec();
Line 59: Line 64:
 
#include <QLabel>
 
#include <QLabel>
 
#include <QLineEdit>
 
#include <QLineEdit>
 
 
#include <kross/core/action.h>
 
#include <kross/core/action.h>
  
Line 67: Line 71:
 
Q_OBJECT
 
Q_OBJECT
 
public:
 
public:
// The constructor.
 
 
MainWindow(QWidget *parent=0);
 
MainWindow(QWidget *parent=0);
private Q_SLOTS:
+
private Q_SLOTS:
// This slot is called when the item in the combobox is changed.
+
// This slot is called when the item in the combobox is changed.
void interpreterActivated(const QString &);
+
void interpreterActivated(const QString &);
 
private:
 
private:
 
QLineEdit* txtInputString;
 
QLineEdit* txtInputString;
 
QLabel* lblMessage;
 
QLabel* lblMessage;
 
QComboBox* cmbInterpreters;
 
QComboBox* cmbInterpreters;
// We now have the action as class-member.
 
 
Kross::Action* action;
 
Kross::Action* action;
 
};
 
};
Line 86: Line 88:
  
 
<code cpp>
 
<code cpp>
#include "mw.h"
+
#include "mainwindow.h"
 
+
 
#include <QVBoxLayout>
 
#include <QVBoxLayout>
 
#include <QDebug>
 
#include <QDebug>
 
 
#include <kross/core/manager.h>
 
#include <kross/core/manager.h>
 
#include <kross/core/action.h>
 
#include <kross/core/action.h>
Line 114: Line 114:
 
setLayout(vLayout);
 
setLayout(vLayout);
  
// This time we create the Kross::Action already within the
+
// Create the Kross::Action instance and publish some
// constructor and add the objects that should be accessible
+
// QObject instances.
// from within scripting code.
+
 
action = new Kross::Action(this, "MyScript");
 
action = new Kross::Action(this, "MyScript");
 
+
action->addObject(txtInputString, "MyInputString");
    action->addObject(txtInputString, "MyInputString");
+
 
action->addObject(cmbInterpreters, "MyInterpreter");
 
action->addObject(cmbInterpreters, "MyInterpreter");
 
action->addObject(lblMessage, "MyLabel");
 
action->addObject(lblMessage, "MyLabel");
Line 127: Line 125:
 
void MainWindow::interpreterActivated(const QString &strSelectedInterpreter)
 
void MainWindow::interpreterActivated(const QString &strSelectedInterpreter)
 
{
 
{
if(strSelectedInterpreter.isEmpty()) {
+
// this time we are using external script files.
 +
// at this sample we are only using JavaScript but
 +
// other supported backends will work well too.
 +
if(strSelectedInterpreter != "javascript") {
 
lblMessage->setText("-");
 
lblMessage->setText("-");
 
return;
 
return;
 
}
 
}
  
// this time we are using external script files.
+
// set the script file that should be executed
QString filename;
+
action->setFile("Testscriptfile.js");
if(strSelectedInterpreter == "python")
+
filename = "krossSigsSlots.py";
+
else if(strSelectedInterpreter == "javascript")
+
filename = "krossSigsSlots.js";
+
else
+
return;
+
  
// set the script file that should be executed.
+
// execute the scripting code, i.e. preload
action->setFile(filename);
+
 
+
// finally execute the scripting code, i.e. preload
+
 
action->trigger();
 
action->trigger();
  
Line 150: Line 142:
 
QVariantList arguments;
 
QVariantList arguments;
  
 +
// here we pass in the QLineEdit instance as argument.
 
QVariant v;
 
QVariant v;
 
v.setValue( (QWidget*) txtInputString );
 
v.setValue( (QWidget*) txtInputString );
 
arguments << v;
 
arguments << v;
  
 +
// and the second argument is a string
 
arguments << "Hello World";
 
arguments << "Hello World";
 +
 +
// Call the function
 
QVariant result = action->callFunction("reverseString", arguments);
 
QVariant result = action->callFunction("reverseString", arguments);
 +
 +
// Use the returnvalue of the function call
 
lblMessage->setText(result.toString());
 
lblMessage->setText(result.toString());
 +
}
 +
</code>
 +
 +
=== Testscriptfile.js ===
 +
 +
The following JavaScript file does provide us the reverseString function. This function will be executed at the mainwindow.cpp file.
 +
 +
<code javascript>
 +
function reverseString(lineedit, s)
 +
{
 +
    println("reverseString lineedit=" + lineedit + " s=" + s);
 +
 +
    lineedit.setText("Text to reverse: " +s);
 +
    //MyInputString.text = "Text to reverse: " +s;
 +
 +
    return s.split("").reverse().join("");
 
}
 
}
 
</code>
 
</code>

Revision as of 22:41, 16 January 2008

This tutorial shows how to deal with functions provided by a script. It builds upon the Kross Hello World tutorial.

Contents

The source files

This tutorial is based on the Hello World tutorial and extends the codebase we wrote there with new functionality.

CMakeLists.txt

We are using cmake to build our small sample project. The CMakeLists.txt file looks like;

project (krosshello) find_package(KDE4 REQUIRED) include_directories( ${KDE4_INCLUDES} ) set(krosshello_SRCS main.cpp mainwindow.cpp) kde4_add_executable(krosshello ${krosshello_SRCS}) target_link_libraries(krosshello ${KDE4_KDEUI_LIBS} ${KDE4_KROSSUI_LIBS})

main.cpp

The main.cpp does create the sample application and shows the mainwindow instance.

  1. include <QString>
  2. include <KApplication>
  3. include <KAboutData>
  4. include <KMessageBox>
  5. include <KCmdLineArgs>
  6. include <KLocalizedString>
  7. include "mainwindow.h"

int main (int argc, char *argv[]) { // Used to store information about a program. KAboutData aboutData("krosshello", 0, ki18n("Kross Hello World"), "1.0", ki18n("Hello World application for Kross"), KAboutData::License_GPL, ki18n("(c) 2007"), ki18n("Some text..."), "http://kross.dipe.org", "submit@bugs.kde.org"); // Access to the command-line arguments. KCmdLineArgs::init( argc, argv, &aboutData ); // Initialize the application. KApplication app; // Create and show the main window. MainWindow* window = new MainWindow(); window->show(); // Finally execute the application. return app.exec(); }

mainwindow.h

  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <QComboBox>
  2. include <QLabel>
  3. include <QLineEdit>
  4. include <kross/core/action.h>

// The main window to display our combobox and the label. class MainWindow : public QWidget { Q_OBJECT public: MainWindow(QWidget *parent=0); private Q_SLOTS: // This slot is called when the item in the combobox is changed. void interpreterActivated(const QString &); private: QLineEdit* txtInputString; QLabel* lblMessage; QComboBox* cmbInterpreters; Kross::Action* action; };

  1. endif

mainwindow.cpp

  1. include "mainwindow.h"
  2. include <QVBoxLayout>
  3. include <QDebug>
  4. include <kross/core/manager.h>
  5. include <kross/core/action.h>

// the constructor. MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { txtInputString = new QLineEdit(); lblMessage = new QLabel("Hello"); cmbInterpreters = new QComboBox (); cmbInterpreters->addItem("Choose Interpreter", "");

foreach(QString s, Kross::Manager::self().interpreters()) cmbInterpreters->addItem(s);

connect(cmbInterpreters, SIGNAL(activated(const QString &)), SLOT(interpreterActivated(const QString &)));

QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(cmbInterpreters); vLayout->addWidget(txtInputString); vLayout->addWidget(lblMessage); setLayout(vLayout);

// Create the Kross::Action instance and publish some // QObject instances. action = new Kross::Action(this, "MyScript"); action->addObject(txtInputString, "MyInputString"); action->addObject(cmbInterpreters, "MyInterpreter"); action->addObject(lblMessage, "MyLabel"); }

// this slot is called when the active item of the combobox changes. void MainWindow::interpreterActivated(const QString &strSelectedInterpreter) { // this time we are using external script files. // at this sample we are only using JavaScript but // other supported backends will work well too. if(strSelectedInterpreter != "javascript") { lblMessage->setText("-"); return; }

// set the script file that should be executed action->setFile("Testscriptfile.js");

// execute the scripting code, i.e. preload action->trigger();

// now we can call arbitrary functions QVariantList arguments;

// here we pass in the QLineEdit instance as argument. QVariant v; v.setValue( (QWidget*) txtInputString ); arguments << v;

// and the second argument is a string arguments << "Hello World";

// Call the function QVariant result = action->callFunction("reverseString", arguments);

// Use the returnvalue of the function call lblMessage->setText(result.toString()); }

Testscriptfile.js

The following JavaScript file does provide us the reverseString function. This function will be executed at the mainwindow.cpp file.

function reverseString(lineedit, s) {

   println("reverseString lineedit=" + lineedit + " s=" + s);
   lineedit.setText("Text to reverse: " +s);
   //MyInputString.text = "Text to reverse: " +s;
   return s.split("").reverse().join("");

}


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal