|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
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 |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
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 |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
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. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
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. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cppwriteincludes.h" |
|
43 #include "driver.h" |
|
44 #include "ui4.h" |
|
45 #include "uic.h" |
|
46 #include "databaseinfo.h" |
|
47 |
|
48 #include <QtCore/QDebug> |
|
49 #include <QtCore/QFileInfo> |
|
50 #include <QtCore/QTextStream> |
|
51 |
|
52 #include <stdio.h> |
|
53 |
|
54 QT_BEGIN_NAMESPACE |
|
55 |
|
56 enum { debugWriteIncludes = 0 }; |
|
57 enum { warnHeaderGeneration = 0 }; |
|
58 |
|
59 struct ClassInfoEntry |
|
60 { |
|
61 const char *klass; |
|
62 const char *module; |
|
63 const char *header; |
|
64 }; |
|
65 |
|
66 static const ClassInfoEntry qclass_lib_map[] = { |
|
67 #define QT_CLASS_LIB(klass, module, header) { #klass, #module, #header }, |
|
68 #include "qclass_lib_map.h" |
|
69 |
|
70 #undef QT_CLASS_LIB |
|
71 }; |
|
72 |
|
73 // Format a module header as 'QtCore/QObject' |
|
74 static inline QString moduleHeader(const QString &module, const QString &header) |
|
75 { |
|
76 QString rc = module; |
|
77 rc += QLatin1Char('/'); |
|
78 rc += header; |
|
79 return rc; |
|
80 } |
|
81 |
|
82 namespace CPP { |
|
83 |
|
84 WriteIncludes::WriteIncludes(Uic *uic) |
|
85 : m_uic(uic), m_output(uic->output()), m_scriptsActivated(false) |
|
86 { |
|
87 // When possible (no namespace) use the "QtModule/QClass" convention |
|
88 // and create a re-mapping of the old header "qclass.h" to it. Do not do this |
|
89 // for the "Phonon::Someclass" classes, however. |
|
90 const QString namespaceDelimiter = QLatin1String("::"); |
|
91 const ClassInfoEntry *classLibEnd = qclass_lib_map + sizeof(qclass_lib_map)/sizeof(ClassInfoEntry); |
|
92 for(const ClassInfoEntry *it = qclass_lib_map; it < classLibEnd; ++it) { |
|
93 const QString klass = QLatin1String(it->klass); |
|
94 const QString module = QLatin1String(it->module); |
|
95 QLatin1String header = QLatin1String(it->header); |
|
96 if (klass.contains(namespaceDelimiter)) { |
|
97 m_classToHeader.insert(klass, moduleHeader(module, header)); |
|
98 } else { |
|
99 const QString newHeader = moduleHeader(module, klass); |
|
100 m_classToHeader.insert(klass, newHeader); |
|
101 m_oldHeaderToNewHeader.insert(header, newHeader); |
|
102 } |
|
103 } |
|
104 } |
|
105 |
|
106 void WriteIncludes::acceptUI(DomUI *node) |
|
107 { |
|
108 m_scriptsActivated = false; |
|
109 m_localIncludes.clear(); |
|
110 m_globalIncludes.clear(); |
|
111 m_knownClasses.clear(); |
|
112 m_includeBaseNames.clear(); |
|
113 |
|
114 if (node->elementIncludes()) |
|
115 acceptIncludes(node->elementIncludes()); |
|
116 |
|
117 if (node->elementCustomWidgets()) |
|
118 TreeWalker::acceptCustomWidgets(node->elementCustomWidgets()); |
|
119 |
|
120 add(QLatin1String("QApplication")); |
|
121 add(QLatin1String("QVariant")); |
|
122 add(QLatin1String("QAction")); |
|
123 |
|
124 add(QLatin1String("QButtonGroup")); // ### only if it is really necessary |
|
125 add(QLatin1String("QHeaderView")); |
|
126 |
|
127 if (m_uic->hasExternalPixmap() && m_uic->pixmapFunction() == QLatin1String("qPixmapFromMimeSource")) { |
|
128 #ifdef QT_NO_QT3_SUPPORT |
|
129 qWarning("Warning: The form file has external pixmaps or qPixmapFromMimeSource() set as a pixmap function. " |
|
130 "This requires Qt 3 support, which is disabled. The resulting code will not compile."); |
|
131 #endif |
|
132 add(QLatin1String("Q3MimeSourceFactory")); |
|
133 } |
|
134 |
|
135 if (m_uic->databaseInfo()->connections().size()) { |
|
136 add(QLatin1String("QSqlDatabase")); |
|
137 add(QLatin1String("Q3SqlCursor")); |
|
138 add(QLatin1String("QSqlRecord")); |
|
139 add(QLatin1String("Q3SqlForm")); |
|
140 } |
|
141 |
|
142 TreeWalker::acceptUI(node); |
|
143 |
|
144 writeHeaders(m_globalIncludes, true); |
|
145 writeHeaders(m_localIncludes, false); |
|
146 |
|
147 m_output << QLatin1Char('\n'); |
|
148 } |
|
149 |
|
150 void WriteIncludes::acceptWidget(DomWidget *node) |
|
151 { |
|
152 if (debugWriteIncludes) |
|
153 fprintf(stderr, "%s '%s'\n", Q_FUNC_INFO, qPrintable(node->attributeClass())); |
|
154 |
|
155 add(node->attributeClass()); |
|
156 TreeWalker::acceptWidget(node); |
|
157 } |
|
158 |
|
159 void WriteIncludes::acceptLayout(DomLayout *node) |
|
160 { |
|
161 add(node->attributeClass()); |
|
162 TreeWalker::acceptLayout(node); |
|
163 } |
|
164 |
|
165 void WriteIncludes::acceptSpacer(DomSpacer *node) |
|
166 { |
|
167 add(QLatin1String("QSpacerItem")); |
|
168 TreeWalker::acceptSpacer(node); |
|
169 } |
|
170 |
|
171 void WriteIncludes::acceptProperty(DomProperty *node) |
|
172 { |
|
173 if (node->kind() == DomProperty::Date) |
|
174 add(QLatin1String("QDate")); |
|
175 if (node->kind() == DomProperty::Locale) |
|
176 add(QLatin1String("QLocale")); |
|
177 TreeWalker::acceptProperty(node); |
|
178 } |
|
179 |
|
180 void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global) |
|
181 { |
|
182 if (debugWriteIncludes) |
|
183 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global); |
|
184 |
|
185 do { |
|
186 if (!header.isEmpty()) |
|
187 break; |
|
188 |
|
189 // Known class |
|
190 const StringMap::const_iterator it = m_classToHeader.constFind(className); |
|
191 if (it != m_classToHeader.constEnd()) { |
|
192 header = it.value(); |
|
193 global = true; |
|
194 break; |
|
195 } |
|
196 |
|
197 // Quick check by class name to detect includehints provided for custom widgets. |
|
198 // Remove namespaces |
|
199 QString lowerClassName = className.toLower(); |
|
200 static const QString namespaceSeparator = QLatin1String("::"); |
|
201 const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator); |
|
202 if (namespaceIndex != -1) |
|
203 lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size()); |
|
204 if (m_includeBaseNames.contains(lowerClassName)) { |
|
205 header.clear(); |
|
206 break; |
|
207 } |
|
208 |
|
209 // Last resort: Create default header |
|
210 if (!m_uic->option().implicitIncludes) |
|
211 break; |
|
212 header = lowerClassName; |
|
213 header += QLatin1String(".h"); |
|
214 if (warnHeaderGeneration) { |
|
215 qWarning("Warning: generated header '%s' for class '%s'.", qPrintable(header), |
|
216 qPrintable(className)); |
|
217 |
|
218 } |
|
219 |
|
220 global = true; |
|
221 } while (false); |
|
222 |
|
223 if (!header.isEmpty()) |
|
224 insertInclude(header, global); |
|
225 } |
|
226 |
|
227 void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global) |
|
228 { |
|
229 if (debugWriteIncludes) |
|
230 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global); |
|
231 |
|
232 if (className.isEmpty() || m_knownClasses.contains(className)) |
|
233 return; |
|
234 |
|
235 m_knownClasses.insert(className); |
|
236 |
|
237 if (className == QLatin1String("Line")) { // ### hmm, deprecate me! |
|
238 add(QLatin1String("QFrame")); |
|
239 return; |
|
240 } |
|
241 |
|
242 if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView")) || |
|
243 m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) { |
|
244 add(QLatin1String("Q3Header")); |
|
245 } |
|
246 if (determineHeader) |
|
247 insertIncludeForClass(className, header, global); |
|
248 } |
|
249 |
|
250 void WriteIncludes::acceptCustomWidget(DomCustomWidget *node) |
|
251 { |
|
252 const QString className = node->elementClass(); |
|
253 if (className.isEmpty()) |
|
254 return; |
|
255 |
|
256 if (const DomScript *domScript = node->elementScript()) |
|
257 if (!domScript->text().isEmpty()) |
|
258 activateScripts(); |
|
259 |
|
260 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) { |
|
261 add(className, false); // no header specified |
|
262 } else { |
|
263 // custom header unless it is a built-in qt class |
|
264 QString header; |
|
265 bool global = false; |
|
266 if (!m_classToHeader.contains(className)) { |
|
267 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global"); |
|
268 header = node->elementHeader()->text(); |
|
269 } |
|
270 add(className, true, header, global); |
|
271 } |
|
272 } |
|
273 |
|
274 void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node) |
|
275 { |
|
276 Q_UNUSED(node); |
|
277 } |
|
278 |
|
279 void WriteIncludes::acceptIncludes(DomIncludes *node) |
|
280 { |
|
281 TreeWalker::acceptIncludes(node); |
|
282 } |
|
283 |
|
284 void WriteIncludes::acceptInclude(DomInclude *node) |
|
285 { |
|
286 bool global = true; |
|
287 if (node->hasAttributeLocation()) |
|
288 global = node->attributeLocation() == QLatin1String("global"); |
|
289 insertInclude(node->text(), global); |
|
290 } |
|
291 |
|
292 void WriteIncludes::insertInclude(const QString &header, bool global) |
|
293 { |
|
294 if (debugWriteIncludes) |
|
295 fprintf(stderr, "%s %s %d\n", Q_FUNC_INFO, qPrintable(header), global); |
|
296 |
|
297 OrderedSet &includes = global ? m_globalIncludes : m_localIncludes; |
|
298 if (includes.contains(header)) |
|
299 return; |
|
300 // Insert. Also remember base name for quick check of suspicious custom plugins |
|
301 includes.insert(header, false); |
|
302 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower(); |
|
303 m_includeBaseNames.insert(lowerBaseName); |
|
304 } |
|
305 |
|
306 void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global) |
|
307 { |
|
308 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"'); |
|
309 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"'); |
|
310 |
|
311 // Check for the old headers 'qslider.h' and replace by 'QtGui/QSlider' |
|
312 const OrderedSet::const_iterator cend = headers.constEnd(); |
|
313 for (OrderedSet::const_iterator sit = headers.constBegin(); sit != cend; ++sit) { |
|
314 const StringMap::const_iterator hit = m_oldHeaderToNewHeader.constFind(sit.key()); |
|
315 const bool mapped = hit != m_oldHeaderToNewHeader.constEnd(); |
|
316 const QString header = mapped ? hit.value() : sit.key(); |
|
317 if (!header.trimmed().isEmpty()) { |
|
318 m_output << "#include " << openingQuote << header << closingQuote << QLatin1Char('\n'); |
|
319 } |
|
320 } |
|
321 } |
|
322 |
|
323 void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &) |
|
324 { |
|
325 if (!scripts.empty()) { |
|
326 activateScripts(); |
|
327 } |
|
328 } |
|
329 |
|
330 void WriteIncludes::activateScripts() |
|
331 { |
|
332 if (!m_scriptsActivated) { |
|
333 add(QLatin1String("QScriptEngine")); |
|
334 add(QLatin1String("QDebug")); |
|
335 m_scriptsActivated = true; |
|
336 } |
|
337 } |
|
338 } // namespace CPP |
|
339 |
|
340 QT_END_NAMESPACE |