1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "cppwriteincludes.h"
30 #include "driver.h"
31 #include "ui4.h"
32 #include "uic.h"
33 #include "databaseinfo.h"
34
35 #include <qdebug.h>
36 #include <qfileinfo.h>
37 #include <qtextstream.h>
38
39 #include <stdio.h>
40
41 QT_BEGIN_NAMESPACE
42
43 enum { debugWriteIncludes = 0 };
44 enum { warnHeaderGeneration = 0 };
45
46 struct ClassInfoEntry
47 {
48 const char *klass;
49 const char *module;
50 const char *header;
51 };
52
53 static const ClassInfoEntry qclass_lib_map[] = {
54 #define QT_CLASS_LIB(klass, module, header) { #klass, #module, #header },
55 #include "qclass_lib_map.h"
56
57 #undef QT_CLASS_LIB
58 };
59
60 // Format a module header as 'QtCore/QObject'
moduleHeader(const QString & module,const QString & header)61 static inline QString moduleHeader(const QString &module, const QString &header)
62 {
63 QString rc = module;
64 rc += QLatin1Char('/');
65 rc += header;
66 return rc;
67 }
68
69 namespace CPP {
70
WriteIncludes(Uic * uic)71 WriteIncludes::WriteIncludes(Uic *uic)
72 : m_uic(uic), m_output(uic->output())
73 {
74 // When possible (no namespace) use the "QtModule/QClass" convention
75 // and create a re-mapping of the old header "qclass.h" to it. Do not do this
76 // for the "Phonon::Someclass" classes, however.
77 const QString namespaceDelimiter = QLatin1String("::");
78 const ClassInfoEntry *classLibEnd = qclass_lib_map + sizeof(qclass_lib_map)/sizeof(ClassInfoEntry);
79 for (const ClassInfoEntry *it = qclass_lib_map; it < classLibEnd; ++it) {
80 const QString klass = QLatin1String(it->klass);
81 const QString module = QLatin1String(it->module);
82 QLatin1String header = QLatin1String(it->header);
83 if (klass.contains(namespaceDelimiter)) {
84 m_classToHeader.insert(klass, moduleHeader(module, header));
85 } else {
86 const QString newHeader = moduleHeader(module, klass);
87 m_classToHeader.insert(klass, newHeader);
88 m_oldHeaderToNewHeader.insert(header, newHeader);
89 }
90 }
91 }
92
acceptUI(DomUI * node)93 void WriteIncludes::acceptUI(DomUI *node)
94 {
95 m_laidOut = false;
96 m_localIncludes.clear();
97 m_globalIncludes.clear();
98 m_knownClasses.clear();
99 m_includeBaseNames.clear();
100
101 if (node->elementIncludes())
102 acceptIncludes(node->elementIncludes());
103
104 if (node->elementCustomWidgets())
105 TreeWalker::acceptCustomWidgets(node->elementCustomWidgets());
106
107 add(QLatin1String("QApplication"));
108 add(QLatin1String("QVariant"));
109
110 if (node->elementButtonGroups())
111 add(QLatin1String("QButtonGroup"));
112
113 TreeWalker::acceptUI(node);
114
115 const auto includeFile = m_uic->option().includeFile;
116 if (!includeFile.isEmpty())
117 m_globalIncludes.insert(includeFile);
118
119 writeHeaders(m_globalIncludes, true);
120 writeHeaders(m_localIncludes, false);
121
122 m_output << '\n';
123 }
124
acceptWidget(DomWidget * node)125 void WriteIncludes::acceptWidget(DomWidget *node)
126 {
127 if (debugWriteIncludes)
128 fprintf(stderr, "%s '%s'\n", Q_FUNC_INFO, qPrintable(node->attributeClass()));
129
130 add(node->attributeClass());
131 TreeWalker::acceptWidget(node);
132 }
133
acceptLayout(DomLayout * node)134 void WriteIncludes::acceptLayout(DomLayout *node)
135 {
136 add(node->attributeClass());
137 m_laidOut = true;
138 TreeWalker::acceptLayout(node);
139 }
140
acceptSpacer(DomSpacer * node)141 void WriteIncludes::acceptSpacer(DomSpacer *node)
142 {
143 add(QLatin1String("QSpacerItem"));
144 TreeWalker::acceptSpacer(node);
145 }
146
acceptProperty(DomProperty * node)147 void WriteIncludes::acceptProperty(DomProperty *node)
148 {
149 if (node->kind() == DomProperty::Date)
150 add(QLatin1String("QDate"));
151 if (node->kind() == DomProperty::Locale)
152 add(QLatin1String("QLocale"));
153 if (node->kind() == DomProperty::IconSet)
154 add(QLatin1String("QIcon"));
155 TreeWalker::acceptProperty(node);
156 }
157
insertIncludeForClass(const QString & className,QString header,bool global)158 void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
159 {
160 if (debugWriteIncludes)
161 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(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.toLower();
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(".h");
192 if (warnHeaderGeneration) {
193 qWarning("%s: Warning: generated header '%s' for class '%s'.",
194 qPrintable(m_uic->option().messagePrefix()),
195 qPrintable(header), qPrintable(className));
196
197 }
198
199 global = true;
200 } while (false);
201
202 if (!header.isEmpty())
203 insertInclude(header, global);
204 }
205
add(const QString & className,bool determineHeader,const QString & header,bool global)206 void WriteIncludes::add(const QString &className, bool determineHeader, const QString &header, bool global)
207 {
208 if (debugWriteIncludes)
209 fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
210
211 if (className.isEmpty() || m_knownClasses.contains(className))
212 return;
213
214 m_knownClasses.insert(className);
215
216 const CustomWidgetsInfo *cwi = m_uic->customWidgetsInfo();
217 static const QStringList treeViewsWithHeaders = {
218 QLatin1String("QTreeView"), QLatin1String("QTreeWidget"),
219 QLatin1String("QTableView"), QLatin1String("QTableWidget")
220 };
221 if (cwi->extendsOneOf(className, treeViewsWithHeaders))
222 add(QLatin1String("QHeaderView"));
223
224 if (!m_laidOut && cwi->extends(className, QLatin1String("QToolBox")))
225 add(QLatin1String("QLayout")); // spacing property of QToolBox)
226
227 if (className == QLatin1String("Line")) { // ### hmm, deprecate me!
228 add(QLatin1String("QFrame"));
229 return;
230 }
231
232 if (determineHeader)
233 insertIncludeForClass(className, header, global);
234 }
235
acceptCustomWidget(DomCustomWidget * node)236 void WriteIncludes::acceptCustomWidget(DomCustomWidget *node)
237 {
238 const QString className = node->elementClass();
239 if (className.isEmpty())
240 return;
241
242 if (!node->elementHeader() || node->elementHeader()->text().isEmpty()) {
243 add(className, false); // no header specified
244 } else {
245 // custom header unless it is a built-in qt class
246 QString header;
247 bool global = false;
248 if (!m_classToHeader.contains(className)) {
249 global = node->elementHeader()->attributeLocation().toLower() == QLatin1String("global");
250 header = node->elementHeader()->text();
251 }
252 add(className, true, header, global);
253 }
254 }
255
acceptActionGroup(DomActionGroup * node)256 void WriteIncludes::acceptActionGroup(DomActionGroup *node)
257 {
258 add(QLatin1String("QAction"));
259 TreeWalker::acceptActionGroup(node);
260 }
261
acceptAction(DomAction * node)262 void WriteIncludes::acceptAction(DomAction *node)
263 {
264 add(QLatin1String("QAction"));
265 TreeWalker::acceptAction(node);
266 }
267
acceptActionRef(DomActionRef * node)268 void WriteIncludes::acceptActionRef(DomActionRef *node)
269 {
270 add(QLatin1String("QAction"));
271 TreeWalker::acceptActionRef(node);
272 }
273
acceptCustomWidgets(DomCustomWidgets * node)274 void WriteIncludes::acceptCustomWidgets(DomCustomWidgets *node)
275 {
276 Q_UNUSED(node);
277 }
278
acceptIncludes(DomIncludes * node)279 void WriteIncludes::acceptIncludes(DomIncludes *node)
280 {
281 TreeWalker::acceptIncludes(node);
282 }
283
acceptInclude(DomInclude * node)284 void 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
insertInclude(const QString & header,bool global)292 void 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 // Insert (if not already done).
299 const bool isNewHeader = includes.insert(header).second;
300 if (!isNewHeader)
301 return;
302 // Also remember base name for quick check of suspicious custom plugins
303 const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower();
304 m_includeBaseNames.insert(lowerBaseName);
305 }
306
writeHeaders(const OrderedSet & headers,bool global)307 void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global)
308 {
309 const QChar openingQuote = global ? QLatin1Char('<') : QLatin1Char('"');
310 const QChar closingQuote = global ? QLatin1Char('>') : QLatin1Char('"');
311
312 // Check for the old headers 'qslider.h' and replace by 'QtGui/QSlider'
313 for (const QString &header : headers) {
314 const QString value = m_oldHeaderToNewHeader.value(header, header);
315 const auto trimmed = QStringRef(&value).trimmed();
316 if (!trimmed.isEmpty())
317 m_output << "#include " << openingQuote << trimmed << closingQuote << '\n';
318 }
319 }
320
321 } // namespace CPP
322
323 QT_END_NAMESPACE
324