Use the rich text char format to store mentions rather than a separate structure in ChatBarCache.
This removes mentions from ChatBarCache and instead sets mentions as an anchor using the QTextCursor. Saving and restoring the chatbar text content is then done using QTextDocumentFragments which retain the rich text formatting.
This commit is contained in:
@@ -4,22 +4,16 @@
|
||||
|
||||
#include "chatbarsyntaxhighlighter.h"
|
||||
|
||||
#include "chatbarcache.h"
|
||||
#include "chattextitemhelper.h"
|
||||
#include "enums/chatbartype.h"
|
||||
|
||||
ChatBarSyntaxHighlighter::ChatBarSyntaxHighlighter(QObject *parent)
|
||||
: QSyntaxHighlighter(parent)
|
||||
{
|
||||
m_theme = static_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(this, true));
|
||||
connect(m_theme, &Kirigami::Platform::PlatformTheme::colorsChanged, this, [this]() {
|
||||
m_mentionFormat.setForeground(m_theme->linkColor());
|
||||
m_errorFormat.setForeground(m_theme->negativeTextColor());
|
||||
});
|
||||
|
||||
m_mentionFormat.setFontWeight(QFont::Bold);
|
||||
m_mentionFormat.setForeground(m_theme->linkColor());
|
||||
|
||||
m_errorFormat.setForeground(m_theme->negativeTextColor());
|
||||
m_errorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
|
||||
|
||||
@@ -48,36 +42,4 @@ void ChatBarSyntaxHighlighter::highlightBlock(const QString &text)
|
||||
setFormat(error.first, error.second.size(), m_errorFormat);
|
||||
}
|
||||
}
|
||||
|
||||
if (!room || type == ChatBarType::None) {
|
||||
return;
|
||||
}
|
||||
auto mentions = room->cacheForType(type)->mentions();
|
||||
mentions->erase(std::remove_if(mentions->begin(),
|
||||
mentions->end(),
|
||||
[this](auto &mention) {
|
||||
if (document()->toPlainText().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mention.cursor.position() == 0 && mention.cursor.anchor() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mention.cursor.position() - mention.cursor.anchor() != mention.text.size()) {
|
||||
mention.cursor.setPosition(mention.start);
|
||||
mention.cursor.setPosition(mention.cursor.anchor() + mention.text.size(), QTextCursor::KeepAnchor);
|
||||
}
|
||||
|
||||
if (mention.cursor.selectedText() != mention.text) {
|
||||
return true;
|
||||
}
|
||||
if (currentBlock() == mention.cursor.block()) {
|
||||
mention.start = mention.cursor.anchor();
|
||||
mention.position = mention.cursor.position();
|
||||
setFormat(mention.cursor.selectionStart(), mention.cursor.selectedText().size(), m_mentionFormat);
|
||||
}
|
||||
return false;
|
||||
}),
|
||||
mentions->end());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user