diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-31 13:46:48 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-31 13:46:48 +0200 |
commit | 69c2e873e74727526cf197ab4a06b368a1d820ec (patch) | |
tree | 94ff7659ca6bb8e9e26ad1418d24bdd26ccc93af | |
parent | ab2b00093e1a40fc8dabd2c95b4bbd576e88e953 (diff) | |
download | kube-69c2e873e74727526cf197ab4a06b368a1d820ec.tar.gz kube-69c2e873e74727526cf197ab4a06b368a1d820ec.zip |
Fixed texteditor test
-rw-r--r-- | framework/qml/TextEditor.qml | 9 | ||||
-rw-r--r-- | framework/qml/tests/tst_texteditor.qml | 32 | ||||
-rw-r--r-- | framework/src/domain/textdocumenthandler.cpp | 19 | ||||
-rw-r--r-- | framework/src/domain/textdocumenthandler.h | 6 |
4 files changed, 30 insertions, 36 deletions
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 | |||
24 | 24 | ||
25 | FocusScope { | 25 | FocusScope { |
26 | id: root | 26 | id: root |
27 | property string text: "" | 27 | property string text: document.text |
28 | 28 | property bool htmlEnabled: document.containsFormatting | |
29 | property bool htmlEnabled: false | ||
30 | 29 | ||
31 | property alias bold: document.bold | 30 | property alias bold: document.bold |
32 | property alias italic: document.italic | 31 | property alias italic: document.italic |
@@ -46,10 +45,6 @@ FocusScope { | |||
46 | document: edit.textDocument | 45 | document: edit.textDocument |
47 | selectionStart: edit.selectionStart | 46 | selectionStart: edit.selectionStart |
48 | selectionEnd: edit.selectionEnd | 47 | selectionEnd: edit.selectionEnd |
49 | onTextChanged: { | ||
50 | root.htmlEnabled = containsFormatting(); | ||
51 | root.htmlEnabled ? root.text = htmlText : root.text = plainText | ||
52 | } | ||
53 | cursorPosition: edit.cursorPosition | 48 | cursorPosition: edit.cursorPosition |
54 | } | 49 | } |
55 | 50 | ||
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 { | |||
33 | Kube.TextEditor {} | 33 | Kube.TextEditor {} |
34 | } | 34 | } |
35 | 35 | ||
36 | function test_1initialText() { | 36 | property string htmlText: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head><body style=\" font-family:'Noto Sans'; font-size:9pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><b>Foobar</b><br>BarBar</p></body></html>" |
37 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: false}) | 37 | property string plainText: "Foobar\nBarBar" |
38 | compare(editor.text, editor.initialText) | ||
39 | } | ||
40 | 38 | ||
41 | function test_2plainToHtmlConversion() { | 39 | function test_1initialText() { |
42 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: false}) | 40 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: plainText}) |
43 | editor.htmlEnabled = true | ||
44 | verify(editor.text.indexOf("<html>") !== -1) | ||
45 | //It's converted into two paragraphs, so we can't check as a single string | ||
46 | verify(editor.text.indexOf("Foobar") !== -1) | ||
47 | verify(editor.text.indexOf("BarBar") !== -1) | ||
48 | editor.htmlEnabled = false | ||
49 | compare(editor.text, editor.initialText) | 41 | compare(editor.text, editor.initialText) |
50 | |||
51 | editor.htmlEnabled = true | ||
52 | verify(editor.text.indexOf("<html>") !== -1) | ||
53 | } | 42 | } |
54 | 43 | ||
55 | function test_3htmlToPlainConversion() { | 44 | function test_3htmlToPlainConversion() { |
56 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head><body style=\" font-family:'Noto Sans'; font-size:9pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">test</p></body></html>", htmlEnabled: true}) | 45 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: htmlText}) |
57 | editor.htmlEnabled = false | 46 | editor.clearFormatting() |
58 | compare(editor.text, "test") | 47 | compare(editor.text, plainText) |
59 | |||
60 | editor.htmlEnabled = true | ||
61 | verify(editor.text.indexOf("<html>") !== -1) | ||
62 | } | 48 | } |
63 | 49 | ||
64 | function test_4detectPlain() { | 50 | function test_4detectPlain() { |
65 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: "Foobar\nBarBar", htmlEnabled: true}) | 51 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: plainText}) |
66 | compare(editor.htmlEnabled, false) | 52 | compare(editor.htmlEnabled, false) |
67 | } | 53 | } |
68 | 54 | ||
69 | function test_5detectHtml() { | 55 | function test_5detectHtml() { |
70 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head><body style=\" font-family:'Noto Sans'; font-size:9pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">test</p></body></html>", htmlEnabled: false}) | 56 | var editor = createTemporaryObject(editorComponent, testCase, {initialText: htmlText}) |
71 | compare(editor.htmlEnabled, true) | 57 | compare(editor.htmlEnabled, true) |
72 | } | 58 | } |
73 | } | 59 | } |
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) | |||
32 | { | 32 | { |
33 | } | 33 | } |
34 | 34 | ||
35 | bool TextDocumentHandler::containsFormatting() | 35 | bool TextDocumentHandler::containsFormatting() const |
36 | { | 36 | { |
37 | if (mDocument) { | 37 | if (mDocument) { |
38 | for (const auto &format : mDocument->textDocument()->allFormats()) { | 38 | for (const auto &format : mDocument->textDocument()->allFormats()) { |
@@ -62,7 +62,9 @@ bool TextDocumentHandler::containsFormatting() | |||
62 | 62 | ||
63 | void TextDocumentHandler::resetFormat() | 63 | void TextDocumentHandler::resetFormat() |
64 | { | 64 | { |
65 | mDocument->textDocument()->setPlainText(mDocument->textDocument()->toPlainText()); | 65 | if (mDocument) { |
66 | mDocument->textDocument()->setPlainText(mDocument->textDocument()->toPlainText()); | ||
67 | } | ||
66 | mCachedTextFormat = {}; | 68 | mCachedTextFormat = {}; |
67 | reset(); | 69 | reset(); |
68 | } | 70 | } |
@@ -76,12 +78,18 @@ void TextDocumentHandler::setDocument(QQuickTextDocument *document) | |||
76 | { | 78 | { |
77 | if (document != mDocument) { | 79 | if (document != mDocument) { |
78 | mDocument = document; | 80 | mDocument = document; |
79 | connect(mDocument->textDocument(), &QTextDocument::contentsChanged, this, [this] () { | ||
80 | emit textChanged(); | ||
81 | }); | ||
82 | connect(mDocument->textDocument(), &QTextDocument::contentsChange, this, &TextDocumentHandler::contentsChange); | 81 | connect(mDocument->textDocument(), &QTextDocument::contentsChange, this, &TextDocumentHandler::contentsChange); |
83 | emit documentChanged(); | 82 | emit documentChanged(); |
83 | emit textChanged(); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | QString TextDocumentHandler::text() const | ||
88 | { | ||
89 | if (containsFormatting()) { | ||
90 | return htmlText(); | ||
84 | } | 91 | } |
92 | return plainText(); | ||
85 | } | 93 | } |
86 | 94 | ||
87 | QString TextDocumentHandler::plainText() const | 95 | QString TextDocumentHandler::plainText() const |
@@ -296,6 +304,7 @@ void TextDocumentHandler::contentsChange(int position, int charsRemoved, int cha | |||
296 | } | 304 | } |
297 | mCachedTextFormat = {}; | 305 | mCachedTextFormat = {}; |
298 | } | 306 | } |
307 | emit textChanged(); | ||
299 | } | 308 | } |
300 | 309 | ||
301 | void TextDocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) | 310 | 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 | |||
41 | Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged) | 41 | Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged) |
42 | Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) | 42 | Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) |
43 | Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) | 43 | Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) |
44 | Q_PROPERTY(bool containsFormatting READ containsFormatting NOTIFY textChanged) | ||
44 | 45 | ||
45 | Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) | 46 | Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) |
46 | 47 | ||
48 | Q_PROPERTY(QString text READ text NOTIFY textChanged) | ||
47 | Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged) | 49 | Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged) |
48 | Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged) | 50 | Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged) |
49 | 51 | ||
@@ -53,9 +55,12 @@ public: | |||
53 | QQuickTextDocument *document() const; | 55 | QQuickTextDocument *document() const; |
54 | void setDocument(QQuickTextDocument *document); | 56 | void setDocument(QQuickTextDocument *document); |
55 | 57 | ||
58 | QString text() const; | ||
56 | QString plainText() const; | 59 | QString plainText() const; |
57 | QString htmlText() const; | 60 | QString htmlText() const; |
58 | 61 | ||
62 | bool containsFormatting() const; | ||
63 | |||
59 | int cursorPosition() const; | 64 | int cursorPosition() const; |
60 | void setCursorPosition(int position); | 65 | void setCursorPosition(int position); |
61 | 66 | ||
@@ -87,7 +92,6 @@ public: | |||
87 | void setFontSize(int size); | 92 | void setFontSize(int size); |
88 | 93 | ||
89 | Q_INVOKABLE void resetFormat(); | 94 | Q_INVOKABLE void resetFormat(); |
90 | Q_INVOKABLE bool containsFormatting(); | ||
91 | 95 | ||
92 | Q_INVOKABLE static bool isHtml(const QString &); | 96 | Q_INVOKABLE static bool isHtml(const QString &); |
93 | 97 | ||