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