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