quiloader.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 585 行 · 第 1/2 页

CPP
585
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, 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.**** Trolltech reserves all rights not expressly granted herein.**** 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 "quiloader.h"#include "customwidget.h"#include <formbuilder.h>#include <ui4_p.h>#include <qdebug.h>#include <QAction>#include <QActionGroup>#include <QApplication>#include <QDir>#include <QLibraryInfo>#include <QLayout>#include <QWidget>#include <QMap>#include <QTabWidget>#include <QTreeWidget>#include <QListWidget>#include <QTableWidget>#include <QToolBox>#include <QComboBox>#include <QFontComboBox>#include <private/qobject_p.h>typedef QMap<QString, bool> widget_map;Q_GLOBAL_STATIC(widget_map, g_widgets)#ifdef QFORMINTERNAL_NAMESPACEnamespace QFormInternal{#endifclass FormBuilderPrivate: public QFormBuilder{    friend class ::QUiLoader;    friend class ::QUiLoaderPrivate;    typedef QFormBuilder ParentClass;public:    QUiLoader *loader;    FormBuilderPrivate(): loader(0) {}    QWidget *defaultCreateWidget(const QString &className, QWidget *parent, const QString &name)    {        return ParentClass::createWidget(className, parent, name);    }    QLayout *defaultCreateLayout(const QString &className, QObject *parent, const QString &name)    {        return ParentClass::createLayout(className, parent, name);    }    QAction *defaultCreateAction(QObject *parent, const QString &name)    {        return ParentClass::createAction(parent, name);    }    QActionGroup *defaultCreateActionGroup(QObject *parent, const QString &name)    {        return ParentClass::createActionGroup(parent, name);    }    virtual QWidget *createWidget(const QString &className, QWidget *parent, const QString &name)    {        if (QWidget *widget = loader->createWidget(className, parent, name)) {            widget->setObjectName(name);            return widget;        }        return 0;    }    virtual QLayout *createLayout(const QString &className, QObject *parent, const QString &name)    {        if (QLayout *layout = loader->createLayout(className, parent, name)) {            layout->setObjectName(name);            return layout;        }        return 0;    }    virtual QActionGroup *createActionGroup(QObject *parent, const QString &name)    {        if (QActionGroup *actionGroup = loader->createActionGroup(parent, name)) {            actionGroup->setObjectName(name);            return actionGroup;        }        return 0;    }    virtual QAction *createAction(QObject *parent, const QString &name)    {        if (QAction *action = loader->createAction(parent, name)) {            action->setObjectName(name);            return action;        }        return 0;    }    virtual void applyProperties(QObject *o, const QList<DomProperty*> &properties);    virtual QWidget *create(DomUI *ui, QWidget *parentWidget);    virtual QWidget *create(DomWidget *ui_widget, QWidget *parentWidget);private:    QString m_class;};void FormBuilderPrivate::applyProperties(QObject *o, const QList<DomProperty*> &properties){    typedef QList<DomProperty*> DomPropertyList;    QFormBuilder::applyProperties(o, properties);    if (properties.empty())        return;    // translate string properties    const DomPropertyList::const_iterator cend = properties.constEnd();    for (DomPropertyList::const_iterator it = properties.constBegin(); it != cend; ++it) {        const DomProperty *p = *it;        if (p->kind() != DomProperty::String)            continue;        const DomString *dom_str = p->elementString();        if (dom_str->hasAttributeNotr()) {            const QString notr = dom_str->attributeNotr();            if (notr == QLatin1String("yes") || notr == QLatin1String("true"))                continue;        }        const QByteArray name = p->attributeName().toUtf8();        const QVariant v = o->property(name);        if (v.type() != QVariant::String)            continue;        const QString text = QApplication::translate(m_class.toUtf8(),                                                     v.toString().toUtf8(),                                                     dom_str->attributeComment().toUtf8(),                                                     QCoreApplication::UnicodeUTF8);        o->setProperty(name, text);    }}QWidget *FormBuilderPrivate::create(DomUI *ui, QWidget *parentWidget){    m_class = ui->elementClass();    return QFormBuilder::create(ui, parentWidget);}static void recursiveTranslate(QTreeWidgetItem *item, const QString &class_name){    int cnt = item->columnCount();    for (int i = 0; i < cnt; ++i) {        QString text = QApplication::translate(class_name.toUtf8(),                                                item->text(i).toUtf8(),                                                "",                                                QCoreApplication::UnicodeUTF8);        item->setText(i, text);    }    cnt = item->childCount();    for (int i = 0; i < cnt; ++i)        recursiveTranslate(item->child(i), class_name);}QWidget *FormBuilderPrivate::create(DomWidget *ui_widget, QWidget *parentWidget){    QWidget *w = QFormBuilder::create(ui_widget, parentWidget);    if (w == 0)        return 0;    if (QTabWidget *tabw = qobject_cast<QTabWidget*>(w)) {        const int cnt = tabw->count();        for (int i = 0; i < cnt; ++i) {            const QString text = QApplication::translate(m_class.toUtf8(),                                                    tabw->tabText(i).toUtf8(),                                                    "",                                                    QCoreApplication::UnicodeUTF8);            tabw->setTabText(i, text);        }    } else if (QListWidget *listw = qobject_cast<QListWidget*>(w)) {        const int cnt = listw->count();        for (int i = 0; i < cnt; ++i) {            QListWidgetItem *item = listw->item(i);            const QString text = QApplication::translate(m_class.toUtf8(),                                                    item->text().toUtf8(),                                                    "",                                                    QCoreApplication::UnicodeUTF8);            item->setText(text);        }    } else if (QTreeWidget *treew = qobject_cast<QTreeWidget*>(w)) {        const int cnt = treew->topLevelItemCount();        for (int i = 0; i < cnt; ++i) {            QTreeWidgetItem *item = treew->topLevelItem(i);            recursiveTranslate(item, m_class);        }    } else if (QTableWidget *tablew = qobject_cast<QTableWidget*>(w)) {        const int row_cnt = tablew->rowCount();        const int col_cnt = tablew->columnCount();        for (int i = 0; i < row_cnt; ++i) {            for (int j = 0; j < col_cnt; ++j) {                QTableWidgetItem *item = tablew->item(i, j);                if (item == 0)                    continue;                const QString text = QApplication::translate(m_class.toUtf8(),                                                        item->text().toUtf8(),                                                        "",                                                        QCoreApplication::UnicodeUTF8);                item->setText(text);            }        }    } else if (QComboBox *combow = qobject_cast<QComboBox*>(w)) {        if (!qobject_cast<QFontComboBox*>(w)) {            const int cnt = combow->count();            for (int i = 0; i < cnt; ++i) {                const QString text = QApplication::translate(m_class.toUtf8(),                        combow->itemText(i).toUtf8(),                        "",                        QCoreApplication::UnicodeUTF8);                combow->setItemText(i, text);            }        }    } else if (QToolBox *toolw = qobject_cast<QToolBox*>(w)) {        const int cnt = toolw->count();        for (int i = 0; i < cnt; ++i) {            const QString text = QApplication::translate(m_class.toUtf8(),                                                    toolw->itemText(i).toUtf8(),                                                    "",                                                    QCoreApplication::UnicodeUTF8);            toolw->setItemText(i, text);        }    }    return w;}#ifdef QFORMINTERNAL_NAMESPACE}#endifclass QUiLoaderPrivate: public QObjectPrivate{    Q_DECLARE_PUBLIC(QUiLoader)public:#ifdef QFORMINTERNAL_NAMESPACE    QFormInternal::FormBuilderPrivate builder;#else    FormBuilderPrivate builder;#endif    void setupWidgetMap() const;};void QUiLoaderPrivate::setupWidgetMap() const

⌨️ 快捷键说明

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