Parent Directory | Revision Log
Coordinate popup
1 | harbaum | 1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. | ||
3 | * | ||
4 | * This file is part of GPXView. | ||
5 | * | ||
6 | * GPXView 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 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * GPXView 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 GPXView. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include "gpxview.h" | ||
21 | #include <math.h> | ||
22 | |||
23 | harbaum | 198 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5) |
24 | #include <hildon/hildon-entry.h> | ||
25 | #endif | ||
26 | |||
27 | harbaum | 1 | #define STR_NAN "----" |
28 | |||
29 | typedef struct { | ||
30 | appdata_t *appdata; | ||
31 | |||
32 | GtkWidget *lon1, *lat1, *dist1, *dir1; | ||
33 | GtkWidget *lon2, *lat2; | ||
34 | |||
35 | GtkWidget *distance, *proj_lat, *proj_lon; | ||
36 | GtkWidget *middle_lat, *middle_lon; | ||
37 | } math_dialog_state_t; | ||
38 | |||
39 | static gboolean mark(GtkWidget *widget, gboolean valid) { | ||
40 | gtk_widget_set_state(widget, valid?GTK_STATE_NORMAL:TAG_STATE); | ||
41 | return valid; | ||
42 | } | ||
43 | |||
44 | /* a entry that is colored red when being "active" */ | ||
45 | static GtkWidget *gtk_red_entry_new(void) { | ||
46 | GdkColor color; | ||
47 | harbaum | 212 | GtkWidget *widget = entry_new(); |
48 | harbaum | 1 | gdk_color_parse("#ff0000", &color); |
49 | gtk_widget_modify_text(widget, TAG_STATE, &color); | ||
50 | return widget; | ||
51 | } | ||
52 | |||
53 | /* a comboboxentry that is colored red when being "active" */ | ||
54 | static GtkWidget *gtk_red_combo_box_entry_new_text(void) { | ||
55 | GdkColor color; | ||
56 | GtkWidget *widget = gtk_combo_box_entry_new_text(); | ||
57 | gdk_color_parse("#ff0000", &color); | ||
58 | gtk_widget_modify_text(GTK_BIN(widget)->child, TAG_STATE, &color); | ||
59 | return widget; | ||
60 | } | ||
61 | |||
62 | /* a label that is colored red when being "active" */ | ||
63 | static GtkWidget *gtk_red_label_new(char *str) { | ||
64 | GdkColor color; | ||
65 | harbaum | 198 | GtkWidget *widget = left_label_new(str); |
66 | harbaum | 1 | gdk_color_parse("#ff0000", &color); |
67 | gtk_widget_modify_fg(widget, TAG_STATE, &color); | ||
68 | return widget; | ||
69 | } | ||
70 | |||
71 | static GtkWidget *gtk_dir_entry_new(float val) { | ||
72 | char str[16]; | ||
73 | GtkWidget *w = gtk_red_entry_new(); | ||
74 | |||
75 | snprintf(str, sizeof(str), _("%.1f°"), val); | ||
76 | gtk_entry_set_text(GTK_ENTRY(w), str); | ||
77 | |||
78 | return w; | ||
79 | } | ||
80 | |||
81 | float pos_lat_eval_combo(GtkWidget *widget) { | ||
82 | char *p = (char*)gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget)); | ||
83 | float val = pos_parse_lat(p); | ||
84 | mark(widget, !isnan(val)); | ||
85 | |||
86 | return val; | ||
87 | } | ||
88 | |||
89 | static float distance_eval(GtkWidget *widget, math_dialog_state_t *state) { | ||
90 | char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget)); | ||
91 | float val = distance_parse(p, state->appdata->imperial); | ||
92 | mark(widget, !isnan(val)); | ||
93 | |||
94 | return val; | ||
95 | } | ||
96 | |||
97 | float direction_eval(GtkWidget *widget) { | ||
98 | char *p = (char*)gtk_entry_get_text(GTK_ENTRY(widget)); | ||
99 | float val; | ||
100 | if(sscanf(p, _("%f°"), &val) != 1) | ||
101 | val = NAN; | ||
102 | |||
103 | mark(widget, !isnan(val)); | ||
104 | |||
105 | return val; | ||
106 | } | ||
107 | |||
108 | static void on_calc_clicked(GtkButton *button, gpointer user_data) { | ||
109 | math_dialog_state_t *state = (math_dialog_state_t*)user_data; | ||
110 | pos_t pos1, pos2; | ||
111 | gboolean pos1_ok = FALSE, pos2_ok = FALSE; | ||
112 | float dist1; | ||
113 | gboolean dist1_ok = FALSE; | ||
114 | float dir1; | ||
115 | gboolean dir1_ok = FALSE; | ||
116 | |||
117 | /* parse input */ | ||
118 | pos1.lat = lat_get(state->lat1); | ||
119 | pos1.lon = lon_get(state->lon1); | ||
120 | if(!isnan(pos1.lat) && !isnan(pos1.lon)) pos1_ok = TRUE; | ||
121 | |||
122 | pos2.lat = lat_get(state->lat2); | ||
123 | pos2.lon = lon_get(state->lon2); | ||
124 | if(!isnan(pos2.lat) && !isnan(pos2.lon)) pos2_ok = TRUE; | ||
125 | |||
126 | dist1 = distance_eval(state->dist1, state); | ||
127 | if(!isnan(dist1)) dist1_ok = TRUE; | ||
128 | |||
129 | dir1 = direction_eval(state->dir1); | ||
130 | if(!isnan(dir1)) dir1_ok = TRUE; | ||
131 | |||
132 | /* ------------------- do all calculations ------------------- */ | ||
133 | |||
134 | |||
135 | /* ------------------- distance of coo1 and coo2 ------------------- */ | ||
136 | if(mark(state->distance, pos1_ok && pos2_ok)) { | ||
137 | char str[32]; | ||
138 | float dist = gpx_pos_get_distance(pos1, pos2, state->appdata->imperial); | ||
139 | distance_str(str, sizeof(str), dist, state->appdata->imperial); | ||
140 | |||
141 | gtk_label_set_text(GTK_LABEL(state->distance), str); | ||
142 | } else | ||
143 | gtk_label_set_text(GTK_LABEL(state->distance), STR_NAN); | ||
144 | |||
145 | // N 53° 09.033' W 001° 50.666' 100km / 30° = N 53° 55.616, W001° 04.850 | ||
146 | /* ------------------- coordinate projection ---------------- */ | ||
147 | mark(state->proj_lat, pos1_ok && dist1_ok && dir1_ok); | ||
148 | if(mark(state->proj_lon, pos1_ok && dist1_ok && dir1_ok)) { | ||
149 | pos_t pro; | ||
150 | |||
151 | /* get great circle radius in miles/kilometers */ | ||
152 | float gcrad = state->appdata->imperial?3959.0:6371.0; | ||
153 | |||
154 | // from: http://www.movable-type.co.uk/scripts/latlong.html | ||
155 | pro.lat = asin(sin(pos1.lat/180*M_PI) * cos(dist1/gcrad) + | ||
156 | cos(pos1.lat/180*M_PI) * sin(dist1/gcrad) * | ||
157 | cos(dir1/180*M_PI) )/M_PI*180; | ||
158 | pro.lon = pos1.lon + atan2(sin(dir1/180*M_PI)*sin(dist1/gcrad)* | ||
159 | cos(pos1.lat/180*M_PI), | ||
160 | cos(dist1/gcrad)-sin(pos1.lat/180*M_PI)* | ||
161 | sin(pro.lat/180*M_PI))/M_PI*180; | ||
162 | pro.lon = fmodf(pro.lon+180,360) - 180; // normalise to -180...+180 | ||
163 | |||
164 | char str[16]; | ||
165 | pos_lat_str(str, sizeof(str), pro.lat); | ||
166 | gtk_label_set_text(GTK_LABEL(state->proj_lat), str); | ||
167 | pos_lon_str(str, sizeof(str), pro.lon); | ||
168 | gtk_label_set_text(GTK_LABEL(state->proj_lon), str); | ||
169 | |||
170 | if(!isnan(pro.lat) && !isnan(pro.lon)) | ||
171 | state->appdata->geomath = pro; | ||
172 | } else { | ||
173 | gtk_label_set_text(GTK_LABEL(state->proj_lat), STR_NAN); | ||
174 | gtk_label_set_text(GTK_LABEL(state->proj_lon), STR_NAN); | ||
175 | } | ||
176 | |||
177 | #if 0 | ||
178 | /* ------------ middle between both points ------------- */ | ||
179 | mark(state->middle_lat, pos1_ok && pos2_ok); | ||
180 | if(mark(state->middle_lon, pos1_ok && pos2_ok)) { | ||
181 | pos_t res; | ||
182 | |||
183 | float dlon = fabs(pos2.lon - pos1.lon)/180*M_PI; | ||
184 | |||
185 | /* http://mathforum.org/library/drmath/view/51822.html */ | ||
186 | res.lon = 180/M_PI * atan(cos(pos2.lat/180*M_PI)*sin(pos2.lon/180*M_PI)/ | ||
187 | (cos(pos1.lat/180*M_PI)+cos(pos2.lat/180*M_PI)*cos(pos2.lon/180*M_PI))); | ||
188 | res.lat = 180/M_PI * atan((sin(pos1.lat/180*M_PI)+sin(pos2.lat/180*M_PI))/ | ||
189 | sqrt(pow(cos(pos2.lat/180*M_PI)+cos(pos2.lat/180*M_PI)*cos(dlon),2)+ | ||
190 | pow(cos(pos2.lat/180*M_PI)*sin(dlon),2))); | ||
191 | |||
192 | char str1[32], str2[32]; | ||
193 | pos_lat_str(str1,sizeof(str1),res.lat); | ||
194 | pos_lon_str(str2,sizeof(str2),res.lon); | ||
195 | printf("pos = %s/%s\n", str1, str2); | ||
196 | } else { | ||
197 | gtk_label_set_text(GTK_LABEL(state->middle_lat), STR_NAN); | ||
198 | gtk_label_set_text(GTK_LABEL(state->middle_lon), STR_NAN); | ||
199 | } | ||
200 | #endif | ||
201 | } | ||
202 | |||
203 | static gint waypoint_changed_event(GtkWidget *widget, gpointer data ) { | ||
204 | math_dialog_state_t *state = (math_dialog_state_t*)data; | ||
205 | int wpt_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(widget)); | ||
206 | pos_t *pos = NULL; | ||
207 | |||
208 | if(wpt_idx < 0) | ||
209 | return FALSE; | ||
210 | |||
211 | if(wpt_idx == 0) | ||
212 | pos = gps_get_pos(state->appdata); | ||
213 | else if(wpt_idx == 1) { | ||
214 | harbaum | 198 | #ifdef ENABLE_OSM_GPS_MAP |
215 | if(!isnan(state->appdata->map.pos.lat) && | ||
216 | !isnan(state->appdata->map.pos.lon)) | ||
217 | pos = &state->appdata->map.pos; | ||
218 | #endif | ||
219 | } else if(wpt_idx == 2) { | ||
220 | harbaum | 1 | pos_t cache_pos = gpx_cache_pos(state->appdata->cur_cache); |
221 | pos = &cache_pos; | ||
222 | } else { | ||
223 | wpt_t *wpt = state->appdata->cur_cache->wpt; | ||
224 | harbaum | 198 | while(wpt_idx > 3) { |
225 | harbaum | 1 | g_assert(wpt != NULL); |
226 | wpt = wpt->next; | ||
227 | wpt_idx--; | ||
228 | } | ||
229 | pos = &wpt->pos; | ||
230 | } | ||
231 | |||
232 | if(pos) { | ||
233 | char str[32]; | ||
234 | pos_lat_str(str,sizeof(str),pos->lat); | ||
235 | gtk_entry_set_text(GTK_ENTRY(state->lat1), str); | ||
236 | pos_lon_str(str,sizeof(str),pos->lon); | ||
237 | gtk_entry_set_text(GTK_ENTRY(state->lon1), str); | ||
238 | } else { | ||
239 | gtk_entry_set_text(GTK_ENTRY(state->lat1), STR_NAN); | ||
240 | gtk_entry_set_text(GTK_ENTRY(state->lon1), STR_NAN); | ||
241 | } | ||
242 | |||
243 | mark(state->lat1, pos != NULL); | ||
244 | mark(state->lon1, pos != NULL); | ||
245 | |||
246 | return FALSE; | ||
247 | } | ||
248 | |||
249 | harbaum | 198 | static void callback_modified_lat(GtkWidget *widget, gpointer data ) { |
250 | float i = pos_parse_lat((char*)gtk_entry_get_text(GTK_ENTRY(widget))); | ||
251 | mark(widget, !isnan(i)); | ||
252 | } | ||
253 | |||
254 | harbaum | 1 | void geomath_dialog(appdata_t *appdata) { |
255 | static pos_t pos1 = { 0.0, 0.0 }, pos2 = { 0.0, 0.0 }; | ||
256 | static float dist1 = 0.0; | ||
257 | static float dir1 = 0.0; | ||
258 | static gboolean is_imperial = FALSE; | ||
259 | |||
260 | math_dialog_state_t state; | ||
261 | char str[32]; | ||
262 | |||
263 | /* this is quite ugly. It would be nice to run the entire system on */ | ||
264 | /* one specific system (e.g. metric) and only convert for in- and output */ | ||
265 | if(!appdata->imperial && is_imperial) | ||
266 | dist1 *= 6371.0/3959.0; /* we just switched to metric */ | ||
267 | if(appdata->imperial && !is_imperial) | ||
268 | dist1 *= 3959.0/6371.0; /* we just switched to imperial */ | ||
269 | is_imperial = appdata->imperial; | ||
270 | |||
271 | state.appdata = appdata; | ||
272 | |||
273 | #ifdef USE_MAEMO | ||
274 | if(appdata->cur_cache) | ||
275 | printf("current cache is %s\n", appdata->cur_cache->id); | ||
276 | else | ||
277 | printf("no current cache\n"); | ||
278 | #endif | ||
279 | |||
280 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Geomath"), | ||
281 | GTK_WINDOW(appdata->window), | ||
282 | // GTK_DIALOG_NO_SEPARATOR | | ||
283 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
284 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
285 | NULL); | ||
286 | |||
287 | #if defined(USE_MAEMO) && defined(HILDON_HELP) | ||
288 | hildon_help_dialog_help_enable(GTK_DIALOG(dialog), HELP_ID_GEOMATH, | ||
289 | appdata->osso_context); | ||
290 | #endif | ||
291 | |||
292 | gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT); | ||
293 | |||
294 | /* do geomath dialog */ | ||
295 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); | ||
296 | |||
297 | /* ------------------------- input area ------------------------- */ | ||
298 | GtkWidget *table = gtk_table_new(5, 3, FALSE); | ||
299 | gtk_table_set_col_spacing(GTK_TABLE(table), 1, 20); | ||
300 | |||
301 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Coordinate 1")), 1, 2, 0, 1); |
302 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Coordinate 2")), 2, 3, 0, 1); | ||
303 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Latitude:")), 0, 1, 1, 2); | ||
304 | harbaum | 1 | |
305 | GtkWidget *cbox = gtk_red_combo_box_entry_new_text(); | ||
306 | gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), _("GPS")); | ||
307 | harbaum | 198 | gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), _("Map")); |
308 | harbaum | 1 | |
309 | if(appdata->cur_cache) { | ||
310 | gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), appdata->cur_cache->id); | ||
311 | wpt_t *wpt = appdata->cur_cache->wpt; | ||
312 | while(wpt) { | ||
313 | gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), wpt->id); | ||
314 | wpt = wpt->next; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | state.lat1 = GTK_BIN(cbox)->child; | ||
319 | harbaum | 198 | g_signal_connect(G_OBJECT(state.lat1), "changed", |
320 | G_CALLBACK(callback_modified_lat), NULL); | ||
321 | harbaum | 1 | pos_lat_str(str, sizeof(str), pos1.lat); |
322 | gtk_entry_set_text(GTK_ENTRY(state.lat1), str); | ||
323 | |||
324 | gtk_signal_connect(GTK_OBJECT(cbox), "changed", | ||
325 | (GtkSignalFunc)waypoint_changed_event, &state); | ||
326 | gtk_table_attach_defaults(GTK_TABLE(table), cbox, 1,2,1,2); | ||
327 | |||
328 | |||
329 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), state.lat2 = lat_entry_new(pos2.lat), 2, 3, 1, 2); |
330 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Longitude:")), 0, 1, 2, 3); | ||
331 | gtk_table_attach_defaults(GTK_TABLE(table), state.lon1 = lon_entry_new(pos1.lon), 1, 2, 2, 3); | ||
332 | gtk_table_attach_defaults(GTK_TABLE(table), state.lon2 = lon_entry_new(pos2.lon), 2, 3, 2, 3); | ||
333 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Distance:")), 0, 1, 3, 4); | ||
334 | harbaum | 1 | gtk_table_attach_defaults(GTK_TABLE(table), state.dist1 = dist_entry_new(dist1, appdata->imperial), 1, 2, 3, 4); |
335 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Direction:")), 0, 1, 4, 5); |
336 | gtk_table_attach_defaults(GTK_TABLE(table), state.dir1 = gtk_dir_entry_new(dir1), 1, 2, 4, 5); | ||
337 | harbaum | 1 | |
338 | gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0); | ||
339 | |||
340 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0); | ||
341 | |||
342 | /* ------------------------- do-it-button ------------------------- */ | ||
343 | |||
344 | GtkWidget *button = gtk_button_new_with_label(_("Calculate!")); | ||
345 | harbaum | 198 | g_signal_connect(button, "clicked", (GCallback)on_calc_clicked, &state); |
346 | |||
347 | harbaum | 133 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) |
348 | hildon_gtk_widget_set_theme_size(button, | ||
349 | (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); | ||
350 | harbaum | 198 | gtk_table_attach(GTK_TABLE(table), button, 2,3,3,5, GTK_EXPAND, GTK_EXPAND, 0, 0); |
351 | #else | ||
352 | /* in non-maemo5 the button has its own row */ | ||
353 | hbox = gtk_hbox_new(FALSE, 0); | ||
354 | harbaum | 1 | gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0); |
355 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, FALSE, 0); | ||
356 | harbaum | 198 | #endif |
357 | harbaum | 1 | |
358 | /* ------------------------- output area ------------------------- */ | ||
359 | |||
360 | table = gtk_table_new(3, 3, FALSE); | ||
361 | |||
362 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Distance = ")), |
363 | harbaum | 1 | 0, 1, 0, 1); |
364 | gtk_table_attach_defaults(GTK_TABLE(table), state.distance = gtk_red_label_new(STR_NAN), | ||
365 | 1, 3, 0, 1); | ||
366 | |||
367 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Projection = ")), |
368 | harbaum | 1 | 0, 1, 1, 2); |
369 | gtk_table_attach_defaults(GTK_TABLE(table), state.proj_lat = gtk_red_label_new(STR_NAN), | ||
370 | 1, 2, 1, 2); | ||
371 | gtk_table_attach_defaults(GTK_TABLE(table), state.proj_lon = gtk_red_label_new(STR_NAN), | ||
372 | 2, 3, 1, 2); | ||
373 | #if 0 | ||
374 | harbaum | 198 | gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Middle = ")), |
375 | harbaum | 1 | 0, 1, 2, 3); |
376 | gtk_table_attach_defaults(GTK_TABLE(table), state.middle_lat = gtk_red_label_new(STR_NAN), | ||
377 | 1, 2, 2, 3); | ||
378 | gtk_table_attach_defaults(GTK_TABLE(table), state.middle_lon = gtk_red_label_new(STR_NAN), | ||
379 | 2, 3, 2, 3); | ||
380 | #endif | ||
381 | |||
382 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0); | ||
383 | |||
384 | |||
385 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); | ||
386 | |||
387 | gtk_widget_show_all(dialog); | ||
388 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
389 | |||
390 | /* copy values back to local static variables so they re-appear if */ | ||
391 | /* the dialog is re-opened, convert illegal values (NAN) to 0 */ | ||
392 | |||
393 | pos1.lat = lat_get(state.lat1); if(isnan(pos1.lat)) pos1.lat=0; | ||
394 | pos1.lon = lon_get(state.lon1); if(isnan(pos1.lon)) pos1.lon=0; | ||
395 | pos2.lat = lat_get(state.lat2); if(isnan(pos2.lat)) pos2.lat=0; | ||
396 | pos2.lon = lon_get(state.lon2); if(isnan(pos2.lon)) pos2.lon=0; | ||
397 | dist1 = distance_eval(state.dist1, &state); if(isnan(dist1)) dist1=0; | ||
398 | dir1 = direction_eval(state.dir1); if(isnan(dir1)) dir1=0; | ||
399 | |||
400 | gtk_widget_destroy(dialog); | ||
401 | } |