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