]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Adding Maplint OpenStreetMap layer
[andy/viking.git] / src / viktrwlayer_propwin.c
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
45 static 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
60 static GtkWidget *label_date;
61
62 #define MARGIN 50
63 #define LINES 5
64 void track_profile_click( GtkWidget *image, GdkEventButton *event, gpointer *pass_along )
65 {
66   gdouble x = event->x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
67   if (x < 0)
68     x = 0;
69   if (x > PROFILE_WIDTH)
70     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 );
73   if ( trackpoint ) {
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 );
78   }
79 }
80 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
81 {
82   VikTrack *tr = pass_along[0];
83   int mouse_x, mouse_y;
84   GdkModifierType state;
85
86   if (event->is_hint)
87     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
88   else
89     mouse_x = event->x;
90
91   gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
92   if (x < 0)
93     x = 0;
94   if (x > PROFILE_WIDTH)
95     x = PROFILE_WIDTH;
96
97   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH );
98   if (trackpoint) {
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;
103
104     gtk_label_set_text(GTK_LABEL(label_date), tmp_buf);
105   }
106 }
107
108 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt, gpointer vlp )
109 {
110   GdkPixmap *pix;
111   GtkWidget *image;
112   gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
113   gdouble mina, maxa;
114   GtkWidget *eventbox;
115   gpointer *pass_along;
116   guint i;
117
118   if ( altitudes == NULL ) {
119     *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
120     return NULL;
121   }
122
123   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
124   image = gtk_image_new_from_pixmap ( pix, NULL );
125
126   GdkGC *no_alt_info = gdk_gc_new ( window->window );
127   GdkColor color;
128
129   gdk_color_parse ( "red", &color );
130   gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
131
132
133   minmax_alt(altitudes, min_alt, max_alt);
134   mina = *min_alt; 
135   maxa = *max_alt;
136   if  (maxa-mina < MIN_ALT_DIFF) {
137     maxa = mina + MIN_ALT_DIFF;
138   }
139   
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);
145
146   /* draw grid */
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);
151     gchar s[32];
152
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);
160
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);
163   }
164
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 );
170     else 
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) );
173
174   /* draw border */
175   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
176
177
178
179   g_object_unref ( G_OBJECT(pix) );
180   g_free ( altitudes );
181   g_object_unref ( G_OBJECT(no_alt_info) );
182
183   pass_along = g_malloc ( sizeof(gpointer) * 2 );
184   pass_along[0] = tr;
185   pass_along[1] = vlp;
186
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);
195
196   return eventbox;
197 }
198
199 #define METRIC 1
200 #ifdef METRIC 
201 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
202 #else
203 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
204 #endif
205
206 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp)
207 {
208   GdkPixmap *pix;
209   GtkWidget *image;
210   gdouble mins, maxs;
211   guint i;
212   GtkWidget *eventbox;
213   gpointer *pass_along;
214
215   pass_along = g_malloc ( sizeof(gpointer) * 2 );
216   pass_along[0] = tr;
217   pass_along[1] = vlp;
218
219   gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
220   if ( speeds == NULL )
221     return NULL;
222
223   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
224   image = gtk_image_new_from_pixmap ( pix, NULL );
225
226   for (i=0; i<PROFILE_WIDTH; i++) {
227     speeds[i] = MTOK(speeds[i]);
228   }
229
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;
235   }
236   
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);
242
243 #if 0
244   /* XXX this can go out, it's just a helpful dev tool */
245   {
246     int j;
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);
258       }
259     }
260   }
261 #else
262
263   /* draw grid */
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);
268     gchar s[32];
269
270     pfd = pango_font_description_from_string (LABEL_FONT);
271     pango_layout_set_font_description (pl, pfd);
272     pango_font_description_free (pfd);
273 #ifdef METRIC 
274     sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
275 #else
276     sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
277 #endif
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);
281
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);
284   }
285
286   /* draw speeds */
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) );
290 #endif
291   /* draw border */
292   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
293
294   g_object_unref ( G_OBJECT(pix) );
295   g_free ( speeds );
296
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);
305
306   return eventbox;
307 }
308 #undef MARGIN
309 #undef LINES
310
311 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr, gpointer vlp )
312 {
313   GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
314                                                   parent,
315                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
316                                                   GTK_STOCK_CANCEL,
317                                                   GTK_RESPONSE_REJECT,
318                                                   "Split Segments",
319                                                   VIK_TRW_LAYER_PROPWIN_SPLIT,
320                                                   "Reverse",
321                                                   VIK_TRW_LAYER_PROPWIN_REVERSE,
322                                                   "Delete Dupl.",
323                                                   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
324                                                   GTK_STOCK_OK,
325                                                   GTK_RESPONSE_ACCEPT,
326                                                   NULL);
327   GtkTable *table;
328   GtkWidget *e_cmt;
329   gdouble tr_len;
330   guint32 tp_count, seg_count;
331   gint resp;
332
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();
337
338   GtkWidget *content[20];
339   int cnt;
340   int i;
341
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];
344   gdouble tmp_speed;
345
346   cnt = 0;
347   e_cmt = gtk_entry_new ();
348   if ( tr->comment )
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;
352
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 );
356
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 );
360
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 );
364
365   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
366   content[cnt++] = gtk_label_new ( tmp_buf );
367
368   tmp_speed = vik_track_get_max_speed(tr);
369   if ( tmp_speed == 0 )
370     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
371   else
372     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
373   content[cnt++] = gtk_label_new ( tmp_buf );
374
375   tmp_speed = vik_track_get_average_speed(tr);
376   if ( tmp_speed == 0 )
377     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
378   else
379     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
380   content[cnt++] = gtk_label_new ( tmp_buf );
381
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 );
384
385   if ( min_alt == VIK_DEFAULT_ALTITUDE )
386     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
387   else
388     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
389   content[cnt++] = gtk_label_new ( tmp_buf );
390
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");
394   else
395     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
396   content[cnt++] = gtk_label_new ( tmp_buf );
397
398 #if 0
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); 
401   PACK(l_len);
402   PACK(l_tps);
403   PACK(l_segs);
404   PACK(l_dups);
405   PACK(l_maxs);
406   PACK(l_avgs);
407   PACK(l_avgd);
408   PACK(l_elev);
409   PACK(l_galo);
410 #undef PACK;
411 #endif
412
413   if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
414   {
415     time_t t1, t2;
416     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
417     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
418
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);
422
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);
426
427     g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
428     content[cnt++] = gtk_label_new(tmp_buf);
429   } else {
430     content[cnt++] = gtk_label_new("No Data");
431     content[cnt++] = gtk_label_new("No Data");
432     content[cnt++] = gtk_label_new("No Data");
433   }
434   label_date = gtk_label_new("No Data");
435   content[cnt++] = label_date;
436
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++) {
440     GtkWidget *label;
441
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 );
448     }
449     gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
450   }
451
452   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(table), FALSE, FALSE, 0);
453
454   if ( profile )
455     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
456
457   if ( vtdiag )
458     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
459
460   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
461   
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) ) );
466
467   gtk_widget_destroy ( dialog );
468   return resp;
469 }