2024-06-28 11:10:20 -04:00
|
|
|
#ifndef CHATVIEWTEXTPROCESSOR_H
|
|
|
|
#define CHATVIEWTEXTPROCESSOR_H
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QColor>
|
2023-06-10 10:15:38 -04:00
|
|
|
#include <QObject>
|
|
|
|
#include <QQmlEngine>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QQuickTextDocument> // IWYU pragma: keep
|
|
|
|
#include <QRectF>
|
|
|
|
#include <QSizeF>
|
|
|
|
#include <QString>
|
2023-06-10 10:15:38 -04:00
|
|
|
#include <QSyntaxHighlighter>
|
2023-12-21 13:59:33 -05:00
|
|
|
#include <QTextObjectInterface>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QVector>
|
|
|
|
|
|
|
|
class QPainter;
|
|
|
|
class QTextDocument;
|
|
|
|
class QTextFormat;
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2024-06-30 19:15:01 -04:00
|
|
|
struct CodeColors {
|
|
|
|
Q_GADGET
|
|
|
|
Q_PROPERTY(QColor defaultColor MEMBER defaultColor)
|
|
|
|
Q_PROPERTY(QColor keywordColor MEMBER keywordColor)
|
|
|
|
Q_PROPERTY(QColor functionColor MEMBER functionColor)
|
|
|
|
Q_PROPERTY(QColor functionCallColor MEMBER functionCallColor)
|
|
|
|
Q_PROPERTY(QColor commentColor MEMBER commentColor)
|
|
|
|
Q_PROPERTY(QColor stringColor MEMBER stringColor)
|
|
|
|
Q_PROPERTY(QColor numberColor MEMBER numberColor)
|
|
|
|
Q_PROPERTY(QColor headerColor MEMBER headerColor)
|
|
|
|
Q_PROPERTY(QColor backgroundColor MEMBER backgroundColor)
|
|
|
|
|
|
|
|
public:
|
|
|
|
QColor defaultColor;
|
|
|
|
QColor keywordColor;
|
|
|
|
QColor functionColor;
|
|
|
|
QColor functionCallColor;
|
|
|
|
QColor commentColor;
|
|
|
|
QColor stringColor;
|
|
|
|
QColor numberColor;
|
|
|
|
QColor headerColor;
|
|
|
|
QColor backgroundColor;
|
|
|
|
|
|
|
|
QColor preprocessorColor = keywordColor;
|
|
|
|
QColor typeColor = numberColor;
|
|
|
|
QColor arrowColor = functionColor;
|
|
|
|
QColor commandColor = functionCallColor;
|
|
|
|
QColor variableColor = numberColor;
|
|
|
|
QColor keyColor = functionColor;
|
|
|
|
QColor valueColor = stringColor;
|
|
|
|
QColor parameterColor = stringColor;
|
|
|
|
QColor attributeNameColor = numberColor;
|
|
|
|
QColor attributeValueColor = stringColor;
|
|
|
|
QColor specialCharacterColor = functionColor;
|
|
|
|
QColor doctypeColor = commentColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(CodeColors)
|
|
|
|
|
2023-06-10 10:15:38 -04:00
|
|
|
class SyntaxHighlighter : public QSyntaxHighlighter {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
SyntaxHighlighter(QObject *parent);
|
|
|
|
~SyntaxHighlighter();
|
|
|
|
void highlightBlock(const QString &text) override;
|
2024-06-30 19:15:01 -04:00
|
|
|
|
|
|
|
CodeColors codeColors() const { return m_codeColors; }
|
|
|
|
void setCodeColors(const CodeColors &colors) { m_codeColors = colors; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CodeColors m_codeColors;
|
2023-06-10 10:15:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ContextLink {
|
|
|
|
int startPos = -1;
|
|
|
|
int endPos = -1;
|
|
|
|
QString text;
|
|
|
|
QString href;
|
|
|
|
};
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
struct CodeCopy {
|
|
|
|
int startPos = -1;
|
|
|
|
int endPos = -1;
|
|
|
|
QString text;
|
|
|
|
};
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
class ChatViewTextProcessor : public QObject
|
2023-06-10 10:15:38 -04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QQuickTextDocument* textDocument READ textDocument WRITE setTextDocument NOTIFY textDocumentChanged())
|
2024-06-28 11:10:20 -04:00
|
|
|
Q_PROPERTY(bool shouldProcessText READ shouldProcessText WRITE setShouldProcessText NOTIFY shouldProcessTextChanged())
|
2024-06-30 15:10:19 -04:00
|
|
|
Q_PROPERTY(qreal fontPixelSize READ fontPixelSize WRITE setFontPixelSize NOTIFY fontPixelSizeChanged())
|
2024-06-30 19:15:01 -04:00
|
|
|
Q_PROPERTY(CodeColors codeColors READ codeColors WRITE setCodeColors NOTIFY codeColorsChanged())
|
2023-06-10 10:15:38 -04:00
|
|
|
QML_ELEMENT
|
|
|
|
public:
|
2024-06-28 11:10:20 -04:00
|
|
|
explicit ChatViewTextProcessor(QObject *parent = nullptr);
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
QQuickTextDocument* textDocument() const;
|
|
|
|
void setTextDocument(QQuickTextDocument* textDocument);
|
|
|
|
|
2024-07-08 17:24:02 -04:00
|
|
|
Q_INVOKABLE void setValue(const QString &value);
|
2023-06-10 12:34:43 -04:00
|
|
|
Q_INVOKABLE bool tryCopyAtPosition(int position) const;
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
bool shouldProcessText() const;
|
|
|
|
void setShouldProcessText(bool b);
|
|
|
|
|
2024-06-30 15:10:19 -04:00
|
|
|
qreal fontPixelSize() const;
|
|
|
|
void setFontPixelSize(qreal b);
|
|
|
|
|
2024-06-30 19:15:01 -04:00
|
|
|
CodeColors codeColors() const;
|
|
|
|
void setCodeColors(const CodeColors &colors);
|
|
|
|
|
2023-06-10 10:15:38 -04:00
|
|
|
Q_SIGNALS:
|
|
|
|
void textDocumentChanged();
|
2024-06-28 11:10:20 -04:00
|
|
|
void shouldProcessTextChanged();
|
2024-06-30 15:10:19 -04:00
|
|
|
void fontPixelSizeChanged();
|
2024-06-30 19:15:01 -04:00
|
|
|
void codeColorsChanged();
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void handleTextChanged();
|
2023-06-10 12:34:43 -04:00
|
|
|
void handleCodeBlocks();
|
2024-06-28 11:10:20 -04:00
|
|
|
void handleMarkdown();
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
private:
|
2024-07-08 17:24:02 -04:00
|
|
|
QQuickTextDocument *m_quickTextDocument;
|
2023-06-10 10:15:38 -04:00
|
|
|
SyntaxHighlighter *m_syntaxHighlighter;
|
|
|
|
QVector<ContextLink> m_links;
|
2023-06-10 12:34:43 -04:00
|
|
|
QVector<CodeCopy> m_copies;
|
2024-06-28 11:10:20 -04:00
|
|
|
bool m_shouldProcessText = false;
|
2024-06-30 15:10:19 -04:00
|
|
|
qreal m_fontPixelSize;
|
2023-06-10 10:15:38 -04:00
|
|
|
};
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
#endif // CHATVIEWTEXTPROCESSOR_H
|