📄 form.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the tools applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.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.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "ui3reader.h"#include "parser.h"#include "domtool.h"#include "globaldefs.h"// uic4#include "uic.h"#include "ui4.h"#include "driver.h"#include "option.h"#include <QStringList>#include <QFile>#include <QFileInfo>#include <QDir>#include <QRegExp>#include <QtDebug>QByteArray combinePath(const char *infile, const char *outfile){ QFileInfo inFileInfo(QDir::current(), QFile::decodeName(infile)); QFileInfo outFileInfo(QDir::current(), QFile::decodeName(outfile)); int numCommonComponents = 0; QStringList inSplitted = inFileInfo.dir().canonicalPath().split(QLatin1Char('/')); QStringList outSplitted = outFileInfo.dir().canonicalPath().split(QLatin1Char('/')); while (!inSplitted.isEmpty() && !outSplitted.isEmpty() && inSplitted.first() == outSplitted.first()) { inSplitted.erase(inSplitted.begin()); outSplitted.erase(outSplitted.begin()); numCommonComponents++; } if (numCommonComponents < 2) { /* The paths don't have the same drive, or they don't have the same root directory. Use an absolute path. */ return QFile::encodeName(inFileInfo.absoluteFilePath()); } else { /* The paths have something in common. Use a path relative to the output file. */ while (!outSplitted.isEmpty()) { outSplitted.erase(outSplitted.begin()); inSplitted.prepend(QLatin1String("..")); } inSplitted.append(inFileInfo.fileName()); return QFile::encodeName(inSplitted.join(QLatin1String("/"))); }}/*! Creates a declaration (header file) for the form given in \a e \sa createFormImpl()*/void Ui3Reader::createFormDecl(const QDomElement &e){ QDomElement body = e; QDomElement n; QDomNodeList nl; int i; QString objClass = getClassName(e); if (objClass.isEmpty()) return; QString objName = getObjectName(e); QStringList typeDefs; QMap<QString, CustomInclude> customWidgetIncludes; /* We are generating a few QImage members that are not strictly necessary in some cases. Ideally, we would use requiredImage, which is computed elsewhere, to keep the generated .h and .cpp files synchronized. */ // at first the images QMap<QString, int> customWidgets; QStringList forwardDecl; QStringList forwardDecl2; QString exportMacro; for (n = e; !n.isNull(); n = n.nextSibling().toElement()) { if (n.tagName().toLower() == QLatin1String("customwidgets")) { QDomElement n2 = n.firstChild().toElement(); while (!n2.isNull()) { if (n2.tagName().toLower() == QLatin1String("customwidget")) { QDomElement n3 = n2.firstChild().toElement(); QString cl; while (!n3.isNull()) { QString tagName = n3.tagName().toLower(); if (tagName == QLatin1String("class")) { cl = n3.firstChild().toText().data(); if (!nofwd) forwardDecl << cl; customWidgets.insert(cl, 0); } else if (tagName == QLatin1String("header")) { CustomInclude ci; ci.header = n3.firstChild().toText().data(); ci.location = n3.attribute(QLatin1String("location"), QLatin1String("global")); if (!ci.header.isEmpty()) forwardDecl.removeAll(cl); customWidgetIncludes.insert(cl, ci); } n3 = n3.nextSibling().toElement(); } } n2 = n2.nextSibling().toElement(); } } } // register the object and unify its name objName = registerObject(objName); QString protector = objName.toUpper() + QLatin1String("_H"); protector.replace(QLatin1String("::"), QLatin1String("_")); out << "#ifndef " << protector << endl; out << "#define " << protector << endl; out << endl; out << "#include <qvariant.h>" << endl; // for broken HP-UX compilers QStringList globalIncludes, localIncludes; { QMap<QString, CustomInclude>::Iterator it = customWidgetIncludes.find(objClass); if (it != customWidgetIncludes.end()) { if ((*it).location == QLatin1String("global")) globalIncludes += (*it).header; else localIncludes += (*it).header; } } QStringList::Iterator it; globalIncludes = unique(globalIncludes); for (it = globalIncludes.begin(); it != globalIncludes.end(); ++it) { if (!(*it).isEmpty()) { QString header = fixHeaderName(*it); out << "#include <" << header << ">" << endl; } } localIncludes = unique(localIncludes); for (it = localIncludes.begin(); it != localIncludes.end(); ++it) { if (!(*it).isEmpty()) { QString header = fixHeaderName(*it); out << "#include \"" << header << "\"" << endl; } } out << endl; bool dbForm = false; registerDatabases(e); dbConnections = unique(dbConnections); if (dbForms[QLatin1String("(default)")].count()) dbForm = true; bool subDbForms = false; for (it = dbConnections.begin(); it != dbConnections.end(); ++it) { if (!(*it).isEmpty() && (*it) != QLatin1String("(default)")) { if (dbForms[(*it)].count()) { subDbForms = true; break; } } } // some typedefs, maybe typeDefs = unique(typeDefs); for (it = typeDefs.begin(); it != typeDefs.end(); ++it) { if (!(*it).isEmpty()) out << "typedef " << *it << ";" << endl; } nl = e.parentNode().toElement().elementsByTagName(QLatin1String("forward")); for (i = 0; i < (int) nl.length(); i++) forwardDecl2 << fixDeclaration(nl.item(i).toElement().firstChild().toText().data()); nl = e.parentNode().toElement().elementsByTagName(QLatin1String("exportmacro")); if (nl.length() == 1) exportMacro = nl.item(0).firstChild().toText().data(); forwardDecl = unique(forwardDecl); for (it = forwardDecl.begin(); it != forwardDecl.end(); ++it) { if (!(*it).isEmpty() && (*it) != objClass) { QString forwardName = *it; QStringList forwardNamespaces = forwardName.split(QLatin1String("::")); forwardName = forwardNamespaces.last(); forwardNamespaces.removeAt(forwardNamespaces.size()-1); QStringList::ConstIterator ns = forwardNamespaces.begin(); while (ns != forwardNamespaces.end()) { out << "namespace " << *ns << " {" << endl; ++ns; } out << "class " << forwardName << ";" << endl; for (int i = 0; i < (int) forwardNamespaces.count(); i++) out << "}" << endl; } } for (it = forwardDecl2.begin(); it != forwardDecl2.end(); ++it) { QString fd = *it; fd = fd.trimmed(); if (!fd.endsWith(QLatin1String(";"))) fd += QLatin1String(";"); out << fd << endl; } out << endl; Driver d; d.option().headerProtection = false; d.option().copyrightHeader = false; if (trmacro.size()) d.option().translateFunction = trmacro; DomUI *ui = generateUi4(e); d.uic(fileName, ui, &out); delete ui; QStringList::ConstIterator ns = namespaces.begin(); while (ns != namespaces.end()) { out << "namespace " << *ns << " {" << endl; ++ns; } out << "class "; if (!exportMacro.isEmpty()) out << exportMacro << " "; out << bareNameOfClass << " : public " << objClass << ", public Ui::" << bareNameOfClass << endl << "{" << endl; /* qmake ignore Q_OBJECT */ out << " Q_OBJECT" << endl; out << endl; out << "public:" << endl; // constructor if (objClass == QLatin1String("QDialog") || objClass == QLatin1String("QWizard")) { out << " " << bareNameOfClass << "(QWidget* parent = 0, const char* name = 0, bool modal = false, Qt::WFlags fl = 0);" << endl; } else if (objClass == QLatin1String("QWidget")) { out << " " << bareNameOfClass << "(QWidget* parent = 0, const char* name = 0, Qt::WFlags fl = 0);" << endl; } else if (objClass == QLatin1String("QMainWindow") || objClass == QLatin1String("Q3MainWindow")) { out << " " << bareNameOfClass << "(QWidget* parent = 0, const char* name = 0, Qt::WFlags fl = Qt::WType_TopLevel);" << endl; isMainWindow = true; } else { out << " " << bareNameOfClass << "(QWidget* parent = 0, const char* name = 0);" << endl; } // destructor out << " ~" << bareNameOfClass << "();" << endl; out << endl; // database connections dbConnections = unique(dbConnections); bool hadOutput = false; for (it = dbConnections.begin(); it != dbConnections.end(); ++it) { if (!(*it).isEmpty()) { // only need pointers to non-default connections if ((*it) != QLatin1String("(default)") && !(*it).isEmpty()) { out << indent << "QSqlDatabase* " << *it << "Connection;" << endl; hadOutput = true; } } } if (hadOutput) out << endl; QStringList publicSlots, protectedSlots, privateSlots; QStringList publicSlotTypes, protectedSlotTypes, privateSlotTypes; QStringList publicSlotSpecifier, protectedSlotSpecifier, privateSlotSpecifier; nl = e.parentNode().toElement().elementsByTagName(QLatin1String("slot")); for (i = 0; i < (int) nl.length(); i++) { n = nl.item(i).toElement(); if (n.parentNode().toElement().tagName() != QLatin1String("slots")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -