EventsDirectoryPage.vala 5.98 KB
Newer Older
1 2 3 4 5
/* Copyright 2009 Yorba Foundation
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution. 
 */
6

7
public class DirectoryItem : LayoutItem, EventSource {
8
    public const int SCALE =
9 10 11
        ThumbnailCache.MEDIUM_SCALE + ((ThumbnailCache.BIG_SCALE - ThumbnailCache.MEDIUM_SCALE) / 2);
    
    public EventID event_id;
12 13

    public DirectoryItem(EventID event_id, EventTable event_table) {
14 15
        this.event_id = event_id;
        
16
        set_title(event_table.get_name(event_id));
17 18
        
        PhotoID photo_id = event_table.get_primary_photo(event_id);
19
        assert(photo_id.is_valid());
20
        
21 22
        LibraryPhoto photo = LibraryPhoto.fetch(photo_id);
        Gdk.Pixbuf pixbuf = photo.get_preview_pixbuf(SCALE);
23

24
        set_image(pixbuf);
25
    }
26

27 28
    public time_t get_start_time() {
        return (new EventTable()).get_start_time(event_id);
29 30
    }

31 32
    public time_t get_end_time() {
        return (new EventTable()).get_start_time(event_id);
33 34
    }

35
    public Gee.Iterable<PhotoSource> get_photos() {
36
        Gee.ArrayList<PhotoID?> photo_ids = (new PhotoTable()).get_event_photos(event_id);
37
        Gee.ArrayList<LibraryPhoto> photos = new Gee.ArrayList<LibraryPhoto>();
38
        foreach (PhotoID photo_id in photo_ids) {
39
            photos.add(LibraryPhoto.fetch(photo_id));
40 41 42 43
        }
        return photos;
    }

44
    public int get_photo_count() {
45 46
        return (new PhotoTable()).get_event_photos(event_id).size;
    }
47 48 49
}

public class EventsDirectoryPage : CheckerboardPage {
50 51 52 53 54
    private class CompareEventItem : Comparator<DirectoryItem> {
        private EventTable event_table;
        private int sort;
        
        public CompareEventItem(EventTable event_table, int sort) {
55 56
            assert(sort == LibraryWindow.SORT_EVENTS_ORDER_ASCENDING 
                || sort == LibraryWindow.SORT_EVENTS_ORDER_DESCENDING);
57 58 59 60 61 62 63 64 65 66
            
            this.event_table = event_table;
            this.sort = sort;
        }
        
        public override int64 compare(DirectoryItem a, DirectoryItem b) {
            int64 start_a = (int64) event_table.get_start_time(a.event_id);
            int64 start_b = (int64) event_table.get_start_time(b.event_id);
            
            switch (sort) {
67
                case LibraryWindow.SORT_EVENTS_ORDER_ASCENDING:
68 69
                    return start_a - start_b;
                
70
                case LibraryWindow.SORT_EVENTS_ORDER_DESCENDING:
71 72 73 74 75 76
                default:
                    return start_b - start_a;
            }
        }
    }
    
77 78 79
    // TODO: Mark fields for translation
    private const Gtk.ActionEntry[] ACTIONS = {
        { "FileMenu", null, "_File", null, null, null },
80 81
        
        { "ViewMenu", null, "_View", null, null, on_view_menu },
82 83 84 85 86 87 88 89

        { "HelpMenu", null, "_Help", null, null, null }
    };
    
    private EventTable event_table = new EventTable();
    private Gtk.Toolbar toolbar = new Gtk.Toolbar();

    public EventsDirectoryPage() {
90
        base("Events");
Jim Nelson's avatar
Jim Nelson committed
91
        
92 93 94 95 96
        init_ui_start("events_directory.ui", "EventsDirectoryActionGroup", ACTIONS);
        init_ui_bind("/EventsDirectoryMenuBar");
        
        // scrollbar policy
        set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
97
        
98
        set_layout_comparator(new CompareEventItem(event_table, LibraryWindow.get_app().get_events_sort()));
99 100 101 102 103 104 105 106
    }
    
    public override Gtk.Toolbar get_toolbar() {
        return toolbar;
    }
    
    public override void on_item_activated(LayoutItem item) {
        DirectoryItem event = (DirectoryItem) item;
107
        LibraryWindow.get_app().switch_to_event(event.event_id);
108 109
    }
    
110 111 112 113 114 115 116 117 118 119 120
    public override LayoutItem? get_fullscreen_photo() {
        Gee.Iterable<LayoutItem> iter = null;
        
        // use first selected item, otherwise use first item
        if (get_selected_count() > 0) {
            iter = get_selected();
        } else {
            iter = get_items();
        }
        
        foreach (LayoutItem item in iter) {
121
            EventPage page = LibraryWindow.get_app().find_event_page(((DirectoryItem) item).event_id);
122 123 124 125 126 127 128
            if (page != null)
                return page.get_fullscreen_photo();
        }
        
        return null;
    }
    
129
    public void add_event(EventID event_id) {
130
        DirectoryItem item = new DirectoryItem(event_id, event_table);
131 132 133 134
        add_item(item);
    }
    
    public override void switched_to() {
135 136
        base.switched_to();

137 138
        remove_all();
        
139 140
        Gee.ArrayList<EventID?> event_ids = event_table.get_events();
        foreach (EventID event_id in event_ids)
141 142 143 144 145 146
            add_event(event_id);
        
        show_all();

        refresh();
    }
147
    
148 149 150 151 152
    public void notify_sort_changed(int sort) {
        set_layout_comparator(new CompareEventItem(event_table, sort));
        refresh();
    }
    
153 154 155
    private void on_view_menu() {
        set_item_sensitive("/EventsDirectoryMenuBar/ViewMenu/Fullscreen", get_count() > 0);
    }
156 157 158 159 160
}

public class EventPage : CollectionPage {
    public EventID event_id;
    
161 162 163
    private EventTable event_table = new EventTable();
    
    private const Gtk.ActionEntry[] ACTIONS = {
164
        { "MakePrimary", Resources.MAKE_PRIMARY, "Make _Key Photo for Event", null, null, on_make_primary }
165 166
    };

167
    public EventPage(EventID event_id) {
168
        base("Event", "event.ui", ACTIONS);
169 170
        
        this.event_id = event_id;
171 172

        set_page_name(event_table.get_name(event_id));
Jim Nelson's avatar
Jim Nelson committed
173 174
    }
    
175 176 177 178 179 180 181 182 183 184 185 186
    protected override void on_photos_menu() {
        set_item_sensitive("/CollectionMenuBar/PhotosMenu/MakePrimary", get_selected_count() == 1);
        
        base.on_photos_menu();
    }
    
    private void on_make_primary() {
        assert(get_selected_count() == 1);
        
        // iterate to first one, use that, bail out
        foreach (LayoutItem item in get_selected()) {
            Thumbnail thumbnail = (Thumbnail) item;
187
            event_table.set_primary_photo(event_id, thumbnail.get_photo().get_photo_id());
188 189 190 191
            
            break;
        }
    }
192 193
}