Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/scripting/kexiscripting/kexiscriptadaptor.h
Show All 17 Lines | |||||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #ifndef KEXISCRIPTADAPTOR_H | 20 | #ifndef KEXISCRIPTADAPTOR_H | ||
21 | #define KEXISCRIPTADAPTOR_H | 21 | #define KEXISCRIPTADAPTOR_H | ||
22 | 22 | | |||
23 | #include <QObject> | 23 | #include <QObject> | ||
24 | #include <QMetaObject> | 24 | #include <QMetaObject> | ||
25 | #include <QAction> | 25 | #include <QAction> | ||
26 | 26 | #include <QJSEngine> | |||
27 | #include <Kross/Manager> | 27 | #include <QJSValue> | ||
28 | 28 | | |||
29 | #include <KDbConnection> | 29 | #include <KDbConnection> | ||
30 | 30 | | |||
31 | #include <kexi.h> | 31 | #include <kexi.h> | ||
32 | #include <kexipart.h> | 32 | #include <kexipart.h> | ||
33 | #include <kexiproject.h> | 33 | #include <kexiproject.h> | ||
34 | #include <KexiMainWindowIface.h> | 34 | #include <KexiMainWindowIface.h> | ||
35 | #include <KexiWindow.h> | 35 | #include <KexiWindow.h> | ||
36 | #include <KexiView.h> | 36 | #include <KexiView.h> | ||
37 | 37 | | |||
38 | #include "../kexidb/kexidbmodule.h" | ||||
39 | #include "KexiScriptingDebug.h" | ||||
40 | | ||||
38 | /** | 41 | /** | ||
39 | * Adaptor class that provides Kexi specific functionality to | 42 | * Adaptor class that provides Kexi specific functionality to | ||
40 | * the scripting world. | 43 | * the scripting world. | ||
41 | */ | 44 | */ | ||
42 | class KexiScriptAdaptor : public QObject | 45 | class KexiScriptAdaptor : public QObject | ||
43 | { | 46 | { | ||
44 | Q_OBJECT | 47 | Q_OBJECT | ||
45 | public: | 48 | public: | ||
46 | explicit KexiScriptAdaptor() : m_kexidbmodule(0) { | 49 | Q_INVOKABLE KexiScriptAdaptor() { | ||
staniek: -explicit | |||||
47 | setObjectName("Kexi"); | 50 | KexiScriptingDebug() << "Created Script Adaptor"; | ||
48 | } | 51 | } | ||
49 | virtual ~KexiScriptAdaptor() {} | 52 | virtual ~KexiScriptAdaptor() {} | ||
50 | public Q_SLOTS: | | |||
51 | 53 | | |||
52 | /** | 54 | /** | ||
53 | * Returns the current KexiWindow widget. | 55 | * Returns the current KexiWindow widget. | ||
54 | */ | 56 | */ | ||
55 | QWidget* windowWidget() const { | 57 | Q_INVOKABLE QWidget* windowWidget() const { | ||
56 | return currentWindow(); | 58 | return currentWindow(); | ||
57 | } | 59 | } | ||
58 | 60 | | |||
59 | /** | 61 | /** | ||
60 | * Returns the current KexiView widget. | 62 | * Returns the current KexiView widget. | ||
61 | */ | 63 | */ | ||
62 | QWidget* viewWidget() const { | 64 | Q_INVOKABLE QWidget* viewWidget() const { | ||
63 | return currentView(); | 65 | return currentView(); | ||
64 | } | 66 | } | ||
65 | 67 | | |||
66 | /** | 68 | /** | ||
67 | * Returns a list of all QAction instances the Kexi main | 69 | * Returns a list of all QAction instances the Kexi main | ||
68 | * window provides. | 70 | * window provides. | ||
69 | * | 71 | * | ||
70 | * Python sample that prints the list of all actions the | 72 | * Python sample that prints the list of all actions the | ||
71 | * main window does provide. | 73 | * main window does provide. | ||
72 | * \code | 74 | * \code | ||
73 | * import Kexi | 75 | * import Kexi | ||
74 | * for a in Kexi.actions(): | 76 | * for a in Kexi.actions(): | ||
75 | * print "name=%s text=%s" % (a.objectName,a.text) | 77 | * print "name=%s text=%s" % (a.objectName,a.text) | ||
76 | * \endcode | 78 | * \endcode | ||
77 | */ | 79 | */ | ||
78 | QVariantList actions() { | 80 | Q_INVOKABLE QVariantList actions() { | ||
79 | QVariantList list; | 81 | QVariantList list; | ||
80 | foreach(QAction* action, mainWindow()->allActions()) { | 82 | foreach(QAction* action, mainWindow()->allActions()) { | ||
81 | QVariant v; | 83 | QVariant v; | ||
82 | v.setValue((QObject*) action); | 84 | v.setValue((QObject*) action); | ||
83 | list << v; | 85 | list << v; | ||
84 | } | 86 | } | ||
85 | return list; | 87 | return list; | ||
86 | } | 88 | } | ||
87 | 89 | | |||
88 | /** | 90 | /** | ||
89 | * Returns the QAction instance the Kexi main window provides that | 91 | * Returns the QAction instance the Kexi main window provides that | ||
90 | * has the objectName \p name or NULL if there is no such action. | 92 | * has the objectName \p name or NULL if there is no such action. | ||
91 | */ | 93 | */ | ||
92 | QObject* action(const QString& name) { | 94 | Q_INVOKABLE QObject* action(const QString& name) { | ||
93 | foreach(QAction* action, mainWindow()->allActions()) { | 95 | foreach(QAction* action, mainWindow()->allActions()) { | ||
94 | if (action->objectName() == name) | 96 | if (action->objectName() == name) | ||
95 | return action; | 97 | return action; | ||
96 | } | 98 | } | ||
97 | return 0; | 99 | return 0; | ||
98 | } | 100 | } | ||
99 | 101 | | |||
100 | /** | 102 | /** | ||
101 | * Returns true if we are connected with a project else false | 103 | * Returns true if we are connected with a project else false | ||
102 | * is returned. | 104 | * is returned. | ||
103 | */ | 105 | */ | ||
104 | bool isConnected() { | 106 | Q_INVOKABLE bool isConnected() { | ||
105 | return project() ? project()->isConnected() : false; | 107 | return project() ? project()->isConnected() : false; | ||
106 | } | 108 | } | ||
107 | 109 | | |||
108 | /** | 110 | /** | ||
109 | * Returns the KexiDBConnection object that belongs to the opened | 111 | * Returns the KexiDBConnection object that belongs to the opened | ||
110 | * project or return NULL if there was no project opened (no | 112 | * project or return NULL if there was no project opened (no | ||
111 | * connection established). | 113 | * connection established). | ||
112 | */ | 114 | */ | ||
113 | QObject* getConnection() { | 115 | Q_INVOKABLE QObject* getConnection() { | ||
114 | if (! m_kexidbmodule) | | |||
115 | m_kexidbmodule = Kross::Manager::self().module("kexidb"); | | |||
116 | KDbConnection *connection = project() ? project()->dbConnection() : 0; | 116 | KDbConnection *connection = project() ? project()->dbConnection() : 0; | ||
117 | if (m_kexidbmodule && connection) { | 117 | if (connection) { | ||
118 | QObject* result = 0; | 118 | QObject* result = 0; | ||
119 | if (QMetaObject::invokeMethod(m_kexidbmodule, "connectionWrapper", Q_RETURN_ARG(QObject*, result), Q_ARG(QObject*, connection))) | 119 | if (QMetaObject::invokeMethod(&m_kexidbmodule, "connectionWrapper", Q_RETURN_ARG(QObject*, result), Q_ARG(KDbConnection*, connection))) | ||
120 | return result; | 120 | return result; | ||
121 | } | 121 | } | ||
122 | return 0; | 122 | return 0; | ||
123 | } | 123 | } | ||
124 | 124 | | |||
125 | /** | 125 | /** | ||
126 | * Returns a list of names of all items the part class provides. Possible | 126 | * Returns a list of names of all items the part class provides. Possible | ||
127 | * classes are for example "org.kexi-project.table", "org.kexi-project.query", | 127 | * classes are for example "org.kexi-project.table", "org.kexi-project.query", | ||
128 | * "org.kexi-project.form" or "org.kexi-project.script". | 128 | * "org.kexi-project.form" or "org.kexi-project.script". | ||
129 | * | 129 | * | ||
130 | * Python sample that prints all tables within the current project. | 130 | * Python sample that prints all tables within the current project. | ||
131 | * \code | 131 | * \code | ||
132 | * import Kexi | 132 | * import Kexi | ||
133 | * print Kexi.items("table") | 133 | * print Kexi.items("table") | ||
134 | * \endcode | 134 | * \endcode | ||
135 | */ | 135 | */ | ||
136 | QStringList items(const QString& pluginId) { | 136 | Q_INVOKABLE QStringList items(const QString& plugin) { | ||
137 | QStringList list; | 137 | QStringList list; | ||
138 | if (project()) { | 138 | if (project()) { | ||
139 | KexiPart::ItemList l; | 139 | KexiPart::ItemList l; | ||
140 | project()->getSortedItemsForPluginId(&l, pluginId(pluginId).toUtf8()); | 140 | project()->getSortedItemsForPluginId(&l, pluginId(plugin).toUtf8()); | ||
141 | l.sort(); | 141 | l.sort(); | ||
142 | foreach(KexiPart::Item* i, l) { | 142 | foreach(KexiPart::Item* i, l) { | ||
143 | list << i->name(); | 143 | list << i->name(); | ||
144 | } | 144 | } | ||
145 | } | 145 | } | ||
146 | return list; | 146 | return list; | ||
147 | } | 147 | } | ||
148 | 148 | | |||
149 | /** | 149 | /** | ||
150 | * Returns the caption for the item defined with \p pluginId and \p name . | 150 | * Returns the caption for the item defined with \p pluginId and \p name . | ||
151 | */ | 151 | */ | ||
152 | QString itemCaption(const QString& pluginId, const QString& name) const { | 152 | Q_INVOKABLE QString itemCaptioq_gadgetn(const QString& plugin, const QString& name) const { | ||
153 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 153 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
154 | return item ? item->caption() : QString(); | 154 | return item ? item->caption() : QString(); | ||
155 | } | 155 | } | ||
156 | 156 | | |||
157 | /** | 157 | /** | ||
158 | * Set the caption for the item defined with \p pluginId and \p name . | 158 | * Set the caption for the item defined with \p pluginId and \p name . | ||
159 | */ | 159 | */ | ||
160 | void setItemCaption(const QString& pluginId, const QString& name, const QString& caption) { | 160 | Q_INVOKABLE void setItemCaption(const QString& plugin, const QString& name, const QString& caption) { | ||
161 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 161 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
162 | item->setCaption(caption); | 162 | item->setCaption(caption); | ||
163 | } | 163 | } | ||
164 | 164 | | |||
165 | /** | 165 | /** | ||
166 | * Returns the description for the item defined with \p className and \p name . | 166 | * Returns the description for the item defined with \p className and \p name . | ||
167 | */ | 167 | */ | ||
168 | QString itemDescription(const QString& pluginId, const QString& name) const { | 168 | Q_INVOKABLE QString itemDescription(const QString& plugin, const QString& name) const { | ||
169 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 169 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
170 | return item ? item->description() : QString(); | 170 | return item ? item->description() : QString(); | ||
171 | } | 171 | } | ||
172 | 172 | | |||
173 | /** | 173 | /** | ||
174 | * Set the description for the item defined with \p className and \p name . | 174 | * Set the description for the item defined with \p className and \p name . | ||
175 | */ | 175 | */ | ||
176 | void setItemDescription(const QString& pluginId, const QString& name, const QString& description) { | 176 | Q_INVOKABLE void setItemDescription(const QString& plugin, const QString& name, const QString& description) { | ||
177 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 177 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
178 | item->setDescription(description); | 178 | item->setDescription(description); | ||
179 | } | 179 | } | ||
180 | 180 | | |||
181 | /** | 181 | /** | ||
182 | * Open an item. A window for the item defined with \p pluginId and \p name will | 182 | * Open an item. A window for the item defined with \p pluginId and \p name will | ||
183 | * be opened and we switch to it. The \p viewmode could be for example "data" (the | 183 | * be opened and we switch to it. The \p viewmode could be for example "data" (the | ||
184 | * default), "design" or "text" while the \args are optional arguments passed | 184 | * default), "design" or "text" while the \args are optional arguments passed | ||
185 | * to the item. | 185 | * to the item. | ||
186 | * | 186 | * | ||
187 | * Python sample that opens the "cars" form in design view mode and sets then the | 187 | * Python sample that opens the "cars" form in design view mode and sets then the | ||
188 | * dirty state to mark the formular as modified. | 188 | * dirty state to mark the formular as modified. | ||
189 | * \code | 189 | * \code | ||
190 | * import Kexi | 190 | * import Kexi | ||
191 | * Kexi.openItem("form","cars","design") | 191 | * Kexi.openItem("form","cars","design") | ||
192 | * Kexi.windowWidget().setDirty(True) | 192 | * Kexi.windowWidget().setDirty(True) | ||
193 | * \endcode | 193 | * \endcode | ||
194 | */ | 194 | */ | ||
195 | bool openItem(const QString& pluginId, const QString& name, const QString& viewmode = QString(), | 195 | Q_INVOKABLE bool openItem(const QString& plugin, const QString& name, const QString& viewmode = QString(), | ||
196 | QVariantMap args = QVariantMap()) | 196 | QVariantMap args = QVariantMap()) | ||
197 | { | 197 | { | ||
198 | bool openingCancelled; | 198 | bool openingCancelled; | ||
199 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 199 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
200 | KexiWindow* window = item | 200 | KexiWindow* window = item | ||
201 | ? mainWindow()->openObject( | 201 | ? mainWindow()->openObject( | ||
202 | item, | 202 | item, | ||
203 | stringToViewMode(viewmode), | 203 | stringToViewMode(viewmode), | ||
204 | &openingCancelled, | 204 | &openingCancelled, | ||
205 | args.isEmpty() ? 0 : &args | 205 | args.isEmpty() ? 0 : &args | ||
206 | ) | 206 | ) | ||
207 | : 0; | 207 | : 0; | ||
208 | return (window && ! openingCancelled); | 208 | return (window && ! openingCancelled); | ||
209 | } | 209 | } | ||
210 | 210 | | |||
211 | /** | 211 | /** | ||
212 | * Close an opened item. The window for the item defined with \p className and \p name | 212 | * Close an opened item. The window for the item defined with \p className and \p name | ||
213 | * will be closed. | 213 | * will be closed. | ||
214 | * | 214 | * | ||
215 | * Python sample that opens the "table1" table and closes the window right after | 215 | * Python sample that opens the "table1" table and closes the window right after | ||
216 | * being opened. | 216 | * being opened. | ||
217 | * \code | 217 | * \code | ||
218 | * import Kexi | 218 | * import Kexi | ||
219 | * Kexi.openItem("table","table1") | 219 | * Kexi.openItem("table","table1") | ||
220 | * Kexi.closeItem("table","table1") | 220 | * Kexi.closeItem("table","table1") | ||
221 | * \endcode | 221 | * \endcode | ||
222 | */ | 222 | */ | ||
223 | bool closeItem(const QString& pluginId, const QString& name) { | 223 | Q_INVOKABLE bool closeItem(const QString& plugin, const QString& name) { | ||
224 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 224 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
225 | return mainWindow()->closeObject(item) == true; | 225 | return mainWindow()->closeObject(item) == true; | ||
226 | return false; | 226 | return false; | ||
227 | } | 227 | } | ||
228 | 228 | | |||
229 | /** | 229 | /** | ||
230 | * Print the item defined with \p pluginId and \p name . | 230 | * Print the item defined with \p plugin and \p name . | ||
231 | */ | 231 | */ | ||
232 | bool printItem(const QString& pluginId, const QString& name, bool preview = false) { | 232 | Q_INVOKABLE bool printItem(const QString& plugin, const QString& name, bool preview = false) { | ||
233 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 233 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
234 | return (preview ? mainWindow()->printPreviewForItem(item) : mainWindow()->printItem(item)) == true; | 234 | return (preview ? mainWindow()->printPreviewForItem(item) : mainWindow()->printItem(item)) == true; | ||
235 | return false; | 235 | return false; | ||
236 | } | 236 | } | ||
237 | 237 | | |||
238 | /** | 238 | /** | ||
239 | * Executes custom action for the item defined with \p pluginId and \p name . | 239 | * Executes custom action for the item defined with \p pluginId and \p name . | ||
240 | */ | 240 | */ | ||
241 | bool executeItem(const QString& pluginId, const QString& name, const QString& actionName) { | 241 | Q_INVOKABLE bool executeItem(const QString& plugin, const QString& name, const QString& actionName) { | ||
242 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 242 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
243 | return mainWindow()->executeCustomActionForObject(item, actionName) == true; | 243 | return mainWindow()->executeCustomActionForObject(item, actionName) == true; | ||
244 | return false; | 244 | return false; | ||
245 | } | 245 | } | ||
246 | 246 | | |||
247 | 247 | | |||
248 | /** | 248 | /** | ||
249 | * Returns the name of the current viewmode. This could be for example "data", | 249 | * Returns the name of the current viewmode. This could be for example "data", | ||
250 | * "design", "text" or just an empty string if there is no view at the moment. | 250 | * "design", "text" or just an empty string if there is no view at the moment. | ||
251 | */ | 251 | */ | ||
252 | QString viewMode() const { | 252 | Q_INVOKABLE QString viewMode() const { | ||
253 | return currentView() ? viewModeToString(currentView()->viewMode()) : QString(); | 253 | return currentView() ? viewModeToString(currentView()->viewMode()) : QString(); | ||
254 | } | 254 | } | ||
255 | 255 | | |||
256 | /** | 256 | /** | ||
257 | * Returns a list of names of all available viewmodes the view supports. | 257 | * Returns a list of names of all available viewmodes the view supports. | ||
258 | */ | 258 | */ | ||
259 | QStringList viewModes() const { | 259 | Q_INVOKABLE QStringList viewModes() const { | ||
260 | QStringList list; | 260 | QStringList list; | ||
261 | if (currentWindow()) { | 261 | if (currentWindow()) { | ||
262 | Kexi::ViewModes modes = currentWindow()->supportedViewModes(); | 262 | Kexi::ViewModes modes = currentWindow()->supportedViewModes(); | ||
263 | if (modes & Kexi::DataViewMode) | 263 | if (modes & Kexi::DataViewMode) | ||
264 | list << "data"; | 264 | list << "data"; | ||
265 | if (modes & Kexi::DesignViewMode) | 265 | if (modes & Kexi::DesignViewMode) | ||
266 | list << "design"; | 266 | list << "design"; | ||
267 | if (modes & Kexi::TextViewMode) | 267 | if (modes & Kexi::TextViewMode) | ||
268 | list << "text"; | 268 | list << "text"; | ||
269 | } | 269 | } | ||
270 | return list; | 270 | return list; | ||
271 | } | 271 | } | ||
272 | 272 | | |||
273 | /** | 273 | /** | ||
274 | * Returns true if there is a current view and those current view is dirty aka | 274 | * Returns true if there is a current view and those current view is dirty aka | ||
275 | * has the dirty-flag set that indicates that something changed. | 275 | * has the dirty-flag set that indicates that something changed. | ||
276 | */ | 276 | */ | ||
277 | bool viewIsDirty() const { | 277 | Q_INVOKABLE bool viewIsDirty() const { | ||
278 | return currentView() ? currentView()->isDirty() : false; | 278 | return currentView() ? currentView()->isDirty() : false; | ||
279 | } | 279 | } | ||
280 | 280 | | |||
281 | private: | 281 | private: | ||
282 | QObject* m_kexidbmodule; | 282 | Scripting::KexiDBModule m_kexidbmodule; | ||
283 | 283 | | |||
284 | KexiMainWindowIface* mainWindow() const { | 284 | KexiMainWindowIface* mainWindow() const { | ||
285 | return KexiMainWindowIface::global(); | 285 | return KexiMainWindowIface::global(); | ||
286 | } | 286 | } | ||
287 | KexiProject* project() const { | 287 | KexiProject* project() const { | ||
288 | return mainWindow()->project(); | 288 | return mainWindow()->project(); | ||
289 | } | 289 | } | ||
290 | KexiWindow* currentWindow() const { | 290 | KexiWindow* currentWindow() const { | ||
291 | return mainWindow()->currentWindow(); | 291 | return mainWindow()->currentWindow(); | ||
292 | } | 292 | } | ||
293 | KexiView* currentView() const { | 293 | KexiView* currentView() const { | ||
294 | return currentWindow() ? currentWindow()->selectedView() : 0; | 294 | return currentWindow() ? currentWindow()->selectedView() : 0; | ||
295 | } | 295 | } | ||
296 | KexiPart::Item* partItem(const QString& pluginId, const QString& name) const { | 296 | KexiPart::Item* partItem(const QString& plugin, const QString& name) const { | ||
297 | return project() ? project()->itemForPluginId(pluginId(pluginId), name) : 0; | 297 | return project() ? project()->itemForPluginId(pluginId(plugin), name) : 0; | ||
298 | } | 298 | } | ||
299 | QString pluginId(const QString& pluginId) const { | 299 | QString pluginId(const QString& pluginId) const { | ||
300 | return pluginId.contains('.') ? pluginId : (QString::fromLatin1("org.kexi-project.")+pluginId); | 300 | return pluginId.contains('.') ? pluginId : (QString::fromLatin1("org.kexi-project.")+pluginId); | ||
301 | } | 301 | } | ||
302 | QString viewModeToString(Kexi::ViewMode mode, const QString& defaultViewMode = QString()) const { | 302 | QString viewModeToString(Kexi::ViewMode mode, const QString& defaultViewMode = QString()) const { | ||
303 | switch (mode) { | 303 | switch (mode) { | ||
304 | case Kexi::DataViewMode: | 304 | case Kexi::DataViewMode: | ||
305 | return "data"; | 305 | return "data"; | ||
Show All 10 Lines | 315 | Kexi::ViewMode stringToViewMode(const QString& mode, Kexi::ViewMode defaultViewMode = Kexi::DataViewMode) const { | |||
316 | if (mode == "data") | 316 | if (mode == "data") | ||
317 | return Kexi::DataViewMode; | 317 | return Kexi::DataViewMode; | ||
318 | if (mode == "design") | 318 | if (mode == "design") | ||
319 | return Kexi::DesignViewMode; | 319 | return Kexi::DesignViewMode; | ||
320 | if (mode == "text") | 320 | if (mode == "text") | ||
321 | return Kexi::TextViewMode; | 321 | return Kexi::TextViewMode; | ||
322 | return defaultViewMode; | 322 | return defaultViewMode; | ||
323 | } | 323 | } | ||
324 | | ||||
324 | }; | 325 | }; | ||
325 | 326 | | |||
326 | #endif | 327 | #endif | ||
327 | 328 | |
-explicit