]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikwebtoolcenter.c
Extract a UI module for babel
[andy/viking.git] / src / vikwebtoolcenter.c
index 7ba2ad772b12cf51f520f40cc7e4a3ab3ba5ff07..3cef2ba44d28b908e97367c33f548f725bcea734 100644 (file)
@@ -32,9 +32,7 @@
 
 #include "util.h"
 #include "globals.h"
-
-static void webtool_center_class_init ( VikWebtoolCenterClass *klass );
-static void webtool_center_init ( VikWebtoolCenter *vwd );
+#include "maputils.h"
 
 static GObjectClass *parent_class;
 
@@ -54,29 +52,7 @@ struct _VikWebtoolCenterPrivate
                                        VIK_WEBTOOL_CENTER_TYPE,          \
                                        VikWebtoolCenterPrivate))
 
-GType vik_webtool_center_get_type()
-{
-  static GType w_type = 0;
-
-  if (!w_type)
-  {
-    static const GTypeInfo w_info = 
-    {
-      sizeof (VikWebtoolCenterClass),
-      NULL, /* base_init */
-      NULL, /* base_finalize */
-      (GClassInitFunc) webtool_center_class_init,
-      NULL, /* class_finalize */
-      NULL, /* class_data */
-      sizeof (VikWebtoolCenter),
-      0,
-      (GInstanceInitFunc) webtool_center_init,
-    };
-    w_type = g_type_register_static ( VIK_WEBTOOL_TYPE, "VikWebtoolCenter", &w_info, 0 );
-  }
-
-  return w_type;
-}
+G_DEFINE_TYPE (VikWebtoolCenter, vik_webtool_center, VIK_WEBTOOL_TYPE)
 
 enum
 {
@@ -131,7 +107,8 @@ webtool_center_get_property (GObject    *object,
     }
 }
 
-static void webtool_center_class_init ( VikWebtoolCenterClass *klass )
+static void
+vik_webtool_center_class_init ( VikWebtoolCenterClass *klass )
 {
   GObjectClass *gobject_class;
   VikWebtoolClass *base_class;
@@ -177,7 +154,8 @@ VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, cons
   return result;
 }
 
-static void webtool_center_init ( VikWebtoolCenter *self )
+static void
+vik_webtool_center_init ( VikWebtoolCenter *self )
 {
   VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self);
   priv->url = NULL;
@@ -190,28 +168,8 @@ static void webtool_center_finalize ( GObject *gob )
   G_OBJECT_CLASS(parent_class)->finalize(gob);
 }
 
-/* 1 << (x) is like a 2**(x) */
-#define GZ(x) (1<<(x))
-
-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),
-                                      GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
-
-static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
-
-#define ERROR_MARGIN 0.01
 static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) {
-  gint i;
-  for ( i = 0; i < num_scales; i++ ) {
-    if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
-      g_debug ( "webtool_center_mpp_to_zoom: %f -> %d", mpp, i );
-      return i;
-    }
-  }
-  // Handle mpp smaller than 1
-  // return a useful value such that '17 - this number' gives a natural number.
-  // Ideally should return '-1' or '0.5' but that's tricky with an unsigned int type!
-  // (i.e. should rework to support zoom levels of 18 or 19)
-  return 0;
+  return map_utils_mpp_to_zoom_level ( mpp );
 }
 
 static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
@@ -219,7 +177,7 @@ static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
   VikWebtoolCenterPrivate *priv = NULL;
   VikViewport *viewport = NULL;
   const VikCoord *coord = NULL;
-  guint8 zoom = 0;
+  guint8 zoom = 17;
   struct LatLon ll;
   gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE];
 
@@ -230,18 +188,16 @@ static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow )
   coord = vik_viewport_get_center ( viewport );
   vik_coord_to_latlon ( coord, &ll );
 
-  // zoom - ideally x & y factors need to be the same otherwise use a default
+  // zoom - ideally x & y factors need to be the same otherwise use the default
   if ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) )
     zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) );
-  else
-    zoom = 1.0;
 
   // Cannot simply use g_strdup_printf and gdouble due to locale.
   // As we compute an URL, we have to think in C locale.
   g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat);
   g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon);
 
-  return g_strdup_printf ( priv->url, strlat, strlon, 17-zoom );
+  return g_strdup_printf ( priv->url, strlat, strlon, zoom );
 }
 
 guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp)