on xenial help sign in
added PreviewCommentInput, a single line input item for comments/replies
commit5303d17a8bc6fcaa7810bfc8d9fb61a81ac442e6 authorMichael Zanetti <michael.zanetti@canonical.com> dateMon 1 Jun 2015, 15:05 (11 years ago) parent60639b5
4 files changed+190
qml/Dash/Previews/PreviewCommentInput.qml+103 qml/Dash/Previews/PreviewWidgetFactory.qml+1 tests/qmltests/CMakeLists.txt+1 tests/qmltests/Dash/Previews/tst_PreviewCommentInput.qml+85
qml/Dash/Previews/PreviewCommentInput.qmlnew file+103
@@ -0,0 +1,103 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+import Ubuntu.Components 0.1
19+import "../../Components"
20+
21+/*! \brief Preview widget for rating.
22+
23+ The widget can show a rating widget and a field to enter a comment.
24+ The visibility of the two widgets is specified by widgetData["visible"],
25+ accepting "both", "rating" or "review".
26+ The requirement of the review is specified by widgetData["visible"],
27+ accepting "both", "rating" or "review".
28+ It is possible to customise labels, widgetData["rating-label"] for the rating,
29+ widgetData["rewiew-label"] for the comment field and widgetData["submit-label"]
30+ for the submit button.
31+ The icons used in the rating widget can be customised with
32+ widgetData["rating-icon-empty"] and widgetData["rating-icon-full"].
33+ The successeful submit emits triggered(widgetId, widgetData["required"], data),
34+ with data being {"rating": rating value, "review": review comment, "author": null (for now)}.
35+*/
36+
37+PreviewWidget {
38+ id: root
39+ implicitHeight: commentContainer.implicitHeight
40+
41+ property alias commentText: commentTextArea.text
42+
43+ function submit() {
44+ // checks if comment text area actually contains something
45+ if (commentTextArea.text === "") return;
46+
47+ var data = { "comment": commentTextArea.text };
48+ triggered(root.widgetId, "commented", data);
49+ }
50+
51+ Item {
52+ id: commentContainer
53+ implicitHeight: commentSubmitContainer.implicitHeight + anchors.topMargin
54+
55+ readonly property real innerMargin: units.gu(1)
56+
57+ anchors {
58+ left: parent.left
59+ right: parent.right
60+ top: parent.top
61+ bottom: parent.bottom
62+ }
63+
64+ Item {
65+ id: commentSubmitContainer
66+ anchors {
67+ top: parent.top
68+ left: parent.left
69+ right: parent.right
70+ bottom: parent.bottom
71+ }
72+ implicitHeight: commentTextArea.implicitHeight + anchors.topMargin
73+
74+ TextField {
75+ id: commentTextArea
76+ objectName: "commentTextArea"
77+ anchors {
78+ top: parent.top
79+ left: parent.left
80+ right: submitButton.left
81+ rightMargin: commentContainer.innerMargin
82+ }
83+ }
84+
85+ Button {
86+ id: submitButton
87+ objectName: "submitButton"
88+
89+ readonly property bool readyToSubmit: commentTextArea.text.length > 0
90+
91+ anchors {
92+ top: parent.top
93+ right: parent.right
94+ }
95+ color: readyToSubmit ? Theme.palette.selected.base : Theme.palette.normal.base
96+ text: widgetData["submit-label"] || i18n.tr("Send")
97+ onClicked: {
98+ if (readyToSubmit) root.submit()
99+ }
100+ }
101+ }
102+ }
103+}
qml/Dash/Previews/PreviewWidgetFactory.qml+1
@@ -62,6 +62,7 @@ Loader {
6262 case "table": return "PreviewTable.qml";
6363 case "text": return "PreviewTextSummary.qml";
6464 case "video": return "PreviewVideoPlayback.qml";
65+ case "comment": return "PreviewCommentInput.qml";
6566 default: return "";
6667 }
6768 }
tests/qmltests/CMakeLists.txt+1
@@ -43,6 +43,7 @@ add_qml_test(Dash PreviewListView)
4343add_qml_test(Dash/Previews Preview)
4444add_qml_test(Dash/Previews PreviewActions)
4545add_qml_test(Dash/Previews PreviewAudioPlayback)
46+add_qml_test(Dash/Previews PreviewCommentInput)
4647add_qml_test(Dash/Previews PreviewExpandable)
4748add_qml_test(Dash/Previews PreviewHeader)
4849add_qml_test(Dash/Previews PreviewImageGallery)
tests/qmltests/Dash/Previews/tst_PreviewCommentInput.qmlnew file+85
@@ -0,0 +1,85 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+import QtTest 1.0
19+import Ubuntu.Components 0.1
20+import "../../../../qml/Dash/Previews"
21+import Unity.Test 0.1 as UT
22+
23+Rectangle {
24+ id: root
25+ width: units.gu(40)
26+ height: units.gu(80)
27+ color: Theme.palette.selected.background
28+
29+ property real validInputRating: 1
30+ property real invalidInputRating: -1
31+ property string validInputText: "Great!"
32+ property string invalidInputText: ""
33+
34+ property var widgetDataNewLabels: { "submit-label": "TestSubmitLabel" }
35+
36+ PreviewCommentInput {
37+ id: previewCommentInput
38+ anchors.left: parent.left
39+ anchors.right: parent.right
40+ widgetData: widgetDataNewLabels
41+ widgetId: "previewCommentInput"
42+ }
43+
44+ SignalSpy {
45+ id: spy
46+ signalName: "triggered"
47+ }
48+
49+ UT.UnityTestCase {
50+ name: "PreviewCommentInputTest"
51+ when: windowShown
52+
53+ property var commentTextArea: findChild(previewCommentInput, "commentTextArea")
54+ property var submitButton: findChild(previewCommentInput, "submitButton")
55+
56+ function init() {
57+ commentTextArea.text = "";
58+ }
59+
60+ function test_labels() {
61+ previewCommentInput.widgetData = widgetDataNewLabels;
62+ compare(submitButton.text, widgetDataNewLabels["submit-label"]);
63+ }
64+
65+ function test_submit_data() {
66+ return [
67+ {tag: "empty review", inputText: invalidInputText, emitted: false},
68+ {tag: "filled review", inputText: validInputText, emitted: true},
69+ ];
70+ }
71+
72+ function test_submit(data) {
73+ spy.clear();
74+ spy.target = previewCommentInput;
75+
76+ commentTextArea.text = data.inputText;
77+ mouseClick(submitButton);
78+ if (data.inputText === "") {
79+ compare(spy.count, 0);
80+ } else {
81+ compare(spy.count, 1);
82+ }
83+ }
84+ }
85+}