@@ -1,314 +1,309 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Script Generator project on Qt Labs. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 | 10 | ** No Commercial Usage |
|
11 | 11 | ** This file contains pre-release code and may not be distributed. |
|
12 | 12 | ** You may use this file in accordance with the terms and conditions |
|
13 | 13 | ** contained in the Technology Preview License Agreement accompanying |
|
14 | 14 | ** this package. |
|
15 | 15 | ** |
|
16 | 16 | ** GNU Lesser General Public License Usage |
|
17 | 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 | 18 | ** General Public License version 2.1 as published by the Free Software |
|
19 | 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 | 20 | ** packaging of this file. Please review the following information to |
|
21 | 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 | 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 | 23 | ** |
|
24 | 24 | ** In addition, as a special exception, Nokia gives you certain additional |
|
25 | 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 | 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 | 27 | ** |
|
28 | 28 | ** If you have questions regarding the use of this file, please contact |
|
29 | 29 | ** Nokia at qt-info@nokia.com. |
|
30 | 30 | ** |
|
31 | 31 | ** |
|
32 | 32 | ** |
|
33 | 33 | ** |
|
34 | 34 | ** |
|
35 | 35 | ** |
|
36 | 36 | ** |
|
37 | 37 | ** |
|
38 | 38 | ** $QT_END_LICENSE$ |
|
39 | 39 | ** |
|
40 | 40 | ****************************************************************************/ |
|
41 | 41 | |
|
42 | 42 | #include "shellheadergenerator.h" |
|
43 | 43 | #include "fileout.h" |
|
44 | 44 | |
|
45 | 45 | #include <QtCore/QDir> |
|
46 | 46 | |
|
47 | 47 | #include <qdebug.h> |
|
48 | 48 | |
|
49 | 49 | QString ShellHeaderGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const |
|
50 | 50 | { |
|
51 | 51 | return QString("PythonQtWrapper_%1.h").arg(meta_class->name()); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | void ShellHeaderGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *field) |
|
56 | 56 | { |
|
57 | 57 | const AbstractMetaFunction *setter = field->setter(); |
|
58 | 58 | const AbstractMetaFunction *getter = field->getter(); |
|
59 | 59 | |
|
60 | 60 | // static fields are not supported (yet?) |
|
61 | 61 | if (setter->isStatic()) return; |
|
62 | 62 | |
|
63 | 63 | // Uuid data4 did not work (TODO: move to typesystem...( |
|
64 | 64 | if (field->enclosingClass()->name()=="QUuid" && setter->name()=="data4") return; |
|
65 | 65 | if (field->enclosingClass()->name()=="QIPv6Address") return; |
|
66 | 66 | |
|
67 | 67 | if (!field->type()->isConstant()) { |
|
68 | 68 | writeFunctionSignature(s, setter, 0, QString(), |
|
69 | 69 | Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | ShowStatic | UnderscoreSpaces)); |
|
70 | 70 | s << "{ theWrappedObject->" << field->name() << " = " << setter->arguments()[0]->argumentName() << "; }\n"; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | writeFunctionSignature(s, getter, 0, QString(), |
|
74 | 74 | Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces)); |
|
75 | 75 | s << "{ return theWrappedObject->" << field->name() << "; }\n"; |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class) |
|
79 | 79 | { |
|
80 | 80 | QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":""; |
|
81 | 81 | QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri"; |
|
82 | 82 | priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class)); |
|
83 | 83 | setupGenerator->addClass(meta_class->package().replace(".", "_") + builtIn, meta_class); |
|
84 | 84 | |
|
85 | 85 | QString include_block = "PYTHONQTWRAPPER_" + meta_class->name().toUpper() + "_H"; |
|
86 | 86 | |
|
87 | 87 | s << "#ifndef " << include_block << endl |
|
88 | 88 | << "#define " << include_block << endl << endl; |
|
89 | 89 | |
|
90 | 90 | Include inc = meta_class->typeEntry()->include(); |
|
91 | 91 | ShellGenerator::writeInclude(s, inc); |
|
92 | 92 | |
|
93 | 93 | s << "#include <QObject>" << endl << endl; |
|
94 | 94 | s << "#include <PythonQt.h>" << endl << endl; |
|
95 | 95 | |
|
96 | 96 | IncludeList list = meta_class->typeEntry()->extraIncludes(); |
|
97 | 97 | qSort(list.begin(), list.end()); |
|
98 | 98 | foreach (const Include &inc, list) { |
|
99 | 99 | ShellGenerator::writeInclude(s, inc); |
|
100 | 100 | } |
|
101 | 101 | s << endl; |
|
102 | 102 | |
|
103 | 103 | AbstractMetaFunctionList ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors |
|
104 | 104 | | AbstractMetaClass::WasVisible |
|
105 | 105 | | AbstractMetaClass::NotRemovedFromTargetLang); |
|
106 | 106 | |
|
107 | 107 | // Shell------------------------------------------------------------------- |
|
108 | 108 | if (meta_class->generateShellClass()) { |
|
109 | 109 | |
|
110 | 110 | AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class); |
|
111 | 111 | |
|
112 | 112 | s << "class " << shellClassName(meta_class) |
|
113 | 113 | << " : public " << meta_class->qualifiedCppName() << endl << "{" << endl; |
|
114 | 114 | s << "public:" << endl; |
|
115 | 115 | foreach(AbstractMetaFunction* fun, ctors) { |
|
116 | 116 | s << " "; |
|
117 | 117 | writeFunctionSignature(s, fun, 0,"PythonQtShell_", |
|
118 | 118 | Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces)); |
|
119 | 119 | s << ":" << meta_class->qualifiedCppName() << "("; |
|
120 | 120 | QString scriptFunctionName = fun->originalName(); |
|
121 | 121 | AbstractMetaArgumentList args = fun->arguments(); |
|
122 | 122 | for (int i = 0; i < args.size(); ++i) { |
|
123 | 123 | if (i > 0) |
|
124 | 124 | s << ", "; |
|
125 | 125 | s << args.at(i)->argumentName(); |
|
126 | 126 | } |
|
127 | 127 | s << "),_wrapper(NULL) {};" << endl; |
|
128 | 128 | } |
|
129 | 129 | s << endl; |
|
130 | 130 | s << " ~" << shellClassName(meta_class) << "();" << endl; |
|
131 | 131 | s << endl; |
|
132 | 132 | |
|
133 | 133 | foreach(AbstractMetaFunction* fun, virtualsForShell) { |
|
134 | 134 | s << "virtual "; |
|
135 | 135 | writeFunctionSignature(s, fun, 0, QString(), |
|
136 | 136 | Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces)); |
|
137 | 137 | s << ";" << endl; |
|
138 | 138 | } |
|
139 | 139 | s << endl; |
|
140 | 140 | s << " PythonQtInstanceWrapper* _wrapper; " << endl; |
|
141 | 141 | |
|
142 | 142 | s << "};" << endl << endl; |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | // Promoter------------------------------------------------------------------- |
|
146 | 146 | AbstractMetaFunctionList promoteFunctions = getProtectedFunctionsThatNeedPromotion(meta_class); |
|
147 | 147 | if (!promoteFunctions.isEmpty()) { |
|
148 | 148 | s << "class " << promoterClassName(meta_class) |
|
149 | 149 | << " : public " << meta_class->qualifiedCppName() << endl << "{ public:" << endl; |
|
150 | 150 | |
|
151 | 151 | foreach(AbstractMetaFunction* fun, promoteFunctions) { |
|
152 | 152 | s << "inline "; |
|
153 | 153 | writeFunctionSignature(s, fun, 0, "promoted_", |
|
154 | 154 | Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces)); |
|
155 | 155 | s << " { "; |
|
156 | 156 | QString scriptFunctionName = fun->originalName(); |
|
157 | 157 | AbstractMetaArgumentList args = fun->arguments(); |
|
158 | 158 | if (fun->type()) |
|
159 | 159 | s << "return "; |
|
160 | 160 | s << meta_class->qualifiedCppName() << "::"; |
|
161 | 161 | s << fun->originalName() << "("; |
|
162 | 162 | for (int i = 0; i < args.size(); ++i) { |
|
163 | 163 | if (i > 0) |
|
164 | 164 | s << ", "; |
|
165 | 165 | s << args.at(i)->argumentName(); |
|
166 | 166 | } |
|
167 | 167 | s << "); }" << endl; |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | s << "};" << endl << endl; |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | // Wrapper------------------------------------------------------------------- |
|
174 | 174 | |
|
175 | 175 | s << "class " << wrapperClassName(meta_class) |
|
176 | 176 | << " : public QObject" << endl |
|
177 | 177 | << "{ Q_OBJECT" << endl; |
|
178 | 178 | |
|
179 | 179 | s << "public:" << endl; |
|
180 | 180 | |
|
181 | 181 | AbstractMetaEnumList enums1 = meta_class->enums(); |
|
182 | 182 | AbstractMetaEnumList enums; |
|
183 | 183 | QList<FlagsTypeEntry*> flags; |
|
184 | 184 | foreach(AbstractMetaEnum* enum1, enums1) { |
|
185 | 185 | // catch gadgets and enums that are not exported on QObjects... |
|
186 | 186 | if (enum1->wasPublic() && (!meta_class->isQObject() || !enum1->hasQEnumsDeclaration())) { |
|
187 | 187 | enums << enum1; |
|
188 | 188 | if (enum1->typeEntry()->flags()) { |
|
189 | 189 | flags << enum1->typeEntry()->flags(); |
|
190 | 190 | } |
|
191 | 191 | } |
|
192 | 192 | } |
|
193 | 193 | if (enums.count()) { |
|
194 | 194 | s << "Q_ENUMS("; |
|
195 | 195 | foreach(AbstractMetaEnum* enum1, enums) { |
|
196 | 196 | s << enum1->name() << " "; |
|
197 | 197 | } |
|
198 | 198 | s << ")" << endl; |
|
199 | 199 | |
|
200 | 200 | if (flags.count()) { |
|
201 | 201 | s << "Q_FLAGS("; |
|
202 | 202 | foreach(FlagsTypeEntry* flag1, flags) { |
|
203 | 203 | QString origName = flag1->originalName(); |
|
204 | 204 | int idx = origName.lastIndexOf("::"); |
|
205 | 205 | if (idx!= -1) { |
|
206 | 206 | origName = origName.mid(idx+2); |
|
207 | 207 | } |
|
208 | 208 | s << origName << " "; |
|
209 | 209 | } |
|
210 | 210 | s << ")" << endl; |
|
211 | 211 | } |
|
212 | 212 | |
|
213 | 213 | foreach(AbstractMetaEnum* enum1, enums) { |
|
214 | 214 | s << "enum " << enum1->name() << "{" << endl; |
|
215 | 215 | bool first = true; |
|
216 | 216 | foreach(AbstractMetaEnumValue* value, enum1->values()) { |
|
217 | 217 | if (first) { first = false; } |
|
218 | 218 | else { s << ", "; } |
|
219 | 219 | s << " " << value->name() << " = " << meta_class->qualifiedCppName() << "::" << value->name(); |
|
220 | 220 | } |
|
221 | 221 | s << "};" << endl; |
|
222 | 222 | } |
|
223 | 223 | if (flags.count()) { |
|
224 | 224 | foreach(AbstractMetaEnum* enum1, enums) { |
|
225 | 225 | if (enum1->typeEntry()->flags()) { |
|
226 | 226 | QString origName = enum1->typeEntry()->flags()->originalName(); |
|
227 | 227 | int idx = origName.lastIndexOf("::"); |
|
228 | 228 | if (idx!= -1) { |
|
229 | 229 | origName = origName.mid(idx+2); |
|
230 | 230 | } |
|
231 | 231 | s << "Q_DECLARE_FLAGS("<< origName << ", " << enum1->name() <<")"<<endl; |
|
232 | 232 | } |
|
233 | 233 | } |
|
234 | 234 | } |
|
235 | 235 | } |
|
236 | 236 | s << "public slots:" << endl; |
|
237 | 237 | if (meta_class->generateShellClass() || !meta_class->isAbstract()) { |
|
238 | 238 | |
|
239 | 239 | bool copyConstructorSeen = false; |
|
240 | 240 | bool defaultConstructorSeen = false; |
|
241 | 241 | foreach (const AbstractMetaFunction *fun, ctors) { |
|
242 | 242 | if (!fun->isPublic() || fun->isAbstract()) { continue; } |
|
243 | 243 | s << meta_class->qualifiedCppName() << "* "; |
|
244 | 244 | writeFunctionSignature(s, fun, 0, "new_", |
|
245 | 245 | Option(IncludeDefaultExpression | OriginalName | ShowStatic)); |
|
246 | 246 | s << ";" << endl; |
|
247 | 247 | if (fun->arguments().size()==1 && meta_class->qualifiedCppName() == fun->arguments().at(0)->type()->typeEntry()->qualifiedCppName()) { |
|
248 | 248 | copyConstructorSeen = true; |
|
249 | 249 | } |
|
250 | 250 | if (fun->arguments().size()==0) { |
|
251 | 251 | defaultConstructorSeen = true; |
|
252 | 252 | } |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | if (meta_class->typeEntry()->isValue() |
|
256 | 256 | && !copyConstructorSeen && defaultConstructorSeen) { |
|
257 | 257 | QString className = meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName(); |
|
258 | 258 | s << meta_class->qualifiedCppName() << "* new_" << meta_class->name() << "(const " << meta_class->qualifiedCppName() << "& other) {" << endl; |
|
259 | 259 | s << className << "* a = new " << className << "();" << endl; |
|
260 | 260 | s << "*((" << meta_class->qualifiedCppName() << "*)a) = other;" << endl; |
|
261 | 261 | s << "return a; }" << endl; |
|
262 | 262 | } |
|
263 | 263 | } |
|
264 | 264 | if (meta_class->hasPublicDestructor() && !meta_class->isNamespace()) { |
|
265 | 265 | s << "void delete_" << meta_class->name() << "(" << meta_class->qualifiedCppName() << "* obj) { delete obj; } "; |
|
266 | 266 | s << endl; |
|
267 | 267 | } |
|
268 | if (meta_class->name()=="QTreeWidgetItem") { | |
|
269 | s << "bool py_hasOwner(QTreeWidgetItem* theWrappedObject) { return theWrappedObject->treeWidget()!=NULL || theWrappedObject->parent()!=NULL; }" << endl; | |
|
270 | } else if (meta_class->name()=="QGraphicsItem") { | |
|
271 | s << "bool py_hasOwner(QGraphicsItem* theWrappedObject) { return theWrappedObject->scene()!=NULL || theWrappedObject->parentItem()!=NULL; }" << endl; | |
|
272 | } | |
|
273 | 268 | |
|
274 | 269 | AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class); |
|
275 | 270 | |
|
276 | 271 | foreach (const AbstractMetaFunction *function, functions) { |
|
277 | 272 | if (!function->isSlot() || function->isVirtual()) { |
|
278 | 273 | s << " "; |
|
279 | 274 | writeFunctionSignature(s, function, 0, QString(), |
|
280 | 275 | Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces)); |
|
281 | 276 | s << ";" << endl; |
|
282 | 277 | } |
|
283 | 278 | } |
|
284 | 279 | if (meta_class->hasDefaultToStringFunction() || meta_class->hasToStringCapability()) { |
|
285 | 280 | s << " QString py_toString(" << meta_class->qualifiedCppName() << "*);" << endl; |
|
286 | 281 | } |
|
287 | 282 | if (meta_class->hasDefaultIsNull()) { |
|
288 | 283 | s << " bool __nonzero__(" << meta_class->qualifiedCppName() << "* obj) { return !obj->isNull(); }" << endl; |
|
289 | 284 | } |
|
290 | 285 | |
|
291 | 286 | // Field accessors |
|
292 | 287 | foreach (const AbstractMetaField *field, meta_class->fields()) { |
|
293 | 288 | if (field->isPublic()) { |
|
294 | 289 | writeFieldAccessors(s, field); |
|
295 | 290 | } |
|
296 | 291 | } |
|
297 | 292 | |
|
298 | 293 | writeInjectedCode(s, meta_class); |
|
299 | 294 | |
|
300 | 295 | |
|
301 | 296 | s << "};" << endl << endl |
|
302 | 297 | << "#endif // " << include_block << endl; |
|
303 | 298 | |
|
304 | 299 | } |
|
305 | 300 | |
|
306 | 301 | void ShellHeaderGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class) |
|
307 | 302 | { |
|
308 | 303 | CodeSnipList code_snips = meta_class->typeEntry()->codeSnips(); |
|
309 | 304 | foreach (const CodeSnip &cs, code_snips) { |
|
310 | 305 | if (cs.language == TypeSystem::PyWrapperDeclaration) { |
|
311 | 306 | s << cs.code() << endl; |
|
312 | 307 | } |
|
313 | 308 | } |
|
314 | 309 | } |
General Comments 0
You need to be logged in to leave comments.
Login now