]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Modifica alla funzione SolipaUtils::gtktreemodel_copy.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 4 Oct 2011 09:05:26 +0000 (11:05 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 4 Oct 2011 09:05:26 +0000 (11:05 +0200)
Per avere qualsiasi annidamento possibile tra GtkTreeModel,
GtkTreeModelSort e GtkTreeModelFilter.

src/utils.c
tests/utils_gtktreemodel_copy.c

index 9da2b57162981a8173251f2f7e87ba0df688f13d..16c810716ee6a5d45a6cc1eeb01128588db04679 100644 (file)
@@ -511,17 +511,52 @@ GType
        return ret;
 }
 
+static GtkTreeIter
+*solipa_gtktreemodel_get_iter_child (GtkTreeIter *iter, GPtrArray *models)
+{
+       GtkTreeModel *model;
+       GtkTreeIter *iter_parent;
+       GtkTreeIter *iter_child;
+
+       guint n;
+
+       iter_parent = iter;
+       iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
+
+       for (n = 0; n < (models->len - 1); n++)
+               {
+                       model = (GtkTreeModel *)g_ptr_array_index (models, n);
+                       if (GTK_IS_TREE_MODEL_SORT (model))
+                               {
+                                       gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model),
+                                                                                       iter_child, iter_parent);
+                                       iter_parent = iter_child;
+                                       iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
+                               }
+                       else if (GTK_IS_TREE_MODEL_FILTER (model))
+                               {
+                                       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
+                                                                                         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,
+                               GPtrArray *models,
                                gboolean is_list_store,
                                guint cols,
                                GtkTreeIter *iter,
                                GtkTreeIter *iter_parent)
 {
        GtkTreeIter iter_new;
-       GtkTreeIter iter_child;
+       GtkTreeIter *iter_child;
 
        guint col;
 
@@ -536,19 +571,14 @@ solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                                        gtk_tree_store_append (GTK_TREE_STORE (ret), &iter_new, iter_parent);
                                }
 
+                       iter_child = solipa_gtktreemodel_get_iter_child (iter, 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 ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1),
+                                                                 iter_child,
+                                                                 col, &gval);
 
                                        if (is_list_store)
                                                {
@@ -565,14 +595,37 @@ 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, 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, GPtrArray *models)
+{
+       GtkTreeModel *my_model;
+
+       if (GTK_IS_TREE_MODEL_SORT (model))
+               {
+                       my_model = (gpointer)gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
+                       g_ptr_array_add (models, my_model);
+
+                       solipa_gtktreemodel_get_models_chain (my_model, models);
+               }
+       else if (GTK_IS_TREE_MODEL_FILTER (model))
+               {
+                       my_model = (gpointer)gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
+                       g_ptr_array_add (models, my_model);
+
+                       solipa_gtktreemodel_get_models_chain (my_model, models);
+               }
+}
+
 GtkTreeModel
 *solipa_gtktreemodel_copy (GtkTreeModel *model, gboolean only_schema)
 {
@@ -581,28 +634,26 @@ GtkTreeModel
        GType *gtypes;
 
        guint cols;
-       guint col;
 
-       GtkTreeModel *model_child;
+       GPtrArray *models; /* GtkTreeModel */
 
        GtkTreeIter iter;
 
        ret = NULL;
 
        gtypes = solipa_gtktreemodel_get_gtypes (model, &cols);
-       if (gtypes != NULL)
+       if (gtypes != NULL && cols > 0)
                {
-                       if (GTK_IS_TREE_MODEL_FILTER (model))
-                               {
-                                       model_child = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
-                               }
-                       else
+                       models = g_ptr_array_new ();
+                       g_ptr_array_add (models, (gpointer)model);
+
+                       if (GTK_IS_TREE_MODEL_SORT (model) ||
+                           GTK_IS_TREE_MODEL_FILTER (model))
                                {
-                                       model_child = NULL;
+                                       solipa_gtktreemodel_get_models_chain (model, models);
                                }
 
-                       if (GTK_IS_LIST_STORE (model)
-                           || (model_child != NULL && GTK_IS_LIST_STORE (model_child)))
+                       if (GTK_IS_LIST_STORE ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1)))
                                {
                                        ret = GTK_TREE_MODEL (gtk_list_store_newv (cols, gtypes));
 
@@ -610,12 +661,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, 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 ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1)))
                                {
                                        ret = GTK_TREE_MODEL (gtk_tree_store_newv (cols, gtypes));
 
@@ -623,7 +673,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, FALSE, cols, &iter, NULL);
                                                                }
                                                }
                                }
@@ -631,6 +681,8 @@ GtkTreeModel
                                {
                                        g_warning ("GtkTreeModel type not expected.");
                                }
+
+                       g_ptr_array_free (models, TRUE);
                }
 
        return ret;
index f2ddc561d6dfd11e1517602cd0f12742520f2ab1..fea02fb05909f2ca049cb75aa43b13cc0fa00e1d 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 */
@@ -101,6 +103,33 @@ main (int argc, char *argv[])
                {
                        g_error ("Copia del GTK_LIST_STORE vuota.");
                }
+       g_free (copia);
+
+       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), 2, GTK_SORT_ASCENDING);
+
+       copia = solipa_gtktreemodel_copy (sort, 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_free (copia);
 
        /* copia di un TREE_STORE */
        g_printf ("\n\nCOPIA DEL TREE_STORE\n"