From 2bf195a1f5cbf5f473cbbcc929ad64d675e829cb Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 30 Jul 2018 18:06:31 +0200 Subject: Removed the html switch. Instead of having a switch we're going to always use an html editor, and simply send as plaintext if there is no formatting. To easily remove all formatting a button is added. --- framework/qml/TextEditor.qml | 17 ++++---------- framework/src/domain/textdocumenthandler.cpp | 34 ++++++++++++++++++++++++---- framework/src/domain/textdocumenthandler.h | 1 + 3 files changed, 34 insertions(+), 18 deletions(-) (limited to 'framework') diff --git a/framework/qml/TextEditor.qml b/framework/qml/TextEditor.qml index 7b1c5dee..45bf2ecd 100644 --- a/framework/qml/TextEditor.qml +++ b/framework/qml/TextEditor.qml @@ -34,21 +34,11 @@ FocusScope { property string initialText onInitialTextChanged: { - htmlEnabled = document.isHtml(initialText) edit.text = initialText } - onHtmlEnabledChanged: { - if (htmlEnabled) { - var t = document.htmlText - edit.textFormat = Qt.RichText - edit.text = t - } else { - var t = document.plainText - document.resetFormat() - edit.textFormat = Qt.PlainText - edit.text = t - } + function clearFormatting() { + document.resetFormat() } Kube.TextDocumentHandler { @@ -57,6 +47,7 @@ FocusScope { selectionStart: edit.selectionStart selectionEnd: edit.selectionEnd onTextChanged: { + root.htmlEnabled = containsFormatting(); root.htmlEnabled ? root.text = htmlText : root.text = plainText } cursorPosition: edit.cursorPosition @@ -105,7 +96,7 @@ FocusScope { focus: true selectByMouse: true wrapMode: TextEdit.Wrap - textFormat: Qt.PlainText + textFormat: Qt.RichText onCursorRectangleChanged: flickableItem.ensureVisible(cursorRectangle) color: Kube.Colors.textColor diff --git a/framework/src/domain/textdocumenthandler.cpp b/framework/src/domain/textdocumenthandler.cpp index ea312282..f0cde10d 100644 --- a/framework/src/domain/textdocumenthandler.cpp +++ b/framework/src/domain/textdocumenthandler.cpp @@ -32,13 +32,37 @@ TextDocumentHandler::TextDocumentHandler(QObject *parent) { } +bool TextDocumentHandler::containsFormatting() +{ + if (mDocument) { + for (const auto &format : mDocument->textDocument()->allFormats()) { + switch(format.type()) { + case QTextFormat::CharFormat: { + const auto charFormat = format.toCharFormat(); + if (charFormat.fontWeight() != QFont::Normal) { + return true; + } + if (charFormat.fontItalic()) { + return true; + } + if (charFormat.fontUnderline()) { + return true; + } + break; + } + case QTextFormat::BlockFormat: + case QTextFormat::FrameFormat: + default: + break; + } + } + } + return false; +} + void TextDocumentHandler::resetFormat() { - //Clear all formatting from the document. - auto cursor = textCursor(); - cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor); - cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); - cursor.setCharFormat({}); + mDocument->textDocument()->setPlainText(mDocument->textDocument()->toPlainText()); mCachedTextFormat = {}; reset(); } diff --git a/framework/src/domain/textdocumenthandler.h b/framework/src/domain/textdocumenthandler.h index 38de917d..cb4d4e29 100644 --- a/framework/src/domain/textdocumenthandler.h +++ b/framework/src/domain/textdocumenthandler.h @@ -87,6 +87,7 @@ public: void setFontSize(int size); Q_INVOKABLE void resetFormat(); + Q_INVOKABLE bool containsFormatting(); Q_INVOKABLE static bool isHtml(const QString &); -- cgit v1.2.3