]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
GtkTreeModel copy chain. gtktree
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 30 Sep 2011 11:48:12 +0000 (13:48 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 30 Sep 2011 11:48:12 +0000 (13:48 +0200)
src/utils.c
tests/utils_gtktreemodel_copy.c

index 9da2b57162981a8173251f2f7e87ba0df688f13d..31735985cee568f6d4492d7644fea88f50b39781 100644 (file)
@@ -511,17 +511,51 @@ GType
        return ret;
 }
 
+static GtkTreeIter
+*solipa_gtktreemodel_get_iter_child (GtkTreeIter *iter, GtkTreeModel **models, guint n_models)
+{
+       GtkTreeIter *iter_parent;
+       GtkTreeIter *iter_child;
+
+       guint n;
+
+       iter_parent = iter;
+       iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
+
+       for (n = 0; n < (n_models - 1); n++)
+               {
+                       if (GTK_IS_TREE_MODEL_SORT (models[n]))
+                               {
+                                       gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (models[n]),
+                                                                                       iter_child, iter_parent);
+                                       iter_parent = iter_child;
+                                       iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
+                               }
+                       else if (GTK_IS_TREE_MODEL_FILTER (models[n]))
+                               {
+                                       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (models[n]),
+                                                                                         iter_child, iter_parent);
+                                       iter_parent = iter_child;
+                                       iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
+                               }
+               }
+       g_free (iter_child);
+
+       return iter_parent;
+}
+
 static void
 solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                                GtkTreeModel *model,
-                               GtkTreeModel *model_child,
+                               GtkTreeModel **models,
+                               guint n_models,
                                gboolean is_list_store,
                                guint cols,
                                GtkTreeIter *iter,
                                GtkTreeIter *iter_parent)
 {
        GtkTreeIter iter_new;
-       GtkTreeIter iter_child;
+       GtkTreeIter *iter_child;
 
        guint col;
 
@@ -535,20 +569,15 @@ solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                                {
                                        gtk_tree_store_append (GTK_TREE_STORE (ret), &iter_new, iter_parent);
                                }
-
+continue;
+                       iter_child = solipa_gtktreemodel_get_iter_child (iter, models, n_models);
                        for (col = 0; col < cols; col++)
                                {
                                        GValue gval = {0};
 
-                                       if (model_child != NULL)
-                                               {
-                                                       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &iter_child, iter);
-                                                       gtk_tree_model_get_value (model_child, &iter_child, col, &gval);
-                                               }
-                                       else
-                                               {
-                                                       gtk_tree_model_get_value (model, iter, col, &gval);
-                                               }
+                                       gtk_tree_model_get_value (models[n_models - 1],
+                                                                 iter_child,
+                                                                 col, &gval);
 
                                        if (is_list_store)
                                                {
@@ -565,14 +594,39 @@ solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                        if (!is_list_store)
                                {
                                        /* is_tree_store */
-                                       if (gtk_tree_model_iter_children (model, &iter_child, iter))
+                                       GtkTreeIter iter_children;
+
+                                       if (gtk_tree_model_iter_children (model, &iter_children, iter_child))
                                                {
-                                                       solipa_gtktreemodel_copy_iter (ret, model, model_child, is_list_store, cols, &iter_child, &iter_new);
+                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, is_list_store, cols, &iter_children, &iter_new);
                                                }
                                }
                } while (gtk_tree_model_iter_next (model, iter));
 }
 
