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);
91 #define LABEL_FONT "Sans 8"
92 for (i=0; i<=LINES; i++) {
93 PangoFontDescription *pfd;
94 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
97 pfd = pango_font_description_from_string (LABEL_FONT);
98 pango_layout_set_font_description (pl, pfd);
99 pango_font_description_free (pfd);
100 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
101 pango_layout_set_text(pl, s, -1);
102 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
103 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
105 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
106 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
109 /* draw elevations */
110 for ( i = 0; i < PROFILE_WIDTH; i++ )
111 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
112 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
113 i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
115 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
116 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
119 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
121 g_object_unref ( G_OBJECT(pix) );
122 g_free ( altitudes );
123 g_object_unref ( G_OBJECT(no_alt_info) );
128 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
130 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
133 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr)
141 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
142 if ( speeds == NULL )
145 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
146 image = gtk_image_new_from_pixmap ( pix, NULL );
148 for (i=0; i<PROFILE_WIDTH; i++) {
149 speeds[i] = MTOK(speeds[i]);
152 minmax_alt(speeds, &mins, &maxs);
153 if (maxs-mins < MIN_SPEED_DIFF) {
154 maxs = mins + MIN_SPEED_DIFF;
157 /* clear the image */
158 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
159 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
160 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
161 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
164 /* XXX this can go out, it's just a helpful dev tool */
167 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
168 window->style->light_gc,
169 window->style->dark_gc, window->style->mid_gc,
170 window->style->text_gc, window->style->base_gc,
171 window->style->text_aa_gc };
172 for (i=0; i<5; i++) {
173 for (j=0; j<8; j++) {
174 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
175 TRUE, i*20, j*20, 20, 20);
176 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
177 FALSE, i*20, j*20, 20, 20);
184 #define LABEL_FONT "Sans 8"
185 for (i=0; i<=LINES; i++) {
186 PangoFontDescription *pfd;
187 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
190 pfd = pango_font_description_from_string (LABEL_FONT);
191 pango_layout_set_font_description (pl, pfd);
192 pango_font_description_free (pfd);
194 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
196 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
198 pango_layout_set_text(pl, s, -1);
199 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
200 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
202 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
203 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
207 for ( i = 0; i < PROFILE_WIDTH; i++ )
208 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
209 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
212 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
214 g_object_unref ( G_OBJECT(pix) );
221 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr )
223 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
225 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
229 VIK_TRW_LAYER_PROPWIN_SPLIT,
231 VIK_TRW_LAYER_PROPWIN_REVERSE,
233 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
237 GtkWidget *left_vbox, *right_vbox, *hbox;
239 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;
241 guint32 tp_count, seg_count;
244 gdouble min_alt, max_alt;
245 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt);
246 GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr);
247 GtkWidget *graphs = gtk_notebook_new();
249 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>" };
250 static gchar tmp_buf[25];
252 left_vbox = a_dialog_create_label_vbox ( label_texts, sizeof(label_texts) / sizeof(label_texts[0]) );
253 right_vbox = gtk_vbox_new ( TRUE, 3 );
255 e_cmt = gtk_entry_new ();
257 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
258 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
260 tr_len = vik_track_get_length(tr);
261 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
262 l_len = gtk_label_new ( tmp_buf );
264 tp_count = vik_track_get_tp_count(tr);
265 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
266 l_tps = gtk_label_new ( tmp_buf );
268 seg_count = vik_track_get_segment_count(tr) ;
269 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
270 l_segs = gtk_label_new ( tmp_buf );
272 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
273 l_dups = gtk_label_new ( tmp_buf );
275 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_max_speed(tr) );
276 l_maxs = gtk_label_new ( tmp_buf );
278 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_average_speed(tr) );
279 l_avgs = gtk_label_new ( tmp_buf );
281 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
282 l_avgd = gtk_label_new ( tmp_buf );
284 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
285 l_elev = gtk_label_new ( tmp_buf );
287 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
288 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
289 l_galo = gtk_label_new ( tmp_buf );
293 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
294 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
296 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
297 tmp_buf[strlen(tmp_buf)-1] = 0;
298 l_begin = gtk_label_new(tmp_buf);
300 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
301 tmp_buf[strlen(tmp_buf)-1] = 0;
302 l_end = gtk_label_new(tmp_buf);
304 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
305 l_dur = gtk_label_new(tmp_buf);
308 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
309 gtk_box_pack_start (GTK_BOX(right_vbox), l_len, FALSE, FALSE, 0);
310 gtk_box_pack_start (GTK_BOX(right_vbox), l_tps, FALSE, FALSE, 0);
311 gtk_box_pack_start (GTK_BOX(right_vbox), l_segs, FALSE, FALSE, 0);
312 gtk_box_pack_start (GTK_BOX(right_vbox), l_dups, FALSE, FALSE, 0);
313 gtk_box_pack_start (GTK_BOX(right_vbox), l_maxs, FALSE, FALSE, 0);
314 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgs, FALSE, FALSE, 0);
315 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgd, FALSE, FALSE, 0);
316 gtk_box_pack_start (GTK_BOX(right_vbox), l_elev, FALSE, FALSE, 0);
317 gtk_box_pack_start (GTK_BOX(right_vbox), l_galo, FALSE, FALSE, 0);
318 gtk_box_pack_start (GTK_BOX(right_vbox), l_begin, FALSE, FALSE, 0);
319 gtk_box_pack_start (GTK_BOX(right_vbox), l_end, FALSE, FALSE, 0);
320 gtk_box_pack_start (GTK_BOX(right_vbox), l_dur, FALSE, FALSE, 0);
322 hbox = gtk_hbox_new ( TRUE, 0 );
323 gtk_box_pack_start (GTK_BOX(hbox), left_vbox, FALSE, FALSE, 0);
324 gtk_box_pack_start (GTK_BOX(hbox), right_vbox, FALSE, FALSE, 0);
325 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 0);
328 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
331 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
333 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
335 gtk_widget_show_all ( dialog );
336 resp = gtk_dialog_run (GTK_DIALOG (dialog));
337 if ( resp == GTK_RESPONSE_ACCEPT )
338 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
340 gtk_widget_destroy ( dialog );