]> saetta.ns0.it Git - libgdaex/commitdiff
Added ability to rightly sort numeric and date columns (closes #739).
authorAndrea Zagli <azagli@libero.it>
Sun, 3 Nov 2013 09:07:11 +0000 (10:07 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 3 Nov 2013 09:07:11 +0000 (10:07 +0100)
.anjuta/session/anjuta.session
.anjuta/session/dock-layout.xml
.anjuta_sym_db.db
src/grid.c

index df1b1e287a8c173d81aceac8236b1c8ee8313619..6527328d82669877fb0e3f546a040ab0474a9567 100644 (file)
@@ -3,12 +3,13 @@ Maximized=1
 Geometry=1680x998+0+27
 
 [File Loader]
-Files=../../src/grid.c#289
+Files=../../src/grid.c#809%%%../../tests/grid.c#88
 
 [Document Manager]
 bookmarks=<?xml version="1.0" encoding="UTF-8"?>\n<bookmarks/>\n
 
 [Project Manager]
+Shortcut=libgdaex//tests//fill_liststore%%%libgdaex//tests//getsql%%%libgdaex//tests//grid%%%libgdaex//tests//query_editor%%%libgdaex//tests//select%%%libgdaex//tests//test_prefix
 Expand=libgdaex%%%libgdaex//src%%%libgdaex//src//libgdaex.la
 
 [Execution]
index d3a3dbf9c1514d899ff916d3b929163a4f85d56d..98616c65a9c644f7acbfac923e4fd66a0ebfca71 100644 (file)
@@ -1,2 +1,2 @@
 <?xml version="1.0"?>
-<dock-layout><layout name="__default__"><dock name="__dock_1" floating="no" width="-1" height="-1" floatx="0" floaty="0" skip-taskbar="yes"><paned orientation="horizontal" locked="no" iconified="no" closed="no" position="1320"><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="0"><item name="AnjutaDocumentManager" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpDisplay" orientation="vertical" locked="yes" iconified="no" closed="no"/><item name="AnjutaTerminal" orientation="vertical" locked="yes" iconified="no" closed="no"/></notebook><paned orientation="vertical" locked="no" iconified="no" closed="no" position="284"><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="1"><item name="AnjutaFileManager" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaProjectManager" orientation="vertical" locked="no" iconified="no" closed="no"/></notebook><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="0"><item name="AnjutaSymbolDB" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpIndex" orientation="vertical" locked="no" iconified="no" closed="no"/></notebook></paned></paned></dock></layout></dock-layout>
+<dock-layout><layout name="__default__"><dock name="__dock_1" floating="no" width="-1" height="-1" floatx="0" floaty="0" skip-taskbar="yes"><paned orientation="horizontal" locked="no" iconified="no" closed="no" position="1320"><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="2"><item name="AnjutaDocumentManager" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpDisplay" orientation="vertical" locked="yes" iconified="no" closed="no"/><item name="AnjutaTerminal" orientation="vertical" locked="yes" iconified="no" closed="no"/></notebook><paned orientation="vertical" locked="no" iconified="no" closed="no" position="284"><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="0"><item name="AnjutaFileManager" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaProjectManager" orientation="vertical" locked="no" iconified="no" closed="no"/></notebook><notebook orientation="vertical" locked="no" iconified="no" closed="no" page="0"><item name="AnjutaSymbolDB" orientation="vertical" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpIndex" orientation="vertical" locked="no" iconified="no" closed="no"/></notebook></paned></paned></dock></layout></dock-layout>
index 6d7c67ca5afb6f946a22cd99041d37b784af121c..8c42f428fbeabcaf3e2d6124dfb70b9e3d804fef 100644 (file)
Binary files a/.anjuta_sym_db.db and b/.anjuta_sym_db.db differ
index 608abfa84d9350ed6aadbe2dc64150549d543446..28e3f0fe80dacfbeec149e0814dcf2937b35504e 100644 (file)
@@ -268,13 +268,19 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
        GdaDataModelIter *dm_iter;
        GtkTreeIter iter;
 
+       const gchar *field_name;
+
        guint cols;
+       guint cols_sorted;
        guint col;
 
        GType col_gtype;
 
-       GdaColumn *gcol;
-       GType gcol_gtype;
+       GdaExGridColumn *gdaex_col;
+       GType gdaex_col_gtype;
+
+       GdaColumn *gda_col;
+       GType gda_col_gtype;
 
        gint *columns;
        GValue *values;
@@ -299,88 +305,130 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
        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));
+       columns = g_new0 (gint, cols);
+       values = g_new0 (GValue, cols);
 
        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++)
