##// END OF EJS Templates
added support for qflags, which where previously missing...
florianlink -
r49:d99de02fe548
parent child
Show More
@@ -1,244 +1,272
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 4 **
5 5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
6 6 **
7 7 ** This file may be used under the terms of the GNU General Public
8 8 ** License version 2.0 as published by the Free Software Foundation
9 9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 10 ** this file. Please review the following information to ensure GNU
11 11 ** General Public Licensing requirements will be met:
12 12 ** http://www.trolltech.com/products/qt/opensource.html
13 13 **
14 14 ** If you are unsure which license is appropriate for your use, please
15 15 ** review the following information:
16 16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 17 ** sales department at sales@trolltech.com.
18 18 **
19 19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 21 **
22 22 ****************************************************************************/
23 23
24 24 #include "shellheadergenerator.h"
25 25 #include "fileout.h"
26 26
27 27 #include <QtCore/QDir>
28 28
29 29 #include <qdebug.h>
30 30
31 31 QString ShellHeaderGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
32 32 {
33 33 return QString("PythonQtWrapper_%1.h").arg(meta_class->name());
34 34 }
35 35
36 36 void writeQtScriptQtBindingsLicense(QTextStream &stream);
37 37
38 38 void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
39 39 {
40 40
41 41 setupGenerator->addClass(meta_class);
42 42
43 43 if (FileOut::license)
44 44 writeQtScriptQtBindingsLicense(s);
45 45
46 46 QString include_block = "PYTHONQTWRAPPER_" + meta_class->name().toUpper() + "_H";
47 47
48 48 s << "#ifndef " << include_block << endl
49 49 << "#define " << include_block << endl << endl;
50 50
51 51 Include inc = meta_class->typeEntry()->include();
52 52 ShellGenerator::writeInclude(s, inc);
53 53
54 54 s << "#include <QObject>" << endl << endl;
55 55 s << "#include <PythonQt.h>" << endl << endl;
56 56
57 57 IncludeList list = meta_class->typeEntry()->extraIncludes();
58 58 qSort(list.begin(), list.end());
59 59 foreach (const Include &inc, list) {
60 60 ShellGenerator::writeInclude(s, inc);
61 61 }
62 62 s << endl;
63 63
64 64 QString pro_file_name = meta_class->package().replace(".", "_") + "/" + meta_class->package().replace(".", "_") + ".pri";
65 65
66 66 // if (!meta_class->generateShellClass()) {
67 67 // s << "#endif" << endl << endl;
68 68 // priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class));
69 69 // return ;
70 70 // }
71 71
72 72 AbstractMetaFunctionList ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
73 73 | AbstractMetaClass::WasVisible
74 74 | AbstractMetaClass::NotRemovedFromTargetLang);
75 75
76 76 // Shell-------------------------------------------------------------------
77 77 if (meta_class->generateShellClass()) {
78 78
79 79 AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
80 80
81 81 s << "class " << shellClassName(meta_class)
82 82 << " : public " << meta_class->qualifiedCppName() << endl << "{" << endl;
83 83 s << "public:" << endl;
84 84 foreach(AbstractMetaFunction* fun, ctors) {
85 85 s << " ";
86 86 writeFunctionSignature(s, fun, 0,"PythonQtShell_",
87 87 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
88 88 s << ":" << meta_class->qualifiedCppName() << "(";
89 89 QString scriptFunctionName = fun->originalName();
90 90 AbstractMetaArgumentList args = fun->arguments();
91 91 for (int i = 0; i < args.size(); ++i) {
92 92 if (i > 0)
93 93 s << ", ";
94 94 s << args.at(i)->argumentName();
95 95 }
96 96 s << "),_wrapper(NULL) {};" << endl;
97 97 }
98 98 s << endl;
99 99
100 100 foreach(AbstractMetaFunction* fun, virtualsForShell) {
101 101 s << "virtual ";
102 102 writeFunctionSignature(s, fun, 0, QString(),
103 103 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
104 104 s << ";" << endl;
105 105 }
106 106 s << endl;
107 107 s << " PythonQtInstanceWrapper* _wrapper; " << endl;
108 108
109 109 s << "};" << endl << endl;
110 110 }
111 111
112 112 // Promoter-------------------------------------------------------------------
113 113 AbstractMetaFunctionList promoteFunctions = getProtectedFunctionsThatNeedPromotion(meta_class);
114 114 if (!promoteFunctions.isEmpty()) {
115 115 s << "class " << promoterClassName(meta_class)
116 116 << " : public " << meta_class->qualifiedCppName() << endl << "{ public:" << endl;
117 117
118 118 foreach(AbstractMetaFunction* fun, promoteFunctions) {
119 119 s << "inline ";
120 120 writeFunctionSignature(s, fun, 0, "promoted_",
121 121 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
122 122 s << " { ";
123 123 QString scriptFunctionName = fun->originalName();
124 124 AbstractMetaArgumentList args = fun->arguments();
125 125 if (fun->type())
126 126 s << "return ";
127 127 s << meta_class->qualifiedCppName() << "::";
128 128 s << fun->originalName() << "(";
129 129 for (int i = 0; i < args.size(); ++i) {
130 130 if (i > 0)
131 131 s << ", ";
132 132 s << args.at(i)->argumentName();
133 133 }
134 134 s << "); }" << endl;
135 135 }
136 136
137 137 s << "};" << endl << endl;
138 138 }
139 139
140 140 // Wrapper-------------------------------------------------------------------
141 141
142 142 s << "class " << wrapperClassName(meta_class)
143 143 << " : public QObject" << endl
144 144 << "{ Q_OBJECT" << endl;
145 145
146 146 s << "public:" << endl;
147 147
148 148 AbstractMetaEnumList enums1 = meta_class->enums();
149 149 AbstractMetaEnumList enums;
150 QList<FlagsTypeEntry*> flags;
150 151 foreach(AbstractMetaEnum* enum1, enums1) {
151 152 // catch gadgets and enums that are not exported on QObjects...
152 153 if (enum1->wasPublic() && (!meta_class->isQObject() || !enum1->hasQEnumsDeclaration())) {
153 154 enums << enum1;
155 if (enum1->typeEntry()->flags()) {
156 flags << enum1->typeEntry()->flags();
157 }
154 158 }
155 159 }
156 160 if (enums.count()) {
157 161 s << "Q_ENUMS(";
158 162 foreach(AbstractMetaEnum* enum1, enums) {
159 163 s << enum1->name() << " ";
160 164 }
161 165 s << ")" << endl;
162
166
167 if (flags.count()) {
168 s << "Q_FLAGS(";
169 foreach(FlagsTypeEntry* flag1, flags) {
170 QString origName = flag1->originalName();
171 int idx = origName.lastIndexOf("::");
172 if (idx!= -1) {
173 origName = origName.mid(idx+2);
174 }
175 s << origName << " ";
176 }
177 s << ")" << endl;
178 }
179
163 180 foreach(AbstractMetaEnum* enum1, enums) {
164 181 s << "enum " << enum1->name() << "{" << endl;
165 182 bool first = true;
166 183 foreach(AbstractMetaEnumValue* value, enum1->values()) {
167 184 if (first) { first = false; }
168 185 else { s << ", "; }
169 186 s << " " << value->name() << " = " << meta_class->qualifiedCppName() << "::" << value->name();
170 187 }
171 188 s << "};" << endl;
172 189 }
190 if (flags.count()) {
191 foreach(AbstractMetaEnum* enum1, enums) {
192 if (enum1->typeEntry()->flags()) {
193 QString origName = enum1->typeEntry()->flags()->originalName();
194 int idx = origName.lastIndexOf("::");
195 if (idx!= -1) {
196 origName = origName.mid(idx+2);
197 }
198 s << "Q_DECLARE_FLAGS("<< origName << ", " << enum1->name() <<")"<<endl;
199 }
200 }
201 }
173 202 }
174
175 203 s << "public slots:" << endl;
176 204 if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
177 205
178 206 bool copyConstructorSeen = false;
179 207 bool defaultConstructorSeen = false;
180 208 foreach (const AbstractMetaFunction *fun, ctors) {
181 209 if (!fun->isPublic() || fun->isAbstract()) { continue; }
182 210 s << meta_class->qualifiedCppName() << "* ";
183 211 writeFunctionSignature(s, fun, 0, "new_",
184 212 Option(IncludeDefaultExpression | OriginalName | ShowStatic));
185 213 s << ";" << endl;
186 214 if (fun->arguments().size()==1 && meta_class->qualifiedCppName() == fun->arguments().at(0)->type()->typeEntry()->qualifiedCppName()) {
187 215 copyConstructorSeen = true;
188 216 }
189 217 if (fun->arguments().size()==0) {
190 218 defaultConstructorSeen = true;
191 219 }
192 220 }
193 221
194 222 if (meta_class->typeEntry()->isValue()
195 223 && !copyConstructorSeen && defaultConstructorSeen) {
196 224 QString className = meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName();
197 225 s << meta_class->qualifiedCppName() << "* new_" << meta_class->name() << "(const " << meta_class->qualifiedCppName() << "& other) {" << endl;
198 226 s << className << "* a = new " << className << "();" << endl;
199 227 s << "*((" << meta_class->qualifiedCppName() << "*)a) = other;" << endl;
200 228 s << "return a; }" << endl;
201 229 }
202 230 }
203 231 if (meta_class->hasPublicDestructor() && !meta_class->isNamespace()) {
204 232 s << "void delete_" << meta_class->name() << "(" << meta_class->qualifiedCppName() << "* obj) { delete obj; } ";
205 233 s << endl;
206 234 }
207 235 if (meta_class->name()=="QTreeWidgetItem") {
208 236 s << "bool hasOwner(QTreeWidgetItem* theWrappedObject) { return theWrappedObject->treeWidget()!=NULL || theWrappedObject->parent()!=NULL; }" << endl;
209 237 } else if (meta_class->name()=="QGraphicsItem") {
210 238 s << "bool hasOwner(QGraphicsItem* theWrappedObject) { return theWrappedObject->scene()!=NULL || theWrappedObject->parentItem()!=NULL; }" << endl;
211 239 }
212 240
213 241 AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class);
214 242
215 243 foreach (const AbstractMetaFunction *function, functions) {
216 244 if (!function->isSlot()) {
217 245 s << " ";
218 246 writeFunctionSignature(s, function, 0, QString(),
219 247 Option(FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
220 248 s << ";" << endl;
221 249 }
222 250 }
223 251
224 252 // writeInjectedCode(s, meta_class);
225 253
226 254 // s << endl << " QScriptValue __qtscript_self;" << endl;
227 255
228 256 s << "};" << endl << endl
229 257 << "#endif // " << include_block << endl;
230 258
231 259 if (!ShellGenerator::isBuiltIn(meta_class->name())) {
232 260 priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class));
233 261 }
234 262 }
235 263
236 264 void ShellHeaderGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)
237 265 {
238 266 CodeSnipList code_snips = meta_class->typeEntry()->codeSnips();
239 267 foreach (const CodeSnip &cs, code_snips) {
240 268 if (cs.language == TypeSystem::ShellDeclaration) {
241 269 s << cs.code() << endl;
242 270 }
243 271 }
244 272 }
General Comments 0
You need to be logged in to leave comments. Login now