2023-06-10 10:15:38 -04:00
|
|
|
#ifndef RESPONSETEXT_H
|
|
|
|
#define RESPONSETEXT_H
|
|
|
|
|
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
|
|
|
|
|
|
|
class SyntaxHighlighter : public QSyntaxHighlighter {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
SyntaxHighlighter(QObject *parent);
|
|
|
|
~SyntaxHighlighter();
|
|
|
|
void highlightBlock(const QString &text) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-06-10 10:15:38 -04:00
|
|
|
class ResponseText : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QQuickTextDocument* textDocument READ textDocument WRITE setTextDocument NOTIFY textDocumentChanged())
|
|
|
|
QML_ELEMENT
|
|
|
|
public:
|
|
|
|
explicit ResponseText(QObject *parent = nullptr);
|
|
|
|
|
|
|
|
QQuickTextDocument* textDocument() const;
|
|
|
|
void setTextDocument(QQuickTextDocument* textDocument);
|
|
|
|
|
|
|
|
Q_INVOKABLE void setLinkColor(const QColor &c) { m_linkColor = c; }
|
2023-06-10 12:34:43 -04:00
|
|
|
Q_INVOKABLE void setHeaderColor(const QColor &c) { m_headerColor = c; }
|
|
|
|
|
|
|
|
Q_INVOKABLE bool tryCopyAtPosition(int position) const;
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void textDocumentChanged();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void handleTextChanged();
|
2023-06-10 12:34:43 -04:00
|
|
|
void handleCodeBlocks();
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
QQuickTextDocument *m_textDocument;
|
|
|
|
SyntaxHighlighter *m_syntaxHighlighter;
|
|
|
|
QVector<ContextLink> m_links;
|
2023-06-10 12:34:43 -04:00
|
|
|
QVector<CodeCopy> m_copies;
|
2023-06-10 10:15:38 -04:00
|
|
|
QColor m_linkColor;
|
2023-06-10 12:34:43 -04:00
|
|
|
QColor m_headerColor;
|
2023-06-10 10:15:38 -04:00
|
|
|
bool m_isProcessingText = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RESPONSETEXT_H
|