]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Start abstracting out the cylinder equipment widgets
[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 struct cylinder_widget {
12         int changed;
13         GtkComboBox *description;
14         GtkSpinButton *size, *pressure;
15         GtkWidget *o2, *gasmix_button;
16 };
17
18 static struct cylinder_widget cylinder;
19
20 static void set_cylinder_spinbuttons(int ml, int mbar)
21 {
22         double volume, pressure;
23
24         volume = ml / 1000.0;
25         pressure = mbar / 1000.0;
26         if (mbar) {
27                 if (output_units.volume == CUFT) {
28                         volume /= 28.3168466;   /* Liters to cuft */
29                         volume *= pressure / 1.01325;
30                 }
31                 if (output_units.pressure == PSI) {
32                         pressure *= 14.5037738; /* Bar to PSI */
33                 }
34         }
35
36         gtk_spin_button_set_value(cylinder.size, volume);
37         gtk_spin_button_set_value(cylinder.pressure, pressure);
38 }
39
40 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
41 {
42         GtkTreeIter iter;
43         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
44         GValue value1 = {0, }, value2 = {0,};
45         cylinder_t *cyl = current_dive->cylinder + 0;
46
47         /* Did the user set it to some non-standard value? */
48         if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
49                 cylinder.changed = 1;
50                 return;
51         }
52
53         /*
54          * We get "change" signal callbacks just because we set
55          * the description by hand. Whatever. So ignore them if
56          * they are no-ops.
57          */
58         if (!cylinder.changed && cyl->type.description) {
59                 int same;
60                 char *desc = gtk_combo_box_get_active_text(combo_box);
61
62                 same = !strcmp(desc, cyl->type.description);
63                 g_free(desc);
64                 if (same)
65                         return;
66         }
67         cylinder.changed = 1;
68
69         gtk_tree_model_get_value(model, &iter, 1, &value1);
70         gtk_tree_model_get_value(model, &iter, 2, &value2);
71
72         set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
73 }
74
75 /*
76  * The gtk_tree_model_foreach() interface is bad. It could have
77  * returned whether the callback ever returned true
78  */
79 static int found_match = 0;
80
81 static gboolean match_cylinder(GtkTreeModel *model,
82                                 GtkTreePath *path,
83                                 GtkTreeIter *iter,
84                                 gpointer data)
85 {
86         const char *name;
87         const char *desc = data;
88         GValue value = {0, };
89
90         gtk_tree_model_get_value(model, iter, 0, &value);
91         name = g_value_get_string(&value);
92         if (strcmp(desc, name))
93                 return FALSE;
94         gtk_combo_box_set_active_iter(cylinder.description, iter);
95         found_match = 1;
96         return TRUE;
97 }
98
99 static void add_cylinder(const char *desc, int ml, int mbar)
100 {
101         GtkTreeModel *model;
102
103         found_match = 0;
104         model = gtk_combo_box_get_model(cylinder.description);
105         gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
106
107         if (!found_match) {
108                 GtkListStore *store = GTK_LIST_STORE(model);
109                 GtkTreeIter iter;
110
111                 gtk_list_store_append(store, &iter);
112                 gtk_list_store_set(store, &iter,
113                         0, desc,
114                         1, ml,
115                         2, mbar,
116                         -1);
117                 gtk_combo_box_set_active_iter(cylinder.description, &iter);
118         }
119 }
120
121 void show_dive_equipment(struct dive *dive)
122 {
123         cylinder_t *cyl = &dive->cylinder[0];
124         const char *desc = cyl->type.description;
125         int ml, mbar;
126         double o2;
127
128         if (!desc)
129                 desc = "";
130
131         ml = cyl->type.size.mliter;
132         mbar = cyl->type.workingpressure.mbar;
133         add_cylinder(desc, ml, mbar);
134
135         set_cylinder_spinbuttons(cyl->type.size.mliter, cyl->type.workingpressure.mbar);
136         o2 = cyl->gasmix.o2.permille / 10.0;
137         gtk_widget_set_sensitive(cylinder.o2, !!o2);
138         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder.gasmix_button), !!o2);
139         if (!o2)
140                 o2 = 21.0;
141         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder.o2), o2);
142 }
143
144 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
145 {
146         GtkWidget *frame, *hbox, *button;
147
148         frame = gtk_frame_new(name);
149         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
150
151         hbox = gtk_hbox_new(FALSE, 3);
152         gtk_container_add(GTK_CONTAINER(frame), hbox);
153
154         button = gtk_spin_button_new_with_range(min, max, incr);
155         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
156
157         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
158
159         return button;
160 }
161
162 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
163 {
164         int mbar, ml;
165
166         if (output_units.pressure == PSI)
167                 pressure /= 14.5037738;
168
169         if (pressure && output_units.volume == CUFT) {
170                 volume *= 28.3168466;   /* CUFT to liter */
171                 volume /= pressure / 1.01325;
172         }
173
174         ml = volume * 1000 + 0.5;
175         mbar = pressure * 1000 + 0.5;
176
177         if (o2 < 211)
178                 o2 = 0;
179         cyl->type.description = desc;
180         cyl->type.size.mliter = ml;
181         cyl->type.workingpressure.mbar = mbar;
182         cyl->gasmix.o2.permille = o2;
183
184         /*
185          * Also, insert it into the model if it doesn't already exist
186          */
187         add_cylinder(desc, ml, mbar);
188 }
189
190 static void record_cylinder_changes(struct dive *dive)
191 {
192         const gchar *desc;
193         GtkComboBox *box = cylinder.description;
194         double volume, pressure;
195         int o2;
196
197         desc = gtk_combo_box_get_active_text(box);
198         volume = gtk_spin_button_get_value(cylinder.size);
199         pressure = gtk_spin_button_get_value(cylinder.pressure);
200         o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder.o2))*10 + 0.5;
201         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder.gasmix_button)))
202                 o2 = 0;
203         fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
204 }
205
206 void flush_dive_equipment_changes(struct dive *dive)
207 {
208         record_cylinder_changes(dive);
209 }
210
211 /*
212  * We hardcode the most common standard cylinders,
213  * we should pick up any other names from the dive
214  * logs directly.
215  */
216 static struct tank_info {
217         const char *name;
218         int size;       /* cuft if < 1000, otherwise mliter */
219         int psi;        /* If zero, size is in mliter */
220 } tank_info[100] = {
221         /* Need an empty entry for the no-cylinder case */
222         { "", 0, 0 },
223
224         /* Size-only metric cylinders */
225         { "10.0 l", 10000 },
226         { "11.1 l", 11100 },
227
228         /* Most common AL cylinders */
229         { "AL50",   50, 3000 },
230         { "AL63",   63, 3000 },
231         { "AL72",   72, 3000 },
232         { "AL80",   80, 3000 },
233         { "AL100", 100, 3300 },
234
235         /* Somewhat common LP steel cylinders */
236         { "LP85",   85, 2640 },
237         { "LP95",   95, 2640 },
238         { "LP108", 108, 2640 },
239         { "LP121", 121, 2640 },
240
241         /* Somewhat common HP steel cylinders */
242         { "HP65",   65, 3442 },
243         { "HP80",   80, 3442 },
244         { "HP100", 100, 3442 },
245         { "HP119", 119, 3442 },
246         { "HP130", 130, 3442 },
247
248         /* We'll fill in more from the dive log dynamically */
249         { NULL, }
250 };
251
252 static void fill_tank_list(GtkListStore *store)
253 {
254         GtkTreeIter iter;
255         struct tank_info *info = tank_info;
256
257         while (info->name) {
258                 int size = info->size;
259                 int psi = info->psi;
260                 int mbar = 0, ml = size;
261
262                 /* Is it in cuft and psi? */
263                 if (psi) {
264                         double bar = 0.0689475729 * psi;
265                         double airvolume = 28316.8466 * size;
266                         double atm = bar / 1.01325;
267
268                         ml = airvolume / atm + 0.5;
269                         mbar = bar*1000 + 0.5;
270                 }
271
272                 gtk_list_store_append(store, &iter);
273                 gtk_list_store_set(store, &iter,
274                         0, info->name,
275                         1, ml,
276                         2, mbar,
277                         -1);
278                 info++;
279         }
280 }
281
282 static void nitrox_cb(GtkToggleButton *button, gpointer data)
283 {
284         GtkWidget *o2 = data;
285         int state;
286
287         state = gtk_toggle_button_get_active(button);
288         gtk_widget_set_sensitive(o2, state);
289 }
290
291 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
292 {
293         GtkWidget *frame, *hbox, *hbox2;
294         GtkWidget *widget;
295         char buffer[80];
296
297         hbox = gtk_hbox_new(FALSE, 3);
298         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
299
300         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
301         frame = gtk_frame_new(buffer);
302         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
303
304         hbox2 = gtk_hbox_new(FALSE, 3);
305         gtk_container_add(GTK_CONTAINER(frame), hbox2);
306
307         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
308         gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
309
310         cylinder.description = GTK_COMBO_BOX(widget);
311         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
312
313         widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
314         cylinder.size = GTK_SPIN_BUTTON(widget);
315
316         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
317         cylinder.pressure = GTK_SPIN_BUTTON(widget);
318
319         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
320         cylinder.o2 = widget;
321         cylinder.gasmix_button = gtk_check_button_new();
322         gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder.o2)),
323                 cylinder.gasmix_button, FALSE, FALSE, 3);
324         g_signal_connect(cylinder.gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder.o2);
325
326         gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder.o2), 21.0, 100.0);
327 }
328
329 static GtkListStore *create_tank_size_model(void)
330 {
331         GtkListStore *model;
332
333         model = gtk_list_store_new(3,
334                 G_TYPE_STRING,          /* Tank name */
335                 G_TYPE_INT,             /* Tank size in mliter */
336                 G_TYPE_INT,             /* Tank working pressure in mbar */
337                 -1);
338
339         fill_tank_list(model);
340         return model;
341 }
342
343 GtkWidget *equipment_widget(void)
344 {
345         GtkWidget *vbox;
346         GtkListStore *model;
347
348         vbox = gtk_vbox_new(FALSE, 3);
349
350         model = create_tank_size_model();
351         cylinder_widget(vbox, 0, model);
352
353         return vbox;
354 }