2
* This file is part of unity-2d
4
* Copyright 2010-2011 Canonical Ltd.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
property real rating: 3.7
25
property int size: 5 /* Number of stars */
26
property alias enabled: starsMouseArea.enabled
27
property alias spacing: stars.spacing
29
/* Configure icon size to use. Requires icon files named:
30
- artwork/star_empty-${starIconSize}.png
31
- artwork/star_full-${starIconSize}.png
33
property int starIconSize: 32
38
function clamp(x, min, max) {
39
return Math.max(Math.min(x, max), min)
42
function incrementRating() {
43
/* Make math easier since half rating accepted */
44
var double_rating = rating * 2
46
if ( double_rating % 1 > 0){ /* if non-integer, round up */
47
double_rating = Math.ceil(double_rating)
50
double_rating = clamp(double_rating+1, 0, size * 2)
52
rating = double_rating / 2
55
function decrementRating() {
56
var double_rating = rating * 2
58
if ( double_rating % 1 > 0){ /* if non-integer, round down */
59
double_rating = Math.ceil(double_rating)
62
double_rating = clamp(double_rating-1, 0, size * 2)
64
rating = double_rating / 2
67
Keys.onPressed: if (handleKeyPress(event.key)) event.accepted = true
68
function handleKeyPress(key) {
86
fill: clamp(rating - index, 0, 1)
87
iconSize: starIconSize
88
selected: ( ratingStars.activeFocus )
96
/* Calculating Star Rating incorporating inter-star spacing/gap.
98
Consider each star+gap as a "unit". Determine what unit the mouse is over,
99
and remove the width of the gaps to the left from the reported mouse position.
102
/* Width of each unit */
103
property int unitWidth: starIconSize + stars.spacing
105
function calculateRating( posX ){
106
/* Small left-hand edge to set zero rating */
107
if( posX < 4 ) return 0
109
/* Mouse X coordinate over one unit, relative to that unit's left edge*/
110
var posXOverUnit = posX % unitWidth
112
/* What unit is the mouse over? This is the integer part of the rating (plus one)*/
113
var rating = (posX - posXOverUnit) / unitWidth + 1
115
/* If posX under half the star's width, remove 0.5 from the rating */
116
if( posXOverUnit <= (starIconSize/2) ){
117
rating = rating - 0.5
119
return clamp( rating, 0, size )
124
onPressed: rating = calculateRating(mouseX)
126
if (pressed) rating = calculateRating(mouseX)