1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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'
moduleHeader(const QString & module,const QString & header)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
WriteIncludes(Uic * uic)84 WriteIncludes::WriteIncludes(Uic *uic)
85 : m_uic(uic), m_output(uic->output()), m_scriptsActivated(false), m_laidOut(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
acceptUI(DomUI * node)106 void WriteIncludes::acceptUI(DomUI *node)
107 {
108 m_scriptsActivated = false;
109 m_laidOut = false;
110 m_localIncludes.clear();
111 m_globalIncludes.clear();
112 m_knownClasses.clear();
113 m_includeBaseNames.clear();
114
115 if (node->elementIncludes())
116 acceptIncludes(node->elementIncludes());
117
118 if (node->elementCustomWidgets())
119 TreeWalker::acceptCustomWidgets(node->elementCustomWidgets());
120
121 add(QLatin1String("QApplication"));
122 add(QLatin1String("QVariant"));
123 add(QLatin1String("QAction"));
124
125 add(QLatin1String("QButtonGroup")); // ### only if it is really necessary
126 add(QLatin1String("QHeaderView"));
127
128 if (m_uic->hasExternalPixmap() && m_uic->pixmapFunction() == QLatin1String("qPixmapFromMimeSource")) {
129 #ifdef QT_NO_QT3_SUPPORT
130 qWarning("%s: Warning: The form file has external pixmaps or qPixmapFromMimeSource() set as a pixmap function. "
131 "This requires Qt 3 support, which is disabled. The resulting code will not compile.",
132 qPrintable(m_uic->option().messagePrefix()));
133 #endif
134 add(QLatin1String("Q3MimeSourceFactory"));
135 }
136
137 if (m_uic->databaseInfo()->connections().size()) {
138 add(QLatin1String("QSqlDatabase"));
139 add(QLatin1String("Q3SqlCursor"));
140 add(QLatin1String("QSqlRecord"));
141 add(QLatin1String("Q3SqlForm"));
142 }
143
144 TreeWalker::acceptUI(node);
145
146 writeHeaders(m_globalIncludes, true);
147 writeHeaders(m_localIncludes, false);
148
149 m_output << QLatin1Char('\n');
150 }
151
acceptWidget(DomWidget * node)152 void WriteIncludes::acceptWidget(DomWidget *node)
153 {
154 if (debugWriteIncludes)
155 fprintf(stderr, "%s '%s'\n", Q_FUNC_INFO, qPrintable(node->attributeClass()));
156
157 add(node->attributeClass());
158 TreeWalker::acceptWidget(node);
159 }
160
acceptLayout(DomLayout * node)161 void WriteIncludes::acceptLayout(DomLayout *node)
162 {
163 add(node->attributeClass());
164 m_laidOut = true;
165 TreeWalker::acceptLayout(node);
166 }
167
acceptSpacer(DomSpacer * node)168 void WriteIncludes::acceptSpacer(DomSpacer *node)
169 {
170 add(QLatin1String("QSpacerItem"));
171 TreeWalker::acceptSpacer(node);
172 }
173
acceptProperty(DomProperty * node)174 void WriteIncludes::acceptProperty(DomProperty *node)
175 {
176 if (node->kind() == DomProperty::Date)
177 add(QLatin1String("QDate"));
178 if (node->kind() == DomProperty::Locale)
179 add(QLatin1String("QLocale"));
180 TreeWalker::acceptProperty(node);
181 }
182
insertIncludeForClass(const QString & className,QString header,bool global)183 void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
184 {
185 if (debugWriteIncludes)
186 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
187
188 do {
189 if (!header.isEmpty())
190 break;
191
192 // Known class
193 const StringMap::const_iterator it = m_classToHeader.constFind(className);
194 if (it != m_classToHeader.constEnd()) {
195 header = it.value();
196 global = true;
197 break;
198 }
199
200 // Quick check by class name to detect includehints provided for custom widgets.
201 // Remove namespaces
202 QString lowerClassName = className.toLower();
203 static const QString namespaceSeparator = QLatin1String("::");
204 const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
205 if (namespaceIndex != -1)
206 lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size());
207 if (m_includeBaseNames.contains(lowerClassName)) {
208 header.clear();
209 break;
210 }
211
212 // Last resort: Create default header
213 if (!m_uic->option().implicitIncludes)
214 break;
215 header = lowerClassName;
216 header += QLatin1String(".h");
217 if (warnHeaderGeneration) {
218 qWarning("%s: Warning: generated header '%s' for class '%s'.",
219 qPrintable(m_uic->option().messagePrefix()),
220 qPrintable(header), qPrintable(className));
221
222 }
223
224 global = true;
225 } while (false);
226
227 if (!header.isEmpty())
228 insertInclude(header, global);
229 }
230
add(const QString & className,bool determineHeader,const QString & header,bool global)231 void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global)
232 {
233 if (debugWriteIncludes)
234 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
235
236 if (className.isEmpty() || m_knownClasses.contains(className))
237 return;
238
239 m_knownClasses.insert(className);
240
241 if (!m_laidOut && m_uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBox")))
242 add(QLatin1String("QLayout")); // spacing property of QToolBox)
243
244 if (className == QLatin1String("Line")) { // ### hmm, deprecate me!
245 add(QLatin1String("QFrame"));
246 return;
247 }
248
249 if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView")) ||
250 m_uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {
251 add(QLatin1String("Q3Header"));
252 }
253 if (determineHeader)
254 insertIncludeForClass(className, header, global);
255 }
256
acceptCustomWidget(DomCustomWidget * node)257 void WriteIncludes::acceptCustomWidget(DomCustomWidget *node)
258 {
259 const QString className = node->elementClass();
260 if (className.isEmpty())
261 return;
262
263 if (const DomScript *domScript = node->elementScript())
264 if (!domScript->text().isEmpty())
265 activateScripts();
266
267 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) {
268 add(className, false); // no header specified
269 } else {
270 // custom header unless it is a built-in qt class
271 QString header;
272 bool global = false;
273 if (!m_classToHeader.contains(className)) {
274 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global");
275 header = node->elementHeader()->text();
276 }
277 add(className, true, header, global);
278 }
279 }
280
acceptCustomWidgets(DomCustomWidgets * node)281 void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node)
282 {
283 Q_UNUSED(node);
284 }
285
acceptIncludes(DomIncludes * node)286 void WriteIncludes::acceptIncludes(DomIncludes *node)
287 {
288 TreeWalker::acceptIncludes(node);
289 }
290
acceptInclude(DomInclude * node)291 void WriteIncludes::acceptInclude(DomInclude *node)
292 {
293 bool global = true;
294 if (node->hasAttributeLocation())
295 global = node->attributeLocation() == QLatin1String("global");
296 insertInclude(node->text(), global);
297 }
298
insertInclude(const QString & header,bool global)299 void WriteIncludes::insertInclude(const QString &header, bool global)
300 {
301 if (debugWriteIncludes)
302 fprintf(stderr, "%s %s %d\n", Q_FUNC_INFO, qPrintable(header), global);
303
304 OrderedSet &includes = global ? m_globalIncludes : m_localIncludes;
305 if (includes.contains(header))
306 return;
307 // Insert. Also remember base name for quick check of suspicious custom plugins
308 includes.insert(header, false);
309 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower();
310 m_includeBaseNames.insert(lowerBaseName);
311 }
312
writeHeaders(const OrderedSet & headers,bool global)313 void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global)
314 {
315 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"');
316 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"');
317
318 // Check for the old headers 'qslider.h' and replace by 'QtGui/QSlider'
319 const OrderedSet::const_iterator cend = headers.constEnd();
320 for (OrderedSet::const_iterator sit = headers.constBegin(); sit != cend; ++sit) {
321 const StringMap::const_iterator hit = m_oldHeaderToNewHeader.constFind(sit.key());
322 const bool mapped = hit != m_oldHeaderToNewHeader.constEnd();
323 const QString header = mapped ? hit.value() : sit.key();
324 if (!header.trimmed().isEmpty()) {
325 m_output << "#include " << openingQuote << header << closingQuote << QLatin1Char('\n');
326 }
327 }
328 }
329
acceptWidgetScripts(const DomScripts & scripts,DomWidget *,const DomWidgets &)330 void WriteIncludes::acceptWidgetScripts(const DomScripts &scripts, DomWidget *, const DomWidgets &)
331 {
332 if (!scripts.empty()) {
333 activateScripts();
334 }
335 }
336
activateScripts()337 void WriteIncludes::activateScripts()
338 {
339 if (!m_scriptsActivated) {
340 add(QLatin1String("QScriptEngine"));
341 add(QLatin1String("QDebug"));
342 m_scriptsActivated = true;
343 }
344 }
345 } // namespace CPP
346
347 QT_END_NAMESPACE
348