]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/viktrwlayer_propwin.c
The "Selected Time" on track properties dialog is now correct.
[andy/viking.git] / src / viktrwlayer_propwin.c
... / ...
CommitLineData
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include <gtk/gtk.h>
23#include <time.h>
24#include <string.h>
25#include "coords.h"
26#include "vikcoord.h"
27#include "viktrack.h"
28#include "viktrwlayer_propwin.h"
29#include "vikwaypoint.h"
30#include "dialog.h"
31#include "globals.h"
32
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 */
39
40#define PROFILE_WIDTH 600
41#define PROFILE_HEIGHT 300
42#define MIN_ALT_DIFF 100.0
43#define MIN_SPEED_DIFF 20.0
44
45static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
46{
47 *max = -1000;
48 *min = 20000;
49 guint i;
50 for ( i=0; i < PROFILE_WIDTH; i++ ) {
51 if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
52 if ( altitudes[i] > *max )
53 *max = altitudes[i];
54 if ( altitudes[i] < *min )
55 *min = altitudes[i];
56 }
57 }
58}
59
60static GtkWidget *label_date;
61
62#define MARGIN 50
63#define LINES 5
64static void set_center_at_graph_position(gdouble event_x, gint img_width, VikLayersPanel *vlp, VikTrack *tr, gboolean time_base)
65{
66 VikTrackpoint *trackpoint;
67 gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
68 if (x < 0)
69 x = 0;
70 if (x > PROFILE_WIDTH)
71 x = PROFILE_WIDTH;
72
73 if (time_base)
74 trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH );
75 else
76 trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH );
77
78 if ( trackpoint ) {
79 VikCoord coord = trackpoint->coord;
80 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
81 vik_layers_panel_emit_update ( vlp );
82 }
83}
84void track_profile_click( GtkWidget *image, GdkEventButton *event, gpointer *pass_along )
85{
86 VikTrack *tr = pass_along[0];
87 VikLayersPanel *vlp = pass_along[1];
88 set_center_at_graph_position(event->x, image->allocation.width, vlp, tr, FALSE);
89}
90
91void track_vt_click( GtkWidget *image, GdkEventButton *event, gpointer *pass_along )
92{
93 VikTrack *tr = pass_along[0];
94 VikLayersPanel *vlp = pass_along[1];
95 set_center_at_graph_position(event->x, image->allocation.width, vlp, tr, TRUE);
96}
97
98void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
99{
100 VikTrack *tr = pass_along[0];
101 int mouse_x, mouse_y;
102 GdkModifierType state;
103
104 if (event->is_hint)
105 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
106 else
107 mouse_x = event->x;
108
109 gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
110 if (x < 0)
111 x = 0;
112 if (x > PROFILE_WIDTH)
113 x = PROFILE_WIDTH;
114
115 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH );
116 if (trackpoint) {
117 time_t t1 = trackpoint->timestamp;
118 static gchar tmp_buf[50];
119 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
120 tmp_buf[sizeof(tmp_buf) -1] = '\0';
121 tmp_buf[strlen(tmp_buf)-1] = 0;
122
123 gtk_label_set_text(GTK_LABEL(label_date), tmp_buf);
124 }
125}
126
127GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt, gpointer vlp )
128{
129 GdkPixmap *pix;
130 GtkWidget *image;
131 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
132 gdouble mina, maxa;
133 GtkWidget *eventbox;
134 gpointer *pass_along;
135 guint i;
136
137 if ( altitudes == NULL ) {
138 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
139 return NULL;
140 }
141
142 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
143 image = gtk_image_new_from_pixmap ( pix, NULL );
144
145 GdkGC *no_alt_info = gdk_gc_new ( window->window );
146 GdkColor color;
147
148 gdk_color_parse ( "red", &color );
149 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
150
151
152 minmax_alt(altitudes, min_alt, max_alt);
153 mina = *min_alt;
154 maxa = *max_alt;
155 if (maxa-mina < MIN_ALT_DIFF) {
156 maxa = mina + MIN_ALT_DIFF;
157 }
158
159 /* clear the image */
160 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
161 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
162 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
163 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
164
165 /* draw grid */
166#define LABEL_FONT "Sans 8"
167 for (i=0; i<=LINES; i++) {
168 PangoFontDescription *pfd;
169 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
170 gchar s[32];
171
172 pfd = pango_font_description_from_string (LABEL_FONT);
173 pango_layout_set_font_description (pl, pfd);
174 pango_font_description_free (pfd);
175 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
176 pango_layout_set_text(pl, s, -1);
177 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
178 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
179
180 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
181 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
182 }
183
184 /* draw elevations */
185 for ( i = 0; i < PROFILE_WIDTH; i++ )
186 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
187 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
188 i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
189 else
190 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
191 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
192
193 /* draw border */
194 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
195
196
197
198 g_object_unref ( G_OBJECT(pix) );
199 g_free ( altitudes );
200 g_object_unref ( G_OBJECT(no_alt_info) );
201
202 pass_along = g_malloc ( sizeof(gpointer) * 2 );
203 pass_along[0] = tr;
204 pass_along[1] = vlp;
205
206 eventbox = gtk_event_box_new ();
207 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
208 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
209 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
210 gtk_container_add ( GTK_CONTAINER(eventbox), image );
211 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
212 | GDK_POINTER_MOTION_MASK
213 | GDK_POINTER_MOTION_HINT_MASK);
214
215 return eventbox;
216}
217
218#define METRIC 1
219#ifdef METRIC
220#define MTOK(v) ( (v)*3.6) /* m/s to km/h */
221#else
222#define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
223#endif
224
225GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp)
226{
227 GdkPixmap *pix;
228 GtkWidget *image;
229 gdouble mins, maxs;
230 guint i;
231 GtkWidget *eventbox;
232 gpointer *pass_along;
233
234 pass_along = g_malloc ( sizeof(gpointer) * 2 );
235 pass_along[0] = tr;
236 pass_along[1] = vlp;
237
238 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
239 if ( speeds == NULL )
240 return NULL;
241
242 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
243 image = gtk_image_new_from_pixmap ( pix, NULL );
244
245 for (i=0; i<PROFILE_WIDTH; i++) {
246 speeds[i] = MTOK(speeds[i]);
247 }
248
249 minmax_alt(speeds, &mins, &maxs);
250 mins = 0; /* splines sometimes give negative speeds */
251 maxs = maxs * 110 / 100;
252 if (maxs-mins < MIN_SPEED_DIFF) {
253 maxs = mins + MIN_SPEED_DIFF;
254 }
255
256 /* clear the image */
257 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
258 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
259 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
260 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
261
262#if 0
263 /* XXX this can go out, it's just a helpful dev tool */
264 {
265 int j;
266 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
267 window->style->light_gc,
268 window->style->dark_gc, window->style->mid_gc,
269 window->style->text_gc, window->style->base_gc,
270 window->style->text_aa_gc };
271 for (i=0; i<5; i++) {
272 for (j=0; j<8; j++) {
273 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
274 TRUE, i*20, j*20, 20, 20);
275 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
276 FALSE, i*20, j*20, 20, 20);
277 }
278 }
279 }
280#else
281
282 /* draw grid */
283#define LABEL_FONT "Sans 8"
284 for (i=0; i<=LINES; i++) {
285 PangoFontDescription *pfd;
286 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
287 gchar s[32];
288
289 pfd = pango_font_description_from_string (LABEL_FONT);
290 pango_layout_set_font_description (pl, pfd);
291 pango_font_description_free (pfd);
292#ifdef METRIC
293 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
294#else
295 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
296#endif
297 pango_layout_set_text(pl, s, -1);
298 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
299 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
300
301 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
302 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
303 }
304
305 /* draw speeds */
306 for ( i = 0; i < PROFILE_WIDTH; i++ )
307 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
308 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
309#endif
310 /* draw border */
311 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
312
313 g_object_unref ( G_OBJECT(pix) );
314 g_free ( speeds );
315
316 eventbox = gtk_event_box_new ();
317 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
318 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
319 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
320 gtk_container_add ( GTK_CONTAINER(eventbox), image );
321 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
322 | GDK_POINTER_MOTION_MASK
323 | GDK_POINTER_MOTION_HINT_MASK);
324
325 return eventbox;
326}
327#undef MARGIN
328#undef LINES
329
330gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr, gpointer vlp )
331{
332 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
333 parent,
334 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
335 GTK_STOCK_CANCEL,
336 GTK_RESPONSE_REJECT,
337 "Split Segments",
338 VIK_TRW_LAYER_PROPWIN_SPLIT,
339 "Reverse",
340 VIK_TRW_LAYER_PROPWIN_REVERSE,
341 "Delete Dupl.",
342 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
343 GTK_STOCK_OK,
344 GTK_RESPONSE_ACCEPT,
345 NULL);
346 GtkTable *table;
347 GtkWidget *e_cmt;
348 gdouble tr_len;
349 guint32 tp_count, seg_count;
350 gint resp;
351
352 gdouble min_alt, max_alt;
353 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt,vlp);
354 GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp);
355 GtkWidget *graphs = gtk_notebook_new();
356
357 GtkWidget *content[20];
358 int cnt;
359 int i;
360
361 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 Time:</b>" };
362 static gchar tmp_buf[50];
363 gdouble tmp_speed;
364
365 cnt = 0;
366 e_cmt = gtk_entry_new ();
367 if ( tr->comment )
368 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
369 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
370 content[cnt++] = e_cmt;
371
372 tr_len = vik_track_get_length(tr);
373 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
374 content[cnt++] = gtk_label_new ( tmp_buf );
375
376 tp_count = vik_track_get_tp_count(tr);
377 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
378 content[cnt++] = gtk_label_new ( tmp_buf );
379
380 seg_count = vik_track_get_segment_count(tr) ;
381 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
382 content[cnt++] = gtk_label_new ( tmp_buf );
383
384 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
385 content[cnt++] = gtk_label_new ( tmp_buf );
386
387 tmp_speed = vik_track_get_max_speed(tr);
388 if ( tmp_speed == 0 )
389 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
390 else
391 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s (%.0f km/h)", tmp_speed, MTOK(tmp_speed) );
392 content[cnt++] = gtk_label_new ( tmp_buf );
393
394 tmp_speed = vik_track_get_average_speed(tr);
395 if ( tmp_speed == 0 )
396 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
397 else
398 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s (%.0f km/h)", tmp_speed, MTOK(tmp_speed) );
399 content[cnt++] = gtk_label_new ( tmp_buf );
400
401 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
402 content[cnt++] = gtk_label_new ( tmp_buf );
403
404 if ( min_alt == VIK_DEFAULT_ALTITUDE )
405 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
406 else
407 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
408 content[cnt++] = gtk_label_new ( tmp_buf );
409
410 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
411 if ( min_alt == VIK_DEFAULT_ALTITUDE )
412 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
413 else
414 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
415 content[cnt++] = gtk_label_new ( tmp_buf );
416
417#if 0
418#define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
419 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
420 PACK(l_len);
421 PACK(l_tps);
422 PACK(l_segs);
423 PACK(l_dups);
424 PACK(l_maxs);
425 PACK(l_avgs);
426 PACK(l_avgd);
427 PACK(l_elev);
428 PACK(l_galo);
429#undef PACK;
430#endif
431
432 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
433 {
434 time_t t1, t2;
435 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
436 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
437
438 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
439 tmp_buf[sizeof(tmp_buf)-1] = 0;
440 g_strchomp(tmp_buf);
441 content[cnt++] = gtk_label_new(tmp_buf);
442
443 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
444 tmp_buf[sizeof(tmp_buf)-1] = 0;
445 g_strchomp(tmp_buf);
446 content[cnt++] = gtk_label_new(tmp_buf);
447
448 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
449 content[cnt++] = gtk_label_new(tmp_buf);
450 } else {
451 content[cnt++] = gtk_label_new("No Data");
452 content[cnt++] = gtk_label_new("No Data");
453 content[cnt++] = gtk_label_new("No Data");
454 }
455 label_date = gtk_label_new("No Data");
456 content[cnt++] = label_date;
457
458 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
459 gtk_table_set_col_spacing (table, 0, 10);
460 for (i=0; i<cnt; i++) {
461 GtkWidget *label;
462
463 label = gtk_label_new(NULL);
464 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 );
465 gtk_label_set_markup ( GTK_LABEL(label), label_texts[i] );
466 gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 );
467 if (GTK_IS_MISC(content[i])) {
468 gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 );
469 }
470 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
471 }
472
473 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(table), FALSE, FALSE, 0);
474
475 if ( profile )
476 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
477
478 if ( vtdiag )
479 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
480
481 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
482
483 gtk_widget_show_all ( dialog );
484 resp = gtk_dialog_run (GTK_DIALOG (dialog));
485 if ( resp == GTK_RESPONSE_ACCEPT )
486 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
487
488 gtk_widget_destroy ( dialog );
489 return resp;
490}