summaryrefslogtreecommitdiffstats
path: root/kexi/formeditor/scripting
diff options
context:
space:
mode:
Diffstat (limited to 'kexi/formeditor/scripting')
-rw-r--r--kexi/formeditor/scripting/Makefile.am18
-rw-r--r--kexi/formeditor/scripting/formscript.cpp109
-rw-r--r--kexi/formeditor/scripting/formscript.h78
-rw-r--r--kexi/formeditor/scripting/scriptIO.cpp186
-rw-r--r--kexi/formeditor/scripting/scriptIO.h64
-rw-r--r--kexi/formeditor/scripting/scriptmanager.cpp70
-rw-r--r--kexi/formeditor/scripting/scriptmanager.h69
7 files changed, 594 insertions, 0 deletions
diff --git a/kexi/formeditor/scripting/Makefile.am b/kexi/formeditor/scripting/Makefile.am
new file mode 100644
index 00000000..f03ccfdf
--- /dev/null
+++ b/kexi/formeditor/scripting/Makefile.am
@@ -0,0 +1,18 @@
+include $(top_srcdir)/kexi/Makefile.global
+
+noinst_LTLIBRARIES = libkformdesigner_scripting.la
+
+libkformdesigner_scripting_la_SOURCES = formscript.cpp scriptIO.cpp scriptmanager.cpp
+
+libkformdesigner_scripting_la_LDFLAGS = $(all_libraries) -Wnounresolved
+libkformdesigner_scripting_la_LIBADD = $(top_builddir)/kexi/formeditor/libkformdesigner.la
+
+libkformdesigner_scripting_la_METASOURCES = AUTO
+
+SUBDIRS = .
+
+INCLUDES= -I$(top_srcdir)/kexi/formeditor \
+ -I$(top_srcdir)/kexi \
+ -I$(top_srcdir)/kexi/scripting \
+ -I$(top_srcdir)/kexi/core $(all_includes)
+
diff --git a/kexi/formeditor/scripting/formscript.cpp b/kexi/formeditor/scripting/formscript.cpp
new file mode 100644
index 00000000..b598066e
--- /dev/null
+++ b/kexi/formeditor/scripting/formscript.cpp
@@ -0,0 +1,109 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#include "formscript.h"
+#include "scriptmanager.h"
+
+#include "form.h"
+#include "objecttree.h"
+// Kross Includes
+#include "main/manager.h"
+#include "main/scriptcontainer.h"
+#include "api/exception.h"
+
+#include <kdebug.h>
+
+using namespace KFormDesigner;
+
+FormScript::FormScript(Form *form, ScriptManager *manager, const char *name)
+ : QObject(manager, name), m_manager(manager), m_form(form)
+{
+ m_script = manager->krossManager()->getScriptContainer(form->widget()->name());
+}
+
+FormScript::~FormScript()
+{
+}
+
+QString
+FormScript::getCode(const QString &)
+{
+ /// \todo Allow to select only one function
+ return m_script->getCode();
+}
+
+void
+FormScript::setCode(const QString &code)
+{
+ m_script->setCode(code);
+}
+
+void
+FormScript::appendCode(const QString &code)
+{
+ QString s = m_script->getCode();
+ s.append("\n");
+ s.append(code);
+ m_script->setCode(s);
+}
+
+bool
+FormScript::execute(const QString &functionName)
+{
+ /// \todo support return value and arguments
+ try {
+ m_script->callFunction(functionName);
+ }
+ catch(Kross::Api::Exception& e) {
+ kdDebug() << QString("EXCEPTION type='%1' description='%2'").arg(e.type()).arg(e.description()) << endl;
+ return false;
+ }
+ return true;
+}
+
+void
+FormScript::connectEvents()
+{
+ if(!m_form || !m_form->objectTree())
+ return;
+ // first call addQObject for each widget in the Form
+ ObjectTreeDict *dict = m_form->objectTree()->dict();
+ ObjectTreeDictIterator it(*dict);
+ for(; it.current(); ++it)
+ m_script->addQObject(it.current()->widget());
+ m_script->addQObject(m_form->widget());
+
+ // Then we connect all signals
+ QValueListConstIterator<Event*> endIt = m_list.constEnd();
+ for(QValueListConstIterator<Event*> it = m_list.constBegin(); it != endIt; ++it) {
+ if( (*it)->type() == Event::Slot) {
+ connect((*it)->sender(), (*it)->signal(), (*it)->receiver(), (*it)->slot());
+ }
+ else if( (*it)->type() == Event::UserFunction) {
+ m_script->connect((*it)->sender(), (*it)->signal(), (*it)->slot());
+ }
+ else if( (*it)->type() == Event::Action) {
+ /// \todo connect signals with actions
+ }
+ }
+}
+
+
+#include "formscript.moc"
+
diff --git a/kexi/formeditor/scripting/formscript.h b/kexi/formeditor/scripting/formscript.h
new file mode 100644
index 00000000..e3d63d74
--- /dev/null
+++ b/kexi/formeditor/scripting/formscript.h
@@ -0,0 +1,78 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#ifndef FORMSCRIPT_H
+#define FORMSCRIPT_H
+
+#include "kexievents.h"
+
+#include <qobject.h>
+#include <qstring.h>
+#include <ksharedptr.h>
+
+class ScriptManager;
+
+namespace KFormDesigner {
+ class Form;
+}
+
+namespace Kross {
+ namespace Api {
+ class ScriptContainer;
+ }
+}
+
+using namespace KFormDesigner;
+
+//! A class that stores the code and events related to a single form
+class FormScript : public QObject
+{
+ Q_OBJECT
+
+ public:
+ FormScript(Form *form, ScriptManager *manager, const char *name=0);
+ ~FormScript();
+
+ EventList* eventList() { return &m_list; }
+ Kross::Api::ScriptContainer* scriptContainer() { return m_script; }
+
+ /*! \return The code of funtionName. If parameter is empty, it returns the full code of this form.*/
+ QString getCode(const QString &functionName=QString::null);
+ /*! Replaces the actual form code with the string \a code.
+ Called eg by (future) script editor. */
+ void setCode(const QString &code);
+ /*! Adds the string \a code at the end of current code. Used to add a function in the script. */
+ void appendCode(const QString &code);
+
+ /*! Executes the \a functionName.
+ \todo how do we give parameters? */
+ bool execute(const QString &functionName);
+ /*! Really connects all events in the list.
+ Also calls Kross;;Api::Manager::addObject for each widget in the form to allow the user to
+ use these widgets in the script. */
+ void connectEvents();
+
+ private:
+ ScriptManager *m_manager;
+ Form *m_form;
+ KSharedPtr<Kross::Api::ScriptContainer> m_script;
+ EventList m_list;
+};
+
+#endif
diff --git a/kexi/formeditor/scripting/scriptIO.cpp b/kexi/formeditor/scripting/scriptIO.cpp
new file mode 100644
index 00000000..8620a8ae
--- /dev/null
+++ b/kexi/formeditor/scripting/scriptIO.cpp
@@ -0,0 +1,186 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#include "scriptIO.h"
+#include "formscript.h"
+#include "kexievents.h"
+
+#include "form.h"
+#include "objecttree.h"
+// Kross includes
+#include "main/scriptcontainer.h"
+
+bool
+ScriptIO::saveFormEvents(QDomNode &parentNode, FormScript *formScript)
+{
+ QDomDocument domDoc = parentNode.ownerDocument();
+
+ // Save the form's code
+ if(!formScript->getCode().isEmpty()) {
+ QDomElement script = domDoc.createElement("script");
+ script.setAttribute("language", formScript->scriptContainer()->getInterpreterName());
+ parentNode.appendChild(script);
+ QDomText scriptCode = domDoc.createTextNode(formScript->getCode());
+ script.appendChild(scriptCode);
+ }
+
+ // Save all form events
+ if(!formScript->eventList()->isEmpty())
+ saveEventList(formScript->eventList(), parentNode);
+ return true;
+}
+
+bool
+ScriptIO::loadFormEvents(QDomNode &parentNode, Form *form, ScriptManager *manager)
+{
+ QDomElement script = parentNode.namedItem("script").toElement();
+ QDomElement events = parentNode.namedItem("events").toElement();
+
+ // Load script code
+ FormScript *formScript = new FormScript(form, manager);
+ if(!script.isNull()) {
+ formScript->scriptContainer()->setInterpreterName(script.attribute("language"));
+ formScript->setCode(script.text());
+ }
+
+ // Load all events in the EventList
+ if(!events.isNull()) {
+ for(QDomNode n = events.firstChild(); !n.isNull(); n = n.nextSibling())
+ loadEvent(n, formScript->eventList(), form);
+ }
+ return true;
+}
+
+bool
+ScriptIO::saveAllEventsForWidget(QObject *widget, FormScript *formScript, QDomNode &node)
+{
+ EventList *l = formScript->eventList()->allEventsForObject(widget);
+ saveEventList(l, node);
+ return true;
+}
+
+void
+ScriptIO::saveEvent(Event *event, QDomNode &parentNode)
+{
+ if(!event)
+ return;
+
+ QDomDocument domDoc = parentNode.ownerDocument();
+ QDomElement eventNode = domDoc.createElement("event");
+ eventNode.setAttribute("type", event->type());
+ parentNode.appendChild(eventNode);
+
+ switch(event->type()) {
+ case Event::Slot: {
+ QDomElement sender = domDoc.createElement("sender");
+ eventNode.appendChild(sender);
+ QDomText senderText = domDoc.createTextNode(event->sender() ? event->sender()->name() : "");
+ sender.appendChild(senderText);
+
+ QDomElement signal = domDoc.createElement("signal");
+ eventNode.appendChild(signal);
+ QDomText signalText = domDoc.createTextNode(event->signal());
+ signal.appendChild(signalText);
+
+ QDomElement receiver = domDoc.createElement("receiver");
+ eventNode.appendChild(receiver);
+ QDomText receiverText = domDoc.createTextNode(event->receiver() ? event->receiver()->name() : "");
+ receiver.appendChild(receiverText);
+
+ QDomElement slot = domDoc.createElement("slot");
+ eventNode.appendChild(slot);
+ QDomText slotText = domDoc.createTextNode(event->slot());
+ slot.appendChild(slotText);
+ break;
+ }
+
+ case Event::UserFunction: {
+ QDomElement sender = domDoc.createElement("sender");
+ eventNode.appendChild(sender);
+ QDomText senderText = domDoc.createTextNode(event->sender() ? event->sender()->name() : "");
+ sender.appendChild(senderText);
+
+ QDomElement signal = domDoc.createElement("signal");
+ eventNode.appendChild(signal);
+ QDomText signalText = domDoc.createTextNode(event->signal());
+ signal.appendChild(signalText);
+
+ QDomElement function = domDoc.createElement("function");
+ eventNode.appendChild(function);
+ QDomText functionText = domDoc.createTextNode(event->slot());
+ function.appendChild(functionText);
+ break;
+ }
+
+ case Event::Action:
+ // !\todo
+ break;
+
+ default:
+ break;
+ }
+}
+
+void
+ScriptIO::saveEventList(EventList *list, QDomNode &parentNode)
+{
+ if(!list || list->isEmpty())
+ return;
+
+ QDomDocument domDoc = parentNode.ownerDocument();
+ QDomElement events = domDoc.createElement("events");
+ parentNode.appendChild(events);
+
+ QValueListConstIterator<Event*> endIt = list->constEnd();
+ for(QValueListConstIterator<Event*> it = list->constBegin(); it != endIt; ++it)
+ saveEvent((*it), events);
+}
+
+void
+ScriptIO::loadEvent(QDomNode &node, EventList *list, Form *form)
+{
+ int type = node.toElement().attribute("type").toInt();
+ Event *event = new Event();
+
+ switch(type) {
+ case Event::Slot: {
+ ObjectTreeItem *sender = form->objectTree()->lookup(node.namedItem("sender").toElement().text());
+ event->setSender(sender ? sender->widget() : 0);
+ event->setSignal(node.namedItem("signal").toElement().text().local8Bit());
+ ObjectTreeItem *receiver = form->objectTree()->lookup(node.namedItem("receiver").toElement().text());
+ event->setReceiver(receiver ? receiver->widget() : 0);
+ event->setSlot(node.namedItem("slot").toElement().text().local8Bit());
+ event->setType(Event::Slot);
+ break;
+ }
+
+ case Event::UserFunction: {
+ ObjectTreeItem *sender = form->objectTree()->lookup(node.namedItem("sender").toElement().text());
+ event->setSender(sender ? sender->widget() : 0);
+ event->setSignal(node.namedItem("signal").toElement().text().local8Bit());
+ event->setSlot(node.namedItem("function").toElement().text().local8Bit());
+ event->setType(Event::UserFunction);
+ break;
+ }
+ default: break;
+ }
+
+ list->addEvent(event);
+}
+
diff --git a/kexi/formeditor/scripting/scriptIO.h b/kexi/formeditor/scripting/scriptIO.h
new file mode 100644
index 00000000..9fed06a6
--- /dev/null
+++ b/kexi/formeditor/scripting/scriptIO.h
@@ -0,0 +1,64 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#ifndef SCRIPTIO_H
+#define SCRIPTIO_H
+
+#include <qdom.h>
+
+class QString;
+class QObject;
+class Event;
+class EventList;
+class ScriptManager;
+class FormScript;
+
+namespace KFormDesigner {
+ class Form;
+}
+
+using namespace KFormDesigner;
+
+//! A static class to deal with loading/saving events from/to XML
+class ScriptIO
+{
+ public:
+ /*! Save the evnts of a form.
+ Creates an \<events\> tag, and then one \<event\> tag for each event.
+ Each event contains \<sender\> and \<receiver\> tags, with attributes depending on event type. */
+ static bool saveFormEvents(QDomNode &parentNode, FormScript *script);
+ /*! Reads the \<events\> tag (\a parentNode), then creates and fills a FormScript object linked to this \a form.
+ The new FormScript object is then added to ScriptManager list.*/
+ static bool loadFormEvents(QDomNode &parentNode, Form *form, ScriptManager *manager);
+
+ /*! Save only the events related to widget \a name in the FormScript \a fscript.
+ Used eg when copying/pasting widgets to keep also events related to it.*/
+ static bool saveAllEventsForWidget(QObject *widget, FormScript *fscript, QDomNode &node);
+
+ static void saveEvent(Event *event, QDomNode &parentNode);
+ static void saveEventList(EventList *list, QDomNode &parentNode);
+ static void loadEvent(QDomNode &node, EventList *list, Form *form);
+
+ protected:
+ ScriptIO() {;}
+ ~ScriptIO() {;}
+};
+
+#endif
+
diff --git a/kexi/formeditor/scripting/scriptmanager.cpp b/kexi/formeditor/scripting/scriptmanager.cpp
new file mode 100644
index 00000000..c572c685
--- /dev/null
+++ b/kexi/formeditor/scripting/scriptmanager.cpp
@@ -0,0 +1,70 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#include "scriptmanager.h"
+#include "formscript.h"
+
+#include "form.h"
+#include "formmanager.h"
+// Kross includes
+#include "main/manager.h"
+
+using KFormDesigner::Form;
+
+ScriptManager::ScriptManager(QObject *parent, const char *name)
+ : QObject(parent, name)
+{
+ m_manager = Kross::Api::Manager::scriptManager();
+ m_dict.setAutoDelete(true);
+}
+
+ScriptManager::~ScriptManager()
+{
+}
+
+FormScript*
+ScriptManager::newFormScript(Form *form)
+{
+ FormScript *script = new FormScript(form, this);
+ m_dict.insert(form, script);
+ return script;
+}
+
+FormScript*
+ScriptManager::scriptForForm(Form *form)
+{
+ return m_dict[form];
+}
+
+void
+ScriptManager::setFormManager(FormManager *manager)
+{
+ m_formManager = manager;
+ connect(m_formManager, SIGNAL(aboutToDeleteForm(KFormDesigner::Form*)), this, SLOT(slotFormDeleted(KFormDesigner::Form*)));
+ connect(m_formManager, SIGNAL(formCreated(KFormDesigner::Form*)), this, SLOT(newFormScript(KFormDesigner::Form*)));
+}
+
+void
+ScriptManager::slotFormDeleted(KFormDesigner::Form *form)
+{
+ m_dict.remove(form);
+}
+
+#include "scriptmanager.moc"
+
diff --git a/kexi/formeditor/scripting/scriptmanager.h b/kexi/formeditor/scripting/scriptmanager.h
new file mode 100644
index 00000000..258ce2cc
--- /dev/null
+++ b/kexi/formeditor/scripting/scriptmanager.h
@@ -0,0 +1,69 @@
+/* This file is part of the KDE project
+ Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#ifndef SCRIPTMANAGER_H
+#define SCRIPTMANAGER_H
+
+#include <qobject.h>
+#include <qptrdict.h>
+
+class FormScript;
+
+namespace Kross {
+ namespace Api {
+ class Manager;
+ }
+}
+
+namespace KFormDesigner {
+ class FormManager;
+ class Form;
+}
+
+using namespace KFormDesigner;
+
+class ScriptManager : public QObject
+{
+ Q_OBJECT
+
+ public:
+ ScriptManager(QObject *parent=0, const char *name=0);
+ ~ScriptManager();
+
+ /*! \return The FormScript object associated to this Form. */
+ FormScript* scriptForForm(Form *form);
+
+ void setFormManager(FormManager *manager);
+ FormManager* formManager() { return m_formManager; }
+ Kross::Api::Manager* krossManager() { return m_manager; }
+
+ public slots:
+ /*! Called when a form is deleted. It is removed from the dict. */
+ void slotFormDeleted(KFormDesigner::Form *form);
+ /*! \return A new FormScript object associated to the Form \a form. */
+ FormScript* newFormScript(KFormDesigner::Form *form);
+
+ private:
+ Kross::Api::Manager *m_manager;
+ KFormDesigner::FormManager *m_formManager;
+ QPtrDict<FormScript> m_dict;
+};
+
+#endif
+