Mercurial > projects > qtd
annotate tools/duic/d/dwriteincludes.cpp @ 391:4d17d96480db
Fixed generation of size policies ? QSizePolicy is a class.
author | David Nadlinger <code@klickverbot.at> |
---|---|
date | Fri, 27 Aug 2010 15:06:38 +0200 |
parents | d5a6b6269f44 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the tools applications of the Qt Toolkit. | |
7 ** | |
8 ** Commercial Usage | |
9 ** Licensees holding valid Qt Commercial licenses may use this file in | |
10 ** accordance with the Qt Commercial License Agreement provided with the | |
11 ** Software or, alternatively, in accordance with the terms contained in | |
12 ** a written agreement between you and Nokia. | |
13 ** | |
14 ** | |
15 ** GNU General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU | |
17 ** General Public License versions 2.0 or 3.0 as published by the Free | |
18 ** Software Foundation and appearing in the file LICENSE.GPL included in | |
19 ** the packaging of this file. Please review the following information | |
20 ** to ensure GNU General Public Licensing requirements will be met: | |
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
23 ** exception, Nokia gives you certain additional rights. These rights | |
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in | |
25 ** the file GPL_EXCEPTION.txt in this package. | |
26 ** | |
27 ** Qt for Windows(R) Licensees | |
28 ** As a special exception, Nokia, as the sole copyright holder for Qt | |
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the | |
30 ** right for the Qt/Eclipse Integration to link to functionality | |
31 ** provided by Qt Designer and its related libraries. | |
32 ** | |
33 ** If you are unsure which license is appropriate for your use, please | |
34 ** contact the sales department at qt-sales@nokia.com. | |
35 ** | |
36 ****************************************************************************/ | |
37 | |
38 #include "dwriteincludes.h" | |
39 #include "driver.h" | |
40 #include "ui4.h" | |
41 #include "uic.h" | |
42 #include "databaseinfo.h" | |
43 #include <QtCore/QDebug> | |
44 #include <QtCore/QFileInfo> | |
45 #include <QtCore/QTextStream> | |
46 | |
47 QT_BEGIN_NAMESPACE | |
48 | |
49 namespace { | |
50 enum { debugWriteIncludes = 0 }; | |
51 enum { warnHeaderGeneration = 0 }; | |
52 } | |
53 | |
54 namespace D { | |
55 | |
56 struct ClassInfoEntry | |
57 { | |
58 const char *klass; | |
59 const char *module; | |
60 const char *header; | |
61 }; | |
62 | |
63 static const ClassInfoEntry qclass_lib_map[] = { | |
64 #define QT_CLASS_LIB(klass, module, header) { #klass, #module, #header }, | |
65 #include "qclass_lib_map.h" | |
66 | |
67 #undef QT_CLASS_LIB | |
68 { 0, 0, 0 } | |
69 }; | |
70 | |
71 WriteIncludes::WriteIncludes(Uic *uic) : | |
72 m_uic(uic), | |
73 m_output(uic->output()), | |
74 m_scriptsActivated(false) | |
75 { | |
76 for(const ClassInfoEntry *it = &qclass_lib_map[0]; it->klass != 0; ++it) { | |
77 QString newHeader = QString("qt.%1").arg(QString(it->module).mid(2).toLower()); | |
78 newHeader += QLatin1Char('.'); | |
79 newHeader += QLatin1String(it->klass); | |
80 m_classToHeader.insert(QLatin1String(it->klass), newHeader); | |
81 m_oldHeaderToNewHeader.insert(QLatin1String(it->header), newHeader); | |
82 } | |
83 } | |
84 | |
85 void WriteIncludes::acceptUI(DomUI *node) | |
86 { | |
87 m_scriptsActivated = false; | |
88 m_localIncludes.clear(); | |
89 m_globalIncludes.clear(); | |
90 m_knownClasses.clear(); | |
91 m_includeBaseNames.clear(); | |
92 | |
93 if (node->elementIncludes()) | |
94 acceptIncludes(node->elementIncludes()); | |
95 | |
96 if (node->elementCustomWidgets()) | |
97 TreeWalker::acceptCustomWidgets(node->elementCustomWidgets()); | |
98 | |
99 add(QLatin1String("QApplication")); | |
100 add(QLatin1String("QVariant")); | |
101 add(QLatin1String("QAction")); | |
56
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
48
diff
changeset
|
102 add(QLatin1String("QString")); |
1 | 103 |
104 add(QLatin1String("QButtonGroup")); // ### only if it is really necessary | |
105 | |
106 if (m_uic->hasExternalPixmap() && m_uic->pixmapFunction() == QLatin1String("qPixmapFromMimeSource")) { | |
107 #ifdef QT_NO_QT3_SUPPORT | |
108 qWarning("Warning: The form file has external pixmaps or qPixmapFromMimeSource() set as a pixmap function. " | |
109 "This requires Qt 3 support, which is disabled. The resulting code will not compile."); | |
110 #endif | |
111 add(QLatin1String("qt.qt3support.Q3MimeSourceFactory")); | |
112 } | |
113 | |
114 if (m_uic->databaseInfo()->connections().size()) { | |
115 add(QLatin1String("QSqlDatabase")); | |
116 add(QLatin1String("Q3SqlCursor")); | |
117 add(QLatin1String("QSqlRecord")); | |
118 add(QLatin1String("Q3SqlForm")); | |
119 } | |
120 | |
121 TreeWalker::acceptUI(node); | |
122 | |
123 QString qualifiedClassName = node->elementClass(); | |
56
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
48
diff
changeset
|
124 m_output << "module ui_" << qualifiedClassName.toLower() << ";\n\n"; |
1 | 125 |
126 writeHeaders(m_globalIncludes, true); | |
127 writeHeaders(m_localIncludes, false); | |
128 | |
129 m_output << QLatin1Char('\n'); | |
130 } | |
131 | |
132 void WriteIncludes::acceptWidget(DomWidget *node) | |
133 { | |
134 add(QString("%1").arg(node->attributeClass())); | |
135 TreeWalker::acceptWidget(node); | |
136 } | |
137 | |
138 void WriteIncludes::acceptLayout(DomLayout *node) | |
139 { | |
140 add(QString("%1").arg(node->attributeClass())); | |
141 TreeWalker::acceptLayout(node); | |
142 } | |
143 | |
144 void WriteIncludes::acceptSpacer(DomSpacer *node) | |
145 { | |
146 add(QLatin1String("QSpacerItem")); | |
147 TreeWalker::acceptSpacer(node); | |
148 } | |
149 | |
150 void WriteIncludes::acceptProperty(DomProperty *node) | |
151 { | |
152 if (node->kind() == DomProperty::Date) | |
153 add(QLatin1String("QDate")); | |
154 if (node->kind() == DomProperty::Locale) | |
155 add(QLatin1String("QLocale")); | |
156 TreeWalker::acceptProperty(node); | |
157 } | |
158 | |
159 void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global) | |
160 { | |
161 if (debugWriteIncludes) | |
162 qDebug() << "WriteIncludes::insertIncludeForClass" << className << header << global; | |
163 | |
164 do { | |
165 if (!header.isEmpty()) | |
166 break; | |
167 | |
168 // Known class | |
169 const StringMap::const_iterator it = m_classToHeader.constFind(className); | |
170 if (it != m_classToHeader.constEnd()) { | |
171 header = it.value(); | |
172 global = true; | |
173 break; | |
174 } | |
175 | |
176 // Quick check by class name to detect includehints provided for custom widgets. | |
177 // Remove namespaces | |
178 QString lowerClassName = className; | |
179 static const QString namespaceSeparator = QLatin1String("."); | |
180 const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator); | |
181 if (namespaceIndex != -1) | |
182 lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size()); | |
183 if (m_includeBaseNames.contains(lowerClassName)) { | |
184 header.clear(); | |
185 break; | |
186 } | |
187 | |
188 // Last resort: Create default header | |
189 if (!m_uic->option().implicitIncludes) | |
190 break; | |
191 header = lowerClassName; | |
192 header += QLatin1String(""); | |
193 if (warnHeaderGeneration) { | |
194 qWarning("Warning: generated header '%s' for class '%s'.", qPrintable(header), | |
195 qPrintable(className)); | |
196 | |
197 } | |
198 | |
199 global = true; | |
200 } while (false); | |
201 | |
202 if (!header.isEmpty()) | |
203 insertInclude(header, global); | |
204 } | |
205 | |
206 void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global) | |
207 { | |
208 if (debugWriteIncludes) | |
209 qDebug() << "WriteIncludes::add" << className << header << global; | |
210 | |
211 if (className.isEmpty() || m_knownClasses.contains(className)) | |
212 return; | |
213 | |
214 m_knownClasses.insert(className); | |
215 | |
216 if (className == QLatin1String("Line")) { // ### hmm, deprecate me! | |
217 add(QLatin1String("qt.gui.QFrame")); | |
218 return; | |
219 } | |
220 | |
221 if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView")) || | |
222 m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) { | |
223 add(QLatin1String("qt.qt3support.Q3Header")); | |
224 } | |
225 if (determineHeader) | |
226 insertIncludeForClass(className, header, global); | |
227 } | |
228 | |
229 void WriteIncludes::acceptCustomWidget(DomCustomWidget *node) | |
230 { | |
231 const QString className = node->elementClass(); | |
232 if (className.isEmpty()) | |
233 return; | |
234 | |
235 if (const DomScript *domScript = node->elementScript()) | |
236 if (!domScript->text().isEmpty()) | |
237 activateScripts(); | |
238 | |
239 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) { | |
240 add(className, false); // no header specified | |
241 } else { | |
242 // custom header unless it is a built-in qt class | |
243 QString header; | |
244 bool global = false; | |
245 if (!m_classToHeader.contains(className)) { | |
246 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global"); | |
247 header = node->elementHeader()->text(); | |
248 } | |
249 add(className, true, header, global); | |
250 } | |
251 } | |
252 | |
253 void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node) | |
254 { | |
255 Q_UNUSED(node); | |
256 } | |
257 | |
258 void WriteIncludes::acceptIncludes(DomIncludes *node) | |
259 { | |
260 TreeWalker::acceptIncludes(node); | |
261 } | |
262 | |
263 void WriteIncludes::acceptInclude(DomInclude *node) | |
264 { | |
265 bool global = true; | |
266 if (node->hasAttributeLocation()) | |
267 global = node->attributeLocation() == QLatin1String("global"); | |
268 insertInclude(node->text(), global); | |
269 } | |
270 void WriteIncludes::insertInclude(const QString &header, bool global) | |
271 { | |
272 if (debugWriteIncludes) | |
273 qDebug() << "WriteIncludes::insertInclude" << header << global; | |
274 | |
275 OrderedSet &includes = global ? m_globalIncludes : m_localIncludes; | |
276 if (includes.contains(header)) | |
277 return; | |
278 // Insert. Also remember base name for quick check of suspicious custom plugins | |
279 includes.insert(header, false); | |
280 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower(); | |
281 m_includeBaseNames.insert(lowerBaseName); | |
282 } | |
283 | |
284 void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global) | |
285 { | |
286 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"'); | |
287 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"'); | |
288 | |
289 const OrderedSet::const_iterator cend = headers.constEnd(); | |
290 for ( OrderedSet::const_iterator sit = headers.constBegin(); sit != cend; ++sit) { | |
291 const StringMap::const_iterator hit = m_oldHeaderToNewHeader.constFind(sit.key()); | |
292 const bool mapped = hit != m_oldHeaderToNewHeader.constEnd(); | |
293 const QString header = mapped ? hit.value() : sit.key(); | |
294 if (!header.trimmed().isEmpty()) { | |
56
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
48
diff
changeset
|
295 m_output << "public import " << header << QLatin1Char(';') << QLatin1Char('\n'); |
1 | 296 } |
297 } | |
298 } | |
299 | |
300 void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &) | |
301 { | |
302 if (!scripts.empty()) { | |
303 activateScripts(); | |
304 } | |
305 } | |
306 | |
307 void WriteIncludes::activateScripts() | |
308 { | |
309 if (!m_scriptsActivated) { | |
310 add(QLatin1String("qt.script.QScriptEngine")); | |
311 add(QLatin1String("qt.core.QDebug")); | |
312 m_scriptsActivated = true; | |
313 } | |
314 } | |
315 } // namespace D | |
316 | |
317 QT_END_NAMESPACE |