From kwrite-devel Wed Jan 18 22:38:57 2012 From: Dominik Haumann Date: Wed, 18 Jan 2012 22:38:57 +0000 To: kwrite-devel Subject: Re: Fwd: Re: Scripting in Kile Message-Id: <201201182338.57773.dhdev () gmx ! de> X-MARC-Message: https://marc.info/?l=kwrite-devel&m=132692656302434 On Wednesday, 18. January 2012, Dominik Haumann wrote: > Hi, > > > > [...] > > > Now, since Kile certainly wants to provide its own additional > > > functions to the "view" and "document" object, we have to check how > > > Kile can extend these objects with own functions. Right now, I do > > > not know how this works. > > more on this in a separate mail. Way #1: via setPrototype What we have in Kate Part is this: // set the view/document objects as necessary m_engine->globalObject().setProperty("document", m_engine->newQObject(m_document = new KateScriptDocument())); m_engine->globalObject().setProperty("view", m_engine->newQObject(m_view = new KateScriptView())); // What I now added is this: QScriptValue v = m_engine->globalObject().property("view"); v.setPrototype(m_engine->globalObject().property("document")); The result is that the view inherits all the functions from document. This should work for Kile as well: 1. add property "kileDocument" with KileScriptDoument to the globalObject 2. add property "document" with the QObject* pointer of the Kate Part interface 3. call QScriptValue d = m_engine->globalObject().property("document"); d.setPrototype(m_engine->globalObject().property("kileDocument")); 4. now document.* also provides all the kileDocument functions. The uncomfortable part with this approach is, that document kind of inherits kileDocument, while the inheritance should logically work the other way round. And kileDocument "pollutes" the global scripting namespace. Way #2: moving javascript properties (in Qt terms: reparent functions) Javascript allows to enumerate all properties/functions with the following code: for (var p in kileView) // p is cursorLeft, cursorRight etc... So, as example, what we can do to move all document functions into the view prototype is to evaluate code like this in C++: QVariantList vl = m_engine->evaluate("var arr = new Array();\n" "for (var p in document)\n" " arr.push(p);\n" "arr;").toVariant().toList(); QScriptValue d = m_engine->globalObject().property("document"); QScriptValue v = m_engine->globalObject().property("view"); foreach (QVariant i, vl) { QString item = i.toString(); int idx = item.indexOf('('); if (idx > -1) { item.truncate(idx); // move from document to view QScriptValue sv = d.property(item); v.setProperty(item, sv); } } Now, view.insertText(0, 0, "asdf") works as well. This approach has an unfortuante side effect though: QObject properties are also exposed in javascript, this includes - objectName - destroyed(QObject*) - destroyed() - deleteLater() These are functions we do not want to move... Either we ignore this, or we have to make sure to only move the correct ones. These two ways work. Maybe there are other, better ways?! Greetings, Dominik _______________________________________________ KWrite-Devel mailing list KWrite-Devel@kde.org https://mail.kde.org/mailman/listinfo/kwrite-devel