From 69c2e873e74727526cf197ab4a06b368a1d820ec Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 31 Jul 2018 13:46:48 +0200 Subject: Fixed texteditor test --- framework/qml/TextEditor.qml | 9 ++------ framework/qml/tests/tst_texteditor.qml | 32 ++++++++-------------------- framework/src/domain/textdocumenthandler.cpp | 19 ++++++++++++----- framework/src/domain/textdocumenthandler.h | 6 +++++- 4 files changed, 30 insertions(+), 36 deletions(-) (limited to 'framework') diff --git a/framework/qml/TextEditor.qml b/framework/qml/TextEditor.qml index 7dd30625..bc50169e 100644 --- a/framework/qml/TextEditor.qml +++ b/framework/qml/TextEditor.qml @@ -24,9 +24,8 @@ import org.kube.framework 1.0 as Kube FocusScope { id: root - property string text: "" - - property bool htmlEnabled: false + property string text: document.text + property bool htmlEnabled: document.containsFormatting property alias bold: document.bold property alias italic: document.italic @@ -46,10 +45,6 @@ FocusScope { document: edit.textDocument selectionStart: edit.selectionStart selectionEnd: edit.selectionEnd - onTextChanged: { - root.htmlEnabled = containsFormatting(); - root.htmlEnabled ? root.text = htmlText : root.text = plainText - } cursorPosition: edit.cursorPosition } diff --git a/framework/qml/tests/tst_texteditor.qml b/framework/qml/tests/tst_texteditor.qml index f80afe66..544270e5 100644 --- a/framework/qml/tests/tst_texteditor.qml +++ b/framework/qml/tests/tst_texteditor.qml @@ -33,41 +33,27 @@ TestCase { Kube.TextEditor {} } - function test_1initialText() { - var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: false}) - compare(editor.text, editor.initialText) - } + property string htmlText: "

Foobar
BarBar

" + property string plainText: "Foobar\nBarBar" - function test_2plainToHtmlConversion() { - var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: false}) - editor.htmlEnabled = true - verify(editor.text.indexOf("") !== -1) - //It's converted into two paragraphs, so we can't check as a single string - verify(editor.text.indexOf("Foobar") !== -1) - verify(editor.text.indexOf("BarBar") !== -1) - editor.htmlEnabled = false + function test_1initialText() { + var editor = createTemporaryObject(editorComponent, testCase, {initialText: plainText}) compare(editor.text, editor.initialText) - - editor.htmlEnabled = true - verify(editor.text.indexOf("") !== -1) } function test_3htmlToPlainConversion() { - var editor = createTemporaryObject(editorComponent, testCase, {initialText: "

test

", htmlEnabled: true}) - editor.htmlEnabled = false - compare(editor.text, "test") - - editor.htmlEnabled = true - verify(editor.text.indexOf("") !== -1) + var editor = createTemporaryObject(editorComponent, testCase, {initialText: htmlText}) + editor.clearFormatting() + compare(editor.text, plainText) } function test_4detectPlain() { - var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: true}) + var editor = createTemporaryObject(editorComponent, testCase, {initialText: plainText}) compare(editor.htmlEnabled, false) } function test_5detectHtml() { - var editor = createTemporaryObject(editorComponent, testCase, {initialText: "

test

", htmlEnabled: false}) + var editor = createTemporaryObject(editorComponent, testCase, {initialText: htmlText}) compare(editor.htmlEnabled, true) } } diff --git a/framework/src/domain/textdocumenthandler.cpp b/framework/src/domain/textdocumenthandler.cpp index f0cde10d..ba0aa98f 100644 --- a/framework/src/domain/textdocumenthandler.cpp +++ b/framework/src/domain/textdocumenthandler.cpp @@ -32,7 +32,7 @@ TextDocumentHandler::TextDocumentHandler(QObject *parent) { } -bool TextDocumentHandler::containsFormatting() +bool TextDocumentHandler::containsFormatting() const { if (mDocument) { for (const auto &format : mDocument->textDocument()->allFormats()) { @@ -62,7 +62,9 @@ bool TextDocumentHandler::containsFormatting() void TextDocumentHandler::resetFormat() { - mDocument->textDocument()->setPlainText(mDocument->textDocument()->toPlainText()); + if (mDocument) { + mDocument->textDocument()->setPlainText(mDocument->textDocument()->toPlainText()); + } mCachedTextFormat = {}; reset(); } @@ -76,12 +78,18 @@ void TextDocumentHandler::setDocument(QQuickTextDocument *document) { if (document != mDocument) { mDocument = document; - connect(mDocument->textDocument(), &QTextDocument::contentsChanged, this, [this] () { - emit textChanged(); - }); connect(mDocument->textDocument(), &QTextDocument::contentsChange, this, &TextDocumentHandler::contentsChange); emit documentChanged(); + emit textChanged(); + } +} + +QString TextDocumentHandler::text() const +{ + if (containsFormatting()) { + return htmlText(); } + return plainText(); } QString TextDocumentHandler::plainText() const @@ -296,6 +304,7 @@ void TextDocumentHandler::contentsChange(int position, int charsRemoved, int cha } mCachedTextFormat = {}; } + emit textChanged(); } void TextDocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) diff --git a/framework/src/domain/textdocumenthandler.h b/framework/src/domain/textdocumenthandler.h index cb4d4e29..e5fe705b 100644 --- a/framework/src/domain/textdocumenthandler.h +++ b/framework/src/domain/textdocumenthandler.h @@ -41,9 +41,11 @@ class KUBE_EXPORT TextDocumentHandler : public QObject Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged) Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) + Q_PROPERTY(bool containsFormatting READ containsFormatting NOTIFY textChanged) Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) + Q_PROPERTY(QString text READ text NOTIFY textChanged) Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged) Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged) @@ -53,9 +55,12 @@ public: QQuickTextDocument *document() const; void setDocument(QQuickTextDocument *document); + QString text() const; QString plainText() const; QString htmlText() const; + bool containsFormatting() const; + int cursorPosition() const; void setCursorPosition(int position); @@ -87,7 +92,6 @@ public: void setFontSize(int size); Q_INVOKABLE void resetFormat(); - Q_INVOKABLE bool containsFormatting(); Q_INVOKABLE static bool isHtml(const QString &); -- cgit v1.2.3