bool FontPropertyManager::readFamilyMapping(NameMap *rc, QString *errorMessage) { rc->clear(); const QString fileName = QLatin1String(":/trolltech/propertyeditor/fontmapping.xml"); QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { *errorMessage = QString::fromUtf8("Unable to open %1: %2").arg(fileName, file.errorString()); return false; } QXmlStreamReader reader(&file); QXmlStreamReader::TokenType token; QString family; ParseStage stage = ParseBeginning; do { token = reader.readNext(); switch (token) { case QXmlStreamReader::Invalid: *errorMessage = msgXmlError(reader, fileName); return false; case QXmlStreamReader::StartElement: stage = nextStage(stage, reader.name()); switch (stage) { case ParseError: reader.raiseError(QString::fromUtf8("Unexpected element <%1>.").arg(reader.name().toString())); *errorMessage = msgXmlError(reader, fileName); return false; case ParseWithinFamily: family = reader.readElementText(); break; case ParseWithinDisplay: rc->insert(family, reader.readElementText()); break; default: break; } default: break; } } while (token != QXmlStreamReader::EndDocument); return true; }
bool WidgetBoxTreeWidget::readCategories(const QString &fileName, const QString &contents, CategoryList *cats, QString *errorMessage) { // Read widget box XML: // //<widgetbox version="4.5"> // <category name="Layouts"> // <categoryentry name="Vertical Layout" icon="win/editvlayout.png" type="default"> // <widget class="QListWidget" ...> // ... QXmlStreamReader reader(contents); // Entries of category with name="invisible" should be ignored bool ignoreEntries = false; while (!reader.atEnd()) { switch (reader.readNext()) { case QXmlStreamReader::StartElement: { const QStringRef tag = reader.name(); if (tag == QLatin1String(widgetBoxRootElementC)) { //<widgetbox version="4.5"> continue; } if (tag == QLatin1String(categoryElementC)) { // <category name="Layouts"> const QXmlStreamAttributes attributes = reader.attributes(); const QString categoryName = attributes.value(QLatin1String(nameAttributeC)).toString(); if (categoryName == QLatin1String(invisibleNameC)) { ignoreEntries = true; } else { Category category(categoryName); if (attributes.value(QLatin1String(typeAttributeC)) == QLatin1String(scratchPadValueC)) category.setType(Category::Scratchpad); cats->push_back(category); } continue; } if (tag == QLatin1String(categoryEntryElementC)) { // <categoryentry name="Vertical Layout" icon="win/editvlayout.png" type="default"> if (!ignoreEntries) { QXmlStreamAttributes attr = reader.attributes(); const QString widgetName = attr.value(QLatin1String(nameAttributeC)).toString(); const QString widgetIcon = attr.value(QLatin1String(iconAttributeC)).toString(); const WidgetBoxTreeWidget::Widget::Type widgetType = attr.value(QLatin1String(typeAttributeC)).toString() == QLatin1String(customValueC) ? WidgetBoxTreeWidget::Widget::Custom : WidgetBoxTreeWidget::Widget::Default; Widget w; w.setName(widgetName); w.setIconName(widgetIcon); w.setType(widgetType); if (!readWidget(&w, contents, reader)) continue; cats->back().addWidget(w); } // ignoreEntries continue; } break; } case QXmlStreamReader::EndElement: { const QStringRef tag = reader.name(); if (tag == QLatin1String(widgetBoxRootElementC)) { continue; } if (tag == QLatin1String(categoryElementC)) { ignoreEntries = false; continue; } if (tag == QLatin1String(categoryEntryElementC)) { continue; } break; } default: break; } } if (reader.hasError()) { *errorMessage = msgXmlError(fileName, reader); return false; } return true; }