on xenial help sign in
Use single mouse area, make starCount writable
6a8c4c7Andrea Cimitan
Components/RatingStars.qml+26−14
@@ -17,7 +17,7 @@
17import QtQuick 2.0
17import QtQuick 2.0
18import Ubuntu.Components 0.1
18import Ubuntu.Components 0.1
19
19
20−Row {
20+Item {
21 id: root
21 id: root
22
22
23 /*!
23 /*!
@@ -35,24 +35,36 @@ Row {
35 */
35 */
36 property int maximumRating: 5
36 property int maximumRating: 5
37
37
38+ /*!
39+ Number of rating stars.
40+ */
38− readonly property int starCount: 5
41+ property int starCount: 5
42+
39− readonly property int effectiveRating: Math.max(0, Math.min(starCount * rating / maximumRating, maximumRating))
43+ readonly property int effectiveRating: MathUtils.clamp(starCount * rating / maximumRating, 0, maximumRating)
40
44
45+ Row {
46+ id: row
47+ anchors.centerIn: parent
41− height: childrenRect.height
48+ height: childrenRect.height
42− width: childrenRect.width
49+ width: childrenRect.width
43
50
44− Repeater {
51+ Repeater {
45− model: root.starCount
52+ id: repeater
46
53
47− Image {
54+ property int averageDelegateWidth: row.width / root.starCount
48− objectName: "ratingStar" + index
49− source: index < root.effectiveRating ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
50
55
51− MouseArea {
56+ model: root.starCount
52− anchors.fill: parent
57+
53− enabled: root.interactive
58+ Image {
54− onClicked: root.rating = (index + 1) * root.maximumRating / root.starCount
59+ objectName: "ratingStar" + index
60+ source: index < root.effectiveRating ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
55 }
61 }
56 }
62 }
57 }
63 }
64+
65+ MouseArea {
66+ anchors.fill: row
67+ enabled: root.interactive
68+ onClicked: root.rating = Math.ceil((mouse.x / repeater.averageDelegateWidth)) * root.maximumRating / root.starCount
69+ }
58}
70}