src/tools/uic3/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 int 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 nofwd = false;
       
    82     bool fix = false;
       
    83     bool deps = false;
       
    84     bool implicitIncludes = true;
       
    85     QByteArray pchFile;
       
    86 
       
    87 
       
    88     QApplication app(argc, argv, false);
       
    89 
       
    90     for (int n = 1; n < argc && error == 0; n++) {
       
    91         QByteArray arg = argv[n];
       
    92         if (arg[0] == '-') {                        // option
       
    93             QByteArray opt = arg.data() + 1;
       
    94             if (opt[0] == 'o') {                // output redirection
       
    95                 if (opt[1] == '\0') {
       
    96                     if (!(n < argc-1)) {
       
    97                         error = "Missing output-file name";
       
    98                         break;
       
    99                     }
       
   100                     outputFile = argv[++n];
       
   101                 } else
       
   102                     outputFile = opt.data() + 1;
       
   103             } else if (opt[0] == 'i' || opt == "impl") {
       
   104                 impl = true;
       
   105                 if (opt == "impl" || opt[1] == '\0') {
       
   106                     if (!(n < argc-1)) {
       
   107                         error = "Missing name of header file";
       
   108                         break;
       
   109                     }
       
   110                     headerFile = argv[++n];
       
   111                 } else
       
   112                     headerFile = opt.data() + 1;
       
   113             } else if (opt[0] == 'w' || opt == "wrap") {
       
   114                 wrap = true;
       
   115                 if (opt == "wrap" || opt[1] == '\0') {
       
   116                     if (!(n < argc-1)) {
       
   117                         error = "Missing name of converted UI file";
       
   118                         break;
       
   119                     }
       
   120                     convertedUiFile = argv[++n];
       
   121                 } else
       
   122                     convertedUiFile = opt.data() + 1;
       
   123             } else if (opt == "extract") {                // output redirection
       
   124                 extract = true;
       
   125                 if (!(n < argc-1)) {
       
   126                     error = "Missing output qrc-file name";
       
   127                     break;
       
   128                 }
       
   129                 qrcOutputFile = QFile::decodeName(argv[++n]);
       
   130             } else if ( opt[0] == 'e' || opt == "embed" ) {
       
   131                 imagecollection = true;
       
   132                 if ( opt == "embed" || opt[1] == '\0' ) {
       
   133                     if ( !(n < argc-1) ) {
       
   134                         error = "Missing name of project";
       
   135                         break;
       
   136                     }
       
   137                     projectName = argv[++n];
       
   138                 } else {
       
   139                     projectName = opt.data() + 1;
       
   140                 }
       
   141                 if ( argc > n+1 && qstrcmp( argv[n+1], "-f" ) == 0 ) {
       
   142                     imagecollection_tmpfile = true;
       
   143                     image_tmpfile = argv[n+2];
       
   144                     n += 2;
       
   145                 }
       
   146             } else if (opt == "d") {
       
   147                 deps = true;
       
   148             } else if (opt == "no-implicit-includes") {
       
   149                 implicitIncludes = false;
       
   150             } else if (opt == "nofwd") {
       
   151                 nofwd = true;
       
   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-no-implicit-includes Do not generate #include-directives for custom classes\n"
       
   257                  "\t-nounload          Do not unload plugins after processing\n"
       
   258                  "\t-tr func           Use func() instead of tr() for i18n\n"
       
   259                  "\t-L path            Additional plugin search path\n"
       
   260                  "\t-version           Display version of uic\n"
       
   261                  "\t-help              Display this information\n"
       
   262                  , argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]
       
   263            );
       
   264         return 1;
       
   265     }
       
   266 
       
   267     if (imagecollection_tmpfile) {
       
   268         QFile ifile(QFile::decodeName(image_tmpfile));
       
   269         if (ifile.open(QIODevice::ReadOnly)) {
       
   270             QTextStream ts(&ifile);
       
   271             QString s = ts.read();
       
   272             s = s.simplified();
       
   273             images = s.split(QLatin1Char(' '));
       
   274             for (QStringList::Iterator it = images.begin(); it != images.end(); ++it)
       
   275                 *it = (*it).simplified();
       
   276         }
       
   277     }
       
   278 
       
   279     QFile fileOut;
       
   280     if (!outputFile.isEmpty()) {
       
   281         fileOut.setFileName(QFile::decodeName(outputFile));
       
   282         if (!fileOut.open(QIODevice::WriteOnly)) {
       
   283             fprintf(stderr, "%s: Could not open output file '%s'\n", argv[0], outputFile.data());
       
   284             return 1;
       
   285         }
       
   286     } else {
       
   287         fileOut.open(QIODevice::WriteOnly, stdout);
       
   288     }
       
   289 
       
   290     QTextStream out(&fileOut);
       
   291 
       
   292     Ui3Reader ui3(out);
       
   293     ui3.setExtractImages(extract, qrcOutputFile);
       
   294 
       
   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, implicitIncludes);
       
   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         nofwd,
       
   392         implicitIncludes, 
       
   393         convertedUi);
       
   394 
       
   395     if (!protector.isEmpty()) {
       
   396         out << endl;
       
   397         out << "#endif // " << protector << endl;
       
   398     }
       
   399 
       
   400     if (fileOut.error() != QFile::NoError) {
       
   401         fprintf(stderr, "%s: Error writing to file\n", argv[0]);
       
   402         if (!outputFile.isEmpty())
       
   403             remove(outputFile);
       
   404     }
       
   405 
       
   406     return 0;
       
   407 }
       
   408 
       
   409 QT_END_NAMESPACE
       
   410 
       
   411 int main(int argc, char * argv[])
       
   412 {
       
   413     return QT_PREPEND_NAMESPACE(runUic3(argc, argv));
       
   414 }