void WidgetBoxCategoryModel::addWidget(const QDesignerWidgetBoxInterface::Widget &widget, const QIcon &icon,bool editable) { // build item. Filter on name + class name if it is different and not a layout. QString filter = widget.name(); if (!filter.contains(QLatin1String("Layout")) && m_classNameRegExp.indexIn(widget.domXml()) != -1) { const QString className = m_classNameRegExp.cap(1); if (!filter.contains(className)) filter += className; } WidgetBoxCategoryEntry item(widget, filter, icon, editable); const QDesignerWidgetDataBaseInterface *db = m_core->widgetDataBase(); const int dbIndex = db->indexOfClassName(widget.name()); if (dbIndex != -1) { const QDesignerWidgetDataBaseItemInterface *dbItem = db->item(dbIndex); const QString toolTip = dbItem->toolTip(); if (!toolTip.isEmpty()) item.toolTip = toolTip; const QString whatsThis = dbItem->whatsThis(); if (!whatsThis.isEmpty()) item.whatsThis = whatsThis; } // insert const int row = m_items.size(); beginInsertRows(QModelIndex(), row, row); m_items.push_back(item); endInsertRows(); }
// Get XML for a new form from the widget box. Change objectName/geometry // properties to be suitable for new forms static QString xmlFromWidgetBox(const QDesignerFormEditorInterface *core, const QString &className, const QString &objectName) { typedef QList<DomProperty*> PropertyList; QDesignerWidgetBoxInterface::Widget widget; const bool found = QDesignerWidgetBox::findWidget(core->widgetBox(), className, QString(), &widget); if (!found) return QString(); QScopedPointer<DomUI> domUI(QDesignerWidgetBox::xmlToUi(className, widget.domXml(), false)); if (domUI.isNull()) return QString(); domUI->setAttributeVersion(QLatin1String("4.0")); DomWidget *domWidget = domUI->elementWidget(); if (!domWidget) return QString(); // Properties: Remove the "objectName" property in favour of the name attribute and check geometry. domWidget->setAttributeName(objectName); const QString geometryProperty = QLatin1String("geometry"); const QString objectNameProperty = QLatin1String("objectName"); PropertyList properties = domWidget->elementProperty(); for (PropertyList::iterator it = properties.begin(); it != properties.end(); ) { DomProperty *property = *it; if (property->attributeName() == objectNameProperty) { // remove "objectName" it = properties.erase(it); delete property; } else { if (property->attributeName() == geometryProperty) { // Make sure form is at least 400, 300 if (DomRect *geom = property->elementRect()) { if (geom->elementWidth() < NewFormWidth) geom->setElementWidth(NewFormWidth); if (geom->elementHeight() < NewFormHeight) geom->setElementHeight(NewFormHeight); } } ++it; } } // Add a window title property DomString *windowTitleString = new DomString; windowTitleString->setText(objectName); DomProperty *windowTitleProperty = new DomProperty; windowTitleProperty->setAttributeName(QLatin1String("windowTitle")); windowTitleProperty->setElementString(windowTitleString); properties.push_back(windowTitleProperty); // ------ domWidget->setElementProperty(properties); // Embed in in DomUI and get string. Omit the version number. domUI->setElementClass(objectName); QString rc; { // Serialize domUI QXmlStreamWriter writer(&rc); writer.setAutoFormatting(true); writer.setAutoFormattingIndent(1); writer.writeStartDocument(); domUI->write(writer); writer.writeEndDocument(); } return rc; }
QString WidgetBoxCategoryListView::widgetDomXml(const QDesignerWidgetBoxInterface::Widget &widget) { QString domXml = widget.domXml(); if (domXml.isEmpty()) { domXml = QLatin1String(uiOpeningTagC); domXml += QLatin1String("<widget class=\""); domXml += widget.name(); domXml += QLatin1String("\"/>"); domXml += QLatin1String(uiClosingTagC); } return domXml; }