[libchamplain] Add ChamplainBoundingBox (a boxed struct), add bounding-box property



commit 855945cbd545ca70aba6f45d9d60e23ca30451e8
Author: Simon Wenner <simon wenner ch>
Date:   Sun Jul 26 22:56:35 2009 +0200

    Add ChamplainBoundingBox (a boxed struct), add bounding-box property

 champlain/Makefile.am                         |    9 ++-
 champlain/champlain-bounding-box.c            |   95 +++++++++++++++++++++++++
 champlain/champlain-bounding-box.h            |   59 +++++++++++++++
 champlain/champlain-local-map-data-source.c   |   20 +++++-
 champlain/champlain-map-data-source.c         |   37 ++++++++--
 champlain/champlain-map-data-source.h         |    8 --
 champlain/champlain-network-map-data-source.c |   11 +++
 7 files changed, 222 insertions(+), 17 deletions(-)
---
diff --git a/champlain/Makefile.am b/champlain/Makefile.am
index 2c6b118..0c9d494 100644
--- a/champlain/Makefile.am
+++ b/champlain/Makefile.am
@@ -115,17 +115,20 @@ libchamplain_headers += \
 	champlain-memphis-map-source.h  \
 	champlain-map-data-source.h	\
 	champlain-local-map-data-source.h \
-	champlain-network-map-data-source.h
+	champlain-network-map-data-source.h \
+	champlain-bounding-box.h
 libchamplain_0_3_la_SOURCES += \
 	champlain-memphis-map-source.c  \
 	champlain-map-data-source.c \
 	champlain-local-map-data-source.c \
-	champlain-network-map-data-source.c
+	champlain-network-map-data-source.c \
+	champlain-bounding-box.c
 libchamplain_include_HEADERS += \
 	champlain-memphis-map-source.h  \
 	champlain-map-data-source.h \
 	champlain-local-map-data-source.h \
-	champlain-network-map-data-source.h
+	champlain-network-map-data-source.h \
+	champlain-bounding-box.h
 libchamplain_0_3_la_LIBADD += ../memphis/libmemphis-1.0.la
 AM_CPPFLAGS += -I$(top_srcdir)/memphis -DMEMPHIS_ENABLED
 endif
diff --git a/champlain/champlain-bounding-box.c b/champlain/champlain-bounding-box.c
new file mode 100644
index 0000000..495837b
--- /dev/null
+++ b/champlain/champlain-bounding-box.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2009 Simon Wenner <simon wenner ch>
+ *
+ * This file is inspired by clutter-color.c which is
+ * Copyright (C) 2006 OpenedHand, and has the same license.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * SECTION:champlain-bounding-box
+ * @short_description: A basic struct to describe a bounding box
+ */
+
+#include "champlain-bounding-box.h"
+
+GType
+champlain_bounding_box_get_type (void)
+{
+  static GType type = 0;
+
+  if (G_UNLIKELY (type == 0))
+    {
+      type = g_boxed_type_register_static (g_intern_static_string ("ChamplainBoundingBox"),
+          (GBoxedCopyFunc) champlain_bounding_box_copy,
+          (GBoxedFreeFunc) champlain_bounding_box_free);
+    }
+
+  return type;
+}
+
+/**
+ * champlain_bounding_box_copy:
+ * @bbox: a #ChamplainBoundingBox
+ *
+ * Makes a copy of the bounding box structure. The result must be
+ * freed using champlain_bounding_box_free().
+ *
+ * Return value: an allocated copy of @bbox.
+ *
+ * Since: 0.6
+ */
+ChamplainBoundingBox *
+champlain_bounding_box_copy (const ChamplainBoundingBox *bbox)
+{
+  if (G_LIKELY (bbox != NULL))
+    return g_slice_dup (ChamplainBoundingBox, bbox);
+
+  return NULL;
+}
+
+/**
+ * champlain_bounding_box_free:
+ * @bbox: a #ChamplainBoundingBox
+ *
+ * Frees a bounding box structure created with #champlain_bounding_box_new or
+ * #champlain_bounding_box_copy
+ *
+ * Since: 0.6
+ */
+void
+champlain_bounding_box_free (ChamplainBoundingBox *bbox)
+{
+
+  if (G_UNLIKELY (bbox == NULL))
+    return;
+
+  g_slice_free (ChamplainBoundingBox, bbox);
+}
+
+/**
+ * champlain_bounding_box_new:
+ *
+ * Return value: a newly allocated #ChamplainBoundingBox to be freed
+ * with #champlain_bounding_box_free
+ *
+ * Since: 0.6
+ */
+ChamplainBoundingBox *
+champlain_bounding_box_new ()
+{
+  return g_slice_new (ChamplainBoundingBox);
+}
diff --git a/champlain/champlain-bounding-box.h b/champlain/champlain-bounding-box.h
new file mode 100644
index 0000000..38480da
--- /dev/null
+++ b/champlain/champlain-bounding-box.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2009 Simon Wenner <simon wenner ch>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#if !defined (__CHAMPLAIN_CHAMPLAIN_H_INSIDE__) && !defined (CHAMPLAIN_COMPILATION)
+#error "Only <champlain/champlain.h> can be included directly."
+#endif
+
+#ifndef CHAMPLAIN_BOUNDING_BOX_H
+#define CHAMPLAIN_BOUNDING_BOX_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct _ChamplainBoundingBox ChamplainBoundingBox;
+
+#define CHAMPLAIN_BOUNDING_BOX(obj)     ((ChamplainBoundingBox *) (obj))
+
+/**
+ * ChamplainBoundingBox:
+ *
+ * Defines the area of a ChamplainMapDataSource that contains data.
+ *
+ * Since: 0.6
+ */
+struct _ChamplainBoundingBox {
+  gdouble left;
+  gdouble bottom;
+  gdouble right;
+  gdouble top;
+};
+
+GType champlain_bounding_box_get_type (void) G_GNUC_CONST;
+#define CHAMPLAIN_TYPE_BOUNDING_BOX (champlain_bounding_box_get_type ())
+
+ChamplainBoundingBox * champlain_bounding_box_copy (const ChamplainBoundingBox *bbox);
+
+void champlain_bounding_box_free (ChamplainBoundingBox *bbox);
+
+ChamplainBoundingBox * champlain_bounding_box_new (void);
+
+G_END_DECLS
+
+#endif
diff --git a/champlain/champlain-local-map-data-source.c b/champlain/champlain-local-map-data-source.c
index 763f5da..fd6027d 100644
--- a/champlain/champlain-local-map-data-source.c
+++ b/champlain/champlain-local-map-data-source.c
@@ -20,6 +20,7 @@
 
 #define DEBUG_FLAG CHAMPLAIN_DEBUG_MEMPHIS
 #include "champlain-debug.h"
