~victored/beat-box/miller-columns

« back to all changes in this revision

Viewing changes to src/Views/ListView/MillerColumns/MillerColumn.vala

  • Committer: Victor Eduardo
  • Date: 2012-03-04 20:06:25 UTC
  • Revision ID: victoreduardm@gmail.com-20120304200625-mbu206k74objwxhj
Improve performance by avoiding recursive callbacks

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*-
2
 
 * Copyright (c) 2011-2012       Scott Ringwelski <sgringwe@mtu.edu>
 
2
 * Copyright (c) 2011-2012 BeatBox Developers
3
3
 *
4
 
 * Originally Written by Scott Ringwelski for BeatBox Music Player
5
4
 * BeatBox Music Player: http://www.launchpad.net/beat-box
6
5
 *
7
6
 * This library is free software; you can redistribute it and/or
41
40
        // All the medias
42
41
        public Collection<int> medias {get; private set;}
43
42
 
44
 
        // Displayed medias (search results)
45
 
        public LinkedList<int> media_results;
46
 
        public LinkedList<int> album_results;
 
43
        // Search results
 
44
        private LinkedList<int> _media_results;
 
45
        private LinkedList<int> _album_results;
 
46
 
 
47
        // Filtered media results. We provide the data. No need to search again outside
 
48
        public LinkedList<int> media_results { get {return _media_results;} }
 
49
        public LinkedList<int> album_results { get {return _album_results;} }
47
50
 
48
51
        public LibraryManager lm {get; private set;}
49
52
        public LibraryWindow lw {get; private set;}
50
53
 
51
54
        public ViewWrapper.Hint view_type {get; private set;}
52
55
        public Position position {get; private set; default = Position.AUTOMATIC;}
 
56
 
53
57
        public LinkedList<unowned MillerColumn> columns {get; private set;}
54
58
 
55
59
        private Gtk.Menu column_chooser_menu;
98
102
                top_menu_item = new RadioMenuItem.with_label (left_menu_item.get_group (), _("Top"));
99
103
 
100
104
                (automatic_menu_item as CheckMenuItem).toggled.connect ( () => {
101
 
                        set_columns_position (Position.AUTOMATIC);              
 
105
                        set_columns_position (Position.AUTOMATIC);
102
106
                });
103
107
 
104
108
                (left_menu_item as CheckMenuItem).toggled.connect ( () => {
105
 
                        set_columns_position (Position.LEFT);           
 
109
                        set_columns_position (Position.LEFT);
106
110
                });
107
111
 
108
112
                (top_menu_item as CheckMenuItem).toggled.connect ( () => {
109
 
                        set_columns_position (Position.TOP);            
 
113
                        set_columns_position (Position.TOP);
110
114
                });
111
115
 
112
116
                switch (position) {
134
138
        public void set_columns_position (Position pos) {
135
139
                position = pos;
136
140
 
137
 
                debug ("selected_position = %i", (int) position);
 
141
                debug ("selected_position = %s", position.to_string ());
138
142
 
139
143
                lm.settings.set_miller_columns_position ((int) position);
140
144
 
149
153
                        }
150
154
                }
151
155
 
 
156
                critical ("Couldn't find column %s", type.to_string ());
152
157
                return null;
153
158
        }
154
159
 
176
181
        }
177
182
 
178
183
        private void column_selection_changed (MillerColumn.Category category, string val) {
179
 
 
180
 
                /* FIXME: remove this code. See explanation below in populate_columns() */
 
184
                debug (">>> COLUMN '%s' SELECTION CHANGED. RE-POPULATING MILLERS", category.to_string ());
 
185
 
 
186
                // FIXME: remove this code. Each ViewWrapper will have its own MillerColumns object
 
187
                // and thus we'll use this.view_type instead (of couse it's something that has to
 
188
                // be fixed in ViewWrapper.vala and LibraryWindow.vala). There are serious performance
 
189
                // issues with the current method (re-populating the same miller object).
 
190
 
 
191
                /* @deprecated */
181
192
                Widget w = lw.sideTree.getSelectedWidget();
182
193
                ViewWrapper.Hint hint = ViewWrapper.Hint.MUSIC;
183
194
 
185
196
                        hint = ((ViewWrapper)w).hint;
186
197
                }
187
198
                else {
188
 
                        return; // no need to populate if not viewing viewwrapper
189
 
                }
 
199
                        // no need to populate if not viewing viewwrapper
 
200
                        return;
 
201
                } /* end */
190
202
 
191
203
                /**
192
 
                 * Since the columns follow a hierarchical model, we have to re-populate all the columns
 
204
                 * Since the columns follow a tree model, we have to re-populate all the columns
193
205
                 * that have a lower hierarchical level.
194
206
                 */
195
207
 
