fix(desktop/chatinput) Adding custom syntax highligher for text formatting in chat input
Closes #39
This commit is contained in:
parent
f0e3f04994
commit
08a8c0cc98
|
@ -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)
|
||||
|
|
50
vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h
vendored
Normal file
50
vendor/DOtherSide/lib/include/DOtherSide/DOtherSideStatusSyntaxHighlighter.h
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef STATUSSYNTAXHIGHLIGHTER_H
|
||||
#define STATUSSYNTAXHIGHLIGHTER_H
|
||||
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTextCharFormat>
|
||||
#include <QRegularExpression>
|
||||
|
||||
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<HighlightingRule> 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
|
|
@ -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<QVector<int>>();
|
||||
qmlRegisterType<StatusWindow>("DotherSide", 0 , 1, "StatusWindow");
|
||||
qmlRegisterType<StatusSyntaxHighlighterHelper>("DotherSide", 0 , 1, "StatusSyntaxHighlighter");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
#include "DOtherSide/DOtherSideStatusSyntaxHighlighter.h"
|
||||
#include <QQuickTextDocument>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue