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