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

Last change on this file since 11 was 8, checked in by dmik, 20 years ago

Transferred Qt for OS/2 version 3.3.1-rc5 sources from the CVS

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