[libchamplain/libchamplain-0-4] Add Miles unites



commit b3a3a731588ac506abc1439c901c3d81b7369ef1
Author: Pierre-Luc Beaudoin <pierre-luc pierlux com>
Date:   Tue Dec 15 14:58:11 2009 -0500

    Add Miles unites

 champlain/champlain-defines.h |    6 ++
 champlain/champlain-view.c    |  111 +++++++++++++++++++++++++++++++++--------
 champlain/champlain-view.h    |    2 +
 3 files changed, 98 insertions(+), 21 deletions(-)
---
diff --git a/champlain/champlain-defines.h b/champlain/champlain-defines.h
index 54056b7..8a279a5 100644
--- a/champlain/champlain-defines.h
+++ b/champlain/champlain-defines.h
@@ -47,4 +47,10 @@ typedef enum
   CHAMPLAIN_STATE_DONE
 } ChamplainState;
 
+typedef enum
+{
+  CHAMPLAIN_UNIT_KM,
+  CHAMPLAIN_UNIT_MILES,
+} ChamplainUnit;
+
 #endif
diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c
index e92af53..930ea64 100644
--- a/champlain/champlain-view.c
+++ b/champlain/champlain-view.c
@@ -159,6 +159,7 @@ struct _ChamplainViewPrivate
 
   ClutterActor *scale_actor;
   gboolean show_scale;
+  ChamplainUnit scale_unit;
 
   ChamplainState state; /* View's global state */
 
