gui/model: Add support for gtk_tree_model_get_iter()
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
ff81af24fc
commit
eb481b8d09
27
gui/model.c
27
gui/model.c
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2016 (c) Anna Schumaker.
|
* Copyright 2016 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
|
#include <core/queue.h>
|
||||||
#include <gui/model.h>
|
#include <gui/model.h>
|
||||||
|
|
||||||
static const GTypeInfo queue_type_info;
|
static const GTypeInfo queue_type_info;
|
||||||
|
@ -38,6 +39,27 @@ static GType _queue_model_get_column_type(GtkTreeModel *model, gint index)
|
||||||
return queue_columns[index];
|
return queue_columns[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean _queue_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter,
|
||||||
|
GtkTreePath *path)
|
||||||
|
{
|
||||||
|
GuiQueueModel *gqm = GUI_QUEUE_MODEL(model);
|
||||||
|
gint *indices, depth;
|
||||||
|
|
||||||
|
g_assert(path != NULL);
|
||||||
|
|
||||||
|
indices = gtk_tree_path_get_indices_with_depth(path, &depth);
|
||||||
|
g_assert(depth == 1);
|
||||||
|
|
||||||
|
if (indices[0] >= queue_size(gqm->gqm_queue))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
_q_iter_set(&gqm->gqm_queue->q_tracks, &gqm->gqm_iter, indices[0]);
|
||||||
|
iter->stamp = gqm->gqm_stamp;
|
||||||
|
iter->user_data = &gqm->gqm_iter;
|
||||||
|
iter->user_data2 = queue_at(gqm->gqm_queue, indices[0]);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static void _queue_model_init(GuiQueueModel *model)
|
static void _queue_model_init(GuiQueueModel *model)
|
||||||
{
|
{
|
||||||
model->gqm_stamp = g_random_int();
|
model->gqm_stamp = g_random_int();
|
||||||
|
@ -63,14 +85,17 @@ static void _queue_tree_model_init(GtkTreeModelIface *iface)
|
||||||
iface->get_flags = _queue_model_get_flags;
|
iface->get_flags = _queue_model_get_flags;
|
||||||
iface->get_n_columns = _queue_model_get_n_columns;
|
iface->get_n_columns = _queue_model_get_n_columns;
|
||||||
iface->get_column_type = _queue_model_get_column_type;
|
iface->get_column_type = _queue_model_get_column_type;
|
||||||
|
iface->get_iter = _queue_model_get_iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GuiQueueModel *gui_queue_model_new()
|
GuiQueueModel *gui_queue_model_new(struct queue *queue)
|
||||||
{
|
{
|
||||||
GuiQueueModel *model = g_object_new(GUI_QUEUE_MODEL_TYPE, NULL);
|
GuiQueueModel *model = g_object_new(GUI_QUEUE_MODEL_TYPE, NULL);
|
||||||
|
|
||||||
g_assert(model != NULL);
|
g_assert(model != NULL);
|
||||||
|
model->gqm_queue = queue;
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
#define GUI_QUEUE_MODEL_TYPE (gui_queue_model_get_type())
|
#define GUI_QUEUE_MODEL_TYPE (gui_queue_model_get_type())
|
||||||
|
#define GUI_QUEUE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
|
||||||
|
GUI_QUEUE_MODEL_TYPE, GuiQueueModel))
|
||||||
|
|
||||||
enum queue_model_columns {
|
enum queue_model_columns {
|
||||||
Q_MODEL_TRACK_NR,
|
Q_MODEL_TRACK_NR,
|
||||||
|
@ -27,8 +29,10 @@ typedef struct _gui_queue_model_class GuiQueueModelClass;
|
||||||
|
|
||||||
struct _gui_queue_model {
|
struct _gui_queue_model {
|
||||||
GObject gqm_parent; /* This MUST be the first member. */
|
GObject gqm_parent; /* This MUST be the first member. */
|
||||||
|
|
||||||
gint gqm_stamp; /* This is used to check iter validity. */
|
gint gqm_stamp; /* This is used to check iter validity. */
|
||||||
|
|
||||||
|
struct queue *gqm_queue; /* The model's associated queue. */
|
||||||
|
struct _q_iter gqm_iter; /* The current _q_iter. */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _gui_queue_model_class {
|
struct _gui_queue_model_class {
|
||||||
|
@ -37,7 +41,7 @@ struct _gui_queue_model_class {
|
||||||
|
|
||||||
|
|
||||||
/* Called to allocate a new GuiQueueModel */
|
/* Called to allocate a new GuiQueueModel */
|
||||||
GuiQueueModel *gui_queue_model_new();
|
GuiQueueModel *gui_queue_model_new(struct queue *);
|
||||||
|
|
||||||
/* Called to find the GType of the GuiQueueModel */
|
/* Called to find the GType of the GuiQueueModel */
|
||||||
GType gui_queue_model_get_type();
|
GType gui_queue_model_get_type();
|
||||||
|
|
|
@ -1,19 +1,30 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2016 (c) Anna Schumaker.
|
* Copyright 2016 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
|
#include <core/collection.h>
|
||||||
|
#include <core/idle.h>
|
||||||
|
#include <core/core.h>
|
||||||
#include <gui/model.h>
|
#include <gui/model.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
|
struct core_init_data init_data;
|
||||||
|
|
||||||
static void test_model()
|
static void test_model()
|
||||||
{
|
{
|
||||||
GuiQueueModel *model = gui_queue_model_new();
|
|
||||||
GtkTreeModel *treemodel;
|
GtkTreeModel *treemodel;
|
||||||
|
GuiQueueModel *model;
|
||||||
|
GtkTreeIter iter;
|
||||||
|
int argc = 0;
|
||||||
GType type;
|
GType type;
|
||||||
|
|
||||||
|
core_init(&argc, NULL, &init_data);
|
||||||
|
|
||||||
|
model = gui_queue_model_new(collection_get_queue());
|
||||||
test_not_equal((void *)model, NULL);
|
test_not_equal((void *)model, NULL);
|
||||||
test_equal(GTK_IS_TREE_MODEL(model), true);
|
test_equal(GTK_IS_TREE_MODEL(model), true);
|
||||||
treemodel = GTK_TREE_MODEL(model);
|
test_equal((void *)model->gqm_queue, (void *)collection_get_queue());
|
||||||
|
|
||||||
|
treemodel = GTK_TREE_MODEL(model);
|
||||||
test_equal(gtk_tree_model_get_flags(treemodel), GTK_TREE_MODEL_LIST_ONLY);
|
test_equal(gtk_tree_model_get_flags(treemodel), GTK_TREE_MODEL_LIST_ONLY);
|
||||||
test_equal(gtk_tree_model_get_n_columns(treemodel), 10);
|
test_equal(gtk_tree_model_get_n_columns(treemodel), 10);
|
||||||
|
|
||||||
|
@ -42,6 +53,17 @@ static void test_model()
|
||||||
type = gtk_tree_model_get_column_type(treemodel, Q_MODEL_N_COLUMNS);
|
type = gtk_tree_model_get_column_type(treemodel, Q_MODEL_N_COLUMNS);
|
||||||
test_equal(type, G_TYPE_INVALID);
|
test_equal(type, G_TYPE_INVALID);
|
||||||
|
|
||||||
|
/* Tests before adding tracks to the collection */
|
||||||
|
test_equal(gtk_tree_model_get_iter_first(treemodel, &iter), false);
|
||||||
|
|
||||||
|
/* Okay, now scan a directory and test again */
|
||||||
|
collection_add("tests/Music/Hyrule Symphony");
|
||||||
|
while (idle_run_task() == true) {}
|
||||||
|
test_equal(queue_size(collection_get_queue()), 13);
|
||||||
|
|
||||||
|
test_equal(gtk_tree_model_get_iter_first(treemodel, &iter), true);
|
||||||
|
test_equal(((struct track *)iter.user_data2)->tr_track, 1);
|
||||||
|
|
||||||
g_object_unref(model);
|
g_object_unref(model);
|
||||||
test_equal(G_IS_OBJECT(model), false);
|
test_equal(G_IS_OBJECT(model), false);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue