Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/scripting/kexiscripting/kexiscriptadaptor.h
Show All 18 Lines | |||||
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 | | |||
27 | #include <Kross/Manager> | | |||
28 | | ||||
29 | #include <KDbConnection> | 27 | #include <KDbConnection> | ||
30 | 28 | | |||
31 | #include <kexi.h> | 29 | #include <kexi.h> | ||
32 | #include <kexipart.h> | 30 | #include <kexipart.h> | ||
33 | #include <kexiproject.h> | 31 | #include <kexiproject.h> | ||
34 | #include <KexiMainWindowIface.h> | 32 | #include <KexiMainWindowIface.h> | ||
35 | #include <KexiWindow.h> | 33 | #include <KexiWindow.h> | ||
36 | #include <KexiView.h> | 34 | #include <KexiView.h> | ||
37 | 35 | | |||
36 | #include "../kexidb/kexidbmodule.h" | ||||
37 | | ||||
38 | /** | 38 | /** | ||
39 | * Adaptor class that provides Kexi specific functionality to | 39 | * Adaptor class that provides Kexi specific functionality to | ||
40 | * the scripting world. | 40 | * the scripting world. | ||
41 | */ | 41 | */ | ||
42 | class KexiScriptAdaptor : public QObject | 42 | class KexiScriptAdaptor : public QObject | ||
43 | { | 43 | { | ||
44 | Q_OBJECT | 44 | Q_OBJECT | ||
45 | public: | 45 | public: | ||
46 | explicit KexiScriptAdaptor() : m_kexidbmodule(0) { | 46 | explicit KexiScriptAdaptor() { | ||
staniek: -explicit | |||||
47 | setObjectName("Kexi"); | 47 | setObjectName("Kexi"); | ||
48 | } | 48 | } | ||
49 | virtual ~KexiScriptAdaptor() {} | 49 | virtual ~KexiScriptAdaptor() {} | ||
50 | public Q_SLOTS: | 50 | public Q_SLOTS: | ||
51 | 51 | | |||
52 | /** | 52 | /** | ||
53 | * Returns the current KexiWindow widget. | 53 | * Returns the current KexiWindow widget. | ||
54 | */ | 54 | */ | ||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
106 | } | 106 | } | ||
107 | 107 | | |||
108 | /** | 108 | /** | ||
109 | * Returns the KexiDBConnection object that belongs to the opened | 109 | * Returns the KexiDBConnection object that belongs to the opened | ||
110 | * project or return NULL if there was no project opened (no | 110 | * project or return NULL if there was no project opened (no | ||
111 | * connection established). | 111 | * connection established). | ||
112 | */ | 112 | */ | ||
113 | QObject* getConnection() { | 113 | QObject* getConnection() { | ||
114 | if (! m_kexidbmodule) | | |||
115 | m_kexidbmodule = Kross::Manager::self().module("kexidb"); | | |||
116 | KDbConnection *connection = project() ? project()->dbConnection() : 0; | 114 | KDbConnection *connection = project() ? project()->dbConnection() : 0; | ||
117 | if (m_kexidbmodule && connection) { | 115 | if (connection) { | ||
118 | QObject* result = 0; | 116 | QObject* result = 0; | ||
119 | if (QMetaObject::invokeMethod(m_kexidbmodule, "connectionWrapper", Q_RETURN_ARG(QObject*, result), Q_ARG(QObject*, connection))) | 117 | if (QMetaObject::invokeMethod(&m_kexidbmodule, "connectionWrapper", Q_RETURN_ARG(QObject*, result), Q_ARG(KDbConnection*, connection))) | ||
120 | return result; | 118 | return result; | ||
121 | } | 119 | } | ||
122 | return 0; | 120 | return 0; | ||
123 | } | 121 | } | ||
124 | 122 | | |||
125 | /** | 123 | /** | ||
126 | * Returns a list of names of all items the part class provides. Possible | 124 | * 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", | 125 | * classes are for example "org.kexi-project.table", "org.kexi-project.query", | ||
128 | * "org.kexi-project.form" or "org.kexi-project.script". | 126 | * "org.kexi-project.form" or "org.kexi-project.script". | ||
129 | * | 127 | * | ||
130 | * Python sample that prints all tables within the current project. | 128 | * Python sample that prints all tables within the current project. | ||
131 | * \code | 129 | * \code | ||
132 | * import Kexi | 130 | * import Kexi | ||
133 | * print Kexi.items("table") | 131 | * print Kexi.items("table") | ||
134 | * \endcode | 132 | * \endcode | ||
135 | */ | 133 | */ | ||
136 | QStringList items(const QString& pluginId) { | 134 | QStringList items(const QString& plugin) { | ||
137 | QStringList list; | 135 | QStringList list; | ||
138 | if (project()) { | 136 | if (project()) { | ||
139 | KexiPart::ItemList l; | 137 | KexiPart::ItemList l; | ||
140 | project()->getSortedItemsForPluginId(&l, pluginId(pluginId).toUtf8()); | 138 | project()->getSortedItemsForPluginId(&l, pluginId(plugin).toUtf8()); | ||
141 | l.sort(); | 139 | l.sort(); | ||
142 | foreach(KexiPart::Item* i, l) { | 140 | foreach(KexiPart::Item* i, l) { | ||
143 | list << i->name(); | 141 | list << i->name(); | ||
144 | } | 142 | } | ||
145 | } | 143 | } | ||
146 | return list; | 144 | return list; | ||
147 | } | 145 | } | ||
148 | 146 | | |||
149 | /** | 147 | /** | ||
150 | * Returns the caption for the item defined with \p pluginId and \p name . | 148 | * Returns the caption for the item defined with \p pluginId and \p name . | ||
151 | */ | 149 | */ | ||
152 | QString itemCaption(const QString& pluginId, const QString& name) const { | 150 | QString itemCaption(const QString& plugin, const QString& name) const { | ||
153 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 151 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
154 | return item ? item->caption() : QString(); | 152 | return item ? item->caption() : QString(); | ||
155 | } | 153 | } | ||
156 | 154 | | |||
157 | /** | 155 | /** | ||
158 | * Set the caption for the item defined with \p pluginId and \p name . | 156 | * Set the caption for the item defined with \p pluginId and \p name . | ||
159 | */ | 157 | */ | ||
160 | void setItemCaption(const QString& pluginId, const QString& name, const QString& caption) { | 158 | void setItemCaption(const QString& plugin, const QString& name, const QString& caption) { | ||
161 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 159 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
162 | item->setCaption(caption); | 160 | item->setCaption(caption); | ||
163 | } | 161 | } | ||
164 | 162 | | |||
165 | /** | 163 | /** | ||
166 | * Returns the description for the item defined with \p className and \p name . | 164 | * Returns the description for the item defined with \p className and \p name . | ||
167 | */ | 165 | */ | ||
168 | QString itemDescription(const QString& pluginId, const QString& name) const { | 166 | QString itemDescription(const QString& plugin, const QString& name) const { | ||
169 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 167 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
170 | return item ? item->description() : QString(); | 168 | return item ? item->description() : QString(); | ||
171 | } | 169 | } | ||
172 | 170 | | |||
173 | /** | 171 | /** | ||
174 | * Set the description for the item defined with \p className and \p name . | 172 | * Set the description for the item defined with \p className and \p name . | ||
175 | */ | 173 | */ | ||
176 | void setItemDescription(const QString& pluginId, const QString& name, const QString& description) { | 174 | void setItemDescription(const QString& plugin, const QString& name, const QString& description) { | ||
177 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 175 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
178 | item->setDescription(description); | 176 | item->setDescription(description); | ||
179 | } | 177 | } | ||
180 | 178 | | |||
181 | /** | 179 | /** | ||
182 | * Open an item. A window for the item defined with \p pluginId and \p name will | 180 | * 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 | 181 | * 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 | 182 | * default), "design" or "text" while the \args are optional arguments passed | ||
185 | * to the item. | 183 | * to the item. | ||
186 | * | 184 | * | ||
187 | * Python sample that opens the "cars" form in design view mode and sets then the | 185 | * Python sample that opens the "cars" form in design view mode and sets then the | ||
188 | * dirty state to mark the formular as modified. | 186 | * dirty state to mark the formular as modified. | ||
189 | * \code | 187 | * \code | ||
190 | * import Kexi | 188 | * import Kexi | ||
191 | * Kexi.openItem("form","cars","design") | 189 | * Kexi.openItem("form","cars","design") | ||
192 | * Kexi.windowWidget().setDirty(True) | 190 | * Kexi.windowWidget().setDirty(True) | ||
193 | * \endcode | 191 | * \endcode | ||
194 | */ | 192 | */ | ||
195 | bool openItem(const QString& pluginId, const QString& name, const QString& viewmode = QString(), | 193 | bool openItem(const QString& plugin, const QString& name, const QString& viewmode = QString(), | ||
196 | QVariantMap args = QVariantMap()) | 194 | QVariantMap args = QVariantMap()) | ||
197 | { | 195 | { | ||
198 | bool openingCancelled; | 196 | bool openingCancelled; | ||
199 | KexiPart::Item *item = partItem(pluginId(pluginId), name); | 197 | KexiPart::Item *item = partItem(pluginId(plugin), name); | ||
200 | KexiWindow* window = item | 198 | KexiWindow* window = item | ||
201 | ? mainWindow()->openObject( | 199 | ? mainWindow()->openObject( | ||
202 | item, | 200 | item, | ||
203 | stringToViewMode(viewmode), | 201 | stringToViewMode(viewmode), | ||
204 | &openingCancelled, | 202 | &openingCancelled, | ||
205 | args.isEmpty() ? 0 : &args | 203 | args.isEmpty() ? 0 : &args | ||
206 | ) | 204 | ) | ||
207 | : 0; | 205 | : 0; | ||
208 | return (window && ! openingCancelled); | 206 | return (window && ! openingCancelled); | ||
209 | } | 207 | } | ||
210 | 208 | | |||
211 | /** | 209 | /** | ||
212 | * Close an opened item. The window for the item defined with \p className and \p name | 210 | * Close an opened item. The window for the item defined with \p className and \p name | ||
213 | * will be closed. | 211 | * will be closed. | ||
214 | * | 212 | * | ||
215 | * Python sample that opens the "table1" table and closes the window right after | 213 | * Python sample that opens the "table1" table and closes the window right after | ||
216 | * being opened. | 214 | * being opened. | ||
217 | * \code | 215 | * \code | ||
218 | * import Kexi | 216 | * import Kexi | ||
219 | * Kexi.openItem("table","table1") | 217 | * Kexi.openItem("table","table1") | ||
220 | * Kexi.closeItem("table","table1") | 218 | * Kexi.closeItem("table","table1") | ||
221 | * \endcode | 219 | * \endcode | ||
222 | */ | 220 | */ | ||
223 | bool closeItem(const QString& pluginId, const QString& name) { | 221 | bool closeItem(const QString& plugin, const QString& name) { | ||
224 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 222 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
225 | return mainWindow()->closeObject(item) == true; | 223 | return mainWindow()->closeObject(item) == true; | ||
226 | return false; | 224 | return false; | ||
227 | } | 225 | } | ||
228 | 226 | | |||
229 | /** | 227 | /** | ||
230 | * Print the item defined with \p pluginId and \p name . | 228 | * Print the item defined with \p plugin and \p name . | ||
231 | */ | 229 | */ | ||
232 | bool printItem(const QString& pluginId, const QString& name, bool preview = false) { | 230 | bool printItem(const QString& plugin, const QString& name, bool preview = false) { | ||
233 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 231 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
234 | return (preview ? mainWindow()->printPreviewForItem(item) : mainWindow()->printItem(item)) == true; | 232 | return (preview ? mainWindow()->printPreviewForItem(item) : mainWindow()->printItem(item)) == true; | ||
235 | return false; | 233 | return false; | ||
236 | } | 234 | } | ||
237 | 235 | | |||
238 | /** | 236 | /** | ||
239 | * Executes custom action for the item defined with \p pluginId and \p name . | 237 | * Executes custom action for the item defined with \p pluginId and \p name . | ||
240 | */ | 238 | */ | ||
241 | bool executeItem(const QString& pluginId, const QString& name, const QString& actionName) { | 239 | bool executeItem(const QString& plugin, const QString& name, const QString& actionName) { | ||
242 | if (KexiPart::Item *item = partItem(pluginId(pluginId), name)) | 240 | if (KexiPart::Item *item = partItem(pluginId(plugin), name)) | ||
243 | return mainWindow()->executeCustomActionForObject(item, actionName) == true; | 241 | return mainWindow()->executeCustomActionForObject(item, actionName) == true; | ||
244 | return false; | 242 | return false; | ||
245 | } | 243 | } | ||
246 | 244 | | |||
247 | 245 | | |||
248 | /** | 246 | /** | ||
249 | * Returns the name of the current viewmode. This could be for example "data", | 247 | * 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. | 248 | * "design", "text" or just an empty string if there is no view at the moment. | ||
Show All 23 Lines | |||||
274 | * Returns true if there is a current view and those current view is dirty aka | 272 | * 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. | 273 | * has the dirty-flag set that indicates that something changed. | ||
276 | */ | 274 | */ | ||
277 | bool viewIsDirty() const { | 275 | bool viewIsDirty() const { | ||
278 | return currentView() ? currentView()->isDirty() : false; | 276 | return currentView() ? currentView()->isDirty() : false; | ||
279 | } | 277 | } | ||
280 | 278 | | |||
281 | private: | 279 | private: | ||
282 | QObject* m_kexidbmodule; | 280 | Scripting::KexiDBModule m_kexidbmodule; | ||
283 | 281 | | |||
284 | KexiMainWindowIface* mainWindow() const { | 282 | KexiMainWindowIface* mainWindow() const { | ||
285 | return KexiMainWindowIface::global(); | 283 | return KexiMainWindowIface::global(); | ||
286 | } | 284 | } | ||
287 | KexiProject* project() const { | 285 | KexiProject* project() const { | ||
288 | return mainWindow()->project(); | 286 | return mainWindow()->project(); | ||
289 | } | 287 | } | ||
290 | KexiWindow* currentWindow() const { | 288 | KexiWindow* currentWindow() const { | ||
291 | return mainWindow()->currentWindow(); | 289 | return mainWindow()->currentWindow(); | ||
292 | } | 290 | } | ||
293 | KexiView* currentView() const { | 291 | KexiView* currentView() const { | ||
294 | return currentWindow() ? currentWindow()->selectedView() : 0; | 292 | return currentWindow() ? currentWindow()->selectedView() : 0; | ||
295 | } | 293 | } | ||
296 | KexiPart::Item* partItem(const QString& pluginId, const QString& name) const { | 294 | KexiPart::Item* partItem(const QString& plugin, const QString& name) const { | ||
297 | return project() ? project()->itemForPluginId(pluginId(pluginId), name) : 0; | 295 | return project() ? project()->itemForPluginId(pluginId(plugin), name) : 0; | ||
298 | } | 296 | } | ||
299 | QString pluginId(const QString& pluginId) const { | 297 | QString pluginId(const QString& pluginId) const { | ||
300 | return pluginId.contains('.') ? pluginId : (QString::fromLatin1("org.kexi-project.")+pluginId); | 298 | return pluginId.contains('.') ? pluginId : (QString::fromLatin1("org.kexi-project.")+pluginId); | ||
301 | } | 299 | } | ||
302 | QString viewModeToString(Kexi::ViewMode mode, const QString& defaultViewMode = QString()) const { | 300 | QString viewModeToString(Kexi::ViewMode mode, const QString& defaultViewMode = QString()) const { | ||
303 | switch (mode) { | 301 | switch (mode) { | ||
304 | case Kexi::DataViewMode: | 302 | case Kexi::DataViewMode: | ||
305 | return "data"; | 303 | return "data"; | ||
Show All 22 Lines |
-explicit