on xenial help sign in
Use single mouse area, make starCount writable
commit6a8c4c79221fdfbf8b38f3a48afbec53079b1686 authorAndrea Cimitan <andrea.cimitan@gmail.com> dateMon 28 Oct 2013, 15:19 (13 years ago) parent41f4318
1 file changed+26−14
Components/RatingStars.qml+26−14
Components/RatingStars.qml+26−14
@@ -17,7 +17,7 @@
1717import QtQuick 2.0
1818import Ubuntu.Components 0.1
1919
20−Row {
20+Item {
2121 id: root
2222
2323 /*!
@@ -35,24 +35,36 @@ Row {
3535 */
3636 property int maximumRating: 5
3737
38− readonly property int starCount: 5
39− readonly property int effectiveRating: Math.max(0, Math.min(starCount * rating / maximumRating, maximumRating))
38+ /*!
39+ Number of rating stars.
40+ */
41+ property int starCount: 5
42+
43+ readonly property int effectiveRating: MathUtils.clamp(starCount * rating / maximumRating, 0, maximumRating)
4044
41− height: childrenRect.height
42− width: childrenRect.width
45+ Row {
46+ id: row
47+ anchors.centerIn: parent
48+ height: childrenRect.height
49+ width: childrenRect.width
4350
44− Repeater {
45− model: root.starCount
51+ Repeater {
52+ id: repeater
4653
47− Image {
48− objectName: "ratingStar" + index
49− source: index < root.effectiveRating ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
54+ property int averageDelegateWidth: row.width / root.starCount
5055
51− MouseArea {
52− anchors.fill: parent
53− enabled: root.interactive
54− onClicked: root.rating = (index + 1) * root.maximumRating / root.starCount
56+ model: root.starCount
57+
58+ Image {
59+ objectName: "ratingStar" + index
60+ source: index < root.effectiveRating ? "graphics/icon_star_on.png" : "graphics/icon_star_off.png"
5561 }
5662 }
5763 }
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+ }
5870}