]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_propwin.c
Update TODO/ChangeLog
[andy/viking.git] / src / viktrwlayer_propwin.c
CommitLineData
50a14534
EB
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>
f583082b 24#include <string.h>
50a14534
EB
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
24d5c7e2
EB
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
50a14534
EB
40#define PROFILE_WIDTH 600
41#define PROFILE_HEIGHT 300
25e44eac
AF
42#define MIN_ALT_DIFF 100.0
43#define MIN_SPEED_DIFF 20.0
50a14534
EB
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 }
50a14534
EB
58}
59
25e44eac
AF
60#define MARGIN 50
61#define LINES 5
24d5c7e2
EB
62void track_profile_click( GtkWidget *image, GdkEventButton *event, gpointer *pass_along )
63{
64 VikTrack *tr = pass_along[0];
65 VikCoord *coord = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) (event->x - MARGIN - 2) / PROFILE_WIDTH );
66 if ( coord ) {
67 VikLayersPanel *vlp = pass_along[1];
68 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), coord );
69 vik_layers_panel_emit_update ( vlp );
70 g_free ( coord );
71 }
72}
73
74GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt, gpointer vlp )
50a14534 75{
c79f0206
EB
76 GdkPixmap *pix;
77 GtkWidget *image;
50a14534 78 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
25e44eac 79 gdouble mina, maxa;
561e6ad0 80 GtkWidget *eventbox;
24d5c7e2 81 gpointer *pass_along;
50a14534
EB
82 guint i;
83
8c4f1350
EB
84 if ( altitudes == NULL ) {
85 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
c79f0206 86 return NULL;
8c4f1350 87 }
c79f0206
EB
88
89 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
90 image = gtk_image_new_from_pixmap ( pix, NULL );
91
50a14534
EB
92 GdkGC *no_alt_info = gdk_gc_new ( window->window );
93 GdkColor color;
94
95 gdk_color_parse ( "red", &color );
96 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
97
25e44eac 98
50a14534 99 minmax_alt(altitudes, min_alt, max_alt);
25e44eac
AF
100 mina = *min_alt;
101 maxa = *max_alt;
102 if (maxa-mina < MIN_ALT_DIFF) {
103 maxa = mina + MIN_ALT_DIFF;
104 }
105
106 /* clear the image */
107 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
108 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
109 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
110 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
111
25e44eac
AF
112 /* draw grid */
113#define LABEL_FONT "Sans 8"
114 for (i=0; i<=LINES; i++) {
115 PangoFontDescription *pfd;
116 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
117 gchar s[32];
118
119 pfd = pango_font_description_from_string (LABEL_FONT);
120 pango_layout_set_font_description (pl, pfd);
121 pango_font_description_free (pfd);
122 sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
123 pango_layout_set_text(pl, s, -1);
124 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
125 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
126
127 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
128 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
129 }
130
131 /* draw elevations */
50a14534 132 for ( i = 0; i < PROFILE_WIDTH; i++ )
bb71de8b 133 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
25e44eac
AF
134 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
135 i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
bb71de8b 136 else
25e44eac
AF
137 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
138 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
734652bf 139
25e44eac
AF
140 /* draw border */
141 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
142
24d5c7e2
EB
143
144
50a14534
EB
145 g_object_unref ( G_OBJECT(pix) );
146 g_free ( altitudes );
147 g_object_unref ( G_OBJECT(no_alt_info) );
24d5c7e2
EB
148
149 pass_along = g_malloc ( sizeof(gpointer) * 2 );
150 pass_along[0] = tr;
151 pass_along[1] = vlp;
152
153 eventbox = gtk_event_box_new ();
154 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
155 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
156 gtk_container_add ( GTK_CONTAINER(eventbox), image );
157
158 return eventbox;
50a14534 159}
24d5c7e2 160
25e44eac
AF
161#define METRIC 1
162#ifdef METRIC
163#define MTOK(v) ( (v)*3600.0/1000.0) /* m/s to km/h */
164#else
165#define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
166#endif
167
561e6ad0 168GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp)
25e44eac 169{
c79f0206
EB
170 GdkPixmap *pix;
171 GtkWidget *image;
25e44eac 172 gdouble mins, maxs;
25e44eac 173 guint i;
561e6ad0
EB
174 GtkWidget *eventbox;
175 gpointer *pass_along;
25e44eac 176
561e6ad0
EB
177 pass_along = g_malloc ( sizeof(gpointer) * 2 );
178 pass_along[0] = tr;
179 pass_along[1] = vlp;
c79f0206
EB
180
181 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
182 if ( speeds == NULL )
183 return NULL;
184
185 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
186 image = gtk_image_new_from_pixmap ( pix, NULL );
187
25e44eac
AF
188 for (i=0; i<PROFILE_WIDTH; i++) {
189 speeds[i] = MTOK(speeds[i]);
190 }
191
192 minmax_alt(speeds, &mins, &maxs);
bf35388d
EB
193 mins = 0; /* splines sometimes give negative speeds */
194 maxs = maxs * 110 / 100;
25e44eac
AF
195 if (maxs-mins < MIN_SPEED_DIFF) {
196 maxs = mins + MIN_SPEED_DIFF;
197 }
198
199 /* clear the image */
200 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
201 TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
202 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
203 TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
204
205#if 0
206 /* XXX this can go out, it's just a helpful dev tool */
207 {
208 int j;
209 GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc,
210 window->style->light_gc,
211 window->style->dark_gc, window->style->mid_gc,
212 window->style->text_gc, window->style->base_gc,
213 window->style->text_aa_gc };
214 for (i=0; i<5; i++) {
215 for (j=0; j<8; j++) {
216 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
217 TRUE, i*20, j*20, 20, 20);
218 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
219 FALSE, i*20, j*20, 20, 20);
220 }
221 }
222 }
223#else
224
225 /* draw grid */
226#define LABEL_FONT "Sans 8"
227 for (i=0; i<=LINES; i++) {
228 PangoFontDescription *pfd;
229 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
230 gchar s[32];
231
232 pfd = pango_font_description_from_string (LABEL_FONT);
233 pango_layout_set_font_description (pl, pfd);
234 pango_font_description_free (pfd);
235#ifdef METRIC
236 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
237#else
238 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
239#endif
240 pango_layout_set_text(pl, s, -1);
241 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0,
242 CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
243
244 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
245 MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
246 }
247
248 /* draw speeds */
249 for ( i = 0; i < PROFILE_WIDTH; i++ )
250 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
251 i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
252#endif
253 /* draw border */
254 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
255
256 g_object_unref ( G_OBJECT(pix) );
257 g_free ( speeds );
561e6ad0
EB
258
259 eventbox = gtk_event_box_new ();
260 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
261 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
262 gtk_container_add ( GTK_CONTAINER(eventbox), image );
263
264 return eventbox;
25e44eac
AF
265}
266#undef MARGIN
267#undef LINES
50a14534 268
24d5c7e2 269gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr, gpointer vlp )
50a14534
EB
270{
271 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
272 parent,
25e44eac 273 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
50a14534
EB
274 GTK_STOCK_CANCEL,
275 GTK_RESPONSE_REJECT,
276 "Split Segments",
277 VIK_TRW_LAYER_PROPWIN_SPLIT,
278 "Reverse",
279 VIK_TRW_LAYER_PROPWIN_REVERSE,
280 "Delete Dupl.",
281 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
282 GTK_STOCK_OK,
283 GTK_RESPONSE_ACCEPT,
284 NULL);
285 GtkWidget *left_vbox, *right_vbox, *hbox;
286 GtkWidget *e_cmt;
f583082b 287 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;
50a14534
EB
288 gdouble tr_len;
289 guint32 tp_count, seg_count;
290 gint resp;
291
292 gdouble min_alt, max_alt;
24d5c7e2 293 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt,vlp);
561e6ad0 294 GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp);
25e44eac 295 GtkWidget *graphs = gtk_notebook_new();
50a14534 296
f583082b 297 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>" };
bb71de8b 298 static gchar tmp_buf[25];
8c4f1350 299 gdouble tmp_speed;
50a14534
EB
300
301 left_vbox = a_dialog_create_label_vbox ( label_texts, sizeof(label_texts) / sizeof(label_texts[0]) );
302 right_vbox = gtk_vbox_new ( TRUE, 3 );
303
304 e_cmt = gtk_entry_new ();
305 if ( tr->comment )
306 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
307 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
308
309 tr_len = vik_track_get_length(tr);
310 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
311 l_len = gtk_label_new ( tmp_buf );
312
313 tp_count = vik_track_get_tp_count(tr);
314 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
315 l_tps = gtk_label_new ( tmp_buf );
316
317 seg_count = vik_track_get_segment_count(tr) ;
318 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
319 l_segs = gtk_label_new ( tmp_buf );
320
321 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
322 l_dups = gtk_label_new ( tmp_buf );
323
8c4f1350
EB
324 tmp_speed = vik_track_get_max_speed(tr);
325 if ( tmp_speed == 0 )
326 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
327 else
328 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
50a14534
EB
329 l_maxs = gtk_label_new ( tmp_buf );
330
8c4f1350
EB
331 tmp_speed = vik_track_get_average_speed(tr);
332 if ( tmp_speed == 0 )
333 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
334 else
335 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
50a14534
EB
336 l_avgs = gtk_label_new ( tmp_buf );
337
338 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
339 l_avgd = gtk_label_new ( tmp_buf );
340
8c4f1350
EB
341 if ( min_alt == VIK_DEFAULT_ALTITUDE )
342 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
343 else
344 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
50a14534
EB
345 l_elev = gtk_label_new ( tmp_buf );
346
347 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
bf35388d 348 if ( min_alt == VIK_DEFAULT_ALTITUDE )
8c4f1350
EB
349 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
350 else
351 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
50a14534
EB
352 l_galo = gtk_label_new ( tmp_buf );
353
24d5c7e2
EB
354
355 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
356 gtk_box_pack_start (GTK_BOX(right_vbox), l_len, FALSE, FALSE, 0);
357 gtk_box_pack_start (GTK_BOX(right_vbox), l_tps, FALSE, FALSE, 0);
358 gtk_box_pack_start (GTK_BOX(right_vbox), l_segs, FALSE, FALSE, 0);
359 gtk_box_pack_start (GTK_BOX(right_vbox), l_dups, FALSE, FALSE, 0);
360 gtk_box_pack_start (GTK_BOX(right_vbox), l_maxs, FALSE, FALSE, 0);
361 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgs, FALSE, FALSE, 0);
362 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgd, FALSE, FALSE, 0);
363 gtk_box_pack_start (GTK_BOX(right_vbox), l_elev, FALSE, FALSE, 0);
364 gtk_box_pack_start (GTK_BOX(right_vbox), l_galo, FALSE, FALSE, 0);
365
8c4f1350 366 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
f583082b
AF
367 {
368 time_t t1, t2;
369 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
370 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
371
372 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
373 tmp_buf[strlen(tmp_buf)-1] = 0;
374 l_begin = gtk_label_new(tmp_buf);
375
376 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
377 tmp_buf[strlen(tmp_buf)-1] = 0;
378 l_end = gtk_label_new(tmp_buf);
379
380 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
381 l_dur = gtk_label_new(tmp_buf);
8c4f1350
EB
382 } else {
383 l_begin = gtk_label_new("No Data");
384 l_end = gtk_label_new("No Data");
385 l_dur = gtk_label_new("No Data");
24d5c7e2 386 }
8c4f1350
EB
387 gtk_box_pack_start (GTK_BOX(right_vbox), l_begin, FALSE, FALSE, 0);
388 gtk_box_pack_start (GTK_BOX(right_vbox), l_end, FALSE, FALSE, 0);
389 gtk_box_pack_start (GTK_BOX(right_vbox), l_dur, FALSE, FALSE, 0);
50a14534
EB
390
391 hbox = gtk_hbox_new ( TRUE, 0 );
392 gtk_box_pack_start (GTK_BOX(hbox), left_vbox, FALSE, FALSE, 0);
393 gtk_box_pack_start (GTK_BOX(hbox), right_vbox, FALSE, FALSE, 0);
394 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 0);
50a14534 395
c79f0206
EB
396 if ( profile )
397 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
398
399 if ( vtdiag )
400 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
50a14534 401
25e44eac
AF
402 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
403
50a14534
EB
404 gtk_widget_show_all ( dialog );
405 resp = gtk_dialog_run (GTK_DIALOG (dialog));
406 if ( resp == GTK_RESPONSE_ACCEPT )
407 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
408
409 gtk_widget_destroy ( dialog );
410 return resp;
411}