diff options
author | Marco Bubke <marco.bubke@theqtcompany.com> | 2015-09-10 14:18:37 +0200 |
---|---|---|
committer | David Schulz <david.schulz@theqtcompany.com> | 2015-09-10 13:40:04 +0000 |
commit | 331e9d887825515567cf52e518b76cfcc9f49acd (patch) | |
tree | 650b02ed7045d0027604ec34cbde5dad6e8f2a03 /src/plugins/texteditor/colorscheme.cpp | |
parent | 0d20d5618387a57bcbc667e9dd792bc4bd50bc58 (diff) |
TextEdit: Add underline support to text settings
Underlines colors was hard coded to the foreground color of some settings.
With this patch you can set the underline color and the underline style
to your taste.
Change-Id: Ibc64c2d2c89bf1827a2cadaf6aee9d50d08cd1ee
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
Diffstat (limited to 'src/plugins/texteditor/colorscheme.cpp')
-rw-r--r-- | src/plugins/texteditor/colorscheme.cpp | 113 |
1 files changed, 95 insertions, 18 deletions
diff --git a/src/plugins/texteditor/colorscheme.cpp b/src/plugins/texteditor/colorscheme.cpp index d8abfddc4be..5a50d649e35 100644 --- a/src/plugins/texteditor/colorscheme.cpp +++ b/src/plugins/texteditor/colorscheme.cpp @@ -36,27 +36,20 @@ #include <QFile> #include <QCoreApplication> +#include <QMetaEnum> #include <QXmlStreamWriter> using namespace TextEditor; static const char trueString[] = "true"; +static const char falseString[] = "false"; // Format -Format::Format() : - m_foreground(Qt::black), - m_background(Qt::white), - m_bold(false), - m_italic(false) -{ -} Format::Format(const QColor &foreground, const QColor &background) : m_foreground(foreground), - m_background(background), - m_bold(false), - m_italic(false) + m_background(background) { } @@ -80,6 +73,26 @@ void Format::setItalic(bool italic) m_italic = italic; } +void Format::setUnderlineColor(const QColor &underlineColor) +{ + m_underlineColor = underlineColor; +} + +QColor Format::underlineColor() const +{ + return m_underlineColor; +} + +void Format::setUnderlineStyle(QTextCharFormat::UnderlineStyle underlineStyle) +{ + m_underlineStyle = underlineStyle; +} + +QTextCharFormat::UnderlineStyle Format::underlineStyle() const +{ + return m_underlineStyle; +} + static QColor stringToColor(const QString &string) { if (string == QLatin1String("invalid")) @@ -87,10 +100,62 @@ static QColor stringToColor(const QString &string) return QColor(string); } -bool Format::equals(const Format &f) const +static QTextCharFormat::UnderlineStyle stringToUnderlineStyle(const QString &string) +{ + if (string.isEmpty() || string == QStringLiteral("NoUnderline")) + return QTextCharFormat::NoUnderline; + else if (string == QStringLiteral("SingleUnderline")) + return QTextCharFormat::SingleUnderline; + else if (string == QStringLiteral("DashUnderline")) + return QTextCharFormat::DashUnderline; + else if (string == QStringLiteral("DotLine")) + return QTextCharFormat::DotLine; + else if (string == QStringLiteral("DashDotLine")) + return QTextCharFormat::DashDotLine; + else if (string == QStringLiteral("DashDotDotLine")) + return QTextCharFormat::DashDotDotLine; + else if (string == QStringLiteral("WaveUnderline")) + return QTextCharFormat::WaveUnderline; + + return QTextCharFormat::NoUnderline; +} + +static QString underlineStyleToString(QTextCharFormat::UnderlineStyle underlineStyle) +{ + switch (underlineStyle) { + case QTextCharFormat::NoUnderline: return QStringLiteral("NoUnderline"); + case QTextCharFormat::SingleUnderline: return QStringLiteral("SingleUnderline"); + case QTextCharFormat::DashUnderline: return QStringLiteral("DashUnderline"); + case QTextCharFormat::DotLine: return QStringLiteral("DotLine"); + case QTextCharFormat::DashDotLine: return QStringLiteral("DashDotLine"); + case QTextCharFormat::DashDotDotLine: return QStringLiteral("DashDotDotLine"); + case QTextCharFormat::WaveUnderline: return QStringLiteral("WaveUnderline"); + case QTextCharFormat::SpellCheckUnderline: return QString(); + } + + return QString(); +} + +bool Format::equals(const Format &other) const +{ + return m_foreground == other.m_foreground + && m_background == other.m_background + && m_underlineColor == other.m_underlineColor + && m_underlineStyle == other.m_underlineStyle + && m_bold == other.m_bold + && m_italic == other.m_italic; +} + +QString Format::toString() const { - return m_foreground == f.m_foreground && m_background == f.m_background && - m_bold == f.m_bold && m_italic == f.m_italic; + QStringList text({m_foreground.name(), + m_background.name(), + m_bold ? QLatin1String(trueString) : QLatin1String(falseString), + m_italic ? QLatin1String(trueString) : QLatin1String(falseString), + m_underlineColor.name(), + underlineStyleToString(m_underlineStyle)}); + + return text.join(QLatin1Char(';')); } bool Format::fromString(const QString &str) @@ -98,23 +163,22 @@ bool Format::fromString(const QString &str) *this = Format(); const QStringList lst = str.split(QLatin1Char(';')); - if (lst.count() != 4) + if (lst.count() != 4 && lst.count() != 6) return false; m_foreground = stringToColor(lst.at(0)); m_background = stringToColor(lst.at(1)); m_bold = lst.at(2) == QLatin1String(trueString); m_italic = lst.at(3) == QLatin1String(trueString); + m_underlineColor = stringToColor(lst.at(4)); + m_underlineStyle = stringToUnderlineStyle(lst.at(5)); + return true; } // ColorScheme -ColorScheme::ColorScheme() -{ -} - bool ColorScheme::contains(TextStyle category) const { return m_formats.contains(category); @@ -167,6 +231,10 @@ bool ColorScheme::save(const QString &fileName, QWidget *parent) const w.writeAttribute(QLatin1String("bold"), QLatin1String(trueString)); if (format.italic()) w.writeAttribute(QLatin1String("italic"), QLatin1String(trueString)); + if (format.underlineColor().isValid()) + w.writeAttribute(QStringLiteral("underlineColor"), format.underlineColor().name().toLower()); + if (format.underlineStyle() != QTextCharFormat::NoUnderline) + w.writeAttribute(QLatin1String("underlineStyle"), underlineStyleToString(format.underlineStyle())); w.writeEndElement(); } @@ -274,6 +342,8 @@ void ColorSchemeReader::readStyle() QString background = attr.value(QLatin1String("background")).toString(); bool bold = attr.value(QLatin1String("bold")) == QLatin1String(trueString); bool italic = attr.value(QLatin1String("italic")) == QLatin1String(trueString); + QString underlineColor = attr.value(QLatin1String("underlineColor")).toString(); + QString underlineStyle = attr.value(QLatin1String("underlineStyle")).toString(); Format format; @@ -290,6 +360,13 @@ void ColorSchemeReader::readStyle() format.setBold(bold); format.setItalic(italic); + if (QColor::isValidColor(underlineColor)) + format.setUnderlineColor(QColor(underlineColor)); + else + format.setUnderlineColor(QColor()); + + format.setUnderlineStyle(stringToUnderlineStyle(underlineStyle)); + m_scheme->setFormatFor(Constants::styleFromName(name), format); skipCurrentElement(); |