##// END OF EJS Templates
fixed builtin package name...
florianlink -
r94:f55700c8b936
parent child
Show More
@@ -1,233 +1,233
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
6 **
7 ** This file is part of the Qt Script Generator project on Qt Labs.
7 ** This file is part of the Qt Script Generator project on Qt Labs.
8 **
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
14 ** this package.
15 **
15 **
16 ** GNU Lesser General Public License Usage
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
27 **
28 ** If you have questions regarding the use of this file, please contact
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
29 ** Nokia at qt-info@nokia.com.
30 **
30 **
31 **
31 **
32 **
32 **
33 **
33 **
34 **
34 **
35 **
35 **
36 **
36 **
37 **
37 **
38 ** $QT_END_LICENSE$
38 ** $QT_END_LICENSE$
39 **
39 **
40 ****************************************************************************/
40 ****************************************************************************/
41
41
42 #include "setupgenerator.h"
42 #include "setupgenerator.h"
43 #include "shellgenerator.h"
43 #include "shellgenerator.h"
44 #include "reporthandler.h"
44 #include "reporthandler.h"
45 #include "fileout.h"
45 #include "fileout.h"
46
46
47 //#define Q_SCRIPT_LAZY_GENERATOR
47 //#define Q_SCRIPT_LAZY_GENERATOR
48
48
49 void SetupGenerator::addClass(const QString& package, const AbstractMetaClass *cls)
49 void SetupGenerator::addClass(const QString& package, const AbstractMetaClass *cls)
50 {
50 {
51 packHash[package].append(cls);
51 packHash[package].append(cls);
52 }
52 }
53
53
54 void maybeDeclareMetaType(QTextStream &stream, const QString &typeName,
54 void maybeDeclareMetaType(QTextStream &stream, const QString &typeName,
55 QSet<QString> &registeredTypeNames);
55 QSet<QString> &registeredTypeNames);
56 bool hasDefaultConstructor(const AbstractMetaClass *meta_class);
56 bool hasDefaultConstructor(const AbstractMetaClass *meta_class);
57
57
58 void SetupGenerator::generate()
58 void SetupGenerator::generate()
59 {
59 {
60 AbstractMetaClassList classes_with_polymorphic_id;
60 AbstractMetaClassList classes_with_polymorphic_id;
61 {
61 {
62 QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
62 QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
63 while (pack.hasNext()) {
63 while (pack.hasNext()) {
64 pack.next();
64 pack.next();
65 QList<const AbstractMetaClass*> list = pack.value();
65 QList<const AbstractMetaClass*> list = pack.value();
66 foreach (const AbstractMetaClass *cls, list) {
66 foreach (const AbstractMetaClass *cls, list) {
67 if (cls->typeEntry()->isPolymorphicBase()) {
67 if (cls->typeEntry()->isPolymorphicBase()) {
68 classes_with_polymorphic_id.append((AbstractMetaClass*)cls);
68 classes_with_polymorphic_id.append((AbstractMetaClass*)cls);
69 }
69 }
70 }
70 }
71 }
71 }
72 }
72 }
73
73
74 QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
74 QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
75 while (pack.hasNext()) {
75 while (pack.hasNext()) {
76 pack.next();
76 pack.next();
77 QList<const AbstractMetaClass*> list = pack.value();
77 QList<const AbstractMetaClass*> list = pack.value();
78 if (list.isEmpty())
78 if (list.isEmpty())
79 continue;
79 continue;
80
80
81 QString packKey = pack.key();
81 QString packKey = pack.key();
82 QString packName = pack.key();
82 QString packName = pack.key();
83 QStringList components = packName.split("_");
83 QStringList components = packName.split("_");
84 if ((components.size() > 2) && (components.at(0) == "com")
84 if ((components.size() > 2) && (components.at(0) == "com")
85 && (components.at(1) == "trolltech")) {
85 && (components.at(1) == "trolltech")) {
86 // kill com.trolltech in key
86 // kill com.trolltech in key
87 components.removeAt(0);
87 components.removeAt(0);
88 components.removeAt(0);
88 components.removeAt(0);
89 }
89 }
90
90
91 QString shortPackName;
91 QString shortPackName;
92 foreach (QString comp, components) {
92 foreach (QString comp, components) {
93 comp[0] = comp[0].toUpper();
93 comp[0] = comp[0].toUpper();
94 shortPackName += comp;
94 shortPackName += comp;
95 }
95 }
96 // add missing camel case (workaround..)
96 // add missing camel case (workaround..)
97 if (shortPackName == "QtWebkit") {
97 if (shortPackName == "QtWebkit") {
98 shortPackName = "QtWebKit";
98 shortPackName = "QtWebKit";
99 } else if (shortPackName == "QtXmlpatterns") {
99 } else if (shortPackName == "QtXmlpatterns") {
100 shortPackName = "QtXmlPatterns";
100 shortPackName = "QtXmlPatterns";
101 } else if (shortPackName == "QtOpengl") {
101 } else if (shortPackName == "QtOpengl") {
102 shortPackName = "QtOpenGL";
102 shortPackName = "QtOpenGL";
103 } else if (shortPackName == "QtUitools") {
103 } else if (shortPackName == "QtUitools") {
104 shortPackName = "QtUiTools";
104 shortPackName = "QtUiTools";
105 }
105 }
106
106
107
107
108 {
108 {
109 FileOut initFile(m_out_dir + "/generated_cpp/" + packName + "/" + packKey + "_init.cpp");
109 FileOut initFile(m_out_dir + "/generated_cpp/" + packName + "/" + packKey + "_init.cpp");
110 QTextStream &s = initFile.stream;
110 QTextStream &s = initFile.stream;
111
111
112 s << "#include <PythonQt.h>" << endl;
112 s << "#include <PythonQt.h>" << endl;
113
113
114 for (int i=0; i<(list.count()+MAX_CLASSES_PER_FILE-1) / MAX_CLASSES_PER_FILE; i++) {
114 for (int i=0; i<(list.count()+MAX_CLASSES_PER_FILE-1) / MAX_CLASSES_PER_FILE; i++) {
115 s << "#include \"" << packKey << QString::number(i) << ".h\"" << endl;
115 s << "#include \"" << packKey << QString::number(i) << ".h\"" << endl;
116 }
116 }
117 s << endl;
117 s << endl;
118
118
119 QStringList polymorphicHandlers;
119 QStringList polymorphicHandlers;
120 if (!packName.endsWith("_builtin")) {
120 if (!packName.endsWith("_builtin")) {
121 polymorphicHandlers = writePolymorphicHandler(s, list.at(0)->package(), classes_with_polymorphic_id);
121 polymorphicHandlers = writePolymorphicHandler(s, list.at(0)->package(), classes_with_polymorphic_id);
122 s << endl;
122 s << endl;
123 }
123 }
124
124
125 // declare individual class creation functions
125 // declare individual class creation functions
126 s << "void PythonQt_init_" << shortPackName << "() {" << endl;
126 s << "void PythonQt_init_" << shortPackName << "() {" << endl;
127
127
128 if (shortPackName.endsWith("Builtin")) {
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 QStringList cppClassNames;
132 QStringList cppClassNames;
133 foreach (const AbstractMetaClass *cls, list) {
133 foreach (const AbstractMetaClass *cls, list) {
134
134
135 QString shellCreator;
135 QString shellCreator;
136 if (cls->generateShellClass()) {
136 if (cls->generateShellClass()) {
137 shellCreator = ", PythonQtSetInstanceWrapperOnShell<" + ShellGenerator::shellClassName(cls) + ">";
137 shellCreator = ", PythonQtSetInstanceWrapperOnShell<" + ShellGenerator::shellClassName(cls) + ">";
138 }
138 }
139 if (cls->isQObject()) {
139 if (cls->isQObject()) {
140 s << "PythonQt::self()->registerClass(&" << cls->qualifiedCppName() << "::staticMetaObject, \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl;
140 s << "PythonQt::self()->registerClass(&" << cls->qualifiedCppName() << "::staticMetaObject, \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl;
141 } else {
141 } else {
142 QString baseName = cls->baseClass()?cls->baseClass()->qualifiedCppName():"";
142 QString baseName = cls->baseClass()?cls->baseClass()->qualifiedCppName():"";
143 s << "PythonQt::self()->registerCPPClass(\""<< cls->qualifiedCppName() << "\", \"" << baseName << "\", \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl;
143 s << "PythonQt::self()->registerCPPClass(\""<< cls->qualifiedCppName() << "\", \"" << baseName << "\", \"" << shortPackName <<"\", PythonQtCreateObject<PythonQtWrapper_" << cls->name() << ">" << shellCreator << ");" << endl;
144 }
144 }
145 foreach(AbstractMetaClass* interface, cls->interfaces()) {
145 foreach(AbstractMetaClass* interface, cls->interfaces()) {
146 // the interface might be our own class... (e.g. QPaintDevice)
146 // the interface might be our own class... (e.g. QPaintDevice)
147 if (interface->qualifiedCppName() != cls->qualifiedCppName()) {
147 if (interface->qualifiedCppName() != cls->qualifiedCppName()) {
148 s << "PythonQt::self()->addParentClass(\""<< cls->qualifiedCppName() << "\", \"" << interface->qualifiedCppName() << "\",PythonQtUpcastingOffset<" << cls->qualifiedCppName() <<","<<interface->qualifiedCppName()<<">());" << endl;
148 s << "PythonQt::self()->addParentClass(\""<< cls->qualifiedCppName() << "\", \"" << interface->qualifiedCppName() << "\",PythonQtUpcastingOffset<" << cls->qualifiedCppName() <<","<<interface->qualifiedCppName()<<">());" << endl;
149 }
149 }
150 }
150 }
151 }
151 }
152 s << endl;
152 s << endl;
153 foreach (QString handler, polymorphicHandlers) {
153 foreach (QString handler, polymorphicHandlers) {
154 s << "PythonQt::self()->addPolymorphicHandler(\""<< handler << "\", polymorphichandler_" << handler << ");" << endl;
154 s << "PythonQt::self()->addPolymorphicHandler(\""<< handler << "\", polymorphichandler_" << handler << ");" << endl;
155 }
155 }
156
156
157 s << "}";
157 s << "}";
158 s << endl;
158 s << endl;
159 }
159 }
160 }
160 }
161 }
161 }
162
162
163 QStringList SetupGenerator::writePolymorphicHandler(QTextStream &s, const QString &package,
163 QStringList SetupGenerator::writePolymorphicHandler(QTextStream &s, const QString &package,
164 const AbstractMetaClassList &classes)
164 const AbstractMetaClassList &classes)
165 {
165 {
166 QStringList handlers;
166 QStringList handlers;
167 foreach (AbstractMetaClass *cls, classes) {
167 foreach (AbstractMetaClass *cls, classes) {
168 const ComplexTypeEntry *centry = cls->typeEntry();
168 const ComplexTypeEntry *centry = cls->typeEntry();
169 if (!centry->isPolymorphicBase())
169 if (!centry->isPolymorphicBase())
170 continue;
170 continue;
171 bool isGraphicsItem = (cls->qualifiedCppName()=="QGraphicsItem");
171 bool isGraphicsItem = (cls->qualifiedCppName()=="QGraphicsItem");
172
172
173 AbstractMetaClassList classList = this->classes();
173 AbstractMetaClassList classList = this->classes();
174 bool first = true;
174 bool first = true;
175 foreach (AbstractMetaClass *clazz, classList) {
175 foreach (AbstractMetaClass *clazz, classList) {
176 bool inherits = false;
176 bool inherits = false;
177 if (isGraphicsItem) {
177 if (isGraphicsItem) {
178 foreach(AbstractMetaClass* interfaze, clazz->interfaces()) {
178 foreach(AbstractMetaClass* interfaze, clazz->interfaces()) {
179 if (interfaze->qualifiedCppName()=="QGraphicsItem") {
179 if (interfaze->qualifiedCppName()=="QGraphicsItem") {
180 inherits = true;
180 inherits = true;
181 break;
181 break;
182 }
182 }
183 }
183 }
184 } else {
184 } else {
185 inherits = clazz->inheritsFrom(cls);
185 inherits = clazz->inheritsFrom(cls);
186 }
186 }
187 if (clazz->package() == package && inherits) {
187 if (clazz->package() == package && inherits) {
188 if (!clazz->typeEntry()->polymorphicIdValue().isEmpty() || isGraphicsItem) {
188 if (!clazz->typeEntry()->polymorphicIdValue().isEmpty() || isGraphicsItem) {
189 // On first find, open the function
189 // On first find, open the function
190 if (first) {
190 if (first) {
191 first = false;
191 first = false;
192
192
193 QString handler = cls->name();
193 QString handler = cls->name();
194 handlers.append(handler);
194 handlers.append(handler);
195
195
196 s << "static void* polymorphichandler_" << handler
196 s << "static void* polymorphichandler_" << handler
197 << "(const void *ptr, char **class_name)" << endl
197 << "(const void *ptr, char **class_name)" << endl
198 << "{" << endl
198 << "{" << endl
199 << " Q_ASSERT(ptr != 0);" << endl
199 << " Q_ASSERT(ptr != 0);" << endl
200 << " " << cls->qualifiedCppName() << " *object = ("
200 << " " << cls->qualifiedCppName() << " *object = ("
201 << cls->qualifiedCppName() << " *)ptr;" << endl;
201 << cls->qualifiedCppName() << " *)ptr;" << endl;
202 }
202 }
203
203
204 // For each, add case label
204 // For each, add case label
205 QString polyId = clazz->typeEntry()->polymorphicIdValue();
205 QString polyId = clazz->typeEntry()->polymorphicIdValue();
206 if (isGraphicsItem) {
206 if (isGraphicsItem) {
207 polyId = "%1->type() == " + clazz->qualifiedCppName() + "::Type";
207 polyId = "%1->type() == " + clazz->qualifiedCppName() + "::Type";
208 }
208 }
209 s << " if ("
209 s << " if ("
210 << polyId.replace("%1", "object")
210 << polyId.replace("%1", "object")
211 << ") {" << endl
211 << ") {" << endl
212 << " *class_name = \"" << clazz->name() << "\";" << endl
212 << " *class_name = \"" << clazz->name() << "\";" << endl
213 << " return (" << clazz->qualifiedCppName() << "*)object;" << endl
213 << " return (" << clazz->qualifiedCppName() << "*)object;" << endl
214 << " }" << endl;
214 << " }" << endl;
215 } else {
215 } else {
216 QString warning = QString("class '%1' inherits from polymorphic class '%2', but has no polymorphic id set")
216 QString warning = QString("class '%1' inherits from polymorphic class '%2', but has no polymorphic id set")
217 .arg(clazz->name())
217 .arg(clazz->name())
218 .arg(cls->name());
218 .arg(cls->name());
219
219
220 ReportHandler::warning(warning);
220 ReportHandler::warning(warning);
221 }
221 }
222 }
222 }
223 }
223 }
224
224
225 // Close the function if it has been opened
225 // Close the function if it has been opened
226 if (!first) {
226 if (!first) {
227 s << " return NULL;" << endl
227 s << " return NULL;" << endl
228 << "}" << endl;
228 << "}" << endl;
229 }
229 }
230 }
230 }
231
231
232 return handlers;
232 return handlers;
233 }
233 }
General Comments 0
You need to be logged in to leave comments. Login now