qdesigner_resource.cpp

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

CPP
1,680
字号
    } else {        core()->metaDataBase()->add(w);    }    w->setWindowFlags(w->windowFlags() & ~Qt::Window);    // Make sure it is non-modal (for example, KDialog calls setModal(true) in the constructor).    w->setWindowModality(Qt::NonModal);    return w;}QLayout *QDesignerResource::createLayout(const QString &layoutName, QObject *parent, const QString &name){    QWidget *layoutBase = 0;    QLayout *layout = qobject_cast<QLayout*>(parent);    if (parent->isWidgetType())        layoutBase = static_cast<QWidget*>(parent);    else {        Q_ASSERT( layout != 0 );        layoutBase = layout->parentWidget();    }    LayoutInfo::Type layoutType = LayoutInfo::Grid;    if (layoutName == QLatin1String("QVBoxLayout"))        layoutType = LayoutInfo::VBox;    else if (layoutName == QLatin1String("QHBoxLayout"))        layoutType = LayoutInfo::HBox;    else if (layoutName == QLatin1String("QStackedLayout"))        layoutType = LayoutInfo::Stacked;    QLayout *lay = core()->widgetFactory()->createLayout(layoutBase, layout, layoutType);    if (lay != 0)        changeObjectName(lay, name);    return lay;}// saveDomWidget *QDesignerResource::createDom(QWidget *widget, DomWidget *ui_parentWidget, bool recursive){    QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(widget);    if (!item)        return 0;    if (qobject_cast<Spacer*>(widget) && m_copyWidget == false) {        ++m_topLevelSpacerCount;        return 0;    }    QDesignerWidgetDataBaseItemInterface *widgetInfo =  0;    const int widgetInfoIndex = core()->widgetDataBase()->indexOfObject(widget, false);    if (widgetInfoIndex != -1) {        widgetInfo = core()->widgetDataBase()->item(widgetInfoIndex);        if (widgetInfo->isCustom()) {            if (widgetInfo->extends().isEmpty()) {                const QMetaObject *mo = widget->metaObject()->superClass();                while (mo != 0) {                    if (core()->widgetDataBase()->indexOfClassName(QLatin1String(mo->className())) != -1) {                        widgetInfo->setExtends(QLatin1String(mo->className()));                        break;                    }                    mo = mo->superClass();                }            }            m_usedCustomWidgets.insert(widgetInfo, true);        }    }    DomWidget *w = 0;    if (QDesignerTabWidget *tabWidget = qobject_cast<QDesignerTabWidget*>(widget))        w = saveWidget(tabWidget, ui_parentWidget);    else if (QDesignerStackedWidget *stackedWidget = qobject_cast<QDesignerStackedWidget*>(widget))        w = saveWidget(stackedWidget, ui_parentWidget);    else if (QDesignerToolBox *toolBox = qobject_cast<QDesignerToolBox*>(widget))        w = saveWidget(toolBox, ui_parentWidget);    else if (QToolBar *toolBar = qobject_cast<QToolBar*>(widget))        w = saveWidget(toolBar, ui_parentWidget);    else if (QDesignerDockWidget *dockWidget = qobject_cast<QDesignerDockWidget*>(widget))        w = saveWidget(dockWidget, ui_parentWidget);    else if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget))        w = saveWidget(widget, container, ui_parentWidget);    else        w = QAbstractFormBuilder::createDom(widget, ui_parentWidget, recursive);    Q_ASSERT( w != 0 );    if (!qobject_cast<QLayoutWidget*>(widget) && w->attributeClass() == QLatin1String("QWidget")) {        w->setAttributeNative(true);    }    const QString className = w->attributeClass();    if (m_internal_to_qt.contains(className))        w->setAttributeClass(m_internal_to_qt.value(className));    w->setAttributeName(widget->objectName());    if (isPromoted( core(), widget)) { // is promoted?        Q_ASSERT(widgetInfo != 0);        w->setAttributeName(widget->objectName());        w->setAttributeClass(widgetInfo->name());        QList<DomProperty*> prop_list = w->elementProperty();        foreach (DomProperty *prop, prop_list) {            if (prop->attributeName() == QLatin1String("geometry")) {                if (DomRect *rect = prop->elementRect()) {                    rect->setElementX(widget->x());                    rect->setElementY(widget->y());                }                break;            }        }    } else if (widgetInfo != 0 && m_usedCustomWidgets.contains(widgetInfo)) {        if (widgetInfo->name() != w->attributeClass())            w->setAttributeClass(widgetInfo->name());    }    addExtensionDataToDOM(this, core(), w, widget);    addUserDefinedScripts(widget, w);    return w;}DomLayout *QDesignerResource::createDom(QLayout *layout, DomLayout *ui_parentLayout, DomWidget *ui_parentWidget){    QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(layout);    if (item == 0) {        layout = qFindChild<QLayout*>(layout);        // refresh the meta database item        item = core()->metaDataBase()->item(layout);    }    if (item == 0) {        // nothing to do.        return 0;    }    if (qobject_cast<QSplitter*>(layout->parentWidget()) != 0) {        // nothing to do.        return 0;    }    m_chain.push(layout);    DomLayout *l = QAbstractFormBuilder::createDom(layout, ui_parentLayout, ui_parentWidget);    Q_ASSERT(l != 0);    m_chain.pop();    return l;}DomLayoutItem *QDesignerResource::createDom(QLayoutItem *item, DomLayout *ui_layout, DomWidget *ui_parentWidget){    DomLayoutItem *ui_item = 0;    if (Spacer *s = qobject_cast<Spacer*>(item->widget())) {        if (!core()->metaDataBase()->item(s))            return 0;        DomSpacer *spacer = new DomSpacer();        const QList<DomProperty*> properties = computeProperties(item->widget());        // ### filter the properties        spacer->setElementProperty(properties);        ui_item = new DomLayoutItem();        ui_item->setElementSpacer(spacer);        m_laidout.insert(item->widget(), true);    } else if (QLayoutWidget *layoutWidget = qobject_cast<QLayoutWidget*>(item->widget())) {        Q_ASSERT(layoutWidget->layout());        DomLayout *l = createDom(layoutWidget->layout(), ui_layout, ui_parentWidget);        ui_item = new DomLayoutItem();        ui_item->setElementLayout(l);        m_laidout.insert(item->widget(), true);    } else if (!item->spacerItem()) { // we use spacer as fake item in the Designer        ui_item = QAbstractFormBuilder::createDom(item, ui_layout, ui_parentWidget);    } else {        return 0;    }    if (m_chain.size() && item->widget()) {        if (QGridLayout *grid = qobject_cast<QGridLayout*>(m_chain.top())) {            const int index = Utils::indexOfWidget(grid, item->widget());            int row, column, rowspan, colspan;            grid->getItemPosition(index, &row, &column, &rowspan, &colspan);            ui_item->setAttributeRow(row);            ui_item->setAttributeColumn(column);            if (colspan != 1)                ui_item->setAttributeColSpan(colspan);            if (rowspan != 1)                ui_item->setAttributeRowSpan(rowspan);        }    }    return ui_item;}void QDesignerResource::addCustomWidgetsToWidgetDatabase(DomCustomWidgetList& custom_widget_list){    // Perform one iteration of adding the custom widgets to the database,    // looking up the base class and inheriting its data.    // Remove the succeeded custom widgets from the list.    // Classes whose base class could not be found are left in the list.    QDesignerWidgetDataBaseInterface *db = m_formWindow->core()->widgetDataBase();    for (int i=0; i < custom_widget_list.size(); ) {        bool classInserted = false;        DomCustomWidget *custom_widget = custom_widget_list[i];        const QString customClassName = custom_widget->elementClass();        const QString base_class = custom_widget->elementExtends();        QString includeFile;        IncludeType includeType = IncludeLocal;        if (const DomHeader *header = custom_widget->elementHeader()) {            includeFile = header->text();            if (header->hasAttributeLocation() && header->attributeLocation() == QLatin1String("global"))                includeType = IncludeGlobal;        }        const bool domIsContainer = custom_widget->elementContainer();        // Append a new item        if (base_class.isEmpty()) {            WidgetDataBaseItem *item = new WidgetDataBaseItem(customClassName);            item->setPromoted(false);            item->setGroup(QApplication::translate("Designer", "Custom Widgets"));            item->setIncludeFile(buildIncludeFile(includeFile, includeType));            item->setContainer(domIsContainer);            item->setCustom(true);            db->append(item);            custom_widget_list.removeAt(i);            classInserted = true;        } else {            // Create a new entry cloned from base class. Note that this will ignore existing            // classes, eg, plugin custom widgets.            QDesignerWidgetDataBaseItemInterface *item =                appendDerived(db, customClassName, QApplication::translate("Designer", "Promoted Widgets"),                              base_class,                              buildIncludeFile(includeFile, includeType),                              true,true);            // Ok, base class found.            if (item) {                // Hack to accommodate for old UI-files in which "contains" is not set properly:                // Apply "contains" from DOM only if true (else, eg classes from QFrame might not accept                // dropping child widgets on them as container=false). This also allows for                // QWidget-derived stacked pages.                if (domIsContainer)                    item->setContainer(domIsContainer);                custom_widget_list.removeAt(i);                classInserted = true;            }        }        // Skip failed item.        if (!classInserted)            i++;    }}void QDesignerResource::createCustomWidgets(DomCustomWidgets *dom_custom_widgets){    if (dom_custom_widgets == 0)        return;    DomCustomWidgetList custom_widget_list = dom_custom_widgets->elementCustomWidget();    // Attempt to insert each item derived from its base class.    // This should at most require two iterations in the event that the classes are out of order    // (derived first, max depth: promoted custom plugin = 2)    for (int iteration = 0;  iteration < 2;  iteration++) {        addCustomWidgetsToWidgetDatabase(custom_widget_list);        if (custom_widget_list.empty())            return;    }    // Oops, there are classes left whose base class could not be found.    // Default them to QWidget with warnings.    const QString fallBackBaseClass = QLatin1String("QWidget");    for (int i=0; i < custom_widget_list.size(); i++ ) {        DomCustomWidget *custom_widget = custom_widget_list[i];        const QString customClassName = custom_widget->elementClass();        const QString base_class = custom_widget->elementExtends();        qDebug() << "** WARNING The base class " << base_class << " of the custom widget class " << customClassName             << " could not be found. Defaulting to " << fallBackBaseClass << '.';        custom_widget->setElementExtends(fallBackBaseClass);    }    // One more pass.    addCustomWidgetsToWidgetDatabase(custom_widget_list);    Q_ASSERT(custom_widget_list.empty());}DomTabStops *QDesignerResource::saveTabStops(){    QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);    Q_ASSERT(item);    QStringList tabStops;    foreach (QWidget *widget, item->tabOrder()) {        if (m_formWindow->mainContainer()->isAncestorOf(widget))            tabStops.append(widget->objectName());    }    if (tabStops.count()) {        DomTabStops *dom = new DomTabStops;        dom->setElementTabStop(tabStops);        return dom;    }    return 0;}void QDesignerResource::applyTabStops(QWidget *widget, DomTabStops *tabStops){    if (!tabStops)        return;    QList<QWidget*> tabOrder;    foreach (QString widgetName, tabStops->elementTabStop()) {        if (QWidget *w = qFindChild<QWidget*>(widget, widgetName)) {            tabOrder.append(w);        }    }    QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);    Q_ASSERT(item);    item->setTabOrder(tabOrder);}DomWidget *QDesignerResource::saveWidget(QWidget *widget, QDesignerContainerExtension *container, DomWidget *ui_parentWidget){    DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);    QList<DomWidget*> ui_widget_list;    for (int i=0; i<container->count(); ++i) {        QWidget *page = container->widget(i);        Q_ASSERT(page);        DomWidget *ui_page = createDom(page, ui_widget);

⌨️ 快捷键说明

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