From: Philipp Zabel Date: Wed, 14 Jul 2010 21:20:40 +0000 (+0200) Subject: Rating widget: switch range to 0-100 for 0-10 stars, allow half stars X-Git-Url: http://git.maemo.org/git/?p=cinaest;a=commitdiff_plain;h=d9edb26f6cf6d89dd02c7eea7d6a2f6704b24264 Rating widget: switch range to 0-100 for 0-10 stars, allow half stars --- diff --git a/src/movie-menu.vala b/src/movie-menu.vala index a3e2544..b3ae17b 100644 --- a/src/movie-menu.vala +++ b/src/movie-menu.vala @@ -77,7 +77,7 @@ public class MovieMenu : AppMenu { dialog.destroy (); if (res == Gtk.ResponseType.OK) { movie.julian_date = get_julian_date (date); - movie.rating = 10 * rating.get_rating (); + movie.rating = rating.get_rating (); Banner.show_information (parent_window, null, _("Rated movie '%s': %.1f").printf (movie.title, movie.rating/10.0)); foreach (Plugin plugin in CinaestProgram.plugins) { foreach (MovieSource source in plugin.get_sources ()) { diff --git a/src/rating-widget.vala b/src/rating-widget.vala index 70b27f4..d432b48 100644 --- a/src/rating-widget.vala +++ b/src/rating-widget.vala @@ -40,19 +40,28 @@ public class RatingWidget : DrawingArea { public override bool expose_event (Gdk.EventExpose event) { var cr = Gdk.cairo_create (this.window); + var radius = double.min (this.allocation.width / 20, + this.allocation.height / 2); cr.rectangle (event.area.x, event.area.y, event.area.width, event.area.height); cr.clip (); - for (int i = 0; i < 10; i++) { - draw_star (cr, i, (i >= rating) ? disabled_color : active_color); - } + cr.save (); + cr.rectangle (radius * rating / 5.0, 0, this.allocation.width - radius * rating / 5.0, this.allocation.height); + cr.clip (); + for (int i = rating/10; i < 10; i++) + draw_star (cr, i, radius, disabled_color); + cr.restore (); + + cr.new_path (); + cr.rectangle (0, 0, radius * rating / 5.0, this.allocation.height); + cr.clip (); + for (int i = 0; i < (rating+5)/10; i++) + draw_star (cr, i, radius, active_color); return false; } - private void draw_star (Cairo.Context cr, int n, Gdk.Color color) { - var radius = double.min (this.allocation.width / 20, - this.allocation.height / 2); + private void draw_star (Cairo.Context cr, int n, double radius, Gdk.Color color) { var x = radius * (2 * n + 1); var y = this.allocation.height / 2; @@ -73,7 +82,7 @@ public class RatingWidget : DrawingArea { public override bool button_press_event (Gdk.EventButton event) { button_pressed = true; - rate (10 * event.x / this.allocation.width); + rate (event.x); return false; } @@ -86,18 +95,24 @@ public class RatingWidget : DrawingArea { public override bool motion_notify_event (Gdk.EventMotion event) { if (button_pressed) - rate (10 * event.x / this.allocation.width); + rate (event.x); return false; } - private void rate (double r) { - if (r <= 0) + private void rate (double x) { + double r = 10 * x / this.allocation.width; + if (r <= 0) { rating = 0; - else - rating = (int) r + 1; - if (rating > 10) - rating = 10; + } else { + int star = (int) r; + rating = 10 * (star + 1); + r -= star; + if (r <= 0.333) + rating -= 5; + if (rating > 100) + rating = 100; + } unowned Gdk.Region region = this.window.get_clip_region (); // redraw the cairo canvas completely by exposing it