]>
Commit | Line | Data |
---|---|---|
ebebb610 RN |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
4 | * | |
5 | * Copyright (C) 2013 Rob Norris <rw_norris@hotmail.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | *********************************************************** | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <math.h> | |
26 | #include <time.h> | |
27 | #include <string.h> | |
28 | #include <glib/gprintf.h> | |
29 | #include <glib/gi18n.h> | |
30 | ||
31 | #include "viking.h" | |
32 | #include "viktrwlayer_analysis.h" | |
33 | ||
34 | // Units of each item are in SI Units | |
35 | // (as returned by the appropriate internal viking track functions) | |
36 | typedef struct { | |
37 | gdouble min_alt; | |
38 | gdouble max_alt; | |
39 | gdouble elev_gain; | |
40 | gdouble elev_loss; | |
41 | gdouble length; | |
42 | gdouble length_gaps; | |
43 | gdouble max_speed; | |
44 | gulong trackpoints; | |
45 | guint segments; | |
46 | gint duration; | |
47 | time_t start_time; | |
48 | time_t end_time; | |
49 | gint count; | |
50 | } track_stats; | |
51 | ||
52 | // Early incarnations of the code had facilities to print output for multiple files | |
53 | // but has been rescoped to work on a single list of tracks for the GUI | |
54 | typedef enum { | |
55 | //TS_TRACK, | |
56 | TS_TRACKS, | |
57 | //TS_FILES, | |
58 | } track_stat_block; | |
59 | static track_stats tracks_stats[1]; | |
60 | ||
61 | // cf with vik_track_get_minmax_alt internals | |
62 | #define VIK_VAL_MIN_ALT 25000.0 | |
63 | #define VIK_VAL_MAX_ALT -5000.0 | |
64 | ||
65 | /** | |
66 | * Reset the specified block | |
67 | * Call this when starting to processing multiple items | |
68 | */ | |
69 | static void val_reset ( track_stat_block block ) | |
70 | { | |
71 | tracks_stats[block].min_alt = VIK_VAL_MIN_ALT; | |
72 | tracks_stats[block].max_alt = VIK_VAL_MAX_ALT; | |
73 | tracks_stats[block].elev_gain = 0.0; | |
74 | tracks_stats[block].elev_loss = 0.0; | |
75 | tracks_stats[block].length = 0.0; | |
76 | tracks_stats[block].length_gaps = 0.0; | |
77 | tracks_stats[block].max_speed = 0.0; | |
78 | tracks_stats[block].trackpoints = 0; | |
79 | tracks_stats[block].segments = 0; | |
80 | tracks_stats[block].duration = 0; | |
81 | tracks_stats[block].start_time = 0; | |
82 | tracks_stats[block].end_time = 0; | |
83 | tracks_stats[block].count = 0; | |
84 | } | |
85 | ||
86 | /** | |
87 | * @val_analyse_track: | |
88 | * @trk: The track to be analyse | |
89 | * | |
90 | * Function to collect statistics, using the internal track functions | |
91 | */ | |
92 | static void val_analyse_track ( VikTrack *trk ) | |
93 | { | |
94 | //val_reset ( TS_TRACK ); | |
95 | gdouble min_alt; | |
96 | gdouble max_alt; | |
97 | gdouble up; | |
98 | gdouble down; | |
99 | ||
100 | gdouble length = 0.0; | |
101 | gdouble length_gaps = 0.0; | |
102 | gdouble max_speed = 0.0; | |
103 | gulong trackpoints = 0; | |
104 | guint segments = 0; | |
105 | ||
106 | tracks_stats[TS_TRACKS].count++; | |
107 | ||
108 | trackpoints = vik_track_get_tp_count (trk); | |
109 | segments = vik_track_get_segment_count (trk); | |
110 | length = vik_track_get_length (trk); | |
111 | length_gaps = vik_track_get_length_including_gaps (trk); | |
112 | max_speed = vik_track_get_max_speed (trk); | |
113 | ||
114 | int ii; | |
115 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
116 | tracks_stats[ii].trackpoints += trackpoints; | |
117 | tracks_stats[ii].segments += segments; | |
118 | tracks_stats[ii].length += length; | |
119 | tracks_stats[ii].length_gaps += length_gaps; | |
120 | if ( max_speed > tracks_stats[ii].max_speed ) | |
121 | tracks_stats[ii].max_speed = max_speed; | |
122 | } | |
123 | ||
124 | if ( vik_track_get_minmax_alt (trk, &min_alt, &max_alt) ) { | |
125 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
126 | if ( min_alt < tracks_stats[ii].min_alt ) | |
127 | tracks_stats[ii].min_alt = min_alt; | |
128 | if ( max_alt > tracks_stats[ii].max_alt ) | |
129 | tracks_stats[ii].max_alt = max_alt; | |
130 | } | |
131 | } | |
132 | ||
133 | vik_track_get_total_elevation_gain (trk, &up, &down ); | |
134 | ||
135 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
136 | tracks_stats[ii].elev_gain += up; | |
137 | tracks_stats[ii].elev_loss += down; | |
138 | } | |
139 | ||
140 | if ( trk->trackpoints && VIK_TRACKPOINT(trk->trackpoints->data)->timestamp ) { | |
141 | time_t t1, t2; | |
142 | t1 = VIK_TRACKPOINT(g_list_first(trk->trackpoints)->data)->timestamp; | |
143 | t2 = VIK_TRACKPOINT(g_list_last(trk->trackpoints)->data)->timestamp; | |
144 | ||
145 | // Assume never actually have a track with a time of 0 (1st Jan 1970) | |
146 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
147 | if ( tracks_stats[ii].start_time == 0) | |
148 | tracks_stats[ii].start_time = t1; | |
149 | if ( tracks_stats[ii].end_time == 0) | |
150 | tracks_stats[ii].end_time = t2; | |
151 | } | |
152 | ||
153 | // Initialize to the first value | |
154 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
155 | if (t1 < tracks_stats[ii].start_time) | |
156 | tracks_stats[ii].start_time = t1; | |
157 | if (t2 > tracks_stats[ii].end_time) | |
158 | tracks_stats[ii].end_time = t2; | |
159 | } | |
160 | ||
161 | for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) { | |
162 | tracks_stats[ii].duration = tracks_stats[ii].duration + (int)(t2-t1); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | // Could use GtkGrids but that is Gtk3+ | |
168 | static GtkWidget *create_table (int cnt, char *labels[], GtkWidget *contents[]) | |
169 | { | |
170 | GtkTable *table; | |
171 | int i; | |
172 | ||
173 | table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE)); | |
174 | gtk_table_set_col_spacing (table, 0, 10); | |
175 | for (i=0; i<cnt; i++) { | |
176 | GtkWidget *label; | |
177 | label = gtk_label_new(NULL); | |
178 | gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane | |
179 | // All text labels are set to be in bold | |
180 | char *markup = g_markup_printf_escaped ("<b>%s:</b>", _(labels[i]) ); | |
181 | gtk_label_set_markup ( GTK_LABEL(label), markup ); | |
182 | g_free ( markup ); | |
183 | gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_EXPAND, 4, 2 ); | |
184 | if (GTK_IS_MISC(contents[i])) { | |
185 | gtk_misc_set_alignment ( GTK_MISC(contents[i]), 0, 0.5 ); | |
186 | } | |
187 | gtk_table_attach_defaults ( table, contents[i], 1, 2, i, i+1 ); | |
188 | } | |
189 | return GTK_WIDGET (table); | |
190 | } | |
191 | ||
192 | static gchar *label_texts[] = { | |
193 | N_("Number of Tracks"), | |
194 | N_("Date Range"), | |
195 | N_("Total Length"), | |
196 | N_("Average Length"), | |
197 | N_("Max Speed"), | |
198 | N_("Avg. Speed"), | |
199 | N_("Minimum Altitude"), | |
200 | N_("Maximum Altitude"), | |
201 | N_("Total Elevation Gain/Loss"), | |
202 | N_("Avg. Elevation Gain/Loss"), | |
203 | N_("Total Duration"), | |
204 | N_("Avg. Duration"), | |
205 | }; | |
206 | ||
207 | /** | |
208 | * create_layout: | |
209 | * | |
210 | * Returns a widget to hold the stats information in a table grid layout | |
211 | */ | |
212 | static GtkWidget *create_layout ( GtkWidget *content[] ) | |
213 | { | |
214 | int cnt = 0; | |
215 | for ( cnt = 0; cnt < G_N_ELEMENTS(label_texts); cnt++ ) | |
216 | content[cnt] = gtk_label_new ( NULL ); | |
217 | ||
218 | return create_table (cnt, label_texts, content); | |
219 | } | |
220 | ||
221 | /** | |
222 | * table_output: | |
223 | * | |
224 | * Update the given widgets table with the values from the track stats | |
225 | */ | |
226 | static void table_output ( track_stats ts, GtkWidget *content[] ) | |
227 | { | |
228 | int cnt = 0; | |
229 | ||
230 | gchar tmp_buf[64]; | |
231 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "%d", ts.count ); | |
232 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
233 | ||
234 | if ( ts.count == 0 ) { | |
235 | // Blank all other fields | |
236 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
237 | for ( cnt = 1; cnt < G_N_ELEMENTS(label_texts); cnt++ ) | |
238 | gtk_label_set_text ( GTK_LABEL(content[cnt]), tmp_buf ); | |
239 | return; | |
240 | } | |
241 | ||
242 | // Check for potential date range | |
243 | // Test if the same day by comparing the date string of the timestamp | |
244 | GDate* gdate_start = g_date_new (); | |
245 | g_date_set_time_t ( gdate_start, ts.start_time ); | |
246 | gchar time_start[32]; | |
247 | g_date_strftime ( time_start, sizeof(time_start), "%x", gdate_start ); | |
248 | g_date_free ( gdate_start ); | |
249 | ||
250 | GDate* gdate_end = g_date_new (); | |
251 | g_date_set_time_t ( gdate_end, ts.end_time ); | |
252 | gchar time_end[32]; | |
253 | g_date_strftime ( time_end, sizeof(time_end), "%x", gdate_end ); | |
254 | g_date_free ( gdate_end ); | |
255 | ||
256 | if ( ts.start_time == ts.end_time ) | |
257 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("No Data") ); | |
258 | else if ( strncmp(time_start, time_end, 32) ) | |
259 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s --> %s", time_start, time_end ); | |
260 | else | |
261 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s", time_start ); | |
262 | ||
263 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
264 | ||
265 | switch (a_vik_get_units_distance ()) { | |
266 | case VIK_UNITS_DISTANCE_MILES: | |
267 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f miles"), VIK_METERS_TO_MILES(ts.length) ); | |
268 | break; | |
269 | default: | |
270 | //VIK_UNITS_DISTANCE_KILOMETRES | |
271 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f km"), ts.length/1000.0 ); | |
272 | break; | |
273 | } | |
274 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
275 | ||
276 | switch (a_vik_get_units_distance ()) { | |
277 | case VIK_UNITS_DISTANCE_MILES: | |
278 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f miles"), (VIK_METERS_TO_MILES(ts.length)/ts.count) ); | |
279 | break; | |
280 | default: | |
281 | //VIK_UNITS_DISTANCE_KILOMETRES | |
282 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km"), ts.length/(1000.0*ts.count) ); | |
283 | break; | |
284 | } | |
285 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
286 | ||
287 | // I'm sure this could be cleaner... | |
288 | switch (a_vik_get_units_speed()) { | |
289 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
290 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.max_speed) ); | |
291 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
292 | if ( ts.duration > 0 ) | |
293 | g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.length/ts.duration) ); | |
294 | break; | |
295 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
296 | if ( ts.max_speed > 0 ) | |
297 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f m/s"), (double)ts.max_speed ); | |
298 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
299 | if ( ts.duration > 0 ) | |
300 | g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.2f m/s"), (double)(ts.length/ts.duration) ); | |
301 | else | |
302 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
303 | break; | |
304 | case VIK_UNITS_SPEED_KNOTS: | |
305 | if ( ts.max_speed > 0 ) | |
306 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots\n"), (double)VIK_MPS_TO_KNOTS(ts.max_speed) ); | |
307 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
308 | if ( ts.duration > 0 ) | |
309 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots"), (double)VIK_MPS_TO_KNOTS(ts.length/ts.duration) ); | |
310 | else | |
311 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
312 | break; | |
313 | default: | |
314 | //VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
315 | if ( ts.max_speed > 0 ) | |
316 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.max_speed) ); | |
317 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
318 | if ( ts.duration > 0 ) | |
319 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.length/ts.duration) ); | |
320 | else | |
321 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
322 | break; | |
323 | } | |
324 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
325 | ||
326 | switch ( a_vik_get_units_height() ) { | |
327 | // Note always round off height value output since sub unit accuracy is overkill | |
328 | case VIK_UNITS_HEIGHT_FEET: | |
329 | if ( ts.min_alt != VIK_VAL_MIN_ALT ) | |
330 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.min_alt)) ); | |
331 | else | |
332 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
333 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
334 | ||
335 | if ( ts.max_alt != VIK_VAL_MAX_ALT ) | |
336 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.max_alt)) ); | |
337 | else | |
338 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
339 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
340 | ||
341 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss)) ); | |
342 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
343 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain/ts.count)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss/ts.count)) ); | |
344 | break; | |
345 | default: | |
346 | //VIK_UNITS_HEIGHT_METRES | |
347 | if ( ts.min_alt != VIK_VAL_MIN_ALT ) | |
348 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.min_alt) ); | |
349 | else | |
350 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
351 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
352 | ||
353 | if ( ts.max_alt != VIK_VAL_MAX_ALT ) | |
354 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.max_alt) ); | |
355 | else | |
356 | g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" ); | |
357 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
358 | ||
359 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain), (int)round(ts.elev_loss) ); | |
360 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
361 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain/ts.count), (int)round(ts.elev_loss/ts.count) ); | |
362 | break; | |
363 | } | |
364 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
365 | ||
366 | gint hours; | |
367 | gint minutes; | |
368 | gint days; | |
369 | // Total Duration | |
370 | days = (gint)(ts.duration / (60*60*24)); | |
371 | hours = (gint)floor((ts.duration - (days*60*60*24)) / (60*60)); | |
372 | minutes = (gint)((ts.duration - (days*60*60*24) - (hours*60*60)) / 60); | |
373 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d:%02d days:hrs:mins"), days, hours, minutes ); | |
374 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
375 | ||
376 | // Average Duration | |
377 | gint avg_dur = ts.duration / ts.count; | |
378 | hours = (gint)floor(avg_dur / (60*60)); | |
379 | minutes = (gint)((avg_dur - (hours*60*60)) / 60); | |
380 | g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d hrs:mins"), hours, minutes ); | |
381 | gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf ); | |
382 | } | |
383 | ||
384 | /** | |
385 | * val_analyse_item_maybe: | |
386 | * @vtlist: A track and the associated layer to consider for analysis | |
387 | * @data: Whether to include invisible items | |
388 | * | |
389 | * Analyse this particular track | |
390 | * considering whether it should be included depending on it's visibility | |
391 | */ | |
392 | static void val_analyse_item_maybe ( vik_trw_track_list_t *vtlist, const gpointer data ) | |
393 | { | |
394 | gboolean include_invisible = GPOINTER_TO_INT(data); | |
395 | VikTrack *trk = vtlist->trk; | |
396 | VikTrwLayer *vtl = vtlist->vtl; | |
397 | ||
398 | // Safety first - items shouldn't be deleted... | |
399 | if ( !IS_VIK_TRW_LAYER(vtl) ) return; | |
400 | if ( !trk ) return; | |
401 | ||
402 | if ( !include_invisible ) { | |
403 | // Skip invisible layers or sublayers | |
404 | if ( !VIK_LAYER(vtl)->visible || | |
405 | (trk->is_route && !vik_trw_layer_get_routes_visibility(vtl)) || | |
406 | (!trk->is_route && !vik_trw_layer_get_tracks_visibility(vtl)) ) | |
407 | return; | |
408 | ||
409 | // Skip invisible tracks | |
410 | if ( !trk->visible ) | |
411 | return; | |
412 | } | |
413 | ||
414 | val_analyse_track ( trk ); | |
415 | } | |
416 | ||
417 | /** | |
418 | * val_analyse: | |
419 | * @widgets: The widget layout | |
420 | * @tracks_and_layers: A list of #vik_trw_track_list_t | |
421 | * @include_invisible: Whether to include invisible layers and tracks | |
422 | * | |
423 | * Analyse each item in the @tracks_and_layers list | |
424 | * | |
425 | */ | |
426 | void val_analyse ( GtkWidget *widgets[], GList *tracks_and_layers, gboolean include_invisible ) | |
427 | { | |
428 | val_reset ( TS_TRACKS ); | |
429 | ||
430 | GList *gl = g_list_first ( tracks_and_layers ); | |
431 | if ( gl ) { | |
432 | g_list_foreach ( gl, (GFunc) val_analyse_item_maybe, GINT_TO_POINTER(include_invisible) ); | |
433 | } | |
434 | ||
435 | table_output ( tracks_stats[TS_TRACKS], widgets ); | |
436 | } | |
437 | ||
438 | typedef struct { | |
439 | GtkWidget **widgets; | |
440 | GtkWidget *layout; | |
441 | GtkWidget *check_button; | |
442 | GList *tracks_and_layers; | |
443 | VikLayer *vl; | |
444 | gpointer user_data; | |
445 | VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb; | |
446 | VikTrwlayerAnalyseCloseFunc on_close_cb; | |
447 | } analyse_cb_t; | |
448 | ||
449 | static void include_invisible_toggled_cb ( GtkToggleButton *togglebutton, analyse_cb_t *acb ) | |
450 | { | |
451 | gboolean value = FALSE; | |
452 | if ( gtk_toggle_button_get_active ( togglebutton ) ) | |
453 | value = TRUE; | |
454 | ||
455 | // Delete old list of items | |
456 | if ( acb->tracks_and_layers ) { | |
457 | g_list_foreach ( acb->tracks_and_layers, (GFunc) g_free, NULL ); | |
458 | g_list_free ( acb->tracks_and_layers ); | |
459 | } | |
460 | ||
461 | // Get the latest list of items to analyse | |
462 | acb->tracks_and_layers = acb->get_tracks_and_layers_cb ( acb->vl, acb->user_data ); | |
463 | ||
464 | val_analyse ( acb->widgets, acb->tracks_and_layers, value ); | |
465 | gtk_widget_show_all ( acb->layout ); | |
466 | } | |
467 | ||
468 | #define VIK_SETTINGS_ANALYSIS_DO_INVISIBLE "track_analysis_do_invisible" | |
469 | ||
470 | /** | |
471 | * analyse_close: | |
472 | * | |
473 | * Multi stage closure - as we need to clear allocations made here | |
474 | * before passing on to the callee so they know then the dialog is closed too | |
475 | */ | |
476 | static void analyse_close ( GtkWidget *dialog, gint resp, analyse_cb_t *data ) | |
477 | { | |
478 | // Save current invisible value for next time | |
479 | gboolean do_invisible = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(data->check_button) ); | |
480 | a_settings_set_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, do_invisible ); | |
481 | ||
482 | //g_free ( data->layout ); | |
483 | g_free ( data->widgets ); | |
484 | g_list_foreach ( data->tracks_and_layers, (GFunc) g_free, NULL ); | |
485 | g_list_free ( data->tracks_and_layers ); | |
486 | ||
487 | if ( data->on_close_cb ) | |
488 | data->on_close_cb ( dialog, resp, data->vl ); | |
489 | ||
490 | g_free ( data ); | |
491 | } | |
492 | ||
493 | /** | |
494 | * vik_trw_layer_analyse_this: | |
495 | * @window: A window from which the dialog will be derived | |
496 | * @name: The name to be shown | |
497 | * @vl: The #VikLayer passed on into get_tracks_and_layers_cb() | |
498 | * @user_data: Data passed on into get_tracks_and_layers_cb() | |
499 | * @get_tracks_and_layers_cb: The function to call to construct items to be analysed | |
500 | * | |
501 | * Display a dialog with stats across many tracks | |
502 | * | |
503 | * Returns: The dialog that is created to display the analyse information | |
504 | */ | |
505 | GtkWidget* vik_trw_layer_analyse_this ( GtkWindow *window, | |
8a0615e3 | 506 | const gchar *name, |
ebebb610 RN |
507 | VikLayer *vl, |
508 | gpointer user_data, | |
509 | VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb, | |
510 | VikTrwlayerAnalyseCloseFunc on_close_cb ) | |
511 | { | |
512 | //VikWindow *vw = VIK_WINDOW(window); | |
513 | ||
514 | GtkWidget *dialog; | |
515 | dialog = gtk_dialog_new_with_buttons ( _("Statistics"), | |
516 | window, | |
517 | GTK_DIALOG_DESTROY_WITH_PARENT, | |
518 | GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, | |
519 | NULL ); | |
520 | ||
521 | GtkWidget *name_l = gtk_label_new ( NULL ); | |
522 | gchar *myname = g_markup_printf_escaped ( "<b>%s</b>", name ); | |
523 | gtk_label_set_markup ( GTK_LABEL(name_l), myname ); | |
524 | g_free ( myname ); | |
525 | ||
526 | GtkWidget *content = gtk_dialog_get_content_area ( GTK_DIALOG(dialog) ); | |
527 | gtk_box_pack_start ( GTK_BOX(content), name_l, FALSE, FALSE, 10); | |
528 | ||
529 | // Get previous value (if any) from the settings | |
530 | gboolean include_invisible; | |
531 | if ( ! a_settings_get_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, &include_invisible ) ) | |
532 | include_invisible = TRUE; | |
533 | ||
534 | analyse_cb_t *acb = g_malloc (sizeof(analyse_cb_t)); | |
535 | acb->vl = vl; | |
536 | acb->user_data = user_data; | |
537 | acb->get_tracks_and_layers_cb = get_tracks_and_layers_cb; | |
538 | acb->on_close_cb = on_close_cb; | |
539 | acb->tracks_and_layers = get_tracks_and_layers_cb ( vl, user_data ); | |
540 | acb->widgets = g_malloc ( sizeof(GtkWidget*) * G_N_ELEMENTS(label_texts) ); | |
541 | acb->layout = create_layout ( acb->widgets ); | |
542 | ||
543 | gtk_box_pack_start ( GTK_BOX(content), acb->layout, FALSE, FALSE, 0 ); | |
544 | ||
545 | // Analysis seems reasonably quick | |
546 | // unless you have really large numbers of tracks (i.e. many many thousands or a really slow computer) | |
547 | // One day might store stats in the track itself.... | |
548 | val_analyse ( acb->widgets, acb->tracks_and_layers, include_invisible ); | |
549 | ||
550 | GtkWidget *cb = gtk_check_button_new_with_label ( _("Include Invisible Items") ); | |
551 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(cb), include_invisible ); | |
552 | gtk_box_pack_start ( GTK_BOX(content), cb, FALSE, FALSE, 10); | |
553 | acb->check_button = cb; | |
554 | ||
555 | gtk_widget_show_all ( dialog ); | |
556 | ||
557 | g_signal_connect ( G_OBJECT(cb), "toggled", G_CALLBACK(include_invisible_toggled_cb), acb ); | |
558 | g_signal_connect ( G_OBJECT(dialog), "response", G_CALLBACK(analyse_close), acb ); | |
559 | ||
560 | return dialog; | |
561 | } |