]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
a482007a GB |
5 | * Copyright (C) 2005-2007, Alex Foobarian <foobarian@gmail.com> |
6 | * Copyright (C) 2007-2008, Quy Tonthat <qtonthat@gmail.com> | |
50a14534 EB |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
4c77d5e0 GB |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
27 | ||
8c00358d | 28 | #ifdef HAVE_MATH_H |
07596bf4 | 29 | #include <math.h> |
8c00358d | 30 | #endif |
4c77d5e0 | 31 | |
50a14534 | 32 | #include <gtk/gtk.h> |
4c77d5e0 | 33 | #include <glib/gi18n.h> |
50a14534 | 34 | #include <time.h> |
8c00358d | 35 | #ifdef HAVE_STRING_H |
f583082b | 36 | #include <string.h> |
8c00358d | 37 | #endif |
50a14534 EB |
38 | #include "coords.h" |
39 | #include "vikcoord.h" | |
40 | #include "viktrack.h" | |
21700912 | 41 | #include "viktrwlayer.h" |
50a14534 EB |
42 | #include "viktrwlayer_propwin.h" |
43 | #include "vikwaypoint.h" | |
44 | #include "dialog.h" | |
45 | #include "globals.h" | |
07596bf4 | 46 | #include "dems.h" |
50a14534 | 47 | |
24d5c7e2 EB |
48 | #include "vikviewport.h" /* ugh */ |
49 | #include "viktreeview.h" /* ugh */ | |
50 | #include <gdk-pixbuf/gdk-pixdata.h> | |
51 | #include "viklayer.h" /* ugh */ | |
52 | #include "vikaggregatelayer.h" | |
53 | #include "viklayerspanel.h" /* ugh */ | |
54 | ||
b66b0b38 RN |
55 | #define PROPWIN_PROFILE_WIDTH 600 |
56 | #define PROPWIN_PROFILE_HEIGHT 300 | |
950d8a07 RN |
57 | |
58 | #define PROPWIN_LABEL_FONT "Sans 7" | |
59 | ||
25e44eac | 60 | #define MIN_ALT_DIFF 100.0 |
c37f1cd6 | 61 | #define MIN_SPEED_DIFF 5.0 |
50a14534 | 62 | |
ca7e67ef QT |
63 | typedef struct _propsaved { |
64 | gboolean saved; | |
65 | gint pos; | |
66 | GdkImage *img; | |
67 | } PropSaved; | |
68 | ||
21700912 | 69 | typedef struct _propwidgets { |
950d8a07 | 70 | gboolean configure_dialog; |
21700912 QT |
71 | VikTrwLayer *vtl; |
72 | VikTrack *tr; | |
73 | VikLayersPanel *vlp; | |
74 | gchar *track_name; | |
b66b0b38 RN |
75 | gint profile_width; |
76 | gint profile_height; | |
2f078f56 RN |
77 | gint profile_width_old; |
78 | gint profile_height_old; | |
79 | gint profile_width_offset; | |
80 | gint profile_height_offset; | |
8dcb3ba4 | 81 | GtkWidget *dialog; |
21700912 QT |
82 | GtkWidget *w_comment; |
83 | GtkWidget *w_track_length; | |
84 | GtkWidget *w_tp_count; | |
85 | GtkWidget *w_segment_count; | |
86 | GtkWidget *w_duptp_count; | |
87 | GtkWidget *w_max_speed; | |
88 | GtkWidget *w_avg_speed; | |
89 | GtkWidget *w_avg_dist; | |
90 | GtkWidget *w_elev_range; | |
91 | GtkWidget *w_elev_gain; | |
92 | GtkWidget *w_time_start; | |
93 | GtkWidget *w_time_end; | |
94 | GtkWidget *w_time_dur; | |
065f60f1 | 95 | GtkWidget *w_cur_dist; /*< Current distance */ |
780d3771 | 96 | GtkWidget *w_cur_elevation; |
065f60f1 | 97 | GtkWidget *w_cur_time; /*< Current time */ |
780d3771 | 98 | GtkWidget *w_cur_speed; |
e60fc2c9 | 99 | gdouble track_length; |
ca7e67ef QT |
100 | PropSaved elev_graph_saved_img; |
101 | PropSaved speed_graph_saved_img; | |
e60fc2c9 QT |
102 | GtkWidget *elev_box; |
103 | GtkWidget *speed_box; | |
0b2bfa08 RN |
104 | gdouble *altitudes; |
105 | gdouble *speeds; | |
8dcb3ba4 | 106 | VikTrackpoint *marker_tp; |
7ee55449 | 107 | gboolean is_marker_drawn; |
21700912 QT |
108 | } PropWidgets; |
109 | ||
ca7e67ef QT |
110 | static PropWidgets *prop_widgets_new() |
111 | { | |
112 | PropWidgets *widgets = g_malloc0(sizeof(PropWidgets)); | |
113 | ||
114 | return widgets; | |
115 | } | |
116 | ||
117 | static void prop_widgets_free(PropWidgets *widgets) | |
118 | { | |
ca7e67ef QT |
119 | if (widgets->elev_graph_saved_img.img) |
120 | g_object_unref(widgets->elev_graph_saved_img.img); | |
121 | if (widgets->speed_graph_saved_img.img) | |
122 | g_object_unref(widgets->speed_graph_saved_img.img); | |
0b2bfa08 RN |
123 | if (widgets->altitudes) |
124 | g_free(widgets->altitudes); | |
125 | if (widgets->speeds) | |
126 | g_free(widgets->speeds); | |
ca7e67ef QT |
127 | g_free(widgets); |
128 | } | |
129 | ||
b66b0b38 | 130 | static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH) |
50a14534 EB |
131 | { |
132 | *max = -1000; | |
133 | *min = 20000; | |
134 | guint i; | |
135 | for ( i=0; i < PROFILE_WIDTH; i++ ) { | |
93a85f58 RN |
136 | if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) { |
137 | if ( array[i] > *max ) | |
138 | *max = array[i]; | |
139 | if ( array[i] < *min ) | |
140 | *min = array[i]; | |
50a14534 EB |
141 | } |
142 | } | |
50a14534 EB |
143 | } |
144 | ||
d03d80e6 | 145 | #define MARGIN 70 |
25e44eac | 146 | #define LINES 5 |
b66b0b38 RN |
147 | static VikTrackpoint *set_center_at_graph_position(gdouble event_x, |
148 | gint img_width, | |
149 | VikTrwLayer *vtl, | |
150 | VikLayersPanel *vlp, | |
151 | VikViewport *vvp, | |
152 | VikTrack *tr, | |
153 | gboolean time_base, | |
154 | gint PROFILE_WIDTH) | |
24d5c7e2 | 155 | { |
32e48121 QT |
156 | VikTrackpoint *trackpoint; |
157 | gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2; | |
e1e2f2c6 JJ |
158 | if (x < 0) |
159 | x = 0; | |
160 | if (x > PROFILE_WIDTH) | |
161 | x = PROFILE_WIDTH; | |
32e48121 QT |
162 | |
163 | if (time_base) | |
ddc2372e | 164 | trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL ); |
32e48121 | 165 | else |
ddc2372e | 166 | trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL ); |
32e48121 | 167 | |
e1e2f2c6 JJ |
168 | if ( trackpoint ) { |
169 | VikCoord coord = trackpoint->coord; | |
6bb72350 RN |
170 | if ( vlp ) { |
171 | vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord ); | |
172 | vik_layers_panel_emit_update ( vlp ); | |
173 | } | |
174 | else { | |
175 | /* since vlp not set, vvp should be valid instead! */ | |
176 | if ( vvp ) | |
177 | vik_viewport_set_center_coord ( vvp, &coord ); | |
178 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
179 | } | |
e1e2f2c6 | 180 | } |
e60fc2c9 | 181 | return trackpoint; |
e1e2f2c6 | 182 | } |
ca7e67ef | 183 | |
7ee55449 RN |
184 | /** |
185 | * Returns whether the marker was drawn or not | |
186 | */ | |
b1dab522 RN |
187 | static void save_image_and_draw_graph_mark (GtkWidget *image, |
188 | gdouble event_x, | |
189 | gint img_width, | |
190 | GdkGC *gc, | |
191 | PropSaved *saved_img, | |
192 | gint PROFILE_WIDTH, | |
193 | gint PROFILE_HEIGHT, | |
194 | gboolean *marker_drawn) | |
32e48121 | 195 | { |
d973878f | 196 | GdkPixmap *pix = NULL; |
9dc30292 QT |
197 | /* the pixmap = margin + graph area */ |
198 | gdouble x = event_x - img_width/2 + PROFILE_WIDTH/2 + MARGIN/2; | |
199 | ||
e60fc2c9 | 200 | // fprintf(stderr, "event_x=%f img_width=%d x=%f\n", event_x, img_width, x); |
ca7e67ef QT |
201 | |
202 | gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL); | |
7ee55449 | 203 | /* Restore previously saved image */ |
ca7e67ef QT |
204 | if (saved_img->saved) { |
205 | gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, | |
206 | saved_img->pos, 0, -1, -1); | |
207 | saved_img->saved = FALSE; | |
9dc30292 QT |
208 | gtk_widget_queue_draw_area(image, |
209 | saved_img->pos + img_width/2 - PROFILE_WIDTH/2 - MARGIN/2, 0, | |
ca7e67ef QT |
210 | saved_img->img->width, saved_img->img->height); |
211 | } | |
212 | if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) { | |
7ee55449 | 213 | /* Save part of the image */ |
ca7e67ef QT |
214 | if (saved_img->img) |
215 | gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, | |
d973878f | 216 | x, 0, 0, 0, saved_img->img->width, saved_img->img->height); |
ca7e67ef QT |
217 | else |
218 | saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), | |
d973878f GB |
219 | saved_img->img, x, 0, 0, 0, 1, PROFILE_HEIGHT); |
220 | saved_img->pos = x; | |
ca7e67ef QT |
221 | saved_img->saved = TRUE; |
222 | gdk_draw_line (GDK_DRAWABLE(pix), gc, x, 0, x, image->allocation.height); | |
223 | /* redraw the area which contains the line, saved_width is just convenient */ | |
d973878f | 224 | gtk_widget_queue_draw_area(image, event_x, 0, 1, PROFILE_HEIGHT); |
7ee55449 | 225 | *marker_drawn = TRUE; |
ca7e67ef | 226 | } |
7ee55449 RN |
227 | else |
228 | *marker_drawn = FALSE; | |
32e48121 QT |
229 | } |
230 | ||
fb822443 RN |
231 | /** |
232 | * Return the percentage of how far a trackpoint is a long a track via the time method | |
233 | */ | |
234 | static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint ) | |
235 | { | |
236 | gdouble pc = NAN; | |
237 | if (trackpoint == NULL) | |
238 | return pc; | |
239 | time_t t_start, t_end, t_total; | |
240 | t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
241 | t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
242 | t_total = t_end - t_start; | |
243 | pc = (gdouble)(trackpoint->timestamp - t_start)/t_total; | |
244 | return pc; | |
245 | } | |
246 | ||
247 | /** | |
248 | * Return the percentage of how far a trackpoint is a long a track via the distance method | |
249 | */ | |
250 | static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length ) | |
251 | { | |
252 | gdouble pc = NAN; | |
253 | if (trackpoint == NULL) | |
254 | return pc; | |
255 | gdouble dist = 0.0; | |
256 | GList *iter; | |
257 | for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) { | |
258 | dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord), | |
259 | &(VIK_TRACKPOINT(iter->prev->data)->coord)); | |
260 | /* Assuming trackpoint is not a copy */ | |
261 | if (trackpoint == VIK_TRACKPOINT(iter->data)) | |
262 | break; | |
263 | } | |
264 | if (iter != NULL) | |
265 | pc = dist/track_length; | |
266 | return pc; | |
267 | } | |
268 | ||
ca7e67ef | 269 | static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph ) |
32e48121 QT |
270 | { |
271 | VikTrack *tr = pass_along[0]; | |
272 | VikLayersPanel *vlp = pass_along[1]; | |
6bb72350 RN |
273 | VikViewport *vvp = pass_along[2]; |
274 | PropWidgets *widgets = pass_along[3]; | |
ca7e67ef QT |
275 | GList *child = gtk_container_get_children(GTK_CONTAINER(event_box)); |
276 | GtkWidget *image = GTK_WIDGET(child->data); | |
277 | GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box)); | |
278 | ||
b66b0b38 | 279 | VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_vt_graph, widgets->profile_width); |
b1dab522 RN |
280 | save_image_and_draw_graph_mark(image, |
281 | event->x, | |
282 | event_box->allocation.width, | |
283 | window->style->black_gc, | |
284 | is_vt_graph ? &widgets->speed_graph_saved_img : &widgets->elev_graph_saved_img, | |
285 | widgets->profile_width, | |
286 | widgets->profile_height, | |
287 | &widgets->is_marker_drawn); | |
ca7e67ef | 288 | g_list_free(child); |
8dcb3ba4 | 289 | widgets->marker_tp = trackpoint; |
7ee55449 | 290 | gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn); |
ca7e67ef | 291 | |
e60fc2c9 QT |
292 | /* draw on the other graph */ |
293 | if (trackpoint == NULL || widgets->elev_box == NULL || widgets->speed_box == NULL) | |
294 | /* This test assumes we have only 2 graphs */ | |
295 | return; | |
296 | ||
297 | gdouble pc = NAN; | |
298 | gdouble x2; | |
299 | GList *other_child = gtk_container_get_children(GTK_CONTAINER( | |
300 | is_vt_graph ? widgets->elev_box : widgets->speed_box)); | |
301 | GtkWidget *other_image = GTK_WIDGET(other_child->data); | |
302 | if (is_vt_graph) { | |
fb822443 | 303 | pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length ); |
e60fc2c9 | 304 | } else { |
fb822443 | 305 | pc = tp_percentage_by_time ( tr, trackpoint ); |
e60fc2c9 QT |
306 | } |
307 | if (!isnan(pc)) { | |
b66b0b38 | 308 | x2 = pc * widgets->profile_width + MARGIN + (event_box->allocation.width/2 - widgets->profile_width/2 - MARGIN/2); |
b1dab522 RN |
309 | save_image_and_draw_graph_mark(other_image, |
310 | x2, | |
311 | event_box->allocation.width, | |
312 | window->style->black_gc, | |
313 | is_vt_graph ? &widgets->elev_graph_saved_img : &widgets->speed_graph_saved_img, | |
314 | widgets->profile_width, | |
315 | widgets->profile_height, | |
316 | &widgets->is_marker_drawn); | |
e60fc2c9 QT |
317 | } |
318 | ||
319 | g_list_free(other_child); | |
320 | ||
ca7e67ef QT |
321 | } |
322 | ||
323 | static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along ) | |
324 | { | |
325 | track_graph_click(event_box, event, pass_along, FALSE); | |
326 | return TRUE; /* don't call other (further) callbacks */ | |
327 | } | |
328 | ||
329 | static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along ) | |
330 | { | |
331 | track_graph_click(event_box, event, pass_along, TRUE); | |
332 | return TRUE; /* don't call other (further) callbacks */ | |
32e48121 QT |
333 | } |
334 | ||
e1e2f2c6 JJ |
335 | void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along ) |
336 | { | |
337 | VikTrack *tr = pass_along[0]; | |
6bb72350 | 338 | PropWidgets *widgets = pass_along[3]; |
e1e2f2c6 JJ |
339 | int mouse_x, mouse_y; |
340 | GdkModifierType state; | |
341 | ||
342 | if (event->is_hint) | |
343 | gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state); | |
344 | else | |
345 | mouse_x = event->x; | |
346 | ||
b66b0b38 | 347 | gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2; |
e1e2f2c6 JJ |
348 | if (x < 0) |
349 | x = 0; | |
b66b0b38 RN |
350 | if (x > widgets->profile_width) |
351 | x = widgets->profile_width; | |
e1e2f2c6 | 352 | |
ddc2372e | 353 | gdouble meters_from_start; |
b66b0b38 | 354 | VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start ); |
065f60f1 | 355 | if (trackpoint && widgets->w_cur_dist) { |
ddc2372e | 356 | static gchar tmp_buf[20]; |
6f9336aa RN |
357 | vik_units_distance_t dist_units = a_vik_get_units_distance (); |
358 | switch (dist_units) { | |
359 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
360 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0); | |
361 | break; | |
362 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 363 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) ); |
6f9336aa RN |
364 | break; |
365 | default: | |
366 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
367 | } | |
065f60f1 | 368 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf); |
ddc2372e | 369 | } |
780d3771 RN |
370 | |
371 | // Show track elevation for this position - to the nearest whole number | |
372 | if (trackpoint && widgets->w_cur_elevation) { | |
373 | static gchar tmp_buf[20]; | |
374 | if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET) | |
375 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude)); | |
376 | else | |
377 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude); | |
378 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_elevation), tmp_buf); | |
379 | } | |
ddc2372e QT |
380 | } |
381 | ||
382 | void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along ) | |
383 | { | |
384 | VikTrack *tr = pass_along[0]; | |
6bb72350 | 385 | PropWidgets *widgets = pass_along[3]; |
ddc2372e QT |
386 | int mouse_x, mouse_y; |
387 | GdkModifierType state; | |
388 | ||
389 | if (event->is_hint) | |
390 | gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state); | |
391 | else | |
392 | mouse_x = event->x; | |
393 | ||
b66b0b38 | 394 | gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2; |
ddc2372e QT |
395 | if (x < 0) |
396 | x = 0; | |
b66b0b38 RN |
397 | if (x > widgets->profile_width) |
398 | x = widgets->profile_width; | |
ddc2372e QT |
399 | |
400 | time_t seconds_from_start; | |
b66b0b38 | 401 | VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start ); |
065f60f1 | 402 | if (trackpoint && widgets->w_cur_time) { |
ddc2372e QT |
403 | static gchar tmp_buf[20]; |
404 | guint h, m, s; | |
405 | h = seconds_from_start/3600; | |
406 | m = (seconds_from_start - h*3600)/60; | |
407 | s = seconds_from_start - (3600*h) - (60*m); | |
408 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s); | |
e1e2f2c6 | 409 | |
065f60f1 | 410 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf); |
24d5c7e2 | 411 | } |
780d3771 RN |
412 | |
413 | // Show track speed for this position | |
414 | if (trackpoint && widgets->w_cur_speed) { | |
415 | static gchar tmp_buf[20]; | |
416 | // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown | |
417 | gint ix = (gint)x; | |
418 | // Ensure ix is inbounds | |
419 | if (ix == widgets->profile_width) | |
420 | ix--; | |
421 | ||
422 | vik_units_speed_t speed_units = a_vik_get_units_speed (); | |
423 | switch (speed_units) { | |
424 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
425 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), VIK_MPS_TO_KPH(widgets->speeds[ix])); | |
426 | break; | |
427 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
428 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), VIK_MPS_TO_MPH(widgets->speeds[ix])); | |
429 | break; | |
430 | case VIK_UNITS_SPEED_KNOTS: | |
431 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), VIK_MPS_TO_KNOTS(widgets->speeds[ix])); | |
432 | break; | |
433 | default: | |
434 | // VIK_UNITS_SPEED_METRES_PER_SECOND: | |
435 | // No need to convert as already in m/s | |
436 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds[ix]); | |
437 | break; | |
438 | } | |
439 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed), tmp_buf); | |
440 | } | |
24d5c7e2 EB |
441 | } |
442 | ||
b42009f6 | 443 | static void draw_dem_alt_speed_dist(VikTrack *tr, GdkDrawable *pix, GdkGC *alt_gc, GdkGC *speed_gc, gdouble alt_offset, gdouble alt_diff, gint width, gint height, gint margin) |
07596bf4 QT |
444 | { |
445 | GList *iter; | |
446 | gdouble dist = 0; | |
447 | gdouble max_speed = 0; | |
448 | gdouble total_length = vik_track_get_length_including_gaps(tr); | |
449 | ||
450 | for (iter = tr->trackpoints->next; iter; iter = iter->next) { | |
451 | if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) | |
452 | max_speed = MAX(max_speed, VIK_TRACKPOINT(iter->data)->speed); | |
453 | } | |
3220e336 | 454 | max_speed = max_speed * 110 / 100; |
07596bf4 QT |
455 | |
456 | for (iter = tr->trackpoints->next; iter; iter = iter->next) { | |
457 | int x, y_alt, y_speed; | |
228213c5 | 458 | gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST); |
b42009f6 | 459 | elev -= alt_offset; |
07596bf4 QT |
460 | dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), |
461 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
462 | x = (width * dist)/total_length + margin; | |
463 | if ( elev != VIK_DEM_INVALID_ELEVATION ) { | |
2c50b28a | 464 | y_alt = height - ((height * elev)/alt_diff); |
07596bf4 QT |
465 | gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4); |
466 | } | |
467 | if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) { | |
468 | y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed; | |
469 | gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
950d8a07 RN |
474 | /** |
475 | * Draw just the height profile image | |
476 | */ | |
477 | static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets ) | |
50a14534 | 478 | { |
950d8a07 | 479 | GtkWidget *window; |
c79f0206 | 480 | GdkPixmap *pix; |
25e44eac | 481 | gdouble mina, maxa; |
50a14534 EB |
482 | guint i; |
483 | ||
950d8a07 RN |
484 | GdkGC *no_alt_info; |
485 | GdkGC *dem_alt_gc; | |
486 | GdkGC *gps_speed_gc; | |
487 | ||
488 | GdkColor color; | |
489 | ||
0b2bfa08 RN |
490 | // Free previous allocation |
491 | if ( widgets->altitudes ) | |
492 | g_free ( widgets->altitudes ); | |
493 | ||
b66b0b38 | 494 | widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width ); |
950d8a07 | 495 | |
0b2bfa08 | 496 | if ( widgets->altitudes == NULL ) |
950d8a07 RN |
497 | return; |
498 | ||
b66b0b38 | 499 | minmax_array(widgets->altitudes, &mina, &maxa, TRUE, widgets->profile_width); |
950d8a07 RN |
500 | maxa = maxa + ((maxa - mina) * 0.25); // Make visible window a bit bigger than highest point |
501 | ||
502 | window = gtk_widget_get_toplevel (widgets->elev_box); | |
c79f0206 | 503 | |
b66b0b38 | 504 | pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 ); |
c79f0206 | 505 | |
950d8a07 | 506 | gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL ); |
50a14534 | 507 | |
950d8a07 | 508 | no_alt_info = gdk_gc_new ( window->window ); |
07596bf4 | 509 | gdk_color_parse ( "yellow", &color ); |
50a14534 | 510 | gdk_gc_set_rgb_fg_color ( no_alt_info, &color); |
950d8a07 RN |
511 | |
512 | dem_alt_gc = gdk_gc_new ( window->window ); | |
07596bf4 QT |
513 | gdk_color_parse ( "green", &color ); |
514 | gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color); | |
950d8a07 RN |
515 | |
516 | gps_speed_gc = gdk_gc_new ( window->window ); | |
07596bf4 QT |
517 | gdk_color_parse ( "red", &color ); |
518 | gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color); | |
50a14534 | 519 | |
25e44eac AF |
520 | /* clear the image */ |
521 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], | |
b66b0b38 | 522 | TRUE, 0, 0, MARGIN, widgets->profile_height); |
25e44eac | 523 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], |
b66b0b38 | 524 | TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height); |
950d8a07 | 525 | |
25e44eac | 526 | /* draw grid */ |
6027a9c5 | 527 | vik_units_height_t height_units = a_vik_get_units_height (); |
25e44eac AF |
528 | for (i=0; i<=LINES; i++) { |
529 | PangoFontDescription *pfd; | |
530 | PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL); | |
531 | gchar s[32]; | |
d03d80e6 | 532 | int w, h; |
25e44eac | 533 | |
d03d80e6 | 534 | pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT); |
950d8a07 | 535 | pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT); |
25e44eac AF |
536 | pango_layout_set_font_description (pl, pfd); |
537 | pango_font_description_free (pfd); | |
6027a9c5 RN |
538 | switch (height_units) { |
539 | case VIK_UNITS_HEIGHT_METRES: | |
540 | sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES)); | |
541 | break; | |
542 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 543 | sprintf(s, "%8dft", (int)VIK_METERS_TO_FEET(mina + (LINES-i)*(maxa-mina)/LINES)); |
6027a9c5 RN |
544 | break; |
545 | default: | |
546 | sprintf(s, "--"); | |
547 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
548 | } | |
25e44eac | 549 | pango_layout_set_text(pl, s, -1); |
d03d80e6 AF |
550 | pango_layout_get_pixel_size (pl, &w, &h); |
551 | gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, | |
b66b0b38 | 552 | CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl); |
25e44eac AF |
553 | |
554 | gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], | |
b66b0b38 | 555 | MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i); |
6278ccc6 | 556 | g_object_unref ( G_OBJECT ( pl ) ); |
e6869c62 | 557 | pl = NULL; |
25e44eac AF |
558 | } |
559 | ||
560 | /* draw elevations */ | |
b66b0b38 | 561 | for ( i = 0; i < widgets->profile_width; i++ ) |
0b2bfa08 | 562 | if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE ) |
25e44eac | 563 | gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, |
b66b0b38 | 564 | i + MARGIN, 0, i + MARGIN, widgets->profile_height ); |
bb71de8b | 565 | else |
25e44eac | 566 | gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], |
b66b0b38 | 567 | i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(maxa-mina) ); |
734652bf | 568 | |
b66b0b38 | 569 | draw_dem_alt_speed_dist(tr, GDK_DRAWABLE(pix), dem_alt_gc, gps_speed_gc, mina, maxa - mina, widgets->profile_width, widgets->profile_height, MARGIN); |
07596bf4 | 570 | |
25e44eac | 571 | /* draw border */ |
b66b0b38 | 572 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1); |
25e44eac | 573 | |
50a14534 | 574 | g_object_unref ( G_OBJECT(pix) ); |
50a14534 | 575 | g_object_unref ( G_OBJECT(no_alt_info) ); |
07596bf4 QT |
576 | g_object_unref ( G_OBJECT(dem_alt_gc) ); |
577 | g_object_unref ( G_OBJECT(gps_speed_gc) ); | |
24d5c7e2 | 578 | |
50a14534 | 579 | } |
24d5c7e2 | 580 | |
950d8a07 RN |
581 | /** |
582 | * Draw just the speed (velocity)/time image | |
583 | */ | |
584 | static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets) | |
25e44eac | 585 | { |
950d8a07 | 586 | GtkWidget *window; |
c79f0206 | 587 | GdkPixmap *pix; |
25e44eac | 588 | gdouble mins, maxs; |
25e44eac | 589 | guint i; |
c79f0206 | 590 | |
0b2bfa08 RN |
591 | // Free previous allocation |
592 | if ( widgets->speeds ) | |
593 | g_free ( widgets->speeds ); | |
594 | ||
b66b0b38 | 595 | widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width ); |
0b2bfa08 | 596 | if ( widgets->speeds == NULL ) |
950d8a07 RN |
597 | return; |
598 | ||
599 | GdkGC *gps_speed_gc; | |
600 | GdkColor color; | |
601 | ||
602 | window = gtk_widget_get_toplevel (widgets->speed_box); | |
c79f0206 | 603 | |
b66b0b38 | 604 | pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 ); |
950d8a07 RN |
605 | |
606 | gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL ); | |
607 | ||
608 | gps_speed_gc = gdk_gc_new ( window->window ); | |
609 | gdk_color_parse ( "red", &color ); | |
610 | gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color); | |
c79f0206 | 611 | |
b66b0b38 | 612 | minmax_array(widgets->speeds, &mins, &maxs, FALSE, widgets->profile_width); |
2c50b28a QT |
613 | if (mins < 0.0) |
614 | mins = 0; /* splines sometimes give negative speeds */ | |
615 | maxs = maxs + ((maxs - mins) * 0.1); | |
25e44eac AF |
616 | if (maxs-mins < MIN_SPEED_DIFF) { |
617 | maxs = mins + MIN_SPEED_DIFF; | |
618 | } | |
619 | ||
620 | /* clear the image */ | |
621 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], | |
b66b0b38 | 622 | TRUE, 0, 0, MARGIN, widgets->profile_height); |
25e44eac | 623 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], |
b66b0b38 | 624 | TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height); |
25e44eac | 625 | |
25e44eac | 626 | /* draw grid */ |
25e44eac AF |
627 | for (i=0; i<=LINES; i++) { |
628 | PangoFontDescription *pfd; | |
629 | PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL); | |
630 | gchar s[32]; | |
d03d80e6 | 631 | int w, h; |
25e44eac | 632 | |
d03d80e6 | 633 | pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT); |
950d8a07 | 634 | pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT); |
25e44eac AF |
635 | pango_layout_set_font_description (pl, pfd); |
636 | pango_font_description_free (pfd); | |
13bdea80 RN |
637 | vik_units_speed_t speed_units = a_vik_get_units_speed (); |
638 | switch (speed_units) { | |
639 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
7972f8ff | 640 | sprintf(s, "%6.1fkm/h", VIK_MPS_TO_KPH(mins + (LINES-i)*(maxs-mins)/LINES)); |
13bdea80 RN |
641 | break; |
642 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
7972f8ff | 643 | sprintf(s, "%6.1fmph", VIK_MPS_TO_MPH(mins + (LINES-i)*(maxs-mins)/LINES)); |
13bdea80 RN |
644 | break; |
645 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
646 | sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*(maxs-mins)/LINES)); | |
647 | break; | |
a4c92313 | 648 | case VIK_UNITS_SPEED_KNOTS: |
7972f8ff | 649 | sprintf(s, "%6.1fknots", VIK_MPS_TO_KNOTS(mins + (LINES-i)*(maxs-mins)/LINES)); |
a4c92313 | 650 | break; |
13bdea80 RN |
651 | default: |
652 | sprintf(s, "--"); | |
653 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
654 | } | |
655 | ||
25e44eac | 656 | pango_layout_set_text(pl, s, -1); |
d03d80e6 AF |
657 | pango_layout_get_pixel_size (pl, &w, &h); |
658 | gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, | |
b66b0b38 | 659 | CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl); |
25e44eac AF |
660 | |
661 | gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], | |
b66b0b38 | 662 | MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i); |
6278ccc6 | 663 | g_object_unref ( G_OBJECT ( pl ) ); |
e6869c62 | 664 | pl = NULL; |
25e44eac | 665 | } |
d03d80e6 | 666 | |
25e44eac AF |
667 | |
668 | /* draw speeds */ | |
b66b0b38 | 669 | for ( i = 0; i < widgets->profile_width; i++ ) |
25e44eac | 670 | gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], |
b66b0b38 | 671 | i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(maxs-mins) ); |
07596bf4 | 672 | |
07596bf4 QT |
673 | |
674 | time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
675 | time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time; | |
676 | GList *iter; | |
677 | for (iter = tr->trackpoints; iter; iter = iter->next) { | |
678 | gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed; | |
679 | if (isnan(gps_speed)) | |
680 | continue; | |
b66b0b38 RN |
681 | int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur; |
682 | int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(maxs - mins); | |
07596bf4 QT |
683 | gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4); |
684 | } | |
685 | ||
25e44eac | 686 | /* draw border */ |
b66b0b38 | 687 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1); |
25e44eac AF |
688 | |
689 | g_object_unref ( G_OBJECT(pix) ); | |
07596bf4 | 690 | g_object_unref ( G_OBJECT(gps_speed_gc) ); |
561e6ad0 | 691 | |
950d8a07 RN |
692 | } |
693 | #undef LINES | |
694 | ||
950d8a07 | 695 | /** |
2f078f56 | 696 | * Draw all graphs |
950d8a07 | 697 | */ |
2f078f56 | 698 | static void draw_all_graphs ( GtkWidget *widget, gpointer *pass_along, gboolean resized ) |
950d8a07 RN |
699 | { |
700 | VikTrack *tr = pass_along[0]; | |
701 | PropWidgets *widgets = pass_along[2]; | |
702 | ||
950d8a07 RN |
703 | // Draw graphs even if they are not visible |
704 | ||
705 | GList *child = NULL; | |
2f078f56 RN |
706 | GtkWidget *image = NULL; |
707 | GtkWidget *window = gtk_widget_get_toplevel(widget); | |
708 | gdouble pc = NAN; | |
709 | ||
950d8a07 RN |
710 | // Draw elevations |
711 | if (widgets->elev_box != NULL) { | |
2f078f56 RN |
712 | |
713 | // Saved image no longer any good as we've resized, so we remove it here | |
714 | if (resized && widgets->elev_graph_saved_img.img) { | |
715 | g_object_unref(widgets->elev_graph_saved_img.img); | |
716 | widgets->elev_graph_saved_img.img = NULL; | |
717 | widgets->elev_graph_saved_img.saved = FALSE; | |
718 | } | |
719 | ||
950d8a07 RN |
720 | child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box)); |
721 | draw_elevations (GTK_WIDGET(child->data), tr, widgets ); | |
2f078f56 RN |
722 | |
723 | image = GTK_WIDGET(child->data); | |
950d8a07 | 724 | g_list_free(child); |
2f078f56 RN |
725 | |
726 | // Ensure marker is redrawn if necessary | |
727 | if (widgets->is_marker_drawn) { | |
728 | ||
729 | pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length ); | |
730 | gdouble marker_x = 0.0; | |
731 | if (!isnan(pc)) { | |
732 | marker_x = (pc * widgets->profile_width) + MARGIN + (image->allocation.width/2 - widgets->profile_width/2 - MARGIN/2); | |
733 | save_image_and_draw_graph_mark(image, | |
734 | marker_x, | |
735 | image->allocation.width, | |
736 | window->style->black_gc, | |
737 | &widgets->elev_graph_saved_img, | |
738 | widgets->profile_width, | |
739 | widgets->profile_height, | |
740 | &widgets->is_marker_drawn); | |
741 | } | |
742 | } | |
950d8a07 RN |
743 | } |
744 | ||
745 | // Draw speeds | |
746 | if (widgets->speed_box != NULL) { | |
2f078f56 RN |
747 | |
748 | // Saved image no longer any good as we've resized | |
749 | if (resized && widgets->speed_graph_saved_img.img) { | |
750 | g_object_unref(widgets->speed_graph_saved_img.img); | |
751 | widgets->speed_graph_saved_img.img = NULL; | |
752 | widgets->speed_graph_saved_img.saved = FALSE; | |
753 | } | |
754 | ||
950d8a07 RN |
755 | child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box)); |
756 | draw_vt (GTK_WIDGET(child->data), tr, widgets ); | |
2f078f56 RN |
757 | |
758 | image = GTK_WIDGET(child->data); | |
950d8a07 | 759 | g_list_free(child); |
2f078f56 RN |
760 | |
761 | // Ensure marker is redrawn if necessary | |
762 | if (widgets->is_marker_drawn) { | |
763 | ||
764 | pc = tp_percentage_by_time ( tr, widgets->marker_tp ); | |
765 | ||
766 | gdouble marker_x = 0.0; | |
767 | if (!isnan(pc)) { | |
768 | marker_x = (pc * widgets->profile_width) + MARGIN + (image->allocation.width/2 - widgets->profile_width/2 - MARGIN/2); | |
769 | save_image_and_draw_graph_mark(image, | |
770 | marker_x, | |
771 | image->allocation.width, | |
772 | window->style->black_gc, | |
773 | &widgets->speed_graph_saved_img, | |
774 | widgets->profile_width, | |
775 | widgets->profile_height, | |
776 | &widgets->is_marker_drawn); | |
777 | } | |
778 | } | |
779 | } | |
780 | } | |
781 | ||
782 | /** | |
783 | * Configure/Resize the profile & speed/time images | |
784 | */ | |
785 | static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along ) | |
786 | { | |
787 | PropWidgets *widgets = pass_along[2]; | |
788 | ||
789 | if (widgets->configure_dialog) { | |
790 | // Determine size offsets between dialog size and size for images | |
791 | // Only on the initialisation of the dialog | |
792 | widgets->profile_width_offset = event->width - widgets->profile_width; | |
793 | widgets->profile_height_offset = event->height - widgets->profile_height; | |
794 | widgets->configure_dialog = FALSE; | |
795 | ||
796 | // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller! | |
797 | gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height ); | |
798 | // In fact this allows one to compress it a bit more vertically as I don't add on the height offset | |
950d8a07 | 799 | } |
2f078f56 RN |
800 | else { |
801 | widgets->profile_width_old = widgets->profile_width; | |
802 | widgets->profile_height_old = widgets->profile_height; | |
803 | } | |
804 | ||
805 | // Now adjust From Dialog size to get image size | |
806 | widgets->profile_width = event->width - widgets->profile_width_offset; | |
807 | widgets->profile_height = event->height - widgets->profile_height_offset; | |
808 | ||
809 | // ATM we receive configure_events when the dialog is moved and so no further action is necessary | |
810 | if ( !widgets->configure_dialog && | |
811 | (widgets->profile_width_old == widgets->profile_width) && (widgets->profile_height_old == widgets->profile_height) ) | |
812 | return FALSE; | |
813 | ||
814 | // Draw stuff | |
815 | draw_all_graphs ( widget, pass_along, TRUE ); | |
816 | ||
950d8a07 RN |
817 | return FALSE; |
818 | } | |
819 | ||
820 | /** | |
821 | * Create height profile widgets including the image and callbacks | |
822 | */ | |
823 | GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt) | |
824 | { | |
825 | GdkPixmap *pix; | |
826 | GtkWidget *image; | |
950d8a07 RN |
827 | GtkWidget *eventbox; |
828 | gpointer *pass_along; | |
829 | ||
0b2bfa08 | 830 | // First allocation |
b66b0b38 | 831 | widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width ); |
0b2bfa08 RN |
832 | |
833 | if ( widgets->altitudes == NULL ) { | |
950d8a07 RN |
834 | *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE; |
835 | return NULL; | |
836 | } | |
837 | ||
b66b0b38 | 838 | minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width); |
950d8a07 | 839 | |
b66b0b38 | 840 | pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 ); |
950d8a07 RN |
841 | image = gtk_image_new_from_pixmap ( pix, NULL ); |
842 | ||
950d8a07 RN |
843 | g_object_unref ( G_OBJECT(pix) ); |
844 | ||
845 | pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */ | |
846 | pass_along[0] = tr; | |
847 | pass_along[1] = vlp; | |
848 | pass_along[2] = vvp; | |
849 | pass_along[3] = widgets; | |
850 | ||
851 | eventbox = gtk_event_box_new (); | |
852 | g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along ); | |
853 | g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along ); | |
854 | g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along ); | |
855 | gtk_container_add ( GTK_CONTAINER(eventbox), image ); | |
856 | gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK); | |
857 | ||
858 | return eventbox; | |
859 | } | |
860 | ||
861 | /** | |
862 | * Create speed/time widgets including the image and callbacks | |
863 | */ | |
864 | GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets) | |
865 | { | |
866 | GdkPixmap *pix; | |
867 | GtkWidget *image; | |
868 | GtkWidget *eventbox; | |
869 | gpointer *pass_along; | |
870 | ||
0b2bfa08 | 871 | // First allocation |
b66b0b38 | 872 | widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width ); |
0b2bfa08 | 873 | if ( widgets->speeds == NULL ) |
950d8a07 RN |
874 | return NULL; |
875 | ||
876 | pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */ | |
877 | pass_along[0] = tr; | |
878 | pass_along[1] = vlp; | |
879 | pass_along[2] = vvp; | |
880 | pass_along[3] = widgets; | |
881 | ||
b66b0b38 | 882 | pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 ); |
950d8a07 RN |
883 | image = gtk_image_new_from_pixmap ( pix, NULL ); |
884 | ||
885 | #if 0 | |
886 | /* XXX this can go out, it's just a helpful dev tool */ | |
887 | { | |
888 | int j; | |
889 | GdkGC **colors[8] = { window->style->bg_gc, | |
890 | window->style->fg_gc, | |
891 | window->style->light_gc, | |
892 | window->style->dark_gc, | |
893 | window->style->mid_gc, | |
894 | window->style->text_gc, | |
895 | window->style->base_gc, | |
896 | window->style->text_aa_gc }; | |
897 | for (i=0; i<5; i++) { | |
898 | for (j=0; j<8; j++) { | |
899 | gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i], | |
900 | TRUE, i*20, j*20, 20, 20); | |
901 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, | |
902 | FALSE, i*20, j*20, 20, 20); | |
903 | } | |
904 | } | |
905 | } | |
906 | #endif | |
907 | ||
950d8a07 RN |
908 | g_object_unref ( G_OBJECT(pix) ); |
909 | ||
561e6ad0 | 910 | eventbox = gtk_event_box_new (); |
32e48121 | 911 | g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along ); |
ddc2372e | 912 | g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along ); |
561e6ad0 EB |
913 | g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along ); |
914 | gtk_container_add ( GTK_CONTAINER(eventbox), image ); | |
950d8a07 | 915 | gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK); |
561e6ad0 EB |
916 | |
917 | return eventbox; | |
25e44eac AF |
918 | } |
919 | #undef MARGIN | |
50a14534 | 920 | |
42f34743 | 921 | static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets) |
50a14534 | 922 | { |
21700912 QT |
923 | VikTrack *tr = widgets->tr; |
924 | VikTrwLayer *vtl = widgets->vtl; | |
8dcb3ba4 | 925 | gboolean keep_dialog = FALSE; |
21700912 QT |
926 | |
927 | /* FIXME: check and make sure the track still exists before doing anything to it */ | |
928 | /* Note: destroying diaglog (eg, parent window exit) won't give "response" */ | |
929 | switch (resp) { | |
930 | case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */ | |
931 | case GTK_RESPONSE_REJECT: | |
932 | break; | |
933 | case GTK_RESPONSE_ACCEPT: | |
934 | vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment))); | |
935 | break; | |
936 | case VIK_TRW_LAYER_PROPWIN_REVERSE: | |
937 | vik_track_reverse(tr); | |
938 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
939 | break; | |
940 | case VIK_TRW_LAYER_PROPWIN_DEL_DUP: | |
941 | vik_track_remove_dup_points(tr); | |
942 | /* above operation could have deleted current_tp or last_tp */ | |
943 | trw_layer_cancel_tps_of_track ( vtl, widgets->track_name ); | |
944 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
945 | break; | |
946 | case VIK_TRW_LAYER_PROPWIN_SPLIT: | |
947 | { | |
948 | /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */ | |
949 | guint ntracks; | |
950 | VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks); | |
951 | gchar *new_tr_name; | |
952 | guint i; | |
953 | for ( i = 0; i < ntracks; i++ ) | |
954 | { | |
955 | g_assert ( tracks[i] ); | |
956 | new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1); | |
957 | /* if ( (wp_exists) && (! overwrite) ) */ | |
958 | /* don't need to upper case new_tr_name because old tr name was uppercase */ | |
959 | if ( vik_trw_layer_get_track(vtl, new_tr_name ) && | |
d91e5f2b | 960 | ( ! a_dialog_yes_or_no ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) ) |
21700912 | 961 | { |
e13ab673 | 962 | gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL ); |
21700912 QT |
963 | g_free ( new_tr_name ); |
964 | if (new_new_tr_name) | |
965 | new_tr_name = new_new_tr_name; | |
966 | else | |
967 | { | |
968 | new_tr_name = NULL; | |
969 | vik_track_free ( tracks[i] ); | |
970 | } | |
971 | } | |
972 | if ( new_tr_name ) | |
973 | vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] ); | |
974 | } | |
975 | if ( tracks ) | |
976 | { | |
977 | g_free ( tracks ); | |
978 | /* Don't let track destroy this dialog */ | |
979 | vik_track_clear_property_dialog(tr); | |
980 | vik_trw_layer_delete_track ( vtl, widgets->track_name ); | |
981 | vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */ | |
982 | } | |
983 | } | |
984 | break; | |
8dcb3ba4 QT |
985 | case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER: |
986 | { | |
987 | GList *iter = tr->trackpoints; | |
988 | while ((iter = iter->next)) { | |
989 | if (widgets->marker_tp == VIK_TRACKPOINT(iter->data)) | |
990 | break; | |
991 | } | |
992 | if (iter == NULL) { | |
993 | a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR, | |
994 | _("Failed spliting track. Track unchanged"), NULL); | |
995 | keep_dialog = TRUE; | |
996 | break; | |
997 | } | |
998 | ||
999 | gchar *r_name = g_strdup_printf("%s #R", widgets->track_name); | |
1000 | if (vik_trw_layer_get_track(vtl, r_name ) && | |
d91e5f2b | 1001 | ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl), |
8dcb3ba4 QT |
1002 | "The track \"%s\" exists, do you wish to overwrite it?", r_name))) |
1003 | { | |
e13ab673 | 1004 | gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL ); |
8dcb3ba4 QT |
1005 | if (new_r_name) { |
1006 | g_free( r_name ); | |
1007 | r_name = new_r_name; | |
1008 | } | |
1009 | else { | |
1010 | a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING, | |
1011 | _("Operation Aborted. Track unchanged"), NULL); | |
1012 | keep_dialog = TRUE; | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | iter->prev->next = NULL; | |
1017 | iter->prev = NULL; | |
1018 | VikTrack *tr_right = vik_track_new(); | |
1019 | if ( tr->comment ) | |
1020 | vik_track_set_comment ( tr_right, tr->comment ); | |
1021 | tr_right->visible = tr->visible; | |
1022 | tr_right->trackpoints = iter; | |
1023 | ||
1024 | vik_trw_layer_add_track(vtl, r_name, tr_right); | |
1025 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
1026 | } | |
1027 | break; | |
21700912 QT |
1028 | default: |
1029 | fprintf(stderr, "DEBUG: unknown response\n"); | |
1030 | return; | |
1031 | } | |
1032 | ||
1033 | /* Keep same behaviour for now: destroy dialog if click on any button */ | |
8dcb3ba4 QT |
1034 | if (!keep_dialog) { |
1035 | prop_widgets_free(widgets); | |
1036 | vik_track_clear_property_dialog(tr); | |
1037 | gtk_widget_destroy ( GTK_WIDGET(dialog) ); | |
1038 | } | |
21700912 QT |
1039 | } |
1040 | ||
780d3771 RN |
1041 | /** |
1042 | * Create the widgets for the given graph tab | |
1043 | */ | |
065f60f1 GB |
1044 | static GtkWidget *create_graph_page ( GtkWidget *graph, |
1045 | const gchar *markup, | |
780d3771 RN |
1046 | GtkWidget *value, |
1047 | const gchar *markup2, | |
1048 | GtkWidget *value2) | |
065f60f1 GB |
1049 | { |
1050 | GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 ); | |
1051 | GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 ); | |
1052 | GtkWidget *label = gtk_label_new (NULL); | |
780d3771 | 1053 | GtkWidget *label2 = gtk_label_new (NULL); |
065f60f1 GB |
1054 | gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0); |
1055 | gtk_label_set_markup ( GTK_LABEL(label), markup ); | |
780d3771 | 1056 | gtk_label_set_markup ( GTK_LABEL(label2), markup2 ); |
065f60f1 GB |
1057 | gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0); |
1058 | gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0); | |
780d3771 RN |
1059 | gtk_box_pack_start (GTK_BOX(hbox), label2, FALSE, FALSE, 0); |
1060 | gtk_box_pack_start (GTK_BOX(hbox), value2, FALSE, FALSE, 0); | |
065f60f1 GB |
1061 | gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
1062 | ||
1063 | return vbox; | |
1064 | } | |
1065 | ||
6bb72350 | 1066 | void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name, VikViewport *vvp ) |
21700912 QT |
1067 | { |
1068 | /* FIXME: free widgets when destroy signal received */ | |
ca7e67ef | 1069 | PropWidgets *widgets = prop_widgets_new(); |
21700912 QT |
1070 | widgets->vtl = vtl; |
1071 | widgets->tr = tr; | |
1072 | widgets->vlp = vlp; | |
b66b0b38 RN |
1073 | widgets->profile_width = PROPWIN_PROFILE_WIDTH; |
1074 | widgets->profile_height = PROPWIN_PROFILE_HEIGHT; | |
21700912 | 1075 | widgets->track_name = track_name; |
4c77d5e0 | 1076 | gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name); |
dc27aba1 | 1077 | GtkWidget *dialog = gtk_dialog_new_with_buttons (title, |
21700912 QT |
1078 | parent, |
1079 | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, | |
1080 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | |
c51f24b1 RN |
1081 | _("Split at _Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, |
1082 | _("Split _Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT, | |
1083 | _("_Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE, | |
1084 | _("_Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP, | |
21700912 QT |
1085 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
1086 | NULL); | |
8dcb3ba4 | 1087 | widgets->dialog = dialog; |
dc27aba1 | 1088 | g_free(title); |
42f34743 | 1089 | g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets); |
4b00e581 | 1090 | GtkTable *table; |
50a14534 EB |
1091 | gdouble tr_len; |
1092 | guint32 tp_count, seg_count; | |
50a14534 EB |
1093 | |
1094 | gdouble min_alt, max_alt; | |
950d8a07 RN |
1095 | widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), tr, vlp, vvp, widgets, &min_alt, &max_alt); |
1096 | widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets); | |
25e44eac | 1097 | GtkWidget *graphs = gtk_notebook_new(); |
50a14534 | 1098 | |
4b00e581 AF |
1099 | GtkWidget *content[20]; |
1100 | int cnt; | |
1101 | int i; | |
1102 | ||
065f60f1 | 1103 | static gchar *label_texts[] = { N_("<b>Comment:</b>"), N_("<b>Track Length:</b>"), N_("<b>Trackpoints:</b>"), N_("<b>Segments:</b>"), N_("<b>Duplicate Points:</b>"), N_("<b>Max Speed:</b>"), N_("<b>Avg. Speed:</b>"), N_("<b>Avg. Dist. Between TPs:</b>"), N_("<b>Elevation Range:</b>"), N_("<b>Total Elevation Gain/Loss:</b>"), N_("<b>Start:</b>"), N_("<b>End:</b>"), N_("<b>Duration:</b>") }; |
32e48121 | 1104 | static gchar tmp_buf[50]; |
8c4f1350 | 1105 | gdouble tmp_speed; |
50a14534 | 1106 | |
4b00e581 | 1107 | cnt = 0; |
21700912 | 1108 | widgets->w_comment = gtk_entry_new (); |
50a14534 | 1109 | if ( tr->comment ) |
21700912 QT |
1110 | gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment ); |
1111 | g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) ); | |
1112 | content[cnt++] = widgets->w_comment; | |
50a14534 | 1113 | |
6f9336aa RN |
1114 | vik_units_distance_t dist_units = a_vik_get_units_distance (); |
1115 | ||
e60fc2c9 | 1116 | tr_len = widgets->track_length = vik_track_get_length(tr); |
6f9336aa RN |
1117 | switch (dist_units) { |
1118 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
1119 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 ); | |
1120 | break; | |
1121 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 1122 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) ); |
6f9336aa RN |
1123 | break; |
1124 | default: | |
1125 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
1126 | } | |
21700912 | 1127 | widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
1128 | |
1129 | tp_count = vik_track_get_tp_count(tr); | |
1130 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count ); | |
21700912 | 1131 | widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
1132 | |
1133 | seg_count = vik_track_get_segment_count(tr) ; | |
1134 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count ); | |
21700912 | 1135 | widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
1136 | |
1137 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) ); | |
21700912 | 1138 | widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 1139 | |
13bdea80 | 1140 | vik_units_speed_t speed_units = a_vik_get_units_speed (); |
8c4f1350 EB |
1141 | tmp_speed = vik_track_get_max_speed(tr); |
1142 | if ( tmp_speed == 0 ) | |
4c77d5e0 | 1143 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
13bdea80 RN |
1144 | else { |
1145 | switch (speed_units) { | |
1146 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
ab1e0693 | 1147 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed)); |
13bdea80 RN |
1148 | break; |
1149 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
ab1e0693 | 1150 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed)); |
13bdea80 RN |
1151 | break; |
1152 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
1153 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed ); | |
1154 | break; | |
a4c92313 | 1155 | case VIK_UNITS_SPEED_KNOTS: |
ab1e0693 | 1156 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed)); |
a4c92313 | 1157 | break; |
13bdea80 RN |
1158 | default: |
1159 | g_snprintf (tmp_buf, sizeof(tmp_buf), "--" ); | |
1160 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
1161 | } | |
1162 | } | |
21700912 | 1163 | widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 1164 | |
8c4f1350 EB |
1165 | tmp_speed = vik_track_get_average_speed(tr); |
1166 | if ( tmp_speed == 0 ) | |
4c77d5e0 | 1167 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
13bdea80 RN |
1168 | else { |
1169 | switch (speed_units) { | |
1170 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
ab1e0693 | 1171 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed)); |
13bdea80 RN |
1172 | break; |
1173 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
ab1e0693 | 1174 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed)); |
13bdea80 RN |
1175 | break; |
1176 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
1177 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed ); | |
1178 | break; | |
a4c92313 | 1179 | case VIK_UNITS_SPEED_KNOTS: |
ab1e0693 | 1180 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed)); |
a4c92313 | 1181 | break; |
13bdea80 RN |
1182 | default: |
1183 | g_snprintf (tmp_buf, sizeof(tmp_buf), "--" ); | |
1184 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
1185 | } | |
1186 | } | |
21700912 | 1187 | widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 1188 | |
6f9336aa RN |
1189 | switch (dist_units) { |
1190 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
1191 | // Even though kilometres, the average distance between points is going to be quite small so keep in metres | |
1192 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) ); | |
1193 | break; | |
1194 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 1195 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.3f miles", (tp_count - seg_count) == 0 ? 0 : VIK_METERS_TO_MILES(tr_len / ( tp_count - seg_count )) ); |
6f9336aa RN |
1196 | break; |
1197 | default: | |
1198 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
1199 | } | |
21700912 | 1200 | widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 1201 | |
6027a9c5 | 1202 | vik_units_height_t height_units = a_vik_get_units_height (); |
8c4f1350 | 1203 | if ( min_alt == VIK_DEFAULT_ALTITUDE ) |
4c77d5e0 | 1204 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
6027a9c5 RN |
1205 | else { |
1206 | switch (height_units) { | |
1207 | case VIK_UNITS_HEIGHT_METRES: | |
1208 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt ); | |
1209 | break; | |
1210 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 1211 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) ); |
6027a9c5 RN |
1212 | break; |
1213 | default: | |
1214 | g_snprintf(tmp_buf, sizeof(tmp_buf), "--" ); | |
1215 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
1216 | } | |
1217 | } | |
21700912 | 1218 | widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
1219 | |
1220 | vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt ); | |
bf35388d | 1221 | if ( min_alt == VIK_DEFAULT_ALTITUDE ) |
4c77d5e0 | 1222 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
6027a9c5 RN |
1223 | else { |
1224 | switch (height_units) { | |
1225 | case VIK_UNITS_HEIGHT_METRES: | |
1226 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt ); | |
1227 | break; | |
1228 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 1229 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) ); |
6027a9c5 RN |
1230 | break; |
1231 | default: | |
1232 | g_snprintf(tmp_buf, sizeof(tmp_buf), "--" ); | |
1233 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
1234 | } | |
1235 | } | |
21700912 | 1236 | widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf ); |
24d5c7e2 | 1237 | |
4b00e581 AF |
1238 | #if 0 |
1239 | #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0); | |
1240 | gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0); | |
1241 | PACK(l_len); | |
1242 | PACK(l_tps); | |
1243 | PACK(l_segs); | |
1244 | PACK(l_dups); | |
1245 | PACK(l_maxs); | |
1246 | PACK(l_avgs); | |
1247 | PACK(l_avgd); | |
1248 | PACK(l_elev); | |
1249 | PACK(l_galo); | |
1250 | #undef PACK; | |
1251 | #endif | |
24d5c7e2 | 1252 | |
8c4f1350 | 1253 | if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp ) |
f583082b AF |
1254 | { |
1255 | time_t t1, t2; | |
1256 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
1257 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
1258 | ||
1259 | strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf)); | |
a45242c2 | 1260 | tmp_buf[sizeof(tmp_buf)-1] = 0; |
32e48121 | 1261 | g_strchomp(tmp_buf); |
21700912 | 1262 | widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf); |
f583082b AF |
1263 | |
1264 | strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf)); | |
a45242c2 | 1265 | tmp_buf[sizeof(tmp_buf)-1] = 0; |
32e48121 | 1266 | g_strchomp(tmp_buf); |
21700912 | 1267 | widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf); |
f583082b | 1268 | |
4c77d5e0 | 1269 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60); |
21700912 | 1270 | widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf); |
8c4f1350 | 1271 | } else { |
4c77d5e0 GB |
1272 | widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data")); |
1273 | widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data")); | |
1274 | widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data")); | |
24d5c7e2 | 1275 | } |
4b00e581 AF |
1276 | |
1277 | table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE)); | |
1278 | gtk_table_set_col_spacing (table, 0, 10); | |
1279 | for (i=0; i<cnt; i++) { | |
1280 | GtkWidget *label; | |
1281 | ||
2f078f56 RN |
1282 | // Settings so the text positioning only moves around vertically when the dialog is resized |
1283 | // This also gives more room to see the track comment | |
4b00e581 | 1284 | label = gtk_label_new(NULL); |
2f078f56 | 1285 | gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane |
4c77d5e0 | 1286 | gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) ); |
2f078f56 | 1287 | gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_SHRINK, 0, 0 ); |
4b00e581 | 1288 | if (GTK_IS_MISC(content[i])) { |
2f078f56 | 1289 | gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0.5 ); |
4b00e581 AF |
1290 | } |
1291 | gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 ); | |
1292 | } | |
1293 | ||
954e061e | 1294 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics"))); |
50a14534 | 1295 | |
950d8a07 | 1296 | if ( widgets->elev_box ) { |
065f60f1 GB |
1297 | GtkWidget *page = NULL; |
1298 | widgets->w_cur_dist = gtk_label_new(_("No Data")); | |
780d3771 RN |
1299 | widgets->w_cur_elevation = gtk_label_new(_("No Data")); |
1300 | page = create_graph_page (widgets->elev_box, | |
1301 | _("<b>Track Distance:</b>"), widgets->w_cur_dist, | |
1302 | _("<b>Track Height:</b>"), widgets->w_cur_elevation); | |
065f60f1 GB |
1303 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance"))); |
1304 | } | |
c79f0206 | 1305 | |
950d8a07 | 1306 | if ( widgets->speed_box ) { |
065f60f1 GB |
1307 | GtkWidget *page = NULL; |
1308 | widgets->w_cur_time = gtk_label_new(_("No Data")); | |
780d3771 RN |
1309 | widgets->w_cur_speed = gtk_label_new(_("No Data")); |
1310 | page = create_graph_page (widgets->speed_box, | |
1311 | _("<b>Track Time:</b>"), widgets->w_cur_time, | |
1312 | _("<b>Track Speed:</b>"), widgets->w_cur_speed); | |
065f60f1 GB |
1313 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time"))); |
1314 | } | |
50a14534 | 1315 | |
25e44eac | 1316 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0); |
1d0135d8 | 1317 | |
8dcb3ba4 | 1318 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE); |
1d0135d8 | 1319 | if (seg_count <= 1) |
8dbfe7a3 | 1320 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE); |
1d0135d8 | 1321 | if (vik_track_get_dup_point_count(tr) <= 0) |
8dbfe7a3 | 1322 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE); |
1d0135d8 | 1323 | |
950d8a07 RN |
1324 | gpointer *pass_along; |
1325 | pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */ | |
1326 | pass_along[0] = tr; | |
1327 | pass_along[1] = vlp; | |
1328 | pass_along[2] = widgets; | |
1329 | ||
1330 | // On dialog realization configure_event casues the graphs to be initially drawn | |
1331 | widgets->configure_dialog = TRUE; | |
1332 | g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), pass_along); | |
1333 | ||
21700912 | 1334 | vik_track_set_property_dialog(tr, dialog); |
77b591fa | 1335 | gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT ); |
50a14534 | 1336 | gtk_widget_show_all ( dialog ); |
50a14534 | 1337 | } |