Movie list store & view: move poster loading into list view, consider visibility
authorPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 4 Aug 2010 22:04:55 +0000 (00:04 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 4 Aug 2010 22:04:55 +0000 (00:04 +0200)
After moving the poster loading code into the list view, only
posters for movies in the visible range have to be fetched.

src/movie-list-store.vala
src/movie-list-view.vala

index cd26e0e..b7c85e2 100644 (file)
@@ -181,15 +181,8 @@ public class MovieListStore : ListStore, TreeModel {
                if (cancellable.is_cancelled ())
                        return;
 
-               foreach (Movie movie in movies) {
+               foreach (Movie movie in movies)
                        add (movie, out iter);
-                       try {
-                               poster_factory.queue_thumbnail (movie, Poster.ICON_WIDTH, Poster.ICON_HEIGHT, false, receive_poster_icon);
-                               poster_factory.queue_thumbnail (movie, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT, false, receive_poster_small);
-                       } catch (Error e) {
-                               warning ("Failed to queue poster request: %s\n", e.message);
-                       }
-               }
        }
 
        private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
@@ -220,13 +213,6 @@ public class MovieListStore : ListStore, TreeModel {
        public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
                Movie movie;
 
-               // FIXME
-               if (no_poster == null) try {
-                       no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
-               } catch (Error e) {
-                       critical ("Missing general_video icon: %s\n", e.message);
-               }
-
                return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
 
                // Get the Movie from our parent's storage
@@ -258,17 +244,30 @@ public class MovieListStore : ListStore, TreeModel {
                        break;
 
                case Columns.POSTER:
-                       if ((movie.poster != null) && (movie.poster.small != null))
+                       if ((movie.poster != null) && (movie.poster.small != null)) {
                                value.set_object (movie.poster.small);
-                       else
+                       } else {
+                               // FIXME
+                               if (no_poster == null) try {
+                               //      var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/imageviewer_no_pic.png");
+                                       var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_no_thumbnail.png");
+                                       no_poster = new Gdk.Pixbuf (Gdk.Colorspace.RGB, true, 8, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT);
+                                       no_poster.fill (0);
+                                       no_pic.copy_area (0, 0, no_pic.width, no_pic.height, no_poster,
+                                                         (Poster.SMALL_WIDTH - no_pic.width) / 2, (Poster.SMALL_HEIGHT - no_pic.height) / 2);
+                               } catch (Error e) {
+                                       critical ("Missing general_video icon: %s\n", e.message);
+                               }
                                value.set_object (no_poster);
+                       }
                        break;
 
                case Columns.ICON:
-                       if ((movie.poster != null) && (movie.poster.icon != null))
+                       if ((movie.poster != null) && (movie.poster.icon != null)) {
                                value.set_object (movie.poster.icon);
-                       else
-                               value.set_object (no_poster);
+                       } else {
+                               value.set_object (null);
+                       }
                        break;
 
                case Columns.MOVIE:
index d21b965..d92a535 100644 (file)
@@ -29,6 +29,7 @@ public class MovieListView : PannableArea {
        private CellRendererText secondary_renderer;
        private CellRendererText rating_renderer;
        private CellRendererText date_renderer;
+       private MoviePoster.Factory poster_factory;
 
        private bool poster_mode_;
        public bool poster_mode {
@@ -171,11 +172,14 @@ public class MovieListView : PannableArea {
                get_vadjustment ().value_changed.connect (on_adjustment_value_changed);
                tree.row_activated.connect (on_row_activated);
                icons.item_activated.connect (on_item_activated);
+               store.row_changed.connect (on_row_changed);
                store.search_finished.connect (on_search_finished);
        }
 
        construct {
                hscrollbar_policy = Gtk.PolicyType.NEVER;
+
+               poster_factory = MoviePoster.Factory.get_instance ();
        }
 
        public void set_hildon_ui_mode (UIMode mode) {
@@ -242,6 +246,8 @@ public class MovieListView : PannableArea {
 
        // TODO: after scrolling down 80% of the list, load more
        //       results if available.
+       int last_a = 0;
+       int last_b = 0;
        private void on_adjustment_value_changed () {
                if (more_movies_available) {
                        var vadj = get_vadjustment ();
@@ -250,6 +256,61 @@ public class MovieListView : PannableArea {
                                more_movies_available = false;
                        }
                }
+
+               TreePath a_;
+               TreePath b_;
+               bool range;
+               if (poster_mode_) {
+                       range = icons.get_visible_range (out a_, out b_);
+               } else {
+                       range = tree.get_visible_range (out a_, out b_);
+               }
+               if (range) {
+                       // We know the list store is flat
+                       int a = a_.get_indices ()[0];
+                       int b = b_.get_indices ()[0];
+                       assert (a <= b);
+
+                       if (a == last_a && b == last_b)
+                               return;
+
+                       if (a > last_b || b < last_a) {
+                               check_posters (a, b);
+                       } else if (a >= last_a && b > last_b) {
+                               check_posters (last_b + 1, b);
+                       } else if (b <= last_b && a < last_a) {
+                               check_posters (a, last_a - 1);
+                       }
+
+                       last_a = a;
+                       last_b = b;
+               }
+       }
+
+       private void check_posters (int a, int b) {
+               for (int i = a; i <= b; i++) {
+                       var path = new TreePath.from_indices (i);
+                       TreeIter iter;
+                       if (store.get_iter (out iter, path)) {
+                               Movie movie;
+                               store.get (iter, MovieListStore.Columns.MOVIE, out movie);
+                               if (movie != null) {
+                                       if (poster_mode_) {
+                                               if (movie.poster == null || movie.poster.small == null) try {
+                                                       poster_factory.queue_thumbnail (movie, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT, false, receive_poster_small);
+                                               } catch (Error e) {
+                                                       warning ("Failed to queue poster request: %s\n", e.message);
+                                               }
+                                       } else {
+                                               if (movie.poster == null || movie.poster.icon == null) try {
+                                                       poster_factory.queue_thumbnail (movie, Poster.ICON_WIDTH, Poster.ICON_HEIGHT, false, receive_poster_icon);
+                                               } catch (Error e) {
+                                                       warning ("Failed to queue poster request: %s\n", e.message);
+                                               }
+                                       }
+                               }
+                       }
+               }
        }
 
        private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
@@ -266,6 +327,49 @@ public class MovieListView : PannableArea {
                }
        }
 
+       private void on_row_changed (TreePath path, TreeIter iter) {
+               TreePath a;
+               TreePath b;
+               bool range;
+
+               if (poster_mode_) {
+                       range = icons.get_visible_range (out a, out b);
+               } else {
+                       range = tree.get_visible_range (out a, out b);
+               }
+               if (!range ||
+                   (range && path.compare (a) >= 0 && path.compare (b) <= 0)) {
+                       Movie movie;
+
+                       store.get (iter, MovieListStore.Columns.MOVIE, out movie);
+                       if (movie == null)
+                               return;
+
+                       int i = path.get_indices ()[0];
+                       check_posters (i, i);
+               }
+       }
+
+       private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
+               Poster poster;
+               if (movie.poster != null)
+                       poster = movie.poster;
+               else
+                       poster = new Poster ();
+               poster.small = pixbuf;
+               movie.poster = poster;
+       }
+
+       private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
+               Poster poster;
+               if (movie.poster != null)
+                       poster = movie.poster;
+               else
+                       poster = new Poster ();
+               poster.icon = pixbuf;
+               movie.poster = poster;
+       }
+
        private void on_search_finished (int movies) {
                more_movies_available = (movies > 100); // FIXME
        }