diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-01-05 12:56:45 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-01-05 15:51:57 -0800 |
commit | 92a8fde9c256d80eaab9af448819594da00612ab (patch) | |
tree | 6aeba7fa7cc8189aeae46c7939d138d00ffa44fc /gtk-gui.c | |
parent | f31b8368367346277d25e7dc28b0879749da0d95 (diff) | |
download | subsurface-92a8fde9c256d80eaab9af448819594da00612ab.tar.gz |
Do a minimal hook-up of the dive plan tree view to the
actual planning
Yes, you can actually enter your segments now.
No, it's not wonderfully user-friendly. If you don't enter enough
segments to create a dive plan, it will just silently fail, for example.
And the <tab> key that should get you to the next editable segment
doesn't. And so on. But it kind of works.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'gtk-gui.c')
-rw-r--r-- | gtk-gui.c | 81 |
1 files changed, 73 insertions, 8 deletions
@@ -11,6 +11,7 @@ #include <time.h> #include <unistd.h> #include <sys/stat.h> +#include <sys/time.h> #include <ctype.h> #include "dive.h" @@ -1129,6 +1130,22 @@ static void test_planner_cb(GtkWidget *w, gpointer data) test_planner(); } +static void check_last_not_empty(GtkTreeModel *model, GtkTreeIter *iter, const char *text) +{ + /* We only add a new empty entry if the current one isn't empty.. */ + while (isspace(*text)) + text++; + if (!*text) + return; + + /* Is there a 'next' entry? */ + if (gtk_tree_model_iter_next(model, iter)) + return; + + /* Ok, let's add a new empty entry at the end, then.. */ + gtk_list_store_append(GTK_LIST_STORE(model), iter); +} + /* * Get a value in tenths (so "10.2" == 102, "9" = 90) * @@ -1165,10 +1182,13 @@ static int get_permille(char *begin, char **end) return value; } -static int get_gas(char *text, int *o2_p, int *he_p) +static int validate_gas(char *text, int *o2_p, int *he_p) { int o2, he; + if (!text) + return 0; + while (isspace(*text)) text++; @@ -1212,11 +1232,12 @@ static void plan_gas_cb(GtkCellRendererText *cell, gchar *path, gchar *text, gpo return; /* Verify that it's an acceptable gas */ - if (!get_gas(text, &o2, &he)) + if (!validate_gas(text, &o2, &he)) return; /* Ok, looks fine, accept the string */ gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, text, -1); + check_last_not_empty(model, &iter, text); } static int validate_time(char *text, int *sec_p, int *rel_p) @@ -1224,6 +1245,9 @@ static int validate_time(char *text, int *sec_p, int *rel_p) int min, sec, rel; char *end; + if (!text) + return 0; + while (isspace(*text)) text++; @@ -1295,12 +1319,16 @@ static void plan_time_cb(GtkCellRendererText *cell, gchar *path, gchar *text, gp /* Ok, looks fine, accept the string */ gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, text, -1); + check_last_not_empty(model, &iter, text); } static int validate_depth(char *text, int *mm_p) { int depth, imperial; + if (!text) + return 0; + depth = get_tenths(text, &text); if (depth < 0) return 0; @@ -1346,8 +1374,49 @@ static void plan_depth_cb(GtkCellRendererText *cell, gchar *path, gchar *text, g /* Ok, looks fine, accept the string */ gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, text, -1); + check_last_not_empty(model, &iter, text); } +static void run_diveplan(GtkListStore *store) +{ + GtkTreeModel *model = GTK_TREE_MODEL(store); + struct diveplan diveplan = {}; + struct timeval tv; + GtkTreeIter iter; + int ok; + int lasttime = 0; + + gettimeofday(&tv, NULL); + diveplan.when = tv.tv_sec; + diveplan.surface_pressure = 1013; + + ok = gtk_tree_model_get_iter_first(model, &iter); + while (ok) { + gchar *time, *depth, *gas; + int sec, rel, mm, o2, he; + + gtk_tree_model_get(model, &iter, + 0, &time, + 1, &depth, + 2, &gas, + -1); + if (!validate_time(time, &sec, &rel)) + goto next; + if (rel) + sec += lasttime; + if (!validate_depth(depth, &mm)) + goto next; + if (!validate_gas(gas, &o2, &he)) + goto next; + + plan_add_segment(&diveplan, sec, mm, o2, he); +next: + lasttime = sec; + g_free(time); g_free(depth); g_free(gas); + ok = gtk_tree_model_iter_next(model, &iter); + } + plan(&diveplan); +} void input_plan() { @@ -1365,12 +1434,8 @@ void input_plan() container = gtk_dialog_get_content_area(GTK_DIALOG(planner)); store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); + /* We always have an empty entry at the end */ gtk_list_store_append(store, &iter); - gtk_list_store_set(store, &iter, - 0, "7:30", - 1, "100 ft", - 2, "20/30", - -1); view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); g_object_unref(store); @@ -1383,7 +1448,7 @@ void input_plan() gtk_widget_show_all(planner); if (gtk_dialog_run(GTK_DIALOG(planner)) == GTK_RESPONSE_ACCEPT) { - // run plan + run_diveplan(store); } gtk_widget_destroy(planner); } |