uic: Fix enum values for Python

Add a helper for replacing "::" by "." for Python.

Task-number: PYSIDE-797
Change-Id: I017d430b0b8b2ffbbd3300d583603924fee4d479
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
Friedemann Kleint 2018-11-30 09:51:47 +01:00
parent d54de865a4
commit be56db2c49
3 changed files with 109 additions and 77 deletions

View File

@ -102,11 +102,9 @@ namespace {
if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical")) if (orientation == QLatin1String("Qt::Vertical") || orientation == QLatin1String("Vertical"))
isVspacer = true; isVspacer = true;
} }
const QString horizType = isVspacer ? QLatin1String("QSizePolicy::Minimum") : sizeType;
if (isVspacer) const QString vertType = isVspacer ? sizeType : QLatin1String("QSizePolicy::Minimum");
output << "QSizePolicy::Minimum, " << sizeType << ')'; output << language::enumValue(horizType) << ", " << language::enumValue(vertType) << ')';
else
output << sizeType << ", QSizePolicy::Minimum)";
} }
@ -678,7 +676,7 @@ void WriteInitialization::acceptWidget(DomWidget *node)
<< "setMenuBar(" << varName << ')' << language::eol; << "setMenuBar(" << varName << ')' << language::eol;
} else if (cwi->extends(className, QLatin1String("QToolBar"))) { } else if (cwi->extends(className, QLatin1String("QToolBar"))) {
m_output << m_indent << parentWidget << language::derefPointer << "addToolBar(" m_output << m_indent << parentWidget << language::derefPointer << "addToolBar("
<< toolBarAreaStringFromDOMAttributes(attributes) << varName << language::enumValue(toolBarAreaStringFromDOMAttributes(attributes)) << varName
<< ')' << language::eol; << ')' << language::eol;
if (const DomProperty *pbreak = attributes.value(QLatin1String("toolBarBreak"))) { if (const DomProperty *pbreak = attributes.value(QLatin1String("toolBarBreak"))) {
@ -690,8 +688,10 @@ void WriteInitialization::acceptWidget(DomWidget *node)
} else if (cwi->extends(className, QLatin1String("QDockWidget"))) { } else if (cwi->extends(className, QLatin1String("QDockWidget"))) {
m_output << m_indent << parentWidget << language::derefPointer << "addDockWidget("; m_output << m_indent << parentWidget << language::derefPointer << "addDockWidget(";
if (DomProperty *pstyle = attributes.value(QLatin1String("dockWidgetArea"))) if (DomProperty *pstyle = attributes.value(QLatin1String("dockWidgetArea"))) {
m_output << "Qt::" << language::dockWidgetArea(pstyle->elementNumber()) << ", "; m_output << "Qt" << language::qualifier
<< language::dockWidgetArea(pstyle->elementNumber()) << ", ";
}
m_output << varName << ");\n"; m_output << varName << ");\n";
} else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"))) { } else if (m_uic->customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"))) {
m_output << m_indent << parentWidget << language::derefPointer m_output << m_indent << parentWidget << language::derefPointer
@ -1043,16 +1043,16 @@ void WriteInitialization::acceptLayoutItem(DomLayoutItem *node)
const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1; const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1;
m_output << itemName << ", " << row << ", " << col << ", " << rowSpan << ", " << colSpan; m_output << itemName << ", " << row << ", " << col << ", " << rowSpan << ", " << colSpan;
if (!node->attributeAlignment().isEmpty()) if (!node->attributeAlignment().isEmpty())
m_output << ", " << node->attributeAlignment(); m_output << ", " << language::enumValue(node->attributeAlignment());
} else if (layout->attributeClass() == QLatin1String("QFormLayout")) { } else if (layout->attributeClass() == QLatin1String("QFormLayout")) {
const int row = node->attributeRow(); const int row = node->attributeRow();
const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1; const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1;
const QString role = formLayoutRole(node->attributeColumn(), colSpan); const QString role = formLayoutRole(node->attributeColumn(), colSpan);
m_output << row << ", " << role << ", " << itemName; m_output << row << ", " << language::enumValue(role) << ", " << itemName;
} else { } else {
m_output << itemName; m_output << itemName;
if (layout->attributeClass().contains(QLatin1String("Box")) && !node->attributeAlignment().isEmpty()) if (layout->attributeClass().contains(QLatin1String("Box")) && !node->attributeAlignment().isEmpty())
m_output << ", 0, " << node->attributeAlignment(); m_output << ", 0, " << language::enumValue(node->attributeAlignment());
} }
m_output << ");\n\n"; m_output << ");\n\n";
} }
@ -1267,11 +1267,13 @@ void WriteInitialization::writeProperties(const QString &varName,
shape = QLatin1String("QFrame::VLine"); shape = QLatin1String("QFrame::VLine");
m_output << m_indent << varName << language::derefPointer << "setFrameShape(" m_output << m_indent << varName << language::derefPointer << "setFrameShape("
<< shape << ')' << language::eol; << language::enumValue(shape) << ')' << language::eol;
// QFrame Default is 'Plain'. Make the line 'Sunken' unless otherwise specified // QFrame Default is 'Plain'. Make the line 'Sunken' unless otherwise specified
if (!frameShadowEncountered) { if (!frameShadowEncountered) {
m_output << m_indent << varName << language::derefPointer m_output << m_indent << varName << language::derefPointer
<< "setFrameShadow(QFrame::Sunken)" << language::eol; << "setFrameShadow("
<< language::enumValue(QLatin1String("QFrame::Sunken"))
<< ')' << language::eol;
} }
continue; continue;
} else if ((flags & WritePropertyIgnoreMargin) && propertyName == QLatin1String("margin")) { } else if ((flags & WritePropertyIgnoreMargin) && propertyName == QLatin1String("margin")) {
@ -1351,16 +1353,18 @@ void WriteInitialization::writeProperties(const QString &varName,
case DomProperty::CursorShape: case DomProperty::CursorShape:
if (p->hasAttributeStdset() && !p->attributeStdset()) if (p->hasAttributeStdset() && !p->attributeStdset())
varNewName += language::derefPointer + QLatin1String("viewport()"); varNewName += language::derefPointer + QLatin1String("viewport()");
propertyValue = QString::fromLatin1("QCursor(Qt::%1)") propertyValue = QLatin1String("QCursor(Qt") + language::qualifier
.arg(p->elementCursorShape()); + p->elementCursorShape() + QLatin1Char(')');
break; break;
case DomProperty::Enum: case DomProperty::Enum:
propertyValue = p->elementEnum(); propertyValue = p->elementEnum();
if (!propertyValue.contains(QLatin1String("::"))) if (propertyValue.contains(language::cppQualifier))
propertyValue = className + QLatin1String("::") + propertyValue; propertyValue = language::enumValue(propertyValue);
else
propertyValue.prepend(className + language::qualifier);
break; break;
case DomProperty::Set: case DomProperty::Set:
propertyValue = p->elementSet(); propertyValue = language::enumValue(p->elementSet());
break; break;
case DomProperty::Font: case DomProperty::Font:
propertyValue = writeFontProperties(p->elementFont()); propertyValue = writeFontProperties(p->elementFont());
@ -1411,8 +1415,9 @@ void WriteInitialization::writeProperties(const QString &varName,
} }
case DomProperty::Locale: { case DomProperty::Locale: {
const DomLocale *locale = p->elementLocale(); const DomLocale *locale = p->elementLocale();
propertyValue = QString::fromLatin1("QLocale(QLocale::%1, QLocale::%2)") QTextStream(&propertyValue) << "QLocale(QLocale" << language::qualifier
.arg(locale->attributeLanguage(), locale->attributeCountry()); << locale->attributeLanguage() << ", QLocale" << language::qualifier
<< locale->attributeCountry() << ')';
break; break;
} }
case DomProperty::SizePolicy: { case DomProperty::SizePolicy: {
@ -1569,8 +1574,8 @@ QString WriteInitialization::writeSizePolicy(const DomSizePolicy *sp)
m_output << "QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementHSizeType()) m_output << "QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementHSizeType())
<< ", QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementVSizeType()); << ", QSizePolicy" << language::qualifier << language::sizePolicy(sp->elementVSizeType());
} else if (sp->hasAttributeHSizeType() && sp->hasAttributeVSizeType()) { } else if (sp->hasAttributeHSizeType() && sp->hasAttributeVSizeType()) {
m_output << "QSizePolicy::" << sp->attributeHSizeType() << ", QSizePolicy::" m_output << "QSizePolicy" << language::qualifier << sp->attributeHSizeType()
<< sp->attributeVSizeType(); << ", QSizePolicy" << language::qualifier << sp->attributeVSizeType();
} }
m_output << ')' << language::eol; m_output << ')' << language::eol;
@ -1632,16 +1637,28 @@ QString WriteInitialization::writeFontProperties(const DomFont *f)
<< language::boolValue(f->elementKerning()) << ')' << language::eol; << language::boolValue(f->elementKerning()) << ')' << language::eol;
} }
if (f->hasElementAntialiasing()) { if (f->hasElementAntialiasing()) {
m_output << m_indent << fontName << ".setStyleStrategy(" m_output << m_indent << fontName << ".setStyleStrategy(QFont"
<< (f->elementAntialiasing() ? "QFont::PreferDefault" : "QFont::NoAntialias") << ");\n"; << language::qualifier
<< (f->elementAntialiasing() ? "PreferDefault" : "NoAntialias")
<< ')' << language::eol;
} }
if (f->hasElementStyleStrategy()) { if (f->hasElementStyleStrategy()) {
m_output << m_indent << fontName << ".setStyleStrategy(QFont::" m_output << m_indent << fontName << ".setStyleStrategy(QFont"
<< f->elementStyleStrategy() << ");\n"; << language::qualifier << f->elementStyleStrategy() << ')' << language::eol;
} }
return fontName; return fontName;
} }
static void writeIconAddFile(QTextStream &output, const QString &indent,
const QString &iconName, const QString &fileName,
const char *mode, const char *state)
{
output << indent << iconName << ".addFile("
<< language::qstring(fileName, indent) << ", QSize(), QIcon"
<< language::qualifier << mode << ", QIcon" << language::qualifier
<< state << ')' << language::eol;
}
// Post 4.4 write resource icon // Post 4.4 write resource icon
static void writeResourceIcon(QTextStream &output, static void writeResourceIcon(QTextStream &output,
const QString &iconName, const QString &iconName,
@ -1649,91 +1666,92 @@ static void writeResourceIcon(QTextStream &output,
const DomResourceIcon *i) const DomResourceIcon *i)
{ {
if (i->hasElementNormalOff()) { if (i->hasElementNormalOff()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementNormalOff()->text(),
<< language::qstring(i->elementNormalOff()->text(), indent) "Normal", "Off");
<< ", QSize(), QIcon::Normal, QIcon::Off);\n";
} }
if (i->hasElementNormalOn()) { if (i->hasElementNormalOn()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementNormalOn()->text(),
<< language::qstring(i->elementNormalOn()->text(), indent) "Normal", "On");
<< ", QSize(), QIcon::Normal, QIcon::On);\n";
} }
if (i->hasElementDisabledOff()) { if (i->hasElementDisabledOff()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementDisabledOff()->text(),
<< language::qstring(i->elementDisabledOff()->text(), indent) "Disabled", "Off");
<< ", QSize(), QIcon::Disabled, QIcon::Off);\n";
} }
if (i->hasElementDisabledOn()) { if (i->hasElementDisabledOn()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementDisabledOn()->text(),
<< language::qstring(i->elementDisabledOn()->text(), indent) "Disabled", "On");
<< ", QSize(), QIcon::Disabled, QIcon::On);\n";
} }
if (i->hasElementActiveOff()) { if (i->hasElementActiveOff()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementActiveOff()->text(),
<< language::qstring(i->elementActiveOff()->text(), indent) "Active", "Off");
<< ", QSize(), QIcon::Active, QIcon::Off);\n";
} }
if (i->hasElementActiveOn()) { if (i->hasElementActiveOn()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementActiveOn()->text(),
<< language::qstring(i->elementActiveOn()->text(), indent) "Active", "On");
<< ", QSize(), QIcon::Active, QIcon::On);\n";
} }
if (i->hasElementSelectedOff()) { if (i->hasElementSelectedOff()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementSelectedOff()->text(),
<< language::qstring(i->elementSelectedOff()->text(), indent) "Selected", "Off");
<< ", QSize(), QIcon::Selected, QIcon::Off);\n";
} }
if (i->hasElementSelectedOn()) { if (i->hasElementSelectedOn()) {
output << indent << iconName << ".addFile(" writeIconAddFile(output, indent, iconName, i->elementSelectedOff()->text(),
<< language::qstring(i->elementSelectedOn()->text(), indent) "Selected", "On");
<< "), QSize(), QIcon::Selected, QIcon::On);\n";
} }
} }
static void writeIconAddPixmap(QTextStream &output, const QString &indent,
const QString &iconName, const QString &call,
const char *mode, const char *state)
{
output << indent << iconName << ".addPixmap(" << call << ", QIcon"
<< language::qualifier << mode << ", QIcon" << language::qualifier
<< state << ')' << language::eol;
}
void WriteInitialization::writePixmapFunctionIcon(QTextStream &output, void WriteInitialization::writePixmapFunctionIcon(QTextStream &output,
const QString &iconName, const QString &iconName,
const QString &indent, const QString &indent,
const DomResourceIcon *i) const const DomResourceIcon *i) const
{ {
if (i->hasElementNormalOff()) { if (i->hasElementNormalOff()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementNormalOff()->text()) pixCall(QLatin1String("QPixmap"), i->elementNormalOff()->text()),
<< ", QIcon::Normal, QIcon::Off);\n"; "Normal", "Off");
} }
if (i->hasElementNormalOn()) { if (i->hasElementNormalOn()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementNormalOn()->text()) pixCall(QLatin1String("QPixmap"), i->elementNormalOn()->text()),
<< ", QIcon::Normal, QIcon::On);\n"; "Normal", "On");
} }
if (i->hasElementDisabledOff()) { if (i->hasElementDisabledOff()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementDisabledOff()->text()) pixCall(QLatin1String("QPixmap"), i->elementDisabledOff()->text()),
<< ", QIcon::Disabled, QIcon::Off);\n"; "Disabled", "Off");
} }
if (i->hasElementDisabledOn()) { if (i->hasElementDisabledOn()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementDisabledOn()->text()) pixCall(QLatin1String("QPixmap"), i->elementDisabledOn()->text()),
<< ", QIcon::Disabled, QIcon::On);\n"; "Disabled", "On");
} }
if (i->hasElementActiveOff()) { if (i->hasElementActiveOff()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementActiveOff()->text()) pixCall(QLatin1String("QPixmap"), i->elementActiveOff()->text()),
<< ", QIcon::Active, QIcon::Off);\n"; "Active", "Off");
} }
if (i->hasElementActiveOn()) { if (i->hasElementActiveOn()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementActiveOn()->text()) pixCall(QLatin1String("QPixmap"), i->elementActiveOn()->text()),
<< ", QIcon::Active, QIcon::On);\n"; "Active", "On");
} }
if (i->hasElementSelectedOff()) { if (i->hasElementSelectedOff()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementSelectedOff()->text()) pixCall(QLatin1String("QPixmap"), i->elementSelectedOff()->text()),
<< ", QIcon::Selected, QIcon::Off);\n"; "Selected", "Off");
} }
if (i->hasElementSelectedOn()) { if (i->hasElementSelectedOn()) {
output << indent << iconName << ".addPixmap(" writeIconAddPixmap(output, indent, iconName,
<< pixCall(QLatin1String("QPixmap"), i->elementSelectedOn()->text()) pixCall(QLatin1String("QPixmap"), i->elementSelectedOn()->text()),
<< ", QIcon::Selected, QIcon::On);\n"; "Selected", "On");
} }
} }
@ -1826,7 +1844,7 @@ void WriteInitialization::writeColorGroup(DomColorGroup *colorGroup, const QStri
const DomColor *color = colors.at(i); const DomColor *color = colors.at(i);
m_output << m_indent << paletteName << ".setColor(" << group m_output << m_indent << paletteName << ".setColor(" << group
<< ", QPalette::" << language::paletteColorRole(i) << ", QPalette" << language::qualifier << language::paletteColorRole(i)
<< ", " << domColor2QString(color) << ", " << domColor2QString(color)
<< ");\n"; << ");\n";
} }
@ -1843,8 +1861,9 @@ void WriteInitialization::writeColorGroup(DomColorGroup *colorGroup, const QStri
<< versionAdded.majorVersion() << ", " << versionAdded.minorVersion() << versionAdded.majorVersion() << ", " << versionAdded.minorVersion()
<< ", " << versionAdded.microVersion() << ")\n"; << ", " << versionAdded.microVersion() << ")\n";
} }
m_output << m_indent << paletteName << ".setBrush(" << group m_output << m_indent << paletteName << ".setBrush("
<< ", " << "QPalette::" << roleName << language::enumValue(group) << ", "
<< "QPalette" << language::qualifier << roleName
<< ", " << brushName << ");\n"; << ", " << brushName << ");\n";
if (!versionAdded.isNull()) if (!versionAdded.isNull())
m_output << "#endif\n"; m_output << "#endif\n";

View File

@ -412,4 +412,15 @@ QString boolValue(bool v)
Q_UNREACHABLE(); Q_UNREACHABLE();
} }
static inline QString dot() { return QStringLiteral("."); }
QString enumValue(const QString &value)
{
if (language() == Language::Cpp || !value.contains(cppQualifier))
return value;
QString fixed = value;
fixed.replace(cppQualifier, dot());
return fixed;
}
} // namespace language } // namespace language

View File

@ -201,6 +201,8 @@ void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSl
QString boolValue(bool v); QString boolValue(bool v);
QString enumValue(const QString &value);
} // namespace language } // namespace language
#endif // LANGUAGE_H #endif // LANGUAGE_H