+                       cols_sorted = priv->columns->len;
+                       for (col = 0; col < priv->columns->len; col++)
                                {
                                        columns[col] = col;
 
                                        col_gtype = gtk_tree_model_get_column_type (priv->model, col);
 
+                                       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:
-                                                               gcol = gda_data_model_describe_column (dm, col);
-                                                               gcol_gtype = gda_column_get_g_type (gcol);
+                                                               gda_col = gda_data_model_describe_column (dm, col);
+                                                               gda_col_gtype = gda_column_get_g_type (gda_col);
 
-                                                               switch (gcol_gtype)
+                                                               switch (gda_col_gtype)
                                                                        {
                                                                                case G_TYPE_STRING:
-                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (dm_iter, col));
+                                                                                       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_value_boolean_at (dm_iter, col) ? "X" : "");
+                                                                                       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_value_double_at (dm_iter, col);
+                                                                                       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 (gcol_gtype == G_TYPE_DATE
-                                                                                           || gcol_gtype == G_TYPE_DATE_TIME
-                                                                                           || gcol_gtype == GDA_TYPE_TIMESTAMP)
+                                                                                       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_value_gdatetime_at (dm_iter, col);
+                                                                                                       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, gcol_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
+                                                                                                       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, gda_value_stringify (gda_data_model_iter_get_value_at (dm_iter, col)));
+                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name));
                                                                                                }
                                                                                        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;
+                                                               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_value_float_at (dm_iter, col));
-                                                               values[col] = gval;
+                                                               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_value_double_at (dm_iter, col));
-                                                               values[col] = gval;
+                                                               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_value_boolean_at (dm_iter, col));
-                                                               values[col] = gval;
+                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name));
                                                                break;
 
                                                        default:
-                                                               values[col] = *gda_value_new_from_string (gdaex_data_model_iter_get_value_stringify_at (dm_iter, col), col_gtype);
+                                                               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;
+
+                                       gdaex_col_gtype = gdaex_grid_column_get_gtype (gdaex_col);
+
+                                       if ((gdaex_col_gtype == G_TYPE_DATE
+                                            || gdaex_col_gtype == G_TYPE_DATE_TIME
+                                            || gdaex_col_gtype == GDA_TYPE_TIMESTAMP
+                                            || ((gdaex_col_gtype == G_TYPE_INT
+                                                 || gdaex_col_gtype == G_TYPE_FLOAT
+                                                 || gdaex_col_gtype == G_TYPE_DOUBLE)
+                                                && gdaex_grid_column_get_decimals (gdaex_col) > -1))
+                                           && gdaex_grid_column_get_reorderable (gdaex_col))
+                                               {
+                                                       columns[cols_sorted] = cols_sorted;
+
+                                                       GValue gval = {0};
+                                                       if (gdaex_col_gtype == G_TYPE_DATE
+                                                           || gdaex_col_gtype == G_TYPE_DATE_TIME
+                                                           || gdaex_col_gtype == GDA_TYPE_TIMESTAMP)
+                                                               {
+                                                                       g_value_init (&gval, G_TYPE_STRING);
+                                                                       gdatetime = gdaex_data_model_iter_get_field_value_gdatetime_at (dm_iter, field_name);
+                                                                       g_value_set_string (&gval, gdatetime != NULL ? g_date_time_format (gdatetime, gdaex_col_gtype == G_TYPE_DATE ? "%Y%m%d" : "%Y%m%d%H%M%S") : "");
+                                                                       g_date_time_unref (gdatetime);
+                                                               }
+                                                       else if (gdaex_col_gtype == G_TYPE_INT)
+                                                               {
+                                                                       g_value_init (&gval, G_TYPE_INT);
+                                                                       g_value_set_int (&gval, gdaex_data_model_iter_get_field_value_integer_at (dm_iter, field_name));
+                                                               }
+                                                       else if (gdaex_col_gtype == G_TYPE_FLOAT)
+                                                               {
+                                                                       g_value_init (&gval, G_TYPE_FLOAT);
+                                                                       g_value_set_float (&gval, gdaex_data_model_iter_get_field_value_float_at (dm_iter, field_name));
+                                                               }
+                                                       else if (gdaex_col_gtype == G_TYPE_DOUBLE)
+                                                               {
+                                                                       g_value_init (&gval, G_TYPE_DOUBLE);
+                                                                       g_value_set_double (&gval, gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name));
+                                                               }
+                                                       values[cols_sorted] = gval;
+                                                       cols_sorted++;
+                                               }
                                }
 
                        gtk_list_store_set_valuesv (GTK_LIST_STORE (priv->model), &iter, columns, values, cols);
@@ -438,7 +486,11 @@ static GtkTreeModel
        /* TODO for now it returns always a GtkListStore */
        GdaExGridPrivate *priv;
 
+       guint cols;
        guint col;
+
+       GdaExGridColumn *gcolumn;
+
        GType *gtype;
        GType col_gtype;
 
@@ -446,20 +498,39 @@ static GtkTreeModel
 
        priv = GDAEX_GRID_GET_PRIVATE (grid);
 
