comparison tools/duic/d/dwriteincludes.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children 2085c2157b50
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
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"));
102
103 add(QLatin1String("QButtonGroup")); // ### only if it is really necessary
104
105 if (m_uic->hasExternalPixmap() && m_uic->pixmapFunction() == QLatin1String("qPixmapFromMimeSource")) {
106 #ifdef QT_NO_QT3_SUPPORT
107 qWarning("Warning: The form file has external pixmaps or qPixmapFromMimeSource() set as a pixmap function. "
108 "This requires Qt 3 support, which is disabled. The resulting code will not compile.");
109 #endif
110 add(QLatin1String("qt.qt3support.Q3MimeSourceFactory"));
111 }
112
113 if (m_uic->databaseInfo()->connections().size()) {
114 add(QLatin1String("QSqlDatabase"));
115 add(QLatin1String("Q3SqlCursor"));
116 add(QLatin1String("QSqlRecord"));
117 add(QLatin1String("Q3SqlForm"));
118 }
119
120 TreeWalker::acceptUI(node);
121
122 QString qualifiedClassName = node->elementClass();
123 m_output << "module ui." << qualifiedClassName << "\n\n";
124
125 writeHeaders(m_globalIncludes, true);
126 writeHeaders(m_localIncludes, false);
127
128 m_output << QLatin1Char('\n');
129 }
130
131 void WriteIncludes::acceptWidget(DomWidget *node)
132 {
133 add(QString("%1").arg(node->attributeClass()));
134 TreeWalker::acceptWidget(node);
135 }
136
137 void WriteIncludes::acceptLayout(DomLayout *node)
138 {
139 add(QString("%1").arg(node->attributeClass()));
140 TreeWalker::acceptLayout(node);
141 }
142
143 void WriteIncludes::acceptSpacer(DomSpacer *node)
144 {
145 add(QLatin1String("QSpacerItem"));
146 TreeWalker::acceptSpacer(node);
147 }
148
149 void WriteIncludes::acceptProperty(DomProperty *node)
150 {
151 if (node->kind() == DomProperty::Date)
152 add(QLatin1String("QDate"));
153 if (node->kind() == DomProperty::Locale)
154 add(QLatin1String("QLocale"));
155 TreeWalker::acceptProperty(node);
156 }
157
158 void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
159 {
160 if (debugWriteIncludes)
161 qDebug() << "WriteIncludes::insertIncludeForClass" << className << header << global;
162
163 do {
164 if (!header.isEmpty())
165 break;
166
167 // Known class
168 const StringMap::const_iterator it = m_classToHeader.constFind(className);
169 if (it != m_classToHeader.constEnd()) {
170 header = it.value();
171 global = true;
172 break;
173 }
174
175 // Quick check by class name to detect includehints provided for custom widgets.
176 // Remove namespaces
177 QString lowerClassName = className;
178 static const QString namespaceSeparator = QLatin1String(".");
179 const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
180 if (namespaceIndex != -1)
181 lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size());
182 if (m_includeBaseNames.contains(lowerClassName)) {
183 header.clear();
184 break;
185 }
186
187 // Last resort: Create default header
188 if (!m_uic->option().implicitIncludes)
189 break;
190 header = lowerClassName;
191 header += QLatin1String("");
192 if (warnHeaderGeneration) {
193 qWarning("Warning: generated header '%s' for class '%s'.", qPrintable(header),
194 qPrintable(className));
195
196 }
197
198 global = true;
199 } while (false);
200
201 if (!header.isEmpty())
202 insertInclude(header, global);
203 }
204
205 void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global)
206 {
207 if (debugWriteIncludes)
208 qDebug() << "WriteIncludes::add" << className << header << global;
209
210 if (className.isEmpty() || m_knownClasses.contains(className))
211 return;
212
213 m_knownClasses.insert(className);
214
215 if (className == QLatin1String("Line")) { // ### hmm, deprecate me!
216 add(QLatin1String("qt.gui.QFrame"));
217 return;
218 }
219
220 if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView")) ||
221 m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {
222 add(QLatin1String("qt.qt3support.Q3Header"));
223 }
224 if (determineHeader)
225 insertIncludeForClass(className, header, global);
226 }
227
228 void WriteIncludes::acceptCustomWidget(DomCustomWidget *node)
229 {
230 const QString className = node->elementClass();
231 if (className.isEmpty())
232 return;
233
234 if (const DomScript *domScript = node->elementScript())
235 if (!domScript->text().isEmpty())
236 activateScripts();
237
238 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) {
239 add(className, false); // no header specified
240 } else {
241 // custom header unless it is a built-in qt class
242 QString header;
243 bool global = false;
244 if (!m_classToHeader.contains(className)) {
245 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global");
246 header = node->elementHeader()->text();
247 }
248 add(className, true, header, global);
249 }
250 }
251
252 void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node)
253 {
254 Q_UNUSED(node);
255 }
256
257 void WriteIncludes::acceptIncludes(DomIncludes *node)
258 {
259 TreeWalker::acceptIncludes(node);
260 }
261
262 void WriteIncludes::acceptInclude(DomInclude *node)
263 {
264 bool global = true;
265 if (node->hasAttributeLocation())
266 global = node->attributeLocation() == QLatin1String("global");
267 insertInclude(node->text(), global);
268 }
269 void WriteIncludes::insertInclude(const QString &header, bool global)
270 {
271 if (debugWriteIncludes)
272 qDebug() << "WriteIncludes::insertInclude" << header << global;
273
274 OrderedSet &includes = global ? m_globalIncludes : m_localIncludes;
275 if (includes.contains(header))
276 return;
277 // Insert. Also remember base name for quick check of suspicious custom plugins
278 includes.insert(header, false);
279 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower();
280 m_includeBaseNames.insert(lowerBaseName);
281 }
282
283 void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global)
284 {
285 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"');
286 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"');
287
288 const OrderedSet::const_iterator cend = headers.constEnd();
289 for ( OrderedSet::const_iterator sit = headers.constBegin(); sit != cend; ++sit) {
290 const StringMap::const_iterator hit = m_oldHeaderToNewHeader.constFind(sit.key());
291 const bool mapped = hit != m_oldHeaderToNewHeader.constEnd();
292 const QString header = mapped ? hit.value() : sit.key();
293 if (!header.trimmed().isEmpty()) {
294 m_output << "import " << header << QLatin1Char(';') << QLatin1Char('\n');
295 }
296 }
297 }
298
299 void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &)
300 {
301 if (!scripts.empty()) {
302 activateScripts();
303 }
304 }
305
306 void WriteIncludes::activateScripts()
307 {
308 if (!m_scriptsActivated) {
309 add(QLatin1String("qt.script.QScriptEngine"));
310 add(QLatin1String("qt.core.QDebug"));
311 m_scriptsActivated = true;
312 }
313 }
314 } // namespace D
315
316 QT_END_NAMESPACE