]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2008, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> | |
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 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
26 | #include "vikwebtoolcenter.h" | |
27 | ||
28 | #include <string.h> | |
29 | ||
30 | #include <glib.h> | |
31 | #include <glib/gi18n.h> | |
32 | ||
33 | #include "util.h" | |
34 | #include "globals.h" | |
35 | ||
36 | static void webtool_center_class_init ( VikWebtoolCenterClass *klass ); | |
37 | static void webtool_center_init ( VikWebtoolCenter *vwd ); | |
38 | ||
39 | static GObjectClass *parent_class; | |
40 | ||
41 | static void webtool_center_finalize ( GObject *gob ); | |
42 | ||
43 | static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ); | |
44 | static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow ); | |
45 | ||
46 | typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate; | |
47 | ||
48 | struct _VikWebtoolCenterPrivate | |
49 | { | |
50 | gchar *url; | |
51 | }; | |
52 | ||
53 | #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ | |
54 | VIK_WEBTOOL_CENTER_TYPE, \ | |
55 | VikWebtoolCenterPrivate)) | |
56 | ||
57 | GType vik_webtool_center_get_type() | |
58 | { | |
59 | static GType w_type = 0; | |
60 | ||
61 | if (!w_type) | |
62 | { | |
63 | static const GTypeInfo w_info = | |
64 | { | |
65 | sizeof (VikWebtoolCenterClass), | |
66 | NULL, /* base_init */ | |
67 | NULL, /* base_finalize */ | |
68 | (GClassInitFunc) webtool_center_class_init, | |
69 | NULL, /* class_finalize */ | |
70 | NULL, /* class_data */ | |
71 | sizeof (VikWebtoolCenter), | |
72 | 0, | |
73 | (GInstanceInitFunc) webtool_center_init, | |
74 | }; | |
75 | w_type = g_type_register_static ( VIK_WEBTOOL_TYPE, "VikWebtoolCenter", &w_info, 0 ); | |
76 | } | |
77 | ||
78 | return w_type; | |
79 | } | |
80 | ||
81 | enum | |
82 | { | |
83 | PROP_0, | |
84 | ||
85 | PROP_URL, | |
86 | }; | |
87 | ||
88 | static void | |
89 | webtool_center_set_property (GObject *object, | |
90 | guint property_id, | |
91 | const GValue *value, | |
92 | GParamSpec *pspec) | |
93 | { | |
94 | VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); | |
95 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
96 | ||
97 | switch (property_id) | |
98 | { | |
99 | case PROP_URL: | |
100 | g_free (priv->url); | |
101 | priv->url = g_value_dup_string (value); | |
102 | g_debug ("VikWebtoolCenter.url: %s", priv->url); | |
103 | break; | |
104 | ||
105 | default: | |
106 | /* We don't have any other property... */ | |
107 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
108 | break; | |
109 | } | |
110 | } | |
111 | ||
112 | static void | |
113 | webtool_center_get_property (GObject *object, | |
114 | guint property_id, | |
115 | GValue *value, | |
116 | GParamSpec *pspec) | |
117 | { | |
118 | VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); | |
119 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
120 | ||
121 | switch (property_id) | |
122 | { | |
123 | case PROP_URL: | |
124 | g_value_set_string (value, priv->url); | |
125 | break; | |
126 | ||
127 | default: | |
128 | /* We don't have any other property... */ | |
129 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
130 | break; | |
131 | } | |
132 | } | |
133 | ||
134 | static void webtool_center_class_init ( VikWebtoolCenterClass *klass ) | |
135 | { | |
136 | GObjectClass *gobject_class; | |
137 | VikWebtoolClass *base_class; | |
138 | GParamSpec *pspec; | |
139 | ||
140 | gobject_class = G_OBJECT_CLASS (klass); | |
141 | ||
142 | gobject_class->finalize = webtool_center_finalize; | |
143 | gobject_class->set_property = webtool_center_set_property; | |
144 | gobject_class->get_property = webtool_center_get_property; | |
145 | ||
146 | pspec = g_param_spec_string ("url", | |
147 | "Template Url", | |
148 | "Set the template url", | |
149 | VIKING_URL /* default value */, | |
150 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
151 | g_object_class_install_property (gobject_class, | |
152 | PROP_URL, | |
153 | pspec); | |
154 | ||
155 | parent_class = g_type_class_peek_parent (klass); | |
156 | ||
157 | base_class = VIK_WEBTOOL_CLASS ( klass ); | |
158 | base_class->get_url = webtool_center_get_url; | |
159 | ||
160 | klass->mpp_to_zoom = webtool_center_mpp_to_zoom; | |
161 | ||
162 | g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate)); | |
163 | } | |
164 | ||
165 | VikWebtoolCenter *vik_webtool_center_new () | |
166 | { | |
167 | return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) ); | |
168 | } | |
169 | ||
170 | VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url ) | |
171 | { | |
172 | VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, | |
173 | "label", label, | |
174 | "url", url, | |
175 | NULL ) ); | |
176 | ||
177 | return result; | |
178 | } | |
179 | ||
180 | static void webtool_center_init ( VikWebtoolCenter *self ) | |
181 | { | |
182 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
183 | priv->url = NULL; | |
184 | } | |
185 | ||
186 | static void webtool_center_finalize ( GObject *gob ) | |
187 | { | |
188 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob ); | |
189 | g_free ( priv->url ); priv->url = NULL; | |
190 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
191 | } | |
192 | ||
193 | /* 1 << (x) is like a 2**(x) */ | |
194 | #define GZ(x) (1<<(x)) | |
195 | ||
196 | static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9), | |
197 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
198 | ||
199 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
200 | ||
201 | #define ERROR_MARGIN 0.01 | |
202 | static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) { | |
203 | gint i; | |
204 | for ( i = 0; i < num_scales; i++ ) { | |
205 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { | |
206 | g_debug ( "webtool_center_mpp_to_zoom: %f -> %d", mpp, i ); | |
207 | return i; | |
208 | } | |
209 | } | |
210 | return 255; | |
211 | } | |
212 | ||
213 | static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow ) | |
214 | { | |
215 | VikWebtoolCenterPrivate *priv = NULL; | |
216 | VikViewport *viewport = NULL; | |
217 | const VikCoord *coord = NULL; | |
218 | guint8 zoom = 0; | |
219 | struct LatLon ll; | |
220 | gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE]; | |
221 | ||
222 | priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
223 | viewport = vik_window_viewport ( vwindow ); | |
224 | ||
225 | // Coords | |
226 | coord = vik_viewport_get_center ( viewport ); | |
227 | vik_coord_to_latlon ( coord, &ll ); | |
228 | ||
229 | // zoom - ideally x & y factors need to be the same otherwise use a default | |
230 | if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) ) | |
231 | zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) ); | |
232 | else | |
233 | zoom = 1.0; | |
234 | ||
235 | // Cannot simply use g_strdup_printf and gdouble due to locale. | |
236 | // As we compute an URL, we have to think in C locale. | |
237 | g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat); | |
238 | g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon); | |
239 | ||
240 | return g_strdup_printf ( priv->url, strlat, strlon, 17-zoom ); | |
241 | } | |
242 | ||
243 | guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp) | |
244 | { | |
245 | return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp ); | |
246 | } |