+#include "champlain-bounding-box.h"
 
 #include <memphis/memphis.h>
 
@@ -123,8 +124,25 @@ champlain_local_map_data_source_load_map_data (ChamplainLocalMapDataSource *self
       && map_path);
 
   ChamplainLocalMapDataSourcePrivate *priv = GET_PRIVATE(self);
+  ChamplainBoundingBox *bbox;
+  MemphisMap *map = memphis_map_new ();
 
-  memphis_map_load_from_file (priv->map, map_path);
+  memphis_map_set_debug_level (map, 0);
+  memphis_map_load_from_file (map, map_path);
+
+  if (priv->map)
+    memphis_map_free (priv->map);
+
+  priv->map = map;
+
+  // TODO: memphis needs a function to get the bbox
+  bbox = champlain_bounding_box_new ();
+  bbox->left = priv->map->map->minlat;
+  bbox->top = priv->map->map->minlon;
+  bbox->right = priv->map->map->maxlat;
+  bbox->bottom = priv->map->map->maxlon;
+  g_object_set (G_OBJECT (self), "bounding-box", bbox, NULL);
+  champlain_bounding_box_free (bbox);
 
   g_signal_emit_by_name (CHAMPLAIN_MAP_DATA_SOURCE (self),
       "map-data-changed", NULL);
diff --git a/champlain/champlain-map-data-source.c b/champlain/champlain-map-data-source.c
index d309013..7009a76 100644
--- a/champlain/champlain-map-data-source.c
+++ b/champlain/champlain-map-data-source.c
@@ -20,6 +20,7 @@
 
 #define DEBUG_FLAG CHAMPLAIN_DEBUG_MEMPHIS
 #include "champlain-debug.h"
+#include "champlain-bounding-box.h"
 
 G_DEFINE_TYPE (ChamplainMapDataSource, champlain_map_data_source, G_TYPE_OBJECT)
 
@@ -44,7 +45,7 @@ typedef struct _ChamplainMapDataSourcePrivate ChamplainMapDataSourcePrivate;
 
 struct _ChamplainMapDataSourcePrivate {
   ChamplainBoundingBox *bounding_box;
-  /* the area that is covered by this source */
+  /* the area that is covered by this data source */
 };
 
 static void
