2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "viktrwlayer_propwin.h"
29 #include "vikwaypoint.h"
33 #include "vikviewport.h" /* ugh */
34 #include "viktreeview.h" /* ugh */
35 #include <gdk-pixbuf/gdk-pixdata.h>
36 #include "viklayer.h" /* ugh */
37 #include "vikaggregatelayer.h"
38 #include "viklayerspanel.h" /* ugh */
40 #define PROFILE_WIDTH 600
41 #define PROFILE_HEIGHT 300
42 #define MIN_ALT_DIFF 100.0
43 #define MIN_SPEED_DIFF 20.0
45 static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
50 for ( i=0; i < PROFILE_WIDTH; i++ ) {
51 if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
52 if ( altitudes[i] > *max )
54 if ( altitudes[i] < *min )
60 static GtkWidget *label_date;
64 void track_profile_click( GtkWidget *image, GdkEventButton *event, gpointer *pass_along )
66 gdouble x = event->x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
69 if (x > PROFILE_WIDTH)
71 VikTrack *tr = pass_along[0];
72 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH );
74 VikCoord coord = trackpoint->coord;
75 VikLayersPanel *vlp = pass_along[1];
76 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
77 vik_layers_panel_emit_update ( vlp );
80 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
82 VikTrack *tr = pass_along[0];
84 GdkModifierType state;
87 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
91 gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
94 if (x > PROFILE_WIDTH)
97 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH );
99 time_t t1 = trackpoint->timestamp;
100 static gchar tmp_buf[25];
101 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
102 tmp_buf[strlen(tmp_buf)-1] = 0;
104 gtk_label_set_text(GTK_LABEL(label_date), tmp_buf);
108 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt, gpointer vlp )
112 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
115 gpointer *pass_along;
118 if ( altitudes == NULL ) {
119 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
123 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
124 image = gtk_image_new_from_pixmap ( pix, NULL );
126 GdkGC *no_alt_info = gdk_gc_new ( window->window );
129 gdk_color_parse ( "red", &color );
130 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
133 minmax_alt(altitudes, min_alt, max_alt);
136 if (maxa-mina < MIN_ALT_DIFF) {
137 maxa = mina + MIN_ALT_DIFF;
140 /* clear the image */
141 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
142 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
143 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
144 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
147 #define LABEL_FONT "Sans 8"
148 for (i=0; i<=LINES; i++) {
149 PangoFontDescription *pfd;
150 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
153 pfd = pango_font_description_from_string (LABEL_FONT);
154 pango_layout_set_font_description (pl, pfd);
155 pango_font_description_free (pfd);
156 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
157 pango_layout_set_text(pl, s, -1);
158 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
159 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
161 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
162 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
165 /* draw elevations */
166 for ( i = 0; i < PROFILE_WIDTH; i++ )
167 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
168 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
169 i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
171 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
172 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
175 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
179 g_object_unref ( G_OBJECT(pix) );
180 g_free ( altitudes );
181 g_object_unref ( G_OBJECT(no_alt_info) );
183 pass_along = g_malloc ( sizeof(gpointer) * 2 );
187 eventbox = gtk_event_box_new ();
188 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
189 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
190 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
191 gtk_container_add ( GTK_CONTAINER(eventbox), image );
192 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
193 | GDK_POINTER_MOTION_MASK
194 | GDK_POINTER_MOTION_HINT_MASK);
201 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
203 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
206 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp)
213 gpointer *pass_along;
215 pass_along = g_malloc ( sizeof(gpointer) * 2 );
219 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
220 if ( speeds == NULL )
223 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
224 image = gtk_image_new_from_pixmap ( pix, NULL );
226 for (i=0; i<PROFILE_WIDTH; i++) {
227 speeds[i] = MTOK(speeds[i]);
230 minmax_alt(speeds, &mins, &maxs);
231 mins = 0; /* splines sometimes give negative speeds */
232 maxs = maxs * 110 / 100;
233 if (maxs-mins < MIN_SPEED_DIFF) {
234 maxs = mins + MIN_SPEED_DIFF;
237 /* clear the image */
238 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
239 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
240 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
241 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
244 /* XXX this can go out, it's just a helpful dev tool */
247 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
248 window->style->light_gc,
249 window->style->dark_gc, window->style->mid_gc,
250 window->style->text_gc, window->style->base_gc,
251 window->style->text_aa_gc };
252 for (i=0; i<5; i++) {
253 for (j=0; j<8; j++) {
254 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
255 TRUE, i*20, j*20, 20, 20);
256 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
257 FALSE, i*20, j*20, 20, 20);
264 #define LABEL_FONT "Sans 8"
265 for (i=0; i<=LINES; i++) {
266 PangoFontDescription *pfd;
267 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
270 pfd = pango_font_description_from_string (LABEL_FONT);
271 pango_layout_set_font_description (pl, pfd);
272 pango_font_description_free (pfd);
274 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
276 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
278 pango_layout_set_text(pl, s, -1);
279 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
280 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
282 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
283 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
287 for ( i = 0; i < PROFILE_WIDTH; i++ )
288 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
289 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
292 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
294 g_object_unref ( G_OBJECT(pix) );
297 eventbox = gtk_event_box_new ();
298 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
299 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
300 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
301 gtk_container_add ( GTK_CONTAINER(eventbox), image );
302 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
303 | GDK_POINTER_MOTION_MASK
304 | GDK_POINTER_MOTION_HINT_MASK);
311 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr, gpointer vlp )
313 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
315 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
319 VIK_TRW_LAYER_PROPWIN_SPLIT,
321 VIK_TRW_LAYER_PROPWIN_REVERSE,
323 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
330 guint32 tp_count, seg_count;
333 gdouble min_alt, max_alt;
334 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt,vlp);
335 GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp);
336 GtkWidget *graphs = gtk_notebook_new();
338 GtkWidget *content[20];
342 static gchar *label_texts[] = { "<b>Comment:</b>", "<b>Track Length:</b>", "<b>Trackpoints:</b>", "<b>Segments:</b>", "<b>Duplicate Points:</b>", "<b>Max Speed:</b>", "<b>Avg. Speed:</b>", "<b>Avg. Dist. Between TPs:</b>", "<b>Elevation Range:</b>", "<b>Total Elevation Gain/Loss:</b>", "<b>Start:</b>", "<b>End:</b>", "<b>Duration:</b>", "<b>Selected date</b>" };
343 static gchar tmp_buf[25];
347 e_cmt = gtk_entry_new ();
349 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
350 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
351 content[cnt++] = e_cmt;
353 tr_len = vik_track_get_length(tr);
354 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
355 content[cnt++] = gtk_label_new ( tmp_buf );
357 tp_count = vik_track_get_tp_count(tr);
358 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
359 content[cnt++] = gtk_label_new ( tmp_buf );
361 seg_count = vik_track_get_segment_count(tr) ;
362 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
363 content[cnt++] = gtk_label_new ( tmp_buf );
365 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
366 content[cnt++] = gtk_label_new ( tmp_buf );
368 tmp_speed = vik_track_get_max_speed(tr);
369 if ( tmp_speed == 0 )
370 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
372 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
373 content[cnt++] = gtk_label_new ( tmp_buf );
375 tmp_speed = vik_track_get_average_speed(tr);
376 if ( tmp_speed == 0 )
377 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
379 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
380 content[cnt++] = gtk_label_new ( tmp_buf );
382 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
383 content[cnt++] = gtk_label_new ( tmp_buf );
385 if ( min_alt == VIK_DEFAULT_ALTITUDE )
386 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
388 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
389 content[cnt++] = gtk_label_new ( tmp_buf );
391 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
392 if ( min_alt == VIK_DEFAULT_ALTITUDE )
393 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
395 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
396 content[cnt++] = gtk_label_new ( tmp_buf );
399 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
400 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
413 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
416 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
417 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
419 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
420 tmp_buf[strlen(tmp_buf)-1] = 0;
421 content[cnt++] = gtk_label_new(tmp_buf);
423 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
424 tmp_buf[strlen(tmp_buf)-1] = 0;
425 content[cnt++] = gtk_label_new(tmp_buf);
427 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
428 content[cnt++] = gtk_label_new(tmp_buf);
430 content[cnt++] = gtk_label_new("No Data");
431 content[cnt++] = gtk_label_new("No Data");
432 content[cnt++] = gtk_label_new("No Data");
434 label_date = gtk_label_new("No Data");
435 content[cnt++] = label_date;
437 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
438 gtk_table_set_col_spacing (table, 0, 10);
439 for (i=0; i<cnt; i++) {
442 label = gtk_label_new(NULL);
443 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 );
444 gtk_label_set_markup ( GTK_LABEL(label), label_texts[i] );
445 gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 );
446 if (GTK_IS_MISC(content[i])) {
447 gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 );
449 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
452 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(table), FALSE, FALSE, 0);
455 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
458 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
460 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
462 gtk_widget_show_all ( dialog );
463 resp = gtk_dialog_run (GTK_DIALOG (dialog));
464 if ( resp == GTK_RESPONSE_ACCEPT )
465 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
467 gtk_widget_destroy ( dialog );