]>
Commit | Line | Data |
---|---|---|
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 | */ | |
4c77d5e0 GB |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
8c00358d GB |
24 | |
25 | #ifdef HAVE_MATH_H | |
80e450c0 | 26 | #include <math.h> |
8c00358d | 27 | #endif |
4c77d5e0 | 28 | #include <glib/gi18n.h> |
50a14534 EB |
29 | |
30 | #include "viking.h" | |
5bfafde9 | 31 | #include "icons/icons.h" |
50a14534 | 32 | |
0a6cab71 AF |
33 | static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len ); |
34 | static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ); | |
50a14534 EB |
35 | static gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp ); |
36 | static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id ); | |
37 | static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color ); | |
07059501 | 38 | static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file ); |
50a14534 | 39 | |
ad0a8c2d | 40 | static VikLayerParamScale param_scales[] = { |
50a14534 EB |
41 | { 0.05, 60.0, 0.25, 10 }, |
42 | { 1, 10, 1, 0 }, | |
43 | }; | |
44 | ||
ad0a8c2d | 45 | static VikLayerParam coord_layer_params[] = { |
ac68cea3 | 46 | { "color", VIK_LAYER_PARAM_COLOR, VIK_LAYER_GROUP_NONE, N_("Color:"), VIK_LAYER_WIDGET_COLOR, 0 }, |
4c77d5e0 GB |
47 | { "min_inc", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Minutes Width:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 }, |
48 | { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Line Thickness:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 }, | |
50a14534 EB |
49 | }; |
50 | ||
51 | ||
52 | enum { PARAM_COLOR = 0, PARAM_MIN_INC, PARAM_LINE_THICKNESS, NUM_PARAMS }; | |
53 | ||
54 | VikLayerInterface vik_coord_layer_interface = { | |
55 | "Coord", | |
5bfafde9 | 56 | &vikcoordlayer_pixbuf, |
50a14534 EB |
57 | |
58 | NULL, | |
59 | 0, | |
60 | ||
61 | coord_layer_params, | |
62 | NUM_PARAMS, | |
63 | NULL, | |
64 | 0, | |
65 | ||
5a4a28bf QT |
66 | VIK_MENU_ITEM_ALL, |
67 | ||
50a14534 EB |
68 | (VikLayerFuncCreate) vik_coord_layer_create, |
69 | (VikLayerFuncRealize) NULL, | |
b112cbf5 | 70 | (VikLayerFuncPostRead) coord_layer_post_read, |
50a14534 EB |
71 | (VikLayerFuncFree) vik_coord_layer_free, |
72 | ||
73 | (VikLayerFuncProperties) NULL, | |
74 | (VikLayerFuncDraw) vik_coord_layer_draw, | |
75 | (VikLayerFuncChangeCoordMode) NULL, | |
76 | ||
20c7a3a0 QT |
77 | (VikLayerFuncSetMenuItemsSelection) NULL, |
78 | (VikLayerFuncGetMenuItemsSelection) NULL, | |
79 | ||
50a14534 EB |
80 | (VikLayerFuncAddMenuItems) NULL, |
81 | (VikLayerFuncSublayerAddMenuItems) NULL, | |
82 | ||
83 | (VikLayerFuncSublayerRenameRequest) NULL, | |
84 | (VikLayerFuncSublayerToggleVisible) NULL, | |
85 | ||
0a6cab71 AF |
86 | (VikLayerFuncMarshall) coord_layer_marshall, |
87 | (VikLayerFuncUnmarshall) coord_layer_unmarshall, | |
50a14534 EB |
88 | |
89 | (VikLayerFuncSetParam) coord_layer_set_param, | |
90 | (VikLayerFuncGetParam) coord_layer_get_param, | |
91 | ||
92 | (VikLayerFuncReadFileData) NULL, | |
93 | (VikLayerFuncWriteFileData) NULL, | |
94 | ||
33534cd8 | 95 | (VikLayerFuncDeleteItem) NULL, |
50a14534 EB |
96 | (VikLayerFuncCopyItem) NULL, |
97 | (VikLayerFuncPasteItem) NULL, | |
98 | (VikLayerFuncFreeCopiedItem) NULL, | |
70a23263 | 99 | (VikLayerFuncDragDropRequest) NULL, |
50a14534 EB |
100 | }; |
101 | ||
102 | struct _VikCoordLayer { | |
103 | VikLayer vl; | |
104 | GdkGC *gc; | |
105 | gdouble deg_inc; | |
106 | guint8 line_thickness; | |
ac68cea3 | 107 | GdkColor *color; |
50a14534 EB |
108 | }; |
109 | ||
110 | GType vik_coord_layer_get_type () | |
111 | { | |
112 | static GType vcl_type = 0; | |
113 | ||
114 | if (!vcl_type) | |
115 | { | |
116 | static const GTypeInfo vcl_info = | |
117 | { | |
118 | sizeof (VikCoordLayerClass), | |
119 | NULL, /* base_init */ | |
120 | NULL, /* base_finalize */ | |
121 | NULL, /* class init */ | |
122 | NULL, /* class_finalize */ | |
123 | NULL, /* class_data */ | |
124 | sizeof (VikCoordLayer), | |
125 | 0, | |
126 | NULL /* instance init */ | |
127 | }; | |
128 | vcl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikCoordLayer", &vcl_info, 0 ); | |
129 | } | |
130 | ||
131 | return vcl_type; | |
132 | } | |
133 | ||
0a6cab71 AF |
134 | static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len ) |
135 | { | |
136 | vik_layer_marshall_params ( VIK_LAYER(vcl), data, len ); | |
137 | } | |
138 | ||
139 | static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ) | |
140 | { | |
141 | VikCoordLayer *rv = vik_coord_layer_new ( vvp ); | |
142 | vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp ); | |
143 | return rv; | |
144 | } | |
145 | ||
50a14534 EB |
146 | gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp ) |
147 | { | |
148 | switch ( id ) | |
149 | { | |
ac68cea3 | 150 | case PARAM_COLOR: if ( vcl->color ) gdk_color_free ( vcl->color ); vcl->color = gdk_color_copy( &(data.c)); break; |
50a14534 EB |
151 | case PARAM_MIN_INC: vcl->deg_inc = data.d / 60.0; break; |
152 | case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vcl->line_thickness = data.u; break; | |
153 | } | |
154 | return TRUE; | |
155 | } | |
156 | ||
157 | static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id ) | |
158 | { | |
159 | VikLayerParamData rv; | |
160 | switch ( id ) | |
161 | { | |
ac68cea3 MA |
162 | case PARAM_COLOR: |
163 | if (vcl->color) | |
164 | { | |
165 | rv.c.pixel = vcl->color->pixel; | |
166 | rv.c.red = vcl->color->red; | |
167 | rv.c.green = vcl->color->green; | |
168 | rv.c.blue = vcl->color->blue; | |
169 | } | |
170 | break; | |
50a14534 EB |
171 | case PARAM_MIN_INC: rv.d = vcl->deg_inc * 60.0; break; |
172 | case PARAM_LINE_THICKNESS: rv.i = vcl->line_thickness; break; | |
173 | } | |
174 | return rv; | |
175 | } | |
176 | ||
07059501 | 177 | static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file ) |
50a14534 | 178 | { |
94933cb8 | 179 | VikCoordLayer *vcl = VIK_COORD_LAYER(vl); |
50a14534 EB |
180 | if ( vcl->gc ) |
181 | g_object_unref ( G_OBJECT(vcl->gc) ); | |
182 | ||
ac68cea3 MA |
183 | vcl->gc = vik_viewport_new_gc_from_color ( vp, vcl->color, vcl->line_thickness ); |
184 | ||
50a14534 EB |
185 | } |
186 | ||
187 | VikCoordLayer *vik_coord_layer_new ( ) | |
188 | { | |
ac68cea3 MA |
189 | GdkColor InitColor; |
190 | ||
50a14534 EB |
191 | VikCoordLayer *vcl = VIK_COORD_LAYER ( g_object_new ( VIK_COORD_LAYER_TYPE, NULL ) ); |
192 | vik_layer_init ( VIK_LAYER(vcl), VIK_LAYER_COORD ); | |
193 | ||
ac68cea3 | 194 | InitColor.pixel = 0; |
3750ef3e GB |
195 | InitColor.red = 65535; |
196 | InitColor.green = 65535; | |
197 | InitColor.blue = 65535; | |
ac68cea3 | 198 | |
50a14534 EB |
199 | vcl->gc = NULL; |
200 | vcl->deg_inc = 1.0/60.0; | |
201 | vcl->line_thickness = 3; | |
ac68cea3 | 202 | vcl->color = gdk_color_copy (&InitColor); |
50a14534 EB |
203 | return vcl; |
204 | } | |
205 | ||
206 | void vik_coord_layer_draw ( VikCoordLayer *vcl, gpointer data ) | |
207 | { | |
208 | VikViewport *vp = (VikViewport *) data; | |
80e450c0 AF |
209 | |
210 | if ( !vcl->gc ) { | |
50a14534 | 211 | return; |
80e450c0 AF |
212 | } |
213 | ||
214 | if ( vik_viewport_get_coord_mode(vp) != VIK_COORD_UTM ) | |
215 | { | |
216 | VikCoord left, right, left2, right2; | |
217 | gdouble l, r, i, j; | |
218 | gint x1, y1, x2, y2, smod = 1, mmod = 1; | |
219 | gboolean mins = FALSE, secs = FALSE; | |
ac68cea3 MA |
220 | GdkGC *dgc = vik_viewport_new_gc_from_color(vp, vcl->color, vcl->line_thickness); |
221 | GdkGC *mgc = vik_viewport_new_gc_from_color(vp, vcl->color, MAX(vcl->line_thickness/2, 1)); | |
222 | GdkGC *sgc = vik_viewport_new_gc_from_color(vp, vcl->color, MAX(vcl->line_thickness/5, 1)); | |
80e450c0 AF |
223 | |
224 | vik_viewport_screen_to_coord ( vp, 0, 0, &left ); | |
225 | vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &right ); | |
226 | vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &left2 ); | |
227 | vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &right2 ); | |
228 | ||
229 | #define CLINE(gc, c1, c2) { \ | |
230 | vik_viewport_coord_to_screen(vp, (c1), &x1, &y1); \ | |
231 | vik_viewport_coord_to_screen(vp, (c2), &x2, &y2); \ | |
232 | vik_viewport_draw_line (vp, (gc), x1, y1, x2, y2); \ | |
233 | } | |
234 | ||
235 | l = left.east_west; | |
236 | r = right.east_west; | |
237 | if (60*fabs(l-r) < 4) { | |
238 | secs = TRUE; | |
239 | smod = MIN(6, (int)ceil(3600*fabs(l-r)/30.0)); | |
240 | } | |
241 | if (fabs(l-r) < 4) { | |
242 | mins = TRUE; | |
243 | mmod = MIN(6, (int)ceil(60*fabs(l-r)/30.0)); | |
244 | } | |
245 | for (i=floor(l*60); i<ceil(r*60); i+=1.0) { | |
246 | if (secs) { | |
247 | for (j=i*60+1; j<(i+1)*60; j+=1.0) { | |
248 | left.east_west = j/3600.0; | |
249 | left2.east_west = j/3600.0; | |
250 | if ((int)j % smod == 0) CLINE(sgc, &left, &left2); | |
251 | } | |
252 | } | |
253 | if (mins) { | |
254 | left.east_west = i/60.0; | |
255 | left2.east_west = i/60.0; | |
256 | if ((int)i % mmod == 0) CLINE(mgc, &left, &left2); | |
257 | } | |
258 | if ((int)i % 60 == 0) { | |
259 | left.east_west = i/60.0; | |
260 | left2.east_west = i/60.0; | |
261 | CLINE(dgc, &left, &left2); | |
262 | } | |
263 | } | |
264 | ||
265 | vik_viewport_screen_to_coord ( vp, 0, 0, &left ); | |
266 | l = left2.north_south; | |
267 | r = left.north_south; | |
268 | for (i=floor(l*60); i<ceil(r*60); i+=1.0) { | |
269 | if (secs) { | |
270 | for (j=i*60+1; j<(i+1)*60; j+=1.0) { | |
271 | left.north_south = j/3600.0; | |
272 | right.north_south = j/3600.0; | |
273 | if ((int)j % smod == 0) CLINE(sgc, &left, &right); | |
274 | } | |
275 | } | |
276 | if (mins) { | |
277 | left.north_south = i/60.0; | |
278 | right.north_south = i/60.0; | |
279 | if ((int)i % mmod == 0) CLINE(mgc, &left, &right); | |
280 | } | |
281 | if ((int)i % 60 == 0) { | |
282 | left.north_south = i/60.0; | |
283 | right.north_south = i/60.0; | |
284 | CLINE(dgc, &left, &right); | |
285 | } | |
286 | } | |
287 | #undef CLINE | |
288 | g_object_unref(dgc); | |
289 | g_object_unref(sgc); | |
290 | g_object_unref(mgc); | |
291 | return; | |
292 | } | |
293 | ||
294 | if ( vik_viewport_get_coord_mode(vp) == VIK_COORD_UTM ) | |
50a14534 EB |
295 | { |
296 | const struct UTM *center = (const struct UTM *)vik_viewport_get_center ( vp ); | |
297 | gdouble xmpp = vik_viewport_get_xmpp ( vp ), ympp = vik_viewport_get_ympp ( vp ); | |
298 | guint16 width = vik_viewport_get_width ( vp ), height = vik_viewport_get_height ( vp ); | |
299 | struct LatLon ll, ll2, min, max; | |
300 | double lon; | |
301 | int x1, x2; | |
302 | struct UTM utm; | |
303 | ||
304 | utm = *center; | |
305 | utm.northing = center->northing - ( ympp * height / 2 ); | |
306 | ||
307 | a_coords_utm_to_latlon ( &utm, &ll ); | |
308 | ||
309 | utm.northing = center->northing + ( ympp * height / 2 ); | |
310 | ||
311 | a_coords_utm_to_latlon ( &utm, &ll2 ); | |
312 | ||
313 | { | |
314 | /* find corner coords in lat/lon. | |
315 | start at whichever is less: top or bottom left lon. goto whichever more: top or bottom right lon | |
316 | */ | |
317 | struct LatLon topleft, topright, bottomleft, bottomright; | |
318 | struct UTM temp_utm; | |
319 | temp_utm = *center; | |
320 | temp_utm.easting -= (width/2)*xmpp; | |
321 | temp_utm.northing += (height/2)*ympp; | |
322 | a_coords_utm_to_latlon ( &temp_utm, &topleft ); | |
323 | temp_utm.easting += (width*xmpp); | |
324 | a_coords_utm_to_latlon ( &temp_utm, &topright ); | |
325 | temp_utm.northing -= (height*ympp); | |
326 | a_coords_utm_to_latlon ( &temp_utm, &bottomright ); | |
327 | temp_utm.easting -= (width*xmpp); | |
328 | a_coords_utm_to_latlon ( &temp_utm, &bottomleft ); | |
329 | min.lon = (topleft.lon < bottomleft.lon) ? topleft.lon : bottomleft.lon; | |
330 | max.lon = (topright.lon > bottomright.lon) ? topright.lon : bottomright.lon; | |
331 | min.lat = (bottomleft.lat < bottomright.lat) ? bottomleft.lat : bottomright.lat; | |
332 | max.lat = (topleft.lat > topright.lat) ? topleft.lat : topright.lat; | |
333 | } | |
334 | ||
5b1d90a5 RN |
335 | /* Can zoom out more than whole world and so the above can give invalid positions */ |
336 | /* Restrict values properly so drawing doesn't go into a near 'infinite' loop */ | |
337 | if ( min.lon < -180.0 ) | |
338 | min.lon = -180.0; | |
339 | if ( max.lon > 180.0 ) | |
340 | max.lon = 180.0; | |
341 | if ( min.lat < -90.0 ) | |
342 | min.lat = -90.0; | |
343 | if ( max.lat > 90.0 ) | |
344 | max.lat = 90.0; | |
345 | ||
50a14534 EB |
346 | lon = ((double) ((long) ((min.lon)/ vcl->deg_inc))) * vcl->deg_inc; |
347 | ll.lon = ll2.lon = lon; | |
348 | ||
349 | for (; ll.lon <= max.lon; ll.lon+=vcl->deg_inc, ll2.lon+=vcl->deg_inc ) | |
350 | { | |
351 | a_coords_latlon_to_utm ( &ll, &utm ); | |
352 | x1 = ( (utm.easting - center->easting) / xmpp ) + (width / 2); | |
353 | a_coords_latlon_to_utm ( &ll2, &utm ); | |
354 | x2 = ( (utm.easting - center->easting) / xmpp ) + (width / 2); | |
355 | vik_viewport_draw_line (vp, vcl->gc, x1, height, x2, 0); | |
356 | } | |
357 | ||
358 | utm = *center; | |
359 | utm.easting = center->easting - ( xmpp * width / 2 ); | |
360 | ||
361 | a_coords_utm_to_latlon ( &utm, &ll ); | |
362 | ||
363 | utm.easting = center->easting + ( xmpp * width / 2 ); | |
364 | ||
365 | a_coords_utm_to_latlon ( &utm, &ll2 ); | |
366 | ||
367 | /* really lat, just reusing a variable */ | |
368 | lon = ((double) ((long) ((min.lat)/ vcl->deg_inc))) * vcl->deg_inc; | |
369 | ll.lat = ll2.lat = lon; | |
370 | ||
371 | for (; ll.lat <= max.lat ; ll.lat+=vcl->deg_inc, ll2.lat+=vcl->deg_inc ) | |
372 | { | |
373 | a_coords_latlon_to_utm ( &ll, &utm ); | |
374 | x1 = (height / 2) - ( (utm.northing - center->northing) / ympp ); | |
375 | a_coords_latlon_to_utm ( &ll2, &utm ); | |
376 | x2 = (height / 2) - ( (utm.northing - center->northing) / ympp ); | |
377 | vik_viewport_draw_line (vp, vcl->gc, width, x2, 0, x1); | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | void vik_coord_layer_free ( VikCoordLayer *vcl ) | |
383 | { | |
384 | if ( vcl->gc != NULL ) | |
385 | g_object_unref ( G_OBJECT(vcl->gc) ); | |
386 | ||
387 | if ( vcl->color != NULL ) | |
ac68cea3 | 388 | gdk_color_free ( vcl->color ); |
50a14534 EB |
389 | } |
390 | ||
391 | static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color ) | |
392 | { | |
ac68cea3 MA |
393 | GdkColor InitColor; |
394 | ||
50a14534 | 395 | if ( vcl->color ) |
ac68cea3 | 396 | gdk_color_free ( vcl->color ); |
50a14534 | 397 | |
ac68cea3 MA |
398 | gdk_color_parse( color, &InitColor); |
399 | vcl->color = gdk_color_copy( &InitColor ); | |
50a14534 EB |
400 | |
401 | if ( vcl->gc ) | |
402 | g_object_unref ( G_OBJECT(vcl->gc) ); | |
403 | ||
ac68cea3 | 404 | vcl->gc = vik_viewport_new_gc_from_color ( vp, vcl->color, vcl->line_thickness ); |
50a14534 EB |
405 | } |
406 | ||
407 | VikCoordLayer *vik_coord_layer_create ( VikViewport *vp ) | |
408 | { | |
409 | VikCoordLayer *vcl = vik_coord_layer_new (); | |
410 | coord_layer_update_gc ( vcl, vp, "red" ); | |
411 | return vcl; | |
412 | } | |
413 |