]> saetta.ns0.it Git - libgdaex/commitdiff
Added number and date formatting. grid
authorAndrea Zagli <azagli@libero.it>
Sun, 5 Jun 2011 09:01:29 +0000 (11:01 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 5 Jun 2011 09:01:29 +0000 (11:01 +0200)
Bugfixes.

src/gdaex.c
src/grid.c
src/gridcolumn.c
src/gridcolumn.h

index d27976d911a741258ff272ba25ad7d9b5d7260c3..0b9fe38c7337a2b2f2c99f56236c083e3e101f58 100644 (file)
@@ -1312,9 +1312,9 @@ GDateTime
                        else if (gda_value_isa (v, G_TYPE_DATE))
                                {
                                        gdate = gdaex_data_model_get_value_gdate_at (data_model, row, col);
-                                       ret = g_date_time_new_local ((gint)gdate->year,
-                                                                    (gint)gdate->month,
-                                                                    (gint)gdate->day,
+                                       ret = g_date_time_new_local ((gint)g_date_get_year (gdate),
+                                                                    (gint)g_date_get_month (gdate),
+                                                                    (gint)g_date_get_day (gdate),
                                                                     0,
                                                                     0,
                                                                     0.0);
@@ -2034,9 +2034,9 @@ GDateTime
                        else if (gda_value_isa (v, G_TYPE_DATE))
                                {
                                        gdate = gdaex_data_model_iter_get_value_gdate_at (iter, col);
-                                       ret = g_date_time_new_local ((gint)gdate->year,
-                                                                    (gint)gdate->month,
-                                                                    (gint)gdate->day,
+                                       ret = g_date_time_new_local ((gint)g_date_get_year (gdate),
+                                                                    (gint)g_date_get_month (gdate),
+                                                                    (gint)g_date_get_day (gdate),
                                                                     0,
                                                                     0,
                                                                     0.0);
index f4d9304e088a8ccabb958b4c80ca980d4db220f0..e60527b15fde242fa9e4d843a9383a87e531fdec 100644 (file)
@@ -44,6 +44,10 @@ static void gdaex_grid_get_property (GObject *object,
 static GtkTreeModel *gdaex_grid_get_model (GdaExGrid *grid);
 static GtkTreeView *gdaex_grid_get_view (GdaExGrid *grid);
 
+static gchar *grid_format_money (gdouble number,
+                      gint decimals,
+                      gboolean with_currency_symbol);
+
 #define GDAEX_GRID_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GDAEX_TYPE_GRID, GdaExGridPrivate))
 
 typedef struct _GdaExGridPrivate GdaExGridPrivate;
@@ -181,6 +185,9 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
        gint *columns;
        GValue *values;
 
+       GDateTime *gdatetime;
+       gdouble dval;
+
        g_return_val_if_fail (GDAEX_IS_GRID (grid), FALSE);
        g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), FALSE);
 
@@ -230,8 +237,25 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
                                                                                        g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (dm_iter, col) ? "X" : "");
                                                                                        break;
 
+                                                                               case G_TYPE_INT:
+                                                                               case G_TYPE_FLOAT:
+                                                                               case G_TYPE_DOUBLE:
+                                                                                       dval = gdaex_data_model_iter_get_value_double_at (dm_iter, col);
+                                                                                       g_value_set_string (&gval, grid_format_money (dval, gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)), FALSE));
+                                                                                       break;
+
                                                                                default:
-                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (dm_iter, col)));
+                                                                                       if (gcol_gtype == G_TYPE_DATE
+                                                                                           || gcol_gtype == G_TYPE_DATE_TIME
+                                                                                           || gcol_gtype == GDA_TYPE_TIMESTAMP)
+                                                                                               {
+                                                                                                       gdatetime = gdaex_data_model_iter_get_value_gdatetime_at (dm_iter, col);
+                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gcol_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (dm_iter, col)));
+                                                                                               }
                                                                                        break;
                                                                        }
 
@@ -365,6 +389,8 @@ static GtkTreeView
        model = gdaex_grid_get_model (grid);
        view = gtk_tree_view_new_with_model (model);
 
+       gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE);
+
        for (col = 0; col < priv->columns->len; col++)
                {
                        gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
@@ -392,3 +418,74 @@ static GtkTreeView
 
        return GTK_TREE_VIEW (view);
 }
