From 08a8c0cc983da6095a8a9f2dd782e10a0977bf50 Mon Sep 17 00:00:00 2001 From: Alexandra Betouni Date: Thu, 5 Aug 2021 18:25:45 +0300 Subject: [PATCH] fix(desktop/chatinput) Adding custom syntax highligher for text formatting in chat input Closes #39 --- vendor/DOtherSide/lib/CMakeLists.txt | 2 + .../DOtherSideStatusSyntaxHighlighter.h | 50 ++++++++++++++ vendor/DOtherSide/lib/src/DOtherSide.cpp | 2 + .../src/DOtherSideStatusSyntaxHighlighter.cpp | 69 +++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h create mode 100644 vendor/DOtherSide/lib/src/DOtherSideStatusSyntaxHighlighter.cpp diff --git a/vendor/DOtherSide/lib/CMakeLists.txt b/vendor/DOtherSide/lib/CMakeLists.txt index f0e1d0cad1..c6047b5318 100644 --- a/vendor/DOtherSide/lib/CMakeLists.txt +++ b/vendor/DOtherSide/lib/CMakeLists.txt @@ -24,6 +24,7 @@ macro(add_target name type) include/DOtherSide/StatusEvents/StatusOSThemeEvent.h include/DOtherSide/DOtherSideStatusWindow.h include/DOtherSide/DOtherSideSingleInstance.h + include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h src/DOtherSide.cpp src/DosQMetaObject.cpp src/DosQDeclarative.cpp @@ -36,6 +37,7 @@ macro(add_target name type) src/StatusEvents/StatusOSThemeEvent.cpp src/DOtherSideStatusWindow.cpp src/DOtherSideSingleInstance.cpp + src/DOtherSideStatusSyntaxHighlighter.cpp ) if (APPLE) diff --git a/vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h b/vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h new file mode 100644 index 0000000000..533c966e82 --- /dev/null +++ b/vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h @@ -0,0 +1,50 @@ +#ifndef STATUSSYNTAXHIGHLIGHTER_H +#define STATUSSYNTAXHIGHLIGHTER_H + +#include +#include +#include + +class QQuickTextDocument; + +class StatusSyntaxHighlighter : public QSyntaxHighlighter +{ + Q_OBJECT + +public: + StatusSyntaxHighlighter(QTextDocument *parent = nullptr); + +protected: + void highlightBlock(const QString &text) override; + +private: + struct HighlightingRule + { + QRegularExpression pattern; + QTextCharFormat format; + }; + QVector highlightingRules; + + QTextCharFormat singlelineBoldFormat; + QTextCharFormat singleLineItalicFormat; + QTextCharFormat singlelineCodeBlockFormat; + QTextCharFormat singleLineStrikeThroughFormat; + QTextCharFormat multiLineCodeBlockFormat; +}; + +class StatusSyntaxHighlighterHelper : public QObject { + Q_OBJECT + Q_PROPERTY(QQuickTextDocument *quickTextDocument READ quickTextDocument WRITE + setQuickTextDocument NOTIFY quickTextDocumentChanged) +public: + StatusSyntaxHighlighterHelper(QObject *parent = nullptr) + : QObject(parent), m_quicktextdocument(nullptr) {} + QQuickTextDocument *quickTextDocument() const; + void setQuickTextDocument(QQuickTextDocument *quickTextDocument); +signals: + void quickTextDocumentChanged(); + +private: + QQuickTextDocument *m_quicktextdocument; +}; +#endif // STATUSSYNTAXHIGHLIGHTER_H diff --git a/vendor/DOtherSide/lib/src/DOtherSide.cpp b/vendor/DOtherSide/lib/src/DOtherSide.cpp index a68a3a49cd..ae417734f1 100644 --- a/vendor/DOtherSide/lib/src/DOtherSide.cpp +++ b/vendor/DOtherSide/lib/src/DOtherSide.cpp @@ -59,6 +59,7 @@ #include "DOtherSide/DosQQuickImageProvider.h" #include "DOtherSide/DOtherSideStatusWindow.h" #include "DOtherSide/DOtherSideSingleInstance.h" +#include "DOtherSide/DOtherSideStatusSyntaxHighlighter.h" #include "DOtherSide/StatusEvents/StatusDockShowAppEvent.h" #include "DOtherSide/StatusEvents/StatusOSThemeEvent.h" @@ -69,6 +70,7 @@ void register_meta_types() { qRegisterMetaType>(); qmlRegisterType("DotherSide", 0 , 1, "StatusWindow"); + qmlRegisterType("DotherSide", 0 , 1, "StatusSyntaxHighlighter"); } } diff --git a/vendor/DOtherSide/lib/src/DOtherSideStatusSyntaxHighlighter.cpp b/vendor/DOtherSide/lib/src/DOtherSideStatusSyntaxHighlighter.cpp new file mode 100644 index 0000000000..a3d204da15 --- /dev/null +++ b/vendor/DOtherSide/lib/src/DOtherSideStatusSyntaxHighlighter.cpp @@ -0,0 +1,69 @@ +#include "DOtherSide/DOtherSideStatusSyntaxHighlighter.h" +#include + +StatusSyntaxHighlighter::StatusSyntaxHighlighter(QTextDocument *parent) + : QSyntaxHighlighter(parent) +{ + HighlightingRule rule; + +//BOLD + singlelineBoldFormat.setFontWeight(QFont::Bold); + rule.pattern = QRegularExpression(QStringLiteral("\\*\\*(.*?)\\*\\*")); + rule.format = singlelineBoldFormat; + highlightingRules.append(rule); +//BOLD + +//ITALIC + singleLineItalicFormat.setFontItalic(true); + rule.pattern = QRegularExpression(QStringLiteral("\\*(.*?)\\*")); + rule.format = singleLineItalicFormat; + highlightingRules.append(rule); +//ITALIC + +//CODE + singlelineCodeBlockFormat.setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); + singlelineCodeBlockFormat.setFontPointSize(15); + rule.pattern = QRegularExpression(QStringLiteral("\\`(.*?)\\`")); + rule.format = singlelineCodeBlockFormat; + highlightingRules.append(rule); +//CODE + +//STRIKETHROUGH + singleLineStrikeThroughFormat.setFontStrikeOut(true); + rule.pattern = QRegularExpression(QStringLiteral("\\~+(.*?)\\~+")); + rule.format = singleLineStrikeThroughFormat; + highlightingRules.append(rule); +//STRIKETHROUGH + +//CODE BLOCK + multiLineCodeBlockFormat.setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); + multiLineCodeBlockFormat.setFontPointSize(15); + rule.pattern = QRegularExpression(QStringLiteral("\\`\\`\\`(.*?)\\`\\`\\`")); + rule.format = multiLineCodeBlockFormat; + highlightingRules.append(rule); +//CODE BLOCK +} + +void StatusSyntaxHighlighter::highlightBlock(const QString &text) +{ + for (const HighlightingRule &rule : qAsConst(highlightingRules)) { + QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); + while (matchIterator.hasNext()) { + QRegularExpressionMatch match = matchIterator.next(); + setFormat(match.capturedStart(), match.capturedLength(), rule.format); + } + } + setCurrentBlockState(0); +} + +QQuickTextDocument *StatusSyntaxHighlighterHelper::quickTextDocument() const { + return m_quicktextdocument; +} + +void StatusSyntaxHighlighterHelper::setQuickTextDocument( + QQuickTextDocument *quickTextDocument) { + m_quicktextdocument = quickTextDocument; + if (m_quicktextdocument) { + new StatusSyntaxHighlighter(m_quicktextdocument->textDocument()); + } +}