Move the remaining functionality of ChatDocumentHandler to ChatTextItemHelper or split into own objects
This commit is contained in:
@@ -9,8 +9,10 @@ target_sources(LibNeoChat PRIVATE
|
||||
neochatroommember.cpp
|
||||
accountmanager.cpp
|
||||
chatbarcache.cpp
|
||||
chatdocumenthandler.cpp
|
||||
chatbarsyntaxhighlighter.cpp
|
||||
chatkeyhelper.cpp
|
||||
chatmarkdownhelper.cpp
|
||||
chattextitemhelper.cpp
|
||||
clipboard.cpp
|
||||
delegatesizehelper.cpp
|
||||
emojitones.cpp
|
||||
@@ -21,7 +23,6 @@ target_sources(LibNeoChat PRIVATE
|
||||
neochatdatetime.cpp
|
||||
nestedlisthelper_p.h
|
||||
nestedlisthelper.cpp
|
||||
qmltextitemwrapper.cpp
|
||||
roomlastmessageprovider.cpp
|
||||
spacehierarchycache.cpp
|
||||
texthandler.cpp
|
||||
@@ -92,13 +93,6 @@ ecm_qt_declare_logging_category(LibNeoChat
|
||||
DEFAULT_SEVERITY Info
|
||||
)
|
||||
|
||||
ecm_qt_declare_logging_category(LibNeoChat
|
||||
HEADER "chatdocumenthandler_logging.h"
|
||||
IDENTIFIER "ChatDocumentHandling"
|
||||
CATEGORY_NAME "org.kde.neochat.chatdocumenthandler"
|
||||
DEFAULT_SEVERITY Info
|
||||
)
|
||||
|
||||
generate_export_header(LibNeoChat BASE_NAME LibNeoChat)
|
||||
target_include_directories(LibNeoChat PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/enums ${CMAKE_CURRENT_SOURCE_DIR}/events ${CMAKE_CURRENT_SOURCE_DIR}/models)
|
||||
target_link_libraries(LibNeoChat PUBLIC
|
||||
|
||||
83
src/libneochat/chatbarsyntaxhighlighter.cpp
Normal file
83
src/libneochat/chatbarsyntaxhighlighter.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
// SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#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);
|
||||
|
||||
connect(m_checker, &Sonnet::BackgroundChecker::misspelling, this, [this](const QString &word, int start) {
|
||||
m_errors += {start, word};
|
||||
m_checker->continueChecking();
|
||||
});
|
||||
connect(m_checker, &Sonnet::BackgroundChecker::done, this, [this]() {
|
||||
m_rehighlightTimer.start();
|
||||
});
|
||||
m_rehighlightTimer.setInterval(100);
|
||||
m_rehighlightTimer.setSingleShot(true);
|
||||
m_rehighlightTimer.callOnTimeout(this, &QSyntaxHighlighter::rehighlight);
|
||||
}
|
||||
|
||||
void ChatBarSyntaxHighlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
if (m_settings.checkerEnabledByDefault()) {
|
||||
if (text != m_previousText) {
|
||||
m_previousText = text;
|
||||
m_checker->stop();
|
||||
m_errors.clear();
|
||||
m_checker->setText(text);
|
||||
}
|
||||
for (const auto &error : m_errors) {
|
||||
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());
|
||||
}
|
||||
45
src/libneochat/chatbarsyntaxhighlighter.h
Normal file
45
src/libneochat/chatbarsyntaxhighlighter.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QQuickTextDocument>
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTimer>
|
||||
|
||||
#include <Kirigami/Platform/PlatformTheme>
|
||||
|
||||
#include <Sonnet/BackgroundChecker>
|
||||
#include <Sonnet/Settings>
|
||||
|
||||
#include "chattextitemhelper.h"
|
||||
#include "neochatroom.h"
|
||||
|
||||
class ChatBarSyntaxHighlighter : public QSyntaxHighlighter
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ChatBarSyntaxHighlighter(QObject *parent = nullptr);
|
||||
|
||||
QPointer<NeoChatRoom> room;
|
||||
ChatBarType::Type type = ChatBarType::None;
|
||||
|
||||
ChatTextItemHelper *textItem() const;
|
||||
void setTextItem(ChatTextItemHelper *textItem);
|
||||
|
||||
void highlightBlock(const QString &text) override;
|
||||
|
||||
private:
|
||||
Kirigami::Platform::PlatformTheme *m_theme = nullptr;
|
||||
QTextCharFormat m_mentionFormat;
|
||||
QTextCharFormat m_errorFormat;
|
||||
|
||||
Sonnet::BackgroundChecker *m_checker = new Sonnet::BackgroundChecker;
|
||||
Sonnet::Settings m_settings;
|
||||
QString m_previousText;
|
||||
|
||||
QList<QPair<int, QString>> m_errors;
|
||||
QTimer m_rehighlightTimer;
|
||||
};
|
||||
@@ -1,654 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "chatdocumenthandler.h"
|
||||
|
||||
#include <QQmlFile>
|
||||
#include <QQmlFileSelector>
|
||||
#include <QQuickTextDocument>
|
||||
#include <QStringBuilder>
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTextBlock>
|
||||
#include <QTextBoundaryFinder>
|
||||
#include <QTextDocument>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QTextList>
|
||||
#include <QTextTable>
|
||||
#include <QTimer>
|
||||
|
||||
#include <Kirigami/Platform/PlatformTheme>
|
||||
#include <KColorScheme>
|
||||
|
||||
#include <Sonnet/BackgroundChecker>
|
||||
#include <Sonnet/Settings>
|
||||
#include <qfont.h>
|
||||
#include <qlogging.h>
|
||||
#include <qnamespace.h>
|
||||
#include <qtextcursor.h>
|
||||
#include <sched.h>
|
||||
|
||||
#include "chatbartype.h"
|
||||
#include "chatdocumenthandler_logging.h"
|
||||
#include "chatmarkdownhelper.h"
|
||||
#include "eventhandler.h"
|
||||
#include "qmltextitemwrapper.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
class SyntaxHighlighter : public QSyntaxHighlighter
|
||||
{
|
||||
public:
|
||||
QPointer<NeoChatRoom> room;
|
||||
QTextCharFormat mentionFormat;
|
||||
QTextCharFormat errorFormat;
|
||||
Sonnet::BackgroundChecker checker;
|
||||
Sonnet::Settings settings;
|
||||
QList<QPair<int, QString>> errors;
|
||||
QString previousText;
|
||||
QTimer rehighlightTimer;
|
||||
SyntaxHighlighter(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]() {
|
||||
mentionFormat.setForeground(m_theme->linkColor());
|
||||
errorFormat.setForeground(m_theme->negativeTextColor());
|
||||
});
|
||||
|
||||
mentionFormat.setFontWeight(QFont::Bold);
|
||||
mentionFormat.setForeground(m_theme->linkColor());
|
||||
|
||||
errorFormat.setForeground(m_theme->negativeTextColor());
|
||||
errorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
|
||||
|
||||
connect(&checker, &Sonnet::BackgroundChecker::misspelling, this, [this](const QString &word, int start) {
|
||||
errors += {start, word};
|
||||
checker.continueChecking();
|
||||
});
|
||||
connect(&checker, &Sonnet::BackgroundChecker::done, this, [this]() {
|
||||
rehighlightTimer.start();
|
||||
});
|
||||
rehighlightTimer.setInterval(100);
|
||||
rehighlightTimer.setSingleShot(true);
|
||||
rehighlightTimer.callOnTimeout(this, &QSyntaxHighlighter::rehighlight);
|
||||
}
|
||||
void highlightBlock(const QString &text) override
|
||||
{
|
||||
if (settings.checkerEnabledByDefault()) {
|
||||
if (text != previousText) {
|
||||
previousText = text;
|
||||
checker.stop();
|
||||
errors.clear();
|
||||
checker.setText(text);
|
||||
}
|
||||
for (const auto &error : errors) {
|
||||
setFormat(error.first, error.second.size(), errorFormat);
|
||||
}
|
||||
}
|
||||
|
||||
auto handler = dynamic_cast<ChatDocumentHandler *>(parent());
|
||||
auto room = handler->room();
|
||||
if (!room) {
|
||||
return;
|
||||
}
|
||||
if (!room) {
|
||||
return;
|
||||
}
|
||||
auto mentions = room->cacheForType(handler->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(), mentionFormat);
|
||||
}
|
||||
return false;
|
||||
}),
|
||||
mentions->end());
|
||||
}
|
||||
|
||||
private:
|
||||
Kirigami::Platform::PlatformTheme *m_theme = nullptr;
|
||||
};
|
||||
|
||||
ChatDocumentHandler::ChatDocumentHandler(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_textItem(new QmlTextItemWrapper(this))
|
||||
, m_highlighter(new SyntaxHighlighter(this))
|
||||
{
|
||||
connectTextItem();
|
||||
}
|
||||
|
||||
ChatBarType::Type ChatDocumentHandler::type() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setType(ChatBarType::Type type)
|
||||
{
|
||||
if (type == m_type) {
|
||||
return;
|
||||
}
|
||||
m_type = type;
|
||||
Q_EMIT typeChanged();
|
||||
}
|
||||
|
||||
NeoChatRoom *ChatDocumentHandler::room() const
|
||||
{
|
||||
return m_room;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setRoom(NeoChatRoom *room)
|
||||
{
|
||||
if (m_room == room) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_room = room;
|
||||
Q_EMIT roomChanged();
|
||||
}
|
||||
|
||||
QQuickItem *ChatDocumentHandler::textItem() const
|
||||
{
|
||||
return m_textItem->textItem();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setTextItem(QQuickItem *textItem)
|
||||
{
|
||||
m_textItem->setTextItem(textItem);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::connectTextItem()
|
||||
{
|
||||
Q_ASSERT(m_textItem);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, [this]() {
|
||||
m_highlighter->setDocument(m_textItem->document());
|
||||
initializeChars();
|
||||
});
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &ChatDocumentHandler::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::contentsChanged, this, &ChatDocumentHandler::contentsChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::contentsChanged, this, &ChatDocumentHandler::atFirstLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::contentsChanged, this, &ChatDocumentHandler::atLastLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::cursorPositionChanged, this, &ChatDocumentHandler::atFirstLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::cursorPositionChanged, this, &ChatDocumentHandler::atLastLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::formatChanged, this, &ChatDocumentHandler::formatChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textFormatChanged, this, &ChatDocumentHandler::textFormatChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::styleChanged, this, &ChatDocumentHandler::styleChanged);
|
||||
}
|
||||
|
||||
ChatDocumentHandler *ChatDocumentHandler::previousDocumentHandler() const
|
||||
{
|
||||
return m_previousDocumentHandler;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setPreviousDocumentHandler(ChatDocumentHandler *previousDocumentHandler)
|
||||
{
|
||||
m_previousDocumentHandler = previousDocumentHandler;
|
||||
}
|
||||
|
||||
ChatDocumentHandler *ChatDocumentHandler::nextDocumentHandler() const
|
||||
{
|
||||
return m_nextDocumentHandler;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setNextDocumentHandler(ChatDocumentHandler *nextDocumentHandler)
|
||||
{
|
||||
m_nextDocumentHandler = nextDocumentHandler;
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::fixedStartChars() const
|
||||
{
|
||||
return m_fixedStartChars;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setFixedStartChars(const QString &chars)
|
||||
{
|
||||
if (chars == m_fixedStartChars) {
|
||||
return;
|
||||
}
|
||||
m_fixedStartChars = chars;
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::fixedEndChars() const
|
||||
{
|
||||
return m_fixedEndChars;
|
||||
;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setFixedEndChars(const QString &chars)
|
||||
{
|
||||
if (chars == m_fixedEndChars) {
|
||||
return;
|
||||
}
|
||||
m_fixedEndChars = chars;
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::initialText() const
|
||||
{
|
||||
return m_initialText;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setInitialText(const QString &text)
|
||||
{
|
||||
if (text == m_initialText) {
|
||||
return;
|
||||
}
|
||||
m_initialText = text;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::initializeChars()
|
||||
{
|
||||
const auto doc = m_textItem->document();
|
||||
if (!doc) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTextCursor cursor = QTextCursor(doc);
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (doc->isEmpty() && !m_initialText.isEmpty()) {
|
||||
cursor.insertText(m_initialText);
|
||||
}
|
||||
|
||||
if (!m_fixedStartChars.isEmpty() && doc->characterAt(0) != m_fixedStartChars) {
|
||||
cursor.movePosition(QTextCursor::Start);
|
||||
cursor.insertText(m_fixedEndChars);
|
||||
}
|
||||
|
||||
if (!m_fixedStartChars.isEmpty() && doc->characterAt(doc->characterCount()) != m_fixedStartChars) {
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
cursor.insertText(m_fixedEndChars);
|
||||
}
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::isEmpty() const
|
||||
{
|
||||
return htmlText().length() == 0;
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::atFirstLine() const
|
||||
{
|
||||
const auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
return cursor.blockNumber() == 0 && cursor.block().layout()->lineForTextPosition(cursor.positionInBlock()).lineNumber() == 0;
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::atLastLine() const
|
||||
{
|
||||
const auto cursor = m_textItem->textCursor();
|
||||
const auto doc = m_textItem->document();
|
||||
if (cursor.isNull() || !doc) {
|
||||
return false;
|
||||
}
|
||||
return cursor.blockNumber() == doc->blockCount() - 1
|
||||
&& cursor.block().layout()->lineForTextPosition(cursor.positionInBlock()).lineNumber() == (cursor.block().layout()->lineCount() - 1);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setCursorFromDocumentHandler(ChatDocumentHandler *previousDocumentHandler, bool infront, int defaultPosition)
|
||||
{
|
||||
const auto doc = m_textItem->document();
|
||||
if (!doc) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_textItem->forceActiveFocus();
|
||||
|
||||
if (!previousDocumentHandler) {
|
||||
const auto docLastBlockLayout = doc->lastBlock().layout();
|
||||
m_textItem->setCursorPosition(infront ? defaultPosition : docLastBlockLayout->lineAt(docLastBlockLayout->lineCount() - 1).textStart());
|
||||
m_textItem->setCursorVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto previousLinePosition = previousDocumentHandler->cursorPositionInLine();
|
||||
const auto newMaxLineLength = lineLength(infront ? 0 : lineCount() - 1);
|
||||
m_textItem->setCursorPosition(std::min(previousLinePosition, newMaxLineLength ? *newMaxLineLength : defaultPosition)
|
||||
+ (infront ? 0 : doc->lastBlock().position()));
|
||||
m_textItem->setCursorVisible(true);
|
||||
}
|
||||
|
||||
int ChatDocumentHandler::lineCount() const
|
||||
{
|
||||
if (const auto doc = m_textItem->document()) {
|
||||
return doc->lineCount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<int> ChatDocumentHandler::lineLength(int lineNumber) const
|
||||
{
|
||||
const auto doc = m_textItem->document();
|
||||
if (!doc || lineNumber < 0 || lineNumber >= doc->lineCount()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto block = doc->findBlockByLineNumber(lineNumber);
|
||||
const auto lineNumInBlock = lineNumber - block.firstLineNumber();
|
||||
return block.layout()->lineAt(lineNumInBlock).textLength();
|
||||
}
|
||||
|
||||
int ChatDocumentHandler::cursorPositionInLine() const
|
||||
{
|
||||
const auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
return cursor.positionInBlock();
|
||||
}
|
||||
|
||||
QTextDocumentFragment ChatDocumentHandler::takeFirstBlock()
|
||||
{
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return {};
|
||||
}
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::Start);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_fixedStartChars.length());
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
if (m_textItem->document()->blockCount() <= 1) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, m_fixedEndChars.length());
|
||||
}
|
||||
|
||||
const auto block = cursor.selection();
|
||||
cursor.removeSelectedText();
|
||||
cursor.endEditBlock();
|
||||
if (m_textItem->document()->characterCount() - 1 <= (m_fixedStartChars.length() + m_fixedEndChars.length())) {
|
||||
Q_EMIT removeMe(this);
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::fillFragments(bool &hasBefore, QTextDocumentFragment &midFragment, std::optional<QTextDocumentFragment> &afterFragment)
|
||||
{
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (cursor.blockNumber() > 0) {
|
||||
hasBefore = true;
|
||||
}
|
||||
auto afterBlock = cursor.blockNumber() < m_textItem->document()->blockCount() - 1;
|
||||
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
if (!hasBefore) {
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_fixedStartChars.length());
|
||||
}
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
if (!afterBlock) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, m_fixedEndChars.length());
|
||||
}
|
||||
cursor.endEditBlock();
|
||||
|
||||
midFragment = cursor.selection();
|
||||
if (!midFragment.isEmpty()) {
|
||||
cursor.removeSelectedText();
|
||||
}
|
||||
cursor.deletePreviousChar();
|
||||
if (afterBlock) {
|
||||
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
||||
afterFragment = cursor.selection();
|
||||
cursor.removeSelectedText();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::insertFragment(const QTextDocumentFragment fragment, InsertPosition position, bool keepPosition)
|
||||
{
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int currentPosition;
|
||||
switch (position) {
|
||||
case Start:
|
||||
currentPosition = 0;
|
||||
break;
|
||||
case End:
|
||||
currentPosition = m_textItem->document()->characterCount() - 1;
|
||||
break;
|
||||
case Cursor:
|
||||
currentPosition = cursor.position();
|
||||
break;
|
||||
}
|
||||
cursor.setPosition(currentPosition);
|
||||
if (textFormat() && textFormat() == Qt::PlainText) {
|
||||
const auto wasEmpty = isEmpty();
|
||||
auto text = fragment.toPlainText();
|
||||
while (text.startsWith(u"\n"_s)) {
|
||||
text.removeFirst();
|
||||
}
|
||||
while (text.endsWith(u"\n"_s)) {
|
||||
text.removeLast();
|
||||
}
|
||||
cursor.insertText(fragment.toPlainText());
|
||||
if (wasEmpty) {
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
cursor.deletePreviousChar();
|
||||
cursor.movePosition(QTextCursor::EndOfBlock);
|
||||
cursor.deleteChar();
|
||||
}
|
||||
} else {
|
||||
cursor.insertMarkdown(trim(fragment.toMarkdown()));
|
||||
}
|
||||
if (keepPosition) {
|
||||
cursor.setPosition(currentPosition);
|
||||
}
|
||||
m_textItem->setCursorPosition(cursor.position());
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::getText() const
|
||||
{
|
||||
if (!m_textItem->document()) {
|
||||
qCWarning(ChatDocumentHandling) << "getText called with no QQuickTextDocument available.";
|
||||
return {};
|
||||
}
|
||||
return m_textItem->document()->toPlainText();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::pushMention(const Mention mention) const
|
||||
{
|
||||
if (!m_room || m_type == ChatBarType::None) {
|
||||
qCWarning(ChatDocumentHandling) << "pushMention called with no ChatBarCache available. ChatBarType: " << m_type << " Room: " << m_room;
|
||||
return;
|
||||
}
|
||||
m_room->cacheForType(m_type)->mentions()->push_back(mention);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::updateMentions(const QString &editId)
|
||||
{
|
||||
if (editId.isEmpty() || m_type == ChatBarType::None || !m_room) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto event = m_room->findInTimeline(editId); event != m_room->historyEdge()) {
|
||||
if (const auto &roomMessageEvent = &*event->viewAs<Quotient::RoomMessageEvent>()) {
|
||||
// Replaces the mentions that are baked into the HTML but plaintext in the original markdown
|
||||
const QRegularExpression re(uR"lit(<a\shref="https:\/\/matrix.to\/#\/([\S]*)"\s?>([\S]*)<\/a>)lit"_s);
|
||||
|
||||
m_room->cacheForType(m_type)->mentions()->clear();
|
||||
|
||||
int linkSize = 0;
|
||||
auto matches = re.globalMatch(EventHandler::rawMessageBody(*roomMessageEvent));
|
||||
while (matches.hasNext()) {
|
||||
const QRegularExpressionMatch match = matches.next();
|
||||
if (match.hasMatch()) {
|
||||
const QString id = match.captured(1);
|
||||
const QString name = match.captured(2);
|
||||
|
||||
const int position = match.capturedStart(0) - linkSize;
|
||||
const int end = position + name.length();
|
||||
linkSize += match.capturedLength(0) - name.length();
|
||||
|
||||
QTextCursor cursor(m_textItem->document());
|
||||
cursor.setPosition(position);
|
||||
cursor.setPosition(end, QTextCursor::KeepAnchor);
|
||||
cursor.setKeepPositionOnInsert(true);
|
||||
|
||||
pushMention(Mention{.cursor = cursor, .text = name, .start = position, .position = end, .id = id});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<Qt::TextFormat> ChatDocumentHandler::textFormat() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return static_cast<Qt::TextFormat>(m_textItem->property("textFormat").toInt());
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (!cursor.hasSelection()) {
|
||||
cursor.select(QTextCursor::WordUnderCursor);
|
||||
}
|
||||
if (cursor.hasSelection()) {
|
||||
cursor.mergeCharFormat(format);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::insertCompletion(const QString &text, const QUrl &link)
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
while (!cursor.selectedText().startsWith(u' ') && !cursor.atBlockStart()) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
||||
}
|
||||
if (cursor.selectedText().startsWith(u' ')) {
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
}
|
||||
cursor.removeSelectedText();
|
||||
|
||||
const int start = cursor.position();
|
||||
const auto insertString = u"%1 %2"_s.arg(text, link.isEmpty() ? QString() : u" "_s);
|
||||
cursor.insertText(insertString);
|
||||
cursor.setPosition(start);
|
||||
cursor.setPosition(start + text.size(), QTextCursor::KeepAnchor);
|
||||
cursor.setKeepPositionOnInsert(true);
|
||||
cursor.endEditBlock();
|
||||
if (!link.isEmpty()) {
|
||||
pushMention({
|
||||
.cursor = cursor,
|
||||
.text = text,
|
||||
.id = link.toString(),
|
||||
});
|
||||
}
|
||||
m_highlighter->rehighlight();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::tab()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.currentList()) {
|
||||
m_textItem->indentListMoreAtCursor();
|
||||
return;
|
||||
}
|
||||
cursor.insertText(u" "_s);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::deleteChar()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.position() >= m_textItem->document()->characterCount() - m_fixedEndChars.length() - 1) {
|
||||
if (const auto nextHandler = nextDocumentHandler()) {
|
||||
insertFragment(nextHandler->takeFirstBlock(), Cursor, true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
cursor.deleteChar();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::backspace()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.position() <= m_fixedStartChars.length()) {
|
||||
if (cursor.currentList()) {
|
||||
m_textItem->indentListLessAtCursor();
|
||||
return;
|
||||
}
|
||||
if (const auto previousHandler = previousDocumentHandler()) {
|
||||
previousHandler->insertFragment(takeFirstBlock(), End, true);
|
||||
} else {
|
||||
Q_EMIT unhandledBackspaceAtBeginning(this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
cursor.deletePreviousChar();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::insertReturn()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
cursor.insertBlock();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::dumpHtml()
|
||||
{
|
||||
qWarning() << htmlText();
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::htmlText() const
|
||||
{
|
||||
const auto doc = m_textItem->document();
|
||||
if (!doc) {
|
||||
return {};
|
||||
}
|
||||
return trim(doc->toMarkdown());
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::trim(QString string) const
|
||||
{
|
||||
while (string.startsWith(u"\n"_s)) {
|
||||
string.removeFirst();
|
||||
}
|
||||
while (string.endsWith(u"\n"_s)) {
|
||||
string.removeLast();
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
#include "moc_chatdocumenthandler.cpp"
|
||||
@@ -1,196 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2020 Carl Schwan <carl@carlschwan.eu>
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QTextCursor>
|
||||
#include <qnamespace.h>
|
||||
#include <qtextdocumentfragment.h>
|
||||
|
||||
#include "chatbarcache.h"
|
||||
#include "chatmarkdownhelper.h"
|
||||
#include "enums/chatbartype.h"
|
||||
#include "enums/richformat.h"
|
||||
#include "neochatroom.h"
|
||||
#include "nestedlisthelper_p.h"
|
||||
|
||||
class QTextDocument;
|
||||
|
||||
class QmlTextItemWrapper;
|
||||
class NeoChatRoom;
|
||||
class SyntaxHighlighter;
|
||||
|
||||
/**
|
||||
* @class ChatDocumentHandler
|
||||
*
|
||||
* Handle the QQuickTextDocument of a qml text item.
|
||||
*
|
||||
* The class provides functionality to highlight text in the text document as well
|
||||
* as providing completion functionality via a CompletionModel.
|
||||
*
|
||||
* The ChatDocumentHandler is also linked to a NeoChatRoom to provide functionality
|
||||
* to save the chat document text when switching between rooms.
|
||||
*
|
||||
* To get the full functionality the cursor position and text selection information
|
||||
* need to be passed in. For example:
|
||||
*
|
||||
* @code{.qml}
|
||||
* import QtQuick 2.0
|
||||
* import QtQuick.Controls 2.15 as QQC2
|
||||
*
|
||||
* import org.kde.kirigami 2.12 as Kirigami
|
||||
* import org.kde.neochat 1.0
|
||||
*
|
||||
* QQC2.TextArea {
|
||||
* id: textField
|
||||
*
|
||||
* // Set this to a NeoChatRoom object.
|
||||
* property var room
|
||||
*
|
||||
* ChatDocumentHandler {
|
||||
* id: documentHandler
|
||||
* document: textField.textDocument
|
||||
* cursorPosition: textField.cursorPosition
|
||||
* selectionStart: textField.selectionStart
|
||||
* selectionEnd: textField.selectionEnd
|
||||
* mentionColor: Kirigami.Theme.linkColor
|
||||
* errorColor: Kirigami.Theme.negativeTextColor
|
||||
* room: textField.room
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @sa QQuickTextDocument, CompletionModel, NeoChatRoom
|
||||
*/
|
||||
class ChatDocumentHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
/**
|
||||
* @brief The QQuickTextDocument that is being handled.
|
||||
*/
|
||||
Q_PROPERTY(ChatBarType::Type type READ type WRITE setType NOTIFY typeChanged)
|
||||
|
||||
/**
|
||||
* @brief The current room that the text document is being handled for.
|
||||
*/
|
||||
Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
|
||||
|
||||
/**
|
||||
* @brief The QML text Item the ChatDocumentHandler is handling.
|
||||
*/
|
||||
Q_PROPERTY(QQuickItem *textItem READ textItem WRITE setTextItem NOTIFY textItemChanged)
|
||||
|
||||
/**
|
||||
* @brief Whether the cursor is currently on the first line.
|
||||
*/
|
||||
Q_PROPERTY(bool atFirstLine READ atFirstLine NOTIFY atFirstLineChanged)
|
||||
|
||||
/**
|
||||
* @brief Whether the cursor is cuurently on the last line.
|
||||
*/
|
||||
Q_PROPERTY(bool atLastLine READ atLastLine NOTIFY atLastLineChanged)
|
||||
|
||||
public:
|
||||
enum InsertPosition {
|
||||
Cursor,
|
||||
Start,
|
||||
End,
|
||||
};
|
||||
|
||||
explicit ChatDocumentHandler(QObject *parent = nullptr);
|
||||
|
||||
ChatBarType::Type type() const;
|
||||
void setType(ChatBarType::Type type);
|
||||
|
||||
[[nodiscard]] NeoChatRoom *room() const;
|
||||
void setRoom(NeoChatRoom *room);
|
||||
|
||||
QQuickItem *textItem() const;
|
||||
void setTextItem(QQuickItem *textItem);
|
||||
|
||||
ChatDocumentHandler *previousDocumentHandler() const;
|
||||
void setPreviousDocumentHandler(ChatDocumentHandler *previousDocumentHandler);
|
||||
|
||||
ChatDocumentHandler *nextDocumentHandler() const;
|
||||
void setNextDocumentHandler(ChatDocumentHandler *nextDocumentHandler);
|
||||
|
||||
QString fixedStartChars() const;
|
||||
void setFixedStartChars(const QString &chars);
|
||||
QString fixedEndChars() const;
|
||||
void setFixedEndChars(const QString &chars);
|
||||
QString initialText() const;
|
||||
void setInitialText(const QString &text);
|
||||
|
||||
bool isEmpty() const;
|
||||
bool atFirstLine() const;
|
||||
bool atLastLine() const;
|
||||
void setCursorFromDocumentHandler(ChatDocumentHandler *previousDocumentHandler, bool infront, int defaultPosition = 0);
|
||||
int lineCount() const;
|
||||
std::optional<int> lineLength(int lineNumber) const;
|
||||
int cursorPositionInLine() const;
|
||||
QTextDocumentFragment takeFirstBlock();
|
||||
void fillFragments(bool &hasBefore, QTextDocumentFragment &midFragment, std::optional<QTextDocumentFragment> &afterFragment);
|
||||
|
||||
/**
|
||||
* @brief Update the mentions in @p document when editing a message.
|
||||
*/
|
||||
Q_INVOKABLE void updateMentions(const QString &editId);
|
||||
|
||||
Q_INVOKABLE void tab();
|
||||
Q_INVOKABLE void deleteChar();
|
||||
Q_INVOKABLE void backspace();
|
||||
Q_INVOKABLE void insertReturn();
|
||||
void insertFragment(const QTextDocumentFragment fragment, InsertPosition position = Cursor, bool keepPosition = false);
|
||||
Q_INVOKABLE void insertCompletion(const QString &text, const QUrl &link);
|
||||
|
||||
Q_INVOKABLE void dumpHtml();
|
||||
Q_INVOKABLE QString htmlText() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void typeChanged();
|
||||
void textItemChanged();
|
||||
void roomChanged();
|
||||
|
||||
void atFirstLineChanged();
|
||||
void atLastLineChanged();
|
||||
|
||||
void currentListStyleChanged();
|
||||
|
||||
void formatChanged();
|
||||
void textFormatChanged();
|
||||
void styleChanged();
|
||||
|
||||
void contentsChanged();
|
||||
|
||||
void unhandledBackspaceAtBeginning(ChatDocumentHandler *self);
|
||||
void removeMe(ChatDocumentHandler *self);
|
||||
|
||||
private:
|
||||
ChatBarType::Type m_type = ChatBarType::None;
|
||||
QPointer<NeoChatRoom> m_room;
|
||||
QPointer<QmlTextItemWrapper> m_textItem;
|
||||
void connectTextItem();
|
||||
|
||||
QPointer<ChatDocumentHandler> m_previousDocumentHandler;
|
||||
QPointer<ChatDocumentHandler> m_nextDocumentHandler;
|
||||
|
||||
QString m_fixedStartChars = {};
|
||||
QString m_fixedEndChars = {};
|
||||
QString m_initialText = {};
|
||||
void initializeChars();
|
||||
|
||||
SyntaxHighlighter *m_highlighter = nullptr;
|
||||
|
||||
QString getText() const;
|
||||
void pushMention(const Mention mention) const;
|
||||
|
||||
std::optional<Qt::TextFormat> textFormat() const;
|
||||
void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
|
||||
|
||||
QString trim(QString string) const;
|
||||
};
|
||||
122
src/libneochat/chatkeyhelper.cpp
Normal file
122
src/libneochat/chatkeyhelper.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#include "chatkeyhelper.h"
|
||||
|
||||
ChatKeyHelper::ChatKeyHelper(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
ChatTextItemHelper *ChatKeyHelper::textItem() const
|
||||
{
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void ChatKeyHelper::setTextItem(ChatTextItemHelper *textItem)
|
||||
{
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_textItem) {
|
||||
m_textItem->disconnect(this);
|
||||
}
|
||||
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, &ChatTextItemHelper::textItemChanged, this, &ChatKeyHelper::textItemChanged);
|
||||
}
|
||||
|
||||
Q_EMIT textItemChanged();
|
||||
}
|
||||
|
||||
void ChatKeyHelper::up()
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.blockNumber() == 0 && cursor.block().layout()->lineForTextPosition(cursor.positionInBlock()).lineNumber() == 0) {
|
||||
Q_EMIT unhandledUp();
|
||||
return;
|
||||
}
|
||||
cursor.movePosition(QTextCursor::Up);
|
||||
m_textItem->setCursorPosition(cursor.position());
|
||||
}
|
||||
|
||||
void ChatKeyHelper::down()
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.blockNumber() == cursor.document()->blockCount() - 1
|
||||
&& cursor.block().layout()->lineForTextPosition(cursor.positionInBlock()).lineNumber() == (cursor.block().layout()->lineCount() - 1)) {
|
||||
Q_EMIT unhandledDown();
|
||||
return;
|
||||
}
|
||||
cursor.movePosition(QTextCursor::Down);
|
||||
m_textItem->setCursorPosition(cursor.position());
|
||||
}
|
||||
|
||||
void ChatKeyHelper::tab()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.currentList()) {
|
||||
m_textItem->indentListMoreAtCursor();
|
||||
return;
|
||||
}
|
||||
cursor.insertText(u" "_s);
|
||||
}
|
||||
|
||||
void ChatKeyHelper::deleteChar()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.position() >= m_textItem->document()->characterCount() - m_textItem->fixedEndChars().length() - 1) {
|
||||
Q_EMIT unhandledDelete();
|
||||
return;
|
||||
}
|
||||
cursor.deleteChar();
|
||||
}
|
||||
|
||||
void ChatKeyHelper::backspace()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (cursor.position() <= m_textItem->fixedStartChars().length()) {
|
||||
if (cursor.currentList()) {
|
||||
m_textItem->indentListLessAtCursor();
|
||||
return;
|
||||
}
|
||||
Q_EMIT unhandledBackspace();
|
||||
return;
|
||||
}
|
||||
cursor.deletePreviousChar();
|
||||
}
|
||||
|
||||
void ChatKeyHelper::insertReturn()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
cursor.insertBlock();
|
||||
}
|
||||
|
||||
#include "moc_chatkeyhelper.cpp"
|
||||
87
src/libneochat/chatkeyhelper.h
Normal file
87
src/libneochat/chatkeyhelper.h
Normal file
@@ -0,0 +1,87 @@
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
|
||||
#include "chattextitemhelper.h"
|
||||
|
||||
class ChatKeyHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
public:
|
||||
explicit ChatKeyHelper(QObject *parent = nullptr);
|
||||
|
||||
ChatTextItemHelper *textItem() const;
|
||||
void setTextItem(ChatTextItemHelper *textItem);
|
||||
|
||||
/**
|
||||
* @brief Handle up key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void up();
|
||||
|
||||
/**
|
||||
* @brief Handle down key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void down();
|
||||
|
||||
/**
|
||||
* @brief Handle tab key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void tab();
|
||||
|
||||
/**
|
||||
* @brief Handle delete key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void deleteChar();
|
||||
|
||||
/**
|
||||
* @brief Handle backspace key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void backspace();
|
||||
|
||||
/**
|
||||
* @brief Handle return key at current cursor location.
|
||||
*/
|
||||
Q_INVOKABLE void insertReturn();
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
|
||||
/**
|
||||
* @brief There is an unhandled up key press.
|
||||
*
|
||||
* i.e. up is pressed on the first line of the first block of the text item.
|
||||
*/
|
||||
void unhandledUp();
|
||||
|
||||
/**
|
||||
* @brief There is an unhandled down key press.
|
||||
*
|
||||
* i.e. down is pressed on the last line of the last block of the text item.
|
||||
*/
|
||||
void unhandledDown();
|
||||
|
||||
/**
|
||||
* @brief There is an unhandled delete key press.
|
||||
*
|
||||
* i.e. delete is pressed at the end of the last line of the last block of the
|
||||
* text item.
|
||||
*/
|
||||
void unhandledDelete();
|
||||
|
||||
/**
|
||||
* @brief There is an unhandled backspace key press.
|
||||
*
|
||||
* i.e. backspace is pressed at the beginning of the first line of the first
|
||||
* block of the text item.
|
||||
*/
|
||||
void unhandledBackspace();
|
||||
|
||||
private:
|
||||
QPointer<ChatTextItemHelper> m_textItem;
|
||||
};
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <QTextDocument>
|
||||
#include <qtextcursor.h>
|
||||
|
||||
#include "qmltextitemwrapper.h"
|
||||
#include "chattextitemhelper.h"
|
||||
#include "richformat.h"
|
||||
|
||||
namespace
|
||||
@@ -86,12 +86,12 @@ ChatMarkdownHelper::ChatMarkdownHelper(QObject *parent)
|
||||
{
|
||||
}
|
||||
|
||||
QmlTextItemWrapper *ChatMarkdownHelper::textItem() const
|
||||
ChatTextItemHelper *ChatMarkdownHelper::textItem() const
|
||||
{
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::setTextItem(QmlTextItemWrapper *textItem)
|
||||
void ChatMarkdownHelper::setTextItem(ChatTextItemHelper *textItem)
|
||||
{
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
@@ -104,15 +104,15 @@ void ChatMarkdownHelper::setTextItem(QmlTextItemWrapper *textItem)
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &ChatMarkdownHelper::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, [this]() {
|
||||
connect(m_textItem, &ChatTextItemHelper::textItemChanged, this, &ChatMarkdownHelper::textItemChanged);
|
||||
connect(m_textItem, &ChatTextItemHelper::textItemChanged, this, [this]() {
|
||||
m_startPos = m_textItem->cursorPosition();
|
||||
m_endPos = m_startPos;
|
||||
if (m_startPos == 0) {
|
||||
m_currentState = Pre;
|
||||
}
|
||||
});
|
||||
connect(m_textItem, &QmlTextItemWrapper::contentsChange, this, &ChatMarkdownHelper::checkMarkdown);
|
||||
connect(m_textItem, &ChatTextItemHelper::contentsChange, this, &ChatMarkdownHelper::checkMarkdown);
|
||||
}
|
||||
|
||||
Q_EMIT textItemChanged();
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
class QQuickItem;
|
||||
class QTextDocument;
|
||||
|
||||
class QmlTextItemWrapper;
|
||||
class ChatTextItemHelper;
|
||||
|
||||
class ChatMarkdownHelper : public QObject
|
||||
{
|
||||
@@ -19,13 +19,20 @@ class ChatMarkdownHelper : public QObject
|
||||
public:
|
||||
explicit ChatMarkdownHelper(QObject *parent = nullptr);
|
||||
|
||||
QmlTextItemWrapper *textItem() const;
|
||||
void setTextItem(QmlTextItemWrapper *textItem);
|
||||
ChatTextItemHelper *textItem() const;
|
||||
void setTextItem(ChatTextItemHelper *textItem);
|
||||
|
||||
void handleExternalFormatChange();
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
|
||||
/**
|
||||
* @brief There is an unhandled block format request.
|
||||
*
|
||||
* i.e. the markdown for as new block (e.g. code or quote) has been typed which
|
||||
* ChatMarkdownHelper cannot resolve.
|
||||
*/
|
||||
void unhandledBlockFormat(RichFormat::Format format);
|
||||
|
||||
private:
|
||||
@@ -35,7 +42,7 @@ private:
|
||||
Started,
|
||||
};
|
||||
|
||||
QPointer<QmlTextItemWrapper> m_textItem;
|
||||
QPointer<ChatTextItemHelper> m_textItem;
|
||||
|
||||
State m_currentState = None;
|
||||
int m_startPos = 0;
|
||||
|
||||
543
src/libneochat/chattextitemhelper.cpp
Normal file
543
src/libneochat/chattextitemhelper.cpp
Normal file
@@ -0,0 +1,543 @@
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#include "chattextitemhelper.h"
|
||||
#include "richformat.h"
|
||||
|
||||
#include <QQuickTextDocument>
|
||||
#include <QTextCursor>
|
||||
#include <QTextDocumentFragment>
|
||||
|
||||
#include <Kirigami/Platform/PlatformTheme>
|
||||
|
||||
#include "chatbarsyntaxhighlighter.h"
|
||||
#include "neochatroom.h"
|
||||
|
||||
ChatTextItemHelper::ChatTextItemHelper(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_highlighter(new ChatBarSyntaxHighlighter(this))
|
||||
{
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setRoom(NeoChatRoom *room)
|
||||
{
|
||||
m_highlighter->room = room;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setType(ChatBarType::Type type)
|
||||
{
|
||||
m_highlighter->type = type;
|
||||
}
|
||||
|
||||
QQuickItem *ChatTextItemHelper::textItem() const
|
||||
{
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setTextItem(QQuickItem *textItem)
|
||||
{
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_textItem) {
|
||||
m_textItem->disconnect(this);
|
||||
if (const auto textDoc = document()) {
|
||||
textDoc->disconnect(this);
|
||||
}
|
||||
}
|
||||
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, SIGNAL(cursorPositionChanged()), this, SLOT(itemCursorPositionChanged()));
|
||||
if (const auto doc = document()) {
|
||||
connect(doc, &QTextDocument::contentsChanged, this, &ChatTextItemHelper::contentsChanged);
|
||||
connect(doc, &QTextDocument::contentsChange, this, &ChatTextItemHelper::contentsChange);
|
||||
m_highlighter->setDocument(doc);
|
||||
}
|
||||
initializeChars();
|
||||
}
|
||||
|
||||
Q_EMIT textItemChanged();
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
std::optional<Qt::TextFormat> ChatTextItemHelper::textFormat() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return static_cast<Qt::TextFormat>(m_textItem->property("textFormat").toInt());
|
||||
}
|
||||
|
||||
QString ChatTextItemHelper::fixedStartChars() const
|
||||
{
|
||||
return m_fixedStartChars;
|
||||
}
|
||||
|
||||
QString ChatTextItemHelper::fixedEndChars() const
|
||||
{
|
||||
return m_fixedEndChars;
|
||||
;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setFixedChars(const QString &startChars, const QString &endChars)
|
||||
{
|
||||
if (startChars == m_fixedStartChars && endChars == m_fixedEndChars) {
|
||||
return;
|
||||
}
|
||||
m_fixedStartChars = startChars;
|
||||
m_fixedEndChars = endChars;
|
||||
initializeChars();
|
||||
}
|
||||
|
||||
QString ChatTextItemHelper::initialText() const
|
||||
{
|
||||
return m_initialText;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setInitialText(const QString &text)
|
||||
{
|
||||
if (text == m_initialText) {
|
||||
return;
|
||||
}
|
||||
m_initialText = text;
|
||||
initializeChars();
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::initializeChars()
|
||||
{
|
||||
const auto doc = document();
|
||||
if (!doc) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTextCursor cursor = QTextCursor(doc);
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (doc->isEmpty() && !m_initialText.isEmpty()) {
|
||||
cursor.insertText(m_initialText);
|
||||
}
|
||||
|
||||
if (!m_fixedStartChars.isEmpty() && doc->characterAt(0) != m_fixedStartChars) {
|
||||
cursor.movePosition(QTextCursor::Start);
|
||||
cursor.insertText(m_fixedEndChars);
|
||||
}
|
||||
|
||||
if (!m_fixedStartChars.isEmpty() && doc->characterAt(doc->characterCount()) != m_fixedStartChars) {
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
cursor.insertText(m_fixedEndChars);
|
||||
}
|
||||
}
|
||||
|
||||
QTextDocument *ChatTextItemHelper::document() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto quickDocument = qvariant_cast<QQuickTextDocument *>(textItem()->property("textDocument"));
|
||||
return quickDocument ? quickDocument->textDocument() : nullptr;
|
||||
}
|
||||
|
||||
bool ChatTextItemHelper::isEmpty() const
|
||||
{
|
||||
return markdownText().length() == 0;
|
||||
}
|
||||
|
||||
int ChatTextItemHelper::lineCount() const
|
||||
{
|
||||
if (const auto doc = document()) {
|
||||
return doc->lineCount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<int> ChatTextItemHelper::lineLength(int lineNumber) const
|
||||
{
|
||||
const auto doc = document();
|
||||
if (!doc || lineNumber < 0 || lineNumber >= doc->lineCount()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto block = doc->findBlockByLineNumber(lineNumber);
|
||||
const auto lineNumInBlock = lineNumber - block.firstLineNumber();
|
||||
return block.layout()->lineAt(lineNumInBlock).textLength();
|
||||
}
|
||||
|
||||
QTextDocumentFragment ChatTextItemHelper::takeFirstBlock()
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return {};
|
||||
}
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::Start);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_fixedStartChars.length());
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
if (document()->blockCount() <= 1) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, m_fixedEndChars.length());
|
||||
}
|
||||
|
||||
const auto block = cursor.selection();
|
||||
cursor.removeSelectedText();
|
||||
cursor.endEditBlock();
|
||||
if (document()->characterCount() - 1 <= (m_fixedStartChars.length() + m_fixedEndChars.length())) {
|
||||
Q_EMIT cleared(this);
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::fillFragments(bool &hasBefore, QTextDocumentFragment &midFragment, std::optional<QTextDocumentFragment> &afterFragment)
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (cursor.blockNumber() > 0) {
|
||||
hasBefore = true;
|
||||
}
|
||||
auto afterBlock = cursor.blockNumber() < document()->blockCount() - 1;
|
||||
|
||||
cursor.beginEditBlock();
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
if (!hasBefore) {
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_fixedStartChars.length());
|
||||
}
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
if (!afterBlock) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, m_fixedEndChars.length());
|
||||
}
|
||||
cursor.endEditBlock();
|
||||
|
||||
midFragment = cursor.selection();
|
||||
if (!midFragment.isEmpty()) {
|
||||
cursor.removeSelectedText();
|
||||
}
|
||||
cursor.deletePreviousChar();
|
||||
if (afterBlock) {
|
||||
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
||||
afterFragment = cursor.selection();
|
||||
cursor.removeSelectedText();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::insertFragment(const QTextDocumentFragment fragment, InsertPosition position, bool keepPosition)
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int currentPosition;
|
||||
switch (position) {
|
||||
case Start:
|
||||
currentPosition = 0;
|
||||
break;
|
||||
case End:
|
||||
currentPosition = document()->characterCount() - 1;
|
||||
break;
|
||||
case Cursor:
|
||||
currentPosition = cursor.position();
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentPosition < m_fixedStartChars.length()) {
|
||||
currentPosition = m_fixedStartChars.length();
|
||||
}
|
||||
if (currentPosition >= document()->characterCount() - 1 - m_fixedEndChars.length()) {
|
||||
currentPosition = document()->characterCount() - 1 - m_fixedEndChars.length();
|
||||
}
|
||||
|
||||
cursor.setPosition(currentPosition);
|
||||
if (textFormat() && textFormat() == Qt::PlainText) {
|
||||
const auto wasEmpty = isEmpty();
|
||||
auto text = fragment.toPlainText();
|
||||
text = trim(text);
|
||||
cursor.insertText(text);
|
||||
if (wasEmpty) {
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
cursor.deletePreviousChar();
|
||||
cursor.movePosition(QTextCursor::EndOfBlock);
|
||||
cursor.deleteChar();
|
||||
}
|
||||
} else {
|
||||
cursor.insertMarkdown(trim(fragment.toMarkdown()));
|
||||
}
|
||||
if (keepPosition) {
|
||||
cursor.setPosition(currentPosition);
|
||||
}
|
||||
setCursorPosition(cursor.position());
|
||||
}
|
||||
|
||||
int ChatTextItemHelper::cursorPosition() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("cursorPosition").toInt();
|
||||
}
|
||||
|
||||
int ChatTextItemHelper::selectionStart() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("selectionStart").toInt();
|
||||
}
|
||||
|
||||
int ChatTextItemHelper::selectionEnd() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("selectionEnd").toInt();
|
||||
}
|
||||
|
||||
QTextCursor ChatTextItemHelper::textCursor() const
|
||||
{
|
||||
if (!document()) {
|
||||
return QTextCursor();
|
||||
}
|
||||
|
||||
QTextCursor cursor = QTextCursor(document());
|
||||
if (selectionStart() != selectionEnd()) {
|
||||
cursor.setPosition(selectionStart());
|
||||
cursor.setPosition(selectionEnd(), QTextCursor::KeepAnchor);
|
||||
} else {
|
||||
cursor.setPosition(cursorPosition());
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setCursorPosition(int pos)
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->setProperty("cursorPosition", pos);
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setCursorVisible(bool visible)
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->setProperty("cursorVisible", visible);
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::setCursorFromTextItem(ChatTextItemHelper *textItem, bool infront, int defaultPosition)
|
||||
{
|
||||
const auto doc = document();
|
||||
if (!doc) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_textItem->forceActiveFocus();
|
||||
|
||||
if (!textItem) {
|
||||
const auto docLastBlockLayout = doc->lastBlock().layout();
|
||||
setCursorPosition(infront ? defaultPosition : docLastBlockLayout->lineAt(docLastBlockLayout->lineCount() - 1).textStart());
|
||||
setCursorVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto previousLinePosition = textItem->textCursor().positionInBlock();
|
||||
const auto newMaxLineLength = lineLength(infront ? 0 : lineCount() - 1);
|
||||
setCursorPosition(std::min(previousLinePosition, newMaxLineLength ? *newMaxLineLength : defaultPosition) + (infront ? 0 : doc->lastBlock().position()));
|
||||
setCursorVisible(true);
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::itemCursorPositionChanged()
|
||||
{
|
||||
Q_EMIT cursorPositionChanged();
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
QList<RichFormat::Format> ChatTextItemHelper::formatsAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return RichFormat::formatsAtCursor(cursor);
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::mergeFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
switch (RichFormat::typeForFormat(format)) {
|
||||
case RichFormat::Text:
|
||||
mergeTextFormatOnCursor(format, cursor);
|
||||
return;
|
||||
case RichFormat::List:
|
||||
mergeListFormatOnCursor(format, cursor);
|
||||
return;
|
||||
case RichFormat::Block:
|
||||
if (format != RichFormat::Paragraph) {
|
||||
return;
|
||||
}
|
||||
case RichFormat::Style:
|
||||
mergeStyleFormatOnCursor(format, cursor);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::mergeTextFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
if (RichFormat::typeForFormat(format) != RichFormat::Text) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto theme = static_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(this, true));
|
||||
const auto charFormat = RichFormat::charFormatForFormat(format, RichFormat::hasFormat(cursor, format), theme->alternateBackgroundColor());
|
||||
if (!cursor.hasSelection()) {
|
||||
cursor.select(QTextCursor::WordUnderCursor);
|
||||
}
|
||||
cursor.mergeCharFormat(charFormat);
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::mergeStyleFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
// Paragraph is special because it is normally a Block format but if we're already
|
||||
// in a Paragraph it clears any existing style.
|
||||
if (!(RichFormat::typeForFormat(format) == RichFormat::Style || format == RichFormat::Paragraph)) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
cursor.mergeBlockFormat(RichFormat::blockFormatForFormat(format));
|
||||
|
||||
// Applying style to the current line or selection
|
||||
QTextCursor selectCursor = cursor;
|
||||
if (selectCursor.hasSelection()) {
|
||||
QTextCursor top = selectCursor;
|
||||
top.setPosition(qMin(top.anchor(), top.position()));
|
||||
top.movePosition(QTextCursor::StartOfBlock);
|
||||
|
||||
QTextCursor bottom = selectCursor;
|
||||
bottom.setPosition(qMax(bottom.anchor(), bottom.position()));
|
||||
bottom.movePosition(QTextCursor::EndOfBlock);
|
||||
|
||||
selectCursor.setPosition(top.position(), QTextCursor::MoveAnchor);
|
||||
selectCursor.setPosition(bottom.position(), QTextCursor::KeepAnchor);
|
||||
} else {
|
||||
selectCursor.select(QTextCursor::BlockUnderCursor);
|
||||
}
|
||||
|
||||
const auto chrfmt = RichFormat::charFormatForFormat(format);
|
||||
selectCursor.mergeCharFormat(chrfmt);
|
||||
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();
|
||||
}
|
||||
|
||||
bool ChatTextItemHelper::canIndentListMoreAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canIndent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
bool ChatTextItemHelper::canIndentListLessAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canDedent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::indentListMoreAtCursor(QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentMore(cursor);
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::indentListLessAtCursor(QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentLess(cursor);
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::forceActiveFocus() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->forceActiveFocus();
|
||||
}
|
||||
|
||||
void ChatTextItemHelper::rehighlight() const
|
||||
{
|
||||
m_highlighter->rehighlight();
|
||||
}
|
||||
|
||||
QString ChatTextItemHelper::markdownText() const
|
||||
{
|
||||
const auto doc = document();
|
||||
if (!doc) {
|
||||
return {};
|
||||
}
|
||||
return trim(doc->toMarkdown());
|
||||
}
|
||||
|
||||
QString ChatTextItemHelper::trim(QString string) const
|
||||
{
|
||||
while (string.startsWith(u"\n"_s)) {
|
||||
string.removeFirst();
|
||||
}
|
||||
while (string.endsWith(u"\n"_s)) {
|
||||
string.removeLast();
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
#include "moc_chattextitemhelper.cpp"
|
||||
@@ -5,14 +5,19 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QQuickItem>
|
||||
#include <qcontainerfwd.h>
|
||||
|
||||
#include "enums/chatbartype.h"
|
||||
#include "enums/richformat.h"
|
||||
#include "nestedlisthelper_p.h"
|
||||
|
||||
class QTextDocument;
|
||||
|
||||
class ChatBarSyntaxHighlighter;
|
||||
class NeoChatRoom;
|
||||
|
||||
/**
|
||||
* @class QmlTextItemWrapper
|
||||
* @class ChatTextItemHelper
|
||||
*
|
||||
* A class to wrap around a QQuickItem that is a QML TextEdit (or inherited from it).
|
||||
*
|
||||
@@ -21,22 +26,49 @@ class QTextDocument;
|
||||
*
|
||||
* @sa QQuickItem, TextEdit
|
||||
*/
|
||||
class QmlTextItemWrapper : public QObject
|
||||
class ChatTextItemHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
/**
|
||||
* @brief The QML text Item the ChatTextItemHelper is handling.
|
||||
*/
|
||||
Q_PROPERTY(QQuickItem *textItem READ textItem WRITE setTextItem NOTIFY textItemChanged)
|
||||
|
||||
public:
|
||||
explicit QmlTextItemWrapper(QObject *parent);
|
||||
enum InsertPosition {
|
||||
Cursor,
|
||||
Start,
|
||||
End,
|
||||
};
|
||||
|
||||
explicit ChatTextItemHelper(QObject *parent = nullptr);
|
||||
|
||||
void setRoom(NeoChatRoom *room);
|
||||
|
||||
void setType(ChatBarType::Type type);
|
||||
|
||||
QQuickItem *textItem() const;
|
||||
void setTextItem(QQuickItem *textItem);
|
||||
|
||||
QString fixedStartChars() const;
|
||||
QString fixedEndChars() const;
|
||||
void setFixedChars(const QString &startChars, const QString &endChars);
|
||||
QString initialText() const;
|
||||
void setInitialText(const QString &text);
|
||||
|
||||
QTextDocument *document() const;
|
||||
int lineCount() const;
|
||||
QTextDocumentFragment takeFirstBlock();
|
||||
void fillFragments(bool &hasBefore, QTextDocumentFragment &midFragment, std::optional<QTextDocumentFragment> &afterFragment);
|
||||
void insertFragment(const QTextDocumentFragment fragment, InsertPosition position = Cursor, bool keepPosition = false);
|
||||
|
||||
QTextCursor textCursor() const;
|
||||
int cursorPosition() const;
|
||||
void setCursorPosition(int pos);
|
||||
void setCursorVisible(bool visible);
|
||||
void setCursorFromTextItem(ChatTextItemHelper *textItem, bool infront, int defaultPosition = 0);
|
||||
|
||||
QList<RichFormat::Format> formatsAtCursor(QTextCursor cursor = {}) const;
|
||||
void mergeFormatOnCursor(RichFormat::Format format, QTextCursor cursor = {});
|
||||
@@ -48,6 +80,10 @@ public:
|
||||
|
||||
void forceActiveFocus() const;
|
||||
|
||||
void rehighlight() const;
|
||||
|
||||
QString markdownText() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
|
||||
@@ -55,6 +91,8 @@ Q_SIGNALS:
|
||||
|
||||
void contentsChanged();
|
||||
|
||||
void cleared(ChatTextItemHelper *self);
|
||||
|
||||
void cursorPositionChanged();
|
||||
|
||||
void formatChanged();
|
||||
@@ -64,6 +102,17 @@ Q_SIGNALS:
|
||||
|
||||
private:
|
||||
QPointer<QQuickItem> m_textItem;
|
||||
QPointer<ChatBarSyntaxHighlighter> m_highlighter;
|
||||
|
||||
std::optional<Qt::TextFormat> textFormat() const;
|
||||
|
||||
QString m_fixedStartChars = {};
|
||||
QString m_fixedEndChars = {};
|
||||
QString m_initialText = {};
|
||||
void initializeChars();
|
||||
|
||||
bool isEmpty() const;
|
||||
std::optional<int> lineLength(int lineNumber) const;
|
||||
|
||||
int selectionStart() const;
|
||||
int selectionEnd() const;
|
||||
@@ -73,6 +122,8 @@ private:
|
||||
void mergeListFormatOnCursor(RichFormat::Format format, const QTextCursor &cursor);
|
||||
NestedListHelper m_nestedListHelper;
|
||||
|
||||
QString trim(QString string) const;
|
||||
|
||||
private Q_SLOTS:
|
||||
void itemCursorPositionChanged();
|
||||
};
|
||||
@@ -6,35 +6,75 @@
|
||||
#include <QDebug>
|
||||
#include <QTextCursor>
|
||||
|
||||
#include "chattextitemhelper.h"
|
||||
#include "completionproxymodel.h"
|
||||
#include "models/actionsmodel.h"
|
||||
#include "models/customemojimodel.h"
|
||||
#include "models/emojimodel.h"
|
||||
#include "qmltextitemwrapper.h"
|
||||
#include "models/roomlistmodel.h"
|
||||
#include "userlistmodel.h"
|
||||
|
||||
CompletionModel::CompletionModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_textItem(new QmlTextItemWrapper(this))
|
||||
, m_textItem(new ChatTextItemHelper(this))
|
||||
, m_filterModel(new CompletionProxyModel(this))
|
||||
, m_emojiModel(new QConcatenateTablesProxyModel(this))
|
||||
{
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &CompletionModel::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::cursorPositionChanged, this, &CompletionModel::updateTextStart);
|
||||
connect(m_textItem, &QmlTextItemWrapper::contentsChanged, this, &CompletionModel::updateCompletion);
|
||||
|
||||
m_emojiModel->addSourceModel(&CustomEmojiModel::instance());
|
||||
m_emojiModel->addSourceModel(&EmojiModel::instance());
|
||||
}
|
||||
|
||||
QQuickItem *CompletionModel::textItem() const
|
||||
NeoChatRoom *CompletionModel::room() const
|
||||
{
|
||||
return m_textItem->textItem();
|
||||
return m_room;
|
||||
}
|
||||
|
||||
void CompletionModel::setTextItem(QQuickItem *textItem)
|
||||
void CompletionModel::setRoom(NeoChatRoom *room)
|
||||
{
|
||||
m_textItem->setTextItem(textItem);
|
||||
if (m_room == room) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_room = room;
|
||||
Q_EMIT roomChanged();
|
||||
}
|
||||
|
||||
ChatBarType::Type CompletionModel::type() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
void CompletionModel::setType(ChatBarType::Type type)
|
||||
{
|
||||
if (type == m_type) {
|
||||
return;
|
||||
}
|
||||
m_type = type;
|
||||
Q_EMIT typeChanged();
|
||||
}
|
||||
|
||||
ChatTextItemHelper *CompletionModel::textItem() const
|
||||
{
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void CompletionModel::setTextItem(ChatTextItemHelper *textItem)
|
||||
{
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_textItem) {
|
||||
m_textItem->disconnect(this);
|
||||
}
|
||||
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, &ChatTextItemHelper::cursorPositionChanged, this, &CompletionModel::updateTextStart);
|
||||
connect(m_textItem, &ChatTextItemHelper::contentsChanged, this, &CompletionModel::updateCompletion);
|
||||
}
|
||||
Q_EMIT textItemChanged();
|
||||
}
|
||||
|
||||
void CompletionModel::updateTextStart()
|
||||
@@ -239,4 +279,45 @@ void CompletionModel::setUserListModel(UserListModel *userListModel)
|
||||
Q_EMIT userListModelChanged();
|
||||
}
|
||||
|
||||
void CompletionModel::insertCompletion(const QString &text, const QUrl &link)
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
while (!cursor.selectedText().startsWith(u' ') && !cursor.atBlockStart()) {
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
||||
}
|
||||
if (cursor.selectedText().startsWith(u' ')) {
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
}
|
||||
cursor.removeSelectedText();
|
||||
|
||||
const int start = cursor.position();
|
||||
const auto insertString = u"%1 %2"_s.arg(text, link.isEmpty() ? QString() : u" "_s);
|
||||
cursor.insertText(insertString);
|
||||
cursor.setPosition(start);
|
||||
cursor.setPosition(start + text.size(), QTextCursor::KeepAnchor);
|
||||
cursor.setKeepPositionOnInsert(true);
|
||||
cursor.endEditBlock();
|
||||
if (!link.isEmpty()) {
|
||||
pushMention({
|
||||
.cursor = cursor,
|
||||
.text = text,
|
||||
.id = link.toString(),
|
||||
});
|
||||
}
|
||||
m_textItem->rehighlight();
|
||||
}
|
||||
|
||||
void CompletionModel::pushMention(const Mention mention) const
|
||||
{
|
||||
if (!m_room || m_type == ChatBarType::None) {
|
||||
return;
|
||||
}
|
||||
m_room->cacheForType(m_type)->mentions()->push_back(mention);
|
||||
}
|
||||
|
||||
#include "moc_completionmodel.cpp"
|
||||
|
||||
@@ -8,11 +8,13 @@
|
||||
#include <QQuickItem>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "roomlistmodel.h"
|
||||
#include "chatbarcache.h"
|
||||
#include "chattextitemhelper.h"
|
||||
#include "enums/chatbartype.h"
|
||||
#include "neochatroom.h"
|
||||
|
||||
class CompletionProxyModel;
|
||||
class UserListModel;
|
||||
class QmlTextItemWrapper;
|
||||
class RoomListModel;
|
||||
|
||||
/**
|
||||
@@ -28,10 +30,20 @@ class CompletionModel : public QAbstractListModel
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
/**
|
||||
* @brief The current room that the text document is being handled for.
|
||||
*/
|
||||
Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
|
||||
|
||||
/**
|
||||
* @brief The QQuickTextDocument that is being handled.
|
||||
*/
|
||||
Q_PROPERTY(ChatBarType::Type type READ type WRITE setType NOTIFY typeChanged)
|
||||
|
||||
/**
|
||||
* @brief The QML text Item that completions are being provided for.
|
||||
*/
|
||||
Q_PROPERTY(QQuickItem *textItem READ textItem WRITE setTextItem NOTIFY textItemChanged)
|
||||
Q_PROPERTY(ChatTextItemHelper *textItem READ textItem WRITE setTextItem NOTIFY textItemChanged)
|
||||
|
||||
/**
|
||||
* @brief The current type of completion being done on the entered text.
|
||||
@@ -77,8 +89,14 @@ public:
|
||||
|
||||
explicit CompletionModel(QObject *parent = nullptr);
|
||||
|
||||
QQuickItem *textItem() const;
|
||||
void setTextItem(QQuickItem *textItem);
|
||||
NeoChatRoom *room() const;
|
||||
void setRoom(NeoChatRoom *room);
|
||||
|
||||
ChatBarType::Type type() const;
|
||||
void setType(ChatBarType::Type type);
|
||||
|
||||
ChatTextItemHelper *textItem() const;
|
||||
void setTextItem(ChatTextItemHelper *textItem);
|
||||
|
||||
/**
|
||||
* @brief Get the given role value at the given index.
|
||||
@@ -110,16 +128,20 @@ public:
|
||||
AutoCompletionType autoCompletionType() const;
|
||||
void setAutoCompletionType(AutoCompletionType autoCompletionType);
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
Q_INVOKABLE void insertCompletion(const QString &text, const QUrl &link);
|
||||
|
||||
Q_SIGNALS:
|
||||
void roomChanged();
|
||||
void typeChanged();
|
||||
void textItemChanged();
|
||||
void autoCompletionTypeChanged();
|
||||
void roomListModelChanged();
|
||||
void userListModelChanged();
|
||||
|
||||
private:
|
||||
QPointer<QmlTextItemWrapper> m_textItem;
|
||||
QPointer<NeoChatRoom> m_room;
|
||||
ChatBarType::Type m_type = ChatBarType::None;
|
||||
QPointer<ChatTextItemHelper> m_textItem;
|
||||
|
||||
int m_textStart = 0;
|
||||
void updateTextStart();
|
||||
@@ -132,5 +154,6 @@ private:
|
||||
UserListModel *m_userListModel;
|
||||
RoomListModel *m_roomListModel;
|
||||
QConcatenateTablesProxyModel *m_emojiModel;
|
||||
|
||||
void pushMention(const Mention mention) const;
|
||||
};
|
||||
Q_DECLARE_METATYPE(CompletionModel::AutoCompletionType);
|
||||
|
||||
@@ -1,278 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2025 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#include "qmltextitemwrapper.h"
|
||||
#include "richformat.h"
|
||||
|
||||
#include <QQuickTextDocument>
|
||||
#include <QTextCursor>
|
||||
|
||||
#include <Kirigami/Platform/PlatformTheme>
|
||||
|
||||
QmlTextItemWrapper::QmlTextItemWrapper(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QQuickItem *QmlTextItemWrapper::textItem() const
|
||||
{
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::setTextItem(QQuickItem *textItem)
|
||||
{
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_textItem) {
|
||||
m_textItem->disconnect(this);
|
||||
if (const auto textDoc = document()) {
|
||||
textDoc->disconnect(this);
|
||||
}
|
||||
}
|
||||
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, SIGNAL(cursorPositionChanged()), this, SLOT(itemCursorPositionChanged()));
|
||||
if (document()) {
|
||||
connect(document(), &QTextDocument::contentsChanged, this, &QmlTextItemWrapper::contentsChanged);
|
||||
connect(document(), &QTextDocument::contentsChange, this, &QmlTextItemWrapper::contentsChange);
|
||||
}
|
||||
}
|
||||
|
||||
Q_EMIT textItemChanged();
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
QTextDocument *QmlTextItemWrapper::document() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto quickDocument = qvariant_cast<QQuickTextDocument *>(textItem()->property("textDocument"));
|
||||
return quickDocument ? quickDocument->textDocument() : nullptr;
|
||||
}
|
||||
|
||||
int QmlTextItemWrapper::cursorPosition() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("cursorPosition").toInt();
|
||||
}
|
||||
|
||||
int QmlTextItemWrapper::selectionStart() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("selectionStart").toInt();
|
||||
}
|
||||
|
||||
int QmlTextItemWrapper::selectionEnd() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return -1;
|
||||
}
|
||||
return m_textItem->property("selectionEnd").toInt();
|
||||
}
|
||||
|
||||
QTextCursor QmlTextItemWrapper::textCursor() const
|
||||
{
|
||||
if (!document()) {
|
||||
return QTextCursor();
|
||||
}
|
||||
|
||||
QTextCursor cursor = QTextCursor(document());
|
||||
if (selectionStart() != selectionEnd()) {
|
||||
cursor.setPosition(selectionStart());
|
||||
cursor.setPosition(selectionEnd(), QTextCursor::KeepAnchor);
|
||||
} else {
|
||||
cursor.setPosition(cursorPosition());
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::setCursorPosition(int pos)
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->setProperty("cursorPosition", pos);
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::setCursorVisible(bool visible)
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->setProperty("cursorVisible", visible);
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::itemCursorPositionChanged()
|
||||
{
|
||||
Q_EMIT cursorPositionChanged();
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
QList<RichFormat::Format> QmlTextItemWrapper::formatsAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return RichFormat::formatsAtCursor(cursor);
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::mergeFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
switch (RichFormat::typeForFormat(format)) {
|
||||
case RichFormat::Text:
|
||||
mergeTextFormatOnCursor(format, cursor);
|
||||
return;
|
||||
case RichFormat::List:
|
||||
mergeListFormatOnCursor(format, cursor);
|
||||
return;
|
||||
case RichFormat::Block:
|
||||
if (format != RichFormat::Paragraph) {
|
||||
return;
|
||||
}
|
||||
case RichFormat::Style:
|
||||
mergeStyleFormatOnCursor(format, cursor);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::mergeTextFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
if (RichFormat::typeForFormat(format) != RichFormat::Text) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto theme = static_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(this, true));
|
||||
const auto charFormat = RichFormat::charFormatForFormat(format, RichFormat::hasFormat(cursor, format), theme->alternateBackgroundColor());
|
||||
if (!cursor.hasSelection()) {
|
||||
cursor.select(QTextCursor::WordUnderCursor);
|
||||
}
|
||||
cursor.mergeCharFormat(charFormat);
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::mergeStyleFormatOnCursor(RichFormat::Format format, QTextCursor cursor)
|
||||
{
|
||||
// Paragraph is special because it is normally a Block format but if we're already
|
||||
// in a Paragraph it clears any existing style.
|
||||
if (!(RichFormat::typeForFormat(format) == RichFormat::Style || format == RichFormat::Paragraph)) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
cursor.mergeBlockFormat(RichFormat::blockFormatForFormat(format));
|
||||
|
||||
// Applying style to the current line or selection
|
||||
QTextCursor selectCursor = cursor;
|
||||
if (selectCursor.hasSelection()) {
|
||||
QTextCursor top = selectCursor;
|
||||
top.setPosition(qMin(top.anchor(), top.position()));
|
||||
top.movePosition(QTextCursor::StartOfBlock);
|
||||
|
||||
QTextCursor bottom = selectCursor;
|
||||
bottom.setPosition(qMax(bottom.anchor(), bottom.position()));
|
||||
bottom.movePosition(QTextCursor::EndOfBlock);
|
||||
|
||||
selectCursor.setPosition(top.position(), QTextCursor::MoveAnchor);
|
||||
selectCursor.setPosition(bottom.position(), QTextCursor::KeepAnchor);
|
||||
} else {
|
||||
selectCursor.select(QTextCursor::BlockUnderCursor);
|
||||
}
|
||||
|
||||
const auto chrfmt = RichFormat::charFormatForFormat(format);
|
||||
selectCursor.mergeCharFormat(chrfmt);
|
||||
cursor.mergeBlockCharFormat(chrfmt);
|
||||
cursor.endEditBlock();
|
||||
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::mergeListFormatOnCursor(RichFormat::Format format, const QTextCursor &cursor)
|
||||
{
|
||||
m_nestedListHelper.handleOnBulletType(RichFormat::listStyleForFormat(format), cursor);
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
bool QmlTextItemWrapper::canIndentListMoreAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canIndent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
bool QmlTextItemWrapper::canIndentListLessAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canDedent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::indentListMoreAtCursor(QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentMore(cursor);
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::indentListLessAtCursor(QTextCursor cursor)
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentLess(cursor);
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::forceActiveFocus() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
m_textItem->forceActiveFocus();
|
||||
}
|
||||
|
||||
#include "moc_qmltextitemwrapper.cpp"
|
||||
Reference in New Issue
Block a user