1 /***************************************************************************
2                           rkcomponentscripting  -  description
3                              -------------------
4     begin                : Thu Jun 17 2010
5     copyright            : (C) 2010, 2013 by Thomas Friedrichsmeier
6     email                : thomas.friedrichsmeier@kdemail.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef RKCOMPONENTSCRIPTING_H
19 #define RKCOMPONENTSCRIPTING_H
20 
21 #include <QObject>
22 #include <QHash>
23 #include <QScriptEngine>
24 
25 #include "../rbackend/rcommand.h"
26 
27 class RKComponent;
28 class RKComponentBase;
29 class RKComponentPropertyBase;
30 
31 /** This class basically provides the API that is available to scripts running within rkward plugins.
32 The slots are meant to be called from the script.
33 
34 NOTE: This contains some duplication of ScriptBackend and derived classes. Perhaps this can be merged, better.
35 The key technical difference between this, and ScriptBackend, is that this operates in the main thread, while
36 ScriptBackend is designed to operate in a separate thread, and may merge a bunch of changes into a single update. */
37 class RKComponentScriptingProxy : public QObject {
38 Q_OBJECT
39 public:
40 	explicit RKComponentScriptingProxy (RKComponent *component);
41 	~RKComponentScriptingProxy ();
42 
43 	void initialize (const QString& file, const QString& command);
44 public slots:
45 	void componentChanged (RKComponent* changed);
46 	void propertyChanged (RKComponentPropertyBase* changed);
47 
48 // these are meant to be called from the script
49 	void include (const QString& filename);
50 	void addChangeCommand (const QString& changed_id, const QString& command);
51 /** @returns id of the command issued. */
52 	QVariant doRCommand (const QString& command, const QString& callback);
53 
54 	QVariant getValue (const QString &id) const;
55 	QVariant getString (const QString &id) const;
56 	QVariant getBoolean (const QString &id) const;
57 	QVariant getList (const QString &id) const;
58 	void setValue (const QString &value, const QString &id);
59 	void setListValue (const QStringList &value, const QString &id);
60 
61 	QVariantList getObjectInfo (const QString &name);
62 	QString getObjectParent (const QString &name);
63 	QString getObjectChild (const QString &name);
64 signals:
65 	void haveError ();
66 private slots:
67 	void scriptRCommandFinished (RCommand* command);
68 private:
69 	RKComponent* component;
70 	QScriptEngine engine;
71 	struct OutstandingCommand {
72 		RCommand *command;
73 		QString callback;
74 	};
75 	QList<OutstandingCommand> outstanding_commands;
76 	QString _scriptfile;
77 	void evaluate (const QString &code);
78 
79 	void handleChange (RKComponentBase* changed);
80 	QHash<RKComponentBase*, QString> component_commands;
81 
82 	void handleScriptError (const QString& current_file=QString ());
83 };
84 
85 #endif
86