formbuilder.cpp

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

CPP
515
字号
/******************************************************************************** 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 "customwidget.h"#include "formbuilder.h"#include "formbuilderextra_p.h"#include "ui4_p.h"#include <QtGui/QtGui>#ifdef QFORMINTERNAL_NAMESPACEnamespace QFormInternal {#endif/*!    \class QFormBuilder    \brief The QFormBuilder class is used to dynamically construct    user interfaces from .ui files at run-time.    \inmodule QtDesigner    The QFormBuilder class provides a mechanism for dynamically    creating user interfaces at run-time, based on \c{.ui} files    created with \QD. For example:    \code        MyForm::MyForm(QWidget *parent)            : QWidget(parent)        {            QFormBuilder builder;            QFile file(":/forms/myWidget.ui");            file.open(QFile::ReadOnly);            QWidget *myWidget = builder.load(&file, this);            file.close();            QVBoxLayout *layout = new QVBoxLayout;            layout->addWidget(myWidget);            setLayout(layout);        }    \endcode    By including the user interface in the example's resources (\c    myForm.grc), we ensure that it will be present when the example is    run:    \code    <!DOCTYPE RCC><RCC version="1.0">    <qresource prefix="/forms">       <file>mywidget.ui</file>    </qresource>    </RCC>    \endcode    QFormBuilder extends the QAbstractFormBuilder base class with a    number of functions that are used to support custom widget    plugins:    \list    \o pluginPaths() returns the list of paths that the form builder       searches when loading custom widget plugins.    \o addPluginPath() allows additional paths to be registered with       the form builder.    \o setPluginPath() is used to replace the existing list of paths       with a list obtained from some other source.    \o clearPluginPaths() removes all paths registered with the form       builder.    \o customWidgets() returns a list of interfaces to plugins that       can be used to create new instances of registered custom widgets.    \endlist    The QFormBuilder class is typically used by custom components and    applications that embed \QD. Standalone applications that need to    dynamically generate user interfaces at run-time use the    QUiLoader class, found in the QtUiTools module.    \sa QAbstractFormBuilder, {QtUiTools Module}*//*!    \fn QFormBuilder::QFormBuilder()    Constructs a new form builder.*/QFormBuilder::QFormBuilder() : QAbstractFormBuilder(){}/*!    Destroys the form builder.*/QFormBuilder::~QFormBuilder(){}/*!    \internal*/QWidget *QFormBuilder::create(DomWidget *ui_widget, QWidget *parentWidget){    QFormBuilderExtra::instance(this)->setProcessingLayoutWidget(false);    if (ui_widget->attributeClass() == QLatin1String("QWidget") && !ui_widget->hasAttributeNative()            && parentWidget &&            !qobject_cast<QMainWindow *>(parentWidget) &&            !qobject_cast<QToolBox *>(parentWidget) &&            !qobject_cast<QStackedWidget *>(parentWidget) &&            !qobject_cast<QTabWidget *>(parentWidget))        QFormBuilderExtra::instance(this)->setProcessingLayoutWidget(true);    return QAbstractFormBuilder::create(ui_widget, parentWidget);}/*!    \internal*/QWidget *QFormBuilder::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name){    QWidget *w = 0;    if (qobject_cast<QTabWidget*>(parentWidget)            || qobject_cast<QStackedWidget*>(parentWidget)            || qobject_cast<QToolBox*>(parentWidget))        parentWidget = 0;    // ### special-casing for Line (QFrame) -- fix for 4.2    if (widgetName == QLatin1String("Line")) {        w = new QFrame(parentWidget);        static_cast<QFrame*>(w)->setFrameStyle(QFrame::HLine | QFrame::Sunken);    }#define DECLARE_LAYOUT(L, C)#define DECLARE_COMPAT_WIDGET(W, C)#define DECLARE_WIDGET(W, C) else if (widgetName == QLatin1String(#W)) { Q_ASSERT(w == 0); w = new W(parentWidget); }#define DECLARE_WIDGET_1(W, C) else if (widgetName == QLatin1String(#W)) { Q_ASSERT(w == 0); w = new W(0, parentWidget); }#include "widgets.table"#undef DECLARE_COMPAT_WIDGET#undef DECLARE_LAYOUT#undef DECLARE_WIDGET#undef DECLARE_WIDGET_1    if (w == 0) { // try with a registered custom widget        QDesignerCustomWidgetInterface *factory = m_customWidgets.value(widgetName);        if (factory != 0)            w = factory->createWidget(parentWidget);    }    if (w == 0) { // nothing to do        qWarning() << QObject::tr("QFormBuilder was unable to create a widget of the class '%1'.").arg(widgetName);        return 0;    }    w->setObjectName(name);    if (qobject_cast<QDialog *>(w))        w->setParent(parentWidget);    QFormBuilderExtra *fb = QFormBuilderExtra::instance(this);    if (!fb->rootWidget())        fb->setRootWidget(w);    return w;}/*!    \internal*/QLayout *QFormBuilder::createLayout(const QString &layoutName, QObject *parent, const QString &name){    QLayout *l = 0;    QWidget *parentWidget = qobject_cast<QWidget*>(parent);    QLayout *parentLayout = qobject_cast<QLayout*>(parent);    Q_ASSERT(parentWidget || parentLayout);#define DECLARE_WIDGET(W, C)#define DECLARE_COMPAT_WIDGET(W, C)#define DECLARE_LAYOUT(L, C) \    if (layoutName == QLatin1String(#L)) { \        Q_ASSERT(l == 0); \        l = parentLayout \            ? new L() \            : new L(parentWidget); \    }#include "widgets.table"#undef DECLARE_LAYOUT#undef DECLARE_COMPAT_WIDGET#undef DECLARE_WIDGET    if (l) {        l->setObjectName(name);        if (parentLayout) {            QWidget *w = qobject_cast<QWidget *>(parentLayout->parent());            if (w && w->inherits("Q3GroupBox")) {                l->setContentsMargins(w->style()->pixelMetric(QStyle::PM_LayoutLeftMargin),                                    w->style()->pixelMetric(QStyle::PM_LayoutTopMargin),                                    w->style()->pixelMetric(QStyle::PM_LayoutRightMargin),                                    w->style()->pixelMetric(QStyle::PM_LayoutBottomMargin));                QGridLayout *grid = qobject_cast<QGridLayout *>(l);                if (grid) {                    grid->setHorizontalSpacing(-1);                    grid->setVerticalSpacing(-1);                } else {                    l->setSpacing(-1);                }                l->setAlignment(Qt::AlignTop);            }        }    } else {        qWarning() << QObject::tr("The layout type `%1' is not supported.").arg(layoutName);    }    return l;}/*!    \internal*/bool QFormBuilder::addItem(DomLayoutItem *ui_item, QLayoutItem *item, QLayout *layout)

⌨️ 快捷键说明

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