]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Getting closer to a usable cylinder management interface
[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;
14
15 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
16 {
17         GtkTreeIter iter;
18         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
19         GValue value1 = {0, }, value2 = {0,};
20         int volume, pressure;
21
22         cylinder_changed = 1;
23         if (!gtk_combo_box_get_active_iter(combo_box, &iter))
24                 return;
25
26         gtk_tree_model_get_value(model, &iter, 1, &value1);
27         volume = g_value_get_int(&value1);
28         gtk_tree_model_get_value(model, &iter, 2, &value2);
29         pressure = g_value_get_int(&value2);
30
31         gtk_spin_button_set_value(cylinder_size,
32                         volume / 1000.0);
33         gtk_spin_button_set_value(cylinder_pressure,
34                         pressure / 1000.0);
35 }
36
37 static gboolean match_cylinder(GtkTreeModel *model,
38                                 GtkTreePath *path,
39                                 GtkTreeIter *iter,
40                                 gpointer data)
41 {
42         const char *name;
43         const char *desc = data;
44         GValue value = {0, };
45
46         gtk_tree_model_get_value(model, iter, 0, &value);
47         name = g_value_get_string(&value);
48         if (strcmp(desc, name))
49                 return FALSE;
50         gtk_combo_box_set_active_iter(cylinder_description, iter);
51         return TRUE;
52 }
53
54 void show_dive_equipment(struct dive *dive)
55 {
56         cylinder_type_t *type = &dive->cylinder[0].type;
57         const char *desc = type->description;
58         GtkTreeModel *model = gtk_combo_box_get_model(cylinder_description);
59
60         if (desc)
61                 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
62         gtk_spin_button_set_value(cylinder_size,
63                         type->size.mliter / 1000.0);
64         gtk_spin_button_set_value(cylinder_pressure,
65                         type->workingpressure.mbar / 1000.0);
66 }
67
68 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name)
69 {
70         GtkWidget *frame, *button;
71
72         frame = gtk_frame_new(name);
73         gtk_container_add(GTK_CONTAINER(vbox), frame);
74
75         button = gtk_spin_button_new_with_range( 0.0, 3500.0, 0.1);
76         gtk_container_add(GTK_CONTAINER(frame), button);
77
78         return button;
79 }
80
81 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, int mliter, int mbar)
82 {
83         /*
84          * The code is currently too broken to actually set anything,
85          * so just print what we would set
86          */
87         printf("Set cylinder to '%s': %.1f liter at %.1f bar working pressure\n",
88                 desc, mliter / 1000.0, mbar / 1000.);
89 #if 0
90         cyl->type.description = desc;
91         cyl->type.size.mliter = mliter;
92         cyl->type.workingpressure.mbar = mbar;
93 #endif
94 }
95
96 static void record_cylinder_changes(struct dive *dive)
97 {
98         const gchar *desc;
99         GtkComboBox *box = cylinder_description;
100         gdouble volume, pressure;
101
102         desc = gtk_combo_box_get_active_text(box);
103         volume = gtk_spin_button_get_value(cylinder_size);
104         pressure = gtk_spin_button_get_value(cylinder_pressure);
105
106         fill_cylinder_info(dive->cylinder+0,
107                 desc, volume * 1000, pressure * 1000);
108 }
109
110 void flush_dive_equipment_changes(struct dive *dive)
111 {
112         if (cylinder_changed) {
113                 cylinder_changed = 0;
114                 record_cylinder_changes(dive);
115         }
116 }
117
118 /* We should take these from the dive list instead */
119 static struct tank_info {
120         const char *name;
121         int size;       /* cuft or mliter depending on psi */
122         int psi;        /* If zero, size is in mliter */
123 } tank_info[] = {
124         { "None", 0, 0 },
125         { "10.0 l", 10000 },
126         { "11.1 l", 11100 },
127         { "AL72", 72, 3000 },
128         { "AL80", 80, 3000 },
129         { "LP85", 85, 2400 },
130         { "LP95", 95, 2400 },
131         { "LP85+", 85, 2640 },
132         { "LP95+", 95, 2640 },
133         { "HP100", 100, 3442 },
134         { "HP119", 119, 3442 },
135         { NULL, }
136 };
137
138 static void fill_tank_list(GtkListStore *store)
139 {
140         GtkTreeIter iter;
141
142         struct tank_info *info = tank_info;
143
144         while (info->name) {
145                 int size = info->size;
146                 int psi = info->psi;
147                 int mbar = 0, ml = size;
148
149                 /* Is it in cuft and psi? */
150                 if (psi) {
151                         double bar = 0.0689475729 * psi;
152                         double airvolume = 28316.8466 * size;
153                         double atm = bar / 1.01325;
154
155                         ml = airvolume / atm + 0.5;
156                         mbar = bar*1000 + 0.5;
157                 }
158
159                 gtk_list_store_append(store, &iter);
160                 gtk_list_store_set(store, &iter,
161                         0, info->name,
162                         1, ml,
163                         2, mbar,
164                         -1);
165                 info++;
166         }
167 }
168
169 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
170 {
171         GtkWidget *frame, *hbox;
172         GtkWidget *description, *size, *pressure;
173         char buffer[80];
174
175         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr);
176         frame = gtk_frame_new(buffer);
177         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0);
178
179         hbox = gtk_hbox_new(TRUE, 3);
180         gtk_container_add(GTK_CONTAINER(frame), hbox);
181
182         frame = gtk_frame_new("Description");
183         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0);
184
185         description = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
186         gtk_container_add(GTK_CONTAINER(frame), description);
187
188         cylinder_description = GTK_COMBO_BOX(description);
189         g_signal_connect(description, "changed", G_CALLBACK(cylinder_cb), description);
190
191         size = create_spinbutton(hbox, "Size");
192         cylinder_size = GTK_SPIN_BUTTON(size);
193
194         pressure = create_spinbutton(hbox, "Working Pressure");
195         cylinder_pressure = GTK_SPIN_BUTTON(pressure);
196 }
197
198 static GtkListStore *create_tank_size_model(void)
199 {
200         GtkListStore *model;
201
202         model = gtk_list_store_new(3,
203                 G_TYPE_STRING,          /* Tank name */
204                 G_TYPE_INT,             /* Tank size in mliter */
205                 G_TYPE_INT,             /* Tank working pressure in mbar */
206                 -1);
207
208         fill_tank_list(model);
209         return model;
210 }
211
212 GtkWidget *equipment_widget(void)
213 {
214         GtkWidget *vbox;
215         GtkListStore *model;
216
217         vbox = gtk_vbox_new(FALSE, 3);
218
219         model = create_tank_size_model();
220         cylinder_widget(vbox, 0, model);
221
222         return vbox;
223 }