2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2005-2007, Alex Foobarian <foobarian@gmail.com>
6 * Copyright (C) 2007-2008, Quy Tonthat <qtonthat@gmail.com>
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.
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.
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
33 #include <glib/gi18n.h>
41 #include "viktrwlayer.h"
42 #include "viktrwlayer_propwin.h"
43 #include "vikwaypoint.h"
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 */
55 #define PROPWIN_PROFILE_WIDTH 600
56 #define PROPWIN_PROFILE_HEIGHT 300
58 #define PROPWIN_LABEL_FONT "Sans 7"
60 #define MIN_ALT_DIFF 100.0
61 #define MIN_SPEED_DIFF 5.0
63 typedef struct _propsaved {
69 typedef struct _propwidgets {
70 gboolean configure_dialog;
79 GtkWidget *w_track_length;
80 GtkWidget *w_tp_count;
81 GtkWidget *w_segment_count;
82 GtkWidget *w_duptp_count;
83 GtkWidget *w_max_speed;
84 GtkWidget *w_avg_speed;
85 GtkWidget *w_avg_dist;
86 GtkWidget *w_elev_range;
87 GtkWidget *w_elev_gain;
88 GtkWidget *w_time_start;
89 GtkWidget *w_time_end;
90 GtkWidget *w_time_dur;
91 GtkWidget *w_cur_dist; /*< Current distance */
92 GtkWidget *w_cur_time; /*< Current time */
94 PropSaved elev_graph_saved_img;
95 PropSaved speed_graph_saved_img;
100 VikTrackpoint *marker_tp;
101 gboolean is_marker_drawn;
104 static PropWidgets *prop_widgets_new()
106 PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
111 static void prop_widgets_free(PropWidgets *widgets)
113 if (widgets->elev_graph_saved_img.img)
114 g_object_unref(widgets->elev_graph_saved_img.img);
115 if (widgets->speed_graph_saved_img.img)
116 g_object_unref(widgets->speed_graph_saved_img.img);
117 if (widgets->altitudes)
118 g_free(widgets->altitudes);
120 g_free(widgets->speeds);
124 static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
129 for ( i=0; i < PROFILE_WIDTH; i++ ) {
130 if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
131 if ( array[i] > *max )
133 if ( array[i] < *min )
141 static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
150 VikTrackpoint *trackpoint;
151 gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
154 if (x > PROFILE_WIDTH)
158 trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
160 trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
163 VikCoord coord = trackpoint->coord;
165 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
166 vik_layers_panel_emit_update ( vlp );
169 /* since vlp not set, vvp should be valid instead! */
171 vik_viewport_set_center_coord ( vvp, &coord );
172 vik_layer_emit_update ( VIK_LAYER(vtl) );
179 * Returns whether the marker was drawn or not
181 static void draw_graph_mark (GtkWidget *image,
185 PropSaved *saved_img,
188 gboolean *marker_drawn)
190 GdkPixmap *pix = NULL;
191 /* the pixmap = margin + graph area */
192 gdouble x = event_x - img_width/2 + PROFILE_WIDTH/2 + MARGIN/2;
194 // fprintf(stderr, "event_x=%f img_width=%d x=%f\n", event_x, img_width, x);
196 gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
197 /* Restore previously saved image */
198 if (saved_img->saved) {
199 gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0,
200 saved_img->pos, 0, -1, -1);
201 saved_img->saved = FALSE;
202 gtk_widget_queue_draw_area(image,
203 saved_img->pos + img_width/2 - PROFILE_WIDTH/2 - MARGIN/2, 0,
204 saved_img->img->width, saved_img->img->height);
206 if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) {
207 /* Save part of the image */
209 gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img,
210 x, 0, 0, 0, saved_img->img->width, saved_img->img->height);
212 saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix),
213 saved_img->img, x, 0, 0, 0, 1, PROFILE_HEIGHT);
215 saved_img->saved = TRUE;
216 gdk_draw_line (GDK_DRAWABLE(pix), gc, x, 0, x, image->allocation.height);
217 /* redraw the area which contains the line, saved_width is just convenient */
218 gtk_widget_queue_draw_area(image, event_x, 0, 1, PROFILE_HEIGHT);
219 *marker_drawn = TRUE;
222 *marker_drawn = FALSE;
226 * Return the percentage of how far a trackpoint is a long a track via the time method
228 static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
231 if (trackpoint == NULL)
233 time_t t_start, t_end, t_total;
234 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
235 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
236 t_total = t_end - t_start;
237 pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
242 * Return the percentage of how far a trackpoint is a long a track via the distance method
244 static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
247 if (trackpoint == NULL)
251 for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
252 dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
253 &(VIK_TRACKPOINT(iter->prev->data)->coord));
254 /* Assuming trackpoint is not a copy */
255 if (trackpoint == VIK_TRACKPOINT(iter->data))
259 pc = dist/track_length;
263 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph )
265 VikTrack *tr = pass_along[0];
266 VikLayersPanel *vlp = pass_along[1];
267 VikViewport *vvp = pass_along[2];
268 PropWidgets *widgets = pass_along[3];
269 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
270 GtkWidget *image = GTK_WIDGET(child->data);
271 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
273 VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_vt_graph, widgets->profile_width);
274 draw_graph_mark(image,
276 event_box->allocation.width,
277 window->style->black_gc,
278 is_vt_graph ? &widgets->speed_graph_saved_img : &widgets->elev_graph_saved_img,
279 widgets->profile_width,
280 widgets->profile_height,
281 &widgets->is_marker_drawn);
283 widgets->marker_tp = trackpoint;
284 gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
286 /* draw on the other graph */
287 if (trackpoint == NULL || widgets->elev_box == NULL || widgets->speed_box == NULL)
288 /* This test assumes we have only 2 graphs */
293 GList *other_child = gtk_container_get_children(GTK_CONTAINER(
294 is_vt_graph ? widgets->elev_box : widgets->speed_box));
295 GtkWidget *other_image = GTK_WIDGET(other_child->data);
297 pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length );
299 pc = tp_percentage_by_time ( tr, trackpoint );
302 x2 = pc * widgets->profile_width + MARGIN + (event_box->allocation.width/2 - widgets->profile_width/2 - MARGIN/2);
303 draw_graph_mark(other_image,
305 event_box->allocation.width,
306 window->style->black_gc,
307 is_vt_graph ? &widgets->elev_graph_saved_img : &widgets->speed_graph_saved_img,
308 widgets->profile_width,
309 widgets->profile_height,
310 &widgets->is_marker_drawn);
313 g_list_free(other_child);
317 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
319 track_graph_click(event_box, event, pass_along, FALSE);
320 return TRUE; /* don't call other (further) callbacks */
323 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
325 track_graph_click(event_box, event, pass_along, TRUE);
326 return TRUE; /* don't call other (further) callbacks */
329 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
331 VikTrack *tr = pass_along[0];
332 PropWidgets *widgets = pass_along[3];
333 int mouse_x, mouse_y;
334 GdkModifierType state;
337 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
341 gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
344 if (x > widgets->profile_width)
345 x = widgets->profile_width;
347 gdouble meters_from_start;
348 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
349 if (trackpoint && widgets->w_cur_dist) {
350 static gchar tmp_buf[20];
351 vik_units_distance_t dist_units = a_vik_get_units_distance ();
352 switch (dist_units) {
353 case VIK_UNITS_DISTANCE_KILOMETRES:
354 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
356 case VIK_UNITS_DISTANCE_MILES:
357 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
360 g_critical("Houston, we've had a problem. distance=%d", dist_units);
362 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
366 void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
368 VikTrack *tr = pass_along[0];
369 PropWidgets *widgets = pass_along[3];
370 int mouse_x, mouse_y;
371 GdkModifierType state;
374 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
378 gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
381 if (x > widgets->profile_width)
382 x = widgets->profile_width;
384 time_t seconds_from_start;
385 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
386 if (trackpoint && widgets->w_cur_time) {
387 static gchar tmp_buf[20];
389 h = seconds_from_start/3600;
390 m = (seconds_from_start - h*3600)/60;
391 s = seconds_from_start - (3600*h) - (60*m);
392 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
394 gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
398 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)
402 gdouble max_speed = 0;
403 gdouble total_length = vik_track_get_length_including_gaps(tr);
405 for (iter = tr->trackpoints->next; iter; iter = iter->next) {
406 if (!isnan(VIK_TRACKPOINT(iter->data)->speed))
407 max_speed = MAX(max_speed, VIK_TRACKPOINT(iter->data)->speed);
409 max_speed = max_speed * 110 / 100;
411 for (iter = tr->trackpoints->next; iter; iter = iter->next) {
412 int x, y_alt, y_speed;
413 gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
415 dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
416 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
417 x = (width * dist)/total_length + margin;
418 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
419 y_alt = height - ((height * elev)/alt_diff);
420 gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
422 if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
423 y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
424 gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
430 * Draw just the height profile image
432 static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
445 // Free previous allocation
446 if ( widgets->altitudes )
447 g_free ( widgets->altitudes );
449 widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
451 if ( widgets->altitudes == NULL )
454 minmax_array(widgets->altitudes, &mina, &maxa, TRUE, widgets->profile_width);
455 maxa = maxa + ((maxa - mina) * 0.25); // Make visible window a bit bigger than highest point
457 window = gtk_widget_get_toplevel (widgets->elev_box);
459 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
461 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
463 no_alt_info = gdk_gc_new ( window->window );
464 gdk_color_parse ( "yellow", &color );
465 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
467 dem_alt_gc = gdk_gc_new ( window->window );
468 gdk_color_parse ( "green", &color );
469 gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
471 gps_speed_gc = gdk_gc_new ( window->window );
472 gdk_color_parse ( "red", &color );
473 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
475 /* clear the image */
476 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
477 TRUE, 0, 0, MARGIN, widgets->profile_height);
478 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
479 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
482 vik_units_height_t height_units = a_vik_get_units_height ();
483 for (i=0; i<=LINES; i++) {
484 PangoFontDescription *pfd;
485 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
489 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
490 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
491 pango_layout_set_font_description (pl, pfd);
492 pango_font_description_free (pfd);
493 switch (height_units) {
494 case VIK_UNITS_HEIGHT_METRES:
495 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
497 case VIK_UNITS_HEIGHT_FEET:
498 sprintf(s, "%8dft", (int)VIK_METERS_TO_FEET(mina + (LINES-i)*(maxa-mina)/LINES));
502 g_critical("Houston, we've had a problem. height=%d", height_units);
504 pango_layout_set_text(pl, s, -1);
505 pango_layout_get_pixel_size (pl, &w, &h);
506 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
507 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
509 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
510 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
511 g_object_unref ( G_OBJECT ( pl ) );
515 /* draw elevations */
516 for ( i = 0; i < widgets->profile_width; i++ )
517 if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
518 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
519 i + MARGIN, 0, i + MARGIN, widgets->profile_height );
521 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
522 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(maxa-mina) );
524 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);
527 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
529 g_object_unref ( G_OBJECT(pix) );
530 g_object_unref ( G_OBJECT(no_alt_info) );
531 g_object_unref ( G_OBJECT(dem_alt_gc) );
532 g_object_unref ( G_OBJECT(gps_speed_gc) );
537 * Draw just the speed (velocity)/time image
539 static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
546 // Free previous allocation
547 if ( widgets->speeds )
548 g_free ( widgets->speeds );
550 widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
551 if ( widgets->speeds == NULL )
557 window = gtk_widget_get_toplevel (widgets->speed_box);
559 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
561 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
563 gps_speed_gc = gdk_gc_new ( window->window );
564 gdk_color_parse ( "red", &color );
565 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
567 minmax_array(widgets->speeds, &mins, &maxs, FALSE, widgets->profile_width);
569 mins = 0; /* splines sometimes give negative speeds */
570 maxs = maxs + ((maxs - mins) * 0.1);
571 if (maxs-mins < MIN_SPEED_DIFF) {
572 maxs = mins + MIN_SPEED_DIFF;
575 /* clear the image */
576 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
577 TRUE, 0, 0, MARGIN, widgets->profile_height);
578 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
579 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
582 for (i=0; i<=LINES; i++) {
583 PangoFontDescription *pfd;
584 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
588 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
589 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
590 pango_layout_set_font_description (pl, pfd);
591 pango_font_description_free (pfd);
592 vik_units_speed_t speed_units = a_vik_get_units_speed ();
593 switch (speed_units) {
594 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
595 sprintf(s, "%6.1fkm/h", VIK_MPS_TO_KPH(mins + (LINES-i)*(maxs-mins)/LINES));
597 case VIK_UNITS_SPEED_MILES_PER_HOUR:
598 sprintf(s, "%6.1fmph", VIK_MPS_TO_MPH(mins + (LINES-i)*(maxs-mins)/LINES));
600 case VIK_UNITS_SPEED_METRES_PER_SECOND:
601 sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
603 case VIK_UNITS_SPEED_KNOTS:
604 sprintf(s, "%6.1fknots", VIK_MPS_TO_KNOTS(mins + (LINES-i)*(maxs-mins)/LINES));
608 g_critical("Houston, we've had a problem. speed=%d", speed_units);
611 pango_layout_set_text(pl, s, -1);
612 pango_layout_get_pixel_size (pl, &w, &h);
613 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
614 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
616 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
617 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
618 g_object_unref ( G_OBJECT ( pl ) );
624 for ( i = 0; i < widgets->profile_width; i++ )
625 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
626 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(maxs-mins) );
629 time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
630 time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
632 for (iter = tr->trackpoints; iter; iter = iter->next) {
633 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
634 if (isnan(gps_speed))
636 int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
637 int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(maxs - mins);
638 gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
642 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
644 g_object_unref ( G_OBJECT(pix) );
645 g_object_unref ( G_OBJECT(gps_speed_gc) );
652 * Configure the profile & speed/time images
654 static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along )
656 VikTrack *tr = pass_along[0];
657 PropWidgets *widgets = pass_along[2];
659 // ATM we receive configure_events when the dialog is moved and so no further action is necessary
660 if ( !widgets->configure_dialog )
663 widgets->configure_dialog = FALSE;
665 // Draw graphs even if they are not visible
669 if (widgets->elev_box != NULL) {
670 child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
671 draw_elevations (GTK_WIDGET(child->data), tr, widgets );
676 if (widgets->speed_box != NULL) {
677 child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
678 draw_vt (GTK_WIDGET(child->data), tr, widgets );
686 * Create height profile widgets including the image and callbacks
688 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
693 gpointer *pass_along;
696 widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
698 if ( widgets->altitudes == NULL ) {
699 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
703 minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
705 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
706 image = gtk_image_new_from_pixmap ( pix, NULL );
708 g_object_unref ( G_OBJECT(pix) );
710 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
714 pass_along[3] = widgets;
716 eventbox = gtk_event_box_new ();
717 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
718 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
719 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
720 gtk_container_add ( GTK_CONTAINER(eventbox), image );
721 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
727 * Create speed/time widgets including the image and callbacks
729 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
734 gpointer *pass_along;
737 widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
738 if ( widgets->speeds == NULL )
741 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
745 pass_along[3] = widgets;
747 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
748 image = gtk_image_new_from_pixmap ( pix, NULL );
751 /* XXX this can go out, it's just a helpful dev tool */
754 GdkGC **colors[8] = { window->style->bg_gc,
755 window->style->fg_gc,
756 window->style->light_gc,
757 window->style->dark_gc,
758 window->style->mid_gc,
759 window->style->text_gc,
760 window->style->base_gc,
761 window->style->text_aa_gc };
762 for (i=0; i<5; i++) {
763 for (j=0; j<8; j++) {
764 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
765 TRUE, i*20, j*20, 20, 20);
766 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
767 FALSE, i*20, j*20, 20, 20);
773 g_object_unref ( G_OBJECT(pix) );
775 eventbox = gtk_event_box_new ();
776 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
777 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
778 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
779 gtk_container_add ( GTK_CONTAINER(eventbox), image );
780 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
786 static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets)
788 VikTrack *tr = widgets->tr;
789 VikTrwLayer *vtl = widgets->vtl;
790 gboolean keep_dialog = FALSE;
792 /* FIXME: check and make sure the track still exists before doing anything to it */
793 /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
795 case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
796 case GTK_RESPONSE_REJECT:
798 case GTK_RESPONSE_ACCEPT:
799 vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
801 case VIK_TRW_LAYER_PROPWIN_REVERSE:
802 vik_track_reverse(tr);
803 vik_layer_emit_update ( VIK_LAYER(vtl) );
805 case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
806 vik_track_remove_dup_points(tr);
807 /* above operation could have deleted current_tp or last_tp */
808 trw_layer_cancel_tps_of_track ( vtl, widgets->track_name );
809 vik_layer_emit_update ( VIK_LAYER(vtl) );
811 case VIK_TRW_LAYER_PROPWIN_SPLIT:
813 /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
815 VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
818 for ( i = 0; i < ntracks; i++ )
820 g_assert ( tracks[i] );
821 new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1);
822 /* if ( (wp_exists) && (! overwrite) ) */
823 /* don't need to upper case new_tr_name because old tr name was uppercase */
824 if ( vik_trw_layer_get_track(vtl, new_tr_name ) &&
825 ( ! a_dialog_yes_or_no ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) )
827 gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
828 g_free ( new_tr_name );
830 new_tr_name = new_new_tr_name;
834 vik_track_free ( tracks[i] );
838 vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
843 /* Don't let track destroy this dialog */
844 vik_track_clear_property_dialog(tr);
845 vik_trw_layer_delete_track ( vtl, widgets->track_name );
846 vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */
850 case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
852 GList *iter = tr->trackpoints;
853 while ((iter = iter->next)) {
854 if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
858 a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
859 _("Failed spliting track. Track unchanged"), NULL);
864 gchar *r_name = g_strdup_printf("%s #R", widgets->track_name);
865 if (vik_trw_layer_get_track(vtl, r_name ) &&
866 ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl),
867 "The track \"%s\" exists, do you wish to overwrite it?", r_name)))
869 gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
875 a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING,
876 _("Operation Aborted. Track unchanged"), NULL);
881 iter->prev->next = NULL;
883 VikTrack *tr_right = vik_track_new();
885 vik_track_set_comment ( tr_right, tr->comment );
886 tr_right->visible = tr->visible;
887 tr_right->trackpoints = iter;
889 vik_trw_layer_add_track(vtl, r_name, tr_right);
890 vik_layer_emit_update ( VIK_LAYER(vtl) );
894 fprintf(stderr, "DEBUG: unknown response\n");
898 /* Keep same behaviour for now: destroy dialog if click on any button */
900 prop_widgets_free(widgets);
901 vik_track_clear_property_dialog(tr);
902 gtk_widget_destroy ( GTK_WIDGET(dialog) );
906 static GtkWidget *create_graph_page ( GtkWidget *graph,
910 GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
911 GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
912 GtkWidget *label = gtk_label_new (NULL);
913 gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
914 gtk_label_set_markup ( GTK_LABEL(label), markup );
915 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
916 gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
917 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
922 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name, VikViewport *vvp )
924 /* FIXME: free widgets when destroy signal received */
925 PropWidgets *widgets = prop_widgets_new();
929 widgets->profile_width = PROPWIN_PROFILE_WIDTH;
930 widgets->profile_height = PROPWIN_PROFILE_HEIGHT;
931 widgets->track_name = track_name;
932 gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name);
933 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
935 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
936 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
937 _("Split at Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
938 _("Split Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
939 _("Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE,
940 _("Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP,
941 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
943 widgets->dialog = dialog;
945 g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
948 guint32 tp_count, seg_count;
950 gdouble min_alt, max_alt;
951 widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), tr, vlp, vvp, widgets, &min_alt, &max_alt);
952 widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
953 GtkWidget *graphs = gtk_notebook_new();
955 GtkWidget *content[20];
959 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>") };
960 static gchar tmp_buf[50];
964 widgets->w_comment = gtk_entry_new ();
966 gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
967 g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
968 content[cnt++] = widgets->w_comment;
970 vik_units_distance_t dist_units = a_vik_get_units_distance ();
972 tr_len = widgets->track_length = vik_track_get_length(tr);
973 switch (dist_units) {
974 case VIK_UNITS_DISTANCE_KILOMETRES:
975 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 );
977 case VIK_UNITS_DISTANCE_MILES:
978 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) );
981 g_critical("Houston, we've had a problem. distance=%d", dist_units);
983 widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
985 tp_count = vik_track_get_tp_count(tr);
986 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
987 widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
989 seg_count = vik_track_get_segment_count(tr) ;
990 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
991 widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
993 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
994 widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
996 vik_units_speed_t speed_units = a_vik_get_units_speed ();
997 tmp_speed = vik_track_get_max_speed(tr);
998 if ( tmp_speed == 0 )
999 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1001 switch (speed_units) {
1002 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1003 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
1005 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1006 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
1008 case VIK_UNITS_SPEED_METRES_PER_SECOND:
1009 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
1011 case VIK_UNITS_SPEED_KNOTS:
1012 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
1015 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
1016 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1019 widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
1021 tmp_speed = vik_track_get_average_speed(tr);
1022 if ( tmp_speed == 0 )
1023 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1025 switch (speed_units) {
1026 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1027 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
1029 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1030 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
1032 case VIK_UNITS_SPEED_METRES_PER_SECOND:
1033 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
1035 case VIK_UNITS_SPEED_KNOTS:
1036 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
1039 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
1040 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1043 widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
1045 switch (dist_units) {
1046 case VIK_UNITS_DISTANCE_KILOMETRES:
1047 // Even though kilometres, the average distance between points is going to be quite small so keep in metres
1048 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
1050 case VIK_UNITS_DISTANCE_MILES:
1051 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 )) );
1054 g_critical("Houston, we've had a problem. distance=%d", dist_units);
1056 widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
1058 vik_units_height_t height_units = a_vik_get_units_height ();
1059 if ( min_alt == VIK_DEFAULT_ALTITUDE )
1060 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1062 switch (height_units) {
1063 case VIK_UNITS_HEIGHT_METRES:
1064 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
1066 case VIK_UNITS_HEIGHT_FEET:
1067 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) );
1070 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
1071 g_critical("Houston, we've had a problem. height=%d", height_units);
1074 widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
1076 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
1077 if ( min_alt == VIK_DEFAULT_ALTITUDE )
1078 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1080 switch (height_units) {
1081 case VIK_UNITS_HEIGHT_METRES:
1082 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
1084 case VIK_UNITS_HEIGHT_FEET:
1085 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) );
1088 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
1089 g_critical("Houston, we've had a problem. height=%d", height_units);
1092 widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
1095 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
1096 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
1109 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
1112 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1113 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1115 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
1116 tmp_buf[sizeof(tmp_buf)-1] = 0;
1117 g_strchomp(tmp_buf);
1118 widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
1120 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
1121 tmp_buf[sizeof(tmp_buf)-1] = 0;
1122 g_strchomp(tmp_buf);
1123 widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
1125 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60);
1126 widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
1128 widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data"));
1129 widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data"));
1130 widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data"));
1133 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
1134 gtk_table_set_col_spacing (table, 0, 10);
1135 for (i=0; i<cnt; i++) {
1138 label = gtk_label_new(NULL);
1139 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 );
1140 gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) );
1141 gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 );
1142 if (GTK_IS_MISC(content[i])) {
1143 gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 );
1145 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
1148 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics")));
1150 if ( widgets->elev_box ) {
1151 GtkWidget *page = NULL;
1152 widgets->w_cur_dist = gtk_label_new(_("No Data"));
1153 page = create_graph_page (widgets->elev_box, _("<b>Track Distance:</b>"), widgets->w_cur_dist );
1154 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance")));
1157 if ( widgets->speed_box ) {
1158 GtkWidget *page = NULL;
1159 widgets->w_cur_time = gtk_label_new(_("No Data"));
1160 page = create_graph_page (widgets->speed_box, _("<b>Track Time:</b>"), widgets->w_cur_time );
1161 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time")));
1164 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
1166 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
1168 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
1169 if (vik_track_get_dup_point_count(tr) <= 0)
1170 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
1172 gpointer *pass_along;
1173 pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
1175 pass_along[1] = vlp;
1176 pass_along[2] = widgets;
1178 // On dialog realization configure_event casues the graphs to be initially drawn
1179 widgets->configure_dialog = TRUE;
1180 g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), pass_along);
1182 vik_track_set_property_dialog(tr, dialog);
1183 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1184 gtk_widget_show_all ( dialog );