diff options
author | Florian Koch <devfk@outlook.com> | 2022-08-28 19:32:07 +0200 |
---|---|---|
committer | Florian Koch <devfk@outlook.com> | 2022-09-12 16:40:15 +0000 |
commit | 69fad91660ea52b30daffa7e9507163134edf68b (patch) | |
tree | 53061d652550ed9a62bef62fe3d86fb9992da3d3 /src/plugins/texteditor/fontsettings.cpp | |
parent | 0f2ade49f9819ee8ca63c5ee75bb66560e5760cc (diff) |
Re-introduction of the feature to adjust the line spacing
This already has been implemented in change
dc64f3207bdf6c0d295859e47791cb8193e67f4e, but was reverted with
change f220cb0e23729ddccf52c25dae4e4696641bc62d) because this does not
work with text wrapping rendering, due to internal limitations of Qt.
Since this is a highly requested feature (e.g. QTCREATORBUG-13727), but
an internal change within Qt is not in sight, the approach taken here
is to offer the text wrapping feature in the settings only when the
line spacing is set to 100%. Additionally, a change has been made to
the layout of the display settings page to reflect this.
Fixes: QTCREATORBUG-13727
Change-Id: Ib233cf90a5f336bc591fa1bf860e162fa774dfe3
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor/fontsettings.cpp')
-rw-r--r-- | src/plugins/texteditor/fontsettings.cpp | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index 696cf78e12..549106f22b 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -23,6 +23,7 @@ static const char fontFamilyKey[] = "FontFamily"; static const char fontSizeKey[] = "FontSize"; static const char fontZoomKey[] = "FontZoom"; +static const char lineSpacingKey[] = "LineSpacing"; static const char antialiasKey[] = "FontAntialias"; static const char schemeFileNamesKey[] = "ColorSchemes"; @@ -38,7 +39,9 @@ FontSettings::FontSettings() : m_family(defaultFixedFontFamily()), m_fontSize(defaultFontSize()), m_fontZoom(100), - m_antialias(DEFAULT_ANTIALIAS) + m_lineSpacing(100), + m_antialias(DEFAULT_ANTIALIAS), + m_lineSpacingCache(0) { } @@ -47,10 +50,10 @@ void FontSettings::clear() m_family = defaultFixedFontFamily(); m_fontSize = defaultFontSize(); m_fontZoom = 100; + m_lineSpacing = 100; m_antialias = DEFAULT_ANTIALIAS; m_scheme.clear(); - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); } static QString settingsGroup() @@ -70,6 +73,9 @@ void FontSettings::toSettings(QSettings *s) const if (m_fontZoom!= 100 || s->contains(QLatin1String(fontZoomKey))) s->setValue(QLatin1String(fontZoomKey), m_fontZoom); + if (m_lineSpacing != 100 || s->contains(QLatin1String(lineSpacingKey))) + s->setValue(QLatin1String(lineSpacingKey), m_lineSpacing); + if (m_antialias != DEFAULT_ANTIALIAS || s->contains(QLatin1String(antialiasKey))) s->setValue(QLatin1String(antialiasKey), m_antialias); @@ -95,6 +101,7 @@ bool FontSettings::fromSettings(const FormatDescriptions &descriptions, const QS m_family = s->value(group + QLatin1String(fontFamilyKey), defaultFixedFontFamily()).toString(); m_fontSize = s->value(group + QLatin1String(fontSizeKey), m_fontSize).toInt(); m_fontZoom= s->value(group + QLatin1String(fontZoomKey), m_fontZoom).toInt(); + m_lineSpacing = s->value(group + QLatin1String(lineSpacingKey), m_lineSpacing).toInt(); m_antialias = s->value(group + QLatin1String(antialiasKey), DEFAULT_ANTIALIAS).toBool(); if (s->contains(group + QLatin1String(schemeFileNamesKey))) { @@ -114,6 +121,7 @@ bool FontSettings::equals(const FontSettings &f) const return m_family == f.m_family && m_schemeFileName == f.m_schemeFileName && m_fontSize == f.m_fontSize + && m_lineSpacing == f.m_lineSpacing && m_fontZoom == f.m_fontZoom && m_antialias == f.m_antialias && m_scheme == f.m_scheme; @@ -250,6 +258,13 @@ void FontSettings::addMixinStyle(QTextCharFormat &textCharFormat, }; } +void FontSettings::clearCaches() +{ + m_formatCache.clear(); + m_textCharFormatCache.clear(); + m_lineSpacingCache = 0; +} + QTextCharFormat FontSettings::toTextCharFormat(TextStyles textStyles) const { auto textCharFormatIterator = m_textCharFormatCache.find(textStyles); @@ -290,8 +305,7 @@ QString FontSettings::family() const void FontSettings::setFamily(const QString &family) { m_family = family; - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); } /** @@ -305,8 +319,7 @@ int FontSettings::fontSize() const void FontSettings::setFontSize(int size) { m_fontSize = size; - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); } /** @@ -320,8 +333,33 @@ int FontSettings::fontZoom() const void FontSettings::setFontZoom(int zoom) { m_fontZoom = zoom; - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); +} + +qreal FontSettings::lineSpacing() const +{ + if (m_lineSpacing == 100) { + QFontMetricsF fm(font()); + return fm.lineSpacing(); + } + + if (qFuzzyIsNull(m_lineSpacingCache)) { + auto currentFont = font(); + currentFont.setPointSize(m_fontSize * m_fontZoom / 100); + m_lineSpacingCache = QFontMetricsF(currentFont).lineSpacing() / 100 * m_lineSpacing; + } + return m_lineSpacingCache; +} + +int FontSettings::relativeLineSpacing() const +{ + return m_lineSpacing; +} + +void FontSettings::setRelativeLineSpacing(int relativeLineSpacing) +{ + m_lineSpacing = relativeLineSpacing; + m_lineSpacingCache = 0; } QFont FontSettings::font() const @@ -342,8 +380,7 @@ bool FontSettings::antialias() const void FontSettings::setAntialias(bool antialias) { m_antialias = antialias; - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); } /** @@ -380,8 +417,8 @@ void FontSettings::setColorSchemeFileName(const QString &fileName) bool FontSettings::loadColorScheme(const QString &fileName, const FormatDescriptions &descriptions) { - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); + bool loaded = true; m_schemeFileName = fileName; @@ -445,8 +482,7 @@ const ColorScheme &FontSettings::colorScheme() const void FontSettings::setColorScheme(const ColorScheme &scheme) { m_scheme = scheme; - m_formatCache.clear(); - m_textCharFormatCache.clear(); + clearCaches(); } static QString defaultFontFamily() |