Chris Winne wrote:
> Hi,
> 
> Here is another issue related to sorting that differs in the gtk1 and
> gtk2 versions (both CVS 19.8.2003 versions).  My gtk1 version provides
> three decimal places and sorts in numeric order.  The gtk2 version I run
> does not display more than 1 decimal place if it is equal to zero (and
> on startup, the normalized value is apparently rounded to to the nearest
> integer).  The sorting appears to be a two stage process, first on the
> number left of the decimal place and then on the number to the right of
> the decimal place.  (I'm sorry I have not checked the code, but that
> appears to be the case). Therefore, I get strange results like 12.192
> being displayed as > 12.22, I think because 192 > 22.

Yes, this does seem to be the case.  Thanks for the good details.
In the gtk2 code there was some pretty funky type-casting going on
in upload_stats_gui2.c and IMHO a somewhat peculiar method of generating
the string for the normalized stat.  It involved multiplying a float by 1000
casting to an unsigned int then printing something like this.

printf("%ul.%ul",  norm / 1000, norm % 1000);

The values are sorted on the (gfloat) value so in your example

12.192 > 12.022

but when transmogrified as described above, it looks like it sorts
incorrectly.

> My preference would be to keep the fixed 3 decimal place display, which
> would work with the apparent current logic of the sorting, with true (not
> rounded) values display at startup. 

I agree and have attached a short patch to fix this bug.  Thanks for the
detailed report.
The patch changes upload_stats_gui2.c to do the following things.

        * Store normalized value in the GtkListStore as a gfloat instead of guint.
        * Adds the upload_stats_gui_compare_floats_func  to do the sorting.

Peace,
Russ
--- gtk1/gtk-gnutella-cvs/src/upload_stats_gui2.c       Wed Sep  3 22:10:18 2003
+++ gtk2/gtk-gnutella-cvs/src/upload_stats_gui2.c       Thu Sep  4 16:42:05 2003
@@ -68,9 +68,25 @@
 
        gtk_tree_model_get(GTK_TREE_MODEL(model), a, column_id, &size_a, (-1));
        gtk_tree_model_get(GTK_TREE_MODEL(model), b, column_id, &size_b, (-1));
-    return size_a == size_b ? 0 : size_a > size_b ? 1 : -1;
+       return size_a > size_b ? 1 : size_a < size_b ? -1 : 0;
 }
 
+
+static gint upload_stats_gui_compare_floats_func(
+       GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
+{
+       gfloat size_a = 0;
+       gfloat size_b = 0;
+       gint column_id = GPOINTER_TO_INT(user_data);
+
+       g_assert(column_id >= 0 && column_id <= c_us_num);
+
+       gtk_tree_model_get(GTK_TREE_MODEL(model), a, column_id, &size_a, (-1));
+       gtk_tree_model_get(GTK_TREE_MODEL(model), b, column_id, &size_b, (-1));
+       return size_a > size_b ? 1 : size_a < size_b ? -1 : 0;
+}
+
+
 static void upload_stats_cell_render_name_func(
        GtkTreeViewColumn *column,
        GtkCellRenderer *cell,
@@ -135,13 +151,13 @@
        GtkTreeIter *iter,
        gpointer data)
 {
-       guint val = 0;
-       gchar tmpstr[16];
+       gfloat val = 0.0;
+       gchar *tmpstr = NULL;
 
        gtk_tree_model_get(model, iter, c_us_norm, &val, (-1));
-       gm_snprintf(tmpstr, sizeof(tmpstr), "%lu.%lu",
-               (gulong) val / 1000, (gulong) val % 1000);
+       tmpstr = g_strdup_printf("%1.3f", val);
        g_object_set(cell, "text", tmpstr, NULL);
+       G_FREE_NULL(tmpstr);
 }
 
 static void on_column_resized(
@@ -232,7 +248,7 @@
                c_us_attempts, us->attempts,
                c_us_complete, us->complete,
                c_us_size, (guint) us->size,
-               c_us_norm, (guint) us->norm * 1000,
+               c_us_norm, (gfloat) us->norm,
                c_us_stat, us,
                (-1));
 }
@@ -253,7 +269,7 @@
                G_TYPE_UINT,            /* Size */
                G_TYPE_UINT,            /* Attempts */
                G_TYPE_UINT,            /* Completed */  
-               G_TYPE_UINT,            /* Normalized */
+               G_TYPE_FLOAT,           /* Normalized */
                G_TYPE_POINTER));       /* struct ul_stats */
        upload_stats_treeview = GTK_TREE_VIEW(
                lookup_widget(main_window, "treeview_ul_stats"));
@@ -285,7 +301,7 @@
                NULL);
        gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
                c_us_norm,
-               (GtkTreeIterCompareFunc) upload_stats_gui_compare_values_func,
+               (GtkTreeIterCompareFunc) upload_stats_gui_compare_floats_func,
                GINT_TO_POINTER(c_us_norm),
                NULL);
 
@@ -308,7 +324,7 @@
        gtk_list_store_set(store, &iter,
                c_us_attempts, (guint) us->attempts,
                c_us_complete, (guint) us->complete,
-               c_us_norm, (guint) (us->norm * 1000),
+               c_us_norm, (gfloat) us->norm,
                (-1));
 }
 

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to