⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writeinitialization.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** 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 "writeinitialization.h"#include "driver.h"#include "ui4.h"#include "utils.h"#include "uic.h"#include "databaseinfo.h"#include "globaldefs.h"#include <QTextStream>#include <QtDebug>#include <limits.h>WriteInitialization::WriteInitialization(Uic *uic)    : driver(uic->driver()), output(uic->output()), option(uic->option()),      m_defaultMargin(INT_MIN), m_defaultSpacing(INT_MIN),      delayedOut(&m_delayedInitialization, QIODevice::WriteOnly),      refreshOut(&m_refreshInitialization, QIODevice::WriteOnly),      actionOut(&m_delayedActionInitialization, QIODevice::WriteOnly){    this->uic = uic;}void WriteInitialization::acceptUI(DomUI *node){    m_registeredImages.clear();    m_actionGroupChain.push(0);    m_widgetChain.push(0);    m_layoutChain.push(0);    acceptLayoutDefault(node->elementLayoutDefault());    acceptLayoutFunction(node->elementLayoutFunction());    if (node->elementCustomWidgets())        TreeWalker::acceptCustomWidgets(node->elementCustomWidgets());    if (node->elementImages())        TreeWalker::acceptImages(node->elementImages());    if (option.generateImplemetation)        output << "#include <" << driver->headerFileName() << ">\n\n";    m_stdsetdef = true;    if (node->hasAttributeStdSetDef())        m_stdsetdef = node->attributeStdSetDef();    QString className = node->elementClass() + option.postfix;    m_generatedClass = className;    QString varName = driver->findOrInsertWidget(node->elementWidget());    m_registeredWidgets.insert(varName, node->elementWidget()); // register the main widget    QString widgetClassName = node->elementWidget()->attributeClass();    output << option.indent << "void " << "setupUi(" << widgetClassName << " *" << varName << ")\n"           << option.indent << "{\n";    QStringList connections = uic->databaseInfo()->connections();    for (int i=0; i<connections.size(); ++i) {        QString connection = connections.at(i);        if (connection == QLatin1String("(default)"))            continue;        QString varConn = connection + QLatin1String("Connection");        output << option.indent << varConn << " = QSqlDatabase::database(" << fixString(connection, option.indent) << ");\n";    }    acceptWidget(node->elementWidget());    for (int i=0; i<m_buddies.size(); ++i) {        const Buddy &b = m_buddies.at(i);        if (!m_registeredWidgets.contains(b.objName)) {            fprintf(stderr, "'%s' isn't a valid widget\n", b.objName.toLatin1().data());            continue;        } else if (!m_registeredWidgets.contains(b.buddy)) {            fprintf(stderr, "'%s' isn't a valid widget\n", b.buddy.toLatin1().data());            continue;        }        output << option.indent << b.objName << "->setBuddy(" << b.buddy << ");\n";    }    if (node->elementTabStops())        acceptTabStops(node->elementTabStops());    if (m_delayedActionInitialization.size())        output << "\n" << m_delayedActionInitialization;    output << option.indent << "retranslateUi(" << varName << ");\n";    if (node->elementConnections())        acceptConnections(node->elementConnections());    if (!m_delayedInitialization.isEmpty())        output << "\n" << m_delayedInitialization << "\n";    if (option.autoConnection)        output << "\n" << option.indent << "QMetaObject::connectSlotsByName(" << varName << ");\n";    output << option.indent << "} // setupUi\n\n";    if (m_delayedActionInitialization.isEmpty()) {        m_refreshInitialization += option.indent + QLatin1String("Q_UNUSED(") + varName + QLatin1String(");\n");    }    output << option.indent << "void " << "retranslateUi(" << widgetClassName << " *" << varName << ")\n"           << option.indent << "{\n"           << m_refreshInitialization           << option.indent << "} // retranslateUi\n\n";    m_layoutChain.pop();    m_widgetChain.pop();    m_actionGroupChain.pop();}void WriteInitialization::acceptWidget(DomWidget *node){    QString className = node->attributeClass();    QString varName = driver->findOrInsertWidget(node);    m_registeredWidgets.insert(varName, node); // register the current widget    QString parentWidget, parentClass;    if (m_widgetChain.top()) {        parentWidget = driver->findOrInsertWidget(m_widgetChain.top());        parentClass = m_widgetChain.top()->attributeClass();    }    QString savedParentWidget = parentWidget;    if (uic->isContainer(parentClass))        parentWidget.clear();    if (m_widgetChain.size() != 1)        output << option.indent << varName << " = new " << uic->customWidgetsInfo()->realClassName(className) << "(" << parentWidget << ");\n";    parentWidget = savedParentWidget;    if (uic->customWidgetsInfo()->extends(className, QLatin1String("QComboBox"))) {        initializeComboBox(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QListWidget"))) {        initializeListWidget(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QTreeWidget"))) {        initializeTreeWidget(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QTableWidget"))) {        initializeTableWidget(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListBox"))) {        initializeQ3ListBox(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3ListView"))) {        initializeQ3ListView(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3IconView"))) {        initializeQ3IconView(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3Table"))) {        initializeQ3Table(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataTable"))) {        initializeQ3SqlDataTable(node);    } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("Q3DataBrowser"))) {        initializeQ3SqlDataBrowser(node);    }    if (uic->isButton(className)) {        QHash<QString, DomProperty*> attributes = propertyMap(node->elementAttribute());        if (DomProperty *prop = attributes.value(QLatin1String("buttonGroup"))) {            QString groupName = toString(prop->elementString());            if (!m_buttonGroups.contains(groupName)) {                m_buttonGroups.insert(groupName, driver->findOrInsertName(groupName));                QString g = m_buttonGroups.value(groupName);                output << option.indent << "QButtonGroup *" << g << " = new QButtonGroup(" << m_generatedClass << ");\n";            }            QString g = m_buttonGroups.value(groupName);            output << option.indent << g << "->addButton(" << varName << ");\n";        }    }    writeProperties(varName, className, node->elementProperty());    if (uic->customWidgetsInfo()->extends(className, QLatin1String("QMenu")) && parentWidget.size()) {        initializeMenu(node, parentWidget);    }    if (node->elementLayout().isEmpty())        m_layoutChain.push(0);    m_widgetChain.push(node);    m_layoutChain.push(0);    TreeWalker::acceptWidget(node);    m_layoutChain.pop();    m_widgetChain.pop();    QHash<QString, DomProperty*> attributes = propertyMap(node->elementAttribute());    QString title = QLatin1String("Page");    if (DomProperty *ptitle = attributes.value(QLatin1String("title"))) {        title = toString(ptitle->elementString());    }    QString label = QLatin1String("Page");    if (DomProperty *plabel = attributes.value(QLatin1String("label"))) {        label = toString(plabel->elementString());    }    int id = -1;    if (DomProperty *pid = attributes.value(QLatin1String("id"))) {        id = pid->elementNumber();    }    if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QMainWindow"))            || uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow"))) {        if (uic->customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"))) {            if (!uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3MainWindow")))                output << option.indent << parentWidget << "->setMenuBar(" << varName <<");\n";        } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QToolBar"))) {            QString area;            if (DomProperty *pstyle = attributes.value(QLatin1String("toolBarArea"))) {                area += QLatin1String("static_cast<Qt::ToolBarArea>(");                area += QString::number(pstyle->elementNumber());                area += "), ";            }            output << option.indent << parentWidget << "->addToolBar(" << area << varName << ");\n";        } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QDockWidget"))) {            QString area;            if (DomProperty *pstyle = attributes.value(QLatin1String("dockWidgetArea"))) {                area += QLatin1String("static_cast<Qt::DockWidgetArea>(");                area += QString::number(pstyle->elementNumber());                area += "), ";            }            output << option.indent << parentWidget << "->addDockWidget(" << area << varName << ");\n";        } else if (uic->customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"))) {            output << option.indent << parentWidget << "->setStatusBar(" << varName << ");\n";        } else if (className == QLatin1String("QWidget")) {            output << option.indent << parentWidget << "->setCentralWidget(" << varName << ");\n";        }    }    if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QStackedWidget"))) {        output << option.indent << parentWidget << "->addWidget(" << varName << ");\n";    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBar"))) {        output << option.indent << parentWidget << "->addWidget(" << varName << ");\n";    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3WidgetStack"))) {        output << option.indent << parentWidget << "->addWidget(" << varName << ", " << id << ");\n";    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QDockWidget"))) {        output << option.indent << parentWidget << "->setWidget(" << varName << ");\n";    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QSplitter"))) {        output << option.indent << parentWidget << "->addWidget(" << varName << ");\n";    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QToolBox"))) {        QString icon;        if (DomProperty *picon = attributes.value(QLatin1String("icon"))) {            icon += QLatin1String(", ") + pixCall(picon);        }        output << option.indent << parentWidget << "->addItem(" << varName << icon << ", " << trCall(label) << ");\n";        refreshOut << option.indent << parentWidget << "->setItemText("                   << parentWidget << "->indexOf(" << varName << "), " << trCall(label) << ");\n";        if (DomProperty *ptoolTip = attributes.value(QLatin1String("toolTip"))) {            refreshOut << option.indent << parentWidget << "->setItemToolTip("                       << parentWidget << "->indexOf(" << varName << "), " << trCall(ptoolTip->elementString()) << ");\n";        }    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("QTabWidget"))) {        QString icon;        if (DomProperty *picon = attributes.value(QLatin1String("icon"))) {            icon += QLatin1String(", ") + pixCall(picon);        }        output << option.indent << parentWidget << "->addTab(" << varName << icon << ", " << trCall(title) << ");\n";        refreshOut << option.indent << parentWidget << "->setTabText("                   << parentWidget << "->indexOf(" << varName << "), " << trCall(title) << ");\n";        if (DomProperty *ptoolTip = attributes.value(QLatin1String("toolTip"))) {            refreshOut << option.indent << parentWidget << "->setTabToolTip("                       << parentWidget << "->indexOf(" << varName << "), " << trCall(ptoolTip->elementString()) << ");\n";        }    } else if (uic->customWidgetsInfo()->extends(parentClass, QLatin1String("Q3Wizard"))) {        output << option.indent << parentWidget << "->addPage(" << varName << ", " << trCall(title) << ");\n";        refreshOut << option.indent << parentWidget << "->setTitle("                   << varName << ", " << trCall(title) << ");\n";    }    if (node->elementLayout().isEmpty())        m_layoutChain.pop();}void WriteInitialization::acceptLayout(DomLayout *node){    QString className = node->attributeClass();    QString varName = driver->findOrInsertLayout(node);    QHash<QString, DomProperty*> properties = propertyMap(node->elementProperty());    bool isGroupBox = false;    if (m_widgetChain.top()) {        QString parentWidget = m_widgetChain.top()->attributeClass();        if (!m_layoutChain.top() && (uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3GroupBox"))                        || uic->customWidgetsInfo()->extends(parentWidget, QLatin1String("Q3ButtonGroup")))) {            QString parent = driver->findOrInsertWidget(m_widgetChain.top());            isGroupBox = true;            // special case for group box            int margin = m_defaultMargin;            int spacing = m_defaultSpacing;            if (properties.contains(QLatin1String("margin")))                margin = properties.value(QLatin1String("margin"))->elementNumber();            if (properties.contains(QLatin1String("spacing")))                spacing = properties.value(QLatin1String("spacing"))->elementNumber();            output << option.indent << parent << "->setColumnLayout(0, Qt::Vertical);\n";            if (spacing != INT_MIN) {                QString value = QString::number(spacing);                if (!m_spacingFunction.isEmpty() && spacing == m_defaultSpacing)                    value = m_spacingFunction + QLatin1String("()");                output << option.indent << parent << "->layout()->setSpacing(" << value << ");\n";            }            if (margin != INT_MIN) {                QString value = QString::number(margin);                if (!m_marginFunction.isEmpty() && margin == m_defaultMargin)                    value = m_marginFunction + QLatin1String("()");                output << option.indent << parent << "->layout()->setMargin(" << value << ");\n";            }        }    }    output << option.indent << varName << " = new " << className << "(";    if (isGroupBox) {        output << driver->findOrInsertWidget(m_widgetChain.top()) << "->layout()";    } else if (!m_layoutChain.top()) {        output << driver->findOrInsertWidget(m_widgetChain.top());    }    output << ");\n";    QList<DomProperty*> layoutProperties = node->elementProperty();    if (isGroupBox) {        output << option.indent << varName << "->setAlignment(Qt::AlignTop);\n";    } else {        int margin = m_defaultMargin;        int spacing = m_defaultSpacing;        if (properties.contains(QLatin1String("margin"))) {            DomProperty *p = properties.value(QLatin1String("margin"));            Q_ASSERT(p != 0);            margin = properties.value(QLatin1String("margin"))->elementNumber();            layoutProperties.removeAt(layoutProperties.indexOf(p));        }        if (properties.contains(QLatin1String("spacing"))) {            DomProperty *p = properties.value(QLatin1String("spacing"));            Q_ASSERT(p != 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -