X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=divelist.c;h=de2d35df3b9d5a9a1ac103f51f3955943665e239;hb=52748412fff0974fbc76b3f0a72cd0ba58b37771;hp=2d04eb5b09e4469f13ad5029b6838294497a6d2a;hpb=55352a051cfa7ac519e397cd3d18851e5050517b;p=ext%2Fsubsurface.git diff --git a/divelist.c b/divelist.c index 2d04eb5..de2d35d 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, @@ -299,6 +325,7 @@ static double calculate_airuse(struct dive *dive) int i; for (i = 0; i < MAX_CYLINDERS; i++) { + pressure_t start, end; cylinder_t *cyl = dive->cylinder + i; int size = cyl->type.size.mliter; double kilo_atm; @@ -306,7 +333,9 @@ static double calculate_airuse(struct dive *dive) if (!size) continue; - kilo_atm = (to_ATM(cyl->start) - to_ATM(cyl->end)) / 1000.0; + start = cyl->start.mbar ? cyl->start : cyl->sample_start; + end = cyl->end.mbar ? cyl->end : cyl->sample_end; + kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0; /* Liters of air at 1 atm == milliliters at 1k atm*/ airuse += kilo_atm * size; @@ -317,6 +346,7 @@ static double calculate_airuse(struct dive *dive) static int calculate_sac(struct dive *dive) { double airuse, pressure, sac; + int duration, i; airuse = calculate_airuse(dive); if (!airuse) @@ -324,14 +354,38 @@ static int calculate_sac(struct dive *dive) if (!dive->duration.seconds) return 0; + /* find and eliminate long surface intervals */ + duration = dive->duration.seconds; + for (i = 0; i < dive->samples; i++) { + if (dive->sample[i].depth.mm < 100) { /* less than 10cm */ + int end = i + 1; + while (end < dive->samples && dive->sample[end].depth.mm < 100) + end++; + /* we only want the actual surface time during a dive */ + if (end < dive->samples) { + end--; + duration -= dive->sample[end].time.seconds - + dive->sample[i].time.seconds; + i = end + 1; + } + } + } /* Mean pressure in atm: 1 atm per 10m */ pressure = 1 + (dive->meandepth.mm / 10000.0); - sac = airuse / pressure * 60 / dive->duration.seconds; + sac = airuse / pressure * 60 / duration; /* milliliters per minute.. */ return sac * 1000; } +void update_cylinder_related_info(struct dive *dive) +{ + if (dive != NULL) { + dive->sac = calculate_sac(dive); + dive->otu = calculate_otu(dive); + } +} + static void get_string(char **str, const char *s) { int len; @@ -340,8 +394,8 @@ static void get_string(char **str, const char *s) if (!s) s = ""; len = strlen(s); - if (len > 40) - len = 40; + if (len > 60) + len = 60; n = malloc(len+1); memcpy(n, s, len); n[len] = 0; @@ -375,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); @@ -446,11 +501,11 @@ static void fill_dive_list(void) store = GTK_LIST_STORE(dive_list.model); - for (i = 0; i < dive_table.nr; i++) { + i = dive_table.nr; + while (--i >= 0) { struct dive *dive = dive_table.dives[i]; - dive->otu = calculate_otu(dive); - dive->sac = calculate_sac(dive); + update_cylinder_related_info(dive); gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, DIVE_INDEX, i, @@ -495,6 +550,20 @@ static void realize_cb(GtkWidget *tree_view, gpointer userdata) gtk_widget_grab_focus(tree_view); } +static void row_activated_cb(GtkTreeView *tree_view, + GtkTreePath *path, + GtkTreeViewColumn *column, + GtkTreeModel *model) +{ + int index; + GtkTreeIter iter; + + if (!gtk_tree_model_get_iter(model, &iter, path)) + return; + gtk_tree_model_get(model, &iter, DIVE_INDEX, &index, -1); + edit_dive_info(get_dive(index)); +} + GtkWidget *dive_list_create(void) { GtkTreeSelection *selection; @@ -503,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 */ @@ -523,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, 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); @@ -540,6 +611,7 @@ GtkWidget *dive_list_create(void) NULL); g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL); + g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), dive_list.model); g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model); dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);