Movie list view: add poster view mode, shows a 5x2 poster grid
[cinaest] / src / movie-list-view.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 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 using Hildon;
21
22 public class MovieListView : PannableArea {
23         public MovieListStore store;
24         TreeView tree;
25         IconView icons;
26
27         private bool more_movies_available;
28         private CellRendererText title_renderer;
29         private CellRendererText secondary_renderer;
30         private CellRendererText rating_renderer;
31         private CellRendererText date_renderer;
32
33         private bool poster_mode_;
34         public bool poster_mode {
35                 get {
36                         return poster_mode_;
37                 }
38                 set {
39                         if (value & !poster_mode_) {
40                                 remove (tree);
41                                 add (icons);
42                         } else if (!value & poster_mode_) {
43                                 remove (icons);
44                                 add (tree);
45                         }
46                         poster_mode_ = value;
47                 }
48         }
49
50         public signal void movie_activated (Movie movie);
51
52         private Gtk.TreeView create_treeview (Gtk.Window window, bool show_date) {
53                 // Tree View
54                 var tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
55                 tree.set_headers_visible (false);
56                 tree.set_rules_hint (true);
57
58                 // Tree selection object
59                 var selection = tree.get_selection ();
60                 selection.set_mode (SelectionMode.SINGLE);
61
62                 // Title column with poster
63                 var title_column = new TreeViewColumn ();
64                 title_column.set_title (_("Movie"));
65                 title_column.set_sort_column_id (MovieListStore.Columns.TITLE);
66                 title_column.set_reorderable (false);
67                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
68                 title_column.set_expand (true);
69
70                 // Add poster icon to column
71                 var pixbuf_renderer = new CellRendererPixbuf ();
72                 pixbuf_renderer.width = 64;
73                 pixbuf_renderer.xalign = 0.0f;
74                 title_column.pack_start (pixbuf_renderer, false);
75                 title_column.add_attribute (pixbuf_renderer, "pixbuf", MovieListStore.Columns.ICON);
76
77                 // Add text to column
78                 var vbox_renderer = new CellRendererVBox ();
79
80                 title_renderer = new CellRendererText ();
81                 title_renderer.yalign = 1.0f;
82                 title_renderer.ellipsize = Pango.EllipsizeMode.END;
83
84                 vbox_renderer.append (title_renderer, true);
85
86                 // Add secondary text to column (Genres, Director, etc.)
87                 secondary_renderer = new CellRendererText ();
88                 secondary_renderer.yalign = 0;
89                 secondary_renderer.ellipsize = Pango.EllipsizeMode.END;
90                 secondary_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
91
92                 vbox_renderer.append (secondary_renderer, true);
93
94                 title_column.pack_start (vbox_renderer, true);
95                 title_column.set_cell_data_func (vbox_renderer, title_data_func);
96
97                 tree.append_column (title_column);
98
99                 // Year column
100                 var year_column = new TreeViewColumn ();
101                 year_column.set_title (_("Year"));
102                 year_column.set_sort_column_id (MovieListStore.Columns.YEAR);
103                 year_column.set_reorderable (false);
104                 year_column.set_sort_order (SortType.DESCENDING);
105                 tree.append_column (year_column);
106
107                 // Rating column
108                 var rating_column = new TreeViewColumn ();
109                 rating_column.set_title (_("Rating"));
110                 rating_column.set_sort_column_id (MovieListStore.Columns.RATING);
111                 rating_column.set_reorderable (false);
112                 rating_column.set_sort_order (SortType.DESCENDING);
113                 rating_column.xalign = 1.0f;
114
115                 vbox_renderer = new CellRendererVBox ();
116
117                 rating_renderer = new CellRendererText ();
118                 rating_renderer.xalign = 1.0f;
119                 if (show_date)
120                         rating_renderer.yalign = 1.0f;
121
122                 vbox_renderer.append (rating_renderer, true);
123
124                 date_renderer = new CellRendererText ();
125                 date_renderer.yalign = 0;
126                 date_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
127
128                 if (show_date)
129                         vbox_renderer.append (date_renderer, true);
130
131                 rating_column.pack_start (vbox_renderer, true);
132                 rating_column.set_cell_data_func (vbox_renderer, rating_data_func);
133
134                 tree.append_column (rating_column);
135                 tree.show ();
136                 return tree;
137         }
138
139         private Gtk.IconView create_iconview () {
140                 var iconview = (Gtk.IconView) Hildon.gtk_icon_view_new_with_model (Hildon.UIMode.NORMAL, store);
141                 iconview.set_column_spacing (0);
142                 iconview.set_pixbuf_column (MovieListStore.Columns.POSTER);
143                 iconview.margin = 0;
144                 iconview.item_width = Poster.SMALL_WIDTH;
145                 iconview.column_spacing = Hildon.MARGIN_HALF;
146                 iconview.row_spacing = Hildon.MARGIN_HALF;
147                 iconview.show ();
148
149                 return iconview;
150         }
151
152         public MovieListView (Gtk.Window window, bool show_date = false) {
153                 store = new MovieListStore ();
154
155                 // Sort by title
156                 store.set_sort_column_id (MovieListStore.Columns.TITLE, SortType.ASCENDING);
157
158                 Gdk.Color color;
159                 window.ensure_style ();
160                 if (window.style.lookup_color ("SecondaryTextColor", out color)) {
161                         store.year_markup = "<span size=\"small\" fgcolor=\"%s\">(%%d)</span>".printf (color.to_string ());
162                 }
163
164                 tree = create_treeview (window, show_date);
165
166                 icons = create_iconview ();
167
168                 add (tree);
169
170                 // Connect signals
171                 get_vadjustment ().value_changed.connect (on_adjustment_value_changed);
172                 tree.row_activated.connect (on_row_activated);
173                 icons.item_activated.connect (on_item_activated);
174                 store.search_finished.connect (on_search_finished);
175         }
176
177         construct {
178                 hscrollbar_policy = Gtk.PolicyType.NEVER;
179         }
180
181         public void set_hildon_ui_mode (UIMode mode) {
182                 var selection = tree.get_selection ();
183
184                 if (mode == UIMode.NORMAL) {
185                         selection.set_mode (SelectionMode.NONE);
186                         icons.set_selection_mode (SelectionMode.NONE);
187                 }
188                 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
189                 Hildon.gtk_icon_view_set_ui_mode (icons, mode);
190                 if (mode == UIMode.EDIT) {
191                         selection.set_mode (SelectionMode.MULTIPLE);
192                         icons.set_selection_mode (SelectionMode.MULTIPLE);
193                 }
194         }
195
196         private Pango.AttrList get_attributes (Gtk.Window window, string font_name, string color_name) {
197                 Pango.AttrList attr_list = new Pango.AttrList ();
198                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), font_name, null, typeof (void));
199                 if (style != null) {
200                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
201                         attr_list.insert ((owned) attr_font_desc);
202                 }
203                 Gdk.Color color;
204                 window.ensure_style ();
205                 if (window.style.lookup_color (color_name, out color)) {
206                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
207                         attr_list.insert ((owned) attr_color);
208                 }
209                 return attr_list;
210         }
211
212         public void unselect_all () {
213                 tree.get_selection ().unselect_all ();
214                 icons.unselect_all ();
215         }
216
217         public List<Movie> get_selected_movies () {
218                 var movies = new List<Movie> ();
219                 List<TreePath> paths;
220
221                 if (poster_mode_)
222                         paths = icons.get_selected_items ();
223                 else
224                         paths = tree.get_selection ().get_selected_rows (null);
225
226                 // get selected movies from the store
227                 foreach (TreePath path in paths) {
228                         TreeIter iter;
229
230                         if (store.get_iter (out iter, path)) {
231                                 Movie movie;
232
233                                 store.get (iter, MovieListStore.Columns.MOVIE, out movie);
234                                 if (movie != null) {
235                                         movies.append (movie);
236                                 }
237                         }
238                 }
239
240                 return movies;
241         }
242
243         // TODO: after scrolling down 80% of the list, load more
244         //       results if available.
245         private void on_adjustment_value_changed () {
246                 if (more_movies_available) {
247                         var vadj = get_vadjustment ();
248                         if (vadj.value > 0.8 * vadj.upper) {
249                                 Banner.show_information (this, null, _("More results available - refine search to reduce the dataset"));
250                                 more_movies_available = false;
251                         }
252                 }
253         }
254
255         private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
256                 on_item_activated (path);
257         }
258
259         private void on_item_activated (TreePath path) {
260                 TreeIter iter;
261
262                 if (store.get_iter (out iter, path)) {
263                         Movie movie;
264                         store.get (iter, MovieListStore.Columns.MOVIE, out movie);
265                         movie_activated (movie);
266                 }
267         }
268
269         private void on_search_finished (int movies) {
270                 more_movies_available = (movies > 100); // FIXME
271         }
272
273         private void title_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
274                 Movie movie;
275                 string markup;
276
277                 model.get (iter, MovieListStore.Columns.MOVIE, out movie,
278                                  MovieListStore.Columns.MARKUP, out markup);
279                 title_renderer.markup = markup;
280                 secondary_renderer.text = movie.secondary;
281         }
282
283         private void rating_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
284                 string rating;
285                 Movie movie;
286
287                 model.get (iter, MovieListStore.Columns.RATING, out rating,
288                                  MovieListStore.Columns.MOVIE, out movie);
289                 rating_renderer.text = rating;
290                 if (movie.julian_date != 0) {
291                         var date = Date ();
292                         date.set_julian (movie.julian_date);
293                         var s = new char[64];
294                         date.strftime (s, "%x");
295                         date_renderer.text = (string) s;
296                 } else {
297                         date_renderer.text = "";
298                 }
299         }
300 }