@@ -53,9 +54,14 @@ champlain_map_data_source_get_property (GObject *object,
     GValue *value,
     GParamSpec *pspec)
 {
+  ChamplainMapDataSource *self = CHAMPLAIN_MAP_DATA_SOURCE (object);
+  ChamplainMapDataSourcePrivate *priv = GET_PRIVATE (self);
+
   switch (property_id)
     {
-      // TODO
+      case PROP_BOUNDING_BOX:
+        g_value_set_boxed (value, priv->bounding_box);
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -67,9 +73,15 @@ champlain_map_data_source_set_property (GObject *object,
     const GValue *value,
     GParamSpec *pspec)
 {
+  ChamplainMapDataSource *self = CHAMPLAIN_MAP_DATA_SOURCE (object);
+  ChamplainMapDataSourcePrivate *priv = GET_PRIVATE (self);
+
   switch (property_id)
     {
-      // TODO
+      case PROP_BOUNDING_BOX:
+        champlain_bounding_box_free (priv->bounding_box);
+        priv->bounding_box = g_value_dup_boxed (value);
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -87,7 +99,7 @@ champlain_map_data_source_finalize (GObject *object)
   ChamplainMapDataSource *self = CHAMPLAIN_MAP_DATA_SOURCE (object);
   ChamplainMapDataSourcePrivate *priv =  GET_PRIVATE (self);
 
-  g_free (priv->bounding_box);
+  champlain_bounding_box_free (priv->bounding_box);
 
   G_OBJECT_CLASS (champlain_map_data_source_parent_class)->finalize (object);
 }
@@ -113,6 +125,21 @@ champlain_map_data_source_class_init (ChamplainMapDataSourceClass *klass)
   klass->get_map_data = champlain_map_data_source_real_get_map_data;
 
   /**
+  * ChamplainMapDataSource:Bounding-box:
+  *
+  * The bounding box of the area that contains map data.
+  *
+  * Since: 0.6
+  */
+  g_object_class_install_property (object_class,
+      PROP_BOUNDING_BOX,
+      g_param_spec_boxed ("bounding-box",
+        "Bounding Box",
+        "The bounding box of the map data source",
+        CHAMPLAIN_TYPE_BOUNDING_BOX,
+        G_PARAM_READWRITE));
+
+  /**
   * ChamplainMapDataSource::map-data-changed:
   * @map_data_source: the #ChamplainMapDataSource that received the signal
   *
@@ -133,7 +160,7 @@ champlain_map_data_source_init (ChamplainMapDataSource *self)
 {
   ChamplainMapDataSourcePrivate *priv =  GET_PRIVATE (self);
 
-  priv->bounding_box = g_new (ChamplainBoundingBox, 1);
+  priv->bounding_box = champlain_bounding_box_new ();
   priv->bounding_box->left = 0.0;
   priv->bounding_box->bottom = 0.0;
   priv->bounding_box->right = 0.0;
diff --git a/champlain/champlain-map-data-source.h b/champlain/champlain-map-data-source.h
index cfb8d16..9be58b2 100644
--- a/champlain/champlain-map-data-source.h
+++ b/champlain/champlain-map-data-source.h
@@ -45,14 +45,6 @@ typedef struct {
   GObject parent;
 } ChamplainMapDataSource;
 
-// TODO: Maybe it should be an object
-typedef struct {
-  gdouble left;
-  gdouble bottom;
-  gdouble right;
-  gdouble top;
-} ChamplainBoundingBox;
-
 typedef struct {
   GObjectClass parent_class;
   MemphisMap* (*get_map_data) (ChamplainMapDataSource *data_source);
diff --git a/champlain/champlain-network-map-data-source.c b/champlain/champlain-network-map-data-source.c
index 7525f62..1810722 100644
--- a/champlain/champlain-network-map-data-source.c
+++ b/champlain/champlain-network-map-data-source.c
@@ -21,6 +21,7 @@
 
 #define DEBUG_FLAG CHAMPLAIN_DEBUG_MEMPHIS
 #include "champlain-debug.h"
+#include "champlain-bounding-box.h"
 
 #include <memphis/memphis.h>
 #ifdef HAVE_LIBSOUP_GNOME
@@ -211,6 +212,7 @@ load_map_data_cb (SoupSession *session, SoupMessage *msg,
   ChamplainNetworkMapDataSource *self = 
       CHAMPLAIN_NETWORK_MAP_DATA_SOURCE (user_data);
   ChamplainNetworkMapDataSourcePrivate *priv = GET_PRIVATE (self);
+  ChamplainBoundingBox *bbox;
 
   if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
     {
@@ -236,6 +238,15 @@ load_map_data_cb (SoupSession *session, SoupMessage *msg,
 
   priv->map = map;
 
+  // TODO: memphis needs a function to get the bbox
+  bbox = champlain_bounding_box_new ();
+  bbox->left = priv->map->map->minlat;
+  bbox->top = priv->map->map->minlon;
+  bbox->right = priv->map->map->maxlat;
+  bbox->bottom = priv->map->map->maxlon;
+  g_object_set (G_OBJECT (self), "bounding-box", bbox, NULL);
+  champlain_bounding_box_free (bbox);
+
   g_signal_emit_by_name (CHAMPLAIN_MAP_DATA_SOURCE (self),
        "map-data-changed", NULL);
 }



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