X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=divelist.c;h=c537e81862d3cd571c2a36f73fdcece77576b877;hb=2a45f6dbc3aa9bef7832e63e30076eff704153c9;hp=2559c367eccdb173eec2898cb8b6a4616aa9a0fd;hpb=53ac61f23538b8d71a7eb579dd5fc585e71982ab;p=ext%2Fsubsurface.git diff --git a/divelist.c b/divelist.c index 2559c36..c537e81 100644 --- a/divelist.c +++ b/divelist.c @@ -13,9 +13,7 @@ */ enum { DIVE_INDEX = 0, - DIVE_DATESTR, /* "Thu Jun 17th, 2011" or whatever */ DIVE_DATE, /* time_t: dive->when */ - DIVE_DEPTHSTR, /* "67" in ft or whatever */ DIVE_DEPTH, /* int: dive->maxdepth in mm */ DIVE_DURATIONSTR, /* "47" in minutes */ DIVE_DURATION, /* int: in seconds */ @@ -60,34 +58,43 @@ static const char *monthname(int mon) return month_array[mon]; } -static void get_date(struct dive *dive, int *val, char **str) +static void date_data_func(GtkTreeViewColumn *col, + GtkCellRenderer *renderer, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer data) { + int val; struct tm *tm; - time_t when = dive->when; + time_t when; char buffer[40]; + gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1); + /* 2038 problem */ - *val = when; + when = val; - tm = gmtime(&dive->when); + tm = gmtime(&when); snprintf(buffer, sizeof(buffer), "%s, %s %d, %d %02d:%02d", weekday(tm->tm_wday), monthname(tm->tm_mon), tm->tm_mday, tm->tm_year + 1900, tm->tm_hour, tm->tm_min); - *str = strdup(buffer); + g_object_set(renderer, "text", buffer, NULL); } -static void get_depth(struct dive *dive, int *val, char **str) +static void depth_data_func(GtkTreeViewColumn *col, + GtkCellRenderer *renderer, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer data) { - int len; - int depth = dive->maxdepth.mm; - int integer, frac = -1; - char buffer[10]; + int depth, integer, frac, len; + char buffer[40]; + + gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1); - *val = depth; - *str = ""; switch (output_units.length) { case METERS: /* To tenths of meters */ @@ -100,18 +107,17 @@ static void get_depth(struct dive *dive, int *val, char **str) /* Rounding? */ break; case FEET: - integer = to_feet(dive->maxdepth); + integer = mm_to_feet(depth) + 0.5; frac = -1; break; default: return; } - len = snprintf(buffer, sizeof(buffer), - "%d", integer); + len = snprintf(buffer, sizeof(buffer), "%d", integer); if (frac >= 0) - len += snprintf(buffer+len, sizeof(buffer)-len, - ".%d", frac); - *str = strdup(buffer); + len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac); + + g_object_set(renderer, "text", buffer, NULL); } static void get_duration(struct dive *dive, int *val, char **str) @@ -169,31 +175,77 @@ static void get_nitrox(struct dive *dive, int *val, char **str) } } +/* + * Return air usage (in liters). + */ +static double calculate_airuse(struct dive *dive) +{ + double airuse = 0; + int i; + + for (i = 0; i < MAX_CYLINDERS; i++) { + cylinder_t *cyl = dive->cylinder + i; + int size = cyl->type.size.mliter; + double kilo_atm; + + if (!size) + continue; + + kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0; + + /* Liters of air at 1 atm == milliliters at 1k atm*/ + airuse += kilo_atm * size; + } + return airuse; +} + static void get_sac(struct dive *dive, int *val, char **str) { + const double liters_per_cuft = 28.317; + double airuse, pressure, sac; + const char *fmt, *unit; + char buffer[20]; + *val = 0; *str = ""; + airuse = calculate_airuse(dive); + if (!airuse) + return; + if (!dive->duration.seconds) + return; + + /* Mean pressure in atm: 1 atm per 10m */ + pressure = 1 + (dive->meandepth.mm / 10000.0); + sac = airuse / pressure * 60 / dive->duration.seconds; + + /* milliliters per minute.. */ + *val = sac * 1000; + + switch (output_units.volume) { + case LITER: + unit = "l"; + fmt = "%4.0f %s"; + break; + case CUFT: + unit = "cuft"; + fmt = "%4.2f %s"; + airuse /= liters_per_cuft; + sac /= liters_per_cuft; + break; + } + + snprintf(buffer, sizeof(buffer), fmt, sac, unit); + *str = strdup(buffer); } -static gboolean set_one_dive(GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) +static void fill_one_dive(struct dive *dive, + GtkTreeModel *model, + GtkTreeIter *iter) { - GValue value = {0, }; - struct dive *dive; - int date, depth, duration, temp, nitrox, sac; - char *datestr, *depthstr, *durationstr, *tempstr, *nitroxstr, *sacstr; + int duration, temp, nitrox, sac; + char *durationstr, *tempstr, *nitroxstr, *sacstr; char *location; - /* Get the dive number */ - gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value); - dive = get_dive(g_value_get_int(&value)); - if (!dive) - return TRUE; - - get_date(dive, &date, &datestr); - get_depth(dive, &depth, &depthstr); get_duration(dive, &duration, &durationstr); get_location(dive, &location); get_temp(dive, &temp, &tempstr); @@ -205,8 +257,6 @@ static gboolean set_one_dive(GtkTreeModel *model, * The core data itself is unaffected by units */ gtk_list_store_set(GTK_LIST_STORE(model), iter, - DIVE_DATESTR, datestr, - DIVE_DEPTHSTR, depthstr, DIVE_DURATIONSTR, durationstr, DIVE_LOCATION, location, DIVE_TEMPSTR, tempstr, @@ -214,10 +264,35 @@ static gboolean set_one_dive(GtkTreeModel *model, DIVE_NITROXSTR, nitroxstr, DIVE_NITROX, nitrox, DIVE_SACSTR, sacstr, - DIVE_NITROX, sac, + DIVE_SAC, sac, -1); +} + +static gboolean set_one_dive(GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) +{ + GValue value = {0, }; + struct dive *dive; + + /* Get the dive number */ + gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value); + dive = get_dive(g_value_get_int(&value)); + if (!dive) + return TRUE; + if (data && dive != data) + return FALSE; + + fill_one_dive(dive, model, iter); + return dive == data; +} + +void flush_divelist(struct DiveList *dive_list, struct dive *dive) +{ + GtkTreeModel *model = GTK_TREE_MODEL(dive_list->model); - return FALSE; + gtk_tree_model_foreach(model, set_one_dive, dive); } void update_dive_list_units(struct DiveList *dive_list) @@ -252,9 +327,7 @@ static void fill_dive_list(struct DiveList *dive_list) gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, DIVE_INDEX, i, - DIVE_DATESTR, "date", DIVE_DATE, dive->when, - DIVE_DEPTHSTR, "depth", DIVE_DEPTH, dive->maxdepth, DIVE_DURATIONSTR, "duration", DIVE_DURATION, dive->duration.seconds, @@ -287,9 +360,9 @@ struct DiveList dive_list_create(void) dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS, G_TYPE_INT, /* index */ - G_TYPE_STRING, G_TYPE_INT, /* Date */ - G_TYPE_STRING, G_TYPE_INT, /* Depth */ - G_TYPE_STRING, G_TYPE_INT, /* Duration */ + G_TYPE_INT, /* Date */ + G_TYPE_INT, /* Depth */ + G_TYPE_STRING, G_TYPE_INT, /* Duration */ G_TYPE_STRING, /* Location */ G_TYPE_STRING, G_TYPE_INT, /* Temperature */ G_TYPE_STRING, G_TYPE_INT, /* Nitrox */ @@ -307,7 +380,7 @@ struct DiveList dive_list_create(void) gtk_tree_view_column_set_sort_column_id(col, DIVE_DATE); gtk_tree_view_column_set_resizable (col, TRUE); gtk_tree_view_column_pack_start(col, renderer, TRUE); - gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DATESTR); + gtk_tree_view_column_set_cell_data_func(col, renderer, date_data_func, NULL, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); renderer = gtk_cell_renderer_text_new(); @@ -315,7 +388,7 @@ struct DiveList dive_list_create(void) gtk_tree_view_column_set_title(col, "ft"); gtk_tree_view_column_set_sort_column_id(col, DIVE_DEPTH); gtk_tree_view_column_pack_start(col, renderer, FALSE); - gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DEPTHSTR); + gtk_tree_view_column_set_cell_data_func(col, renderer, depth_data_func, NULL, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL); gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5); @@ -350,7 +423,7 @@ struct DiveList dive_list_create(void) renderer = gtk_cell_renderer_text_new(); dive_list.nitrox = col = gtk_tree_view_column_new(); - gtk_tree_view_column_set_title(col, "EAD"); + gtk_tree_view_column_set_title(col, "O2%"); gtk_tree_view_column_set_sort_column_id(col, DIVE_NITROX); gtk_tree_view_column_pack_start(col, renderer, FALSE); gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_NITROXSTR); @@ -379,7 +452,7 @@ struct DiveList dive_list_create(void) dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget), - GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); + GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view); return dive_list;