]> saetta.ns0.it Git - libgtkgis/commitdiff
Added GtkGis::get_xml.
authorAndrea Zagli <azagli@libero.it>
Wed, 15 Jul 2009 16:56:11 +0000 (18:56 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 15 Jul 2009 16:56:11 +0000 (18:56 +0200)
Implemented GtkGis::save_to_xml.
Added GtkGisLayer::get_xml.
Added GtkGisLayerSource::get_xml.
Added GtkGisLayerSourceShp::get_xml.
Added GtkGisLayerSourcePostgis::get_xml.
Added GtkGisLayerSourceRaster::get_xml.

12 files changed:
.gitignore
src/gtkgis.c
src/gtkgis.h
src/layer.c
src/layer.h
src/layersource.c
src/layersource.h
src/layersourcepostgis.c
src/layersourceraster.c
src/layersourceshp.c
tests/from_xml.c
tests/test1.gtkgis

index ed7b3418e5b37c772bac0a8ce44f9a05ec3928ef..b6d27c4f4711e1a60da19c46699842ff5ec0617e 100644 (file)
@@ -30,6 +30,7 @@ stamp-h1
 tests/.deps
 tests/.libs
 tests/from_xml
+tests/test_comune.gtkgis
 tests/samples
 
 # temporarily
index 27ceafeb2c795ce8b7ff9463951d7066901e4902..380640174af4dbcbd2ce04eab7ca35ea514a8b48 100644 (file)
@@ -39,6 +39,10 @@ static void gtk_gis_destroy (GtkObject *object);
 
 static void gtk_gis_add (GtkContainer *container,
                          GtkWidget *widget);
+
+static gboolean gtk_gis_focus (GtkWidget*widget,
+                               GtkDirectionType direction);
+
 static void gtk_gis_forall (GtkContainer *container,
                             gboolean include_internals,
                             GtkCallback callback,
@@ -53,6 +57,9 @@ static void gtk_gis_get_property (GObject *object,
                                      GValue *value,
                                      GParamSpec *pspec);
 
+static gboolean gtk_gis_scroll_event (GtkWidget *widget,
+                                      GdkEventScroll *event);
+
 static void gtk_gis_size_request (GtkWidget *widget,
                                   GtkRequisition *requisition);
 static void gtk_gis_size_allocate (GtkWidget *widget,
@@ -93,8 +100,10 @@ gtk_gis_class_init (GtkGisClass *klass)
 
        object_class->destroy = gtk_gis_destroy;
 
+       widget_class->scroll_event = gtk_gis_scroll_event;
        widget_class->size_request = gtk_gis_size_request;
        widget_class->size_allocate = gtk_gis_size_allocate;
+       widget_class->focus = gtk_gis_focus;
 
        container_class->add = gtk_gis_add;
        container_class->forall = gtk_gis_forall;
@@ -105,6 +114,14 @@ gtk_gis_init (GtkGis *gtk_gis)
 {
        GtkGisPrivate *priv = GTK_GIS_GET_PRIVATE (gtk_gis);
 
+       GTK_WIDGET_SET_FLAGS (gtk_gis, GTK_NO_WINDOW | GTK_CAN_FOCUS);
+
+       gtk_widget_set_events (GTK_WIDGET (gtk_gis),
+                              GDK_BUTTON_PRESS_MASK
+                              | gtk_widget_get_events (GTK_WIDGET (gtk_gis)));
+
+       //g_signal_connect (G_OBJECT (gtk_gis), "scroll-event", G_CALLBACK (gtk_gis_scroll_event), (gpointer)NULL);
+
        priv->scroll_win = gtk_scrolled_window_new (NULL, NULL);
     gtk_container_add (GTK_CONTAINER (gtk_gis), priv->scroll_win);
 
@@ -122,7 +139,7 @@ gtk_gis_init (GtkGis *gtk_gis)
 
        priv->extent = NULL;
 
-       priv->scale = g_malloc0 (sizeof (GtkGisScale));
+       priv->scale = g_malloc (sizeof (GtkGisScale));
        priv->scale->x = 1.0;
        priv->scale->y = 1.0;
        priv->scale->xy = 1.0;
@@ -319,6 +336,46 @@ GtkWidget
        return gtkgis;
 }
 
+/**
+ * gtk_gis_get_xml:
+ * @gtkgis:
+ *
+ */
+xmlDoc
+*gtk_gis_get_xml (GtkGis *gtkgis)
+{
+       xmlDoc *xdoc = NULL;
+       xmlNode *xroot;
+       xmlNode *xreport;
+       xmlNode *xnode;
+
+       GSList *cur_layer;
+
+       GtkGisPrivate *priv;
+
+       priv = GTK_GIS_GET_PRIVATE (gtkgis);
+
+       xdoc = xmlNewDoc ("1.0");
+
+       xroot = xmlNewNode (NULL, "gtkgis");
+       xmlDocSetRootElement (xdoc, xroot);
+
+       cur_layer = priv->layers;
+       while (cur_layer != NULL)
+               {
+                       xnode = gtk_gis_layer_get_xml ((GtkGisLayer *)cur_layer->data);
+
+                       if (xnode != NULL)
+                               {
+                                       xmlAddChild (xroot, xnode);
+                               }
+
+                       cur_layer = g_slist_next (cur_layer);
+               }
+
+       return xdoc;
+}
+
 /**
  * gtk_gis_save_to_xml:
  * @gtkgis:
@@ -327,6 +384,16 @@ GtkWidget
 void
 *gtk_gis_save_to_xml (GtkGis *gtkgis, const gchar *filename)
 {
+       xmlDoc *xdoc;
+
+       g_return_if_fail (filename != NULL);
+
+       xdoc = gtk_gis_get_xml (gtkgis);
+
+       if (xdoc != NULL)
+               {
+                       xmlSaveFormatFile (filename, xdoc, 2);
+               }
 }
 
 /**
@@ -589,6 +656,19 @@ gtk_gis_add (GtkContainer *container,
        gtk_widget_set_parent (child, GTK_WIDGET (bin));
 }
 
+static gboolean
+gtk_gis_focus (GtkWidget*widget,
+               GtkDirectionType direction)
+{
+       if (GTK_WIDGET_CAN_FOCUS (widget))
+               {
+                       gtk_widget_grab_focus (widget);
+                       return TRUE;
+               }
+       else
+               return FALSE;
+}
+
 static void
 gtk_gis_forall (GtkContainer *container,
                 gboolean include_internals,
@@ -634,6 +714,41 @@ gtk_gis_get_property (GObject *object, guint property_id, GValue *value, GParamS
                }
 }
 
+static gboolean
+gtk_gis_scroll_event (GtkWidget *widget,
+                      GdkEventScroll *event)
+{
+       GtkGisPrivate *priv;
+
+       GtkGis *gtkgis;
+       GtkGisScale *scale;
+g_fprintf(stderr,"dentro scroll event");
+       g_return_val_if_fail (IS_GTK_GIS (widget), FALSE);
+       g_return_val_if_fail (event != NULL, FALSE);
+
+       gtkgis = GTK_GIS (widget);
+       priv = GTK_GIS_GET_PRIVATE (gtkgis);
+
+       scale = gtk_gis_get_scale (gtkgis);
+
+       if (event->direction == GDK_SCROLL_UP)
+               {g_fprintf(stderr,"dentro scroll up");
+                       scale->x = (scale->x * 120) / 100;
+                       scale->y = (scale->y * 120) / 100;
+                       scale->xy = (scale->xy * 120) / 100;
+               }
+       else if (event->direction == GDK_SCROLL_DOWN)
+               {g_fprintf(stderr,"dentro scroll down");
+                       scale->x = (scale->x * 80) / 100;
+                       scale->y = (scale->y * 80) / 100;
+                       scale->xy = (scale->xy * 80) / 100;
+               }
+
+       gtk_gis_set_scale (gtkgis, scale);
+
+       return TRUE;
+}
+
 static void
 gtk_gis_size_request (GtkWidget *widget,
                       GtkRequisition *requisition)
index b3d6fa8228ca32a191604745d25108d10b6776f9..aa9077070001ebb307c56dfc26a21ef1a5d1827b 100644 (file)
@@ -59,6 +59,7 @@ GtkWidget *gtk_gis_new (void);
 GtkWidget *gtk_gis_new_from_xml (xmlDoc *xdoc);
 GtkWidget *gtk_gis_new_from_file (const gchar *filename);
 
+xmlDoc *gtk_gis_get_xml (GtkGis *gtkgis);
 void *gtk_gis_save_to_xml (GtkGis *gtkgis, const gchar *filename);
 
 void gtk_gis_add_group (GtkGis *gtkgis, GtkGisLayersGroup *group);
index f33ce1bfac682a1d61fd12b621e07952b70a6c9a..00b8e37ba39565b1f1bdcb2117aa7c410c19659d 100644 (file)
@@ -134,6 +134,38 @@ GtkGisLayer
        return layer;
 }
 
+/**
+ * gtk_gis_layer_get_xml:
+ * @layer:
+ *
+ */
+xmlNode
+*gtk_gis_layer_get_xml (GtkGisLayer *layer)
+{
+       xmlNode *xnode;
+
+       GtkGisLayerPrivate *priv;
+
+       priv = GTK_GIS_LAYER_GET_PRIVATE (layer);
+
+       xnode = xmlNewNode (NULL, "layer");
+
+       if (priv->name != NULL)
+               {
+                       xmlSetProp (xnode, "name", priv->name);
+               }
+
+       if (priv->source != NULL)
+               {
+                       xmlNode *xnode_source;
+                       
+                       xnode_source = gtk_gis_layer_source_get_xml (priv->source);
+                       xmlAddChild (xnode, xnode_source);
+               }
+
+       return xnode;
+}
+
 /**
  * gtk_gis_layer_get_name:
  * @layer:
index d6763b3b41e17503ff5ba6fa50fcbc414cb835bc..31daa88bbdd9e9ad6e9737d729f6e2775f68c342 100644 (file)
@@ -25,6 +25,8 @@
 
 #include <goocanvas.h>
 
+#include <libxml/tree.h>
+
 #include "commons.h"
 #include "geometry.h"
 #include "layersource.h"
@@ -63,6 +65,8 @@ GtkGisLayer *gtk_gis_layer_new (GtkGisLayerSource *source,
 GtkGisLayer *gtk_gis_layer_new_from_source (GtkGisLayerSource *source,
                                             const gchar *name);
 
+xmlNode *gtk_gis_layer_get_xml (GtkGisLayer *layer);
+
 gchar *gtk_gis_layer_get_name (GtkGisLayer *layer);
 
 GtkGisLayerSource *gtk_gis_layer_get_source (GtkGisLayer *layer);
index 8847b02c0ab8103e1efa3617bd6508a716d445c5..71c679ce6973f6f2a6e5f059dc662bb7d626a968 100644 (file)
@@ -87,6 +87,31 @@ gtk_gis_layer_source_init (GtkGisLayerSource *gtk_gis_layer_source)
        GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (gtk_gis_layer_source);
 }
 
+/**
+ * gtk_gis_layer_source_get_xml:
+ * @source:
+ *
+ */
+xmlNode
+*gtk_gis_layer_source_get_xml (GtkGisLayerSource *source)
+{
+       xmlNode *xnode = NULL;
+       xmlNode *xnode_source = NULL;
+
+       if (IS_GTK_GIS_LAYER_SOURCE (source) && GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_xml != NULL)
+               {
+                       xnode_source = GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_xml (source);
+               }
+
+       if (xnode_source != NULL)
+               {
+                       xnode = xmlNewNode (NULL, "source");
+                       xmlAddChild (xnode, xnode_source);
+               }
+
+       return xnode;
+}
+
 /**
  * gtk_gis_layer_source_get_geometries:
  * @source:
index 4ab8648127837365e95017238e9dd0367750a1ae..c1508a6816336cf51d28d0670b2341a41ab637f3 100644 (file)
@@ -27,6 +27,8 @@
 #include <ogr_api.h>
 #include <ogr_core.h>
 
+#include <libxml/tree.h>
+
 #include "commons.h"
 #include "geometry.h"
 
@@ -53,6 +55,8 @@ struct _GtkGisLayerSourceClass
        {
                GObjectClass parent_class;
 
+               xmlNode *(*get_xml) (GtkGisLayerSource *source);
+
                void (*set_ogr_datasource) (GtkGisLayerSource *source,
                                            OGRDataSourceH datasource);
                void (*set_gdal_dataset) (GtkGisLayerSource *source,
@@ -62,6 +66,8 @@ struct _GtkGisLayerSourceClass
 GType gtk_gis_layer_source_get_type (void) G_GNUC_CONST;
 
 
+xmlNode *gtk_gis_layer_source_get_xml (GtkGisLayerSource *source);
+
 GList *gtk_gis_layer_source_get_geometries (GtkGisLayerSource *source,
                                             const gchar *name);
 
index 398b467cfba4d29ae4a9763883e8bf4d8f6d4a34..8c97e23818d4e2c72d59c664282cb2fb76136341 100644 (file)
        #include <config.h>
 #endif
 
+#include <libxml/tree.h>
+
 #include "layersourcepostgis.h"
 
 static void gtk_gis_layer_source_postgis_class_init (GtkGisLayerSourcePostgisClass *klass);
 static void gtk_gis_layer_source_postgis_init (GtkGisLayerSourcePostgis *gtk_gis_layer_source_postgis);
 
+static xmlNode *gtk_gis_layer_source_postgis_get_xml (GtkGisLayerSource *source);
+
 static void gtk_gis_layer_source_postgis_set_property (GObject *object,
                                      guint property_id,
                                      const GValue *value,
@@ -52,11 +56,14 @@ static void
 gtk_gis_layer_source_postgis_class_init (GtkGisLayerSourcePostgisClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkGisLayerSourceClass *gtkgislayersource_class = GTK_GIS_LAYER_SOURCE_CLASS (klass);
 
        g_type_class_add_private (object_class, sizeof (GtkGisLayerSourcePostgisPrivate));
 
        object_class->set_property = gtk_gis_layer_source_postgis_set_property;
        object_class->get_property = gtk_gis_layer_source_postgis_get_property;
+
+       gtkgislayersource_class->get_xml = gtk_gis_layer_source_postgis_get_xml;
 }
 
 static void
@@ -97,6 +104,37 @@ GtkGisLayerSource
        return source;
 }
 
+static xmlNode
+*gtk_gis_layer_source_postgis_get_xml (GtkGisLayerSource *source)
+{
+       xmlNode *xnode = NULL;
+
+       GtkGisLayerSourcePostgisPrivate *priv;
+
+       priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (source);
+
+       xnode = xmlNewNode (NULL, "postgis");
+
+       if (priv->host != NULL)
+               {
+                       xmlSetProp (xnode, "host", priv->host);
+               }
+       if (priv->port != 0)
+               {
+                       xmlSetProp (xnode, "port", g_strdup_printf ("%d", priv->port));
+               }
+       if (priv->username != NULL)
+               {
+                       xmlSetProp (xnode, "username", priv->username);
+               }
+       if (priv->password != NULL)
+               {
+                       xmlSetProp (xnode, "password", priv->password);
+               }
+
+       return xnode;
+}
+
 static void
 gtk_gis_layer_source_postgis_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 {
index be73be0b19b5513ad29cff91cbe52f205c001158..4a3cc2e9d962423db4c08c5105d77018a9411454 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <libxml/tree.h>
+
 #include <gdal.h>
 
 #include "layersourceraster.h"
@@ -27,6 +29,8 @@
 static void gtk_gis_layer_source_raster_class_init (GtkGisLayerSourceRasterClass *klass);
 static void gtk_gis_layer_source_raster_init (GtkGisLayerSourceRaster *gtk_gis_layer_source_raster);
 
+static xmlNode *gtk_gis_layer_source_raster_get_xml (GtkGisLayerSource *source);
+
 static void
 gtk_gis_layer_source_raster_set_gdal_dataset (GtkGisLayerSource *source,
                                               GDALDatasetH hDataset);
@@ -61,6 +65,8 @@ gtk_gis_layer_source_raster_class_init (GtkGisLayerSourceRasterClass *klass)
 
        object_class->set_property = gtk_gis_layer_source_raster_set_property;
        object_class->get_property = gtk_gis_layer_source_raster_get_property;
+
+       gtkgislayersource_class->get_xml = gtk_gis_layer_source_raster_get_xml;
 }
 
 static void
@@ -113,6 +119,25 @@ gchar
        return g_strdup (priv->filename);
 }
 
+static xmlNode
+*gtk_gis_layer_source_raster_get_xml (GtkGisLayerSource *source)
+{
+       xmlNode *xnode = NULL;
+
+       GtkGisLayerSourceRasterPrivate *priv;
+
+       priv = GTK_GIS_LAYER_SOURCE_RASTER_GET_PRIVATE (source);
+
+       xnode = xmlNewNode (NULL, "raster");
+
+       if (priv->filename != NULL)
+               {
+                       xmlSetProp (xnode, "filename", priv->filename);
+               }
+
+       return xnode;
+}
+
 static void
 gtk_gis_layer_source_raster_set_gdal_dataset (GtkGisLayerSource *source,
                                               GDALDatasetH hDataset)
index 5edaafbfab65793e7c5fe4fc7bcbde06ba2fbba6..e557251274158904f2b22ca80277297062ef9be1 100644 (file)
 #include <ogr_api.h>
 #include <ogr_core.h>
 
+#include <libxml/tree.h>
+
 #include "layersourceshp.h"
 
 static void gtk_gis_layer_source_shp_class_init (GtkGisLayerSourceShpClass *klass);
 static void gtk_gis_layer_source_shp_init (GtkGisLayerSourceShp *gtk_gis_layer_source_shp);
 
+static xmlNode *gtk_gis_layer_source_shp_get_xml (GtkGisLayerSource *source);
+
 static void
 gtk_gis_layer_source_shp_set_ogr_datasource (GtkGisLayerSource *source,
                                              OGRDataSourceH datasource);
@@ -62,6 +66,8 @@ gtk_gis_layer_source_shp_class_init (GtkGisLayerSourceShpClass *klass)
 
        object_class->set_property = gtk_gis_layer_source_shp_set_property;
        object_class->get_property = gtk_gis_layer_source_shp_get_property;
+
+       gtkgislayersource_class->get_xml = gtk_gis_layer_source_shp_get_xml;
 }
 
 static void
@@ -114,6 +120,25 @@ gchar
        return g_strdup (priv->filename);
 }
 
+static xmlNode
+*gtk_gis_layer_source_shp_get_xml (GtkGisLayerSource *source)
+{
+       xmlNode *xnode = NULL;
+
+       GtkGisLayerSourceShpPrivate *priv;
+
+       priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source);
+
+       xnode = xmlNewNode (NULL, "shape");
+
+       if (priv->filename != NULL)
+               {
+                       xmlSetProp (xnode, "filename", priv->filename);
+               }
+
+       return xnode;
+}
+
 static void
 gtk_gis_layer_source_shp_set_ogr_datasource (GtkGisLayerSource *source,
                                              OGRDataSourceH datasource)
index 610ae648a4b42f13431240ca10fd24390f82a08e..4272b2815be4389eb3bab1e4fa313f5d09a1437a 100644 (file)
@@ -23,6 +23,8 @@ main (int argc, char *argv[])
        gtk_gis_zoom_to_max (GTK_GIS (gtkgis));
        gtk_gis_draw (GTK_GIS (gtkgis));
 
+       gtk_gis_save_to_xml (GTK_GIS (gtkgis), "test_saved.gtkgis");
+
        gtk_main ();
 
        return 0;
index 3a39de76858bd7f3be68db74b8b8fa6b3c4dd98e..090eb87d9692a9a56b7d64ec2546afc7dbcdb4e3 100644 (file)
@@ -2,25 +2,25 @@
 
        <layer name="alaska">
                <source>
-                       <shape filename="samples/alaska.shp" />
+                       <shape filename="tests/samples/alaska.shp" />
                </source>
        </layer>
 
        <layer name="airports">
                <source>
-                       <shape filename="samples/airports.shp" />
+                       <shape filename="tests/samples/airports.shp" />
                </source>
        </layer>
 
        <layer name="railroads">
                <source>
-                       <shape filename="samples/railroads.shp" />
+                       <shape filename="tests/samples/railroads.shp" />
                </source>
        </layer>
 
        <layer name="rivers">
                <source>
-                       <shape filename="samples/rivers.shp" />
+                       <shape filename="tests/samples/rivers.shp" />
                </source>
        </layer>