]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Let people manage their cylinders in cuft and psi
[ext/subsurface.git] / equipment.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 static int cylinder_changed;
12 static GtkComboBox *cylinder_description;
13 static GtkSpinButton *cylinder_size, *cylinder_pressure, *nitrox_value;
14
15 static void set_cylinder_spinbuttons(int ml, int mbar)
16 {
17         double volume, pressure;
18
19         volume = ml / 1000.0;
20         pressure = mbar / 1000.0;
21         if (mbar) {
22                 if (output_units.volume == CUFT) {
23                         volume /= 28.3168466;   /* Liters to cuft */
24                         volume *= pressure / 1.01325;
25                 }
26                 if (output_units.pressure == PSI) {
27                         pressure *= 14.5037738; /* Bar to PSI */
28                 }
29         }
30
31         gtk_spin_button_set_value(cylinder_size, volume);
32         gtk_spin_button_set_value(cylinder_pressure, pressure);
33 }
34
35 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
36 {
37         GtkTreeIter iter;
38         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
39         GValue value1 = {0, }, value2 = {0,};
40         cylinder_t *cyl = current_dive->cylinder + 0;
41
42         /* Did the user set it to some non-standard value? */
43         if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
44                 cylinder_changed = 1;
45                 return;
46         }
47
48         /*
49          * We get "change" signal callbacks just because we set
50          * the description by hand. Whatever. So ignore them if
51          * they are no-ops.
52          */
53         if (!cylinder_changed && cyl->type.description) {
54                 int same;
55                 char *desc = gtk_combo_box_get_active_text(combo_box);
56
57                 same = !strcmp(desc, cyl->type.description);
58                 g_free(desc);
59                 if (same)
60                         return;
61         }
62         cylinder_changed = 1;
63
64         gtk_tree_model_get_value(model, &iter, 1, &value1);
65         gtk_tree_model_get_value(model, &iter, 2, &value2);
66
67         set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
68 }
69
70 static gboolean match_cylinder(GtkTreeModel *model,
71                                 GtkTreePath *path,
72                                 GtkTreeIter *iter,
73                                 gpointer data)
74 {
75         const char *name;
76         const char *desc = data;
77         GValue value = {0, };
78
79         gtk_tree_model_get_value(model, iter, 0, &value);
80         name = g_value_get_string(&value);
81         if (strcmp(desc, name))
82                 return FALSE;
83         gtk_combo_box_set_active_iter(cylinder_description, iter);
84         return TRUE;
85 }
86
87 void show_dive_equipment(struct dive *dive)
88 {
89         cylinder_t *cyl = &dive->cylinder[0];
90         const char *desc = cyl->type.description;
91         GtkTreeModel *model = gtk_combo_box_get_model(cylinder_description);
92         double o2;
93
94         if (desc)
95                 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
96
97         set_cylinder_spinbuttons(cyl->type.size.mliter, cyl->type.workingpressure.mbar);
98         o2 = cyl->gasmix.o2.permille / 10.0;
99         if (!o2)
100                 o2 = 21.0;
101         gtk_spin_button_set_value(nitrox_value, o2);
102 }
103
104 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
105 {
106         GtkWidget *frame, *button;
107
108         frame = gtk_frame_new(name);
109         gtk_container_add(GTK_CONTAINER(vbox), frame);
110
111         button = gtk_spin_button_new_with_range(min, max, incr);
112         gtk_container_add(GTK_CONTAINER(frame), button);
113
114         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
115
116         return button;
117 }
118
119 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
120 {
121         int mbar, ml;
122
123         if (output_units.pressure == PSI)
124                 pressure /= 14.5037738;
125
126         if (pressure && output_units.volume == CUFT) {
127                 volume *= 28.3168466;   /* CUFT to liter */
128                 volume /= pressure / 1.01325;
129         }
130
131         ml = volume * 1000 + 0.5;
132         mbar = pressure * 1000 + 0.5;
133
134         if (o2 < 211)
135                 o2 = 0;
136         cyl->type.description = desc;
137         cyl->type.size.mliter = ml;
138         cyl->type.workingpressure.mbar = mbar;
139         cyl->gasmix.o2.permille = o2;
140 }
141
142 static void record_cylinder_changes(struct dive *dive)
143 {
144         const gchar *desc;
145         GtkComboBox *box = cylinder_description;
146         double volume, pressure;
147         int o2;
148
149         desc = gtk_combo_box_get_active_text(box);
150         volume = gtk_spin_button_get_value(cylinder_size);
151         pressure = gtk_spin_button_get_value(cylinder_pressure);
152         o2 = gtk_spin_button_get_value(nitrox_value)*10 + 0.5;
153         fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
154 }
155
156 void flush_dive_equipment_changes(struct dive *dive)
157 {
158         record_cylinder_changes(dive);
159 }
160
161 /* We should take these from the dive list instead */
162 static struct tank_info {
163         const char *name;
164         int size;       /* cuft or mliter depending on psi */
165         int psi;        /* If zero, size is in mliter */
166 } tank_info[] = {
167         { "None", 0, 0 },
168         { "10.0 l", 10000 },
169         { "11.1 l", 11100 },
170         { "AL72", 72, 3000 },
171         { "AL80", 80, 3000 },
172         { "LP85", 85, 2400 },
173         { "LP95", 95, 2400 },
174         { "LP85+", 85, 2640 },
175         { "LP95+", 95, 2640 },
176         { "HP100", 100, 3442 },
177         { "HP119", 119, 3442 },
178         { NULL, }
179 };
180
181 static void fill_tank_list(GtkListStore *store)
182 {
183         GtkTreeIter iter;
184
185         struct tank_info *info = tank_info;
186
187         while (info->name) {
188                 int size = info->size;
189                 int psi = info->psi;
190                 int mbar = 0, ml = size;
191
192                 /* Is it in cuft and psi? */
193                 if (psi) {
194                         double bar = 0.0689475729 * psi;
195                         double airvolume = 28316.8466 * size;
196                         double atm = bar / 1.01325;
197
198                         ml = airvolume / atm + 0.5;
199                         mbar = bar*1000 + 0.5;
200                 }
201
202                 gtk_list_store_append(store, &iter);
203                 gtk_list_store_set(store, &iter,
204                         0, info->name,
205                         1, ml,
206                         2, mbar,
207                         -1);
208                 info++;
209         }
210 }
211
212 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
213 {
214         GtkWidget *frame, *hbox;
215         GtkWidget *widget;
216         char buffer[80];
217
218         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr);
219         frame = gtk_frame_new(buffer);
220         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0);
221
222         hbox = gtk_hbox_new(TRUE, 3);
223         gtk_container_add(GTK_CONTAINER(frame), hbox);
224
225         frame = gtk_frame_new("Description");
226         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0);
227
228         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
229         gtk_container_add(GTK_CONTAINER(frame), widget);
230
231         cylinder_description = GTK_COMBO_BOX(widget);
232         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
233
234         widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
235         cylinder_size = GTK_SPIN_BUTTON(widget);
236
237         widget = create_spinbutton(hbox, "Working Pressure", 0, 5000, 1);
238         cylinder_pressure = GTK_SPIN_BUTTON(widget);
239
240         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
241         nitrox_value = GTK_SPIN_BUTTON(widget);
242         gtk_spin_button_set_range(nitrox_value, 21.0, 100.0);
243 }
244
245 static GtkListStore *create_tank_size_model(void)
246 {
247         GtkListStore *model;
248
249         model = gtk_list_store_new(3,
250                 G_TYPE_STRING,          /* Tank name */
251                 G_TYPE_INT,             /* Tank size in mliter */
252                 G_TYPE_INT,             /* Tank working pressure in mbar */
253                 -1);
254
255         fill_tank_list(model);
256         return model;
257 }
258
259 GtkWidget *equipment_widget(void)
260 {
261         GtkWidget *vbox;
262         GtkListStore *model;
263
264         vbox = gtk_vbox_new(FALSE, 3);
265
266         model = create_tank_size_model();
267         cylinder_widget(vbox, 0, model);
268
269         return vbox;
270 }