From af290d5eb281662d336b8186a9df1a489d78ec9d Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 7 Dec 2011 11:58:16 -0800 Subject: Add typical 0 to 5 star rating for dives This works ok-ish, but doesn't allow us to click on the stars and edit them in the divelist, which a user might expect to be able to do - in most "star rating UIs" you simply click on the n-th star to set that rating. Here you need to edit the dive and pick the rating from a drop down menu. Minor oddity: you can actually (if you force it) write anything you want into the star rating. But anything that isn't one of the predefined strings simply results in a zero star rating. Overall the UI feels a bit... forced. But I think this is quite useful anyway. Signed-off-by: Dirk Hohndel --- divelist.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'divelist.c') diff --git a/divelist.c b/divelist.c index a8afd3ac2..3976382aa 100644 --- a/divelist.c +++ b/divelist.c @@ -25,7 +25,7 @@ struct DiveList { GtkWidget *tree_view; GtkWidget *container_widget; GtkListStore *model; - GtkTreeViewColumn *nr, *date, *depth, *duration, *location; + GtkTreeViewColumn *nr, *date, *stars, *depth, *duration, *location; GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac, *otu; int changed; }; @@ -40,6 +40,7 @@ enum { DIVE_INDEX = 0, DIVE_NR, /* int: dive->nr */ DIVE_DATE, /* time_t: dive->when */ + DIVE_RATING, /* int: 0-5 stars */ DIVE_DEPTH, /* int: dive->maxdepth in mm */ DIVE_DURATION, /* int: in seconds */ DIVE_TEMPERATURE, /* int: in mkelvin */ @@ -89,6 +90,31 @@ static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model) } } +const char *star_strings[] = { + ZERO_STARS, + ONE_STARS, + TWO_STARS, + THREE_STARS, + FOUR_STARS, + FIVE_STARS +}; + +static void star_data_func(GtkTreeViewColumn *col, + GtkCellRenderer *renderer, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer data) +{ + int nr_stars; + char buffer[40]; + + gtk_tree_model_get(model, iter, DIVE_RATING, &nr_stars, -1); + if (nr_stars < 0 || nr_stars > 5) + nr_stars = 0; + snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]); + g_object_set(renderer, "text", buffer, NULL); +} + static void date_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, @@ -403,6 +429,7 @@ static void fill_one_dive(struct dive *dive, DIVE_NR, dive->number, DIVE_LOCATION, location, DIVE_CYLINDER, cylinder, + DIVE_RATING, dive->rating, DIVE_SAC, dive->sac, DIVE_OTU, dive->otu, -1); @@ -545,6 +572,7 @@ GtkWidget *dive_list_create(void) G_TYPE_INT, /* index */ G_TYPE_INT, /* nr */ G_TYPE_INT, /* Date */ + G_TYPE_INT, /* Star rating */ G_TYPE_INT, /* Depth */ G_TYPE_INT, /* Duration */ G_TYPE_INT, /* Temperature */ @@ -565,6 +593,7 @@ GtkWidget *dive_list_create(void) dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE); gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1); dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE); + dive_list.stars = divelist_column(&dive_list, DIVE_RATING, "Rating", star_data_func, PANGO_ALIGN_LEFT, TRUE); dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE); dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE); dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature); -- cgit v1.2.3-70-g09d2 From 52748412fff0974fbc76b3f0a72cd0ba58b37771 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 7 Dec 2011 20:49:22 -0800 Subject: Fix some issues with star rating code To waste less space in the tree view heading we simply put a star in the heading instead of "Rating". We now treat "zero stars" to mean "not rated" and don't store that value in the XML file. Rating is no longer a top level tag in the dive entry but instead a property of the dive tag. Signed-off-by: Dirk Hohndel --- divelist.c | 2 +- save-xml.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'divelist.c') diff --git a/divelist.c b/divelist.c index 3976382aa..de2d35df3 100644 --- a/divelist.c +++ b/divelist.c @@ -593,7 +593,7 @@ GtkWidget *dive_list_create(void) dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE); gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1); dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE); - dive_list.stars = divelist_column(&dive_list, DIVE_RATING, "Rating", star_data_func, PANGO_ALIGN_LEFT, TRUE); + dive_list.stars = divelist_column(&dive_list, DIVE_RATING, UTF8_BLACKSTAR, star_data_func, PANGO_ALIGN_LEFT, TRUE); dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE); dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE); dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature); diff --git a/save-xml.c b/save-xml.c index 80c26b882..c9085db51 100644 --- a/save-xml.c +++ b/save-xml.c @@ -182,7 +182,6 @@ static void save_overview(FILE *f, struct dive *dive) show_location(f, dive); show_utf8(f, dive->divemaster, " ","\n"); show_utf8(f, dive->buddy, " ","\n"); - fprintf(f, " %d\n", dive->rating); show_utf8(f, dive->notes, " ","\n"); } @@ -263,6 +262,8 @@ static void save_dive(FILE *f, struct dive *dive) fputs("number) fprintf(f, " number='%d'", dive->number); + if (dive->rating) + fprintf(f, " rating='%d'", dive->rating); fprintf(f, " date='%04u-%02u-%02u'", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); fprintf(f, " time='%02u:%02u:%02u'", -- cgit v1.2.3-70-g09d2