source: trunk/tools/designer/uic/main.cpp@ 7

Last change on this file since 7 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12** licenses may use this file in accordance with the Qt Commercial License
13** Agreement provided with the Software.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** See http://www.trolltech.com/gpl/ for GPL licensing information.
19** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20** information about Qt Commercial License Agreements.
21**
22** Contact info@trolltech.com if any conditions of this licensing are
23** not clear to you.
24**
25**********************************************************************/
26
27#include "uic.h"
28#include "domtool.h"
29#if defined(UIB)
30#include "ui2uib.h"
31#endif
32#include <qapplication.h>
33#include <qfile.h>
34#include <qstringlist.h>
35#include <qdatetime.h>
36#include <qsettings.h>
37#define NO_STATIC_COLORS
38#include <globaldefs.h>
39#include <stdio.h>
40#include <stdlib.h>
41
42// see ### in widgetdatabase.cpp
43extern bool dbnounload;
44extern QStringList *dbpaths;
45
46int main( int argc, char * argv[] )
47{
48 bool impl = FALSE;
49 bool subcl = FALSE;
50 bool imagecollection = FALSE;
51 bool imagecollection_tmpfile = FALSE;
52#if defined(UIB)
53 bool binary = FALSE;
54#endif
55 QStringList images;
56 const char *error = 0;
57 const char* fileName = 0;
58 const char* className = 0;
59 const char* headerFile = 0;
60 QCString outputFile;
61 QCString image_tmpfile;
62 const char* projectName = 0;
63 const char* trmacro = 0;
64 bool nofwd = FALSE;
65 bool fix = FALSE;
66 QCString pchFile;
67 QApplication app(argc, argv, FALSE);
68
69 QString keybase( "/Qt Designer/" +
70 QString::number( (QT_VERSION >> 16) & 0xff ) +"." + QString::number( (QT_VERSION >> 8) & 0xff ) + "/" );
71 QSettings config;
72 config.insertSearchPath( QSettings::Windows, "/Trolltech" );
73 QStringList pluginPaths = config.readListEntry( keybase + "PluginPaths" );
74 if (pluginPaths.count())
75 QApplication::setLibraryPaths(pluginPaths);
76
77 for ( int n = 1; n < argc && error == 0; n++ ) {
78 QCString arg = argv[n];
79 if ( arg[0] == '-' ) { // option
80 QCString opt = &arg[1];
81 if ( opt[0] == 'o' ) { // output redirection
82 if ( opt[1] == '\0' ) {
83 if ( !(n < argc-1) ) {
84 error = "Missing output-file name";
85 break;
86 }
87 outputFile = argv[++n];
88 } else
89 outputFile = &opt[1];
90 } else if ( opt[0] == 'i' || opt == "impl" ) {
91 impl = TRUE;
92 if ( opt == "impl" || opt[1] == '\0' ) {
93 if ( !(n < argc-1) ) {
94 error = "Missing name of header file";
95 break;
96 }
97 headerFile = argv[++n];
98 } else
99 headerFile = &opt[1];
100 } else if ( opt[0] == 'e' || opt == "embed" ) {
101 imagecollection = TRUE;
102 if ( opt == "embed" || opt[1] == '\0' ) {
103 if ( !(n < argc-1) ) {
104 error = "Missing name of project";
105 break;
106 }
107 projectName = argv[++n];
108 } else {
109 projectName = &opt[1];
110 }
111 if ( argc > n+1 && qstrcmp( argv[n+1], "-f" ) == 0 ) {
112 imagecollection_tmpfile = TRUE;
113 image_tmpfile = argv[n+2];
114 n += 2;
115 }
116#if defined(UIB)
117
118 } else if ( opt == "binary" ) {
119 binary = TRUE;
120#endif
121 } else if ( opt == "nofwd" ) {
122 nofwd = TRUE;
123 } else if ( opt == "nounload" ) {
124 dbnounload = TRUE;
125 } else if ( opt == "subdecl" ) {
126 subcl = TRUE;
127 if ( !(n < argc-2) ) {
128 error = "Missing arguments";
129 break;
130 }
131 className = argv[++n];
132 headerFile = argv[++n];
133 } else if ( opt == "subimpl" ) {
134 subcl = TRUE;
135 impl = TRUE;
136 if ( !(n < argc-2) ) {
137 error = "Missing arguments";
138 break;
139 }
140 className = argv[++n];
141 headerFile = argv[++n];
142 } else if ( opt == "tr" ) {
143 if ( opt == "tr" || opt[1] == '\0' ) {
144 if ( !(n < argc-1) ) {
145 error = "Missing tr macro.";
146 break;
147 }
148 trmacro = argv[++n];
149 } else {
150 trmacro = &opt[1];
151 }
152 } else if ( opt == "L" ) {
153 if ( !(n < argc-1) ) {
154 error = "Missing plugin path.";
155 break;
156 }
157 if ( !dbpaths )
158 dbpaths = new QStringList();
159 QString fn = QFile::decodeName( argv[++n] );
160 dbpaths->append( fn );
161 QApplication::addLibraryPath( fn );
162 } else if ( opt == "version" ) {
163 fprintf( stderr,
164 "User Interface Compiler for Qt version %s\n",
165 QT_VERSION_STR );
166 return 1;
167 } else if ( opt == "help" ) {
168 break;
169 } else if ( opt == "fix" ) {
170 fix = TRUE;
171 } else if ( opt == "pch") {
172 if ( !(n < argc-1) ) {
173 error = "Missing name of PCH file";
174 break;
175 }
176 pchFile = argv[++n];
177 } else {
178 error = "Unrecognized option";
179 }
180 } else {
181 if ( imagecollection && !imagecollection_tmpfile )
182 images << argv[n];
183 else if ( fileName ) // can handle only one file
184 error = "Too many input files specified";
185 else
186 fileName = argv[n];
187 }
188 }
189
190 if ( argc < 2 || error || (!fileName && !imagecollection ) ) {
191 fprintf( stderr, "Qt user interface compiler.\n" );
192 if ( error )
193 fprintf( stderr, "uic: %s\n", error );
194
195 fprintf( stderr, "Usage: %s [options] [mode] <uifile>\n\n"
196 "Generate declaration:\n"
197 " %s [options] <uifile>\n"
198 "Generate implementation:\n"
199 " %s [options] -impl <headerfile> <uifile>\n"
200 "\t<headerfile> name of the declaration file\n"
201 "Generate image collection:\n"
202 " %s [options] -embed <project> <image1> <image2> <image3> ...\n"
203 "or\n"
204 " %s [options] -embed <project> -f <temporary file containing image names>\n"
205 "\t<project> project name\n"
206 "\t<image[1-N]> image files\n"
207#if defined(UIB)
208 "Generate binary UI file:\n"
209 " %s [options] -binary <uifile>\n"
210#endif
211 "Generate subclass declaration:\n"
212 " %s [options] -subdecl <subclassname> <baseclassheaderfile> <uifile>\n"
213 "\t<subclassname> name of the subclass to generate\n"
214 "\t<baseclassheaderfile> declaration file of the baseclass\n"
215 "Generate subclass implementation:\n"
216 " %s [options] -subimpl <subclassname> <subclassheaderfile> <uifile>\n"
217 "\t<subclassname> name of the subclass to generate\n"
218 "\t<subclassheaderfile> declaration file of the subclass\n"
219 "Options:\n"
220 "\t-o file Write output to file rather than stdout\n"
221 "\t-pch file Add #include \"file\" as the first statement in implementation\n"
222 "\t-nofwd Omit forward declarations of custom classes\n"
223 "\t-nounload Don't unload plugins after processing\n"
224 "\t-tr func Use func() instead of tr() for i18n\n"
225 "\t-L path Additional plugin search path\n"
226 "\t-version Display version of uic\n"
227 "\t-help Display this information\n"
228 , argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]
229#if defined(UIB)
230 , argv[0]
231#endif
232 );
233 return 1;
234 }
235
236 if ( imagecollection_tmpfile ) {
237 QFile ifile( image_tmpfile );
238 if ( ifile.open( IO_ReadOnly ) ) {
239 QTextStream ts( &ifile );
240 QString s = ts.read();
241 s = s.simplifyWhiteSpace();
242 images = QStringList::split( ' ', s );
243 for ( QStringList::Iterator it = images.begin(); it != images.end(); ++it )
244 *it = (*it).simplifyWhiteSpace();
245 }
246 }
247
248#if defined(UIB)
249 if ( binary && outputFile.isEmpty() ) {
250 outputFile = fileName;
251 if ( outputFile.mid(outputFile.length() - 3).lower() == ".ui" )
252 outputFile.truncate( outputFile.length() - 3 );
253 outputFile += ".uib";
254 }
255#endif
256
257 QFile fileOut;
258 if ( !outputFile.isEmpty() ) {
259 fileOut.setName( outputFile );
260 if (!fileOut.open( IO_WriteOnly ) ) {
261 qWarning( "uic: Could not open output file '%s'", outputFile.data() );
262 return 1;
263 }
264 } else {
265 fileOut.open( IO_WriteOnly, stdout );
266 }
267 QTextStream out( &fileOut );
268
269 if ( imagecollection ) {
270 out.setEncoding( QTextStream::Latin1 );
271 Uic::embed( out, projectName, images );
272 return 0;
273 }
274
275 out.setEncoding( QTextStream::UnicodeUTF8 );
276
277 QFile file( fileName );
278 if ( !file.open( IO_ReadOnly ) ) {
279 qWarning( "uic: Could not open file '%s'", fileName );
280 return 1;
281 }
282
283 QDomDocument doc;
284 QString errMsg;
285 int errLine;
286 if ( !doc.setContent( &file, &errMsg, &errLine ) ) {
287 qWarning( QString("uic: Failed to parse %s: ") + errMsg + QString (" in line %d"), fileName, errLine );
288 return 1;
289 }
290
291 QDomElement e = doc.firstChild().toElement();
292 if ( e.hasAttribute("version") && e.attribute("version").toDouble() > 3.3 ) {
293 qWarning( QString("uic: File generated with too recent version of Qt Designer (%s vs. %s)"),
294 e.attribute("version").latin1(), QT_VERSION_STR );
295 return 1;
296 }
297
298 DomTool::fixDocument( doc );
299
300 if ( fix ) {
301 out << doc.toString();
302 return 0;
303#if defined(UIB)
304 } else if ( binary ) {
305 out.unsetDevice();
306 QDataStream binaryOut( &fileOut );
307 convertUiToUib( doc, binaryOut );
308 return 0;
309#endif
310 }
311
312 if ( !subcl ) {
313 out << "/****************************************************************************" << endl;
314 out << "** Form "<< (impl? "implementation" : "interface") << " generated from reading ui file '" << fileName << "'" << endl;
315 out << "**" << endl;
316 out << "** Created: " << QDateTime::currentDateTime().toString() << endl;
317 out << "** by: The User Interface Compiler ($Id: main.cpp 2 2005-11-16 15:49:26Z dmik $)" << endl;
318 out << "**" << endl;
319 out << "** WARNING! All changes made in this file will be lost!" << endl;
320 out << "****************************************************************************/" << endl << endl;
321 }
322
323 QString protector;
324 if ( subcl && className && !impl )
325 protector = QString::fromLocal8Bit( className ).upper() + "_H";
326
327 if ( !protector.isEmpty() ) {
328 out << "#ifndef " << protector << endl;
329 out << "#define " << protector << endl;
330 }
331
332 if ( !pchFile.isEmpty() && impl ) {
333 out << "#include \"" << pchFile << "\" // PCH include" << endl;
334 }
335
336 if ( headerFile ) {
337 out << "#include \"" << headerFile << "\"" << endl << endl;
338 }
339
340 Uic( fileName, outputFile, out, doc, !impl, subcl, trmacro, className, nofwd );
341
342 if ( !protector.isEmpty() ) {
343 out << endl;
344 out << "#endif // " << protector << endl;
345 }
346 if ( fileOut.status() != IO_Ok ) {
347 qWarning( "uic: Error writing to file" );
348 if ( !outputFile.isEmpty() )
349 remove( outputFile );
350 }
351 return 0;
352}
Note: See TracBrowser for help on using the repository browser.