From edc1d31d2ec6070245d338b2db234aa9d412e373 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 24 Aug 2016 08:02:59 -0400 Subject: [PATCH] gui/filter: Add a function for getting filter treepaths from index This keeps the treepath conversions contained to the filter code when finding paths to scroll to. Signed-off-by: Anna Schumaker --- gui/filter.c | 11 +++++++++++ gui/view.c | 10 +++------- include/gui/filter.h | 3 +++ tests/gui/filter.c | 14 ++++++++++---- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/gui/filter.c b/gui/filter.c index 33f4a212..64d115c7 100644 --- a/gui/filter.c +++ b/gui/filter.c @@ -131,3 +131,14 @@ unsigned int gui_filter_path_get_index(GtkTreePath *path) gtk_tree_path_free(real); return ret; } + +GtkTreePath *gui_filter_path_from_index(unsigned int index) +{ + GtkTreePath *real, *path; + + real = gtk_tree_path_new_from_indices(index, -1); + path = gtk_tree_model_filter_convert_child_path_to_path(filter_model, + real); + gtk_tree_path_free(real); + return path; +} diff --git a/gui/view.c b/gui/view.c index 6f4e3e8b..9a63cf2e 100644 --- a/gui/view.c +++ b/gui/view.c @@ -408,20 +408,16 @@ void gui_view_scroll() { struct playlist *playlist = gui_model_get_playlist(); struct queue *queue = playlist ? &playlist->pl_queue : NULL; - GtkTreePath *real, *path; + GtkTreePath *path; if (!queue || (int)queue->q_cur.it_pos < 0 || view_no_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( - gui_filter_get(), real); + path = gui_filter_path_from_index(queue->q_cur.it_pos); if (!path) - goto out; + return; gtk_tree_view_set_cursor(view_treeview, path, NULL, false); gtk_tree_view_scroll_to_cell(view_treeview, path, NULL, TRUE, 0.5, 0.5); gtk_tree_path_free(path); -out: - gtk_tree_path_free(real); } diff --git a/include/gui/filter.h b/include/gui/filter.h index 26c1da5f..05372feb 100644 --- a/include/gui/filter.h +++ b/include/gui/filter.h @@ -35,6 +35,9 @@ struct track *gui_filter_path_get_track(GtkTreePath *); /* Called to convert a filter model path into a queue index. */ unsigned int gui_filter_path_get_index(GtkTreePath *); +/* Called to convert a playlist iterator index into a path. */ +GtkTreePath *gui_filter_path_from_index(unsigned int); + /* Called to access the filter search-entry. */ static inline GtkSearchEntry *gui_filter_search(void) { diff --git a/tests/gui/filter.c b/tests/gui/filter.c index fb32ac13..90b4a12b 100644 --- a/tests/gui/filter.c +++ b/tests/gui/filter.c @@ -62,15 +62,21 @@ void test_filter() while (idle_run_task()) {}; g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); - g_assert_true(gtk_tree_model_get_iter_first(model, &iter)); - path = gtk_tree_model_get_path(model, &iter); for (i = 0; i < 13; i++) { + path = gui_filter_path_from_index(i); + g_assert_nonnull(path); + g_assert_cmpuint(gui_filter_path_get_index(path), ==, i); + track = gui_filter_path_get_track(path); g_assert_nonnull(track); g_assert_cmpuint(track->tr_track, ==, i + 1); - g_assert_cmpuint(gui_filter_path_get_index(path), ==, i); - gtk_tree_path_next(path); + gtk_tree_path_free(path); } + path = gui_filter_path_from_index(i); + g_assert_null(path); + + path = gui_filter_path_from_index(12); + gtk_tree_path_next(path); g_assert_null(gui_filter_path_get_track(path)); gtk_tree_path_free(path);