From: Andrea Zagli Date: Wed, 29 Jan 2014 11:37:32 +0000 (+0100) Subject: Added functions GdaExGrid::fill_from_sql_with_missing_func and GdaExGrid::fill_from_d... X-Git-Tag: 0.4.0~4 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=6994a674a5befb502a10e5f6091733f3f1bb5636;p=libgdaex Added functions GdaExGrid::fill_from_sql_with_missing_func and GdaExGrid::fill_from_datamodel_with_missing_func (closes #843). --- diff --git a/src/gdaex.c b/src/gdaex.c index 39e633a..57e86e9 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -1,7 +1,7 @@ /* * gdaex.c * - * Copyright (C) 2005-2013 Andrea Zagli + * Copyright (C) 2005-2014 Andrea Zagli * * This file is part of libgdaex. * @@ -2833,7 +2833,9 @@ gdaex_fill_liststore_from_sql_with_missing_func (GdaEx *gdaex, g_return_if_fail (g_strcmp0 (_sql, "") != 0); dm = gdaex_query (gdaex, _sql); + g_free (_sql); gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, cols_format_func, missing_func, user_data); + g_object_unref (dm); } void diff --git a/src/grid.c b/src/grid.c index ded508e..088b3d2 100644 --- a/src/grid.c +++ b/src/grid.c @@ -1,7 +1,7 @@ /* * grid.c * - * Copyright (C) 2010-2013 Andrea Zagli + * Copyright (C) 2010-2014 Andrea Zagli * * This file is part of libgdaex. * @@ -232,7 +232,11 @@ GtkWidget } gboolean -gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error) +gdaex_grid_fill_from_sql_with_missing_func (GdaExGrid *grid, + GdaEx *gdaex, + const gchar *sql, + GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data, + GError **error) { GdaDataModel *dm; @@ -250,14 +254,17 @@ gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GErro dm = gdaex_query (gdaex, _sql); g_free (_sql); - ret = gdaex_grid_fill_from_datamodel (grid, dm, error); + ret = gdaex_grid_fill_from_datamodel_with_missing_func (grid, dm, missing_func, user_data, error); g_object_unref (dm); return ret; } gboolean -gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error) +gdaex_grid_fill_from_datamodel_with_missing_func (GdaExGrid *grid, + GdaDataModel *dm, + GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data, + GError **error) { GdaExGridPrivate *priv; @@ -284,6 +291,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro GDateTime *gdatetime; gdouble dval; + gboolean call_missing_func; + g_return_val_if_fail (GDAEX_IS_GRID (grid), FALSE); g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), FALSE); @@ -304,6 +313,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro columns = g_new0 (gint, cols); values = g_new0 (GValue, cols); + call_missing_func = FALSE; + while (gda_data_model_iter_move_next (dm_iter)) { gtk_list_store_append (GTK_LIST_STORE (priv->model), &iter); @@ -311,6 +322,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro cols_sorted = priv->columns->len; for (col = 0; col < priv->columns->len; col++) { + GValue gval = {0}; + columns[col] = col; col_gtype = gtk_tree_model_get_column_type (priv->model, col); @@ -318,69 +331,77 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro gdaex_col = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col); field_name = gdaex_grid_column_get_field_name (gdaex_col); - GValue gval = {0}; g_value_init (&gval, col_gtype); - switch (col_gtype) - { - case G_TYPE_STRING: - gda_col = gda_data_model_describe_column (dm, col); - gda_col_gtype = gda_column_get_g_type (gda_col); - - switch (gda_col_gtype) - { - case G_TYPE_STRING: - g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name)); - break; - - case G_TYPE_BOOLEAN: - g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name) ? "X" : ""); - break; - - case G_TYPE_INT: - case G_TYPE_FLOAT: - case G_TYPE_DOUBLE: - dval = gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name); - g_value_set_string (&gval, gdaex_format_money (dval, gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)), FALSE)); - break; - - default: - if (gda_col_gtype == G_TYPE_DATE - || gda_col_gtype == G_TYPE_DATE_TIME - || gda_col_gtype == GDA_TYPE_TIMESTAMP) - { - gdatetime = gdaex_data_model_iter_get_field_value_gdatetime_at (dm_iter, field_name); - /* TODO find default format from locale */ - g_value_set_string (&gval, g_date_time_format (gdatetime, gda_col_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S")); - g_date_time_unref (gdatetime); - } - else - { - g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name)); - } - break; - } - - break; - - case G_TYPE_INT: - g_value_set_int (&gval, gdaex_data_model_iter_get_field_value_integer_at (dm_iter, field_name)); - break; - - case G_TYPE_FLOAT: - g_value_set_float (&gval, gdaex_data_model_iter_get_field_value_float_at (dm_iter, field_name)); - break; - case G_TYPE_DOUBLE: - g_value_set_double (&gval, gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name)); - break; - - case G_TYPE_BOOLEAN: - g_value_set_boolean (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name)); - break; + /* checking if field exists */ + if (gda_data_model_get_column_index (dm, field_name) == -1) + { + call_missing_func = TRUE; + } + else + { + switch (col_gtype) + { + case G_TYPE_STRING: + gda_col = gda_data_model_describe_column (dm, col); + gda_col_gtype = gda_column_get_g_type (gda_col); - default: - gval = *gda_value_new_from_string (gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name), col_gtype); - break; + switch (gda_col_gtype) + { + case G_TYPE_STRING: + g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name)); + break; + + case G_TYPE_BOOLEAN: + g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name) ? "X" : ""); + break; + + case G_TYPE_INT: + case G_TYPE_FLOAT: + case G_TYPE_DOUBLE: + dval = gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name); + g_value_set_string (&gval, gdaex_format_money (dval, gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)), FALSE)); + break; + + default: + if (gda_col_gtype == G_TYPE_DATE + || gda_col_gtype == G_TYPE_DATE_TIME + || gda_col_gtype == GDA_TYPE_TIMESTAMP) + { + gdatetime = gdaex_data_model_iter_get_field_value_gdatetime_at (dm_iter, field_name); + /* TODO find default format from locale */ + g_value_set_string (&gval, g_date_time_format (gdatetime, gda_col_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S")); + g_date_time_unref (gdatetime); + } + else + { + g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name)); + } + break; + } + + break; + + case G_TYPE_INT: + g_value_set_int (&gval, gdaex_data_model_iter_get_field_value_integer_at (dm_iter, field_name)); + break; + + case G_TYPE_FLOAT: + g_value_set_float (&gval, gdaex_data_model_iter_get_field_value_float_at (dm_iter, field_name)); + break; + + case G_TYPE_DOUBLE: + g_value_set_double (&gval, gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name)); + break; + + case G_TYPE_BOOLEAN: + g_value_set_boolean (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name)); + break; + + default: + gval = *gda_value_new_from_string (gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name), col_gtype); + break; + } } values[col] = gval; @@ -428,11 +449,28 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro } gtk_list_store_set_valuesv (GTK_LIST_STORE (priv->model), &iter, columns, values, cols); + if (call_missing_func + && missing_func != NULL) + { + missing_func (GTK_LIST_STORE (priv->model), &iter, user_data); + } } return TRUE; } +gboolean +gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error) +{ + return gdaex_grid_fill_from_sql_with_missing_func (grid, gdaex, sql, NULL, NULL, error); +} + +gboolean +gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error) +{ + return gdaex_grid_fill_from_datamodel_with_missing_func (grid, dm, NULL, NULL, error); +} + #ifdef SOLIPA_FOUND void gdaex_grid_set_solipa (GdaExGrid *grid, Solipa *solipa) diff --git a/src/grid.h b/src/grid.h index a7f7873..0f3ff31 100644 --- a/src/grid.h +++ b/src/grid.h @@ -1,7 +1,7 @@ /* * grid.h * - * Copyright (C) 2010-2013 Andrea Zagli + * Copyright (C) 2010-2014 Andrea Zagli * * This file is part of libgdaex. * @@ -73,6 +73,18 @@ void gdaex_grid_clear (GdaExGrid *grid); GtkWidget *gdaex_grid_get_widget (GdaExGrid *grid); +typedef void (*GdaExGridFillListStoreMissingFunc) (GtkListStore *lstore, GtkTreeIter *iter, gpointer user_data); + +gboolean gdaex_grid_fill_from_sql_with_missing_func (GdaExGrid *grid, + GdaEx *gdaex, + const gchar *sql, + GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data, + GError **error); +gboolean gdaex_grid_fill_from_datamodel_with_missing_func (GdaExGrid *grid, + GdaDataModel *dm, + GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data, + GError **error); + 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); diff --git a/tests/grid.c b/tests/grid.c index 294fe16..3fa3509 100644 --- a/tests/grid.c +++ b/tests/grid.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2013 Andrea Zagli + * Copyright (C) 2011-2014 Andrea Zagli * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,14 @@ GdaExGridColumn *gcol_birthday; +static void +missing_func (GtkListStore *lstore, GtkTreeIter *iter, gpointer user_data) +{ + gtk_list_store_set (lstore, iter, + 7, "missing", + -1); +} + static void on_btn_birthday_clicked (GtkButton *button, gpointer user_data) @@ -91,10 +99,13 @@ main (int argc, char **argv) gcol = gdaex_grid_column_new ("Married", "married", G_TYPE_BOOLEAN, TRUE, TRUE, TRUE, TRUE, -1); gdaex_grid_add_column (grid, gcol); + gcol = gdaex_grid_column_new_defaults ("Missing", "missing", G_TYPE_STRING); + gdaex_grid_add_column (grid, gcol); + wgrid = gdaex_grid_get_widget (grid); gtk_container_add (GTK_CONTAINER (scrolledw), wgrid); - gdaex_grid_fill_from_sql (grid, gdaex, "SELECT * FROM clients", NULL); + gdaex_grid_fill_from_sql_with_missing_func (grid, gdaex, "SELECT * FROM clients", missing_func, NULL, NULL); hbtnbox = gtk_hbutton_box_new (); gtk_box_pack_start (GTK_BOX (vbox), hbtnbox, FALSE, FALSE, 0);