on xenial help sign in
Work on the Rating widget
commitce161c0fbd722a0923d5c85263d8cfdb3e62dbf1 authorAndrea Cimitan <andrea.cimitan@gmail.com> dateFri 7 Feb 2014, 14:17 (13 years ago) parentfc277e5
6 files changed+166−157
qml/Components/Rating.qml+48 qml/Components/RatingStars.qml−62 qml/Components/RatingStyle.qml+31 tests/qmltests/CMakeLists.txt+1−1 tests/qmltests/Components/tst_Rating.qml+86 tests/qmltests/Components/tst_RatingStars.qml−94
qml/Components/Rating.qmlnew file+48
@@ -0,0 +1,48 @@
1+/*
2+ * Copyright (C) 2014 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+
20+/*! Rating widget bar. */
21+
22+StyledItem {
23+ id: root
24+
25+ //! True if it accepts user input.
26+ property bool interactive: true
27+
28+ //! Maximum rating.
29+ property real maximumValue: 5
30+
31+ //! Number of rating icons.
32+ property int size: 5
33+
34+ //! Current rating.
35+ property real value: -1
36+
37+ readonly property int effectiveValue: MathUtils.clamp(size * value / maximumValue, 0, maximumValue)
38+
39+ style: RatingStyle {}
40+
41+ MouseArea {
42+ anchors.fill: root
43+ enabled: root.interactive
44+ onClicked: root.value = Math.ceil(mouse.x / averageIconWidth) * root.maximumValue / root.size
45+
46+ readonly property int averageIconWidth: width / root.size
47+ }
48+}
qml/Components/RatingStars.qmldeleted−62
@@ -1,62 +0,0 @@
1−/*
2− * Copyright (C) 2013 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−
20−Item {
21− id: root
22−
23− //! True if it accepts user input.
24− property bool interactive: true
25−
26− //! Current rating.
27− property int rating
28−
29− //! Maximum rating.
30− property int maximumRating: 5
31−
32− //! Number of rating stars.
33− property int starsCount: 5
34−
35− readonly property int effectiveRating: MathUtils.clamp(starsCount * rating / maximumRating, 0, maximumRating)
36−
37− Row {
38− id: row
39− anchors.centerIn: parent
40− height: childrenRect.height
41− width: childrenRect.width
42−
43− Repeater {
44− id: repeater
45−
46− property int averageDelegateWidth: row.width / root.starsCount
47−
48− model: root.starsCount
49−
50− Image {
51− objectName: "ratingStar" + index
52− source: index < root.effectiveRating ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
53− }
54− }
55− }
56−
57− MouseArea {
58− anchors.fill: row
59− enabled: root.interactive
60− onClicked: root.rating = Math.ceil(mouse.x / repeater.averageDelegateWidth) * root.maximumRating / root.starsCount
61− }
62−}
qml/Components/RatingStyle.qmlnew file+31
@@ -0,0 +1,31 @@
1+/*
2+ * Copyright (C) 2014 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+
20+Row {
21+ anchors.fill: parent
22+
23+ Repeater {
24+ model: styledItem.size
25+
26+ Image {
27+ opacity: styledItem.value < 0 ? 0.4 : 1 // Let's make the stars look inactive for a not-set value
28+ source: index < styledItem.effectiveValue ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
29+ }
30+ }
31+}
tests/qmltests/CMakeLists.txt+1−1
@@ -28,7 +28,7 @@ add_qml_test(Components DraggingArea)
2828add_qml_test(Components EdgeDemoOverlay)
2929add_qml_test(Components FilterGrid IMPORT_PATHS ${qmltest_DEFAULT_IMPORT_PATHS} ${CMAKE_BINARY_DIR}/plugins)
3030add_qml_test(Components LazyImage)
31−add_qml_test(Components RatingStars)
31+add_qml_test(Components Rating)
3232add_qml_test(Components ResponsiveFlowView)
3333add_qml_test(Components ResponsiveGridView)
3434add_qml_test(Components Revealer)
tests/qmltests/Components/tst_Rating.qmlnew file+86
@@ -0,0 +1,86 @@
1+/*
2+ * Copyright 2013 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 "../../../qml/Components"
20+import Ubuntu.Components 0.1
21+import Unity.Test 0.1 as UT
22+
23+Rectangle {
24+ id: root
25+ width: units.gu(40)
26+ height: units.gu(10)
27+ color: "black"
28+
29+ Rating {
30+ id: rating
31+ anchors.centerIn: parent
32+ maximumValue: 5
33+ size: 5
34+ value: 3
35+ }
36+
37+ UT.UnityTestCase {
38+ name: "RatingTest"
39+ when: windowShown
40+
41+ function init() {
42+ rating.maximumValue = 5;
43+ rating.size = 5
44+ rating.value = 3;
45+ }
46+
47+ function test_interactive_rating_data() {
48+ return [
49+ {tag: "1st icon without interactive", interactive: false, size: 5, maximumValue: 5, index: 0, value: 3},
50+ {tag: "1st icon", interactive: true, size: 5, maximumValue: 5, index: 0, value: 1},
51+ {tag: "1st icon size 10", interactive: true, size: 10, maximumValue: 10, index: 0, value: 1},
52+ {tag: "3rd icon size 10 small maximumValue", interactive: true, size: 10, maximumValue: 5, index: 2, value: 1.5},
53+ {tag: "2nd icon with big maximumValue", interactive: true, size: 5, maximumValue: 100, index: 1, value: 40},
54+ {tag: "last icon", interactive: true, size: 5, maximumValue: 5, index: 4, value: 5},
55+ ];
56+ }
57+
58+ function test_interactive_rating(data) {
59+ rating.interactive = data.interactive;
60+ rating.maximumValue = data.maximumValue;
61+ rating.size = data.size;
62+
63+ var averageIconWidth = rating.width / rating.size;
64+ mouseClick(rating, averageIconWidth * data.index + averageIconWidth / 2, rating.height / 2);
65+ compare(rating.value, data.value);
66+
67+ rating.interactive = false;
68+ }
69+
70+ function test_effectiveValue_data() {
71+ return [
72+ {tag: "negative", value: -3, expectedValue: 0},
73+ {tag: "ranged value", value: 2, expectedValue: 2},
74+ {tag: "big", value: 200, expectedValue: rating.maximumValue},
75+ {tag: "min", value: 0, expectedValue: 0},
76+ {tag: "max", value: rating.maximumValue, expectedValue: rating.maximumValue}
77+ ];
78+ }
79+
80+ function test_effectiveValue(data) {
81+ rating.value = data.value;
82+ wait(1000)
83+ compare(rating.effectiveValue, data.expectedValue, "effectiveValue not calculated correctly")
84+ }
85+ }
86+}
tests/qmltests/Components/tst_RatingStars.qmldeleted−94
@@ -1,94 +0,0 @@
1−/*
2− * Copyright 2013 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 "../../../qml/Components"
20−import Ubuntu.Components 0.1
21−import Unity.Test 0.1 as UT
22−
23−Rectangle {
24− id: root
25− width: units.gu(20)
26− height: units.gu(10)
27− color: "black"
28−
29− RatingStars {
30− id: ratingStars
31− anchors.centerIn: parent
32− maximumRating: 5
33− rating: 3
34− }
35−
36− UT.UnityTestCase {
37− name: "RatingStars"
38− when: windowShown
39−
40− function init() {
41− ratingStars.maximumRating = 5
42− ratingStars.rating = 3
43− }
44−
45− function test_interactive_rating_data() {
46− return [
47− {tag: "first star without interactive", interactive: false, maximumRating: 5, index: 0, rating: 3},
48− {tag: "first star", interactive: true, maximumRating: 5, index: 0, rating: 1},
49− {tag: "second star with big maximumRating", interactive: true, maximumRating: 100, index: 1, rating: 40},
50− {tag: "last star", interactive: true, maximumRating: 5, index: 4, rating: 5},
51− ];
52− }
53−
54− function test_interactive_rating(data) {
55− ratingStars.interactive = data.interactive
56− ratingStars.maximumRating = data.maximumRating
57−
58− var ratingStar = findChild(ratingStars, "ratingStar"+data.index)
59− mouseClick(ratingStar, ratingStar.width / 2, ratingStar.height / 2)
60− compare(ratingStars.rating, data.rating)
61−
62− ratingStars.interactive = false
63− }
64−
65− function test_rating_init() {
66− compare(ratingStars.effectiveRating, ratingStars.rating, "EffectiveRating not initialized properly")
67− }
68−
69− function test_rating_negative() {
70− ratingStars.rating = -3
71− compare(ratingStars.effectiveRating, 0, "EffectiveRating not calculated correctly")
72− }
73−
74− function test_rating_set_ok() {
75− ratingStars.rating = 2
76− compare(ratingStars.effectiveRating, ratingStars.rating, "EffectiveRating not calculated correctly")
77− }
78−
79− function test_rating_set_too_big() {
80− ratingStars.rating = 200
81− compare(ratingStars.effectiveRating, ratingStars.maximumRating, "EffectiveRating not calculated correctly")
82− }
83−
84− function test_rating_set_min() {
85− ratingStars.rating = 0
86− compare(ratingStars.effectiveRating, 0, "EffectiveRating not calculated correctly")
87− }
88−
89− function test_rating_set_max() {
90− ratingStars.rating = ratingStars.maximumRating
91− compare(ratingStars.effectiveRating, ratingStars.maximumRating, "EffectiveRating not calculated correctly")
92− }
93− }
94−}