Rating widget: turn rating into a property
authorPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 14 Jul 2010 21:23:26 +0000 (23:23 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 14 Jul 2010 21:23:26 +0000 (23:23 +0200)
src/movie-menu.vala
src/rating-widget.vala

index b3ae17b..a6f6a40 100644 (file)
@@ -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 ()) {
index d432b48..cafb73a 100644 (file)
 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;
        }
 }