From 5512d46e4e52e01231b0f88b84e9c6ebdfb1a85d Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 13 Nov 2011 12:05:52 -0500 Subject: [PATCH] ocarina: Filter the song list Users enter text and non-matching songs are removed from the songlist view (they do come back when the text is removed) --- include/ocarina/library.h | 1 + include/ocarina/songlist.h | 2 ++ ocarina/callback.cpp | 7 +++++++ ocarina/library.cpp | 5 +++++ ocarina/songlist/fill.cpp | 2 +- ocarina/songlist/filter.cpp | 17 +++++++++++++++++ ocarina/songlist/init.cpp | 16 +++++++++++++--- ocarina/songlist/songlist.cpp | 9 +++++++++ ocarina/songlist/songlist.h | 1 + 9 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 ocarina/songlist/filter.cpp diff --git a/include/ocarina/library.h b/include/ocarina/library.h index 6b73579c..6d4fe4cb 100644 --- a/include/ocarina/library.h +++ b/include/ocarina/library.h @@ -8,6 +8,7 @@ namespace ocarina void init(); void refresh(); + void refilter(); } }; diff --git a/include/ocarina/songlist.h b/include/ocarina/songlist.h index eecf4d3d..63f5d9c8 100644 --- a/include/ocarina/songlist.h +++ b/include/ocarina/songlist.h @@ -42,6 +42,7 @@ class SongList : public libsaria::SourceModel GtkWidget *treeview; GtkListStore *liststore; + GtkTreeModel *filter; GtkCellRenderer *textcell; void set_label_text(); @@ -59,6 +60,7 @@ class SongList : public libsaria::SourceModel void insert(Track &); void fill(); void clear(); + void refilter(); gint right_click(guint, guint64); }; diff --git a/ocarina/callback.cpp b/ocarina/callback.cpp index 607918a6..e3e68b41 100644 --- a/ocarina/callback.cpp +++ b/ocarina/callback.cpp @@ -35,6 +35,12 @@ void cb_library_refresh() library_settings_refresh(); } +void cb_refilter() +{ + println("Ocarina REFILTER callback!"); + ocarina::library::refilter(); +} + void cb_track_loaded() { println("Ocarina TRACK_LOADED callback!"); @@ -48,5 +54,6 @@ void setup_callbacks() register_callback(PAUSE, cb_pause); register_callback(IDLE_TASK_QUEUED, cb_idle_task_queued); register_callback(LIBRARY_REFRESH, cb_library_refresh); + register_callback(REFILTER, cb_refilter); register_callback(TRACK_LOADED, cb_track_loaded); } diff --git a/ocarina/library.cpp b/ocarina/library.cpp index c1731bf6..cd747256 100644 --- a/ocarina/library.cpp +++ b/ocarina/library.cpp @@ -23,6 +23,11 @@ static void test() namespace ocarina { + void library::refilter() + { + library_list.refilter(); + } + void library::refresh() { library_list.clear(); diff --git a/ocarina/songlist/fill.cpp b/ocarina/songlist/fill.cpp index b9ad83f3..45381f6e 100644 --- a/ocarina/songlist/fill.cpp +++ b/ocarina/songlist/fill.cpp @@ -10,7 +10,7 @@ void SongList::freeze() void SongList::thaw() { gtk_widget_thaw_child_notify(treeview); - gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore)); + gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), filter); } void SongList::clear() diff --git a/ocarina/songlist/filter.cpp b/ocarina/songlist/filter.cpp new file mode 100644 index 00000000..1b762d1f --- /dev/null +++ b/ocarina/songlist/filter.cpp @@ -0,0 +1,17 @@ + +#include +#include + +gboolean songlist_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer data) +{ + ino_t inode; + gtk_tree_model_get(model, iter, 0, &inode, -1); + return libsaria::library::is_visible(inode); +} + +void SongList::refilter() +{ + freeze(); + gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter)); + thaw(); +} diff --git a/ocarina/songlist/init.cpp b/ocarina/songlist/init.cpp index 2ec2d958..d6869b1f 100644 --- a/ocarina/songlist/init.cpp +++ b/ocarina/songlist/init.cpp @@ -31,14 +31,23 @@ static GtkListStore *setup_liststore() return liststore; } -static GtkWidget *setup_treeview(GtkListStore *liststore, SongList *list) +static GtkTreeModel *setup_filter(GtkListStore *liststore) +{ + GtkTreeModel *filter; + filter = gtk_tree_model_filter_new(GTK_TREE_MODEL(liststore), NULL); + gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(filter), + songlist_filter_func, NULL, NULL); + return filter; +} + +static GtkWidget *setup_treeview(GtkTreeModel *model, SongList *list) { GtkWidget *treeview = gtk_tree_view_new(); GTK_CONNECT(treeview, "row-activated", songlist_row_activated, NULL); GTK_CONNECT(treeview, "button-release-event", songlist_button_click, list); gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE); - gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore)); + gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), model); return treeview; } @@ -87,7 +96,8 @@ void SongList::init(string text, SongListFuncs *funcs, list *menu) label = setup_label(name); textcell = setup_renderer(); liststore = setup_liststore(); - treeview = setup_treeview(liststore, this); + filter = setup_filter(liststore); + treeview = setup_treeview(filter, this); window = setup_window(treeview); setup_columns(textcell, treeview); diff --git a/ocarina/songlist/songlist.cpp b/ocarina/songlist/songlist.cpp index 35da3933..5cd5a5b9 100644 --- a/ocarina/songlist/songlist.cpp +++ b/ocarina/songlist/songlist.cpp @@ -8,6 +8,15 @@ using namespace std; #include #include "songlist.h" +void songlist_selected_inode(GtkTreeView *treeview, GtkTreePath *path, ino_t &inode) +{ + GtkTreeIter iter; + GtkTreeModel *model = gtk_tree_view_get_model(treeview); + + gtk_tree_model_get_iter(model, &iter, path); + gtk_tree_model_get(model, &iter, 0, &inode, -1); +} + void songlist_row_activated(GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, gpointer data) { diff --git a/ocarina/songlist/songlist.h b/ocarina/songlist/songlist.h index ab192af7..9f4bd07a 100644 --- a/ocarina/songlist/songlist.h +++ b/ocarina/songlist/songlist.h @@ -5,5 +5,6 @@ void songlist_row_activated(GtkTreeView *, GtkTreePath *, GtkTreeViewColumn *, gpointer); void songlist_button_click(GtkWidget *, GdkEvent *, gpointer); +gboolean songlist_filter_func(GtkTreeModel *, GtkTreeIter *, gpointer); #endif /* OCARINA_SONGLIST_PRIVATE_H */