Add movie object and movie 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         MovieListStore store;
24         TreeView tree;
25         public TreeSortable sorted_store;
26
27         construct {
28                 store = new MovieListStore ();
29
30                 // FIXME - fill store with test data
31                 int i;
32                 for (i = 0; i < 10; i++) {
33                         var m = new Movie ();
34                         m.title = "Test %d".printf (i);
35                         m.year = 2000 - i;
36                         m.rating = 10 * (i % 5) + i;
37                         TreeIter iter;
38                         store.add (m, out iter);
39                 }
40
41                 // Add filter wrapper
42                 var filtered_store = new TreeModelFilter (store, null);
43
44                 // Add sort wrapper
45                 sorted_store = new TreeModelSort.with_model (filtered_store);
46
47                 // Tree View
48                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, sorted_store);
49                 tree.set_headers_visible (false);
50
51                 add (tree);
52
53                 tree.set_rules_hint (true);
54
55                 // Tree selection object
56                 var selection = tree.get_selection ();
57                 selection.set_mode (SelectionMode.SINGLE);
58
59                 // Title column with poster
60                 var title_column = new TreeViewColumn ();
61                 title_column.set_title ("Movie");
62                 title_column.set_sort_column_id (MovieListStore.Columns.TITLE);
63                 title_column.set_reorderable (false);
64                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
65                 title_column.set_expand (true);
66
67                 // Add poster icon to column
68                 var pixbuf_renderer = new CellRendererPixbuf ();
69                 title_column.pack_start (pixbuf_renderer, false);
70                 title_column.add_attribute (pixbuf_renderer, "pixbuf", MovieListStore.Columns.POSTER);
71
72                 // Add text to column
73                 var renderer = new CellRendererText ();
74                 renderer.set ("ellipsize", Pango.EllipsizeMode.END);
75                 title_column.pack_start (renderer, true);
76                 title_column.add_attribute (renderer, "text", MovieListStore.Columns.TITLE);
77                 tree.append_column (title_column);
78
79                 // Sort by title
80                 sorted_store.set_sort_column_id (MovieListStore.Columns.TITLE, SortType.ASCENDING);
81
82                 // Year column
83                 renderer = new CellRendererText ();
84                 var year_column = new TreeViewColumn.with_attributes ("Year", renderer, "text", MovieListStore.Columns.YEAR);
85                 year_column.set_sort_column_id (MovieListStore.Columns.YEAR);
86                 year_column.set_reorderable (false);
87                 year_column.set_sort_order (SortType.DESCENDING);
88                 tree.append_column (year_column);
89
90                 // Rating column
91                 renderer = new CellRendererText ();
92                 var rating_column = new TreeViewColumn.with_attributes ("Rating", renderer, "text", MovieListStore.Columns.RATING);
93                 rating_column.set_sort_column_id (MovieListStore.Columns.RATING);
94                 rating_column.set_reorderable (false);
95                 rating_column.set_sort_order (SortType.DESCENDING);
96                 rating_column.set_cell_data_func (renderer, rating_data_func);
97                 rating_column.xalign = (float) 1.0;
98                 tree.append_column (rating_column);
99         }
100
101         private void rating_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
102                 int rating;
103
104                 model.get (iter, MovieListStore.Columns.RATING, out rating);
105                 ((CellRendererText) cell).text = "%d.%d".printf (rating / 10, rating % 10);
106         }
107 }