Rating widget: switch range to 0-100 for 0-10 stars, allow half stars
[cinaest] / src / rating-widget.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Cinaest is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest 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 Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20
21 public class RatingWidget : DrawingArea {
22         private int rating;
23         private bool button_pressed;
24         private Gdk.Color active_color;
25         private Gdk.Color disabled_color;
26
27         public RatingWidget () {
28                 var style = Gtk.rc_get_style_by_paths (this.get_settings (), "GtkButton", null, typeof (Gtk.Button));
29                 if (!style.lookup_color ("ActiveTextColor", out active_color))
30                         Gdk.Color.parse ("white", out active_color);
31                 if (!style.lookup_color ("DisabledTextColor", out disabled_color))
32                         Gdk.Color.parse ("black", out disabled_color);
33
34                 add_events (Gdk.EventMask.BUTTON_PRESS_MASK
35                             | Gdk.EventMask.BUTTON_RELEASE_MASK
36                             | Gdk.EventMask.POINTER_MOTION_MASK);
37
38                 set_size_request (640, 70);
39         }
40
41         public override bool expose_event (Gdk.EventExpose event) {
42                 var cr = Gdk.cairo_create (this.window);
43                 var radius = double.min (this.allocation.width / 20,
44                                          this.allocation.height / 2);
45                 cr.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
46                 cr.clip ();
47
48                 cr.save ();
49                 cr.rectangle (radius * rating / 5.0, 0, this.allocation.width - radius * rating / 5.0, this.allocation.height);
50                 cr.clip ();
51                 for (int i = rating/10; i < 10; i++)
52                         draw_star (cr, i, radius, disabled_color);
53                 cr.restore ();
54
55                 cr.new_path ();
56                 cr.rectangle (0, 0, radius * rating / 5.0, this.allocation.height);
57                 cr.clip ();
58                 for (int i = 0; i < (rating+5)/10; i++)
59                         draw_star (cr, i, radius, active_color);
60
61                 return false;
62         }
63
64         private void draw_star (Cairo.Context cr, int n, double radius, Gdk.Color color) {
65                 var x = radius * (2 * n + 1);
66                 var y = this.allocation.height / 2;
67
68                 cr.set_source_rgb (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
69                 cr.new_path ();
70                 for (int i = 0; i <= 10; i++) {
71                         double r = radius * (5 + 8 * (i % 2)) / 13.0;
72                         double a = x + r * Math.cos (i * 2*Math.PI/10.0 + Math.PI/2.0);
73                         double b = y + r * Math.sin (i * 2*Math.PI/10.0 + Math.PI/2.0);
74                         if (i == 0)
75                                 cr.move_to (a, b);
76                         else
77                                 cr.line_to (a, b);
78                 }
79                 cr.close_path ();
80                 cr.fill_preserve ();
81         }
82
83         public override bool button_press_event (Gdk.EventButton event) {
84                 button_pressed = true;
85                 rate (event.x);
86
87                 return false;
88         }
89
90         public override bool button_release_event (Gdk.EventButton event) {
91                 button_pressed = false;
92
93                 return false;
94         }
95
96         public override bool motion_notify_event (Gdk.EventMotion event) {
97                 if (button_pressed)
98                         rate (event.x);
99
100                 return false;
101         }
102
103         private void rate (double x) {
104                 double r = 10 * x / this.allocation.width;
105                 if (r <= 0) {
106                         rating = 0;
107                 } else {
108                         int star = (int) r;
109                         rating = 10 * (star + 1);
110                         r -= star;
111                         if (r <= 0.333)
112                                 rating -= 5;
113                         if (rating > 100)
114                                 rating = 100;
115                 }
116
117                 unowned Gdk.Region region = this.window.get_clip_region ();
118                 // redraw the cairo canvas completely by exposing it
119                 this.window.invalidate_region (region, true);
120                 this.window.process_updates (true);
121         }
122
123         public int get_rating () {
124                 return rating;
125         }
126 }