From 608bb8cf3d7e72788a5b47eacfece35f73f90cbb Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Thu, 7 Dec 2023 17:05:47 +0100 Subject: [PATCH] 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 --- src/libs/utils/stylehelper.cpp | 20 ++++++++++++++++ src/libs/utils/stylehelper.h | 1 + .../uifonts/tst_manual_widgets_uifonts.cpp | 24 +++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/libs/utils/stylehelper.cpp b/src/libs/utils/stylehelper.cpp index a5a61ab7bf9..16cc99a5c43 100644 --- a/src/libs/utils/stylehelper.cpp +++ b/src/libs/utils/stylehelper.cpp @@ -979,4 +979,24 @@ QFont StyleHelper::uiFont(UiElement element) 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 diff --git a/src/libs/utils/stylehelper.h b/src/libs/utils/stylehelper.h index b04bf685263..e45bde570bb 100644 --- a/src/libs/utils/stylehelper.h +++ b/src/libs/utils/stylehelper.h @@ -82,6 +82,7 @@ QTCREATOR_UTILS_EXPORT QColor toolBarDropShadowColor(); QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor(); 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 QTCREATOR_UTILS_EXPORT void setBaseColor(const QColor &color); diff --git a/tests/manual/widgets/uifonts/tst_manual_widgets_uifonts.cpp b/tests/manual/widgets/uifonts/tst_manual_widgets_uifonts.cpp index 545cd7ed957..9f07f64865e 100644 --- a/tests/manual/widgets/uifonts/tst_manual_widgets_uifonts.cpp +++ b/tests/manual/widgets/uifonts/tst_manual_widgets_uifonts.cpp @@ -28,22 +28,42 @@ int main(int argc, char *argv[]) static const QString textSample("AaBbCcXxYyZz123"); using namespace Layouting; - Grid fontLabels {}; + Grid fontLabels; for (auto uiElement : uiElements) { const QFont font = StyleHelper::uiFont(uiElement.uiElement); auto *uiElementLabel = new QLabel(uiElement.name); uiElementLabel->setFont(font); + uiElementLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); auto *sampleLabel = new QLabel(textSample); sampleLabel->setFont(font); - fontLabels.addItems({uiElementLabel, sampleLabel, font.toString(), br}); + sampleLabel->setSizePolicy(uiElementLabel->sizePolicy()); + fontLabels.addItems({uiElementLabel, sampleLabel, st, font.toString(), br}); } + QString html(""); + for (auto uiElement : uiElements) { + const QFont font = StyleHelper::uiFont(uiElement.uiElement); + html.append(QString(R"( + + + + + + )").arg(StyleHelper::fontToCssProperties(font)).arg(uiElement.name).arg(textSample)); + } + html.append("
%2%3%1
"); + Column { windowTitle("Utils::StyleHelper::uiFont"), Group { title("As QFont in QLabel"), fontLabels, }, + Group { + title("As inline CSS in HTML elements"), + Row { html }, + }, + st, }.emerge()->show(); return app.exec();