source: trunk/src/tools/uic3/main.cpp@ 930

Last change on this file since 930 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: 15.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 "ui3reader.h"
43#include "domtool.h"
44#include "globaldefs.h"
45
46#include <QApplication>
47#include <QFile>
48#include <QFileInfo>
49#include <QStringList>
50#include <QDateTime>
51#include <QSettings>
52#include <stdio.h>
53#include <stdlib.h>
54
55#if defined Q_WS_WIN
56#include <qt_windows.h>
57#endif
58
59QT_BEGIN_NAMESPACE
60
61int runUic3(int argc, char * argv[])
62{
63 bool impl = false;
64 bool wrap = false;
65 bool subcl = false;
66 bool extract = false;
67 bool imagecollection = false;
68 bool imagecollection_tmpfile = false;
69 bool convert = false;
70 QStringList images;
71 const char *error = 0;
72 const char* fileName = 0;
73 const char* className = 0;
74 const char* headerFile = 0;
75 const char* convertedUiFile = 0;
76 QByteArray outputFile;
77 QString qrcOutputFile;
78 QByteArray image_tmpfile;
79 const char* projectName = 0;
80 const char* trmacro = 0;
81 bool fix = false;
82 bool deps = false;
83 unsigned readerOptions = Ui3Reader::ImplicitIncludes|Ui3Reader::CustomWidgetForwardDeclarations;
84 QByteArray pchFile;
85
86 QApplication app(argc, argv, false);
87
88 for (int n = 1; n < argc && error == 0; n++) {
89 QByteArray arg = argv[n];
90 if (arg[0] == '-') { // option
91 QByteArray opt = arg.data() + 1;
92 if (opt[0] == 'o') { // output redirection
93 if (opt[1] == '\0') {
94 if (!(n < argc-1)) {
95 error = "Missing output-file name";
96 break;
97 }
98 outputFile = argv[++n];
99 } else
100 outputFile = opt.data() + 1;
101 } else if (opt[0] == 'i' || opt == "impl") {
102 impl = true;
103 if (opt == "impl" || opt[1] == '\0') {
104 if (!(n < argc-1)) {
105 error = "Missing name of header file";
106 break;
107 }
108 headerFile = argv[++n];
109 } else
110 headerFile = opt.data() + 1;
111 } else if (opt[0] == 'w' || opt == "wrap") {
112 wrap = true;
113 if (opt == "wrap" || opt[1] == '\0') {
114 if (!(n < argc-1)) {
115 error = "Missing name of converted UI file";
116 break;
117 }
118 convertedUiFile = argv[++n];
119 } else
120 convertedUiFile = opt.data() + 1;
121 } else if (opt == "extract") { // output redirection
122 extract = true;
123 if (!(n < argc-1)) {
124 error = "Missing output qrc-file name";
125 break;
126 }
127 qrcOutputFile = QFile::decodeName(argv[++n]);
128 } else if ( opt[0] == 'e' || opt == "embed" ) {
129 imagecollection = true;
130 if ( opt == "embed" || opt[1] == '\0' ) {
131 if ( !(n < argc-1) ) {
132 error = "Missing name of project";
133 break;
134 }
135 projectName = argv[++n];
136 } else {
137 projectName = opt.data() + 1;
138 }
139 if ( argc > n+1 && qstrcmp( argv[n+1], "-f" ) == 0 ) {
140 imagecollection_tmpfile = true;
141 image_tmpfile = argv[n+2];
142 n += 2;
143 }
144 } else if (opt == "d") {
145 deps = true;
146 } else if (opt == "no-implicit-includes") {
147 readerOptions &= ~Ui3Reader::ImplicitIncludes;
148 } else if (opt == "nofwd") {
149 readerOptions &= ~Ui3Reader::CustomWidgetForwardDeclarations;
150 } else if (opt == "layout-names") {
151 readerOptions |= Ui3Reader::PreserveLayoutNames;
152 } else if (opt == "limit-xpm-linelength") {
153 readerOptions |= Ui3Reader::LimitXPM_LineLength;
154 } else if (opt == "nounload") {
155 // skip
156 } else if (opt == "convert") {
157 convert = true;
158 } else if (opt == "subdecl") {
159 subcl = true;
160 if (!(n < argc-2)) {
161 error = "Missing arguments";
162 break;
163 }
164 className = argv[++n];
165 headerFile = argv[++n];
166 } else if (opt == "subimpl") {
167 subcl = true;
168 impl = true;
169 if (!(n < argc-2)) {
170 error = "Missing arguments";
171 break;
172 }
173 className = argv[++n];
174 headerFile = argv[++n];
175 } else if (opt == "tr") {
176 if (opt == "tr" || opt[1] == '\0') {
177 if (!(n < argc-1)) {
178 error = "Missing tr macro.";
179 break;
180 }
181 trmacro = argv[++n];
182 } else {
183 trmacro = opt.data() + 1;
184 }
185 } else if (opt == "L") {
186 if (!(n < argc-1)) {
187 error = "Missing plugin path.";
188 break;
189 }
190 ++n; // ignore the next argument
191 } else if (opt == "version") {
192 fprintf(stderr,
193 "Qt User Interface Compiler version %s\n",
194 QT_VERSION_STR);
195 return 1;
196 } else if (opt == "help") {
197 break;
198 } else if (opt == "fix") {
199 fix = true;
200 } else if (opt == "pch") {
201 if (!(n < argc-1)) {
202 error = "Missing name of PCH file";
203 break;
204 }
205 pchFile = argv[++n];
206 } else {
207 error = "Unrecognized option";
208 }
209 } else {
210 if (imagecollection && !imagecollection_tmpfile)
211 images << QLatin1String(argv[n]);
212 else if (fileName) // can handle only one file
213 error = "Too many input files specified";
214 else
215 fileName = argv[n];
216 }
217 }
218
219 if (argc < 2 || error || (!fileName && !imagecollection)) {
220 fprintf(stderr,
221 "Qt User Interface Compiler version %s\n",
222 QT_VERSION_STR);
223 if (error)
224 fprintf(stderr, "uic: %s\n", error);
225
226 fprintf(stderr, "Usage: %s [options] [mode] <uifile>\n\n"
227 "Convert a UI file to version 4:\n"
228 " %s [options] -convert <uifile>\n"
229 "Generate declaration:\n"
230 " %s [options] <uifile>\n"
231 "\t<uiheaderfile> name of the data file\n"
232 " %s [options] -decl <uiheaderfile> <uifile>\n"
233 "\t<uiheaderfile> name of the data file\n"
234 " %s [options] -wrap <converteduifile> <uifile>\n"
235 "\t<converteduifile> name of the converted UI file\n"
236 "Generate implementation:\n"
237 " %s [options] -impl <headerfile> <uifile>\n"
238 "\t<headerfile> name of the declaration file\n"
239 "Generate image collection:\n"
240 " %s [options] -embed <project> <image1> <image2> <image3> ...\n"
241 "or\n"
242 " %s [options] -embed <project> -f <temporary file containing image names>\n"
243 "\t<project> project name\n"
244 "\t<image[1-N]> image files\n"
245 "Generate subclass declaration:\n"
246 " %s [options] -subdecl <subclassname> <baseclassheaderfile> <uifile>\n"
247 "\t<subclassname> name of the subclass to generate\n"
248 "\t<baseclassheaderfile> declaration file of the baseclass\n"
249 "Generate subclass implementation:\n"
250 " %s [options] -subimpl <subclassname> <subclassheaderfile> <uifile>\n"
251 "\t<subclassname> name of the subclass to generate\n"
252 "\t<subclassheaderfile> declaration file of the subclass\n"
253 "Options:\n"
254 "\t-o file Write output to file rather than stdout\n"
255 "\t-extract qrcFile Create resource file and extract embedded images into \"image\" dir\n"
256 "\t-limit-xpm-linelength Limit the line length of XPM files for -extract.\n"
257 "\t-pch file Add #include \"file\" as the first statement in implementation\n"
258 "\t-nofwd Omit forward declarations of custom classes\n"
259 "\t-layout-names Preserve layout names of Qt Designer 3\n"
260 "\t-no-implicit-includes Do not generate #include-directives for custom classes\n"
261 "\t-nounload Do not unload plugins after processing\n"
262 "\t-tr func Use func() instead of tr() for i18n\n"
263 "\t-L path Additional plugin search path\n"
264 "\t-version Display version of uic\n"
265 "\t-help Display this information\n"
266 , argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]
267 );
268 return 1;
269 }
270
271 if (imagecollection_tmpfile) {
272 QFile ifile(QFile::decodeName(image_tmpfile));
273 if (ifile.open(QIODevice::ReadOnly)) {
274 QTextStream ts(&ifile);
275 QString s = ts.read();
276 s = s.simplified();
277 images = s.split(QLatin1Char(' '));
278 for (QStringList::Iterator it = images.begin(); it != images.end(); ++it)
279 *it = (*it).simplified();
280 }
281 }
282
283 QFile fileOut;
284 if (!outputFile.isEmpty()) {
285 fileOut.setFileName(QFile::decodeName(outputFile));
286 if (!fileOut.open(QIODevice::WriteOnly)) {
287 fprintf(stderr, "%s: Could not open output file '%s'\n", argv[0], outputFile.data());
288 return 1;
289 }
290 } else {
291 fileOut.open(QIODevice::WriteOnly, stdout);
292 }
293
294 QTextStream out(&fileOut);
295
296 Ui3Reader ui3(out, readerOptions);
297 ui3.setExtractImages(extract, qrcOutputFile);
298 if (projectName && imagecollection) {
299 out.setEncoding(QTextStream::Latin1);
300 ui3.embed(projectName, images);
301 return 0;
302 }
303
304 out.setEncoding(QTextStream::UnicodeUTF8);
305
306 QFile file(QFile::decodeName(fileName));
307 if (!file.open(QIODevice::ReadOnly)) {
308 fprintf(stderr, "%s: Could not open file '%s'\n", argv[0], fileName);
309 return 1;
310 }
311
312 QDomDocument doc;
313 QString errMsg;
314 int errLine;
315 if (!doc.setContent(&file, &errMsg, &errLine)) {
316 fprintf(stderr, "%s: Failed to parse %s: %s in line %d\n", argv[0], fileName, errMsg.latin1(), errLine);
317 return 1;
318 }
319
320 QDomElement e = doc.firstChild().toElement();
321 double version = e.attribute(QLatin1String("version"), QLatin1String("3.0")).toDouble();
322
323 if (version > 3.3) {
324 fprintf(stderr, "%s: File generated with too recent version of Qt Designer (%s vs. %s)\n",
325 argv[0], e.attribute(QLatin1String("version")).latin1(), "3.3");
326 return 1;
327 }
328
329 DomTool::fixDocument(doc);
330
331 if (fix) {
332 out << doc.toString();
333 return 0;
334 }
335
336 if (imagecollection) {
337 out.setEncoding(QTextStream::Latin1);
338 ui3.embed(projectName, images);
339 return 0;
340 } else if (deps) {
341 QStringList globalIncludes, localIncludes;
342 ui3.computeDeps(e, globalIncludes, localIncludes, impl);
343
344 foreach (const QString &i, globalIncludes)
345 printf("%s\n", i.toLatin1().constData());
346
347 foreach (const QString &i, localIncludes)
348 printf("%s\n", i.toLatin1().constData());
349
350 if (impl)
351 printf("%s\n", headerFile);
352
353 return 0;
354 } else if (convert) {
355 ui3.generateUi4(QFile::decodeName(fileName), QFile::decodeName(outputFile), doc);
356 return 0;
357 }
358
359 QString protector;
360 if (subcl && className && !impl)
361 protector = QString::fromUtf8(className).toUpper() + QLatin1String("_H");
362
363 if (!protector.isEmpty()) {
364 out << "#ifndef " << protector << endl;
365 out << "#define " << protector << endl;
366 }
367
368 if (!pchFile.isEmpty() && impl) {
369 out << "#include \"" << pchFile << "\" // PCH include" << endl;
370 }
371
372 if (headerFile) {
373 out << "#include \"" << headerFile << '\"' << endl << endl;
374 }
375
376 QString convertedUi;
377 if (wrap) {
378 convertedUi = QFile::decodeName(convertedUiFile);
379 int pos = convertedUi.lastIndexOf(QLatin1String(".ui"));
380 if (pos > 0) {
381 convertedUi = convertedUi.mid(0, pos);
382 convertedUi += QLatin1String(".h");
383 }
384 convertedUi = QLatin1String("ui_") + convertedUi;
385 }
386
387 ui3.generate(QFile::decodeName(fileName),
388 QFile::decodeName(outputFile),
389 doc,
390 !impl,
391 subcl,
392 QString::fromUtf8(trmacro),
393 QString::fromUtf8(className),
394 convertedUi);
395
396 if (!protector.isEmpty()) {
397 out << endl;
398 out << "#endif // " << protector << endl;
399 }
400
401 if (fileOut.error() != QFile::NoError) {
402 fprintf(stderr, "%s: Error writing to file\n", argv[0]);
403 if (!outputFile.isEmpty())
404 remove(outputFile);
405 }
406
407 return 0;
408}
409
410QT_END_NAMESPACE
411
412int main(int argc, char * argv[])
413{
414 return QT_PREPEND_NAMESPACE(runUic3(argc, argv));
415}
Note: See TracBrowser for help on using the repository browser.