diff options
author | Marco Bubke <marco.bubke@theqtcompany.com> | 2016-02-22 15:54:14 +0100 |
---|---|---|
committer | Marco Bubke <marco.bubke@theqtcompany.com> | 2016-02-23 08:44:07 +0000 |
commit | b21b5bb2aeecda8df530da37e3269f99dba771b1 (patch) | |
tree | dbe8b506b6896ec5fa85e1c11d320b2358dd4b98 /src/plugins/texteditor/fontsettings.cpp | |
parent | d7a43eaa18f5048aa73a5fa55e98a6c0f99d351a (diff) |
TextEditor: Add mixins for font settings
You can only set one text style but in many cases like
Function and Declaration it would be nice if they could be merged.
Change-Id: Icda892057b79eef1bea2fa8b2c5f0f7cbc5f518a
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
Diffstat (limited to 'src/plugins/texteditor/fontsettings.cpp')
-rw-r--r-- | src/plugins/texteditor/fontsettings.cpp | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index e78ce6412c7..5303cf9c8de 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -139,8 +139,9 @@ bool FontSettings::equals(const FontSettings &f) const */ QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const { - if (m_formatCache.contains(category)) - return m_formatCache.value(category); + auto textCharFormatIterator = m_formatCache.find(category); + if (textCharFormatIterator != m_formatCache.end()) + return *textCharFormatIterator; const Format &f = m_scheme.formatFor(category); QTextCharFormat tf; @@ -174,6 +175,60 @@ QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const return tf; } +uint qHash(const TextStyles &textStyles) +{ + uint hash = ::qHash(quint8(textStyles.mainStyle)); + + hash ^= ::qHashRange(textStyles.mixinStyles.cbegin(), textStyles.mixinStyles.cend()); + + return hash; +} + +bool operator==(const TextStyles &first, const TextStyles &second) +{ + return first.mainStyle == second.mainStyle + && first.mixinStyles == second.mixinStyles; +} + +void FontSettings::addMixinStyle(QTextCharFormat &textCharFormat, + const MixinTextStyles &mixinStyles) const +{ + for (TextStyle mixinStyle : mixinStyles) { + const QTextCharFormat mixinTextCharFormat = toTextCharFormat(mixinStyle); + if (!textCharFormat.hasProperty(QTextFormat::ForegroundBrush)) + textCharFormat.setForeground(mixinTextCharFormat.foreground()); + + if (!textCharFormat.hasProperty(QTextFormat::BackgroundBrush)) + textCharFormat.setBackground(mixinTextCharFormat.background()); + + if (!textCharFormat.fontItalic()) + textCharFormat.setFontItalic(mixinTextCharFormat.fontItalic()); + + if (textCharFormat.fontWeight() == QFont::Normal) + textCharFormat.setFontWeight(mixinTextCharFormat.fontWeight()); + + if (textCharFormat.underlineStyle() == QTextCharFormat::NoUnderline) { + textCharFormat.setUnderlineStyle(mixinTextCharFormat.underlineStyle()); + textCharFormat.setUnderlineColor(mixinTextCharFormat.underlineColor()); + } + }; +} + +QTextCharFormat FontSettings::toTextCharFormat(const TextStyles textStyles) const +{ + auto textCharFormatIterator = m_textCharFormatCache.find(textStyles); + if (textCharFormatIterator != m_textCharFormatCache.end()) + return *textCharFormatIterator; + + QTextCharFormat textCharFormat = toTextCharFormat(textStyles.mainStyle); + + addMixinStyle(textCharFormat, textStyles.mixinStyles); + + m_textCharFormatCache.insert(textStyles, textCharFormat); + + return textCharFormat; +} + /** * Returns the list of QTextCharFormats that corresponds to the list of * requested format categories. |