source: trunk/src/tools/uic/cpp/cppwriteincludes.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 11.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
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
54QT_BEGIN_NAMESPACE
55
56enum { debugWriteIncludes = 0 };
57enum { warnHeaderGeneration = 0 };
58
59struct ClassInfoEntry
60{
61 const char *klass;
62 const char *module;
63 const char *header;
64};
65
66static 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'
74static 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
82namespace CPP {
83
84WriteIncludes::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
106void 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("%s: 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 qPrintable(m_uic->option().messagePrefix()));
132#endif
133 add(QLatin1String("Q3MimeSourceFactory"));
134 }
135
136 if (m_uic->databaseInfo()->connections().size()) {
137 add(QLatin1String("QSqlDatabase"));
138 add(QLatin1String("Q3SqlCursor"));
139 add(QLatin1String("QSqlRecord"));
140 add(QLatin1String("Q3SqlForm"));
141 }
142
143 TreeWalker::acceptUI(node);
144
145 writeHeaders(m_globalIncludes, true);
146 writeHeaders(m_localIncludes, false);
147
148 m_output << QLatin1Char('\n');
149}
150
151void WriteIncludes::acceptWidget(DomWidget *node)
152{
153 if (debugWriteIncludes)
154 fprintf(stderr, "%s '%s'\n", Q_FUNC_INFO, qPrintable(node->attributeClass()));
155
156 add(node->attributeClass());
157 TreeWalker::acceptWidget(node);
158}
159
160void WriteIncludes::acceptLayout(DomLayout *node)
161{
162 add(node->attributeClass());
163 TreeWalker::acceptLayout(node);
164}
165
166void WriteIncludes::acceptSpacer(DomSpacer *node)
167{
168 add(QLatin1String("QSpacerItem"));
169 TreeWalker::acceptSpacer(node);
170}
171
172void WriteIncludes::acceptProperty(DomProperty *node)
173{
174 if (node->kind() == DomProperty::Date)
175 add(QLatin1String("QDate"));
176 if (node->kind() == DomProperty::Locale)
177 add(QLatin1String("QLocale"));
178 TreeWalker::acceptProperty(node);
179}
180
181void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
182{
183 if (debugWriteIncludes)
184 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
185
186 do {
187 if (!header.isEmpty())
188 break;
189
190 // Known class
191 const StringMap::const_iterator it = m_classToHeader.constFind(className);
192 if (it != m_classToHeader.constEnd()) {
193 header = it.value();
194 global = true;
195 break;
196 }
197
198 // Quick check by class name to detect includehints provided for custom widgets.
199 // Remove namespaces
200 QString lowerClassName = className.toLower();
201 static const QString namespaceSeparator = QLatin1String("::");
202 const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
203 if (namespaceIndex != -1)
204 lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size());
205 if (m_includeBaseNames.contains(lowerClassName)) {
206 header.clear();
207 break;
208 }
209
210 // Last resort: Create default header
211 if (!m_uic->option().implicitIncludes)
212 break;
213 header = lowerClassName;
214 header += QLatin1String(".h");
215 if (warnHeaderGeneration) {
216 qWarning("%s: Warning: generated header '%s' for class '%s'.",
217 qPrintable(m_uic->option().messagePrefix()),
218 qPrintable(header), qPrintable(className));
219
220 }
221
222 global = true;
223 } while (false);
224
225 if (!header.isEmpty())
226 insertInclude(header, global);
227}
228
229void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global)
230{
231 if (debugWriteIncludes)
232 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
233
234 if (className.isEmpty() || m_knownClasses.contains(className))
235 return;
236
237 m_knownClasses.insert(className);
238
239 if (className == QLatin1String("Line")) { // ### hmm, deprecate me!
240 add(QLatin1String("QFrame"));
241 return;
242 }
243
244 if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView")) ||
245 m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {
246 add(QLatin1String("Q3Header"));
247 }
248 if (determineHeader)
249 insertIncludeForClass(className, header, global);
250}
251
252void WriteIncludes::acceptCustomWidget(DomCustomWidget *node)
253{
254 const QString className = node->elementClass();
255 if (className.isEmpty())
256 return;
257
258 if (const DomScript *domScript = node->elementScript())
259 if (!domScript->text().isEmpty())
260 activateScripts();
261
262 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) {
263 add(className, false); // no header specified
264 } else {
265 // custom header unless it is a built-in qt class
266 QString header;
267 bool global = false;
268 if (!m_classToHeader.contains(className)) {
269 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global");
270 header = node->elementHeader()->text();
271 }
272 add(className, true, header, global);
273 }
274}
275
276void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node)
277{
278 Q_UNUSED(node);
279}
280
281void WriteIncludes::acceptIncludes(DomIncludes *node)
282{
283 TreeWalker::acceptIncludes(node);
284}
285
286void WriteIncludes::acceptInclude(DomInclude *node)
287{
288 bool global = true;
289 if (node->hasAttributeLocation())
290 global = node->attributeLocation() == QLatin1String("global");
291 insertInclude(node->text(), global);
292}
293
294void WriteIncludes::insertInclude(const QString &header, bool global)
295{
296 if (debugWriteIncludes)
297 fprintf(stderr, "%s %s %d\n", Q_FUNC_INFO, qPrintable(header), global);
298
299 OrderedSet &includes = global ? m_globalIncludes : m_localIncludes;
300 if (includes.contains(header))
301 return;
302 // Insert. Also remember base name for quick check of suspicious custom plugins
303 includes.insert(header, false);
304 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower();
305 m_includeBaseNames.insert(lowerBaseName);
306}
307
308void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global)
309{
310 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"');
311 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"');
312
313 // Check for the old headers 'qslider.h' and replace by 'QtGui/QSlider'
314 const OrderedSet::const_iterator cend = headers.constEnd();
315 for (OrderedSet::const_iterator sit = headers.constBegin(); sit != cend; ++sit) {
316 const StringMap::const_iterator hit = m_oldHeaderToNewHeader.constFind(sit.key());
317 const bool mapped = hit != m_oldHeaderToNewHeader.constEnd();
318 const QString header = mapped ? hit.value() : sit.key();
319 if (!header.trimmed().isEmpty()) {
320 m_output << "#include " << openingQuote << header << closingQuote << QLatin1Char('\n');
321 }
322 }
323}
324
325void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &)
326{
327 if (!scripts.empty()) {
328 activateScripts();
329 }
330}
331
332void WriteIncludes::activateScripts()
333{
334 if (!m_scriptsActivated) {
335 add(QLatin1String("QScriptEngine"));
336 add(QLatin1String("QDebug"));
337 m_scriptsActivated = true;
338 }
339}
340} // namespace CPP
341
342QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.