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