]>
Commit | Line | Data |
---|---|---|
92806042 GB |
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 | ||
92806042 GB |
36 | static GObjectClass *parent_class; |
37 | ||
38 | static void webtool_center_finalize ( GObject *gob ); | |
39 | ||
40 | static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ); | |
41 | static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow ); | |
42 | ||
43 | typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate; | |
44 | ||
45 | struct _VikWebtoolCenterPrivate | |
46 | { | |
47 | gchar *url; | |
48 | }; | |
49 | ||
50 | #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ | |
51 | VIK_WEBTOOL_CENTER_TYPE, \ | |
52 | VikWebtoolCenterPrivate)) | |
53 | ||
15044d72 | 54 | G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE) |
92806042 GB |
55 | |
56 | enum | |
57 | { | |
58 | PROP_0, | |
59 | ||
60 | PROP_URL, | |
61 | }; | |
62 | ||
63 | static void | |
64 | webtool_center_set_property (GObject *object, | |
65 | guint property_id, | |
66 | const GValue *value, | |
67 | GParamSpec *pspec) | |
68 | { | |
69 | VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); | |
70 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
71 | ||
72 | switch (property_id) | |
73 | { | |
74 | case PROP_URL: | |
75 | g_free (priv->url); | |
76 | priv->url = g_value_dup_string (value); | |
77 | g_debug ("VikWebtoolCenter.url: %s", priv->url); | |
78 | break; | |
79 | ||
80 | default: | |
81 | /* We don't have any other property... */ | |
82 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
83 | break; | |
84 | } | |
85 | } | |
86 | ||
87 | static void | |
88 | webtool_center_get_property (GObject *object, | |
89 | guint property_id, | |
90 | GValue *value, | |
91 | GParamSpec *pspec) | |
92 | { | |
93 | VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); | |
94 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
95 | ||
96 | switch (property_id) | |
97 | { | |
98 | case PROP_URL: | |
99 | g_value_set_string (value, priv->url); | |
100 | break; | |
101 | ||
102 | default: | |
103 | /* We don't have any other property... */ | |
104 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
105 | break; | |
106 | } | |
107 | } | |
108 | ||
15044d72 GB |
109 | static void |
110 | vik_webtool_center_class_init ( VikWebtoolCenterClass *klass ) | |
92806042 GB |
111 | { |
112 | GObjectClass *gobject_class; | |
113 | VikWebtoolClass *base_class; | |
114 | GParamSpec *pspec; | |
115 | ||
116 | gobject_class = G_OBJECT_CLASS (klass); | |
117 | ||
118 | gobject_class->finalize = webtool_center_finalize; | |
119 | gobject_class->set_property = webtool_center_set_property; | |
120 | gobject_class->get_property = webtool_center_get_property; | |
121 | ||
122 | pspec = g_param_spec_string ("url", | |
123 | "Template Url", | |
124 | "Set the template url", | |
125 | VIKING_URL /* default value */, | |
126 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
127 | g_object_class_install_property (gobject_class, | |
128 | PROP_URL, | |
129 | pspec); | |
130 | ||
131 | parent_class = g_type_class_peek_parent (klass); | |
132 | ||
133 | base_class = VIK_WEBTOOL_CLASS ( klass ); | |
134 | base_class->get_url = webtool_center_get_url; | |
135 | ||
136 | klass->mpp_to_zoom = webtool_center_mpp_to_zoom; | |
137 | ||
138 | g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate)); | |
139 | } | |
140 | ||
141 | VikWebtoolCenter *vik_webtool_center_new () | |
142 | { | |
143 | return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) ); | |
144 | } | |
145 | ||
146 | VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url ) | |
147 | { | |
148 | VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, | |
149 | "label", label, | |
150 | "url", url, | |
151 | NULL ) ); | |
152 | ||
153 | return result; | |
154 | } | |
155 | ||
15044d72 GB |
156 | static void |
157 | vik_webtool_center_init ( VikWebtoolCenter *self ) | |
92806042 GB |
158 | { |
159 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
160 | priv->url = NULL; | |
161 | } | |
162 | ||
163 | static void webtool_center_finalize ( GObject *gob ) | |
164 | { | |
165 | VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob ); | |
166 | g_free ( priv->url ); priv->url = NULL; | |
167 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
168 | } | |
169 | ||
170 | /* 1 << (x) is like a 2**(x) */ | |
171 | #define GZ(x) (1<<(x)) | |
172 | ||
173 | 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), | |
174 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
175 | ||
176 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
177 | ||
178 | #define ERROR_MARGIN 0.01 | |
179 | static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) { | |
180 | gint i; | |
181 | for ( i = 0; i < num_scales; i++ ) { | |
182 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { | |
183 | g_debug ( "webtool_center_mpp_to_zoom: %f -> %d", mpp, i ); | |
184 | return i; | |
185 | } | |
186 | } | |
201d843b RN |
187 | // Handle mpp smaller than 1 |
188 | // return a useful value such that '17 - this number' gives a natural number. | |
189 | // Ideally should return '-1' or '0.5' but that's tricky with an unsigned int type! | |
190 | // (i.e. should rework to support zoom levels of 18 or 19) | |
191 | return 0; | |
92806042 GB |
192 | } |
193 | ||
194 | static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow ) | |
195 | { | |
196 | VikWebtoolCenterPrivate *priv = NULL; | |
197 | VikViewport *viewport = NULL; | |
198 | const VikCoord *coord = NULL; | |
199 | guint8 zoom = 0; | |
200 | struct LatLon ll; | |
201 | gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE]; | |
202 | ||
203 | priv = WEBTOOL_CENTER_GET_PRIVATE (self); | |
204 | viewport = vik_window_viewport ( vwindow ); | |
205 | ||
206 | // Coords | |
207 | coord = vik_viewport_get_center ( viewport ); | |
208 | vik_coord_to_latlon ( coord, &ll ); | |
209 | ||
1d76b08b RN |
210 | // zoom - ideally x & y factors need to be the same otherwise use a default |
211 | if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) ) | |
212 | zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) ); | |
213 | else | |
214 | zoom = 1.0; | |
92806042 GB |
215 | |
216 | // Cannot simply use g_strdup_printf and gdouble due to locale. | |
217 | // As we compute an URL, we have to think in C locale. | |
218 | g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat); | |
219 | g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon); | |
220 | ||
221 | return g_strdup_printf ( priv->url, strlat, strlon, 17-zoom ); | |
222 | } | |
223 | ||
224 | guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp) | |
225 | { | |
226 | return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp ); | |
227 | } |