@@ -1,233 +1,233 | |||
|
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 "setupgenerator.h" |
|
43 | 43 | #include "shellgenerator.h" |
|
44 | 44 | #include "reporthandler.h" |
|
45 | 45 | #include "fileout.h" |
|
46 | 46 | |
|
47 | 47 | //#define Q_SCRIPT_LAZY_GENERATOR |
|
48 | 48 | |
|
49 | 49 | void SetupGenerator::addClass(const QString& package, const AbstractMetaClass *cls) |
|
50 | 50 | { |
|
51 | 51 | packHash[package].append(cls); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | void maybeDeclareMetaType(QTextStream &stream, const QString &typeName, |
|
55 | 55 | QSet<QString> ®isteredTypeNames); |
|
56 | 56 | bool hasDefaultConstructor(const AbstractMetaClass *meta_class); |
|
57 | 57 | |
|
58 | 58 | void SetupGenerator::generate() |
|
59 | 59 | { |
|
60 | 60 | AbstractMetaClassList classes_with_polymorphic_id; |
|
61 | 61 | { |
|
62 | 62 | QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash); |
|
63 | 63 | while (pack.hasNext()) { |
|
64 | 64 | pack.next(); |
|
65 | 65 | QList<const AbstractMetaClass*> list = pack.value(); |
|
66 | 66 | foreach (const AbstractMetaClass *cls, list) { |
|
67 | 67 | if (cls->typeEntry()->isPolymorphicBase()) { |
|
68 | 68 | classes_with_polymorphic_id.append((AbstractMetaClass*)cls); |
|
69 | 69 | } |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash); |
|
75 | 75 | while (pack.hasNext()) { |
|
76 | 76 | pack.next(); |
|
77 | 77 | QList<const AbstractMetaClass*> list = pack.value(); |
|
78 | 78 | if (list.isEmpty()) |
|
79 | 79 | continue; |
|
80 | 80 | |
|
81 | 81 | QString packKey = pack.key(); |
|
82 | 82 | QString packName = pack.key(); |
|
83 | 83 | QStringList components = packName.split("_"); |
|
84 | 84 | if ((components.size() > 2) && (components.at(0) == "com") |
|
85 | 85 | && (components.at(1) == "trolltech")) { |
|
86 | 86 | // kill com.trolltech in key |
|
87 | 87 | components.removeAt(0); |
|
88 | 88 | components.removeAt(0); |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | QString shortPackName; |
|
92 | 92 | foreach (QString comp, components) { |
|
93 | 93 | comp[0] = comp[0].toUpper(); |
|
94 | 94 | shortPackName += comp; |
|
95 | 95 | } |
|
96 | 96 | // add missing camel case (workaround..) |
|
97 | 97 | if (shortPackName == "QtWebkit") { |
|
98 | 98 | shortPackName = "QtWebKit"; |
|
99 | 99 | } else if (shortPackName == "QtXmlpatterns") { |
|
100 | 100 | shortPackName = "QtXmlPatterns"; |
|
101 | 101 | } else if (shortPackName == "QtOpengl") { |
|
102 | 102 | shortPackName = "QtOpenGL"; |
|
103 | 103 | } else if (shortPackName == "QtUitools") { |
|
104 | 104 | shortPackName = "QtUiTools"; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | { |
|
109 | 109 | FileOut initFile(m_out_dir + "/generated_cpp/" + packName + "/" + packKey + "_init.cpp"); |
|
110 | 110 | QTextStream &s = initFile.stream; |
|
111 | 111 | |
|
112 | 112 | s << "#include <PythonQt.h>" << endl; |
|
113 | 113 | |
|
114 | 114 | for (int i=0; i<(list.count()+MAX_CLASSES_PER_FILE-1) / MAX_CLASSES_PER_FILE; i++) { |
|
115 | 115 | s << "#include \"" << packKey << QString::number(i) << ".h\"" << endl; |
|
116 | 116 | } |
|
117 | 117 | s << endl; |
|
118 | 118 | |
|
119 | 119 | QStringList polymorphicHandlers; |
|
120 | 120 | if (!packName.endsWith("_builtin")) { |
|
121 | 121 | polymorphicHandlers = writePolymorphicHandler(s, list.at(0)->package(), classes_with_polymorphic_id); |
|
122 | 122 | s << endl; |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | // declare individual class creation functions |
|
126 | 126 | s << "void PythonQt_init_" << shortPackName << "() {" << endl; |
|
127 | 127 | |
|
128 | 128 | if (shortPackName.endsWith("Builtin")) { |
|
129 | shortPackName = shortPackName.mid(shortPackName.length()-strlen("builtin")); | |
|
129 | shortPackName = shortPackName.mid(0, shortPackName.length()-strlen("builtin")); | |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | QStringList cppClassNames; |
|
133 | 133 | foreach (const AbstractMetaClass *cls, list) { |
|
134 | 134 | |
|
135 | 135 | QString shellCreator; |
|
136 | 136 | if (cls->generateShellClass()) { |
|
137 | 137 | shellCreator = ", PythonQtSetInstanceWrapperOnShell<" + ShellGenerator::shellClassName(cls) + ">"; |
|
138 | 138 | } |
|
139 | 139 | if (cls->isQObject()) { |
|
140 | 140 | s << "PythonQt::self()->registerClass(&" << cls->qualifiedCppName() << "::staticMetaObject, \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl; |
|
141 | 141 | } else { |
|
142 | 142 | QString baseName = cls->baseClass()?cls->baseClass()->qualifiedCppName():""; |
|
143 | 143 | s << "PythonQt::self()->registerCPPClass(\""<< cls->qualifiedCppName() << "\", \"" << baseName << "\", \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl; |
|
144 | 144 | } |
|
145 | 145 | foreach(AbstractMetaClass* interface, cls->interfaces()) { |
|
146 | 146 | // the interface might be our own class... (e.g. QPaintDevice) |
|
147 | 147 | if (interface->qualifiedCppName() != cls->qualifiedCppName()) { |
|
148 | 148 | s << "PythonQt::self()->addParentClass(\""<< cls->qualifiedCppName() << "\", \"" << interface->qualifiedCppName() << "\",PythonQtUpcastingOffset<" << cls->qualifiedCppName() <<","<<interface->qualifiedCppName()<<">());" << endl; |
|
149 | 149 | } |
|
150 | 150 | } |
|
151 | 151 | } |
|
152 | 152 | s << endl; |
|
153 | 153 | foreach (QString handler, polymorphicHandlers) { |
|
154 | 154 | s << "PythonQt::self()->addPolymorphicHandler(\""<< handler << "\", polymorphichandler_" << handler << ");" << endl; |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | s << "}"; |
|
158 | 158 | s << endl; |
|
159 | 159 | } |
|
160 | 160 | } |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | QStringList SetupGenerator::writePolymorphicHandler(QTextStream &s, const QString &package, |
|
164 | 164 | const AbstractMetaClassList &classes) |
|
165 | 165 | { |
|
166 | 166 | QStringList handlers; |
|
167 | 167 | foreach (AbstractMetaClass *cls, classes) { |
|
168 | 168 | const ComplexTypeEntry *centry = cls->typeEntry(); |
|
169 | 169 | if (!centry->isPolymorphicBase()) |
|
170 | 170 | continue; |
|
171 | 171 | bool isGraphicsItem = (cls->qualifiedCppName()=="QGraphicsItem"); |
|
172 | 172 | |
|
173 | 173 | AbstractMetaClassList classList = this->classes(); |
|
174 | 174 | bool first = true; |
|
175 | 175 | foreach (AbstractMetaClass *clazz, classList) { |
|
176 | 176 | bool inherits = false; |
|
177 | 177 | if (isGraphicsItem) { |
|
178 | 178 | foreach(AbstractMetaClass* interfaze, clazz->interfaces()) { |
|
179 | 179 | if (interfaze->qualifiedCppName()=="QGraphicsItem") { |
|
180 | 180 | inherits = true; |
|
181 | 181 | break; |
|
182 | 182 | } |
|
183 | 183 | } |
|
184 | 184 | } else { |
|
185 | 185 | inherits = clazz->inheritsFrom(cls); |
|
186 | 186 | } |
|
187 | 187 | if (clazz->package() == package && inherits) { |
|
188 | 188 | if (!clazz->typeEntry()->polymorphicIdValue().isEmpty() || isGraphicsItem) { |
|
189 | 189 | // On first find, open the function |
|
190 | 190 | if (first) { |
|
191 | 191 | first = false; |
|
192 | 192 | |
|
193 | 193 | QString handler = cls->name(); |
|
194 | 194 | handlers.append(handler); |
|
195 | 195 | |
|
196 | 196 | s << "static void* polymorphichandler_" << handler |
|
197 | 197 | << "(const void *ptr, char **class_name)" << endl |
|
198 | 198 | << "{" << endl |
|
199 | 199 | << " Q_ASSERT(ptr != 0);" << endl |
|
200 | 200 | << " " << cls->qualifiedCppName() << " *object = (" |
|
201 | 201 | << cls->qualifiedCppName() << " *)ptr;" << endl; |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | // For each, add case label |
|
205 | 205 | QString polyId = clazz->typeEntry()->polymorphicIdValue(); |
|
206 | 206 | if (isGraphicsItem) { |
|
207 | 207 | polyId = "%1->type() == " + clazz->qualifiedCppName() + "::Type"; |
|
208 | 208 | } |
|
209 | 209 | s << " if (" |
|
210 | 210 | << polyId.replace("%1", "object") |
|
211 | 211 | << ") {" << endl |
|
212 | 212 | << " *class_name = \"" << clazz->name() << "\";" << endl |
|
213 | 213 | << " return (" << clazz->qualifiedCppName() << "*)object;" << endl |
|
214 | 214 | << " }" << endl; |
|
215 | 215 | } else { |
|
216 | 216 | QString warning = QString("class '%1' inherits from polymorphic class '%2', but has no polymorphic id set") |
|
217 | 217 | .arg(clazz->name()) |
|
218 | 218 | .arg(cls->name()); |
|
219 | 219 | |
|
220 | 220 | ReportHandler::warning(warning); |
|
221 | 221 | } |
|
222 | 222 | } |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | // Close the function if it has been opened |
|
226 | 226 | if (!first) { |
|
227 | 227 | s << " return NULL;" << endl |
|
228 | 228 | << "}" << endl; |
|
229 | 229 | } |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | return handlers; |
|
233 | 233 | } |
General Comments 0
You need to be logged in to leave comments.
Login now