~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/tools/uic3/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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