src/tools/uic3/uic.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 "uic.h"
       
    43 #include "ui4.h"
       
    44 #include "driver.h"
       
    45 #include "option.h"
       
    46 #include "treewalker.h"
       
    47 #include "validator.h"
       
    48 
       
    49 #ifdef QT_UIC_CPP_GENERATOR
       
    50 #include "cppwriteincludes.h"
       
    51 #include "cppwritedeclaration.h"
       
    52 #endif
       
    53 
       
    54 #ifdef QT_UIC_JAVA_GENERATOR
       
    55 #include "javawriteincludes.h"
       
    56 #include "javawritedeclaration.h"
       
    57 #endif
       
    58 
       
    59 #include <QtXml/QDomDocument>
       
    60 #include <QtCore/QFileInfo>
       
    61 #include <QtCore/QRegExp>
       
    62 #include <QtCore/QTextStream>
       
    63 #include <QtCore/QDateTime>
       
    64 
       
    65 #if defined Q_WS_WIN
       
    66 #include <qt_windows.h>
       
    67 #endif
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 Uic::Uic(Driver *d)
       
    72      : drv(d),
       
    73        out(d->output()),
       
    74        opt(d->option()),
       
    75        info(d),
       
    76        externalPix(true)
       
    77 {
       
    78 }
       
    79 
       
    80 Uic::~Uic()
       
    81 {
       
    82 }
       
    83 
       
    84 bool Uic::printDependencies()
       
    85 {
       
    86     QString fileName = opt.inputFile;
       
    87 
       
    88     QFile f;
       
    89     if (fileName.isEmpty())
       
    90         f.open(stdin, QIODevice::ReadOnly);
       
    91     else {
       
    92         f.setFileName(fileName);
       
    93         if (!f.open(QIODevice::ReadOnly))
       
    94             return false;
       
    95     }
       
    96 
       
    97     QDomDocument doc;                        // ### generalize. share more code with the other tools!
       
    98     if (!doc.setContent(&f))
       
    99         return false;
       
   100 
       
   101     QDomElement root = doc.firstChildElement();
       
   102     DomUI *ui = new DomUI();
       
   103     ui->read(root);
       
   104 
       
   105     double version = ui->attributeVersion().toDouble();
       
   106     if (version < 4.0) {
       
   107         delete ui;
       
   108 
       
   109         fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
       
   110         return false;
       
   111     }
       
   112 
       
   113     if (DomIncludes *includes = ui->elementIncludes()) {
       
   114         foreach (DomInclude *incl, includes->elementInclude()) {
       
   115             QString file = incl->text();
       
   116             if (file.isEmpty())
       
   117                 continue;
       
   118 
       
   119             fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
       
   120         }
       
   121     }
       
   122 
       
   123     if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
       
   124         foreach (DomCustomWidget *customWidget, customWidgets->elementCustomWidget()) {
       
   125             if (DomHeader *header = customWidget->elementHeader()) {
       
   126                 QString file = header->text();
       
   127                 if (file.isEmpty())
       
   128                     continue;
       
   129 
       
   130                 fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
       
   131             }
       
   132         }
       
   133     }
       
   134 
       
   135     delete ui;
       
   136 
       
   137     return true;
       
   138 }
       
   139 
       
   140 void Uic::writeCopyrightHeader(DomUI *ui)
       
   141 {
       
   142     QString comment = ui->elementComment();
       
   143     if (comment.size())
       
   144         out << "/*\n" << comment << "\n*/\n\n";
       
   145 
       
   146         out << "/********************************************************************************\n";
       
   147         out << "** Form generated from reading UI file '" << QFileInfo(opt.inputFile).fileName() << "'\n";
       
   148         out << "**\n";
       
   149         out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
       
   150         out << "**      " << QString::fromLatin1("by: Qt User Interface Compiler version %1\n").arg(QLatin1String(QT_VERSION_STR));
       
   151         out << "**\n";
       
   152         out << "** WARNING! All changes made in this file will be lost when recompiling UI file!\n";
       
   153         out << "********************************************************************************/\n\n";
       
   154 }
       
   155 
       
   156 bool Uic::write(QIODevice *in)
       
   157 {
       
   158     QDomDocument doc;
       
   159     if (!doc.setContent(in))
       
   160         return false;
       
   161 
       
   162     if (option().generator == Option::JavaGenerator) {
       
   163          // the Java generator ignores header protection
       
   164         opt.headerProtection = false;
       
   165     }
       
   166 
       
   167     QDomElement root = doc.firstChildElement();
       
   168     DomUI *ui = new DomUI();
       
   169     ui->read(root);
       
   170 
       
   171     double version = ui->attributeVersion().toDouble();
       
   172     if (version < 4.0) {
       
   173         delete ui;
       
   174 
       
   175         fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
       
   176         return false;
       
   177     }
       
   178 
       
   179     QString language = ui->attributeLanguage();
       
   180 
       
   181 
       
   182     bool rtn = false;
       
   183 
       
   184     if (option().generator == Option::JavaGenerator) {
       
   185 #ifdef QT_UIC_JAVA_GENERATOR
       
   186         if (language.toLower() != QLatin1String("jambi")) {
       
   187             fprintf(stderr, "uic: File is not a 'jambi' form\n");
       
   188             return false;
       
   189         }
       
   190         rtn = jwrite (ui);
       
   191 #else
       
   192         fprintf(stderr, "uic: option to generate java code not compiled in\n");
       
   193 #endif
       
   194     } else {
       
   195 #ifdef QT_UIC_CPP_GENERATOR
       
   196         if (!language.isEmpty() && language.toLower() != QLatin1String("c++")) {
       
   197             fprintf(stderr, "uic: File is not a 'c++' ui file, language=%s\n", qPrintable(language));
       
   198             return false;
       
   199         }
       
   200 
       
   201         rtn = write (ui);
       
   202 #else
       
   203         fprintf(stderr, "uic: option to generate cpp code not compiled in\n");
       
   204 #endif
       
   205     }
       
   206 
       
   207     delete ui;
       
   208 
       
   209     return rtn;
       
   210 }
       
   211 
       
   212 #ifdef QT_UIC_CPP_GENERATOR
       
   213 bool Uic::write(DomUI *ui)
       
   214 {
       
   215     using namespace CPP;
       
   216 
       
   217     if (!ui || !ui->elementWidget())
       
   218         return false;
       
   219 
       
   220     if (opt.copyrightHeader)
       
   221         writeCopyrightHeader(ui);
       
   222 
       
   223     if (opt.headerProtection) {
       
   224         writeHeaderProtectionStart();
       
   225         out << "\n";
       
   226     }
       
   227 
       
   228     pixFunction = ui->elementPixmapFunction();
       
   229     if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
       
   230         pixFunction = QLatin1String("qPixmapFromMimeSource");
       
   231 
       
   232     externalPix = ui->elementImages() == 0;
       
   233 
       
   234     info.acceptUI(ui);
       
   235     cWidgetsInfo.acceptUI(ui);
       
   236     WriteIncludes writeIncludes(this);
       
   237     writeIncludes.acceptUI(ui);
       
   238 
       
   239     Validator(this).acceptUI(ui);
       
   240     WriteDeclaration(this, writeIncludes.scriptsActivated()).acceptUI(ui);
       
   241 
       
   242     if (opt.headerProtection)
       
   243         writeHeaderProtectionEnd();
       
   244 
       
   245     return true;
       
   246 }
       
   247 #endif
       
   248 
       
   249 #ifdef QT_UIC_JAVA_GENERATOR
       
   250 bool Uic::jwrite(DomUI *ui)
       
   251 {
       
   252     using namespace Java;
       
   253 
       
   254     if (!ui || !ui->elementWidget())
       
   255         return false;
       
   256 
       
   257     if (opt.copyrightHeader)
       
   258         writeCopyrightHeader(ui);
       
   259 
       
   260     pixFunction = ui->elementPixmapFunction();
       
   261     if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
       
   262         pixFunction = QLatin1String("qPixmapFromMimeSource");
       
   263 
       
   264     externalPix = ui->elementImages() == 0;
       
   265 
       
   266     info.acceptUI(ui);
       
   267     cWidgetsInfo.acceptUI(ui);
       
   268     WriteIncludes(this).acceptUI(ui);
       
   269 
       
   270     Validator(this).acceptUI(ui);
       
   271     WriteDeclaration(this).acceptUI(ui);
       
   272 
       
   273     return true;
       
   274 }
       
   275 #endif
       
   276 
       
   277 #ifdef QT_UIC_CPP_GENERATOR
       
   278 
       
   279 void Uic::writeHeaderProtectionStart()
       
   280 {
       
   281     QString h = drv->headerFileName();
       
   282     out << "#ifndef " << h << "\n"
       
   283         << "#define " << h << "\n";
       
   284 }
       
   285 
       
   286 void Uic::writeHeaderProtectionEnd()
       
   287 {
       
   288     QString h = drv->headerFileName();
       
   289     out << "#endif // " << h << "\n";
       
   290 }
       
   291 #endif
       
   292 
       
   293 bool Uic::isMainWindow(const QString &className) const
       
   294 {
       
   295     return customWidgetsInfo()->extends(className, QLatin1String("Q3MainWindow"))
       
   296         || customWidgetsInfo()->extends(className, QLatin1String("QMainWindow"));
       
   297 }
       
   298 
       
   299 bool Uic::isToolBar(const QString &className) const
       
   300 {
       
   301     return customWidgetsInfo()->extends(className, QLatin1String("Q3ToolBar"))
       
   302         || customWidgetsInfo()->extends(className, QLatin1String("QToolBar"));
       
   303 }
       
   304 
       
   305 bool Uic::isButton(const QString &className) const
       
   306 {
       
   307     return customWidgetsInfo()->extends(className, QLatin1String("QRadioButton"))
       
   308         || customWidgetsInfo()->extends(className, QLatin1String("QToolButton"))
       
   309         || customWidgetsInfo()->extends(className, QLatin1String("QCheckBox"))
       
   310         || customWidgetsInfo()->extends(className, QLatin1String("QPushButton"))
       
   311         || customWidgetsInfo()->extends(className, QLatin1String("QCommandLinkButton"));
       
   312 }
       
   313 
       
   314 bool Uic::isContainer(const QString &className) const
       
   315 {
       
   316     return customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget"))
       
   317         || customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))
       
   318         || customWidgetsInfo()->extends(className, QLatin1String("QTabWidget"))
       
   319         || customWidgetsInfo()->extends(className, QLatin1String("QScrollArea"))
       
   320         || customWidgetsInfo()->extends(className, QLatin1String("QMdiArea"))
       
   321         || customWidgetsInfo()->extends(className, QLatin1String("QWizard"))
       
   322         || customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"));
       
   323 }
       
   324 
       
   325 bool Uic::isStatusBar(const QString &className) const
       
   326 {
       
   327     return customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"));
       
   328 }
       
   329 
       
   330 bool Uic::isMenuBar(const QString &className) const
       
   331 {
       
   332     return customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"));
       
   333 }
       
   334 
       
   335 bool Uic::isMenu(const QString &className) const
       
   336 {
       
   337     return customWidgetsInfo()->extends(className, QLatin1String("QMenu"))
       
   338         || customWidgetsInfo()->extends(className, QLatin1String("QPopupMenu"));
       
   339 }
       
   340 
       
   341 QT_END_NAMESPACE