]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Fixes to Alex's last patch
[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 #define PROFILE_WIDTH 600
34 #define PROFILE_HEIGHT 300
35 #define MIN_ALT_DIFF 100.0
36 #define MIN_SPEED_DIFF 20.0
37
38 static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
39 {
40   *max = -1000;
41   *min = 20000;
42   guint i;
43   for ( i=0; i < PROFILE_WIDTH; i++ ) {
44     if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
45       if ( altitudes[i] > *max )
46         *max = altitudes[i];
47       if ( altitudes[i] < *min )
48         *min = altitudes[i];
49     }
50   }
51
52 }
53
54 #define MARGIN 50
55 #define LINES 5
56 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
57 {
58   GdkPixmap *pix;
59   GtkWidget *image;
60   gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
61   gdouble mina, maxa;
62   guint i;
63
64   if ( altitudes == NULL )
65     return NULL;
66
67   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
68   image = gtk_image_new_from_pixmap ( pix, NULL );
69
70   GdkGC *no_alt_info = gdk_gc_new ( window->window );
71   GdkColor color;
72
73   gdk_color_parse ( "red", &color );
74   gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
75
76
77   minmax_alt(altitudes, min_alt, max_alt);
78   mina = *min_alt; 
79   maxa = *max_alt;
80   if  (maxa-mina < MIN_ALT_DIFF) {
81     maxa = mina + MIN_ALT_DIFF;
82   }
83   
84   /* clear the image */
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);
89
90 #if 0
91   {
92     int j;
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 };
98     for (i=0; i<5; i++) {
99       for (j=0; j<8; j++) {
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);
104       }
105     }
106   }
107 #else
108
109   
110   /* draw grid */
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);
115     gchar s[32];
116
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);
124
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);
127   }
128
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 );
134     else 
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) );
137 #endif
138   /* draw border */
139   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
140
141   g_object_unref ( G_OBJECT(pix) );
142   g_free ( altitudes );
143   g_object_unref ( G_OBJECT(no_alt_info) );
144   return image;
145 }
146 #define METRIC 1
147 #ifdef METRIC 
148 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
149 #else
150 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
151 #endif
152
153 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr)
154 {
155   GdkPixmap *pix;
156   GtkWidget *image;
157   gdouble mins, maxs;
158   guint i;
159
160
161   gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
162   if ( speeds == NULL )
163     return NULL;
164
165   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
166   image = gtk_image_new_from_pixmap ( pix, NULL );
167
168   for (i=0; i<PROFILE_WIDTH; i++) {
169     speeds[i] = MTOK(speeds[i]);
170   }
171
172   minmax_alt(speeds, &mins, &maxs);
173   if  (maxs-mins < MIN_SPEED_DIFF) {
174     maxs = mins + MIN_SPEED_DIFF;
175   }
176   
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);
182
183 #if 0
184   /* XXX this can go out, it's just a helpful dev tool */
185   {
186     int j;
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);
198       }
199     }
200   }
201 #else
202
203   /* draw grid */
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);
208     gchar s[32];
209
210     pfd = pango_font_description_from_string (LABEL_FONT);
211     pango_layout_set_font_description (pl, pfd);
212     pango_font_description_free (pfd);
213 #ifdef METRIC 
214     sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
215 #else
216     sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
217 #endif
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);
221
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);
224   }
225
226   /* draw speeds */
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) );
230 #endif
231   /* draw border */
232   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
233
234   g_object_unref ( G_OBJECT(pix) );
235   g_free ( speeds );
236   return image;
237 }
238 #undef MARGIN
239 #undef LINES
240
241 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr )
242 {
243   GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
244                                                   parent,
245                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
246                                                   GTK_STOCK_CANCEL,
247                                                   GTK_RESPONSE_REJECT,
248                                                   "Split Segments",
249                                                   VIK_TRW_LAYER_PROPWIN_SPLIT,
250                                                   "Reverse",
251                                                   VIK_TRW_LAYER_PROPWIN_REVERSE,
252                                                   "Delete Dupl.",
253                                                   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
254                                                   GTK_STOCK_OK,
255                                                   GTK_RESPONSE_ACCEPT,
256                                                   NULL);
257   GtkWidget *left_vbox, *right_vbox, *hbox;
258   GtkWidget *e_cmt;
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;
260   gdouble tr_len;
261   guint32 tp_count, seg_count;
262   gint resp;
263
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();
268
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];
271
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 );
274
275   e_cmt = gtk_entry_new ();
276   if ( tr->comment )
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) );
279
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 );
283
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 );
287
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 );
291
292   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
293   l_dups = gtk_label_new ( tmp_buf );
294
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 );
297
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 );
300
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 );
303
304   g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
305   l_elev = gtk_label_new ( tmp_buf );
306
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 );
310
311   {
312     time_t t1, t2;
313     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
314     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
315
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);
319
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);
323
324     g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
325     l_dur = gtk_label_new(tmp_buf);
326   }
327
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);
341
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);
346
347   if ( profile )
348     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
349
350   if ( vtdiag )
351     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
352
353   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
354   
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) ) );
359
360   gtk_widget_destroy ( dialog );
361   return resp;
362 }