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 + views/composer/qml/View.qml | 19 +++++----------- 4 files changed, 40 insertions(+), 31 deletions(-) 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 &); diff --git a/views/composer/qml/View.qml b/views/composer/qml/View.qml index 1c08dda6..117c8e01 100644 --- a/views/composer/qml/View.qml +++ b/views/composer/qml/View.qml @@ -303,19 +303,7 @@ Kube.View { spacing: Kube.Units.largeSpacing - Kube.Switch { - id: html - text: checked ? qsTr("plain") : qsTr("html") - focusPolicy: Qt.TabFocus - focus: false - checked: false - onCheckedChanged: { - textEditor.htmlEnabled = checked - } - } - Row { - visible: html.checked spacing: 1 Kube.IconButton { @@ -342,6 +330,12 @@ Kube.View { focusPolicy: Qt.TabFocus focus: false } + Kube.TextButton { + id: deleteButton + text: qsTr("Remove Formatting") + visible: textEditor.htmlEnabled + onClicked: textEditor.clearFormatting() + } } Item { @@ -379,7 +373,6 @@ Kube.View { Layout.fillWidth: true Layout.fillHeight: true onHtmlEnabledChanged: { - html.checked = htmlEnabled composerController.htmlBody = htmlEnabled; } -- cgit v1.2.3