From 4fd370ceb6cf66b80d1ad0a5996efd1eda8dfae8 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 18 Jan 2016 08:31:48 -0500 Subject: [PATCH] gui/model: Send tree model signals "row-inserted", "row-deleted", and "row-changed" Signed-off-by: Anna Schumaker --- gui/model.c | 35 ++++++++++++++++++++++++++++++ include/gui/model.h | 12 +++++++++++ tests/gui/model.c | 52 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/gui/model.c b/gui/model.c index c855508a..b8bd8f4f 100644 --- a/gui/model.c +++ b/gui/model.c @@ -251,6 +251,41 @@ GType gui_queue_model_get_type() return queue_type; } +void gui_queue_model_add(GuiQueueModel *model, unsigned int row) +{ + GtkTreePath *path = gtk_tree_path_new_from_indices(row, -1); + GtkTreeIter iter; + + _queue_model_get_iter(GTK_TREE_MODEL(model), &iter, path); + gtk_tree_model_row_inserted(GTK_TREE_MODEL(model), path, &iter); + gtk_tree_path_free(path); +} + +void gui_queue_model_remove(GuiQueueModel *model, unsigned int row) +{ + GtkTreePath *path = gtk_tree_path_new_from_indices(row, -1); + gtk_tree_model_row_deleted(GTK_TREE_MODEL(model), path); + gtk_tree_path_free(path); +} + +void gui_queue_model_clear(GuiQueueModel *model, unsigned int n) +{ + unsigned int i; + + for (i = 1; i <= n; i++) + gui_queue_model_remove(model, n - i); +} + +void gui_queue_model_update(GuiQueueModel *model, unsigned int row) +{ + GtkTreePath *path = gtk_tree_path_new_from_indices(row, -1); + GtkTreeIter iter; + + _queue_model_get_iter(GTK_TREE_MODEL(model), &iter, path); + gtk_tree_model_row_changed(GTK_TREE_MODEL(model), path, &iter); + gtk_tree_path_free(path); +} + static const GTypeInfo queue_type_info = { .class_size = sizeof(GuiQueueModelClass), diff --git a/include/gui/model.h b/include/gui/model.h index 925a2102..9153f8e4 100644 --- a/include/gui/model.h +++ b/include/gui/model.h @@ -46,4 +46,16 @@ GuiQueueModel *gui_queue_model_new(struct queue *); /* Called to find the GType of the GuiQueueModel */ GType gui_queue_model_get_type(); +/* Called to add a row to the model */ +void gui_queue_model_add(GuiQueueModel *, unsigned int); + +/* Called to remove a row from the model */ +void gui_queue_model_remove(GuiQueueModel *, unsigned int); + +/* Called to remove all rows from the model */ +void gui_queue_model_clear(GuiQueueModel *, unsigned int); + +/* Called to update a row in the model */ +void gui_queue_model_update(GuiQueueModel *, unsigned int); + #endif /* OCARINA_GUI_MODEL_H */ diff --git a/tests/gui/model.c b/tests/gui/model.c index 214c2af5..8c00a896 100644 --- a/tests/gui/model.c +++ b/tests/gui/model.c @@ -7,7 +7,44 @@ #include #include -struct core_init_data init_data; +unsigned int count_insert = 0; +unsigned int count_delete = 0; +unsigned int count_update = 0; + +void on_row_inserted(GtkTreeModel *model, GtkTreePath *path, + GtkTreeIter *iter, gpointer data) + { count_insert++; } +void on_row_deleted(GtkTreeModel *model, GtkTreePath *path, gpointer data) + { count_delete++; } +void on_row_changed(GtkTreeModel *model, GtkTreePath *path, + GtkTreeIter *iter, gpointer data) + { count_update++; } + +void *test_queue_init(struct queue *queue) { return NULL; } +void test_queue_deinit(struct queue *queue) {} +void test_queue_added(struct queue *queue, unsigned int row) + { gui_queue_model_add(queue->q_private, row); } +void test_queue_removed(struct queue *queue, unsigned int row) + { gui_queue_model_remove(queue->q_private, row); } +void test_queue_cleared(struct queue *queue, unsigned int n) + { gui_queue_model_get_type(queue->q_private, n); } +void test_queue_save(struct queue *queue, enum queue_flags flag) {} +void test_queue_updated(struct queue *queue, unsigned int row) + { gui_queue_model_update(queue->q_private, row); } + +struct queue_ops test_ops = { + .qop_init = test_queue_init, + .qop_deinit = test_queue_deinit, + .qop_added = test_queue_added, + .qop_removed = test_queue_removed, + .qop_cleared = test_queue_cleared, + .qop_save = test_queue_save, + .qop_updated = test_queue_updated, +}; + +struct core_init_data init_data = { + .collection_ops = &test_ops, +}; static void test_init() { @@ -110,11 +147,19 @@ static void test_model() model = GTK_TREE_MODEL(gui_queue_model_new(collection_get_queue())); memset(&value, 0, sizeof(GValue)); + g_signal_connect(model, "row-inserted", (GCallback)on_row_inserted, NULL); + g_signal_connect(model, "row-deleted", (GCallback)on_row_deleted, NULL); + g_signal_connect(model, "row-changed", (GCallback)on_row_changed, NULL); + /* Okay, now scan a directory ... */ core_init(&argc, NULL, &init_data); + collection_get_queue()->q_private = model; collection_add("tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} test_equal(queue_size(collection_get_queue()), 13); + test_equal(count_insert, 13); + queue_resort(collection_get_queue()); + test_equal(count_update, 13); /* ... and test again */ test_equal(gtk_tree_model_get_iter_first(model, &iter), true); @@ -176,6 +221,11 @@ static void test_model() test_equal(gtk_tree_model_iter_parent(model, &iter, &iter), false); + while (queue_size(collection_get_queue()) > 0) + queue_remove(collection_get_queue(), 0); + test_equal(count_delete, 13); + + core_deinit(); g_object_unref(model); }