diff options
Diffstat (limited to 'src/tools/uic/cpp/cppwriteinitialization.cpp')
-rw-r--r-- | src/tools/uic/cpp/cppwriteinitialization.cpp | 258 |
1 files changed, 180 insertions, 78 deletions
diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index 4ccda8e11e..205d6a50a9 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -24,6 +24,34 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; namespace { + + // Expand "Horizontal", "Qt::Horizontal" to "Qt::Orientation::Horizontal" + QString expandEnum(QString value, const QString &prefix) + { + if (value.startsWith(prefix)) + return value; + const auto pos = value.lastIndexOf("::"_L1); + if (pos == -1) + return prefix + "::"_L1 + value; + value.replace(0, pos, prefix); + return value; + } + + inline QString expandSizePolicyEnum(const QString &value) + { + return expandEnum(value, "QSizePolicy::Policy"_L1); + } + + inline QString expandToolBarArea(const QString &value) + { + return expandEnum(value, "Qt::ToolBarArea"_L1); + } + + inline QString expandDockWidgetArea(const QString &value) + { + return expandEnum(value, "Qt::DockWidgetArea"_L1); + } + // figure out the toolbar area of a DOM attrib list. // By legacy, it is stored as an integer. As of 4.3.0, it is the enumeration value. QString toolBarAreaStringFromDOMAttributes(const CPP::WriteInitialization::DomPropertyMap &attributes) { @@ -33,7 +61,7 @@ namespace { return result; switch (pstyle->kind()) { case DomProperty::Number: - result = QLatin1StringView(language::toolbarArea(pstyle->elementNumber())); + result = language::toolbarArea(pstyle->elementNumber()); break; case DomProperty::Enum: result = pstyle->elementEnum(); @@ -41,9 +69,7 @@ namespace { default: break; } - if (!result.startsWith("Qt::"_L1)) - result.prepend("Qt::"_L1); - return result + ", "_L1; + return expandToolBarArea(result) + ", "_L1; } // Write a statement to create a spacer item. @@ -62,27 +88,17 @@ namespace { output << w << ", " << h << ", "; // size type - QString sizeType; - if (const DomProperty *st = properties.value("sizeType"_L1)) { - const QString value = st->elementEnum(); - if (value.startsWith("QSizePolicy::"_L1)) - sizeType = value; - else - sizeType = "QSizePolicy::"_L1 + value; - } else { - sizeType = QStringLiteral("QSizePolicy::Expanding"); - } + const DomProperty *st = properties.value("sizeType"_L1); + QString horizType = st != nullptr ? st->elementEnum() : "Expanding"_L1; + QString vertType = "Minimum"_L1; // orientation - bool isVspacer = false; - if (const DomProperty *o = properties.value("orientation"_L1)) { - const QString orientation = o->elementEnum(); - if (orientation == "Qt::Vertical"_L1 || orientation == "Vertical"_L1) - isVspacer = true; - } - const QString horizType = isVspacer ? "QSizePolicy::Minimum"_L1 : sizeType; - const QString vertType = isVspacer ? sizeType : "QSizePolicy::Minimum"_L1; - output << language::enumValue(horizType) << ", " << language::enumValue(vertType) << ')'; + const DomProperty *o = properties.value("orientation"_L1); + if (o != nullptr && o->elementEnum().endsWith("Vertical"_L1)) + std::swap(horizType, vertType); + + output << language::enumValue(expandSizePolicyEnum(horizType)) << ", " + << language::enumValue(expandSizePolicyEnum(vertType)) << ')'; } @@ -180,6 +196,15 @@ FontHandle::FontHandle(const DomFont *domFont) : { } +static QString fontWeight(const DomFont *domFont) +{ + if (domFont->hasElementFontWeight()) + return domFont->elementFontWeight(); + if (domFont->hasElementBold()) + return domFont->elementBold() ? u"Bold"_s : u"Normal"_s; + return {}; +} + int FontHandle::compare(const FontHandle &rhs) const { const QString family = m_domFont->hasElementFamily() ? m_domFont->elementFamily() : QString(); @@ -194,10 +219,10 @@ int FontHandle::compare(const FontHandle &rhs) const if (const int crc = compareInt(pointSize, rhsPointSize)) return crc; - const int bold = m_domFont->hasElementBold() ? (m_domFont->elementBold() ? 1 : 0) : -1; - const int rhsBold = rhs.m_domFont->hasElementBold() ? (rhs.m_domFont->elementBold() ? 1 : 0) : -1; - if (const int crc = compareInt(bold, rhsBold)) - return crc; + const QString fontWeight = CPP::fontWeight(m_domFont); + const QString rhsFontWeight = CPP::fontWeight(rhs.m_domFont); + if (const int wrc = fontWeight.compare(rhsFontWeight)) + return wrc; const int italic = m_domFont->hasElementItalic() ? (m_domFont->elementItalic() ? 1 : 0) : -1; const int rhsItalic = rhs.m_domFont->hasElementItalic() ? (rhs.m_domFont->elementItalic() ? 1 : 0) : -1; @@ -209,11 +234,6 @@ int FontHandle::compare(const FontHandle &rhs) const if (const int crc = compareInt(underline, rhsUnderline)) return crc; - const int weight = m_domFont->hasElementWeight() ? m_domFont->elementWeight() : -1; - const int rhsWeight = rhs.m_domFont->hasElementWeight() ? rhs.m_domFont->elementWeight() : -1; - if (const int crc = compareInt(weight, rhsWeight)) - return crc; - const int strikeOut = m_domFont->hasElementStrikeOut() ? (m_domFont->elementStrikeOut() ? 1 : 0) : -1; const int rhsStrikeOut = rhs.m_domFont->hasElementStrikeOut() ? (rhs.m_domFont->elementStrikeOut() ? 1 : 0) : -1; if (const int crc = compareInt(strikeOut, rhsStrikeOut)) @@ -235,6 +255,13 @@ int FontHandle::compare(const FontHandle &rhs) const if (const int src = styleStrategy.compare(rhsStyleStrategy)) return src; + const QString hintingPreference = m_domFont->hasElementHintingPreference() + ? m_domFont->elementHintingPreference() : QString(); + const QString rhsHintingPreference = rhs.m_domFont->hasElementHintingPreference() + ? rhs.m_domFont->elementHintingPreference() : QString(); + if (const int src = hintingPreference.compare(rhsHintingPreference)) + return src; + return 0; } @@ -513,7 +540,7 @@ void WriteInitialization::acceptUI(DomUI *node) if (!m_buddies.empty()) m_output << language::openQtConfig(shortcutConfigKey()); - for (const Buddy &b : qAsConst(m_buddies)) { + for (const Buddy &b : std::as_const(m_buddies)) { const QString buddyVarName = m_driver->widgetVariableName(b.buddyAttributeName); if (buddyVarName.isEmpty()) { fprintf(stderr, "%s: Warning: Buddy assignment: '%s' is not a valid widget.\n", @@ -690,8 +717,8 @@ void WriteInitialization::acceptWidget(DomWidget *node) } else if (cwi->extends(className, "QDockWidget")) { m_output << m_indent << parentWidget << language::derefPointer << "addDockWidget("; if (DomProperty *pstyle = attributes.value("dockWidgetArea"_L1)) { - m_output << "Qt" << language::qualifier - << language::dockWidgetArea(pstyle->elementNumber()) << ", "; + QString a = expandDockWidgetArea(language::dockWidgetArea(pstyle->elementNumber())); + m_output << language::enumValue(a) << ", "; } m_output << varName << ")" << language::eol; } else if (m_uic->customWidgetsInfo()->extends(className, "QStatusBar")) { @@ -977,7 +1004,7 @@ void WriteInitialization::writePropertyList(const QString &varName, if (value.isEmpty()) return; const QStringList list = value.split(u','); - const int count = list.count(); + const int count = list.size(); for (int i = 0; i < count; i++) { if (list.at(i) != defaultValue) { m_output << m_indent << varName << language::derefPointer << setFunction @@ -1273,9 +1300,9 @@ void WriteInitialization::writeProperties(const QString &varName, } else if (propertyName == "orientation"_L1 && m_uic->customWidgetsInfo()->extends(className, "Line")) { // Line support - QString shape = u"QFrame::HLine"_s; - if (p->elementEnum() == "Qt::Vertical"_L1) - shape = u"QFrame::VLine"_s; + QString shape = u"QFrame::Shape::HLine"_s; + if (p->elementEnum().endsWith("::Vertical"_L1)) + shape = u"QFrame::Shape::VLine"_s; m_output << m_indent << varName << language::derefPointer << "setFrameShape(" << language::enumValue(shape) << ')' << language::eol; @@ -1283,7 +1310,7 @@ void WriteInitialization::writeProperties(const QString &varName, if (!frameShadowEncountered) { m_output << m_indent << varName << language::derefPointer << "setFrameShadow(" - << language::enumValue("QFrame::Sunken"_L1) + << language::enumValue("QFrame::Shadow::Sunken"_L1) << ')' << language::eol; } continue; @@ -1366,8 +1393,8 @@ void WriteInitialization::writeProperties(const QString &varName, case DomProperty::CursorShape: if (p->hasAttributeStdset() && !p->attributeStdset()) varNewName += language::derefPointer + "viewport()"_L1; - propertyValue = "QCursor(Qt"_L1 + language::qualifier - + p->elementCursorShape() + u')'; + propertyValue = "QCursor(Qt"_L1 + language::qualifier + "CursorShape"_L1 + + language::qualifier + p->elementCursorShape() + u')'; break; case DomProperty::Enum: propertyValue = p->elementEnum(); @@ -1583,12 +1610,18 @@ QString WriteInitialization::writeSizePolicy(const DomSizePolicy *sp) m_sizePolicyNameMap.insert(sizePolicyHandle, spName); m_output << m_indent << language::stackVariableWithInitParameters("QSizePolicy", spName); + QString horizPolicy; + QString vertPolicy; if (sp->hasElementHSizeType() && sp->hasElementVSizeType()) { - m_output << "QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementHSizeType()) - << ", QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementVSizeType()); + horizPolicy = language::sizePolicy(sp->elementHSizeType()); + vertPolicy = language::sizePolicy(sp->elementVSizeType()); } else if (sp->hasAttributeHSizeType() && sp->hasAttributeVSizeType()) { - m_output << "QSizePolicy" << language::qualifier << sp->attributeHSizeType() - << ", QSizePolicy" << language::qualifier << sp->attributeVSizeType(); + horizPolicy = sp->attributeHSizeType(); + vertPolicy = sp->attributeVSizeType(); + } + if (!horizPolicy.isEmpty() && !vertPolicy.isEmpty()) { + m_output << language::enumValue(expandSizePolicyEnum(horizPolicy)) + << ", " << language::enumValue(expandSizePolicyEnum(vertPolicy)); } m_output << ')' << language::eol; @@ -1627,10 +1660,14 @@ QString WriteInitialization::writeFontProperties(const DomFont *f) << ")" << language::eol; } - if (f->hasElementBold()) { + if (f->hasElementFontWeight()) { + m_output << m_indent << fontName << ".setWeight(QFont" + << language::qualifier << f->elementFontWeight() << ')' << language::eol; + } else if (f->hasElementBold()) { m_output << m_indent << fontName << ".setBold(" << language::boolValue(f->elementBold()) << ')' << language::eol; } + if (f->hasElementItalic()) { m_output << m_indent << fontName << ".setItalic(" << language::boolValue(f->elementItalic()) << ')' << language::eol; @@ -1657,6 +1694,11 @@ QString WriteInitialization::writeFontProperties(const DomFont *f) m_output << m_indent << fontName << ".setStyleStrategy(QFont" << language::qualifier << f->elementStyleStrategy() << ')' << language::eol; } + if (f->hasElementHintingPreference()) { + m_output << m_indent << fontName << ".setHintingPreference(QFont" + << language::qualifier << f->elementHintingPreference() << ')' << language::eol; + } + return fontName; } @@ -1666,8 +1708,9 @@ static void writeIconAddFile(QTextStream &output, const QString &indent, { output << indent << iconName << ".addFile(" << language::qstring(fileName, indent) << ", QSize(), QIcon" - << language::qualifier << mode << ", QIcon" << language::qualifier - << state << ')' << language::eol; + << language::qualifier << "Mode" << language::qualifier << mode + << ", QIcon" << language::qualifier << "State" << language::qualifier << state + << ')' << language::eol; } // Post 4.4 write resource icon @@ -1715,7 +1758,8 @@ static void writeIconAddPixmap(QTextStream &output, const QString &indent, const char *mode, const char *state) { output << indent << iconName << ".addPixmap(" << call << ", QIcon" - << language::qualifier << mode << ", QIcon" << language::qualifier + << language::qualifier << "Mode" << language::qualifier << mode + << ", QIcon" << language::qualifier << "State" << language::qualifier << state << ')' << language::eol; } @@ -1766,6 +1810,59 @@ void WriteInitialization::writePixmapFunctionIcon(QTextStream &output, } } +// Write QIcon::fromTheme() (value from enum or variable) +struct iconFromTheme +{ + explicit iconFromTheme(const QString &theme) : m_theme(theme) {} + + QString m_theme; +}; + +QTextStream &operator<<(QTextStream &str, const iconFromTheme &i) +{ + str << "QIcon" << language::qualifier << "fromTheme(" << i.m_theme << ')'; + return str; +} + +// Write QIcon::fromTheme() for an XDG icon from string literal +struct iconFromThemeStringLiteral +{ + explicit iconFromThemeStringLiteral(const QString &theme) : m_theme(theme) {} + + QString m_theme; +}; + +QTextStream &operator<<(QTextStream &str, const iconFromThemeStringLiteral &i) +{ + str << "QIcon" << language::qualifier << "fromTheme(" << language::qstring(i.m_theme) << ')'; + return str; +} + +// Write QIcon::fromTheme() with a path as fallback, add a check using +// QIcon::hasThemeIcon(). +void WriteInitialization::writeThemeIconCheckAssignment(const QString &themeValue, + const QString &iconName, + const DomResourceIcon *i) + +{ + const bool isCpp = language::language() == Language::Cpp; + m_output << m_indent << "if "; + if (isCpp) + m_output << '('; + m_output << "QIcon" << language::qualifier << "hasThemeIcon(" + << themeValue << ')' << (isCpp ? ") {" : ":") << '\n' + << m_dindent << iconName << " = " << iconFromTheme(themeValue) + << language::eol; + m_output << m_indent << (isCpp ? "} else {" : "else:") << '\n'; + if (m_uic->pixmapFunction().isEmpty()) + writeResourceIcon(m_output, iconName, m_dindent, i); + else + writePixmapFunctionIcon(m_output, iconName, m_dindent, i); + if (isCpp) + m_output << m_indent << '}'; + m_output << '\n'; +} + QString WriteInitialization::writeIconProperties(const DomResourceIcon *i) { // check cache @@ -1790,7 +1887,8 @@ QString WriteInitialization::writeIconProperties(const DomResourceIcon *i) } // 4.4 onwards - if (i->attributeTheme().isEmpty()) { + QString theme = i->attributeTheme(); + if (theme.isEmpty()) { // No theme: Write resource icon as is m_output << m_indent << language::stackVariable("QIcon", iconName) << language::eol; @@ -1801,12 +1899,21 @@ QString WriteInitialization::writeIconProperties(const DomResourceIcon *i) return iconName; } + const bool isThemeEnum = theme.startsWith("QIcon::"_L1); + if (isThemeEnum) + theme = language::enumValue(theme); + // Theme: Generate code to check the theme and default to resource if (iconHasStatePixmaps(i)) { // Theme + default state pixmaps: // Generate code to check the theme and default to state pixmaps m_output << m_indent << language::stackVariable("QIcon", iconName) << language::eol; - const char themeNameStringVariableC[] = "iconThemeName"; + if (isThemeEnum) { + writeThemeIconCheckAssignment(theme, iconName, i); + return iconName; + } + + static constexpr auto themeNameStringVariableC = "iconThemeName"_L1; // Store theme name in a variable m_output << m_indent; if (m_firstThemeIcon) { // Declare variable string @@ -1815,31 +1922,19 @@ QString WriteInitialization::writeIconProperties(const DomResourceIcon *i) m_firstThemeIcon = false; } m_output << themeNameStringVariableC << " = " - << language::qstring(i->attributeTheme()) << language::eol; - m_output << m_indent << "if "; - if (isCpp) - m_output << '('; - m_output << "QIcon" << language::qualifier << "hasThemeIcon(" - << themeNameStringVariableC << ')' << (isCpp ? ") {" : ":") << '\n' - << m_dindent << iconName << " = QIcon" << language::qualifier << "fromTheme(" - << themeNameStringVariableC << ')' << language::eol - << m_indent << (isCpp ? "} else {" : "else:") << '\n'; - if (m_uic->pixmapFunction().isEmpty()) - writeResourceIcon(m_output, iconName, m_dindent, i); - else - writePixmapFunctionIcon(m_output, iconName, m_dindent, i); - if (isCpp) - m_output << m_indent << '}'; - m_output << '\n'; + << language::qstring(theme) << language::eol; + writeThemeIconCheckAssignment(themeNameStringVariableC, iconName, i); return iconName; } // Theme, but no state pixmaps: Construct from theme directly. m_output << m_indent - << language::stackVariableWithInitParameters("QIcon", iconName) - << "QIcon" << language::qualifier << "fromTheme(" - << language::qstring(i->attributeTheme()) << "))" - << language::eol; + << language::stackVariableWithInitParameters("QIcon", iconName); + if (isThemeEnum) + m_output << iconFromTheme(theme); + else + m_output << iconFromThemeStringLiteral(theme); + m_output << ')' << language::eol; return iconName; } @@ -2329,7 +2424,7 @@ void WriteInitialization::initializeTreeWidget(DomWidget *w) QString tempName = disableSorting(w, varName); const auto items = initializeTreeWidgetItems(w->elementItem()); - for (int i = 0; i < items.count(); i++) { + for (int i = 0; i < items.size(); i++) { Item *itm = items[i]; itm->writeSetupUi(varName); QString parentPath; @@ -2632,14 +2727,21 @@ void WriteInitialization::acceptConnection(DomConnection *connection) return; } const QString senderSignature = connection->elementSignal(); + const QString slotSignature = connection->elementSlot(); + const bool senderAmbiguous = m_uic->customWidgetsInfo()->isAmbiguousSignal(senderDecl.className, + senderSignature); + const bool slotAmbiguous = m_uic->customWidgetsInfo()->isAmbiguousSlot(receiverDecl.className, + slotSignature); + language::SignalSlotOptions signalOptions; - if (m_uic->customWidgetsInfo()->isAmbiguousSignal(senderDecl.className, senderSignature)) - signalOptions.setFlag(language::SignalSlotOption::Ambiguous); + signalOptions.setFlag(language::SignalSlotOption::Ambiguous, senderAmbiguous); + language::SignalSlotOptions slotOptions; + slotOptions.setFlag(language::SignalSlotOption::Ambiguous, slotAmbiguous); language::SignalSlot theSignal{senderDecl.name, senderSignature, senderDecl.className, signalOptions}; - language::SignalSlot theSlot{receiverDecl.name, connection->elementSlot(), - receiverDecl.className, {}}; + language::SignalSlot theSlot{receiverDecl.name, slotSignature, + receiverDecl.className, slotOptions}; m_output << m_indent; language::formatConnection(m_output, theSignal, theSlot, @@ -2734,7 +2836,7 @@ QString WriteInitialization::Item::writeSetupUi(const QString &parent, Item::Emp m_setupUiStream << language::closeQtConfig(it.key()); ++it; } - for (Item *child : qAsConst(m_children)) + for (Item *child : std::as_const(m_children)) child->writeSetupUi(uniqueName); return uniqueName; } |