Disallow formatting when there is an attachment and when adding one if there is rich formatting warn the user it will be removed and remove if they accept.

This commit is contained in:
James Graham
2026-01-10 16:27:46 +00:00
parent d64e6fc206
commit f4cb660422
7 changed files with 257 additions and 44 deletions

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "chattextitemhelper.h"
#include "chatbartype.h"
#include "richformat.h"
#include <QQuickTextDocument>
@@ -13,20 +14,50 @@
#include "chatbarsyntaxhighlighter.h"
#include "neochatroom.h"
// Because we can't get access to the private header we foward declare this so the SIGNAL() macro works in setTextItem.
namespace QQuickTextEdit
{
enum TextFormat : unsigned int;
}
ChatTextItemHelper::ChatTextItemHelper(QObject *parent)
: QObject(parent)
, m_highlighter(new ChatBarSyntaxHighlighter(this))
{
}
NeoChatRoom *ChatTextItemHelper::room() const
{
if (!m_highlighter) {
return nullptr;
}
return m_highlighter->room;
}
void ChatTextItemHelper::setRoom(NeoChatRoom *room)
{
if (!m_highlighter) {
return;
}
m_highlighter->room = room;
Q_EMIT roomChanged();
}
ChatBarType::Type ChatTextItemHelper::type() const
{
if (!m_highlighter) {
return ChatBarType::None;
}
return m_highlighter->type;
}
void ChatTextItemHelper::setType(ChatBarType::Type type)
{
if (!m_highlighter) {
return;
}
m_highlighter->type = type;
Q_EMIT typeChanged();
}
QQuickItem *ChatTextItemHelper::textItem() const
@@ -51,6 +82,7 @@ void ChatTextItemHelper::setTextItem(QQuickItem *textItem)
if (m_textItem) {
connect(m_textItem, SIGNAL(cursorPositionChanged()), this, SLOT(itemCursorPositionChanged()));
connect(m_textItem, SIGNAL(textFormatChanged(QQuickTextEdit::TextFormat)), this, SLOT(itemTextFormatChanged()));
if (const auto doc = document()) {
connect(doc, &QTextDocument::contentsChanged, this, &ChatTextItemHelper::contentsChanged);
connect(doc, &QTextDocument::contentsChange, this, [this]() {
@@ -63,8 +95,8 @@ void ChatTextItemHelper::setTextItem(QQuickItem *textItem)
}
Q_EMIT textItemChanged();
Q_EMIT formatChanged();
Q_EMIT textFormatChanged();
Q_EMIT charFormatChanged();
Q_EMIT styleChanged();
Q_EMIT listChanged();
}
@@ -74,10 +106,14 @@ std::optional<Qt::TextFormat> ChatTextItemHelper::textFormat() const
if (!m_textItem) {
return std::nullopt;
}
return static_cast<Qt::TextFormat>(m_textItem->property("textFormat").toInt());
}
void ChatTextItemHelper::itemTextFormatChanged()
{
Q_EMIT textFormatChanged();
}
QString ChatTextItemHelper::fixedStartChars() const
{
return m_fixedStartChars;
@@ -393,8 +429,8 @@ void ChatTextItemHelper::itemCursorPositionChanged()
Q_EMIT cursorPositionChanged(m_contentsJustChanged);
m_contentsJustChanged = false;
Q_EMIT formatChanged();
Q_EMIT textFormatChanged();
Q_EMIT charFormatChanged();
Q_EMIT styleChanged();
Q_EMIT listChanged();
}
@@ -438,8 +474,7 @@ void ChatTextItemHelper::mergeTextFormatOnCursor(RichFormat::Format format, QTex
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat(charFormat);
Q_EMIT formatChanged();
Q_EMIT textFormatChanged();
Q_EMIT charFormatChanged();
}
void ChatTextItemHelper::mergeStyleFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
@@ -475,14 +510,12 @@ void ChatTextItemHelper::mergeStyleFormatOnCursor(RichFormat::Format format, QTe
cursor.mergeBlockCharFormat(chrfmt);
cursor.endEditBlock();
Q_EMIT formatChanged();
Q_EMIT styleChanged();
}
void ChatTextItemHelper::mergeListFormatOnCursor(RichFormat::Format format, const QTextCursor &cursor)
{
m_nestedListHelper.handleOnBulletType(RichFormat::listStyleForFormat(format), cursor);
Q_EMIT formatChanged();
Q_EMIT listChanged();
}
@@ -545,6 +578,11 @@ void ChatTextItemHelper::rehighlight() const
m_highlighter->rehighlight();
}
bool ChatTextItemHelper::hasRichFormatting() const
{
return markdownText() != plainText();
}
QString ChatTextItemHelper::markdownText() const
{
const auto doc = document();
@@ -554,6 +592,15 @@ QString ChatTextItemHelper::markdownText() const
return trim(doc->toMarkdown());
}
QString ChatTextItemHelper::plainText() const
{
const auto doc = document();
if (!doc) {
return {};
}
return trim(doc->toPlainText());
}
QString ChatTextItemHelper::trim(QString string) const
{
while (string.startsWith(u"\n"_s)) {

View File

@@ -47,6 +47,13 @@ public:
explicit ChatTextItemHelper(QObject *parent = nullptr);
/**
* @brief Get the NeoChatRoom used by the syntax highlighter.
*
* @sa NeoChatRoom
*/
NeoChatRoom *room() const;
/**
* @brief Set the NeoChatRoom required by the syntax highlighter.
*
@@ -54,6 +61,13 @@ public:
*/
void setRoom(NeoChatRoom *room);
/**
* @brief Get the ChatBarType::Type used by the syntax highlighter.
*
* @sa ChatBarType::Type
*/
ChatBarType::Type type() const;
/**
* @brief Set the ChatBarType::Type required by the syntax highlighter.
*
@@ -64,6 +78,11 @@ public:
QQuickItem *textItem() const;
void setTextItem(QQuickItem *textItem);
/**
* @brief The text format of the wrapped item.
*/
std::optional<Qt::TextFormat> textFormat() const;
/**
* @brief Whether a completion has started based on recent text entry.
*/
@@ -205,16 +224,52 @@ public:
*/
void rehighlight() const;
/**
* @brief Whether there is any rich formatting in the item text.
*/
bool hasRichFormatting() const;
/**
* @brief Output the text in the text item in markdown format.
*/
QString markdownText() const;
/**
* @brief Output the text in the text item in plain text format.
*/
QString plainText() const;
Q_SIGNALS:
/**
* @brief Emitted when the NeoChatRoom used by the syntax highlighter is changed.
*/
void roomChanged();
/**
* @brief Emitted when the ChatBarType::Type used by the syntax highlighter is changed.
*/
void typeChanged();
void textItemChanged();
void formatChanged();
/**
* @brief Emitted when the textFormat property of the underlying text item is changed.
*/
void textFormatChanged();
/**
* @brief Emitted when the char format at the current cursor is changed.
*/
void charFormatChanged();
/**
* @brief Emitted when the style at the current cursor is changed.
*/
void styleChanged();
/**
* @brief Emitted when the list format at the current cursor is changed.
*/
void listChanged();
/**
@@ -242,7 +297,6 @@ private:
QPointer<ChatBarSyntaxHighlighter> m_highlighter;
bool m_contentsJustChanged = false;
std::optional<Qt::TextFormat> textFormat() const;
QString m_fixedStartChars = {};
QString m_fixedEndChars = {};
@@ -263,5 +317,6 @@ private:
QString trim(QString string) const;
private Q_SLOTS:
void itemTextFormatChanged();
void itemCursorPositionChanged();
};