]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Guilhem's autotools support/alphabetized trw option
[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   /* draw grid */
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);
95     gchar s[32];
96
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);
104
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);
107   }
108
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 );
114     else 
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) );
117
118   /* draw border */
119   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
120
121   g_object_unref ( G_OBJECT(pix) );
122   g_free ( altitudes );
123   g_object_unref ( G_OBJECT(no_alt_info) );
124   return image;
125 }
126 #define METRIC 1
127 #ifdef METRIC 
128 #define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
129 #else
130 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
131 #endif
132
133 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr)
134 {
135   GdkPixmap *pix;
136   GtkWidget *image;
137   gdouble mins, maxs;
138   guint i;
139
140
141   gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
142   if ( speeds == NULL )
143     return NULL;
144
145   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
146   image = gtk_image_new_from_pixmap ( pix, NULL );
147
148   for (i=0; i<PROFILE_WIDTH; i++) {
149     speeds[i] = MTOK(speeds[i]);
150   }
151
152   minmax_alt(speeds, &mins, &maxs);
153   if  (maxs-mins < MIN_SPEED_DIFF) {
154     maxs = mins + MIN_SPEED_DIFF;
155   }
156   
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);
162
163 #if 0
164   /* XXX this can go out, it's just a helpful dev tool */
165   {
166     int j;
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);
178       }
179     }
180   }
181 #else
182
183   /* draw grid */
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);
188     gchar s[32];
189
190     pfd = pango_font_description_from_string (LABEL_FONT);
191     pango_layout_set_font_description (pl, pfd);
192     pango_font_description_free (pfd);
193 #ifdef METRIC 
194     sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
195 #else
196     sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
197 #endif
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);
201
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);
204   }
205
206   /* draw speeds */
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) );
210 #endif
211   /* draw border */
212   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
213
214   g_object_unref ( G_OBJECT(pix) );
215   g_free ( speeds );
216   return image;
217 }
218 #undef MARGIN
219 #undef LINES
220
221 gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr )
222 {
223   GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
224                                                   parent,
225                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
226                                                   GTK_STOCK_CANCEL,
227                                                   GTK_RESPONSE_REJECT,
228                                                   "Split Segments",
229                                                   VIK_TRW_LAYER_PROPWIN_SPLIT,
230                                                   "Reverse",
231                                                   VIK_TRW_LAYER_PROPWIN_REVERSE,
232                                                   "Delete Dupl.",
233                                                   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
234                                                   GTK_STOCK_OK,
235                                                   GTK_RESPONSE_ACCEPT,
236                                                   NULL);
237   GtkWidget *left_vbox, *right_vbox, *hbox;
238   GtkWidget *e_cmt;
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;
240   gdouble tr_len;
241   guint32 tp_count, seg_count;
242   gint resp;
243
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();
248
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];
251
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 );
254
255   e_cmt = gtk_entry_new ();
256   if ( tr->comment )
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) );
259
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 );
263
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 );
267
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 );
271
272   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
273   l_dups = gtk_label_new ( tmp_buf );
274
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 );
277
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 );
280
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 );
283
284   g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
285   l_elev = gtk_label_new ( tmp_buf );
286
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 );
290
291   {
292     time_t t1, t2;
293     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
294     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
295
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);
299
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);
303
304     g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
305     l_dur = gtk_label_new(tmp_buf);
306   }
307
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);
321
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);
326
327   if ( profile )
328     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
329
330   if ( vtdiag )
331     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
332
333   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
334   
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) ) );
339
340   gtk_widget_destroy ( dialog );
341   return resp;
342 }