Skip to content

Commit

Permalink
Add "syntax highlighter" to set code block background; color setting
Browse files Browse the repository at this point in the history
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
ec1oud committed Apr 19, 2020
1 parent 8b9d7f4 commit ececcd6
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/codeblockhighlighter.cpp
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/codeblockhighlighter.h
@@ -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
34 changes: 34 additions & 0 deletions src/colorswatch.cpp
@@ -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")));
}
24 changes: 24 additions & 0 deletions src/colorswatch.h
@@ -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
1 change: 1 addition & 0 deletions src/mainwindow.cpp
Expand Up @@ -60,6 +60,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->setupUi(this);
m_mainWidget = ui->browser;
m_mainWidget->setDocument(m_document);
m_highlighter = new CodeBlockHighlighter(m_document);
connect(m_mainWidget, &QTextEdit::currentCharFormatChanged,
this, &MainWindow::currentCharFormatChanged);
connect(m_mainWidget, &QTextEdit::cursorPositionChanged,
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.h
Expand Up @@ -27,6 +27,7 @@
#include <QTextBrowser>
#include <QUrl>
#include "markdownbrowser.h"
#include "codeblockhighlighter.h"
#include "ipfsagent.h"
#include "linkdialog.h"
#include "thumbnailscene.h"
Expand Down Expand Up @@ -135,6 +136,7 @@ private slots:
Ui::MainWindow *ui;
MarkdownBrowser *m_mainWidget;
Document *m_document;
CodeBlockHighlighter *m_highlighter;
QJsonDocument m_jsonDocument;
ThumbnailScene *m_thumbs = nullptr;
LinkDialog *m_linkDialog = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/nb.pro
Expand Up @@ -14,6 +14,8 @@ no_kio: DEFINES += NETTEBOOK_NO_KIO

SOURCES += \
cidfinder.cpp \
codeblockhighlighter.cpp \
colorswatch.cpp \
datepickerdialog.cpp \
document.cpp \
ipfsagent.cpp \
Expand All @@ -33,6 +35,8 @@ SOURCES += \

HEADERS += \
cidfinder.h \
codeblockhighlighter.h \
colorswatch.h \
datepickerdialog.h \
document.h \
ipfsagent.h \
Expand Down
2 changes: 2 additions & 0 deletions src/settings.cpp
Expand Up @@ -28,6 +28,8 @@ const QString Settings::journalGroup(QLatin1String("Journal"));
const QString Settings::journalDirectory(QLatin1String("journalDirectory"));
const QString Settings::journalFilenameFormat(QLatin1String("journalFilenameFormat"));
const QString Settings::journalUsesTemplates(QLatin1String("journalUsesTemplates"));
const QString Settings::styleGroup(QLatin1String("Style"));
const QString Settings::codeBlockBackground(QLatin1String("codeBlockBackground"));

Settings* Settings::instance()
{
Expand Down
2 changes: 2 additions & 0 deletions src/settings.h
Expand Up @@ -43,6 +43,8 @@ class Settings : public QSettings
static const QString journalDirectory;
static const QString journalFilenameFormat;
static const QString journalUsesTemplates;
static const QString styleGroup;
static const QString codeBlockBackground;

protected:
Settings(QObject* parent = nullptr);
Expand Down
2 changes: 2 additions & 0 deletions src/settingsdialog.cpp
Expand Up @@ -15,6 +15,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
ui->journalDirectory->setText(settings->stringOrDefault(settings->journalGroup, settings->journalDirectory, QLatin1String("~/journal")));
ui->journalFilenameFormat->setText(settings->stringOrDefault(settings->journalGroup, settings->journalFilenameFormat, QLatin1String("$date-$topics.md")));
ui->useJournalTemplates->setChecked(settings->boolOrDefault(settings->journalGroup, settings->journalUsesTemplates, true));
ui->codeBackgroundColorSwatch->setColor(QColor(settings->stringOrDefault(settings->styleGroup, settings->codeBlockBackground, "#EEE")));
adjustSize();
}

Expand All @@ -31,6 +32,7 @@ void SettingsDialog::on_SettingsDialog_accepted()
settings->setString(settings->journalGroup, settings->journalDirectory, ui->journalDirectory->text());
settings->setString(settings->journalGroup, settings->journalFilenameFormat, ui->journalFilenameFormat->text());
settings->setBool(settings->journalGroup, settings->journalUsesTemplates, ui->useJournalTemplates->checkState());
settings->setString(settings->styleGroup, settings->codeBlockBackground, ui->codeBackgroundColorSwatch->color().name());
}

void SettingsDialog::on_journalDirectoryChooseButton_clicked()
Expand Down
28 changes: 28 additions & 0 deletions src/settingsdialog.ui
Expand Up @@ -20,6 +20,9 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="writingTab">
<attribute name="title">
<string>Writing</string>
Expand Down Expand Up @@ -96,6 +99,23 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="styleTab">
<attribute name="title">
<string>Style</string>
</attribute>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="codeBlockBackgroundLabel">
<property name="text">
<string>Code block background</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="ColorSwatch" name="codeBackgroundColorSwatch" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
Expand All @@ -110,6 +130,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ColorSwatch</class>
<extends>QWidget</extends>
<header>colorswatch.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="resources/resources.qrc"/>
</resources>
Expand Down

0 comments on commit ececcd6

Please sign in to comment.