]>
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 | ||
50a14534 EB |
55 | #define PROFILE_WIDTH 600 |
56 | #define PROFILE_HEIGHT 300 | |
25e44eac | 57 | #define MIN_ALT_DIFF 100.0 |
c37f1cd6 | 58 | #define MIN_SPEED_DIFF 5.0 |
50a14534 | 59 | |
ca7e67ef QT |
60 | typedef struct _propsaved { |
61 | gboolean saved; | |
62 | gint pos; | |
63 | GdkImage *img; | |
64 | } PropSaved; | |
65 | ||
21700912 QT |
66 | typedef struct _propwidgets { |
67 | VikTrwLayer *vtl; | |
68 | VikTrack *tr; | |
69 | VikLayersPanel *vlp; | |
70 | gchar *track_name; | |
8dcb3ba4 | 71 | GtkWidget *dialog; |
21700912 QT |
72 | GtkWidget *w_comment; |
73 | GtkWidget *w_track_length; | |
74 | GtkWidget *w_tp_count; | |
75 | GtkWidget *w_segment_count; | |
76 | GtkWidget *w_duptp_count; | |
77 | GtkWidget *w_max_speed; | |
78 | GtkWidget *w_avg_speed; | |
79 | GtkWidget *w_avg_dist; | |
80 | GtkWidget *w_elev_range; | |
81 | GtkWidget *w_elev_gain; | |
82 | GtkWidget *w_time_start; | |
83 | GtkWidget *w_time_end; | |
84 | GtkWidget *w_time_dur; | |
065f60f1 GB |
85 | GtkWidget *w_cur_dist; /*< Current distance */ |
86 | GtkWidget *w_cur_time; /*< Current time */ | |
e60fc2c9 | 87 | gdouble track_length; |
ca7e67ef QT |
88 | PropSaved elev_graph_saved_img; |
89 | PropSaved speed_graph_saved_img; | |
e60fc2c9 QT |
90 | GtkWidget *elev_box; |
91 | GtkWidget *speed_box; | |
8dcb3ba4 | 92 | VikTrackpoint *marker_tp; |
21700912 QT |
93 | } PropWidgets; |
94 | ||
ca7e67ef QT |
95 | static PropWidgets *prop_widgets_new() |
96 | { | |
97 | PropWidgets *widgets = g_malloc0(sizeof(PropWidgets)); | |
98 | ||
99 | return widgets; | |
100 | } | |
101 | ||
102 | static void prop_widgets_free(PropWidgets *widgets) | |
103 | { | |
104 | ||
105 | if (widgets->elev_graph_saved_img.img) | |
106 | g_object_unref(widgets->elev_graph_saved_img.img); | |
107 | if (widgets->speed_graph_saved_img.img) | |
108 | g_object_unref(widgets->speed_graph_saved_img.img); | |
109 | g_free(widgets); | |
110 | } | |
111 | ||
50a14534 EB |
112 | static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max) |
113 | { | |
114 | *max = -1000; | |
115 | *min = 20000; | |
116 | guint i; | |
117 | for ( i=0; i < PROFILE_WIDTH; i++ ) { | |
118 | if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) { | |
119 | if ( altitudes[i] > *max ) | |
120 | *max = altitudes[i]; | |
121 | if ( altitudes[i] < *min ) | |
122 | *min = altitudes[i]; | |
123 | } | |
124 | } | |
50a14534 EB |
125 | } |
126 | ||
d03d80e6 | 127 | #define MARGIN 70 |
25e44eac | 128 | #define LINES 5 |
e60fc2c9 | 129 | static VikTrackpoint *set_center_at_graph_position(gdouble event_x, gint img_width, VikLayersPanel *vlp, VikTrack *tr, gboolean time_base) |
24d5c7e2 | 130 | { |
32e48121 QT |
131 | VikTrackpoint *trackpoint; |
132 | gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2; | |
e1e2f2c6 JJ |
133 | if (x < 0) |
134 | x = 0; | |
135 | if (x > PROFILE_WIDTH) | |
136 | x = PROFILE_WIDTH; | |
32e48121 QT |
137 | |
138 | if (time_base) | |
ddc2372e | 139 | trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL ); |
32e48121 | 140 | else |
ddc2372e | 141 | trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL ); |
32e48121 | 142 | |
e1e2f2c6 JJ |
143 | if ( trackpoint ) { |
144 | VikCoord coord = trackpoint->coord; | |
e1e2f2c6 | 145 | vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord ); |
24d5c7e2 | 146 | vik_layers_panel_emit_update ( vlp ); |
e1e2f2c6 | 147 | } |
e60fc2c9 | 148 | return trackpoint; |
e1e2f2c6 | 149 | } |
ca7e67ef | 150 | |
9dc30292 | 151 | static void draw_graph_mark(GtkWidget *image, gdouble event_x, gint img_width, GdkGC *gc, PropSaved *saved_img) |
32e48121 | 152 | { |
d973878f | 153 | GdkPixmap *pix = NULL; |
9dc30292 QT |
154 | /* the pixmap = margin + graph area */ |
155 | gdouble x = event_x - img_width/2 + PROFILE_WIDTH/2 + MARGIN/2; | |
156 | ||
e60fc2c9 | 157 | // fprintf(stderr, "event_x=%f img_width=%d x=%f\n", event_x, img_width, x); |
ca7e67ef QT |
158 | |
159 | gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL); | |
d973878f | 160 | /* Restore previously saved image */ |
ca7e67ef QT |
161 | if (saved_img->saved) { |
162 | gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, | |
163 | saved_img->pos, 0, -1, -1); | |
164 | saved_img->saved = FALSE; | |
9dc30292 QT |
165 | gtk_widget_queue_draw_area(image, |
166 | saved_img->pos + img_width/2 - PROFILE_WIDTH/2 - MARGIN/2, 0, | |
ca7e67ef QT |
167 | saved_img->img->width, saved_img->img->height); |
168 | } | |
169 | if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) { | |
d973878f | 170 | /* Save part of the image */ |
ca7e67ef QT |
171 | if (saved_img->img) |
172 | gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, | |
d973878f | 173 | x, 0, 0, 0, saved_img->img->width, saved_img->img->height); |
ca7e67ef QT |
174 | else |
175 | saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), | |
d973878f GB |
176 | saved_img->img, x, 0, 0, 0, 1, PROFILE_HEIGHT); |
177 | saved_img->pos = x; | |
ca7e67ef QT |
178 | saved_img->saved = TRUE; |
179 | gdk_draw_line (GDK_DRAWABLE(pix), gc, x, 0, x, image->allocation.height); | |
180 | /* redraw the area which contains the line, saved_width is just convenient */ | |
d973878f | 181 | gtk_widget_queue_draw_area(image, event_x, 0, 1, PROFILE_HEIGHT); |
ca7e67ef | 182 | } |
32e48121 QT |
183 | } |
184 | ||
ca7e67ef | 185 | static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph ) |
32e48121 QT |
186 | { |
187 | VikTrack *tr = pass_along[0]; | |
188 | VikLayersPanel *vlp = pass_along[1]; | |
ca7e67ef QT |
189 | PropWidgets *widgets = pass_along[2]; |
190 | GList *child = gtk_container_get_children(GTK_CONTAINER(event_box)); | |
191 | GtkWidget *image = GTK_WIDGET(child->data); | |
192 | GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box)); | |
193 | ||
194 | ||
e60fc2c9 | 195 | VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, vlp, tr, is_vt_graph); |
9dc30292 | 196 | draw_graph_mark(image, event->x, event_box->allocation.width, window->style->black_gc, |
ca7e67ef QT |
197 | is_vt_graph ? &widgets->speed_graph_saved_img : &widgets->elev_graph_saved_img); |
198 | g_list_free(child); | |
8dcb3ba4 QT |
199 | widgets->marker_tp = trackpoint; |
200 | gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, TRUE); | |
ca7e67ef | 201 | |
e60fc2c9 QT |
202 | /* draw on the other graph */ |
203 | if (trackpoint == NULL || widgets->elev_box == NULL || widgets->speed_box == NULL) | |
204 | /* This test assumes we have only 2 graphs */ | |
205 | return; | |
206 | ||
207 | gdouble pc = NAN; | |
208 | gdouble x2; | |
209 | GList *other_child = gtk_container_get_children(GTK_CONTAINER( | |
210 | is_vt_graph ? widgets->elev_box : widgets->speed_box)); | |
211 | GtkWidget *other_image = GTK_WIDGET(other_child->data); | |
212 | if (is_vt_graph) { | |
213 | gdouble dist = 0.0; | |
214 | GList *iter; | |
215 | for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) { | |
216 | dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord), | |
217 | &(VIK_TRACKPOINT(iter->prev->data)->coord)); | |
218 | /* Assuming trackpoint is not a copy */ | |
219 | if (trackpoint == VIK_TRACKPOINT(iter->data)) | |
220 | break; | |
221 | } | |
222 | if (iter != NULL) | |
223 | pc = dist/widgets->track_length; | |
224 | } else { | |
225 | time_t t_start, t_end, t_total; | |
226 | t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
227 | t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
228 | t_total = t_end - t_start; | |
229 | pc = (gdouble)(trackpoint->timestamp - t_start)/t_total; | |
230 | } | |
231 | if (!isnan(pc)) { | |
232 | x2 = pc * PROFILE_WIDTH + MARGIN + (event_box->allocation.width/2 - PROFILE_WIDTH/2 - MARGIN/2); | |
233 | draw_graph_mark(other_image, x2, event_box->allocation.width, window->style->black_gc, | |
234 | is_vt_graph ? &widgets->elev_graph_saved_img : &widgets->speed_graph_saved_img); | |
235 | } | |
236 | ||
237 | g_list_free(other_child); | |
238 | ||
ca7e67ef QT |
239 | } |
240 | ||
241 | static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along ) | |
242 | { | |
243 | track_graph_click(event_box, event, pass_along, FALSE); | |
244 | return TRUE; /* don't call other (further) callbacks */ | |
245 | } | |
246 | ||
247 | static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along ) | |
248 | { | |
249 | track_graph_click(event_box, event, pass_along, TRUE); | |
250 | return TRUE; /* don't call other (further) callbacks */ | |
32e48121 QT |
251 | } |
252 | ||
e1e2f2c6 JJ |
253 | void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along ) |
254 | { | |
255 | VikTrack *tr = pass_along[0]; | |
21700912 | 256 | PropWidgets *widgets = pass_along[2]; |
e1e2f2c6 JJ |
257 | int mouse_x, mouse_y; |
258 | GdkModifierType state; | |
259 | ||
260 | if (event->is_hint) | |
261 | gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state); | |
262 | else | |
263 | mouse_x = event->x; | |
264 | ||
265 | gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2; | |
266 | if (x < 0) | |
267 | x = 0; | |
268 | if (x > PROFILE_WIDTH) | |
269 | x = PROFILE_WIDTH; | |
270 | ||
ddc2372e QT |
271 | gdouble meters_from_start; |
272 | VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, &meters_from_start ); | |
065f60f1 | 273 | if (trackpoint && widgets->w_cur_dist) { |
ddc2372e | 274 | static gchar tmp_buf[20]; |
6f9336aa RN |
275 | vik_units_distance_t dist_units = a_vik_get_units_distance (); |
276 | switch (dist_units) { | |
277 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
278 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0); | |
279 | break; | |
280 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 281 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) ); |
6f9336aa RN |
282 | break; |
283 | default: | |
284 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
285 | } | |
065f60f1 | 286 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf); |
ddc2372e QT |
287 | } |
288 | } | |
289 | ||
290 | void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along ) | |
291 | { | |
292 | VikTrack *tr = pass_along[0]; | |
21700912 | 293 | PropWidgets *widgets = pass_along[2]; |
ddc2372e QT |
294 | int mouse_x, mouse_y; |
295 | GdkModifierType state; | |
296 | ||
297 | if (event->is_hint) | |
298 | gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state); | |
299 | else | |
300 | mouse_x = event->x; | |
301 | ||
302 | gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2; | |
303 | if (x < 0) | |
304 | x = 0; | |
305 | if (x > PROFILE_WIDTH) | |
306 | x = PROFILE_WIDTH; | |
307 | ||
308 | time_t seconds_from_start; | |
309 | VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, &seconds_from_start ); | |
065f60f1 | 310 | if (trackpoint && widgets->w_cur_time) { |
ddc2372e QT |
311 | static gchar tmp_buf[20]; |
312 | guint h, m, s; | |
313 | h = seconds_from_start/3600; | |
314 | m = (seconds_from_start - h*3600)/60; | |
315 | s = seconds_from_start - (3600*h) - (60*m); | |
316 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s); | |
e1e2f2c6 | 317 | |
065f60f1 | 318 | gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf); |
24d5c7e2 EB |
319 | } |
320 | } | |
321 | ||
b42009f6 | 322 | 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 |
323 | { |
324 | GList *iter; | |
325 | gdouble dist = 0; | |
326 | gdouble max_speed = 0; | |
327 | gdouble total_length = vik_track_get_length_including_gaps(tr); | |
328 | ||
329 | for (iter = tr->trackpoints->next; iter; iter = iter->next) { | |
330 | if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) | |
331 | max_speed = MAX(max_speed, VIK_TRACKPOINT(iter->data)->speed); | |
332 | } | |
3220e336 | 333 | max_speed = max_speed * 110 / 100; |
07596bf4 QT |
334 | |
335 | for (iter = tr->trackpoints->next; iter; iter = iter->next) { | |
336 | int x, y_alt, y_speed; | |
228213c5 | 337 | gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST); |
b42009f6 | 338 | elev -= alt_offset; |
07596bf4 QT |
339 | dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), |
340 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
341 | x = (width * dist)/total_length + margin; | |
342 | if ( elev != VIK_DEM_INVALID_ELEVATION ) { | |
2c50b28a | 343 | y_alt = height - ((height * elev)/alt_diff); |
07596bf4 QT |
344 | gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4); |
345 | } | |
346 | if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) { | |
347 | y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed; | |
348 | gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4); | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
21700912 | 353 | GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt) |
50a14534 | 354 | { |
c79f0206 EB |
355 | GdkPixmap *pix; |
356 | GtkWidget *image; | |
50a14534 | 357 | gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH ); |
25e44eac | 358 | gdouble mina, maxa; |
561e6ad0 | 359 | GtkWidget *eventbox; |
24d5c7e2 | 360 | gpointer *pass_along; |
50a14534 EB |
361 | guint i; |
362 | ||
8c4f1350 EB |
363 | if ( altitudes == NULL ) { |
364 | *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE; | |
c79f0206 | 365 | return NULL; |
8c4f1350 | 366 | } |
c79f0206 EB |
367 | |
368 | pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 ); | |
369 | image = gtk_image_new_from_pixmap ( pix, NULL ); | |
370 | ||
50a14534 | 371 | GdkGC *no_alt_info = gdk_gc_new ( window->window ); |
07596bf4 QT |
372 | GdkGC *dem_alt_gc = gdk_gc_new ( window->window ); |
373 | GdkGC *gps_speed_gc = gdk_gc_new ( window->window ); | |
50a14534 EB |
374 | GdkColor color; |
375 | ||
07596bf4 | 376 | gdk_color_parse ( "yellow", &color ); |
50a14534 | 377 | gdk_gc_set_rgb_fg_color ( no_alt_info, &color); |
07596bf4 QT |
378 | gdk_color_parse ( "green", &color ); |
379 | gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color); | |
380 | gdk_color_parse ( "red", &color ); | |
381 | gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color); | |
50a14534 | 382 | |
25e44eac | 383 | |
50a14534 | 384 | minmax_alt(altitudes, min_alt, max_alt); |
3220e336 | 385 | mina = *min_alt; |
2c50b28a | 386 | maxa = *max_alt + ((*max_alt - *min_alt) * 0.25); |
25e44eac AF |
387 | |
388 | /* clear the image */ | |
389 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], | |
390 | TRUE, 0, 0, MARGIN, PROFILE_HEIGHT); | |
391 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], | |
392 | TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT); | |
393 | ||
25e44eac | 394 | /* draw grid */ |
d03d80e6 | 395 | #define LABEL_FONT "Sans 7" |
6027a9c5 | 396 | vik_units_height_t height_units = a_vik_get_units_height (); |
25e44eac AF |
397 | for (i=0; i<=LINES; i++) { |
398 | PangoFontDescription *pfd; | |
399 | PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL); | |
400 | gchar s[32]; | |
d03d80e6 | 401 | int w, h; |
25e44eac | 402 | |
d03d80e6 | 403 | pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT); |
25e44eac AF |
404 | pfd = pango_font_description_from_string (LABEL_FONT); |
405 | pango_layout_set_font_description (pl, pfd); | |
406 | pango_font_description_free (pfd); | |
6027a9c5 RN |
407 | switch (height_units) { |
408 | case VIK_UNITS_HEIGHT_METRES: | |
409 | sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES)); | |
410 | break; | |
411 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 412 | sprintf(s, "%8dft", (int)VIK_METERS_TO_FEET(mina + (LINES-i)*(maxa-mina)/LINES)); |
6027a9c5 RN |
413 | break; |
414 | default: | |
415 | sprintf(s, "--"); | |
416 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
417 | } | |
25e44eac | 418 | pango_layout_set_text(pl, s, -1); |
d03d80e6 AF |
419 | pango_layout_get_pixel_size (pl, &w, &h); |
420 | gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, | |
421 | CLAMP((int)i*PROFILE_HEIGHT/LINES - h/2, 0, PROFILE_HEIGHT-h), pl); | |
25e44eac AF |
422 | |
423 | gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], | |
424 | MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i); | |
6278ccc6 | 425 | g_object_unref ( G_OBJECT ( pl ) ); |
e6869c62 | 426 | pl = NULL; |
25e44eac AF |
427 | } |
428 | ||
429 | /* draw elevations */ | |
50a14534 | 430 | for ( i = 0; i < PROFILE_WIDTH; i++ ) |
bb71de8b | 431 | if ( altitudes[i] == VIK_DEFAULT_ALTITUDE ) |
25e44eac AF |
432 | gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, |
433 | i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT ); | |
bb71de8b | 434 | else |
25e44eac AF |
435 | gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], |
436 | i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) ); | |
734652bf | 437 | |
b42009f6 | 438 | draw_dem_alt_speed_dist(tr, GDK_DRAWABLE(pix), dem_alt_gc, gps_speed_gc, mina, maxa - mina, PROFILE_WIDTH, PROFILE_HEIGHT, MARGIN); |
07596bf4 | 439 | |
25e44eac AF |
440 | /* draw border */ |
441 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1); | |
442 | ||
24d5c7e2 EB |
443 | |
444 | ||
50a14534 EB |
445 | g_object_unref ( G_OBJECT(pix) ); |
446 | g_free ( altitudes ); | |
447 | g_object_unref ( G_OBJECT(no_alt_info) ); | |
07596bf4 QT |
448 | g_object_unref ( G_OBJECT(dem_alt_gc) ); |
449 | g_object_unref ( G_OBJECT(gps_speed_gc) ); | |
24d5c7e2 | 450 | |
21700912 | 451 | pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */ |
24d5c7e2 EB |
452 | pass_along[0] = tr; |
453 | pass_along[1] = vlp; | |
21700912 | 454 | pass_along[2] = widgets; |
24d5c7e2 EB |
455 | |
456 | eventbox = gtk_event_box_new (); | |
457 | g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along ); | |
e1e2f2c6 | 458 | g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along ); |
24d5c7e2 EB |
459 | g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along ); |
460 | gtk_container_add ( GTK_CONTAINER(eventbox), image ); | |
e1e2f2c6 JJ |
461 | gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK |
462 | | GDK_POINTER_MOTION_MASK | |
463 | | GDK_POINTER_MOTION_HINT_MASK); | |
24d5c7e2 EB |
464 | |
465 | return eventbox; | |
50a14534 | 466 | } |
24d5c7e2 | 467 | |
25e44eac | 468 | |
21700912 | 469 | GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets) |
25e44eac | 470 | { |
c79f0206 EB |
471 | GdkPixmap *pix; |
472 | GtkWidget *image; | |
25e44eac | 473 | gdouble mins, maxs; |
25e44eac | 474 | guint i; |
561e6ad0 EB |
475 | GtkWidget *eventbox; |
476 | gpointer *pass_along; | |
25e44eac | 477 | |
21700912 | 478 | pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */ |
561e6ad0 EB |
479 | pass_along[0] = tr; |
480 | pass_along[1] = vlp; | |
21700912 | 481 | pass_along[2] = widgets; |
c79f0206 EB |
482 | |
483 | gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH ); | |
3b36279c JJ |
484 | if ( speeds == NULL ) { |
485 | g_free(pass_along); | |
c79f0206 | 486 | return NULL; |
3b36279c | 487 | } |
c79f0206 EB |
488 | |
489 | pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 ); | |
490 | image = gtk_image_new_from_pixmap ( pix, NULL ); | |
491 | ||
25e44eac | 492 | minmax_alt(speeds, &mins, &maxs); |
2c50b28a QT |
493 | if (mins < 0.0) |
494 | mins = 0; /* splines sometimes give negative speeds */ | |
495 | maxs = maxs + ((maxs - mins) * 0.1); | |
25e44eac AF |
496 | if (maxs-mins < MIN_SPEED_DIFF) { |
497 | maxs = mins + MIN_SPEED_DIFF; | |
498 | } | |
499 | ||
500 | /* clear the image */ | |
501 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], | |
502 | TRUE, 0, 0, MARGIN, PROFILE_HEIGHT); | |
503 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], | |
504 | TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT); | |
505 | ||
506 | #if 0 | |
507 | /* XXX this can go out, it's just a helpful dev tool */ | |
508 | { | |
509 | int j; | |
510 | GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc, | |
511 | window->style->light_gc, | |
512 | window->style->dark_gc, window->style->mid_gc, | |
513 | window->style->text_gc, window->style->base_gc, | |
514 | window->style->text_aa_gc }; | |
515 | for (i=0; i<5; i++) { | |
516 | for (j=0; j<8; j++) { | |
517 | gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i], | |
518 | TRUE, i*20, j*20, 20, 20); | |
519 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, | |
520 | FALSE, i*20, j*20, 20, 20); | |
521 | } | |
522 | } | |
523 | } | |
524 | #else | |
525 | ||
526 | /* draw grid */ | |
d03d80e6 | 527 | #define LABEL_FONT "Sans 7" |
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); |
25e44eac AF |
535 | pfd = pango_font_description_from_string (LABEL_FONT); |
536 | pango_layout_set_font_description (pl, pfd); | |
537 | pango_font_description_free (pfd); | |
13bdea80 RN |
538 | vik_units_speed_t speed_units = a_vik_get_units_speed (); |
539 | switch (speed_units) { | |
540 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
7972f8ff | 541 | sprintf(s, "%6.1fkm/h", VIK_MPS_TO_KPH(mins + (LINES-i)*(maxs-mins)/LINES)); |
13bdea80 RN |
542 | break; |
543 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
7972f8ff | 544 | sprintf(s, "%6.1fmph", VIK_MPS_TO_MPH(mins + (LINES-i)*(maxs-mins)/LINES)); |
13bdea80 RN |
545 | break; |
546 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
547 | sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*(maxs-mins)/LINES)); | |
548 | break; | |
a4c92313 | 549 | case VIK_UNITS_SPEED_KNOTS: |
7972f8ff | 550 | sprintf(s, "%6.1fknots", VIK_MPS_TO_KNOTS(mins + (LINES-i)*(maxs-mins)/LINES)); |
a4c92313 | 551 | break; |
13bdea80 RN |
552 | default: |
553 | sprintf(s, "--"); | |
554 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
555 | } | |
556 | ||
25e44eac | 557 | pango_layout_set_text(pl, s, -1); |
d03d80e6 AF |
558 | pango_layout_get_pixel_size (pl, &w, &h); |
559 | gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, | |
560 | CLAMP((int)i*PROFILE_HEIGHT/LINES - h/2, 0, PROFILE_HEIGHT-h), pl); | |
25e44eac AF |
561 | |
562 | gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], | |
563 | MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i); | |
6278ccc6 | 564 | g_object_unref ( G_OBJECT ( pl ) ); |
e6869c62 | 565 | pl = NULL; |
25e44eac | 566 | } |
d03d80e6 | 567 | |
25e44eac AF |
568 | |
569 | /* draw speeds */ | |
570 | for ( i = 0; i < PROFILE_WIDTH; i++ ) | |
571 | gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], | |
572 | i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) ); | |
573 | #endif | |
07596bf4 QT |
574 | |
575 | ||
576 | GdkGC *gps_speed_gc = gdk_gc_new ( window->window ); | |
577 | GdkColor color; | |
578 | ||
579 | gdk_color_parse ( "red", &color ); | |
580 | gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color); | |
581 | ||
582 | time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
583 | time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time; | |
584 | GList *iter; | |
585 | for (iter = tr->trackpoints; iter; iter = iter->next) { | |
586 | gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed; | |
587 | if (isnan(gps_speed)) | |
588 | continue; | |
589 | int x = MARGIN + PROFILE_WIDTH * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur; | |
13bdea80 | 590 | int y = PROFILE_HEIGHT - PROFILE_HEIGHT*(gps_speed - mins)/(maxs - mins); |
07596bf4 QT |
591 | gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4); |
592 | } | |
593 | ||
25e44eac AF |
594 | /* draw border */ |
595 | gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1); | |
596 | ||
597 | g_object_unref ( G_OBJECT(pix) ); | |
07596bf4 | 598 | g_object_unref ( G_OBJECT(gps_speed_gc) ); |
25e44eac | 599 | g_free ( speeds ); |
561e6ad0 EB |
600 | |
601 | eventbox = gtk_event_box_new (); | |
32e48121 | 602 | g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along ); |
ddc2372e | 603 | g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along ); |
561e6ad0 EB |
604 | g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along ); |
605 | gtk_container_add ( GTK_CONTAINER(eventbox), image ); | |
e1e2f2c6 JJ |
606 | gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK |
607 | | GDK_POINTER_MOTION_MASK | |
608 | | GDK_POINTER_MOTION_HINT_MASK); | |
561e6ad0 EB |
609 | |
610 | return eventbox; | |
25e44eac AF |
611 | } |
612 | #undef MARGIN | |
613 | #undef LINES | |
50a14534 | 614 | |
42f34743 | 615 | static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets) |
50a14534 | 616 | { |
21700912 QT |
617 | VikTrack *tr = widgets->tr; |
618 | VikTrwLayer *vtl = widgets->vtl; | |
8dcb3ba4 | 619 | gboolean keep_dialog = FALSE; |
21700912 QT |
620 | |
621 | /* FIXME: check and make sure the track still exists before doing anything to it */ | |
622 | /* Note: destroying diaglog (eg, parent window exit) won't give "response" */ | |
623 | switch (resp) { | |
624 | case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */ | |
625 | case GTK_RESPONSE_REJECT: | |
626 | break; | |
627 | case GTK_RESPONSE_ACCEPT: | |
628 | vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment))); | |
629 | break; | |
630 | case VIK_TRW_LAYER_PROPWIN_REVERSE: | |
631 | vik_track_reverse(tr); | |
632 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
633 | break; | |
634 | case VIK_TRW_LAYER_PROPWIN_DEL_DUP: | |
635 | vik_track_remove_dup_points(tr); | |
636 | /* above operation could have deleted current_tp or last_tp */ | |
637 | trw_layer_cancel_tps_of_track ( vtl, widgets->track_name ); | |
638 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
639 | break; | |
640 | case VIK_TRW_LAYER_PROPWIN_SPLIT: | |
641 | { | |
642 | /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */ | |
643 | guint ntracks; | |
644 | VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks); | |
645 | gchar *new_tr_name; | |
646 | guint i; | |
647 | for ( i = 0; i < ntracks; i++ ) | |
648 | { | |
649 | g_assert ( tracks[i] ); | |
650 | new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1); | |
651 | /* if ( (wp_exists) && (! overwrite) ) */ | |
652 | /* don't need to upper case new_tr_name because old tr name was uppercase */ | |
653 | if ( vik_trw_layer_get_track(vtl, new_tr_name ) && | |
d91e5f2b | 654 | ( ! 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 | 655 | { |
e13ab673 | 656 | gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL ); |
21700912 QT |
657 | g_free ( new_tr_name ); |
658 | if (new_new_tr_name) | |
659 | new_tr_name = new_new_tr_name; | |
660 | else | |
661 | { | |
662 | new_tr_name = NULL; | |
663 | vik_track_free ( tracks[i] ); | |
664 | } | |
665 | } | |
666 | if ( new_tr_name ) | |
667 | vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] ); | |
668 | } | |
669 | if ( tracks ) | |
670 | { | |
671 | g_free ( tracks ); | |
672 | /* Don't let track destroy this dialog */ | |
673 | vik_track_clear_property_dialog(tr); | |
674 | vik_trw_layer_delete_track ( vtl, widgets->track_name ); | |
675 | vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */ | |
676 | } | |
677 | } | |
678 | break; | |
8dcb3ba4 QT |
679 | case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER: |
680 | { | |
681 | GList *iter = tr->trackpoints; | |
682 | while ((iter = iter->next)) { | |
683 | if (widgets->marker_tp == VIK_TRACKPOINT(iter->data)) | |
684 | break; | |
685 | } | |
686 | if (iter == NULL) { | |
687 | a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR, | |
688 | _("Failed spliting track. Track unchanged"), NULL); | |
689 | keep_dialog = TRUE; | |
690 | break; | |
691 | } | |
692 | ||
693 | gchar *r_name = g_strdup_printf("%s #R", widgets->track_name); | |
694 | if (vik_trw_layer_get_track(vtl, r_name ) && | |
d91e5f2b | 695 | ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl), |
8dcb3ba4 QT |
696 | "The track \"%s\" exists, do you wish to overwrite it?", r_name))) |
697 | { | |
e13ab673 | 698 | gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL ); |
8dcb3ba4 QT |
699 | if (new_r_name) { |
700 | g_free( r_name ); | |
701 | r_name = new_r_name; | |
702 | } | |
703 | else { | |
704 | a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING, | |
705 | _("Operation Aborted. Track unchanged"), NULL); | |
706 | keep_dialog = TRUE; | |
707 | break; | |
708 | } | |
709 | } | |
710 | iter->prev->next = NULL; | |
711 | iter->prev = NULL; | |
712 | VikTrack *tr_right = vik_track_new(); | |
713 | if ( tr->comment ) | |
714 | vik_track_set_comment ( tr_right, tr->comment ); | |
715 | tr_right->visible = tr->visible; | |
716 | tr_right->trackpoints = iter; | |
717 | ||
718 | vik_trw_layer_add_track(vtl, r_name, tr_right); | |
719 | vik_layer_emit_update ( VIK_LAYER(vtl) ); | |
720 | } | |
721 | break; | |
21700912 QT |
722 | default: |
723 | fprintf(stderr, "DEBUG: unknown response\n"); | |
724 | return; | |
725 | } | |
726 | ||
727 | /* Keep same behaviour for now: destroy dialog if click on any button */ | |
8dcb3ba4 QT |
728 | if (!keep_dialog) { |
729 | prop_widgets_free(widgets); | |
730 | vik_track_clear_property_dialog(tr); | |
731 | gtk_widget_destroy ( GTK_WIDGET(dialog) ); | |
732 | } | |
21700912 QT |
733 | } |
734 | ||
065f60f1 GB |
735 | static GtkWidget *create_graph_page ( GtkWidget *graph, |
736 | const gchar *markup, | |
737 | GtkWidget *value) | |
738 | { | |
739 | GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 ); | |
740 | GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 ); | |
741 | GtkWidget *label = gtk_label_new (NULL); | |
742 | gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0); | |
743 | gtk_label_set_markup ( GTK_LABEL(label), markup ); | |
744 | gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0); | |
745 | gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0); | |
746 | gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0); | |
747 | ||
748 | return vbox; | |
749 | } | |
750 | ||
21700912 QT |
751 | void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name ) |
752 | { | |
753 | /* FIXME: free widgets when destroy signal received */ | |
ca7e67ef | 754 | PropWidgets *widgets = prop_widgets_new(); |
21700912 QT |
755 | widgets->vtl = vtl; |
756 | widgets->tr = tr; | |
757 | widgets->vlp = vlp; | |
758 | widgets->track_name = track_name; | |
4c77d5e0 | 759 | gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name); |
dc27aba1 | 760 | GtkWidget *dialog = gtk_dialog_new_with_buttons (title, |
21700912 QT |
761 | parent, |
762 | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, | |
763 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | |
8dcb3ba4 | 764 | _("Split at Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, |
7760b0cf JJ |
765 | _("Split Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT, |
766 | _("Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE, | |
767 | _("Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP, | |
21700912 QT |
768 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
769 | NULL); | |
8dcb3ba4 | 770 | widgets->dialog = dialog; |
dc27aba1 | 771 | g_free(title); |
42f34743 | 772 | g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets); |
21700912 | 773 | //fprintf(stderr, "DEBUG: dialog=0x%p\n", dialog); |
4b00e581 | 774 | GtkTable *table; |
50a14534 EB |
775 | gdouble tr_len; |
776 | guint32 tp_count, seg_count; | |
50a14534 EB |
777 | |
778 | gdouble min_alt, max_alt; | |
21700912 QT |
779 | GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr, vlp, widgets, &min_alt,&max_alt); |
780 | GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, widgets); | |
25e44eac | 781 | GtkWidget *graphs = gtk_notebook_new(); |
50a14534 | 782 | |
e60fc2c9 QT |
783 | widgets->elev_box = profile; |
784 | widgets->speed_box = vtdiag; | |
785 | ||
4b00e581 AF |
786 | GtkWidget *content[20]; |
787 | int cnt; | |
788 | int i; | |
789 | ||
065f60f1 | 790 | 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 | 791 | static gchar tmp_buf[50]; |
8c4f1350 | 792 | gdouble tmp_speed; |
50a14534 | 793 | |
4b00e581 | 794 | cnt = 0; |
21700912 | 795 | widgets->w_comment = gtk_entry_new (); |
50a14534 | 796 | if ( tr->comment ) |
21700912 QT |
797 | gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment ); |
798 | g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) ); | |
799 | content[cnt++] = widgets->w_comment; | |
50a14534 | 800 | |
6f9336aa RN |
801 | vik_units_distance_t dist_units = a_vik_get_units_distance (); |
802 | ||
e60fc2c9 | 803 | tr_len = widgets->track_length = vik_track_get_length(tr); |
6f9336aa RN |
804 | switch (dist_units) { |
805 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
806 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 ); | |
807 | break; | |
808 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 809 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) ); |
6f9336aa RN |
810 | break; |
811 | default: | |
812 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
813 | } | |
21700912 | 814 | widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
815 | |
816 | tp_count = vik_track_get_tp_count(tr); | |
817 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count ); | |
21700912 | 818 | widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
819 | |
820 | seg_count = vik_track_get_segment_count(tr) ; | |
821 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count ); | |
21700912 | 822 | widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
823 | |
824 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) ); | |
21700912 | 825 | widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 826 | |
13bdea80 | 827 | vik_units_speed_t speed_units = a_vik_get_units_speed (); |
8c4f1350 EB |
828 | tmp_speed = vik_track_get_max_speed(tr); |
829 | if ( tmp_speed == 0 ) | |
4c77d5e0 | 830 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
13bdea80 RN |
831 | else { |
832 | switch (speed_units) { | |
833 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
ab1e0693 | 834 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed)); |
13bdea80 RN |
835 | break; |
836 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
ab1e0693 | 837 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed)); |
13bdea80 RN |
838 | break; |
839 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
840 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed ); | |
841 | break; | |
a4c92313 | 842 | case VIK_UNITS_SPEED_KNOTS: |
ab1e0693 | 843 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed)); |
a4c92313 | 844 | break; |
13bdea80 RN |
845 | default: |
846 | g_snprintf (tmp_buf, sizeof(tmp_buf), "--" ); | |
847 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
848 | } | |
849 | } | |
21700912 | 850 | widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 851 | |
8c4f1350 EB |
852 | tmp_speed = vik_track_get_average_speed(tr); |
853 | if ( tmp_speed == 0 ) | |
4c77d5e0 | 854 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
13bdea80 RN |
855 | else { |
856 | switch (speed_units) { | |
857 | case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR: | |
ab1e0693 | 858 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed)); |
13bdea80 RN |
859 | break; |
860 | case VIK_UNITS_SPEED_MILES_PER_HOUR: | |
ab1e0693 | 861 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed)); |
13bdea80 RN |
862 | break; |
863 | case VIK_UNITS_SPEED_METRES_PER_SECOND: | |
864 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed ); | |
865 | break; | |
a4c92313 | 866 | case VIK_UNITS_SPEED_KNOTS: |
ab1e0693 | 867 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed)); |
a4c92313 | 868 | break; |
13bdea80 RN |
869 | default: |
870 | g_snprintf (tmp_buf, sizeof(tmp_buf), "--" ); | |
871 | g_critical("Houston, we've had a problem. speed=%d", speed_units); | |
872 | } | |
873 | } | |
21700912 | 874 | widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 875 | |
6f9336aa RN |
876 | switch (dist_units) { |
877 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
878 | // Even though kilometres, the average distance between points is going to be quite small so keep in metres | |
879 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) ); | |
880 | break; | |
881 | case VIK_UNITS_DISTANCE_MILES: | |
433b3f7f | 882 | 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 |
883 | break; |
884 | default: | |
885 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
886 | } | |
21700912 | 887 | widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 | 888 | |
6027a9c5 | 889 | vik_units_height_t height_units = a_vik_get_units_height (); |
8c4f1350 | 890 | if ( min_alt == VIK_DEFAULT_ALTITUDE ) |
4c77d5e0 | 891 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
6027a9c5 RN |
892 | else { |
893 | switch (height_units) { | |
894 | case VIK_UNITS_HEIGHT_METRES: | |
895 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt ); | |
896 | break; | |
897 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 898 | 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 |
899 | break; |
900 | default: | |
901 | g_snprintf(tmp_buf, sizeof(tmp_buf), "--" ); | |
902 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
903 | } | |
904 | } | |
21700912 | 905 | widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf ); |
50a14534 EB |
906 | |
907 | vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt ); | |
bf35388d | 908 | if ( min_alt == VIK_DEFAULT_ALTITUDE ) |
4c77d5e0 | 909 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data")); |
6027a9c5 RN |
910 | else { |
911 | switch (height_units) { | |
912 | case VIK_UNITS_HEIGHT_METRES: | |
913 | g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt ); | |
914 | break; | |
915 | case VIK_UNITS_HEIGHT_FEET: | |
6c20e59a | 916 | 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 |
917 | break; |
918 | default: | |
919 | g_snprintf(tmp_buf, sizeof(tmp_buf), "--" ); | |
920 | g_critical("Houston, we've had a problem. height=%d", height_units); | |
921 | } | |
922 | } | |
21700912 | 923 | widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf ); |
24d5c7e2 | 924 | |
4b00e581 AF |
925 | #if 0 |
926 | #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0); | |
927 | gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0); | |
928 | PACK(l_len); | |
929 | PACK(l_tps); | |
930 | PACK(l_segs); | |
931 | PACK(l_dups); | |
932 | PACK(l_maxs); | |
933 | PACK(l_avgs); | |
934 | PACK(l_avgd); | |
935 | PACK(l_elev); | |
936 | PACK(l_galo); | |
937 | #undef PACK; | |
938 | #endif | |
24d5c7e2 | 939 | |
8c4f1350 | 940 | if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp ) |
f583082b AF |
941 | { |
942 | time_t t1, t2; | |
943 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
944 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
945 | ||
946 | strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf)); | |
a45242c2 | 947 | tmp_buf[sizeof(tmp_buf)-1] = 0; |
32e48121 | 948 | g_strchomp(tmp_buf); |
21700912 | 949 | widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf); |
f583082b AF |
950 | |
951 | strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf)); | |
a45242c2 | 952 | tmp_buf[sizeof(tmp_buf)-1] = 0; |
32e48121 | 953 | g_strchomp(tmp_buf); |
21700912 | 954 | widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf); |
f583082b | 955 | |
4c77d5e0 | 956 | g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60); |
21700912 | 957 | widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf); |
8c4f1350 | 958 | } else { |
4c77d5e0 GB |
959 | widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data")); |
960 | widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data")); | |
961 | widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data")); | |
24d5c7e2 | 962 | } |
4b00e581 AF |
963 | |
964 | table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE)); | |
965 | gtk_table_set_col_spacing (table, 0, 10); | |
966 | for (i=0; i<cnt; i++) { | |
967 | GtkWidget *label; | |
968 | ||
969 | label = gtk_label_new(NULL); | |
970 | gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 ); | |
4c77d5e0 | 971 | gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) ); |
4b00e581 AF |
972 | gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 ); |
973 | if (GTK_IS_MISC(content[i])) { | |
974 | gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 ); | |
975 | } | |
976 | gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 ); | |
977 | } | |
978 | ||
954e061e | 979 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics"))); |
50a14534 | 980 | |
065f60f1 GB |
981 | if ( profile ) { |
982 | GtkWidget *page = NULL; | |
983 | widgets->w_cur_dist = gtk_label_new(_("No Data")); | |
984 | page = create_graph_page (profile, _("<b>Track Distance:</b>"), widgets->w_cur_dist ); | |
985 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance"))); | |
986 | } | |
c79f0206 | 987 | |
065f60f1 GB |
988 | if ( vtdiag ) { |
989 | GtkWidget *page = NULL; | |
990 | widgets->w_cur_time = gtk_label_new(_("No Data")); | |
991 | page = create_graph_page (vtdiag, _("<b>Track Time:</b>"), widgets->w_cur_time ); | |
992 | gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time"))); | |
993 | } | |
50a14534 | 994 | |
25e44eac | 995 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0); |
1d0135d8 | 996 | |
8dcb3ba4 | 997 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE); |
1d0135d8 | 998 | if (seg_count <= 1) |
8dbfe7a3 | 999 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE); |
1d0135d8 | 1000 | if (vik_track_get_dup_point_count(tr) <= 0) |
8dbfe7a3 | 1001 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE); |
1d0135d8 | 1002 | |
21700912 | 1003 | vik_track_set_property_dialog(tr, dialog); |
77b591fa | 1004 | gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT ); |
50a14534 | 1005 | gtk_widget_show_all ( dialog ); |
50a14534 | 1006 | } |