Kross
Kross::Action Class Reference
The Action class is an abstract container to deal with scripts like a single standalone script file. More...
#include <action.h>

Public Slots | |
void | addQObject (QObject *obj, const QString &name=QString()) |
QVariant | callFunction (const QString &name, const QVariantList &args=QVariantList()) |
QByteArray | code () const |
QString | currentPath () const |
QString | description () const |
QVariant | evaluate (const QByteArray &code) |
QString | file () const |
QStringList | functionNames () |
QString | iconName () const |
QString | interpreter () const |
bool | isEnabled () const |
QString | name () const |
QVariant | option (const QString &name, const QVariant &defaultvalue=QVariant()) |
QVariantMap | options () const |
QObject * | qobject (const QString &name) const |
QStringList | qobjectNames () const |
void | setCode (const QByteArray &code) |
void | setDescription (const QString &description) |
void | setEnabled (bool enabled) |
bool | setFile (const QString &scriptfile) |
void | setIconName (const QString &iconname) |
void | setInterpreter (const QString &interpretername) |
bool | setOption (const QString &name, const QVariant &value) |
int | version () const |
Signals | |
void | dataChanged (Action *) |
void | finalized (Kross::Action *) |
void | finished (Kross::Action *) |
void | started (Kross::Action *) |
void | updated () |
Public Member Functions | |
Action (QObject *parent, const QUrl &url) | |
Action (QObject *parent, const QString &name, const QDir &packagepath=QDir()) | |
void | finalize () |
void | fromDomElement (const QDomElement &element) |
bool | initialize () |
bool | isFinalized () const |
Script * | script () const |
QDomElement | toDomElement () const |
virtual | ~Action () |
Detailed Description
The Action class is an abstract container to deal with scripts like a single standalone script file.Each action holds a reference to the matching Kross::Interpreter created Kross::Script instance.
The Manager takes care of handling the Action instances application by providing access to ActionCollection containers for those Action instances.
Once you've such a Action instance you're able to perform actions with it like executing scripting code.
Following sample shows "Hello World." executed with the python interpreter:
# Create a new Kross::Action instance. Kross::Action* action = new Kross::Action(0,"MyFirstScript"); # Set the interpreter we like to use. This could be e.g. "python", "ruby" or "kjs". action->setInterpreter("python"); # Set the scripting code. action->setCode("print \"Hello World.\""); # Execute the scripting code. action->trigger();
Following sample demonstrates how to execute an external python script file. The script file itself is named "mytest.py" and contains:
# this function got called from within C++ def myfunction(args): print "Arguments are: %s" % args # Import the published QObject's import MyFirstQObject, MySecondQObject # Call a slot MyFirstQObject provides. MyFirstQObject.someSlot("Some string") # Set a property MySecondQObject provides. MySecondQObject.someProperty = "Other string"
# Publish a QObject instance for all Kross::Action instances. Kross::Manager::self().addObject(myqobject1, "MyFirstQObject") # Create a new Kross::Action instance. Kross::Action* action = new Kross::Action(0,"MySecondScript"); # Publish a QObject instance only for the Kross::Action instance. action->addObject(myqobject2, "MySecondQObject"); # Set the script file we like to execute. action->setFile("/home/myuser/mytest.py"); # Execute the script file. action->trigger(); # Call the "myfunction" defined in the "mytest.py" python script. QVariant result = action->callFunction("myfunction", QVariantList()<<"Arg");
Definition at line 94 of file action.h.
Constructor & Destructor Documentation
Constructor.
- Parameters:
-
parent The parent QObject this Action is a child of. url The URL should point to a valid scripting file. This Action will be filled with the content of the file (e.g. the file is read and code should return its content and it's also tried to determine the interpreter ). Remember to use QUrl c'tor explicitly. The name will be set to url.path()
Definition at line 120 of file action.cpp.
Action::~Action | ( | ) | [virtual] |
Member Function Documentation
Add a QObject instance to the action.
This instance will be published to scripts.
Definition at line 352 of file action.cpp.
QVariant Action::callFunction | ( | const QString & | name, | |
const QVariantList & | args = QVariantList() | |||
) | [slot] |
Call a function in the script.
- Parameters:
-
name The name of the function which should be called. args The optional list of arguments.
Definition at line 396 of file action.cpp.
QByteArray Action::code | ( | ) | const [slot] |
QString Action::currentPath | ( | ) | const [slot] |
- Returns:
- the current path the script is running in or an empty string if there is no current path defined.
Definition at line 342 of file action.cpp.
void Kross::Action::dataChanged | ( | Action * | ) | [signal] |
This signal is emitted when the data of the Action is changed.
QString Action::description | ( | ) | const [slot] |
QVariant Action::evaluate | ( | const QByteArray & | code | ) | [slot] |
Evaluate some scripting code.
Example how this can be used:
Kross::Action* a = new Kross::Action(0, "MyScript"); a->setInterpreter("python"); a->setCode("def myFunc(x): return x"); a->execute(); int three = a->evaluate("1+2").toInt(); // returns 3 int nine = a->evaluate("myFunc(9)").toInt(); // returns 9
- Parameters:
-
code The scripting code to evaluate.
- Returns:
- The return value of the evaluation.
Definition at line 405 of file action.cpp.
QString Action::file | ( | ) | const [slot] |
void Action::finalize | ( | ) |
Finalize the Script instance and frees any cached or still running executions.
Normally there is no need to call this function directly because the Action will take care of calling it if needed.
Definition at line 462 of file action.cpp.
void Kross::Action::finalized | ( | Kross::Action * | ) | [signal] |
This signal is emitted once a script finalized.
void Kross::Action::finished | ( | Kross::Action * | ) | [signal] |
This signal is emitted after the script got executed.
void Action::fromDomElement | ( | const QDomElement & | element | ) |
Method to read settings from the QDomElement element
that contains details about e.g.
the displayed text, the file to execute or the used interpreter.
Definition at line 150 of file action.cpp.
QStringList Action::functionNames | ( | ) | [slot] |
QString Action::iconName | ( | ) | const [slot] |
bool Action::initialize | ( | ) |
Initialize the Script instance.
Normally there is no need to call this function directly because if will be called internally if needed (e.g. on execute ).
- Returns:
- true if the initialization was successful else false is returned.
Definition at line 414 of file action.cpp.
QString Action::interpreter | ( | ) | const [slot] |
- Returns:
- the name of the interpreter. Could be for example "python" or "ruby".
Definition at line 298 of file action.cpp.
bool Action::isEnabled | ( | ) | const [slot] |
Return true if this Action is enabled else false is returned.
Definition at line 271 of file action.cpp.
bool Action::isFinalized | ( | ) | const |
- Returns:
- true if the action is finalized, which means the action is currently not running.
Definition at line 470 of file action.cpp.
QString Action::name | ( | ) | const [slot] |
- Returns:
- the value of the option defined with
name
. If there doesn't exist an option with such a name, thedefaultvalue
is returned.
Definition at line 367 of file action.cpp.
QVariantMap Action::options | ( | ) | const [slot] |
- Returns:
- a map of options this Action defines. The options are returned call-by-ref, so you are able to manipulate them.
Definition at line 347 of file action.cpp.
QStringList Action::qobjectNames | ( | ) | const [slot] |
Kross::Script * Action::script | ( | ) | const |
- Returns:
- the Kross::Script implementation used by the scripting backend. This returns NULL until the action got triggered or if there was a error before that.
Definition at line 231 of file action.cpp.
void Action::setCode | ( | const QByteArray & | code | ) | [slot] |
void Action::setDescription | ( | const QString & | description | ) | [slot] |
void Action::setEnabled | ( | bool | enabled | ) | [slot] |
bool Action::setFile | ( | const QString & | scriptfile | ) | [slot] |
void Action::setIconName | ( | const QString & | iconname | ) | [slot] |
void Action::setInterpreter | ( | const QString & | interpretername | ) | [slot] |
Set the name of the interpreter (javascript, python or ruby).
Definition at line 303 of file action.cpp.
void Kross::Action::started | ( | Kross::Action * | ) | [signal] |
This signal is emitted before the script got executed.
QDomElement Action::toDomElement | ( | ) | const |
- Returns:
- a QDomElement that contains the settings like e.g. the displayed text, the file to execute or the used interpreter of this Action instance.
Definition at line 201 of file action.cpp.
void Kross::Action::updated | ( | ) | [signal] |
This signal is emitted if the content of the Action was changed.
The ActionCollection instances this Action is a child of are connected with this signal to fire up their own updated signal if an Action of them was updated.
int Action::version | ( | ) | const [slot] |
- Returns:
- the version number this Action has. Per default 0 is returned.
Definition at line 241 of file action.cpp.
The documentation for this class was generated from the following files: