diff tools/duic/uic.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/duic/uic.cpp	Mon May 11 16:01:50 2009 +0000
@@ -0,0 +1,336 @@
+/****************************************************************************
+**
+** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the tools applications of the Qt Toolkit.
+**
+** Commercial Usage
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License versions 2.0 or 3.0 as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file.  Please review the following information
+** to ensure GNU General Public Licensing requirements will be met:
+** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
+** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
+** exception, Nokia gives you certain additional rights. These rights
+** are described in the Nokia Qt GPL Exception version 1.3, included in
+** the file GPL_EXCEPTION.txt in this package.
+**
+** Qt for Windows(R) Licensees
+** As a special exception, Nokia, as the sole copyright holder for Qt
+** Designer, grants users of the Qt/Eclipse Integration plug-in the
+** right for the Qt/Eclipse Integration to link to functionality
+** provided by Qt Designer and its related libraries.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+**
+****************************************************************************/
+
+#include "uic.h"
+#include "ui4.h"
+#include "driver.h"
+#include "option.h"
+#include "treewalker.h"
+#include "validator.h"
+
+#ifdef QT_UIC_D_GENERATOR
+#include "dwriteincludes.h"
+#include "dwritedeclaration.h"
+#endif
+
+#ifdef QT_UIC_JAVA_GENERATOR
+#include "javawriteincludes.h"
+#include "javawritedeclaration.h"
+#endif
+
+#include <QtXml/QDomDocument>
+#include <QtCore/QFileInfo>
+#include <QtCore/QRegExp>
+#include <QtCore/QTextStream>
+#include <QtCore/QDateTime>
+
+#if defined Q_WS_WIN
+#include <qt_windows.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+Uic::Uic(Driver *d)
+     : drv(d),
+       out(d->output()),
+       opt(d->option()),
+       info(d),
+       externalPix(true)
+{
+}
+
+Uic::~Uic()
+{
+}
+
+bool Uic::printDependencies()
+{
+    QString fileName = opt.inputFile;
+
+    QFile f;
+    if (fileName.isEmpty())
+        f.open(stdin, QIODevice::ReadOnly);
+    else {
+        f.setFileName(fileName);
+        if (!f.open(QIODevice::ReadOnly))
+            return false;
+    }
+
+    QDomDocument doc;                        // ### generalize. share more code with the other tools!
+    if (!doc.setContent(&f))
+        return false;
+
+    QDomElement root = doc.firstChildElement();
+    DomUI *ui = new DomUI();
+    ui->read(root);
+
+    double version = ui->attributeVersion().toDouble();
+    if (version < 4.0) {
+        delete ui;
+
+        fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
+        return false;
+    }
+
+    if (DomIncludes *includes = ui->elementIncludes()) {
+        foreach (DomInclude *incl, includes->elementInclude()) {
+            QString file = incl->text();
+            if (file.isEmpty())
+                continue;
+
+            fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
+        }
+    }
+
+    if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
+        foreach (DomCustomWidget *customWidget, customWidgets->elementCustomWidget()) {
+            if (DomHeader *header = customWidget->elementHeader()) {
+                QString file = header->text();
+                if (file.isEmpty())
+                    continue;
+
+                fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
+            }
+        }
+    }
+
+    delete ui;
+
+    return true;
+}
+
+void Uic::writeCopyrightHeader(DomUI *ui)
+{
+    QString comment = ui->elementComment();
+    if (comment.size())
+        out << "/*\n" << comment << "\n*/\n\n";
+
+        out << "/********************************************************************************\n";
+        out << "** Form generated from reading ui file '" << QFileInfo(opt.inputFile).fileName() << "'\n";
+        out << "**\n";
+        out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
+        out << "**      " << QString::fromLatin1("by: QtD User Interface Compiler version %1\n").arg(QLatin1String(QT_VERSION_STR));
+        out << "**\n";
+        out << "** WARNING! All changes made in this file will be lost when recompiling ui file!\n";
+        out << "********************************************************************************/\n\n";
+}
+
+bool Uic::write(QIODevice *in)
+{
+    QDomDocument doc;
+    if (!doc.setContent(in))
+        return false;
+
+    if (option().generator == Option::JavaGenerator || option().generator == Option::DGenerator) {
+         // the Java generator ignores header protection
+        opt.headerProtection = false;
+    }
+
+    QDomElement root = doc.firstChildElement();
+    DomUI *ui = new DomUI();
+    ui->read(root);
+
+    double version = ui->attributeVersion().toDouble();
+    if (version < 4.0) {
+        delete ui;
+
+        fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
+        return false;
+    }
+
+    QString language = ui->attributeLanguage();
+
+
+    bool rtn = false;
+
+    if (option().generator == Option::JavaGenerator) {
+#ifdef QT_UIC_JAVA_GENERATOR
+        if (language.toLower() != QLatin1String("jambi")) {
+            fprintf(stderr, "uic: File is not a 'jambi' form\n");
+            return false;
+        }
+        rtn = jwrite (ui);
+#else
+        fprintf(stderr, "uic: option to generate java code not compiled in\n");
+#endif
+    } else {
+#ifdef QT_UIC_D_GENERATOR
+        if (!language.isEmpty() && language.toLower() != QLatin1String("d")) {
+            fprintf(stderr, "uic: File is not a 'd' ui file, language=%s\n", qPrintable(language));
+            return false;
+        }
+
+        rtn = write (ui);
+#else
+        fprintf(stderr, "uic: option to generate d code not compiled in\n");
+#endif
+    }
+
+    delete ui;
+
+    return rtn;
+}
+
+#ifdef QT_UIC_D_GENERATOR
+bool Uic::write(DomUI *ui)
+{
+    using namespace D;
+
+    if (!ui || !ui->elementWidget())
+        return false;
+
+    if (opt.copyrightHeader)
+        writeCopyrightHeader(ui);
+
+    if (opt.headerProtection) {
+        writeHeaderProtectionStart();
+        out << "\n";
+    }
+
+    pixFunction = ui->elementPixmapFunction();
+    if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
+        pixFunction = QLatin1String("qPixmapFromMimeSource");
+
+    externalPix = ui->elementImages() == 0;
+
+    info.acceptUI(ui);
+    cWidgetsInfo.acceptUI(ui);
+    WriteIncludes writeIncludes(this);
+    writeIncludes.acceptUI(ui);
+
+    Validator(this).acceptUI(ui);
+    WriteDeclaration(this, writeIncludes.scriptsActivated()).acceptUI(ui);
+
+    if (opt.headerProtection)
+        writeHeaderProtectionEnd();
+
+    return true;
+}
+#endif
+
+#ifdef QT_UIC_JAVA_GENERATOR
+bool Uic::jwrite(DomUI *ui)
+{
+    using namespace Java;
+
+    if (!ui || !ui->elementWidget())
+        return false;
+
+    if (opt.copyrightHeader)
+        writeCopyrightHeader(ui);
+
+    pixFunction = ui->elementPixmapFunction();
+    if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
+        pixFunction = QLatin1String("qPixmapFromMimeSource");
+
+    externalPix = ui->elementImages() == 0;
+
+    info.acceptUI(ui);
+    cWidgetsInfo.acceptUI(ui);
+    WriteIncludes(this).acceptUI(ui);
+
+    Validator(this).acceptUI(ui);
+    WriteDeclaration(this).acceptUI(ui);
+
+    return true;
+}
+#endif
+
+#ifdef QT_UIC_D_GENERATOR
+
+void Uic::writeHeaderProtectionStart()
+{
+    QString h = drv->headerFileName();
+    out << "#ifndef " << h << "\n"
+        << "#define " << h << "\n";
+}
+
+void Uic::writeHeaderProtectionEnd()
+{
+    QString h = drv->headerFileName();
+    out << "#endif // " << h << "\n";
+}
+#endif
+
+bool Uic::isMainWindow(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("Q3MainWindow"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QMainWindow"));
+}
+
+bool Uic::isToolBar(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("Q3ToolBar"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QToolBar"));
+}
+
+bool Uic::isButton(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("QRadioButton"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QToolButton"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QCheckBox"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QPushButton"));
+}
+
+bool Uic::isContainer(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QTabWidget"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QScrollArea"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QMdiArea"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QWizard"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"));
+}
+
+bool Uic::isStatusBar(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"));
+}
+
+bool Uic::isMenuBar(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"));
+}
+
+bool Uic::isMenu(const QString &className) const
+{
+    return customWidgetsInfo()->extends(className, QLatin1String("QMenu"))
+        || customWidgetsInfo()->extends(className, QLatin1String("QPopupMenu"));
+}
+
+QT_END_NAMESPACE