Separate ChatButtonHelper from ChatDocumentHandler
This commit is contained in:
@@ -112,7 +112,6 @@ public:
|
||||
mention.cursor.setPosition(mention.cursor.anchor() + mention.text.size(), QTextCursor::KeepAnchor);
|
||||
}
|
||||
|
||||
qWarning() << mention.cursor.selectedText() << mention.text;
|
||||
if (mention.cursor.selectedText() != mention.text) {
|
||||
return true;
|
||||
}
|
||||
@@ -133,12 +132,9 @@ private:
|
||||
ChatDocumentHandler::ChatDocumentHandler(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_textItem(new QmlTextItemWrapper(this))
|
||||
, m_markdownHelper(new ChatMarkdownHelper(this))
|
||||
, m_highlighter(new SyntaxHighlighter(this))
|
||||
{
|
||||
connectTextItem();
|
||||
|
||||
connect(this, &ChatDocumentHandler::formatChanged, m_markdownHelper, &ChatMarkdownHelper::handleExternalFormatChange);
|
||||
}
|
||||
|
||||
ChatBarType::Type ChatDocumentHandler::type() const
|
||||
@@ -178,7 +174,6 @@ QQuickItem *ChatDocumentHandler::textItem() const
|
||||
void ChatDocumentHandler::setTextItem(QQuickItem *textItem)
|
||||
{
|
||||
m_textItem->setTextItem(textItem);
|
||||
m_markdownHelper->setTextItem(textItem);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::connectTextItem()
|
||||
@@ -189,33 +184,14 @@ void ChatDocumentHandler::connectTextItem()
|
||||
initializeChars();
|
||||
});
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &ChatDocumentHandler::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChanged, this, &ChatDocumentHandler::contentsChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChanged, this, &ChatDocumentHandler::atFirstLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChanged, this, &ChatDocumentHandler::atLastLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentCursorPositionChanged, this, &ChatDocumentHandler::atFirstLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentCursorPositionChanged, this, &ChatDocumentHandler::atLastLineChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChange, this, [this](int position) {
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
cursor.setPosition(position);
|
||||
cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
|
||||
if (!cursor.selectedText().isEmpty()) {
|
||||
if (m_pendingFormat) {
|
||||
cursor.mergeCharFormat(*m_pendingFormat);
|
||||
m_pendingFormat = std::nullopt;
|
||||
}
|
||||
if (m_pendingOverrideFormat) {
|
||||
cursor.setCharFormat(*m_pendingOverrideFormat);
|
||||
m_pendingOverrideFormat = std::nullopt;
|
||||
}
|
||||
}
|
||||
});
|
||||
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);
|
||||
connect(m_textItem, &QmlTextItemWrapper::listChanged, this, &ChatDocumentHandler::listChanged);
|
||||
}
|
||||
|
||||
ChatDocumentHandler *ChatDocumentHandler::previousDocumentHandler() const
|
||||
@@ -539,56 +515,6 @@ void ChatDocumentHandler::updateMentions(const QString &editId)
|
||||
}
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setTextColor(const QColor &color)
|
||||
{
|
||||
QTextCharFormat format;
|
||||
format.setForeground(QBrush(color));
|
||||
mergeFormatOnWordOrSelection(format);
|
||||
Q_EMIT textColorChanged();
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::bold() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
return cursor.charFormat().fontWeight() == QFont::Bold;
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::italic() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull())
|
||||
return false;
|
||||
return cursor.charFormat().fontItalic();
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::underline() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull())
|
||||
return false;
|
||||
return cursor.charFormat().fontUnderline();
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::strikethrough() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull())
|
||||
return false;
|
||||
return cursor.charFormat().fontStrikeOut();
|
||||
}
|
||||
|
||||
QColor ChatDocumentHandler::textColor() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull())
|
||||
return QColor(Qt::black);
|
||||
QTextCharFormat format = cursor.charFormat();
|
||||
return format.foreground().color();
|
||||
}
|
||||
|
||||
std::optional<Qt::TextFormat> ChatDocumentHandler::textFormat() const
|
||||
{
|
||||
if (!m_textItem) {
|
||||
@@ -606,57 +532,6 @@ void ChatDocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &fo
|
||||
}
|
||||
if (cursor.hasSelection()) {
|
||||
cursor.mergeCharFormat(format);
|
||||
} else {
|
||||
m_pendingFormat = format.toCharFormat();
|
||||
}
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::currentLinkText() const
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
selectLinkText(&cursor);
|
||||
return cursor.selectedText();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::selectLinkText(QTextCursor *cursor) const
|
||||
{
|
||||
// If the cursor is on a link, select the text of the link.
|
||||
if (cursor->charFormat().isAnchor()) {
|
||||
const QString aHref = cursor->charFormat().anchorHref();
|
||||
|
||||
// Move cursor to start of link
|
||||
while (cursor->charFormat().anchorHref() == aHref) {
|
||||
if (cursor->atStart()) {
|
||||
break;
|
||||
}
|
||||
cursor->setPosition(cursor->position() - 1);
|
||||
}
|
||||
if (cursor->charFormat().anchorHref() != aHref) {
|
||||
cursor->setPosition(cursor->position() + 1, QTextCursor::KeepAnchor);
|
||||
}
|
||||
|
||||
// Move selection to the end of the link
|
||||
while (cursor->charFormat().anchorHref() == aHref) {
|
||||
if (cursor->atEnd()) {
|
||||
break;
|
||||
}
|
||||
const int oldPosition = cursor->position();
|
||||
cursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
// Wordaround Qt Bug. when we have a table.
|
||||
// FIXME selection url
|
||||
if (oldPosition == cursor->position()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cursor->charFormat().anchorHref() != aHref) {
|
||||
cursor->setPosition(cursor->position() - 1, QTextCursor::KeepAnchor);
|
||||
}
|
||||
} else if (cursor->hasSelection()) {
|
||||
// Nothing to do. Using the currently selected text as the link text.
|
||||
} else {
|
||||
// Select current word
|
||||
cursor->movePosition(QTextCursor::StartOfWord);
|
||||
cursor->movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -693,113 +568,6 @@ void ChatDocumentHandler::insertCompletion(const QString &text, const QUrl &link
|
||||
m_highlighter->rehighlight();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::updateLink(const QString &linkUrl, const QString &linkText)
|
||||
{
|
||||
auto cursor = m_textItem->textCursor();
|
||||
selectLinkText(&cursor);
|
||||
|
||||
cursor.beginEditBlock();
|
||||
|
||||
if (!cursor.hasSelection()) {
|
||||
cursor.select(QTextCursor::WordUnderCursor);
|
||||
}
|
||||
|
||||
const auto originalFormat = cursor.charFormat();
|
||||
auto format = cursor.charFormat();
|
||||
// Save original format to create an extra space with the existing char
|
||||
// format for the block
|
||||
if (!linkUrl.isEmpty()) {
|
||||
// Add link details
|
||||
format.setAnchor(true);
|
||||
format.setAnchorHref(linkUrl);
|
||||
// Workaround for QTBUG-1814:
|
||||
// Link formatting does not get applied immediately when setAnchor(true)
|
||||
// is called. So the formatting needs to be applied manually.
|
||||
format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
|
||||
format.setUnderlineColor(linkColor());
|
||||
format.setForeground(linkColor());
|
||||
} else {
|
||||
// Remove link details
|
||||
format.setAnchor(false);
|
||||
format.setAnchorHref(QString());
|
||||
// Workaround for QTBUG-1814:
|
||||
// Link formatting does not get removed immediately when setAnchor(false)
|
||||
// is called. So the formatting needs to be applied manually.
|
||||
QTextDocument defaultTextDocument;
|
||||
QTextCharFormat defaultCharFormat = defaultTextDocument.begin().charFormat();
|
||||
|
||||
format.setUnderlineStyle(defaultCharFormat.underlineStyle());
|
||||
format.setUnderlineColor(defaultCharFormat.underlineColor());
|
||||
format.setForeground(defaultCharFormat.foreground());
|
||||
}
|
||||
|
||||
// Insert link text specified in dialog, otherwise write out url.
|
||||
QString _linkText;
|
||||
if (!linkText.isEmpty()) {
|
||||
_linkText = linkText;
|
||||
} else {
|
||||
_linkText = linkUrl;
|
||||
}
|
||||
cursor.insertText(_linkText, format);
|
||||
cursor.endEditBlock();
|
||||
|
||||
m_pendingOverrideFormat = originalFormat;
|
||||
}
|
||||
|
||||
QColor ChatDocumentHandler::linkColor()
|
||||
{
|
||||
if (mLinkColor.isValid()) {
|
||||
return mLinkColor;
|
||||
}
|
||||
regenerateColorScheme();
|
||||
return mLinkColor;
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::regenerateColorScheme()
|
||||
{
|
||||
mLinkColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground(KColorScheme::LinkText).color();
|
||||
// TODO update existing link
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::setFormat(RichFormat::Format format)
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
m_textItem->mergeFormatOnCursor(format, cursor);
|
||||
}
|
||||
|
||||
int ChatDocumentHandler::currentListStyle() const
|
||||
{
|
||||
return m_textItem->currentListStyle();
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::canIndentListMore() const
|
||||
{
|
||||
return m_textItem->canIndentListMore();
|
||||
}
|
||||
|
||||
bool ChatDocumentHandler::canIndentListLess() const
|
||||
{
|
||||
return m_textItem->canIndentListLess();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::indentListMore()
|
||||
{
|
||||
m_textItem->indentListMore();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::indentListLess()
|
||||
{
|
||||
m_textItem->indentListLess();
|
||||
}
|
||||
|
||||
RichFormat::Format ChatDocumentHandler::style() const
|
||||
{
|
||||
return static_cast<RichFormat::Format>(m_textItem->textCursor().blockFormat().headingLevel());
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::tab()
|
||||
{
|
||||
QTextCursor cursor = m_textItem->textCursor();
|
||||
@@ -807,10 +575,10 @@ void ChatDocumentHandler::tab()
|
||||
return;
|
||||
}
|
||||
if (cursor.currentList()) {
|
||||
indentListMore();
|
||||
m_textItem->indentListMoreAtCursor();
|
||||
return;
|
||||
}
|
||||
insertText(u" "_s);
|
||||
cursor.insertText(u" "_s);
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::deleteChar()
|
||||
@@ -835,9 +603,8 @@ void ChatDocumentHandler::backspace()
|
||||
return;
|
||||
}
|
||||
if (cursor.position() <= m_fixedStartChars.length()) {
|
||||
qWarning() << "unhandled backspace";
|
||||
if (cursor.currentList()) {
|
||||
indentListLess();
|
||||
m_textItem->indentListLessAtCursor();
|
||||
return;
|
||||
}
|
||||
if (const auto previousHandler = previousDocumentHandler()) {
|
||||
@@ -859,16 +626,6 @@ void ChatDocumentHandler::insertReturn()
|
||||
cursor.insertBlock();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::insertText(const QString &text)
|
||||
{
|
||||
m_textItem->textCursor().insertText(text);
|
||||
}
|
||||
|
||||
QString ChatDocumentHandler::currentLinkUrl() const
|
||||
{
|
||||
return m_textItem->textCursor().charFormat().anchorHref();
|
||||
}
|
||||
|
||||
void ChatDocumentHandler::dumpHtml()
|
||||
{
|
||||
qWarning() << htmlText();
|
||||
|
||||
@@ -95,17 +95,6 @@ class ChatDocumentHandler : public QObject
|
||||
*/
|
||||
Q_PROPERTY(bool atLastLine READ atLastLine NOTIFY atLastLineChanged)
|
||||
|
||||
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
|
||||
|
||||
Q_PROPERTY(bool bold READ bold NOTIFY textFormatChanged)
|
||||
Q_PROPERTY(bool italic READ italic NOTIFY textFormatChanged)
|
||||
Q_PROPERTY(bool underline READ underline NOTIFY textFormatChanged)
|
||||
Q_PROPERTY(bool strikethrough READ strikethrough NOTIFY textFormatChanged)
|
||||
|
||||
Q_PROPERTY(RichFormat::Format style READ style NOTIFY styleChanged)
|
||||
|
||||
Q_PROPERTY(int currentListStyle READ currentListStyle NOTIFY listChanged)
|
||||
|
||||
public:
|
||||
enum InsertPosition {
|
||||
Cursor,
|
||||
@@ -152,33 +141,11 @@ public:
|
||||
*/
|
||||
Q_INVOKABLE void updateMentions(const QString &editId);
|
||||
|
||||
QColor textColor() const;
|
||||
void setTextColor(const QColor &color);
|
||||
|
||||
bool bold() const;
|
||||
bool italic() const;
|
||||
bool underline() const;
|
||||
bool strikethrough() const;
|
||||
|
||||
Q_INVOKABLE void setFormat(RichFormat::Format format);
|
||||
|
||||
int currentListStyle() const;
|
||||
bool canIndentListMore() const;
|
||||
bool canIndentListLess() const;
|
||||
Q_INVOKABLE void indentListLess();
|
||||
Q_INVOKABLE void indentListMore();
|
||||
|
||||
RichFormat::Format style() const;
|
||||
|
||||
Q_INVOKABLE void tab();
|
||||
Q_INVOKABLE void deleteChar();
|
||||
Q_INVOKABLE void backspace();
|
||||
Q_INVOKABLE void insertReturn();
|
||||
Q_INVOKABLE void insertText(const QString &text);
|
||||
void insertFragment(const QTextDocumentFragment fragment, InsertPosition position = Cursor, bool keepPosition = false);
|
||||
Q_INVOKABLE QString currentLinkUrl() const;
|
||||
Q_INVOKABLE QString currentLinkText() const;
|
||||
Q_INVOKABLE void updateLink(const QString &linkUrl, const QString &linkText);
|
||||
Q_INVOKABLE void insertCompletion(const QString &text, const QUrl &link);
|
||||
|
||||
Q_INVOKABLE void dumpHtml();
|
||||
@@ -192,14 +159,11 @@ Q_SIGNALS:
|
||||
void atFirstLineChanged();
|
||||
void atLastLineChanged();
|
||||
|
||||
void textColorChanged();
|
||||
|
||||
void currentListStyleChanged();
|
||||
|
||||
void formatChanged();
|
||||
void textFormatChanged();
|
||||
void styleChanged();
|
||||
void listChanged();
|
||||
|
||||
void contentsChanged();
|
||||
|
||||
@@ -220,10 +184,6 @@ private:
|
||||
QString m_initialText = {};
|
||||
void initializeChars();
|
||||
|
||||
QPointer<ChatMarkdownHelper> m_markdownHelper;
|
||||
std::optional<QTextCharFormat> m_pendingFormat = std::nullopt;
|
||||
std::optional<QTextCharFormat> m_pendingOverrideFormat = std::nullopt;
|
||||
|
||||
SyntaxHighlighter *m_highlighter = nullptr;
|
||||
|
||||
QString getText() const;
|
||||
@@ -231,10 +191,6 @@ private:
|
||||
|
||||
std::optional<Qt::TextFormat> textFormat() const;
|
||||
void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
|
||||
void selectLinkText(QTextCursor *cursor) const;
|
||||
QColor linkColor();
|
||||
QColor mLinkColor;
|
||||
void regenerateColorScheme();
|
||||
|
||||
QString trim(QString string) const;
|
||||
};
|
||||
|
||||
@@ -83,32 +83,39 @@ std::optional<MarkdownSyntax> syntaxForSequence(const QString &sequence)
|
||||
|
||||
ChatMarkdownHelper::ChatMarkdownHelper(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_textItem(new QmlTextItemWrapper(this))
|
||||
{
|
||||
connectTextItem();
|
||||
}
|
||||
|
||||
QQuickItem *ChatMarkdownHelper::textItem() const
|
||||
QmlTextItemWrapper *ChatMarkdownHelper::textItem() const
|
||||
{
|
||||
return m_textItem->textItem();
|
||||
return m_textItem;
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::setTextItem(QQuickItem *textItem)
|
||||
void ChatMarkdownHelper::setTextItem(QmlTextItemWrapper *textItem)
|
||||
{
|
||||
m_textItem->setTextItem(textItem);
|
||||
}
|
||||
if (textItem == m_textItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::connectTextItem()
|
||||
{
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &ChatMarkdownHelper::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, [this]() {
|
||||
m_startPos = m_textItem->cursorPosition();
|
||||
m_endPos = m_startPos;
|
||||
if (m_startPos == 0) {
|
||||
m_currentState = Pre;
|
||||
}
|
||||
});
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChange, this, &ChatMarkdownHelper::checkMarkdown);
|
||||
if (m_textItem) {
|
||||
m_textItem->disconnect(this);
|
||||
}
|
||||
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &ChatMarkdownHelper::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::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);
|
||||
}
|
||||
|
||||
Q_EMIT textItemChanged();
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::checkMarkdown(int position, int charsRemoved, int charsAdded)
|
||||
@@ -140,7 +147,6 @@ void ChatMarkdownHelper::checkMarkdown(int position, int charsRemoved, int chars
|
||||
cursor.setPosition(m_startPos);
|
||||
|
||||
const auto result = checkSequence(currentMarkdown, nextChar, cursor.atBlockStart());
|
||||
qWarning() << m_startPos << m_endPos << result;
|
||||
|
||||
switch (m_currentState) {
|
||||
case None:
|
||||
@@ -216,7 +222,6 @@ void ChatMarkdownHelper::complete()
|
||||
m_endPos = result ? m_startPos + 1 : m_startPos;
|
||||
|
||||
cursor.endEditBlock();
|
||||
qWarning() << m_currentState << m_startPos << m_endPos << m_textItem->cursorPosition();
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::handleExternalFormatChange()
|
||||
|
||||
@@ -19,8 +19,8 @@ class ChatMarkdownHelper : public QObject
|
||||
public:
|
||||
explicit ChatMarkdownHelper(QObject *parent = nullptr);
|
||||
|
||||
QQuickItem *textItem() const;
|
||||
void setTextItem(QQuickItem *textItem);
|
||||
QmlTextItemWrapper *textItem() const;
|
||||
void setTextItem(QmlTextItemWrapper *textItem);
|
||||
|
||||
void handleExternalFormatChange();
|
||||
|
||||
@@ -36,7 +36,6 @@ private:
|
||||
};
|
||||
|
||||
QPointer<QmlTextItemWrapper> m_textItem;
|
||||
void connectTextItem();
|
||||
|
||||
State m_currentState = None;
|
||||
int m_startPos = 0;
|
||||
|
||||
@@ -20,8 +20,8 @@ CompletionModel::CompletionModel(QObject *parent)
|
||||
, m_emojiModel(new QConcatenateTablesProxyModel(this))
|
||||
{
|
||||
connect(m_textItem, &QmlTextItemWrapper::textItemChanged, this, &CompletionModel::textItemChanged);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentCursorPositionChanged, this, &CompletionModel::updateTextStart);
|
||||
connect(m_textItem, &QmlTextItemWrapper::textDocumentContentsChanged, this, &CompletionModel::updateCompletion);
|
||||
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());
|
||||
|
||||
@@ -39,39 +39,30 @@ bool NestedListHelper::handleBeforeKeyPressEvent(QKeyEvent *event, const QTextCu
|
||||
|
||||
bool NestedListHelper::canIndent(const QTextCursor &textCursor) const
|
||||
{
|
||||
if ((textCursor.block().isValid())
|
||||
// && ( textEdit->textCursor().block().previous().isValid() )
|
||||
) {
|
||||
const QTextBlock block = textCursor.block();
|
||||
const QTextBlock prevBlock = textCursor.block().previous();
|
||||
if (block.textList()) {
|
||||
if (prevBlock.textList()) {
|
||||
return block.textList()->format().indent() <= prevBlock.textList()->format().indent();
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
const auto block = textCursor.block();
|
||||
if (textCursor.isNull() || !block.isValid()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
if (!block.textList()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return block.textList()->format().indent() < 3;
|
||||
}
|
||||
|
||||
bool NestedListHelper::canDedent(const QTextCursor &textCursor) const
|
||||
{
|
||||
QTextBlock thisBlock = textCursor.block();
|
||||
QTextBlock nextBlock = thisBlock.next();
|
||||
if (thisBlock.isValid()) {
|
||||
int nextBlockIndent = 0;
|
||||
if (nextBlock.isValid() && nextBlock.textList()) {
|
||||
nextBlockIndent = nextBlock.textList()->format().indent();
|
||||
}
|
||||
if (thisBlock.textList()) {
|
||||
const int thisBlockIndent = thisBlock.textList()->format().indent();
|
||||
if (thisBlockIndent >= nextBlockIndent) {
|
||||
return thisBlockIndent > 0;
|
||||
}
|
||||
}
|
||||
const auto block = textCursor.block();
|
||||
if (textCursor.isNull() || !block.isValid()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
if (!block.textList()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return block.textList()->format().indent() > 0;
|
||||
}
|
||||
|
||||
bool NestedListHelper::handleAfterKeyPressEvent(QKeyEvent *event, const QTextCursor &cursor)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// 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>
|
||||
@@ -34,14 +35,18 @@ void QmlTextItemWrapper::setTextItem(QQuickItem *textItem)
|
||||
m_textItem = textItem;
|
||||
|
||||
if (m_textItem) {
|
||||
connect(m_textItem, SIGNAL(cursorPositionChanged()), this, SLOT(textDocCursorChanged()));
|
||||
connect(m_textItem, SIGNAL(cursorPositionChanged()), this, SLOT(itemCursorPositionChanged()));
|
||||
if (document()) {
|
||||
connect(document(), &QTextDocument::contentsChanged, this, &QmlTextItemWrapper::textDocumentContentsChanged);
|
||||
connect(document(), &QTextDocument::contentsChange, this, &QmlTextItemWrapper::textDocumentContentsChange);
|
||||
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
|
||||
@@ -109,15 +114,33 @@ void QmlTextItemWrapper::setCursorVisible(bool visible)
|
||||
m_textItem->setProperty("cursorVisible", visible);
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::textDocCursorChanged()
|
||||
void QmlTextItemWrapper::itemCursorPositionChanged()
|
||||
{
|
||||
Q_EMIT textDocumentCursorPositionChanged();
|
||||
Q_EMIT cursorPositionChanged();
|
||||
Q_EMIT formatChanged();
|
||||
Q_EMIT textFormatChanged();
|
||||
Q_EMIT styleChanged();
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::mergeFormatOnCursor(RichFormat::Format format, const QTextCursor &cursor)
|
||||
QList<RichFormat::Format> QmlTextItemWrapper::formatsAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
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:
|
||||
@@ -126,6 +149,10 @@ void QmlTextItemWrapper::mergeFormatOnCursor(RichFormat::Format format, const QT
|
||||
case RichFormat::List:
|
||||
mergeListFormatOnCursor(format, cursor);
|
||||
return;
|
||||
case RichFormat::Block:
|
||||
if (format != RichFormat::Paragraph) {
|
||||
return;
|
||||
}
|
||||
case RichFormat::Style:
|
||||
mergeStyleFormatOnCursor(format, cursor);
|
||||
return;
|
||||
@@ -194,48 +221,47 @@ void QmlTextItemWrapper::mergeListFormatOnCursor(RichFormat::Format format, cons
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
int QmlTextItemWrapper::currentListStyle() const
|
||||
bool QmlTextItemWrapper::canIndentListMoreAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull() || !textCursor().currentList()) {
|
||||
return 0;
|
||||
}
|
||||
return -cursor.currentList()->format().style();
|
||||
}
|
||||
|
||||
bool QmlTextItemWrapper::canIndentListMore() const
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canIndent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
bool QmlTextItemWrapper::canIndentListLess() const
|
||||
bool QmlTextItemWrapper::canIndentListLessAtCursor(QTextCursor cursor) const
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_nestedListHelper.canDedent(cursor) && cursor.blockFormat().headingLevel() == 0;
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::indentListMore()
|
||||
void QmlTextItemWrapper::indentListMoreAtCursor(QTextCursor cursor)
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentMore(cursor);
|
||||
Q_EMIT listChanged();
|
||||
}
|
||||
|
||||
void QmlTextItemWrapper::indentListLess()
|
||||
void QmlTextItemWrapper::indentListLessAtCursor(QTextCursor cursor)
|
||||
{
|
||||
auto cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
cursor = textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_nestedListHelper.handleOnIndentLess(cursor);
|
||||
Q_EMIT listChanged();
|
||||
|
||||
@@ -38,24 +38,24 @@ public:
|
||||
void setCursorPosition(int pos);
|
||||
void setCursorVisible(bool visible);
|
||||
|
||||
void mergeFormatOnCursor(RichFormat::Format format, const QTextCursor &cursor);
|
||||
QList<RichFormat::Format> formatsAtCursor(QTextCursor cursor = {}) const;
|
||||
void mergeFormatOnCursor(RichFormat::Format format, QTextCursor cursor = {});
|
||||
|
||||
int currentListStyle() const;
|
||||
bool canIndentListMore() const;
|
||||
bool canIndentListLess() const;
|
||||
void indentListMore();
|
||||
void indentListLess();
|
||||
bool canIndentListMoreAtCursor(QTextCursor cursor = {}) const;
|
||||
bool canIndentListLessAtCursor(QTextCursor cursor = {}) const;
|
||||
void indentListMoreAtCursor(QTextCursor cursor = {});
|
||||
void indentListLessAtCursor(QTextCursor cursor = {});
|
||||
|
||||
void forceActiveFocus() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
|
||||
void textDocumentContentsChange(int position, int charsRemoved, int charsAdded);
|
||||
void contentsChange(int position, int charsRemoved, int charsAdded);
|
||||
|
||||
void textDocumentContentsChanged();
|
||||
void contentsChanged();
|
||||
|
||||
void textDocumentCursorPositionChanged();
|
||||
void cursorPositionChanged();
|
||||
|
||||
void formatChanged();
|
||||
void textFormatChanged();
|
||||
@@ -74,5 +74,5 @@ private:
|
||||
NestedListHelper m_nestedListHelper;
|
||||
|
||||
private Q_SLOTS:
|
||||
void textDocCursorChanged();
|
||||
void itemCursorPositionChanged();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user