// Convenience to create a Dom Widget from widget box xml code. DomUI *QDesignerWidgetBox::xmlToUi(const QString &name, const QString &xml, bool insertFakeTopLevel, QString *errorMessage) { QDomDocument doc; int errorLine, errorColumn; if (!doc.setContent(xml, errorMessage, &errorLine, &errorColumn)) { *errorMessage = QObject::tr("A parse error occurred at line %1, column %2 of the XML code specified for the widget %3: %4\n%5"). arg(errorLine).arg(errorColumn).arg(name).arg(*errorMessage).arg(xml); return 0; } if (!doc.hasChildNodes()) { *errorMessage = QObject::tr("The XML code specified for the widget %1 does not contain any widget elements.\n%2").arg(name).arg(xml); return 0; } QDomElement rootElement = doc.firstChildElement(); const QString rootNode = rootElement.nodeName(); const QString widgetTag = QLatin1String("widget"); if (rootNode == widgetTag) { // 4.3 legacy ,wrap into DomUI DomUI *rc = new DomUI; DomWidget *widget = new DomWidget; widget->read(rootElement); if (insertFakeTopLevel) { DomWidget *fakeTopLevel = new DomWidget; QList<DomWidget *> children; children.push_back(widget); fakeTopLevel->setElementWidget(children); rc->setElementWidget(fakeTopLevel); } else { rc->setElementWidget(widget); } return rc; } if (rootNode == QLatin1String("ui")) { // 4.4 QDomElement widgetChild = rootElement.firstChildElement(widgetTag); if (widgetChild.isNull()) { *errorMessage = QObject::tr("The XML code specified for the widget %1 does not contain valid widget element\n%2").arg(name).arg(xml); return 0; } if (insertFakeTopLevel) { QDomElement fakeTopLevel = doc.createElement(widgetTag); rootElement.replaceChild(fakeTopLevel, widgetChild); fakeTopLevel.appendChild(widgetChild); } DomUI *rc = new DomUI; rc->read(rootElement); return rc; } *errorMessage = QObject::tr("The XML code specified for the widget %1 contains an invalid root element %2.\n%3").arg(name).arg(rootNode).arg(xml); return 0; }
// Convenience to create a Dom Widget from widget box xml code. DomUI *QDesignerWidgetBox::xmlToUi(const QString &name, const QString &xml, bool insertFakeTopLevel, QString *errorMessage) { QXmlStreamReader reader(xml); DomUI *ui = 0; // The xml description must either contain a root element "ui" with a child element "widget" // or "widget" as the root element (4.3 legacy) const QString widgetTag = QLatin1String("widget"); while (!reader.atEnd()) { if (reader.readNext() == QXmlStreamReader::StartElement) { const QStringRef name = reader.name(); if (ui) { reader.raiseError(tr("Unexpected element <%1>").arg(name.toString())); continue; } if (name.compare(QLatin1String("widget"), Qt::CaseInsensitive) == 0) { // 4.3 legacy, wrap into DomUI ui = new DomUI; DomWidget *widget = new DomWidget; widget->read(reader); ui->setElementWidget(widget); } else if (name.compare(QLatin1String("ui"), Qt::CaseInsensitive) == 0) { // 4.4 ui = new DomUI; ui->read(reader); } else { reader.raiseError(tr("Unexpected element <%1>").arg(name.toString())); } } } if (reader.hasError()) { delete ui; *errorMessage = tr("A parse error occurred at line %1, column %2 of the XML code " "specified for the widget %3: %4\n%5") .arg(reader.lineNumber()).arg(reader.columnNumber()).arg(name) .arg(reader.errorString()).arg(xml); return 0; } if (!ui || !ui->elementWidget()) { delete ui; *errorMessage = tr("The XML code specified for the widget %1 does not contain " "any widget elements.\n%2").arg(name).arg(xml); return 0; } if (insertFakeTopLevel) { DomWidget *fakeTopLevel = new DomWidget; fakeTopLevel->setAttributeClass(QLatin1String("QWidget")); QList<DomWidget *> children; children.push_back(ui->takeElementWidget()); fakeTopLevel->setElementWidget(children); ui->setElementWidget(fakeTopLevel); } return ui; }
void Ui3Reader::generateUi4(const QString &fn, const QString &outputFn, QDomDocument doc) { init(); fileName = fn; outputFileName = outputFn; DomUI *ui = generateUi4(parse(doc)); if (!ui) return; if (pixmapLoaderFunction.size()) ui->setElementPixmapFunction(pixmapLoaderFunction); QXmlStreamWriter writer(out.device()); writer.setAutoFormatting(true); writer.setAutoFormattingIndent(2); writer.writeStartDocument(); ui->write(writer); writer.writeEndDocument(); delete ui; }
// Set a fixed size on a XML template QString WidgetDataBase::scaleFormTemplate(const QString &xml, const QSize &size, bool fixed) { typedef QList<DomProperty*> PropertyList; DomUI *domUI = QDesignerWidgetBox::xmlToUi(QLatin1String("Form"), xml, false); if (!domUI) return QString(); DomWidget *domWidget = domUI->elementWidget(); if (!domWidget) return QString(); // Properties: Find/Ensure the geometry, minimum and maximum sizes properties const QString geometryPropertyName = QLatin1String("geometry"); const QString minimumSizePropertyName = QLatin1String("minimumSize"); const QString maximumSizePropertyName = QLatin1String("maximumSize"); DomProperty *geomProperty = 0; DomProperty *minimumSizeProperty = 0; DomProperty *maximumSizeProperty = 0; PropertyList properties = domWidget->elementProperty(); const PropertyList::const_iterator cend = properties.constEnd(); for (PropertyList::const_iterator it = properties.constBegin(); it != cend; ++it) { const QString name = (*it)->attributeName(); if (name == geometryPropertyName) { geomProperty = *it; } else { if (name == minimumSizePropertyName) { minimumSizeProperty = *it; } else { if (name == maximumSizePropertyName) maximumSizeProperty = *it; } } } if (!geomProperty) { geomProperty = new DomProperty; geomProperty->setAttributeName(geometryPropertyName); geomProperty->setElementRect(new DomRect); properties.push_front(geomProperty); } if (fixed) { if (!minimumSizeProperty) { minimumSizeProperty = new DomProperty; minimumSizeProperty->setAttributeName(minimumSizePropertyName); minimumSizeProperty->setElementSize(new DomSize); properties.push_back(minimumSizeProperty); } if (!maximumSizeProperty) { maximumSizeProperty = new DomProperty; maximumSizeProperty->setAttributeName(maximumSizePropertyName); maximumSizeProperty->setElementSize(new DomSize); properties.push_back(maximumSizeProperty); } } // Set values of geometry, minimum and maximum sizes properties const int width = size.width(); const int height = size.height(); if (DomRect *geom = geomProperty->elementRect()) { geom->setElementWidth(width); geom->setElementHeight(height); } if (fixed) { if (DomSize *s = minimumSizeProperty->elementSize()) { s->setElementWidth(width); s->setElementHeight(height); } if (DomSize *s = maximumSizeProperty->elementSize()) { s->setElementWidth(width); s->setElementHeight(height); } } // write back domWidget->setElementProperty(properties); QString rc; { // serialize domUI QXmlStreamWriter writer(&rc); writer.setAutoFormatting(true); writer.setAutoFormattingIndent(1); writer.writeStartDocument(); domUI->write(writer); writer.writeEndDocument(); } delete domUI; return rc; }
// 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(); DomUI *domUI = QDesignerWidgetBox::xmlToUi(className, widget.domXml(), false); domUI->setAttributeVersion(QLatin1String("4.0")); if (!domUI) return QString(); 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(); } delete domUI; return rc; }