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 #define PROFILE_WIDTH 600
34 #define PROFILE_HEIGHT 300
35 #define MIN_ALT_DIFF 100.0
36 #define MIN_SPEED_DIFF 20.0
38 static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
43 for ( i=0; i < PROFILE_WIDTH; i++ ) {
44 if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
45 if ( altitudes[i] > *max )
47 if ( altitudes[i] < *min )
56 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
60 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
64 if ( altitudes == NULL )
67 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
68 image = gtk_image_new_from_pixmap ( pix, NULL );
70 GdkGC *no_alt_info = gdk_gc_new ( window->window );
73 gdk_color_parse ( "red", &color );
74 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
77 minmax_alt(altitudes, min_alt, max_alt);
80 if (maxa-mina < MIN_ALT_DIFF) {
81 maxa = mina + MIN_ALT_DIFF;
85 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
86 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
87 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
88 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
93 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
94 window->style->light_gc,
95 window->style->dark_gc, window->style->mid_gc,
96 window->style->text_gc, window->style->base_gc,
97 window->style->text_aa_gc };
100 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
101 TRUE, i*20, j*20, 20, 20);
102 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
103 FALSE, i*20, j*20, 20, 20);
111 #define LABEL_FONT "Sans 8"
112 for (i=0; i<=LINES; i++) {
113 PangoFontDescription *pfd;
114 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
117 pfd = pango_font_description_from_string (LABEL_FONT);
118 pango_layout_set_font_description (pl, pfd);
119 pango_font_description_free (pfd);
120 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
121 pango_layout_set_text(pl, s, -1);
122 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
123 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
125 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
126 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
129 /* draw elevations */
130 for ( i = 0; i < PROFILE_WIDTH; i++ )
131 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
132 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
133 i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
135 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
136 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
139 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
141 g_object_unref ( G_OBJECT(pix) );
142 g_free ( altitudes );
143 g_object_unref ( G_OBJECT(no_alt_info) );
148 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
150 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
153 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr)
161 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
162 if ( speeds == NULL )
165 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
166 image = gtk_image_new_from_pixmap ( pix, NULL );
168 for (i=0; i<PROFILE_WIDTH; i++) {
169 speeds[i] = MTOK(speeds[i]);
172 minmax_alt(speeds, &mins, &maxs);
173 if (maxs-mins < MIN_SPEED_DIFF) {
174 maxs = mins + MIN_SPEED_DIFF;
177 /* clear the image */
178 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
179 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
180 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
181 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
184 /* XXX this can go out, it's just a helpful dev tool */
187 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
188 window->style->light_gc,
189 window->style->dark_gc, window->style->mid_gc,
190 window->style->text_gc, window->style->base_gc,
191 window->style->text_aa_gc };
192 for (i=0; i<5; i++) {
193 for (j=0; j<8; j++) {
194 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
195 TRUE, i*20, j*20, 20, 20);
196 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
197 FALSE, i*20, j*20, 20, 20);
204 #define LABEL_FONT "Sans 8"
205 for (i=0; i<=LINES; i++) {
206 PangoFontDescription *pfd;
207 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
210 pfd = pango_font_description_from_string (LABEL_FONT);
211 pango_layout_set_font_description (pl, pfd);
212 pango_font_description_free (pfd);
214 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
216 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
218 pango_layout_set_text(pl, s, -1);
219 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
220 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
222 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
223 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
227 for ( i = 0; i < PROFILE_WIDTH; i++ )
228 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
229 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
232 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
234 g_object_unref ( G_OBJECT(pix) );
241 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr )
243 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
245 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
249 VIK_TRW_LAYER_PROPWIN_SPLIT,
251 VIK_TRW_LAYER_PROPWIN_REVERSE,
253 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
257 GtkWidget *left_vbox, *right_vbox, *hbox;
259 GtkWidget *l_len, *l_tps, *l_segs, *l_dups, *l_maxs, *l_avgs, *l_avgd, *l_elev, *l_galo, *l_begin, *l_end, *l_dur;
261 guint32 tp_count, seg_count;
264 gdouble min_alt, max_alt;
265 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt);
266 GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr);
267 GtkWidget *graphs = gtk_notebook_new();
269 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>" };
270 static gchar tmp_buf[25];
272 left_vbox = a_dialog_create_label_vbox ( label_texts, sizeof(label_texts) / sizeof(label_texts[0]) );
273 right_vbox = gtk_vbox_new ( TRUE, 3 );
275 e_cmt = gtk_entry_new ();
277 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
278 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
280 tr_len = vik_track_get_length(tr);
281 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
282 l_len = gtk_label_new ( tmp_buf );
284 tp_count = vik_track_get_tp_count(tr);
285 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
286 l_tps = gtk_label_new ( tmp_buf );
288 seg_count = vik_track_get_segment_count(tr) ;
289 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
290 l_segs = gtk_label_new ( tmp_buf );
292 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
293 l_dups = gtk_label_new ( tmp_buf );
295 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_max_speed(tr) );
296 l_maxs = gtk_label_new ( tmp_buf );
298 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_average_speed(tr) );
299 l_avgs = gtk_label_new ( tmp_buf );
301 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
302 l_avgd = gtk_label_new ( tmp_buf );
304 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
305 l_elev = gtk_label_new ( tmp_buf );
307 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
308 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
309 l_galo = gtk_label_new ( tmp_buf );
313 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
314 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
316 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
317 tmp_buf[strlen(tmp_buf)-1] = 0;
318 l_begin = gtk_label_new(tmp_buf);
320 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
321 tmp_buf[strlen(tmp_buf)-1] = 0;
322 l_end = gtk_label_new(tmp_buf);
324 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
325 l_dur = gtk_label_new(tmp_buf);
328 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
329 gtk_box_pack_start (GTK_BOX(right_vbox), l_len, FALSE, FALSE, 0);
330 gtk_box_pack_start (GTK_BOX(right_vbox), l_tps, FALSE, FALSE, 0);
331 gtk_box_pack_start (GTK_BOX(right_vbox), l_segs, FALSE, FALSE, 0);
332 gtk_box_pack_start (GTK_BOX(right_vbox), l_dups, FALSE, FALSE, 0);
333 gtk_box_pack_start (GTK_BOX(right_vbox), l_maxs, FALSE, FALSE, 0);
334 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgs, FALSE, FALSE, 0);
335 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgd, FALSE, FALSE, 0);
336 gtk_box_pack_start (GTK_BOX(right_vbox), l_elev, FALSE, FALSE, 0);
337 gtk_box_pack_start (GTK_BOX(right_vbox), l_galo, FALSE, FALSE, 0);
338 gtk_box_pack_start (GTK_BOX(right_vbox), l_begin, FALSE, FALSE, 0);
339 gtk_box_pack_start (GTK_BOX(right_vbox), l_end, FALSE, FALSE, 0);
340 gtk_box_pack_start (GTK_BOX(right_vbox), l_dur, FALSE, FALSE, 0);
342 hbox = gtk_hbox_new ( TRUE, 0 );
343 gtk_box_pack_start (GTK_BOX(hbox), left_vbox, FALSE, FALSE, 0);
344 gtk_box_pack_start (GTK_BOX(hbox), right_vbox, FALSE, FALSE, 0);
345 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 0);
348 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
351 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
353 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
355 gtk_widget_show_all ( dialog );
356 resp = gtk_dialog_run (GTK_DIALOG (dialog));
357 if ( resp == GTK_RESPONSE_ACCEPT )
358 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
360 gtk_widget_destroy ( dialog );