2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2008, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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
26 #include "vikwebtoolcenter.h"
31 #include <glib/gi18n.h>
36 static void webtool_center_class_init ( VikWebtoolCenterClass
*klass
);
37 static void webtool_center_init ( VikWebtoolCenter
*vwd
);
39 static GObjectClass
*parent_class
;
41 static void webtool_center_finalize ( GObject
*gob
);
43 static guint8
webtool_center_mpp_to_zoom ( VikWebtool
*self
, gdouble mpp
);
44 static gchar
*webtool_center_get_url ( VikWebtool
*vw
, VikWindow
*vwindow
);
46 typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate
;
48 struct _VikWebtoolCenterPrivate
53 #define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
54 VIK_WEBTOOL_CENTER_TYPE, \
55 VikWebtoolCenterPrivate))
57 GType
vik_webtool_center_get_type()
59 static GType w_type
= 0;
63 static const GTypeInfo w_info
=
65 sizeof (VikWebtoolCenterClass
),
67 NULL
, /* base_finalize */
68 (GClassInitFunc
) webtool_center_class_init
,
69 NULL
, /* class_finalize */
70 NULL
, /* class_data */
71 sizeof (VikWebtoolCenter
),
73 (GInstanceInitFunc
) webtool_center_init
,
75 w_type
= g_type_register_static ( VIK_WEBTOOL_TYPE
, "VikWebtoolCenter", &w_info
, 0 );
89 webtool_center_set_property (GObject
*object
,
94 VikWebtoolCenter
*self
= VIK_WEBTOOL_CENTER (object
);
95 VikWebtoolCenterPrivate
*priv
= WEBTOOL_CENTER_GET_PRIVATE (self
);
101 priv
->url
= g_value_dup_string (value
);
102 g_debug ("VikWebtoolCenter.url: %s", priv
->url
);
106 /* We don't have any other property... */
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
113 webtool_center_get_property (GObject
*object
,
118 VikWebtoolCenter
*self
= VIK_WEBTOOL_CENTER (object
);
119 VikWebtoolCenterPrivate
*priv
= WEBTOOL_CENTER_GET_PRIVATE (self
);
124 g_value_set_string (value
, priv
->url
);
128 /* We don't have any other property... */
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
134 static void webtool_center_class_init ( VikWebtoolCenterClass
*klass
)
136 GObjectClass
*gobject_class
;
137 VikWebtoolClass
*base_class
;
140 gobject_class
= G_OBJECT_CLASS (klass
);
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
;
146 pspec
= g_param_spec_string ("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
,
155 parent_class
= g_type_class_peek_parent (klass
);
157 base_class
= VIK_WEBTOOL_CLASS ( klass
);
158 base_class
->get_url
= webtool_center_get_url
;
160 klass
->mpp_to_zoom
= webtool_center_mpp_to_zoom
;
162 g_type_class_add_private (klass
, sizeof (VikWebtoolCenterPrivate
));
165 VikWebtoolCenter
*vik_webtool_center_new ()
167 return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE
, NULL
) );
170 VikWebtoolCenter
*vik_webtool_center_new_with_members ( const gchar
*label
, const gchar
*url
)
172 VikWebtoolCenter
*result
= VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE
,
180 static void webtool_center_init ( VikWebtoolCenter
*self
)
182 VikWebtoolCenterPrivate
*priv
= WEBTOOL_CENTER_GET_PRIVATE (self
);
186 static void webtool_center_finalize ( GObject
*gob
)
188 VikWebtoolCenterPrivate
*priv
= WEBTOOL_CENTER_GET_PRIVATE ( gob
);
189 g_free ( priv
->url
); priv
->url
= NULL
;
190 G_OBJECT_CLASS(parent_class
)->finalize(gob
);
193 /* 1 << (x) is like a 2**(x) */
194 #define GZ(x) (1<<(x))
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) };
199 static const gint num_scales
= (sizeof(scale_mpps
) / sizeof(scale_mpps
[0]));
201 #define ERROR_MARGIN 0.01
202 static guint8
webtool_center_mpp_to_zoom ( VikWebtool
*self
, gdouble mpp
) {
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
);
213 static gchar
*webtool_center_get_url ( VikWebtool
*self
, VikWindow
*vwindow
)
215 VikWebtoolCenterPrivate
*priv
= NULL
;
216 VikViewport
*viewport
= NULL
;
217 const VikCoord
*coord
= NULL
;
220 gchar strlat
[G_ASCII_DTOSTR_BUF_SIZE
], strlon
[G_ASCII_DTOSTR_BUF_SIZE
];
222 priv
= WEBTOOL_CENTER_GET_PRIVATE (self
);
223 viewport
= vik_window_viewport ( vwindow
);
226 coord
= vik_viewport_get_center ( viewport
);
227 vik_coord_to_latlon ( coord
, &ll
);
230 g_assert ( vik_viewport_get_xmpp ( viewport
) == vik_viewport_get_ympp ( viewport
) );
231 zoom
= vik_webtool_center_mpp_to_zoom ( self
, vik_viewport_get_zoom ( viewport
) );
233 // Cannot simply use g_strdup_printf and gdouble due to locale.
234 // As we compute an URL, we have to think in C locale.
235 g_ascii_dtostr (strlat
, G_ASCII_DTOSTR_BUF_SIZE
, ll
.lat
);
236 g_ascii_dtostr (strlon
, G_ASCII_DTOSTR_BUF_SIZE
, ll
.lon
);
238 return g_strdup_printf ( priv
->url
, strlat
, strlon
, 17-zoom
);
241 guint8
vik_webtool_center_mpp_to_zoom (VikWebtool
*self
, gdouble mpp
)
243 return VIK_WEBTOOL_CENTER_GET_CLASS( self
)->mpp_to_zoom( self
, mpp
);