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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 15.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 == "nounload") {
153 // skip
154 } else if (opt == "convert") {
155 convert = true;
156 } else if (opt == "subdecl") {
157 subcl = true;
158 if (!(n < argc-2)) {
159 error = "Missing arguments";
160 break;
161 }
162 className = argv[++n];
163 headerFile = argv[++n];
164 } else if (opt == "subimpl") {
165 subcl = true;
166 impl = true;
167 if (!(n < argc-2)) {
168 error = "Missing arguments";
169 break;
170 }
171 className = argv[++n];
172 headerFile = argv[++n];
173 } else if (opt == "tr") {
174 if (opt == "tr" || opt[1] == '\0') {
175 if (!(n < argc-1)) {
176 error = "Missing tr macro.";
177 break;
178 }
179 trmacro = argv[++n];
180 } else {
181 trmacro = opt.data() + 1;
182 }
183 } else if (opt == "L") {
184 if (!(n < argc-1)) {
185 error = "Missing plugin path.";
186 break;
187 }
188 ++n; // ignore the next argument
189 } else if (opt == "version") {
190 fprintf(stderr,
191 "Qt User Interface Compiler version %s\n",
192 QT_VERSION_STR);
193 return 1;
194 } else if (opt == "help") {
195 break;
196 } else if (opt == "fix") {
197 fix = true;
198 } else if (opt == "pch") {
199 if (!(n < argc-1)) {
200 error = "Missing name of PCH file";
201 break;
202 }
203 pchFile = argv[++n];
204 } else {
205 error = "Unrecognized option";
206 }
207 } else {
208 if (imagecollection && !imagecollection_tmpfile)
209 images << QLatin1String(argv[n]);
210 else if (fileName) // can handle only one file
211 error = "Too many input files specified";
212 else
213 fileName = argv[n];
214 }
215 }
216
217 if (argc < 2 || error || (!fileName && !imagecollection)) {
218 fprintf(stderr,
219 "Qt User Interface Compiler version %s\n",
220 QT_VERSION_STR);
221 if (error)
222 fprintf(stderr, "uic: %s\n", error);
223
224 fprintf(stderr, "Usage: %s [options] [mode] <uifile>\n\n"
225 "Convert a UI file to version 4:\n"
226 " %s [options] -convert <uifile>\n"
227 "Generate declaration:\n"
228 " %s [options] <uifile>\n"
229 "\t<uiheaderfile> name of the data file\n"
230 " %s [options] -decl <uiheaderfile> <uifile>\n"
231 "\t<uiheaderfile> name of the data file\n"
232 " %s [options] -wrap <converteduifile> <uifile>\n"
233 "\t<converteduifile> name of the converted UI file\n"
234 "Generate implementation:\n"
235 " %s [options] -impl <headerfile> <uifile>\n"
236 "\t<headerfile> name of the declaration file\n"
237 "Generate image collection:\n"
238 " %s [options] -embed <project> <image1> <image2> <image3> ...\n"
239 "or\n"
240 " %s [options] -embed <project> -f <temporary file containing image names>\n"
241 "\t<project> project name\n"
242 "\t<image[1-N]> image files\n"
243 "Generate subclass declaration:\n"
244 " %s [options] -subdecl <subclassname> <baseclassheaderfile> <uifile>\n"
245 "\t<subclassname> name of the subclass to generate\n"
246 "\t<baseclassheaderfile> declaration file of the baseclass\n"
247 "Generate subclass implementation:\n"
248 " %s [options] -subimpl <subclassname> <subclassheaderfile> <uifile>\n"
249 "\t<subclassname> name of the subclass to generate\n"
250 "\t<subclassheaderfile> declaration file of the subclass\n"
251 "Options:\n"
252 "\t-o file Write output to file rather than stdout\n"
253 "\t-extract qrcFile Create resource file and extract embedded images into \"image\" dir\n"
254 "\t-pch file Add #include \"file\" as the first statement in implementation\n"
255 "\t-nofwd Omit forward declarations of custom classes\n"
256 "\t-layout-names Preserve layout names of Qt Designer 3\n"
257 "\t-no-implicit-includes Do not generate #include-directives for custom classes\n"
258 "\t-nounload Do not unload plugins after processing\n"
259 "\t-tr func Use func() instead of tr() for i18n\n"
260 "\t-L path Additional plugin search path\n"
261 "\t-version Display version of uic\n"
262 "\t-help Display this information\n"
263 , argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]
264 );
265 return 1;
266 }
267
268 if (imagecollection_tmpfile) {
269 QFile ifile(QFile::decodeName(image_tmpfile));
270 if (ifile.open(QIODevice::ReadOnly)) {
271 QTextStream ts(&ifile);
272 QString s = ts.read();
273 s = s.simplified();
274 images = s.split(QLatin1Char(' '));
275 for (QStringList::Iterator it = images.begin(); it != images.end(); ++it)
276 *it = (*it).simplified();
277 }
278 }
279
280 QFile fileOut;
281 if (!outputFile.isEmpty()) {
282 fileOut.setFileName(QFile::decodeName(outputFile));
283 if (!fileOut.open(QIODevice::WriteOnly)) {
284 fprintf(stderr, "%s: Could not open output file '%s'\n", argv[0], outputFile.data());
285 return 1;
286 }
287 } else {
288 fileOut.open(QIODevice::WriteOnly, stdout);
289 }
290
291 QTextStream out(&fileOut);
292
293 Ui3Reader ui3(out, readerOptions);
294 ui3.setExtractImages(extract, qrcOutputFile);
295 if (projectName && imagecollection) {
296 out.setEncoding(QTextStream::Latin1);
297 ui3.embed(projectName, images);
298 return 0;
299 }
300
301 out.setEncoding(QTextStream::UnicodeUTF8);
302
303 QFile file(QFile::decodeName(fileName));
304 if (!file.open(QIODevice::ReadOnly)) {
305 fprintf(stderr, "%s: Could not open file '%s'\n", argv[0], fileName);
306 return 1;
307 }
308
309 QDomDocument doc;
310 QString errMsg;
311 int errLine;
312 if (!doc.setContent(&file, &errMsg, &errLine)) {
313 fprintf(stderr, "%s: Failed to parse %s: %s in line %d\n", argv[0], fileName, errMsg.latin1(), errLine);
314 return 1;
315 }
316
317 QDomElement e = doc.firstChild().toElement();
318 double version = e.attribute(QLatin1String("version"), QLatin1String("3.0")).toDouble();
319
320 if (version > 3.3) {
321 fprintf(stderr, "%s: File generated with too recent version of Qt Designer (%s vs. %s)\n",
322 argv[0], e.attribute(QLatin1String("version")).latin1(), "3.3");
323 return 1;
324 }
325
326 DomTool::fixDocument(doc);
327
328 if (fix) {
329 out << doc.toString();
330 return 0;
331 }
332
333 if (imagecollection) {
334 out.setEncoding(QTextStream::Latin1);
335 ui3.embed(projectName, images);
336 return 0;
337 } else if (deps) {
338 QStringList globalIncludes, localIncludes;
339 ui3.computeDeps(e, globalIncludes, localIncludes, impl);
340
341 foreach (QString i, globalIncludes)
342 printf("%s\n", i.toLatin1().constData());
343
344 foreach (QString i, localIncludes)
345 printf("%s\n", i.toLatin1().constData());
346
347 if (impl)
348 printf("%s\n", headerFile);
349
350 return 0;
351 } else if (convert) {
352 ui3.generateUi4(QFile::decodeName(fileName), QFile::decodeName(outputFile), doc);
353 return 0;
354 }
355
356 QString protector;
357 if (subcl && className && !impl)
358 protector = QString::fromUtf8(className).toUpper() + QLatin1String("_H");
359
360 if (!protector.isEmpty()) {
361 out << "#ifndef " << protector << endl;
362 out << "#define " << protector << endl;
363 }
364
365 if (!pchFile.isEmpty() && impl) {
366 out << "#include \"" << pchFile << "\" // PCH include" << endl;
367 }
368
369 if (headerFile) {
370 out << "#include \"" << headerFile << '\"' << endl << endl;
371 }
372
373 QString convertedUi;
374 if (wrap) {
375 convertedUi = QFile::decodeName(convertedUiFile);
376 int pos = convertedUi.lastIndexOf(QLatin1String(".ui"));
377 if (pos > 0) {
378 convertedUi = convertedUi.mid(0, pos);
379 convertedUi += QLatin1String(".h");
380 }
381 convertedUi = QLatin1String("ui_") + convertedUi;
382 }
383
384 ui3.generate(QFile::decodeName(fileName),
385 QFile::decodeName(outputFile),
386 doc,
387 !impl,
388 subcl,
389 QString::fromUtf8(trmacro),
390 QString::fromUtf8(className),
391 convertedUi);
392
393 if (!protector.isEmpty()) {
394 out << endl;
395 out << "#endif // " << protector << endl;
396 }
397
398 if (fileOut.error() != QFile::NoError) {
399 fprintf(stderr, "%s: Error writing to file\n", argv[0]);
400 if (!outputFile.isEmpty())
401 remove(outputFile);
402 }
403
404 return 0;
405}
406
407QT_END_NAMESPACE
408
409int main(int argc, char * argv[])
410{
411 return QT_PREPEND_NAMESPACE(runUic3(argc, argv));
412}
Note: See TracBrowser for help on using the repository browser.