forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -139,8 +139,9 @@ bool FontSettings::equals(const FontSettings &f) const
|
|||||||
*/
|
*/
|
||||||
QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const
|
QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const
|
||||||
{
|
{
|
||||||
if (m_formatCache.contains(category))
|
auto textCharFormatIterator = m_formatCache.find(category);
|
||||||
return m_formatCache.value(category);
|
if (textCharFormatIterator != m_formatCache.end())
|
||||||
|
return *textCharFormatIterator;
|
||||||
|
|
||||||
const Format &f = m_scheme.formatFor(category);
|
const Format &f = m_scheme.formatFor(category);
|
||||||
QTextCharFormat tf;
|
QTextCharFormat tf;
|
||||||
@@ -174,6 +175,60 @@ QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const
|
|||||||
return tf;
|
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
|
* Returns the list of QTextCharFormats that corresponds to the list of
|
||||||
* requested format categories.
|
* requested format categories.
|
||||||
|
|||||||
@@ -30,6 +30,8 @@
|
|||||||
|
|
||||||
#include "colorscheme.h"
|
#include "colorscheme.h"
|
||||||
|
|
||||||
|
#include <utils/sizedarray.h>
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
@@ -45,6 +47,13 @@ namespace TextEditor {
|
|||||||
|
|
||||||
class FormatDescription;
|
class FormatDescription;
|
||||||
|
|
||||||
|
using MixinTextStyles = Utils::SizedArray<TextStyle, 6>;
|
||||||
|
|
||||||
|
struct TextStyles {
|
||||||
|
TextStyle mainStyle;
|
||||||
|
MixinTextStyles mixinStyles;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Font settings (default font and enumerated list of formats).
|
* Font settings (default font and enumerated list of formats).
|
||||||
*/
|
*/
|
||||||
@@ -66,6 +75,7 @@ public:
|
|||||||
|
|
||||||
QVector<QTextCharFormat> toTextCharFormats(const QVector<TextStyle> &categories) const;
|
QVector<QTextCharFormat> toTextCharFormats(const QVector<TextStyle> &categories) const;
|
||||||
QTextCharFormat toTextCharFormat(TextStyle category) const;
|
QTextCharFormat toTextCharFormat(TextStyle category) const;
|
||||||
|
QTextCharFormat toTextCharFormat(const TextStyles textStyles) const;
|
||||||
|
|
||||||
QString family() const;
|
QString family() const;
|
||||||
void setFamily(const QString &family);
|
void setFamily(const QString &family);
|
||||||
@@ -99,6 +109,9 @@ public:
|
|||||||
|
|
||||||
static QString defaultSchemeFileName(const QString &fileName = QString());
|
static QString defaultSchemeFileName(const QString &fileName = QString());
|
||||||
|
|
||||||
|
private:
|
||||||
|
void addMixinStyle(QTextCharFormat &textCharFormat, const MixinTextStyles &mixinStyles) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_family;
|
QString m_family;
|
||||||
QString m_schemeFileName;
|
QString m_schemeFileName;
|
||||||
@@ -107,6 +120,7 @@ private:
|
|||||||
bool m_antialias;
|
bool m_antialias;
|
||||||
ColorScheme m_scheme;
|
ColorScheme m_scheme;
|
||||||
mutable QHash<TextStyle, QTextCharFormat> m_formatCache;
|
mutable QHash<TextStyle, QTextCharFormat> m_formatCache;
|
||||||
|
mutable QHash<TextStyles, QTextCharFormat> m_textCharFormatCache;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator==(const FontSettings &f1, const FontSettings &f2) { return f1.equals(f2); }
|
inline bool operator==(const FontSettings &f1, const FontSettings &f2) { return f1.equals(f2); }
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
namespace TextEditor {
|
namespace TextEditor {
|
||||||
|
|
||||||
// Text color and style categories
|
// Text color and style categories
|
||||||
enum TextStyle {
|
enum TextStyle : quint8 {
|
||||||
C_TEXT,
|
C_TEXT,
|
||||||
|
|
||||||
C_LINK,
|
C_LINK,
|
||||||
|
|||||||
Reference in New Issue
Block a user