]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_propwin.c
Alex Foobarian's track profile patch
[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
33#define PROFILE_WIDTH 600
34#define PROFILE_HEIGHT 300
35
36static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
37{
38 *max = -1000;
39 *min = 20000;
40 guint i;
41 for ( i=0; i < PROFILE_WIDTH; i++ ) {
42 if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
43 if ( altitudes[i] > *max )
44 *max = altitudes[i];
45 if ( altitudes[i] < *min )
46 *min = altitudes[i];
47 }
48 }
49
50}
51
52GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
53{
54 GdkPixmap *pix = gdk_pixmap_new( window->window, PROFILE_WIDTH, PROFILE_HEIGHT, -1 );
55 GtkWidget *image = gtk_image_new_from_pixmap ( pix, NULL );
56 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
57
58 guint i;
59
60 GdkGC *no_alt_info = gdk_gc_new ( window->window );
61 GdkColor color;
62
63 gdk_color_parse ( "red", &color );
64 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
65
66 minmax_alt(altitudes, min_alt, max_alt);
67
bb71de8b 68 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], TRUE, 0, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
50a14534 69 for ( i = 0; i < PROFILE_WIDTH; i++ )
bb71de8b 70 if ( altitudes[i] == VIK_DEFAULT_ALTITUDE )
50a14534 71 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, i, 0, i, PROFILE_HEIGHT );
bb71de8b
AF
72 else
73 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->fg_gc[0], i, PROFILE_HEIGHT, i, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-*min_alt)/(*max_alt-*min_alt) );
74 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, 0, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
50a14534
EB
75 g_object_unref ( G_OBJECT(pix) );
76 g_free ( altitudes );
77 g_object_unref ( G_OBJECT(no_alt_info) );
78 return image;
79}
80
81gint vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrack *tr )
82{
83 GtkWidget *dialog = gtk_dialog_new_with_buttons ("Track Properties",
84 parent,
85 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
86 GTK_STOCK_CANCEL,
87 GTK_RESPONSE_REJECT,
88 "Split Segments",
89 VIK_TRW_LAYER_PROPWIN_SPLIT,
90 "Reverse",
91 VIK_TRW_LAYER_PROPWIN_REVERSE,
92 "Delete Dupl.",
93 VIK_TRW_LAYER_PROPWIN_DEL_DUP,
94 GTK_STOCK_OK,
95 GTK_RESPONSE_ACCEPT,
96 NULL);
97 GtkWidget *left_vbox, *right_vbox, *hbox;
98 GtkWidget *e_cmt;
f583082b 99 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
100 gdouble tr_len;
101 guint32 tp_count, seg_count;
102 gint resp;
103
104 gdouble min_alt, max_alt;
105 GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr,&min_alt,&max_alt);
106
f583082b 107 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 108 static gchar tmp_buf[25];
50a14534
EB
109
110 left_vbox = a_dialog_create_label_vbox ( label_texts, sizeof(label_texts) / sizeof(label_texts[0]) );
111 right_vbox = gtk_vbox_new ( TRUE, 3 );
112
113 e_cmt = gtk_entry_new ();
114 if ( tr->comment )
115 gtk_entry_set_text ( GTK_ENTRY(e_cmt), tr->comment );
116 g_signal_connect_swapped ( e_cmt, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
117
118 tr_len = vik_track_get_length(tr);
119 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
120 l_len = gtk_label_new ( tmp_buf );
121
122 tp_count = vik_track_get_tp_count(tr);
123 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
124 l_tps = gtk_label_new ( tmp_buf );
125
126 seg_count = vik_track_get_segment_count(tr) ;
127 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
128 l_segs = gtk_label_new ( tmp_buf );
129
130 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
131 l_dups = gtk_label_new ( tmp_buf );
132
133 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_max_speed(tr) );
134 l_maxs = gtk_label_new ( tmp_buf );
135
136 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", vik_track_get_average_speed(tr) );
137 l_avgs = gtk_label_new ( tmp_buf );
138
139 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
140 l_avgd = gtk_label_new ( tmp_buf );
141
142 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
143 l_elev = gtk_label_new ( tmp_buf );
144
145 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
146 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
147 l_galo = gtk_label_new ( tmp_buf );
148
f583082b
AF
149 {
150 time_t t1, t2;
151 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
152 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
153
154 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
155 tmp_buf[strlen(tmp_buf)-1] = 0;
156 l_begin = gtk_label_new(tmp_buf);
157
158 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
159 tmp_buf[strlen(tmp_buf)-1] = 0;
160 l_end = gtk_label_new(tmp_buf);
161
162 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
163 l_dur = gtk_label_new(tmp_buf);
164 }
165
50a14534
EB
166 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
167 gtk_box_pack_start (GTK_BOX(right_vbox), l_len, FALSE, FALSE, 0);
168 gtk_box_pack_start (GTK_BOX(right_vbox), l_tps, FALSE, FALSE, 0);
169 gtk_box_pack_start (GTK_BOX(right_vbox), l_segs, FALSE, FALSE, 0);
170 gtk_box_pack_start (GTK_BOX(right_vbox), l_dups, FALSE, FALSE, 0);
171 gtk_box_pack_start (GTK_BOX(right_vbox), l_maxs, FALSE, FALSE, 0);
172 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgs, FALSE, FALSE, 0);
173 gtk_box_pack_start (GTK_BOX(right_vbox), l_avgd, FALSE, FALSE, 0);
174 gtk_box_pack_start (GTK_BOX(right_vbox), l_elev, FALSE, FALSE, 0);
175 gtk_box_pack_start (GTK_BOX(right_vbox), l_galo, FALSE, FALSE, 0);
f583082b
AF
176 gtk_box_pack_start (GTK_BOX(right_vbox), l_begin, FALSE, FALSE, 0);
177 gtk_box_pack_start (GTK_BOX(right_vbox), l_end, FALSE, FALSE, 0);
178 gtk_box_pack_start (GTK_BOX(right_vbox), l_dur, FALSE, FALSE, 0);
50a14534
EB
179
180 hbox = gtk_hbox_new ( TRUE, 0 );
181 gtk_box_pack_start (GTK_BOX(hbox), left_vbox, FALSE, FALSE, 0);
182 gtk_box_pack_start (GTK_BOX(hbox), right_vbox, FALSE, FALSE, 0);
183 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 0);
184 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), profile, FALSE, FALSE, 0);
185
186
187 gtk_widget_show_all ( dialog );
188 resp = gtk_dialog_run (GTK_DIALOG (dialog));
189 if ( resp == GTK_RESPONSE_ACCEPT )
190 vik_track_set_comment ( tr, gtk_entry_get_text ( GTK_ENTRY(e_cmt) ) );
191
192 gtk_widget_destroy ( dialog );
193 return resp;
194}