199
211
                var search_artist = ""; // ~ All
200
212
                var search_album  = ""; // ~ All
201
213
 
 
214
                debug (">> Getting search info from parent columns");
 
215
 
202
216
                foreach (var col in columns) {
203
217
                        // Higher hierarchical levels (parent columns)
204
218
                        if (col.category < category) {
217
231
                                else if (col.category == MillerColumn.Category.RATING) {
218
232
                                        search_rating = (col.get_selected () == "") ? -1 : int.parse (col.get_selected ());
219
233
                                }
220
 
                        } else if (col.category == category) {
 
234
                        }
 
235
                        else if (col.category == category) {
221
236
                                if (col.category == MillerColumn.Category.GENRE) {
222
237
                                        search_genre = val;
223
238
                                }
238
253
 
239
254
                // Perform search
240
255
 
241
 
                lm.do_search (medias, out media_results, out album_results, null, null, null, hint,
242
 
                              lw.searchField.get_text (),
243
 
                              search_artist, search_album, search_genre, search_year, search_rating);
 
256
                lm.do_search (medias, out _media_results, out _album_results, null, null, null, hint,
 
257
                                          lw.searchField.get_text (),
 
258
                                          search_artist, search_album, search_genre, search_year, search_rating);
244
259
 
245
260
                // Now re-populate the child columns
 
261
 
246
262
                foreach (var column in columns) {
247
263
                        // Child columns
248
264
                        if (column.category > category) {
 
265
 
 
266
                                debug (">> Populating child column %s", column.category.to_string ());
249
267
                                var column_set = new HashMap<string, int> ();
250
268
 
251
 
                                foreach(int id in media_results) {
 
269
                                foreach(int id in _media_results) {
252
270
                                        var media = lm.media_from_id(id);
253
271
                                        string _val = "";
254
272
 
271
289
                        }
272
290
                }
273
291
 
274
 
                // Now that we've re-populated the columns, emit the 'changed' signal
 
292
                // Notify others about the change
275
293
                changed ();
276
294
        }
277
295
 
307
325
                }
308
326
                // </to_remove>
309
327
 
310
 
                lm.do_search (medias, out media_results, out album_results, null, null, null,
311
 
                              hint, lw.searchField.get_text ());
 
328
                lm.do_search (medias, out _media_results, out _album_results, null, null, null,
 
329
                                          hint, lw.searchField.get_text ());
312
330
 
313
331
                foreach (var column in columns) {
314
332
                        var column_set = new HashMap<string, int>();
315
333
 
316
 
                        foreach (int id in media_results) {
 
334
                        foreach (int id in _media_results) {
317
335
                                var media = lm.media_from_id (id);
318
336
                                string val = "";
319
337
 
410
428
 
411
429
                menu_item.toggled.connect (on_menu_item_toggled);
412
430
                view.get_column (0).get_button ().button_press_event.connect (on_header_clicked);
413
 
                view.get_selection ().changed.connect (selected_item_changed);
414
431
                view.row_activated.connect (view_double_click);
415
432
                view.key_press_event.connect (key_pressed);
416
433
        }
433
450
                else
434
451
                        visible_columns --;
435
452
 
436
 
 
437
453
                // Save settings
438
454
                var visible_columns_list = new LinkedList<string> ();
439
455
 
476
492
        }
477
493
 
478
494
        // selects "All ..."
479
 
        public void select_first_item () {
480
 
                set_selected (null, get_selected () != "");
481
 
                selection_changed (category, get_selected ()); // This will send "" as selected item
 
495
        public void select_first_item (bool? notify = null) {
 
496
                set_selected (null, true);
482
497
        }
483
498
 
484
499
        private bool on_header_clicked (Widget w, EventButton e) {
505
520
        }
506
521
 
507
522
        public void set_selected (string? val, bool notify = false) {
508
 
                if (!lw.initializationFinished || !this.visible || val == _selected)
 
523
                if (!lw.initializationFinished || val == _selected)
509
524
                        return;
510
525
 
511
526
                _selected = val;
560
575
                if (items.size == model.iter_n_children (null))
561
576
                        return;
562
577
 
 
578
                view.get_selection ().changed.disconnect (selected_item_changed);
 
579
 
563
580
                items.unset ("");
564
581
 
565
582
                if (items.get(get_selected()) == 0) {
566
 
                        select_first_item ();
 
583
                        select_first_item (false); // Don't notify
567
584
                }
568
585
 
569
586
                model = new MillerModel (get_category_text ());
573
590
                view.set_model (model);
574
591
 
575
592
                // select selected item
576
 
                view.get_selection ().changed.disconnect (selected_item_changed);
577
593
                model.foreach (select_proper_string);
578
594
                view.get_selection ().changed.connect (selected_item_changed);
579
595
        }