+
+static gchar
+*grid_format_money (gdouble number,
+                      gint decimals,
+                      gboolean with_currency_symbol)
+{
+       gchar *ret;
+
+       GRegex *regex;
+       GError *error;
+
+       gchar *str_format;
+       gchar *str;
+       gssize str_len;
+
+       /* TODO
+        * - get number of decimals from locale
+        * - get grouping char from locale
+        * - get currency symbol from locale
+        */
+
+       ret = g_strdup ("");
+
+       error = NULL;
+       regex = g_regex_new ("(^[-\\d]?\\d+)(\\d\\d\\d)", 0, 0, &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error on creating regex: %s.",
+                                  error->message != NULL ? error->message : "no details");
+                       return "";
+               }
+
+       str_format = g_strdup_printf ("%%0%sf", decimals == 0 ? ".0" : (decimals < 0 ? ".2" : g_strdup_printf (".%d", decimals)));
+       ret = g_strdup_printf (str_format, number);
+
+       while (TRUE)
+               {
+                       error = NULL;
+                       str_len = g_utf8_strlen (ret, -1);
+                       str = g_regex_replace ((const GRegex *)regex,
+                                              ret, str_len, 0,
+                                              "\\1.\\2", 0,
+                                              &error);
+                       if (error != NULL)
+                               {
+                                       g_warning ("Error on regex replacing: %s.",
+                                                  error->message != NULL ? error->message : "no details");
+                                       g_regex_unref (regex);
+                                       return "";
+                               }
+                       if (g_strcmp0 (ret, str) != 0)
+                               {
+                                       ret = g_strdup (str);
+                                       g_free (str);
+                               }
+                       else
+                               {
+                                       break;
+                               }
+               }
+
+       if (with_currency_symbol)
+               {
+                       ret = g_strconcat ("€ ", ret, NULL);
+               }
+
+       g_regex_unref (regex);
+
+       return ret;
+}
+
index 8926c6a833240ab49c5dd6714b27bd4e4318f5cb..89670589eb9f28634d3b3fb2355fda522f3bb8e7 100644 (file)
@@ -53,7 +53,7 @@ struct _GdaExGridColumnPrivate
                gboolean resizable;
                gboolean sortable;
                gboolean reorderable;
-               guint decimals;
+               gint decimals;
 
                GtkTreeViewColumn *vcolumn;
        };
@@ -94,7 +94,7 @@ GdaExGridColumn
                         gboolean resizable,
                         gboolean sortable,
                         gboolean reorderable,
-                        guint decimals)
+                        gint decimals)
 {
        GdaExGridColumn *gdaex_grid_column = GDAEX_GRID_COLUMN (g_object_new (gdaex_grid_column_get_type (), NULL));
 
@@ -255,7 +255,7 @@ gdaex_grid_column_get_reorderable (GdaExGridColumn *column)
 }
 
 void
-gdaex_grid_column_set_decimals (GdaExGridColumn *column, guint decimals)
+gdaex_grid_column_set_decimals (GdaExGridColumn *column, gint decimals)
 {
        g_return_if_fail (GDAEX_IS_GRID_COLUMN (column));
 
@@ -264,7 +264,7 @@ gdaex_grid_column_set_decimals (GdaExGridColumn *column, guint decimals)
        priv->decimals = decimals;
 }
 
-guint
+gint
 gdaex_grid_column_get_decimals (GdaExGridColumn *column)
 {
        g_return_val_if_fail (GDAEX_IS_GRID_COLUMN (column), FALSE);
index 7703bfc14cabc87e64e8741ba6a69856ea00546b..0131a43ed801ee3f45292ebd33d1fc6df872282f 100644 (file)
@@ -63,7 +63,7 @@ GdaExGridColumn *gdaex_grid_column_new (const gchar *title,
                                         gboolean resizable,
                                         gboolean sortable,
                                         gboolean reorderable,
-                                        guint decimals);
+                                        gint decimals);
 
 void gdaex_grid_column_set_title (GdaExGridColumn *column, const gchar *title);
 const gchar *gdaex_grid_column_get_title (GdaExGridColumn *column);
@@ -86,8 +86,8 @@ gboolean gdaex_grid_column_get_sortable (GdaExGridColumn *column);
 void gdaex_grid_column_set_reorderable (GdaExGridColumn *column, gboolean reorderable);
 gboolean gdaex_grid_column_get_reorderable (GdaExGridColumn *column);
 
-void gdaex_grid_column_set_decimals (GdaExGridColumn *column, guint decimals);
-guint gdaex_grid_column_get_decimals (GdaExGridColumn *column);
+void gdaex_grid_column_set_decimals (GdaExGridColumn *column, gint decimals);
+gint gdaex_grid_column_get_decimals (GdaExGridColumn *column);
 
 GtkTreeViewColumn *gdaex_grid_column_get_column (GdaExGridColumn *column);