Movie list view: remove unneeded sort and filter wrappers for the list store
[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
26         private bool more_movies_available;
27         private CellRendererText title_renderer;
28         private CellRendererText secondary_renderer;
29         private CellRendererText rating_renderer;
30         private CellRendererText date_renderer;
31
32         public signal void movie_activated (Movie movie);
33
34         public MovieListView (Gtk.Window window, bool show_date = false) {
35                 store = new MovieListStore ();
36
37                 Gdk.Color color;
38                 window.ensure_style ();
39                 if (window.style.lookup_color ("SecondaryTextColor", out color)) {
40                         store.year_markup = "<span size=\"small\" fgcolor=\"%s\">(%%d)</span>".printf (color.to_string ());
41                 }
42
43                 // Tree View
44                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
45                 tree.set_headers_visible (false);
46
47                 add (tree);
48
49                 tree.set_rules_hint (true);
50
51                 // Tree selection object
52                 var selection = tree.get_selection ();
53                 selection.set_mode (SelectionMode.SINGLE);
54
55                 // Title column with poster
56                 var title_column = new TreeViewColumn ();
57                 title_column.set_title (_("Movie"));
58                 title_column.set_sort_column_id (MovieListStore.Columns.TITLE);
59                 title_column.set_reorderable (false);
60                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
61                 title_column.set_expand (true);
62
63                 // Add poster icon to column
64                 var pixbuf_renderer = new CellRendererPixbuf ();
65                 pixbuf_renderer.width = 64;
66                 pixbuf_renderer.xalign = 0.0f;
67                 title_column.pack_start (pixbuf_renderer, false);
68                 title_column.add_attribute (pixbuf_renderer, "pixbuf", MovieListStore.Columns.POSTER);
69
70                 // Add text to column
71                 var vbox_renderer = new CellRendererVBox ();
72
73                 title_renderer = new CellRendererText ();
74                 title_renderer.yalign = 1.0f;
75                 title_renderer.ellipsize = Pango.EllipsizeMode.END;
76
77                 vbox_renderer.append (title_renderer, true);
78
79                 // Add secondary text to column (Genres, Director, etc.)
80                 secondary_renderer = new CellRendererText ();
81                 secondary_renderer.yalign = 0;
82                 secondary_renderer.ellipsize = Pango.EllipsizeMode.END;
83                 secondary_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
84
85                 vbox_renderer.append (secondary_renderer, true);
86
87                 title_column.pack_start (vbox_renderer, true);
88                 title_column.set_cell_data_func (vbox_renderer, title_data_func);
89
90                 tree.append_column (title_column);
91
92                 // Sort by title
93                 store.set_sort_column_id (MovieListStore.Columns.TITLE, SortType.ASCENDING);
94
95                 // Year column
96                 var year_column = new TreeViewColumn ();
97                 year_column.set_title (_("Year"));
98                 year_column.set_sort_column_id (MovieListStore.Columns.YEAR);
99                 year_column.set_reorderable (false);
100                 year_column.set_sort_order (SortType.DESCENDING);
101                 tree.append_column (year_column);
102
103                 // Rating column
104                 var rating_column = new TreeViewColumn ();
105                 rating_column.set_title (_("Rating"));
106                 rating_column.set_sort_column_id (MovieListStore.Columns.RATING);
107                 rating_column.set_reorderable (false);
108                 rating_column.set_sort_order (SortType.DESCENDING);
109                 rating_column.xalign = 1.0f;
110
111                 vbox_renderer = new CellRendererVBox ();
112
113                 rating_renderer = new CellRendererText ();
114                 rating_renderer.xalign = 1.0f;
115                 if (show_date)
116                         rating_renderer.yalign = 1.0f;
117
118                 vbox_renderer.append (rating_renderer, true);
119
120                 date_renderer = new CellRendererText ();
121                 date_renderer.yalign = 0;
122                 date_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
123
124                 if (show_date)
125                         vbox_renderer.append (date_renderer, true);
126
127                 rating_column.pack_start (vbox_renderer, true);
128                 rating_column.set_cell_data_func (vbox_renderer, rating_data_func);
129
130                 tree.append_column (rating_column);
131
132                 // Connect signals
133                 get_vadjustment ().value_changed.connect (on_adjustment_value_changed);
134                 tree.row_activated.connect (on_row_activated);
135                 store.search_finished.connect (on_search_finished);
136         }
137
138         public void set_hildon_ui_mode (UIMode mode) {
139                 var selection = tree.get_selection ();
140
141                 if (mode == UIMode.NORMAL) {
142                         selection.set_mode (SelectionMode.NONE);
143                 }
144                 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
145                 if (mode == UIMode.EDIT) {
146                         selection.set_mode (SelectionMode.MULTIPLE);
147                 }
148         }
149
150         public unowned TreeSelection get_selection () {
151                 return tree.get_selection ();
152         }
153
154         private Pango.AttrList get_attributes (Gtk.Window window, string font_name, string color_name) {
155                 Pango.AttrList attr_list = new Pango.AttrList ();
156                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), font_name, null, typeof (void));
157                 if (style != null) {
158                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
159                         attr_list.insert ((owned) attr_font_desc);
160                 }
161                 Gdk.Color color;
162                 window.ensure_style ();
163                 if (window.style.lookup_color (color_name, out color)) {
164                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
165                         attr_list.insert ((owned) attr_color);
166                 }
167                 return attr_list;
168         }
169
170         // TODO: after scrolling down 80% of the list, load more
171         //       results if available.
172         private void on_adjustment_value_changed () {
173                 if (more_movies_available) {
174                         var vadj = get_vadjustment ();
175                         if (vadj.value > 0.8 * vadj.upper) {
176                                 Banner.show_information (this, null, _("More results available - refine search to reduce the dataset"));
177                                 more_movies_available = false;
178                         }
179                 }
180         }
181
182         private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
183                 TreeModel model = tree.model;
184                 TreeIter iter;
185
186                 if (model.get_iter (out iter, path)) {
187                         Movie movie;
188                         model.get (iter, MovieListStore.Columns.MOVIE, out movie);
189                         movie_activated (movie);
190                 }
191         }
192
193         private void on_search_finished (int movies) {
194                 more_movies_available = (movies > 100); // FIXME
195         }
196
197         private void title_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
198                 Movie movie;
199                 string markup;
200
201                 model.get (iter, MovieListStore.Columns.MOVIE, out movie,
202                                  MovieListStore.Columns.MARKUP, out markup);
203                 title_renderer.markup = markup;
204                 secondary_renderer.text = movie.secondary;
205         }
206
207         private void rating_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
208                 string rating;
209                 Movie movie;
210
211                 model.get (iter, MovieListStore.Columns.RATING, out rating,
212                                  MovieListStore.Columns.MOVIE, out movie);
213                 rating_renderer.text = rating;
214                 if (movie.julian_date != 0) {
215                         var date = Date ();
216                         date.set_julian (movie.julian_date);
217                         var s = new char[64];
218                         date.strftime (s, "%x");
219                         date_renderer.text = (string) s;
220                 } else {
221                         date_renderer.text = "";
222                 }
223         }
224 }