Markdown: Use different background color for code

Sets the background color for code blocks and for inline code (actually
for any fixed font text).

Change-Id: I1365111b38314a8e0a129aca35d754ad3f2f13bb
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Eike Ziller
2023-07-31 12:14:14 +02:00
parent 9680b8434e
commit 197d80f99b
2 changed files with 30 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
#include "filepath.h" #include "filepath.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "stylehelper.h"
#include "theme/theme.h" #include "theme/theme.h"
#include "utilstr.h" #include "utilstr.h"
@@ -18,6 +19,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonValue> #include <QJsonValue>
#include <QLocale> #include <QLocale>
#include <QPalette>
#include <QRegularExpression> #include <QRegularExpression>
#include <QSet> #include <QSet>
#include <QTextDocument> #include <QTextDocument>
@@ -573,16 +575,28 @@ QTCREATOR_UTILS_EXPORT int endOfNextWord(const QString &string, int position)
MarkdownHighlighter::MarkdownHighlighter(QTextDocument *parent) MarkdownHighlighter::MarkdownHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent) : QSyntaxHighlighter(parent)
, h2Brush(Qt::NoBrush) , h2Brush(Qt::NoBrush)
, m_codeBgBrush(Qt::NoBrush)
{ {
parent->setIndentWidth(30); // default value is 40 parent->setIndentWidth(30); // default value is 40
} }
QBrush MarkdownHighlighter::codeBgBrush()
{
if (m_codeBgBrush.style() == Qt::NoBrush) {
m_codeBgBrush = StyleHelper::mergedColors(QGuiApplication::palette().color(QPalette::Text),
QGuiApplication::palette().color(QPalette::Base),
10);
}
return m_codeBgBrush;
}
void MarkdownHighlighter::highlightBlock(const QString &text) void MarkdownHighlighter::highlightBlock(const QString &text)
{ {
if (text.isEmpty()) if (text.isEmpty())
return; return;
QTextBlockFormat fmt = currentBlock().blockFormat(); const QTextBlock block = currentBlock();
QTextBlockFormat fmt = block.blockFormat();
QTextCursor cur(currentBlock()); QTextCursor cur(currentBlock());
if (fmt.hasProperty(QTextFormat::HeadingLevel)) { if (fmt.hasProperty(QTextFormat::HeadingLevel)) {
fmt.setTopMargin(10); fmt.setTopMargin(10);
@@ -610,7 +624,8 @@ void MarkdownHighlighter::highlightBlock(const QString &text)
} }
cur.setBlockFormat(fmt); cur.setBlockFormat(fmt);
} else if (fmt.hasProperty(QTextFormat::BlockCodeLanguage) && fmt.indent() == 0) { } else if (fmt.hasProperty(QTextFormat::BlockCodeLanguage) && fmt.indent() == 0) {
// set identation for code blocks // set identation and background for code blocks
fmt.setBackground(codeBgBrush());
fmt.setIndent(1); fmt.setIndent(1);
cur.setBlockFormat(fmt); cur.setBlockFormat(fmt);
} }
@@ -624,6 +639,16 @@ void MarkdownHighlighter::highlightBlock(const QString &text)
list->setFormat(listFmt); list->setFormat(listFmt);
} }
} }
// background color of code
for (auto it = block.begin(); it != block.end(); ++it) {
const QTextFragment fragment = it.fragment();
QTextCharFormat fmt = fragment.charFormat();
if (fmt.fontFixedPitch()) {
fmt.setBackground(codeBgBrush());
setFormat(fragment.position() - block.position(), fragment.length(), fmt);
}
}
} }
} // namespace Utils } // namespace Utils

View File

@@ -130,7 +130,10 @@ public:
void highlightBlock(const QString &text); void highlightBlock(const QString &text);
private: private:
QBrush codeBgBrush();
QBrush h2Brush; QBrush h2Brush;
QBrush m_codeBgBrush;
}; };
} // namespace Utils } // namespace Utils