mirror of
https://github.com/status-im/status-desktop.git
synced 2025-02-20 02:28:56 +00:00
StatusSyntaxHighlighter added
This commit is contained in:
parent
922a38108d
commit
a9f179cab3
@ -2,6 +2,7 @@
|
||||
|
||||
#include "DI.h"
|
||||
#include "../Core/Engine.h"
|
||||
#include "../Core/StatusSyntaxHighlighter.h"
|
||||
#include "../Common/Utils.h"
|
||||
#include "../Global/LocalAppSettings.h"
|
||||
#include "../Global/LocalAccountSettings.h"
|
||||
@ -53,6 +54,7 @@ int AppController::exec(int& argc, char** argv)
|
||||
// Once we fully move to c++ we should include the following line instead the line below it (it's here just to align with the current qml files).
|
||||
// qmlRegisterType<AppWindow>("AppWindow", 0 , 1, "AppWindow");
|
||||
qmlRegisterType<AppWindow>("DotherSide", 0 , 1, "StatusWindow");
|
||||
qmlRegisterType<StatusSyntaxHighlighterHelper>("DotherSide", 0, 1, "StatusSyntaxHighlighter");
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -0,0 +1,72 @@
|
||||
#include "StatusSyntaxHighlighter.h"
|
||||
|
||||
using namespace Status;
|
||||
|
||||
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.setFontFamily("Roboto Mono");
|
||||
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.setFontFamily("Roboto Mono");
|
||||
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());
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore>
|
||||
#include <QtGui>
|
||||
#include <QtQuick/QQuickTextDocument>
|
||||
|
||||
namespace Status {
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
@ -1041,9 +1041,9 @@ Rectangle {
|
||||
lastClick = now
|
||||
}
|
||||
|
||||
// StatusSyntaxHighlighter {
|
||||
// quickTextDocument: messageInputField.textDocument
|
||||
// }
|
||||
StatusSyntaxHighlighter {
|
||||
quickTextDocument: messageInputField.textDocument
|
||||
}
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
acceptedButtons: Qt.NoButton
|
||||
|
Loading…
x
Reference in New Issue
Block a user