]> saetta.ns0.it Git - libgdaex/commitdiff
Started developing of function GdaExGrid::fill_from_datamodel.
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 09:19:56 +0000 (11:19 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 09:19:56 +0000 (11:19 +0200)
Added function GdaExGrid::fill_from_sql.

src/grid.c
src/grid.h
src/gridcolumn.c
tests/grid.c

index 8127bf2b28670f74dd84be648be8e67572ce3eba..247787b8458c02f29efccc6959860ce426e57793 100644 (file)
@@ -50,6 +50,8 @@ typedef struct _GdaExGridPrivate GdaExGridPrivate;
 struct _GdaExGridPrivate
        {
                GPtrArray *columns; /* GdaExGridColumn */
+
+               GtkTreeModel *model;
        };
 
 G_DEFINE_TYPE (GdaExGrid, gdaex_grid, G_TYPE_OBJECT)
@@ -141,6 +143,133 @@ GtkWidget
        return GTK_WIDGET (gdaex_grid_get_view (grid));
 }
 
+gboolean
+gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error)
+{
+       GdaDataModel *dm;
+
+       gchar *_sql;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), FALSE);
+       g_return_val_if_fail (IS_GDAEX (gdaex), FALSE);
+       g_return_val_if_fail (sql != NULL, FALSE);
+
+       _sql = g_strstrip (g_strdup (sql));
+
+       g_return_val_if_fail (g_strcmp0 (_sql, "") != 0, FALSE);
+
+       dm = gdaex_query (gdaex, _sql);
+       return gdaex_grid_fill_from_datamodel (grid, dm, error);
+}
+
+gboolean
+gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error)
+{
+       GdaExGridPrivate *priv;
+
+       GdaDataModelIter *dm_iter;
+       GtkTreeIter iter;
+
+       guint cols;
+       guint col;
+
+       GType col_gtype;
+
+       GdaColumn *gcol;
+       GType gcol_gtype;
+
+       gint *columns;
+       GValue *values;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), FALSE);
+       g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), FALSE);
+
+       priv = GDAEX_GRID_GET_PRIVATE (grid);
+
+       if (priv->model == NULL)
+               {
+                       gdaex_grid_get_model (grid);
+               }
+       g_return_val_if_fail (GTK_IS_TREE_MODEL (priv->model), FALSE);
+
+       dm_iter = gda_data_model_create_iter (dm);
+       g_return_val_if_fail (GDA_IS_DATA_MODEL_ITER (dm_iter), FALSE);
+
+       gtk_list_store_clear (GTK_LIST_STORE (priv->model));
+
+       cols = gtk_tree_model_get_n_columns (priv->model);
+
+       columns = g_malloc0 (cols * sizeof (gint));
+       values = g_malloc0 (cols * sizeof (GValue));
+
+       while (gda_data_model_iter_move_next (dm_iter))
+               {
+                       gtk_list_store_append (GTK_LIST_STORE (priv->model), &iter);
+
+                       for (col = 0; col < cols; col++)
+                               {
+                                       columns[col] = col;
+
+                                       col_gtype = gtk_tree_model_get_column_type (priv->model, col);
+
+                                       GValue gval = {0};
+                                       g_value_init (&gval, col_gtype);
+                                       switch (col_gtype)
+                                               {
+                                                       case G_TYPE_STRING:
+                                                               gcol = gda_data_model_describe_column (dm, col);
+                                                               gcol_gtype = gda_column_get_g_type (gcol);
+
+                                                               switch (gcol_gtype)
+                                                                       {
+                                                                               case G_TYPE_STRING:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (dm_iter, col));
+                                                                                       break;
+
+                                                                               case G_TYPE_BOOLEAN:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (dm_iter, col) ? "X" : "");
+                                                                                       break;
+
+                                                                               default:
+                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (dm_iter, col)));
+                                                                                       break;
+                                                                       }
+
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_INT:
+                                                               g_value_set_int (&gval, gdaex_data_model_iter_get_value_integer_at (dm_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_FLOAT:
+                                                               g_value_set_float (&gval, gdaex_data_model_iter_get_value_float_at (dm_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_DOUBLE:
+                                                               g_value_set_double (&gval, gdaex_data_model_iter_get_value_double_at (dm_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_BOOLEAN:
+                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_value_boolean_at (dm_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       default:
+                                                               values[col] = *gda_value_new_from_string (gdaex_data_model_iter_get_value_stringify_at (dm_iter, col), col_gtype);
+                                                               break;
+                                               }
+                               }
+
+                       gtk_list_store_set_valuesv (GTK_LIST_STORE (priv->model), &iter, columns, values, cols);
+               }
+
+       return TRUE;
+}
+
 /* PRIVATE */
 static void
 gdaex_grid_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
@@ -174,9 +303,6 @@ static GtkTreeModel
 *gdaex_grid_get_model (GdaExGrid *grid)
 {
        /* TODO for now it returns always a GtkListStore */
-
-       GtkListStore *store;
-
        GdaExGridPrivate *priv;
 
        guint col;
@@ -193,9 +319,13 @@ static GtkTreeModel
                        gtype[col] = gdaex_grid_column_get_gtype ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col));
                }
 
-       store = gtk_list_store_newv (priv->columns->len, gtype);
+       if (priv->model != NULL)
+               {
+                       g_object_unref (priv->model);
+               }
+       priv->model = GTK_TREE_MODEL (gtk_list_store_newv (priv->columns->len, gtype));
 
-       return GTK_TREE_MODEL (store);
+       return priv->model;
 }
 
 static GtkTreeView
index 7439fb3ff4678e8183fcaec656ab4436aab7a2f7..a487eaffdfd08928f05cc8465d0e94bc0a73323e 100644 (file)
@@ -27,6 +27,7 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include "gdaex.h"
 #include "gridcolumn.h"
 
 G_BEGIN_DECLS
@@ -65,6 +66,9 @@ void gdaex_grid_clear (GdaExGrid *grid);
 
 GtkWidget *gdaex_grid_get_widget (GdaExGrid *grid);
 
+gboolean gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error);
+gboolean gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error);
+
 
 G_END_DECLS
 
index 2a6c104c945b3c8eebf3e9a86fe3a6cd5cc78b7d..295c7587c4e54aefec94e90315d61bca12c699f7 100644 (file)
@@ -298,6 +298,7 @@ GtkTreeViewColumn
            || priv->type == G_TYPE_DOUBLE)
                {
                        renderer = gtk_cell_renderer_spin_new ();
+                       gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5);
                }
        else
                {
index e072c97bb398f787a8d892f6c5d13eb14ce51c9c..b7a8b95f10d7a949898f2f4c24e6c495d53b04b5 100644 (file)
@@ -22,7 +22,6 @@ int
 main (int argc, char **argv)
 {
        GdaEx *gdaex;
-       GdaDataModel *dm;
 
        GtkWidget *w;
 
@@ -60,7 +59,7 @@ main (int argc, char **argv)
        wgrid = gdaex_grid_get_widget (grid);
        gtk_container_add (GTK_CONTAINER (w), wgrid);
 
-       dm = gdaex_query (gdaex, "SELECT * FROM clients");
+       gdaex_grid_fill_from_sql (grid, gdaex, "SELECT * FROM clients", NULL);
 
        gtk_widget_show_all (w);