diff --git a/gui/filter.c b/gui/filter.c new file mode 100644 index 00000000..2b3ef199 --- /dev/null +++ b/gui/filter.c @@ -0,0 +1,25 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include + +static GtkTreeModelFilter *filter_model = NULL; + + +void gui_filter_init() +{ + GtkTreeModel *model = GTK_TREE_MODEL(gui_model_get()); + GtkTreeModel *filter = gtk_tree_model_filter_new(model, NULL); + filter_model = GTK_TREE_MODEL_FILTER(filter); +} + +void gui_filter_deinit() +{ + g_object_unref(G_OBJECT(filter_model)); +} + +GtkTreeModelFilter *gui_filter_get() +{ + return filter_model; +} diff --git a/gui/ocarina.c b/gui/ocarina.c index a617d7f5..83fe3441 100644 --- a/gui/ocarina.c +++ b/gui/ocarina.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,7 @@ static void __ocarina_startup(GApplication *application, gpointer data) core_init(&startup_argc, &startup_argv, &init_data); gui_window_init(icon); gui_model_init(); + gui_filter_init(); gui_view_init(); gui_queue_init(); gui_sidebar_init(); @@ -121,6 +123,7 @@ static void __ocarina_shutdown(GApplication *application, gpointer data) gui_idle_disable(); core_deinit(); + gui_filter_deinit(); gui_model_deinit(); gui_window_deinit(); gui_builder_deinit(); diff --git a/gui/queue.c b/gui/queue.c index 9360fd44..0ca54da4 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,7 @@ void __queue_filter(GtkSearchEntry *entry, gpointer data) gq_queue->gq_active = gtk_combo_box_get_active(combo); } - gtk_tree_model_filter_refilter(gui_view_get_filter()); + gtk_tree_model_filter_refilter(gui_filter_get()); gui_view_scroll(); } @@ -113,7 +114,7 @@ void __queue_filter_how_changed(int n) void gui_queue_init(void) { - gtk_tree_model_filter_set_visible_func(gui_view_get_filter(), + gtk_tree_model_filter_set_visible_func(gui_filter_get(), __queue_visible_func, gui_builder_object("o_search"), NULL); diff --git a/gui/view.c b/gui/view.c index 8212e9fe..8b13afdd 100644 --- a/gui/view.c +++ b/gui/view.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -36,13 +37,13 @@ static const enum compare_t QUEUE_SORT[GUI_MODEL_N_COLUMNS] = { }; static GtkTreeView *view_treeview = NULL; -static GtkTreeModelFilter *view_filter = NULL; static unsigned int view_sort_count = 0; static bool view_no_scroll = false; static inline GtkTreePath *__view_filter_convert_path(GtkTreePath *orig) { - return gtk_tree_model_filter_convert_path_to_child_path(view_filter, orig); + return gtk_tree_model_filter_convert_path_to_child_path( + gui_filter_get(), orig); } static struct track *__view_filter_get_track(GtkTreePath *orig) @@ -406,8 +407,6 @@ void gui_view_init() int i, pos; view_treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview")); - view_filter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new( - GTK_TREE_MODEL(gui_model_get()), NULL)); for (i = 0; i < GUI_MODEL_N_COLUMNS; i++) { col = gtk_tree_view_get_column(view_treeview, i); @@ -416,12 +415,7 @@ void gui_view_init() gtk_tree_view_column_set_fixed_width(col, pos); } - gtk_tree_view_set_model(view_treeview, GTK_TREE_MODEL(view_filter)); -} - -GtkTreeModelFilter *gui_view_get_filter(void) -{ - return view_filter; + gtk_tree_view_set_model(view_treeview, GTK_TREE_MODEL(gui_filter_get())); } void gui_view_set_playlist(struct playlist *playlist) @@ -445,7 +439,8 @@ void gui_view_scroll() return; real = gtk_tree_path_new_from_indices(queue->q_cur.it_pos, -1); - path = gtk_tree_model_filter_convert_child_path_to_path(view_filter, real); + path = gtk_tree_model_filter_convert_child_path_to_path( + gui_filter_get(), real); if (!path) goto out; diff --git a/include/gui/filter.h b/include/gui/filter.h new file mode 100644 index 00000000..7aec6e0b --- /dev/null +++ b/include/gui/filter.h @@ -0,0 +1,17 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#ifndef OCARINA_GUI_FILTER_H +#define OCARINA_GUI_FILTER_H +#include + +/* Called to initialize the filter model. */ +void gui_filter_init(); + +/* Called to deinitialize the filter model. */ +void gui_filter_deinit(); + +/* Called to get the filter model. */ +GtkTreeModelFilter *gui_filter_get(); + +#endif /* OCARINA_GUI_FILTER_H */ diff --git a/include/gui/view.h b/include/gui/view.h index a21d8a3e..26ccfddb 100644 --- a/include/gui/view.h +++ b/include/gui/view.h @@ -12,9 +12,6 @@ void gui_view_init(); /* Called to scroll the GUI treeview to the queue's current position. */ void gui_view_scroll(); -/* Called to get the GtkTreeModelFilter */ -GtkTreeModelFilter *gui_view_get_filter(void); - /* Called to set the currently displayed model. */ void gui_view_set_playlist(struct playlist *); diff --git a/tests/gui/.gitignore b/tests/gui/.gitignore index 5a94e170..19e4ad66 100644 --- a/tests/gui/.gitignore +++ b/tests/gui/.gitignore @@ -2,6 +2,7 @@ builder window idle model +filter view queue sidebar diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index b44bfea1..e1d7bb43 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -8,6 +8,7 @@ gui_unit_test(Builder) gui_unit_test(Window) gui_unit_test(Idle) gui_unit_test(Model) +gui_unit_test(Filter) gui_unit_test(View) gui_unit_test(Queue) gui_unit_test(Sidebar) diff --git a/tests/gui/filter.c b/tests/gui/filter.c new file mode 100644 index 00000000..043f16c3 --- /dev/null +++ b/tests/gui/filter.c @@ -0,0 +1,83 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include + +void *test_queue_init(struct queue *queue, void *data) + { return NULL; } +void test_queue_deinit(struct queue *queue) + { } +void test_queue_add(struct queue *queue, unsigned int n) + { gui_model_add(queue->q_private, n); } +void test_queue_remove(struct queue *queue, unsigned int n) + { gui_model_remove(queue->q_private, n); } +void test_queue_clear(struct queue *queue, unsigned int n) + { gui_model_clear(queue->q_private, n); } +void test_queue_save(struct queue *queue, enum queue_flags flag) {} +void test_queue_update(struct queue *queue, unsigned int n) + { gui_model_update(queue->q_private, n); } + +struct queue_ops test_ops = { + .qop_init = test_queue_init, + .qop_deinit = test_queue_deinit, + .qop_added = test_queue_add, + .qop_removed = test_queue_remove, + .qop_cleared = test_queue_clear, + .qop_save = test_queue_save, + .qop_updated = test_queue_update, +}; + +struct core_init_data init_data = { + .playlist_ops = &test_ops, +}; + +void test_filter() +{ + GtkTreeModel *model; + GtkTreeIter iter; + + g_assert_nonnull(gui_filter_get()); + g_assert_true(GTK_IS_TREE_MODEL_FILTER(gui_filter_get())); + g_assert(gtk_tree_model_filter_get_model(gui_filter_get()) == + GTK_TREE_MODEL(gui_model_get())); + + model = GTK_TREE_MODEL(gui_filter_get()); + playlist_get_queue(PL_SYSTEM, "Collection")->q_private = + playlist_get(PL_SYSTEM, "Collection"); + + g_assert_false(gtk_tree_model_get_iter_first(model, &iter)); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); + g_assert_false(gtk_tree_model_get_iter_first(model, &iter)); + + playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); + while (idle_run_task()) {}; + g_assert_true(gtk_tree_model_get_iter_first(model, &iter)); + g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); +} + +int main(int argc, char **argv) +{ + int ret; + + gtk_init(&argc, NULL); + core_init(&argc, NULL, &init_data); + gui_builder_init("share/ocarina/ocarina.ui"); + gui_window_init("share/ocarina/ocarina.png"); + gui_model_init(); + gui_filter_init(); + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Gui/Filter", test_filter); + ret = g_test_run(); + + gui_filter_deinit(); + gui_model_deinit(); + gui_window_deinit(); + gui_builder_deinit(); + core_deinit(); + return ret; +} diff --git a/tests/gui/playlist.c b/tests/gui/playlist.c index 786aaaa8..12436c53 100644 --- a/tests/gui/playlist.c +++ b/tests/gui/playlist.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,7 @@ int main(int argc, char **argv) gui_builder_init("share/ocarina/ocarina.ui"); core_init(&argc, NULL, &init_data); gui_model_init(); + gui_filter_init(); gui_view_init(); gui_playlist_init(); playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); @@ -117,6 +119,7 @@ int main(int argc, char **argv) ret = g_test_run(); core_deinit(); + gui_filter_deinit(); gui_builder_deinit(); return ret; } diff --git a/tests/gui/queue.c b/tests/gui/queue.c index bb8a1b2f..bb69151e 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -103,7 +104,7 @@ static void test_tracks() playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} - filter = GTK_TREE_MODEL(gui_view_get_filter()); + filter = GTK_TREE_MODEL(gui_filter_get()); g_assert_nonnull(gq); g_assert_nonnull(filter); g_assert_cmpuint(gtk_tree_model_iter_n_children(filter, NULL), ==, 13); @@ -135,6 +136,7 @@ int main(int argc, char **argv) gui_builder_init("share/ocarina/ocarina.ui"); core_init(&argc, NULL, &init_data); gui_model_init(); + gui_filter_init(); gui_view_init(); gui_queue_init(); while (idle_run_task()) {}; diff --git a/tests/gui/view.c b/tests/gui/view.c index 10ae75ea..d89507ac 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -54,7 +55,7 @@ static void test_treeview() playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); - filter = GTK_TREE_MODEL(gui_view_get_filter()); + filter = GTK_TREE_MODEL(gui_filter_get()); treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview")); @@ -90,7 +91,7 @@ static void test_treeview() } g_assert((void *)gtk_tree_view_get_model(treeview) == - (void *)gui_view_get_filter()); + (void *)gui_filter_get()); gtk_tree_model_iter_nth_child(filter, &iter, NULL, 3); path = gtk_tree_model_get_path(filter, &iter);