From d260a9c1ceb6458a73a32c1d00793be75cdb7975 Mon Sep 17 00:00:00 2001 From: Philipp Zabel Date: Wed, 14 Jul 2010 23:23:26 +0200 Subject: [PATCH] Rating widget: turn rating into a property --- src/movie-menu.vala | 2 +- src/rating-widget.vala | 51 +++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/movie-menu.vala b/src/movie-menu.vala index b3ae17b..a6f6a40 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 = rating.get_rating (); + movie.rating = rating.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 d432b48..cafb73a 100644 --- a/src/rating-widget.vala +++ b/src/rating-widget.vala @@ -19,11 +19,28 @@ using Gtk; public class RatingWidget : DrawingArea { - private int rating; + private int _rating; private bool button_pressed; private Gdk.Color active_color; private Gdk.Color disabled_color; + public int rating { + get { + return _rating; + } + set { + if (_rating == value) + return; + + _rating = value; + + unowned Gdk.Region region = this.window.get_clip_region (); + // redraw the cairo canvas completely by exposing it + this.window.invalidate_region (region, true); + this.window.process_updates (true); + } + } + public RatingWidget () { var style = Gtk.rc_get_style_by_paths (this.get_settings (), "GtkButton", null, typeof (Gtk.Button)); if (!style.lookup_color ("ActiveTextColor", out active_color)) @@ -101,26 +118,20 @@ public class RatingWidget : DrawingArea { } private void rate (double x) { - double r = 10 * x / this.allocation.width; - if (r <= 0) { - rating = 0; + int r; + + x = 10 * x / this.allocation.width; + if (x <= 0) { + r = 0; } else { - int star = (int) r; - rating = 10 * (star + 1); - r -= star; - if (r <= 0.333) - rating -= 5; - if (rating > 100) - rating = 100; + int star = (int) x; + r = 10 * (star + 1); + x -= star; + if (x <= 0.333) + r -= 5; + if (r > 100) + r = 100; } - - unowned Gdk.Region region = this.window.get_clip_region (); - // redraw the cairo canvas completely by exposing it - this.window.invalidate_region (region, true); - this.window.process_updates (true); - } - - public int get_rating () { - return rating; + rating = r; } } -- 1.7.9.5