-       gtype = g_new0 (GType, priv->columns->len);
+       cols = priv->columns->len;
+       gtype = g_new0 (GType, cols);
 
        for (col = 0; col < priv->columns->len; col++)
                {
-                       col_gtype = gdaex_grid_column_get_gtype ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col));
+                       gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
+
+                       col_gtype = gdaex_grid_column_get_gtype (gcolumn);
                        if (col_gtype == G_TYPE_DATE
                            || col_gtype == G_TYPE_DATE_TIME
                            || col_gtype == GDA_TYPE_TIMESTAMP
                            || ((col_gtype == G_TYPE_INT
                                 || col_gtype == G_TYPE_FLOAT
                                 || col_gtype == G_TYPE_DOUBLE)
-                               && gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)) > -1))
+                               && gdaex_grid_column_get_decimals (gcolumn) > -1))
                                {
                                        gtype[col] = G_TYPE_STRING;
+
+                                       if (gdaex_grid_column_get_reorderable (gcolumn))
+                                               {
+                                                       /* add one column for sorting */
+                                                       gtype = g_renew (GType, gtype, ++cols);
+                                                       if (col_gtype == G_TYPE_DATE
+                                                           || col_gtype == G_TYPE_DATE_TIME
+                                                           || col_gtype == GDA_TYPE_TIMESTAMP)
+                                                               {
+                                                                       gtype[cols - 1] = G_TYPE_STRING;
+                                                               }
+                                                       else
+                                                               {
+                                                                       gtype[cols - 1] = col_gtype;
+                                                               }
+                                               }
                                }
                        else
                                {
@@ -471,7 +542,7 @@ static GtkTreeModel
                {
                        g_object_unref (priv->model);
                }
-       priv->model = GTK_TREE_MODEL (gtk_list_store_newv (priv->columns->len, gtype));
+       priv->model = GTK_TREE_MODEL (gtk_list_store_newv (cols, gtype));
 
        return priv->model;
 }
@@ -489,7 +560,10 @@ static GtkTreeView
 
        GList *cells;
 
+       GType col_gtype;
+
        guint col;
+       guint cols_sorted;
 
        GtkWidget *mitem;
        GtkWidget *submitem;
@@ -524,6 +598,7 @@ static GtkTreeView
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (mitem), submitem);
        gtk_widget_show (submitem);
 
+       cols_sorted = 0;
        for (col = 0; col < priv->columns->len; col++)
                {
                        gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
@@ -533,7 +608,8 @@ static GtkTreeView
                                        cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (vcolumn));
                                        cells = g_list_first (cells);
 
-                                       if (gdaex_grid_column_get_gtype (gcolumn) == G_TYPE_BOOLEAN)
+                                       col_gtype = gdaex_grid_column_get_gtype (gcolumn);
+                                       if (col_gtype == G_TYPE_BOOLEAN)
                                                {
                                                        gtk_tree_view_column_add_attribute (vcolumn, (GtkCellRenderer *)cells->data, "active", col);
                                                }
@@ -544,7 +620,20 @@ static GtkTreeView
 
                                        if (gdaex_grid_column_get_reorderable (gcolumn))
                                                {
-                                                       gtk_tree_view_column_set_sort_column_id (vcolumn, col);
+                                                       if (col_gtype == G_TYPE_DATE
+                                                           || col_gtype == G_TYPE_DATE_TIME
+                                                           || col_gtype == GDA_TYPE_TIMESTAMP
+                                                           || ((col_gtype == G_TYPE_INT
+                                                                || col_gtype == G_TYPE_FLOAT
+                                                                || col_gtype == G_TYPE_DOUBLE)
+                                                                && gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)) > -1))
+                                                               {
+                                                                       gtk_tree_view_column_set_sort_column_id (vcolumn, priv->columns->len + cols_sorted++);
+                                                               }
+                                                       else
+                                                               {
+                                                                       gtk_tree_view_column_set_sort_column_id (vcolumn, col);
+                                                               }
                                                }
 
                                        gtk_tree_view_append_column (GTK_TREE_VIEW (view), vcolumn);
@@ -711,7 +800,14 @@ gdaex_grid_on_export_menu_activate (GtkMenuItem *menuitem,
                        for (col = 0; col < priv->columns->len; col++)
                                {
                                        gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
-                                       g_string_append_printf (gstr, "|%s", gdaex_grid_column_get_title (gcolumn));
+                                       if (gdaex_grid_column_get_visible (gcolumn))
+                                               {
+                                                       g_string_append_printf (gstr, "|%s", gdaex_grid_column_get_title (gcolumn));
+                                               }
+                                       else
+                                               {
+                                                       g_string_append (gstr, "|{SKIP}");
+                                               }
                                }
 
                        gchar **columns_title = g_strsplit (gstr->str + 1, "|", -1);