uic: Write QIcon theme enums

Split out some helpers to write the theme code with path fallback
checking for the enum case (identified by a fully qualified icon) or
the old XDG/filename case.

Task-number: QTBUG-121823
Change-Id: If2755483ab899f04e372cf95443f7e03970f6e7f
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
(cherry picked from commit b79cb3dd659c71560dd0324893cbdfe41a42f9dc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Friedemann Kleint 2024-02-07 15:52:38 +01:00 committed by Qt Cherry-pick Bot
parent 8f27ca2346
commit b55cbd1a49
4 changed files with 124 additions and 35 deletions

View File

@ -1808,6 +1808,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
@ -1832,7 +1885,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;
@ -1843,12 +1897,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
@ -1857,31 +1920,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;
}

View File

@ -209,6 +209,8 @@ private:
private:
QString writeFontProperties(const DomFont *f);
QString writeIconProperties(const DomResourceIcon *i);
void writeThemeIconCheckAssignment(const QString &themeValue, const QString &iconName,
const DomResourceIcon *i);
void writePixmapFunctionIcon(QTextStream &output, const QString &iconName,
const QString &indent, const DomResourceIcon *i) const;
QString writeSizePolicy(const DomSizePolicy *sp);

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>122</width>
<height>117</height>
<width>343</width>
<height>478</height>
</rect>
</property>
<property name="windowTitle">
@ -42,9 +42,28 @@
<string>PushButton</string>
</property>
<property name="icon">
<iconset theme="edit-copy">
<normaloff/>
</iconset>
<iconset theme="edit-copy"/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="themeenum">
<property name="text">
<string>PushButton</string>
</property>
<property name="icon">
<iconset theme="QIcon::ThemeIcon::EditCopy"/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="fileandthemeenum">
<property name="text">
<string>PushButton</string>
</property>
<property name="icon">
<iconset theme="QIcon::ThemeIcon::EditCopy">
<normaloff>image7.png</normaloff>image7.png</iconset>
</property>
</widget>
</item>

View File

@ -25,12 +25,14 @@ public:
QPushButton *fileicon;
QPushButton *fileandthemeicon;
QPushButton *themeicon;
QPushButton *themeenum;
QPushButton *fileandthemeenum;
void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName("Form");
Form->resize(122, 117);
Form->resize(343, 478);
verticalLayout = new QVBoxLayout(Form);
verticalLayout->setObjectName("verticalLayout");
fileicon = new QPushButton(Form);
@ -56,17 +58,30 @@ public:
themeicon = new QPushButton(Form);
themeicon->setObjectName("themeicon");
QIcon icon2;
iconThemeName = QString::fromUtf8("edit-copy");
if (QIcon::hasThemeIcon(iconThemeName)) {
icon2 = QIcon::fromTheme(iconThemeName);
} else {
icon2.addFile(QString::fromUtf8(""), QSize(), QIcon::Normal, QIcon::Off);
}
QIcon icon2(QIcon::fromTheme(QString::fromUtf8("edit-copy")));
themeicon->setIcon(icon2);
verticalLayout->addWidget(themeicon);
themeenum = new QPushButton(Form);
themeenum->setObjectName("themeenum");
QIcon icon3(QIcon::fromTheme(QIcon::ThemeIcon::EditCopy));
themeenum->setIcon(icon3);
verticalLayout->addWidget(themeenum);
fileandthemeenum = new QPushButton(Form);
fileandthemeenum->setObjectName("fileandthemeenum");
QIcon icon4;
if (QIcon::hasThemeIcon(QIcon::ThemeIcon::EditCopy)) {
icon4 = QIcon::fromTheme(QIcon::ThemeIcon::EditCopy);
} else {
icon4.addFile(QString::fromUtf8("image7.png"), QSize(), QIcon::Normal, QIcon::Off);
}
fileandthemeenum->setIcon(icon4);
verticalLayout->addWidget(fileandthemeenum);
retranslateUi(Form);
@ -79,6 +94,8 @@ public:
fileicon->setText(QCoreApplication::translate("Form", "fileicon", nullptr));
fileandthemeicon->setText(QCoreApplication::translate("Form", "PushButton", nullptr));
themeicon->setText(QCoreApplication::translate("Form", "PushButton", nullptr));
themeenum->setText(QCoreApplication::translate("Form", "PushButton", nullptr));
fileandthemeenum->setText(QCoreApplication::translate("Form", "PushButton", nullptr));
} // retranslateUi
};