81 QString *errorMessage)
84 while (!reader.atEnd()) {
85 switch (reader.readNext()) {
86 case QXmlStreamReader::Invalid:
87 *errorMessage = msgXmlError(reader);
89 case QXmlStreamReader::StartElement:
90 if (reader.name().compare(
"ui"_L1, Qt::CaseInsensitive) == 0) {
91 const QString versionAttribute = u"version"_s;
92 const QString languageAttribute = u"language"_s;
93 const QXmlStreamAttributes attributes = reader.attributes();
94 if (attributes.hasAttribute(versionAttribute)) {
95 const QVersionNumber version =
96 QVersionNumber::fromString(attributes.value(versionAttribute));
97 if (version < QVersionNumber(4)) {
99 QCoreApplication::translate(
"QAbstractFormBuilder",
100 "This file was created using Designer from Qt-%1 and cannot be read.")
101 .arg(attributes.value(versionAttribute));
105 if (attributes.hasAttribute(languageAttribute)) {
107 const QString formLanguage = attributes.value(languageAttribute).toString();
108 if (!formLanguage.isEmpty() && formLanguage.compare(language, Qt::CaseInsensitive)) {
110 QCoreApplication::translate(
"QAbstractFormBuilder",
111 "This file cannot be read because it was created using %1.")
124 *errorMessage = QCoreApplication::translate(
"QAbstractFormBuilder",
125 "Invalid UI file: The root element <ui> is missing.");
170bool QFormBuilderExtra::applyBuddy(
const QString &buddyName, BuddyMode applyMode, QLabel *label)
172 if (buddyName.isEmpty()) {
173 label->setBuddy(
nullptr);
183 QWidget *parent = label->parentWidget();
184 while (parent && widgets.isEmpty()) {
185 widgets = parent->findChildren<QWidget*>(buddyName);
186 parent = parent->parentWidget();
189 if (widgets.isEmpty()) {
190 label->setBuddy(
nullptr);
194 for (
auto *w : widgets) {
195 if (applyMode == BuddyApplyAll || !w->isHidden()) {
201 label->setBuddy(
nullptr);
345inline bool parsePerCellProperty(Layout *l,
int count,
void (Layout::*setter)(
int,
int),
const QString &s,
int defaultValue = 0)
348 clearPerCellValue(l, count, setter, defaultValue);
351 const auto list = QStringView{s}.split(u',');
352 if (list.isEmpty()) {
353 clearPerCellValue(l, count, setter, defaultValue);
357 const int ac = qMin(count, list.size());
360 for ( ; i < ac; i++) {
361 const int value = list.at(i).toInt(&ok);
362 if (!ok || value < 0)
364 (l->*setter)(i, value);
367 for ( ; i < count; i++)
368 (l->*setter)(i, defaultValue);
379void QFormBuilderExtra::getLayoutMargins(
const QList<DomProperty*> &properties,
380 int *left,
int *top,
int *right,
int *bottom)
382 if (
const auto *p = propertyByName(properties,
"leftMargin"))
383 *left = p->elementNumber();
384 if (
const auto *p = propertyByName(properties,
"topMargin"))
385 *top = p->elementNumber();
386 if (
const auto *p = propertyByName(properties,
"rightMargin"))
387 *right = p->elementNumber();
388 if (
const auto *p = propertyByName(properties,
"bottomMargin"))
389 *bottom = p->elementNumber();
512void QFormBuilderExtra::setupColorGroup(QPalette *palette, QPalette::ColorGroup colorGroup,
513 const DomColorGroup *group)
516 const auto &colors = group->elementColor();
517 for (
int role = 0; role < colors.size(); ++role) {
518 const DomColor *color = colors.at(role);
519 const QColor c(color->elementRed(), color->elementGreen(), color->elementBlue());
520 palette->setColor(colorGroup, QPalette::ColorRole(role), c);
524 const QMetaEnum colorRole_enum = metaEnum<QAbstractFormBuilderGadget>(
"colorRole");
526 const auto colorRoles = group->elementColorRole();
527 for (
const DomColorRole *colorRole : colorRoles) {
528 if (colorRole->hasAttributeRole()) {
529 const int r = colorRole_enum.keyToValue(colorRole->attributeRole().toLatin1());
531 const QBrush br = setupBrush(colorRole->elementBrush());
532 palette->setBrush(colorGroup,
static_cast<QPalette::ColorRole>(r), br);
538DomColorGroup *QFormBuilderExtra::saveColorGroup(
const QPalette &palette,
539 QPalette::ColorGroup colorGroup)
542 const QMetaEnum colorRole_enum = metaEnum<QAbstractFormBuilderGadget>(
"colorRole");
544 DomColorGroup *group =
new DomColorGroup();
545 QList<DomColorRole *> colorRoles;
547 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
548 const auto role =
static_cast<QPalette::ColorRole>(r);
549 if (palette.isBrushSet(colorGroup, role)) {
550 const QBrush &br = palette.brush(colorGroup, role);
551 DomColorRole *colorRole =
new DomColorRole();
552 colorRole->setElementBrush(saveBrush(br));
553 colorRole->setAttributeRole(QLatin1StringView(colorRole_enum.valueToKey(role)));
554 colorRoles.append(colorRole);
558 group->setElementColorRole(colorRoles);
572QPalette QFormBuilderExtra::loadPalette(
const DomPalette *dom)
576 if (dom->elementActive())
577 QFormBuilderExtra::setupColorGroup(&palette, QPalette::Active, dom->elementActive());
579 if (dom->elementInactive())
580 QFormBuilderExtra::setupColorGroup(&palette, QPalette::Inactive, dom->elementInactive());
582 if (dom->elementDisabled())
583 QFormBuilderExtra::setupColorGroup(&palette, QPalette::Disabled, dom->elementDisabled());
585 palette.setCurrentColorGroup(QPalette::Active);
589QBrush QFormBuilderExtra::setupBrush(
const DomBrush *brush)
592 if (!brush->hasAttributeBrushStyle())
595 const Qt::BrushStyle style = enumKeyOfObjectToValue<QAbstractFormBuilderGadget, Qt::BrushStyle>(
"brushStyle",
596 brush->attributeBrushStyle().toLatin1().constData());
598 if (style == Qt::LinearGradientPattern ||
599 style == Qt::RadialGradientPattern ||
600 style == Qt::ConicalGradientPattern) {
601 const QMetaEnum gradientType_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientType");
602 const QMetaEnum gradientSpread_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientSpread");
603 const QMetaEnum gradientCoordinate_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientCoordinate");
605 const DomGradient *gradient = brush->elementGradient();
606 const QGradient::Type type = enumKeyToValue<QGradient::Type>(gradientType_enum, gradient->attributeType().toLatin1());
609 QGradient *gr =
nullptr;
611 if (type == QGradient::LinearGradient) {
612 gr =
new QLinearGradient(QPointF(gradient->attributeStartX(), gradient->attributeStartY()),
613 QPointF(gradient->attributeEndX(), gradient->attributeEndY()));
614 }
else if (type == QGradient::RadialGradient) {
615 gr =
new QRadialGradient(QPointF(gradient->attributeCentralX(), gradient->attributeCentralY()),
616 gradient->attributeRadius(),
617 QPointF(gradient->attributeFocalX(), gradient->attributeFocalY()));
618 }
else if (type == QGradient::ConicalGradient) {
619 gr =
new QConicalGradient(QPointF(gradient->attributeCentralX(), gradient->attributeCentralY()),
620 gradient->attributeAngle());
625 const QGradient::Spread spread = enumKeyToValue<QGradient::Spread>(gradientSpread_enum, gradient->attributeSpread().toLatin1());
626 gr->setSpread(spread);
628 const QGradient::CoordinateMode coord = enumKeyToValue<QGradient::CoordinateMode>(gradientCoordinate_enum, gradient->attributeCoordinateMode().toLatin1());
629 gr->setCoordinateMode(coord);
631 const auto &stops = gradient->elementGradientStop();
632 for (
const DomGradientStop *stop : stops) {
633 const DomColor *color = stop->elementColor();
634 gr->setColorAt(stop->attributePosition(), QColor::fromRgb(color->elementRed(),
635 color->elementGreen(), color->elementBlue(), color->attributeAlpha()));
639 }
else if (style == Qt::TexturePattern) {
640 const DomProperty *texture = brush->elementTexture();
641 if (texture && texture->kind() == DomProperty::Pixmap) {
645 const DomColor *color = brush->elementColor();
646 br.setColor(QColor::fromRgb(color->elementRed(),
647 color->elementGreen(), color->elementBlue(), color->attributeAlpha()));
648 br.setStyle((Qt::BrushStyle)style);
653DomBrush *QFormBuilderExtra::saveBrush(
const QBrush &br)
655 const QMetaEnum brushStyle_enum = metaEnum<QAbstractFormBuilderGadget>(
"brushStyle");
657 DomBrush *brush =
new DomBrush();
658 const Qt::BrushStyle style = br.style();
659 brush->setAttributeBrushStyle(QLatin1StringView(brushStyle_enum.valueToKey(style)));
660 if (style == Qt::LinearGradientPattern ||
661 style == Qt::RadialGradientPattern ||
662 style == Qt::ConicalGradientPattern) {
663 const QMetaEnum gradientType_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientType");
664 const QMetaEnum gradientSpread_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientSpread");
665 const QMetaEnum gradientCoordinate_enum = metaEnum<QAbstractFormBuilderGadget>(
"gradientCoordinate");
667 DomGradient *gradient =
new DomGradient();
668 const QGradient *gr = br.gradient();
669 const QGradient::Type type = gr->type();
670 gradient->setAttributeType(QLatin1StringView(gradientType_enum.valueToKey(type)));
671 gradient->setAttributeSpread(QLatin1StringView(gradientSpread_enum.valueToKey(gr->spread())));
672 gradient->setAttributeCoordinateMode(QLatin1StringView(gradientCoordinate_enum.valueToKey(gr->coordinateMode())));
673 QList<DomGradientStop *> stops;
674 const QGradientStops st = gr->stops();
675 for (
const QGradientStop &pair : st) {
676 DomGradientStop *stop =
new DomGradientStop();
677 stop->setAttributePosition(pair.first);
678 DomColor *color =
new DomColor();
679 color->setElementRed(pair.second.red());
680 color->setElementGreen(pair.second.green());
681 color->setElementBlue(pair.second.blue());
682 color->setAttributeAlpha(pair.second.alpha());
683 stop->setElementColor(color);
686 gradient->setElementGradientStop(stops);
687 if (type == QGradient::LinearGradient) {
688 auto lgr =
static_cast<
const QLinearGradient *>(gr);
689 gradient->setAttributeStartX(lgr->start().x());
690 gradient->setAttributeStartY(lgr->start().y());
691 gradient->setAttributeEndX(lgr->finalStop().x());
692 gradient->setAttributeEndY(lgr->finalStop().y());
693 }
else if (type == QGradient::RadialGradient) {
694 auto rgr =
static_cast<
const QRadialGradient *>(gr);
695 gradient->setAttributeCentralX(rgr->center().x());
696 gradient->setAttributeCentralY(rgr->center().y());
697 gradient->setAttributeFocalX(rgr->focalPoint().x());
698 gradient->setAttributeFocalY(rgr->focalPoint().y());
699 gradient->setAttributeRadius(rgr->radius());
700 }
else if (type == QGradient::ConicalGradient) {
701 auto cgr =
static_cast<
const QConicalGradient *>(gr);
702 gradient->setAttributeCentralX(cgr->center().x());
703 gradient->setAttributeCentralY(cgr->center().y());
704 gradient->setAttributeAngle(cgr->angle());
707 brush->setElementGradient(gradient);
708 }
else if (style == Qt::TexturePattern) {
709 const QPixmap pixmap = br.texture();
710 if (!pixmap.isNull()) {
711 DomProperty *p =
new DomProperty;
712 QFormBuilderExtra::setPixmapProperty(p, {});
713 brush->setElementTexture(p);
716 const QColor &c = br.color();
717 DomColor *color =
new DomColor();
718 color->setElementRed(c.red());
719 color->setElementGreen(c.green());
720 color->setElementBlue(c.blue());
721 color->setAttributeAlpha(c.alpha());
722 brush->setElementColor(color);
738QFormBuilderStrings::QFormBuilderStrings() :
740 {Qt::FontRole,
"font"_L1},
741 {Qt::TextAlignmentRole,
"textAlignment"_L1},
742 {Qt::BackgroundRole,
"background"_L1},
743 {Qt::ForegroundRole,
"foreground"_L1},
744 {Qt::CheckStateRole,
"checkState"_L1}
747 { {Qt::EditRole, Qt::DisplayPropertyRole}, textAttribute},
748 { {Qt::ToolTipRole, Qt::ToolTipPropertyRole}, toolTipAttribute},
749 { {Qt::StatusTipRole, Qt::StatusTipPropertyRole},
"statusTip"_L1},
750 { {Qt::WhatsThisRole, Qt::WhatsThisPropertyRole}, whatsThisAttribute}
753 for (
const RoleNName &it : std::as_const(itemRoles))
754 treeItemRoleHash.insert(it.second, it.first);
757 auto it = itemTextRoles.constBegin();
758 const auto end = itemTextRoles.constEnd();
760 treeItemTextRoleHash.insert(it->second, it->first);