Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "syntax highlighter" to set code block background; color setting
Maybe can add real syntax highlighting later; but this is an available hook to set the block background, as a workaround for Qt's lack of a dynamic means of applying CSS to Markdown.
- Loading branch information
Showing
11 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "codeblockhighlighter.h" | ||
#include "settings.h" | ||
|
||
CodeBlockHighlighter::CodeBlockHighlighter(QTextDocument *parent) | ||
: QSyntaxHighlighter(parent) | ||
{ | ||
} | ||
|
||
void CodeBlockHighlighter::highlightBlock(const QString &text) | ||
{ | ||
Q_UNUSED(text) // TODO reuse kate syntax highlighters? | ||
QTextBlockFormat fmt = currentBlock().blockFormat(); | ||
if (fmt.hasProperty(QTextFormat::BlockCodeFence)) { | ||
fmt.setBackground(QColor(Settings::instance()->stringOrDefault( | ||
Settings::styleGroup, Settings::codeBlockBackground, "#EEE"))); | ||
QTextCursor cur(currentBlock()); | ||
cur.setBlockFormat(fmt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef CODEBLOCKHIGHLIGHTER_H | ||
#define CODEBLOCKHIGHLIGHTER_H | ||
|
||
#include <QSyntaxHighlighter> | ||
|
||
class CodeBlockHighlighter : public QSyntaxHighlighter | ||
{ | ||
Q_OBJECT | ||
public: | ||
CodeBlockHighlighter(QTextDocument *parent); | ||
|
||
protected: | ||
void highlightBlock(const QString &text) override; | ||
}; | ||
|
||
#endif // CODEBLOCKHIGHLIGHTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "colorswatch.h" | ||
|
||
#include <QColorDialog> | ||
#include <QPainter> | ||
|
||
ColorSwatch::ColorSwatch(QWidget *parent) : | ||
QWidget(parent), | ||
m_color(QPalette().window().color()) | ||
{ | ||
setFixedSize(24, 24); | ||
} | ||
|
||
void ColorSwatch::setColor(QColor color) | ||
{ | ||
if (!color.isValid()) | ||
return; | ||
m_color = color; | ||
update(); | ||
} | ||
|
||
void ColorSwatch::paintEvent(QPaintEvent *event) | ||
{ | ||
Q_UNUSED(event) | ||
QPainter painter(this); | ||
painter.fillRect(rect(), m_color); | ||
painter.setPen(QPalette().buttonText().color()); | ||
painter.drawRect(rect().adjusted(0, 0, -1, -1)); | ||
} | ||
|
||
void ColorSwatch::mouseReleaseEvent(QMouseEvent *event) | ||
{ | ||
Q_UNUSED(event) | ||
setColor(QColorDialog::getColor(m_color, parentWidget(), tr("Code block background"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef COLORSWATCH_H | ||
#define COLORSWATCH_H | ||
|
||
#include <QWidget> | ||
|
||
class ColorSwatch : public QWidget | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit ColorSwatch(QWidget *parent = nullptr); | ||
QColor color() { return m_color; } | ||
void setColor(QColor color); | ||
|
||
signals: | ||
|
||
protected: | ||
void paintEvent(QPaintEvent *event) override; | ||
void mouseReleaseEvent(QMouseEvent *event) override; | ||
|
||
private: | ||
QColor m_color; | ||
}; | ||
|
||
#endif // COLORSWATCH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters