~fboucault/unity-2d/dash_lens_bar_robust_icon

« back to all changes in this revision

Viewing changes to places/RatingStars.qml

  • Committer: Florian Boucault
  • Date: 2011-08-25 14:25:43 UTC
  • mfrom: (653.2.41 backend_filters)
  • Revision ID: florian@boucault.net-20110825142543-z60gvz5unwgw1fjv
[dash] New filters pane on the right hand side of the search results.

Implemented filters:
- multirange
- checkoption
- ratings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-2d
 
3
 *
 
4
 * Copyright 2010-2011 Canonical Ltd.
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
import QtQuick 1.0
 
20
 
 
21
Item {
 
22
    id: ratingStars
 
23
 
 
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
 
28
 
 
29
    /* Configure icon size to use. Requires icon files named:
 
30
       - artwork/star_empty-${starIconSize}.png
 
31
       - artwork/star_full-${starIconSize}.png
 
32
    */
 
33
    property int starIconSize: 32
 
34
 
 
35
    width: stars.width
 
36
    height: stars.height
 
37
 
 
38
    function clamp(x, min, max) {
 
39
        return Math.max(Math.min(x, max), min)
 
40
    }
 
41
 
 
42
    function incrementRating() {
 
43
        /* Make math easier since half rating accepted */
 
44
        var double_rating = rating * 2
 
45
 
 
46
        if ( double_rating % 1 > 0){ /* if non-integer, round up */
 
47
            double_rating = Math.ceil(double_rating)
 
48
        }
 
49
        else {
 
50
            double_rating = clamp(double_rating+1, 0, size * 2)
 
51
        }
 
52
        rating = double_rating / 2
 
53
    }
 
54
 
 
55
    function decrementRating() {
 
56
        var double_rating = rating * 2
 
57
 
 
58
        if ( double_rating % 1 > 0){ /* if non-integer, round down */
 
59
            double_rating = Math.ceil(double_rating)
 
60
        }
 
61
        else {
 
62
            double_rating = clamp(double_rating-1, 0, size * 2)
 
63
        }
 
64
        rating = double_rating / 2
 
65
    }
 
66
 
 
67
    Keys.onPressed: if (handleKeyPress(event.key)) event.accepted = true
 
68
    function handleKeyPress(key) {
 
69
        switch (key) {
 
70
        case Qt.Key_Right:
 
71
            incrementRating()
 
72
            return true
 
73
        case Qt.Key_Left:
 
74
            decrementRating()
 
75
            return true
 
76
        }
 
77
        return false
 
78
    }
 
79
 
 
80
    Row {
 
81
        id: stars
 
82
 
 
83
        Repeater {
 
84
            model: size
 
85
            Star {
 
86
                fill: clamp(rating - index, 0, 1)
 
87
                iconSize: starIconSize
 
88
                selected: ( ratingStars.activeFocus )
 
89
            }
 
90
        }
 
91
    }
 
92
 
 
93
    MouseArea {
 
94
        id: starsMouseArea
 
95
 
 
96
        /* Calculating Star Rating incorporating inter-star spacing/gap.
 
97
 
 
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.
 
100
          */
 
101
 
 
102
        /* Width of each unit */
 
103
        property int unitWidth: starIconSize + stars.spacing
 
104
 
 
105
        function calculateRating( posX ){
 
106
            /* Small left-hand edge to set zero rating */
 
107
            if( posX < 4 ) return 0
 
108
 
 
109
            /* Mouse X coordinate over one unit, relative to that unit's left edge*/
 
110
            var posXOverUnit = posX % unitWidth
 
111
 
 
112
            /* What unit is the mouse over? This is the integer part of the rating (plus one)*/
 
113
            var rating = (posX - posXOverUnit) / unitWidth + 1
 
114
 
 
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
 
118
            }
 
119
            return clamp( rating, 0, size )
 
120
        }
 
121
 
 
122
        anchors.fill: stars
 
123
 
 
124
        onPressed: rating = calculateRating(mouseX)
 
125
        onPositionChanged: {
 
126
            if (pressed) rating = calculateRating(mouseX)
 
127
        }
 
128
    }
 
129
}