Utils: Add StyleHelper::fontToCssProperties

This function takes a QFont and returns the relevant CSS properties as a
string. Using that CSS string in Qt's HTML supporting widgets ensures
the true to detail HTML rendering of text elements with those CSS
properties.

This change also adds an HTML sample to tst_manual_widgets_uifonts.

Change-Id: I7caf3214854f6ee62ae1211015572c640fc08dc8
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Alessandro Portale
2023-12-07 17:05:47 +01:00
parent 3e62f5d522
commit 608bb8cf3d
3 changed files with 43 additions and 2 deletions

View File

@@ -979,4 +979,24 @@ QFont StyleHelper::uiFont(UiElement element)
return font; return font;
} }
QString StyleHelper::fontToCssProperties(const QFont &font)
{
const QString fontSize = font.pixelSize() != -1 ? QString::number(font.pixelSize()) + "px"
: QString::number(font.pointSizeF()) + "pt";
const QString fontStyle = QLatin1String(font.style() == QFont::StyleNormal
? "normal" : font.style() == QFont::StyleItalic
? "italic" : "oblique");
const QString fontShorthand = fontStyle + " " + QString::number(font.weight()) + " "
+ fontSize + " " + font.family();
const QString textDecoration = QLatin1String(font.underline() ? "underline" : "none");
const QString propertyTemplate = "%1: %2";
const QStringList cssProperties = {
propertyTemplate.arg("font").arg(fontShorthand),
propertyTemplate.arg("text-decoration").arg(textDecoration),
propertyTemplate.arg("word-spacing").arg(font.wordSpacing()),
};
const QString fontCssStyle = cssProperties.join("; ");
return fontCssStyle;
}
} // namespace Utils } // namespace Utils

View File

@@ -82,6 +82,7 @@ QTCREATOR_UTILS_EXPORT QColor toolBarDropShadowColor();
QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor(); QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor();
QTCREATOR_UTILS_EXPORT QFont uiFont(UiElement element); QTCREATOR_UTILS_EXPORT QFont uiFont(UiElement element);
QTCREATOR_UTILS_EXPORT QString fontToCssProperties(const QFont &font);
// Sets the base color and makes sure all top level widgets are updated // Sets the base color and makes sure all top level widgets are updated
QTCREATOR_UTILS_EXPORT void setBaseColor(const QColor &color); QTCREATOR_UTILS_EXPORT void setBaseColor(const QColor &color);

View File

@@ -28,22 +28,42 @@ int main(int argc, char *argv[])
static const QString textSample("AaBbCcXxYyZz123"); static const QString textSample("AaBbCcXxYyZz123");
using namespace Layouting; using namespace Layouting;
Grid fontLabels {}; Grid fontLabels;
for (auto uiElement : uiElements) { for (auto uiElement : uiElements) {
const QFont font = StyleHelper::uiFont(uiElement.uiElement); const QFont font = StyleHelper::uiFont(uiElement.uiElement);
auto *uiElementLabel = new QLabel(uiElement.name); auto *uiElementLabel = new QLabel(uiElement.name);
uiElementLabel->setFont(font); uiElementLabel->setFont(font);
uiElementLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
auto *sampleLabel = new QLabel(textSample); auto *sampleLabel = new QLabel(textSample);
sampleLabel->setFont(font); sampleLabel->setFont(font);
fontLabels.addItems({uiElementLabel, sampleLabel, font.toString(), br}); sampleLabel->setSizePolicy(uiElementLabel->sizePolicy());
fontLabels.addItems({uiElementLabel, sampleLabel, st, font.toString(), br});
} }
QString html("<html><body><table>");
for (auto uiElement : uiElements) {
const QFont font = StyleHelper::uiFont(uiElement.uiElement);
html.append(QString(R"(
<tr>
<td style="%1">%2</td>
<td style="%1">%3</td>
<td>%1</td>
</tr>
)").arg(StyleHelper::fontToCssProperties(font)).arg(uiElement.name).arg(textSample));
}
html.append("</table></body></html>");
Column { Column {
windowTitle("Utils::StyleHelper::uiFont"), windowTitle("Utils::StyleHelper::uiFont"),
Group { Group {
title("As QFont in QLabel"), title("As QFont in QLabel"),
fontLabels, fontLabels,
}, },
Group {
title("As inline CSS in HTML elements"),
Row { html },
},
st,
}.emerge()->show(); }.emerge()->show();
return app.exec(); return app.exec();