+static void
+solipa_gtktreemodel_get_models_chain (GtkTreeModel *model, GtkTreeModel **models, guint *n_models)
+{
+       if (GTK_IS_TREE_MODEL_SORT (model))
+               {
+                       *n_models = *n_models + 1;
+                       models = (GtkTreeModel **)g_realloc (models, *n_models * sizeof (GtkTreeModel *));
+
+                       models[*n_models - 1] = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
+
+                       solipa_gtktreemodel_get_models_chain (models[*n_models - 1], models, n_models);
+               }
+       else if (GTK_IS_TREE_MODEL_FILTER (model))
+               {
+                       *n_models = *n_models + 1;
+                       models = (GtkTreeModel **)g_realloc (models, *n_models * sizeof (GtkTreeModel *));
+
+                       models[*n_models - 1] = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
+
+                       solipa_gtktreemodel_get_models_chain (models[*n_models - 1], models, n_models);
+               }
+}
+
 GtkTreeModel
 *solipa_gtktreemodel_copy (GtkTreeModel *model, gboolean only_schema)
 {
@@ -583,7 +637,8 @@ GtkTreeModel
        guint cols;
        guint col;
 
-       GtkTreeModel *model_child;
+       guint n_models;
+       GtkTreeModel **models;
 
        GtkTreeIter iter;
 
@@ -592,17 +647,17 @@ GtkTreeModel
        gtypes = solipa_gtktreemodel_get_gtypes (model, &cols);
        if (gtypes != NULL)
                {
-                       if (GTK_IS_TREE_MODEL_FILTER (model))
-                               {
-                                       model_child = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
-                               }
-                       else
+                       n_models = 1;
+                       models = (GtkTreeModel **)g_malloc0 (sizeof (GtkTreeModel *));
+                       models[0] = model;
+
+                       if (GTK_IS_TREE_MODEL_SORT (model) ||
+                           GTK_IS_TREE_MODEL_FILTER (model))
                                {
-                                       model_child = NULL;
+                                       solipa_gtktreemodel_get_models_chain (model, models, &n_models);
                                }
 
-                       if (GTK_IS_LIST_STORE (model)
-                           || (model_child != NULL && GTK_IS_LIST_STORE (model_child)))
+                       if (GTK_IS_LIST_STORE (models[n_models - 1]))
                                {
                                        ret = GTK_TREE_MODEL (gtk_list_store_newv (cols, gtypes));
 
@@ -610,12 +665,11 @@ GtkTreeModel
                                                {
                                                        if (gtk_tree_model_get_iter_first (model, &iter))
                                                                {
-                                                                       solipa_gtktreemodel_copy_iter (ret, model, model_child, TRUE, cols, &iter, NULL);
+                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, TRUE, cols, &iter, NULL);
                                                                }
                                                }
                                }
-                       else if (GTK_IS_TREE_STORE (model)
-                                || (model_child != NULL && GTK_IS_TREE_STORE (model_child)))
+                       else if (GTK_IS_TREE_STORE (models[n_models - 1]))
                                {
                                        ret = GTK_TREE_MODEL (gtk_tree_store_newv (cols, gtypes));
 
@@ -623,7 +677,7 @@ GtkTreeModel
                                                {
                                                        if (gtk_tree_model_get_iter_first (model, &iter))
                                                                {
-                                                                       solipa_gtktreemodel_copy_iter (ret, model, model_child, FALSE, cols, &iter, NULL);
+                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, FALSE, cols, &iter, NULL);
                                                                }
                                                }
                                }
index f2ddc561d6dfd11e1517602cd0f12742520f2ab1..f1b1ea0f7e244e99ca35eb206c8ac2eea1d70c4e 100644 (file)
@@ -53,6 +53,8 @@ main (int argc, char *argv[])
        GtkTreeIter iter;
        GtkTreeIter iter_child;
 
+       GtkTreeModel *sort;
+
        gtk_init (&argc, &argv);
 
        /* copia di un LIST_STORE */
@@ -85,6 +87,31 @@ main (int argc, char *argv[])
 
        copia = solipa_gtktreemodel_copy (model, FALSE);
 
+       if (gtk_tree_model_get_iter_first (copia, &iter))
+               {
+                       do
+                               {
+                                       gtk_tree_model_get (copia, &iter,
+                                                           0, &id,
+                                                           1, &cognome,
+                                                           2, &nome,
+                                                           -1);
+                                       g_printf ("%d\t%s\t%s\n", id, cognome, nome);
+                               } while (gtk_tree_model_iter_next (copia, &iter));
+               }
+       else
+               {
+                       g_error ("Copia del GTK_LIST_STORE vuota.");
+               }
+
+       g_printf ("\n\nCOPIA DEL LIST_STORE SORT\n"
+                 "=========================\n");
+
+       sort = gtk_tree_model_sort_new_with_model (model);
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort), 1, GTK_SORT_ASCENDING);
+
+       copia = solipa_gtktreemodel_copy (sort, FALSE);
+
        if (gtk_tree_model_get_iter_first (copia, &iter))
                {
                        do