@@ -1003,7 +1004,7 @@ button_release_cb (ClutterActor *actor,
 static void
 update_scale (ChamplainView *view)
 {
-  gboolean is_small_unit = FALSE;  /* indicates if using meters */
+  gboolean is_small_unit = TRUE;  /* indicates if using meters */
   ClutterActor *text, *line;
   gfloat width;
   ChamplainViewPrivate *priv = view->priv;
@@ -1011,10 +1012,11 @@ update_scale (ChamplainView *view)
   gfloat scale_width = SCALE_WIDTH;
   gchar *label;
   cairo_t *cr;
-  gint base;
+  gfloat base;
   gfloat factor;
+  gboolean final_unit = FALSE;
 
-  if (! priv || !priv->map || !priv->map->current_level)
+  if (!priv || !priv->map || !priv->map->current_level)
     return;
 
   if (priv->show_scale)
@@ -1030,32 +1032,60 @@ update_scale (ChamplainView *view)
   m_per_pixel = champlain_map_source_get_meters_per_pixel (priv->map_source,
       priv->zoom_level, priv->latitude, priv->longitude);
 
-  /* Keep the closest power of 10 */
-  base = floor (log (m_per_pixel * scale_width) / log (10));
-  base = pow (10, base);
+  if (priv->scale_unit == CHAMPLAIN_UNIT_MILES)
+    m_per_pixel *= 3.28; /* m_per_pixel is now in ft */
 
-  /* How many times can it be fitted in our max scale width */
-  scale_width /= m_per_pixel * scale_width / base;
-  factor = floor (SCALE_WIDTH / scale_width);
-  base *= factor;
-  scale_width *= factor;
-
-  if (base / 1000 > 1)
+  /* This loop will find the pretty value to display on the scale.
+   * It will be run once for metric units, and twice for imperials
+   * so that both feet and miles have pretty numbers.
+   */
+  do
     {
-      base /= 1000;
-      is_small_unit = FALSE;
+      /* Keep the previous power of 10 */
+      base = floor (log (m_per_pixel * scale_width) / log (10));
+      base = pow (10, base);
+
+      /* How many times can it be fitted in our max scale width */
+      scale_width /= m_per_pixel * scale_width / base;
+      factor = floor (SCALE_WIDTH / scale_width);
+      base *= factor;
+      scale_width *= factor;
+
+      if (priv->scale_unit == CHAMPLAIN_UNIT_KM)
+        {
+          if (base / 1000 >= 1)
+            {
+              base /= 1000; /* base is now in km */
+              is_small_unit = FALSE;
+            }
+          final_unit = TRUE; /* Don't need to recompute */
+        }
+      else if (priv->scale_unit == CHAMPLAIN_UNIT_MILES)
+        {
+          if (is_small_unit && base / 5280 >= 1)
+            {
+              m_per_pixel /= 5280; /* m_per_pixel is now in miles */
+              is_small_unit = FALSE;
+              /* we need to recompute the base because 1000 ft != 1 mile */
+            }
+          else
+            final_unit = TRUE;
+        }
     }
-  else
-    is_small_unit = TRUE;
+  while (!final_unit);
 
   text = clutter_container_find_child_by_name (CLUTTER_CONTAINER (priv->scale_actor), "scale-far-label");
-  label = g_strdup_printf ("%d", base);
-  /* Get only digits width */
+  label = g_strdup_printf ("%g", base);
+  /* Get only digits width for centering */
   clutter_text_set_text (CLUTTER_TEXT (text), label);
   g_free (label);
   clutter_actor_get_size (text, &width, NULL);
-
-  label = g_strdup_printf ("%d %s", base, (is_small_unit ? "m": "km"));
+  /* actual label with unit */
+  label = g_strdup_printf ("%g %s", base,
+      priv->scale_unit == CHAMPLAIN_UNIT_KM ?
+      (is_small_unit ? "m": "km"):
+      (is_small_unit ? "ft": "miles")
+      );
   clutter_text_set_text (CLUTTER_TEXT (text), label);
   g_free (label);
   clutter_actor_set_position (text, (scale_width - width / 2) + SCALE_INSIDE_PADDING, - SCALE_INSIDE_PADDING);
@@ -1164,6 +1194,7 @@ champlain_view_init (ChamplainView *view)
   priv->map = NULL;
   priv->polygon_redraw_id = 0;
   priv->show_scale = TRUE;
+  priv->scale_unit = CHAMPLAIN_UNIT_KM;
 
   /* Setup viewport */
   priv->viewport = g_object_ref (tidy_viewport_new ());
@@ -2396,6 +2427,27 @@ champlain_view_set_show_scale (ChamplainView *view,
 }
 
 /**
+* champlain_view_set_scale_unit:
+* @view: a #ChamplainView
+* @unit: a #ChamplainUnit
+*
+* Sets the scales unit.
+*
+* Since: 0.4.3
+*/
+void
+champlain_view_set_scale_unit (ChamplainView *view,
+    ChamplainUnit unit)
+{
+  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));
+
+  ChamplainViewPrivate *priv = view->priv;
+
+  priv->scale_unit = unit;
+  update_scale (view);
+}
+
+/**
 * champlain_view_set_zoom_on_double_click:
 * @view: a #ChamplainView
 * @value: a #gboolean
@@ -2781,6 +2833,23 @@ champlain_view_get_show_scale (ChamplainView *view)
 }
 
 /**
+ * champlain_view_get_scale_unit:
+ * @view: The view
+ *
+ * Returns: The unit used by the scale
+ *
+ * Since: 0.4.3
+ */
+ChamplainUnit
+champlain_view_get_scale_unit (ChamplainView *view)
+{
+  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);
+
+  ChamplainViewPrivate *priv = view->priv;
+  return priv->scale_unit;
+}
+
+/**
  * champlain_view_get_zoom_on_double_click:
  * @view: The view
  *
diff --git a/champlain/champlain-view.h b/champlain/champlain-view.h
index 9192d8f..8c3017c 100644
--- a/champlain/champlain-view.h
+++ b/champlain/champlain-view.h
@@ -114,6 +114,7 @@ void champlain_view_set_keep_center_on_resize (ChamplainView *view,
     gboolean value);
 void champlain_view_set_show_license (ChamplainView *view, gboolean value);
 void champlain_view_set_show_scale (ChamplainView *view, gboolean value);
+void champlain_view_set_scale_unit (ChamplainView *view, ChamplainUnit unit);
 void champlain_view_set_zoom_on_double_click (ChamplainView *view,
     gboolean value);
 
@@ -142,6 +143,7 @@ ChamplainScrollMode champlain_view_get_scroll_mode (ChamplainView *view);
 gboolean champlain_view_get_keep_center_on_resize (ChamplainView *view);
 gboolean champlain_view_get_show_license (ChamplainView *view);
 gboolean champlain_view_get_show_scale (ChamplainView *view);
+ChamplainUnit champlain_view_get_scale_unit (ChamplainView *view);
 gboolean champlain_view_get_zoom_on_double_click (ChamplainView *view);
 
 void champlain_view_add_polygon (ChamplainView *view,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]