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

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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("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
150void 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
159void WriteIncludes::acceptLayout(DomLayout *node)
160{
161 add(node->attributeClass());
162 TreeWalker::acceptLayout(node);
163}
164
165void WriteIncludes::acceptSpacer(DomSpacer *node)
166{
167 add(QLatin1String("QSpacerItem"));
168 TreeWalker::acceptSpacer(node);
169}
170
171void 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
180void 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
227void 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
250void 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
274void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node)
275{
276 Q_UNUSED(node);
277}
278
279void WriteIncludes::acceptIncludes(DomIncludes *node)
280{
281 TreeWalker::acceptIncludes(node);
282}
283
284void 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
292void 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
306void 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
323void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &)
324{
325 if (!scripts.empty()) {
326 activateScripts();
327 }
328}
329
330void 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
340QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.