forked from qt-creator/qt-creator
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:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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("<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 {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user