From 840ea6abb9d781b56b7cce60fa23870a27ceb51b Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 5 Aug 2009 17:55:32 +0200 Subject: [PATCH] Added initial support for layer's style. --- data/gtkgis.dtd | 8 ++- docs/reference/libgtkgis-decl-list.txt | 2 + docs/reference/libgtkgis-decl.txt | 16 ++++- docs/reference/libgtkgis-undocumented.txt | 5 +- docs/reference/libgtkgis-unused.txt | 2 + docs/reference/tmpl/geometry.sgml | 1 + src/commons.h | 23 ++++--- src/geometry.c | 6 +- src/geometry.h | 8 ++- src/geometryline.c | 11 ++- src/geometrypoint.c | 13 ++-- src/geometrypolygon.c | 10 ++- src/geometryraster.c | 8 ++- src/gtkgis.c | 41 ++++++++++- src/layer.c | 83 ++++++++++++++++++++++- src/layer.h | 3 + tests/test1.gtkgis | 6 ++ 17 files changed, 212 insertions(+), 34 deletions(-) diff --git a/data/gtkgis.dtd b/data/gtkgis.dtd index 7150f4c..b7d8922 100644 --- a/data/gtkgis.dtd +++ b/data/gtkgis.dtd @@ -2,7 +2,7 @@ - + + + + + + + diff --git a/docs/reference/libgtkgis-decl-list.txt b/docs/reference/libgtkgis-decl-list.txt index 35fd1d9..d13f871 100644 --- a/docs/reference/libgtkgis-decl-list.txt +++ b/docs/reference/libgtkgis-decl-list.txt @@ -120,6 +120,8 @@ gtk_gis_layer_remove_geometry gtk_gis_layer_get_extent gtk_gis_layer_get_editable gtk_gis_layer_set_editable +gtk_gis_layer_get_style +gtk_gis_layer_set_style gtk_gis_layer_draw diff --git a/docs/reference/libgtkgis-decl.txt b/docs/reference/libgtkgis-decl.txt index d6e6f66..bf23256 100644 --- a/docs/reference/libgtkgis-decl.txt +++ b/docs/reference/libgtkgis-decl.txt @@ -523,6 +523,16 @@ GtkGisLayer *layer GtkGisLayer *layer,gboolean editable +gtk_gis_layer_get_style +GtkGisLayerStyle * +GtkGisLayer *layer + + +gtk_gis_layer_set_style +void +GtkGisLayer *layer, GtkGisLayerStyle style + + gtk_gis_layer_draw void GtkGisLayer *layer, GooCanvasItem *root, GtkGisScale *scale @@ -639,7 +649,9 @@ struct _GtkGisGeometryClass { GObjectClass parent_class; - GooCanvasItem *(*draw) (GtkGisGeometry *geometry, GtkGisScale *scale); + GooCanvasItem *(*draw) (GtkGisGeometry *geometry, + GtkGisScale *scale, + GtkGisLayerStyle style); }; @@ -660,7 +672,7 @@ GtkGisGeometry *geometry, gboolean editable gtk_gis_geometry_draw GooCanvasItem * -GtkGisGeometry *geometry, GtkGisScale *scale +GtkGisGeometry *geometry,GtkGisScale *scale,GtkGisLayerStyle style TYPE_GTK_GIS_LAYER_SOURCE_POSTGIS diff --git a/docs/reference/libgtkgis-undocumented.txt b/docs/reference/libgtkgis-undocumented.txt index cdf5a93..0221b22 100644 --- a/docs/reference/libgtkgis-undocumented.txt +++ b/docs/reference/libgtkgis-undocumented.txt @@ -1,7 +1,7 @@ 11% symbol docs coverage. -21 symbols documented. +22 symbols documented. 7 symbols incomplete. -169 not documented. +170 not documented. GTK_GIS @@ -120,6 +120,7 @@ gtk_gis_layer_get_editable gtk_gis_layer_get_extent gtk_gis_layer_get_name gtk_gis_layer_get_source +gtk_gis_layer_get_style gtk_gis_layer_get_type gtk_gis_layer_get_xml gtk_gis_layer_new (geometrytype, geometry_type) diff --git a/docs/reference/libgtkgis-unused.txt b/docs/reference/libgtkgis-unused.txt index 84968bc..4144b61 100644 --- a/docs/reference/libgtkgis-unused.txt +++ b/docs/reference/libgtkgis-unused.txt @@ -10,3 +10,5 @@ GtkGisLayerSourcePostgisClass GtkGisLayerSourceRasterClass GtkGisLayerSourceShpClass GtkGisLayersGroupClass +gtk_gis_layer_get_style +gtk_gis_layer_set_style diff --git a/docs/reference/tmpl/geometry.sgml b/docs/reference/tmpl/geometry.sgml index e859271..073d518 100644 --- a/docs/reference/tmpl/geometry.sgml +++ b/docs/reference/tmpl/geometry.sgml @@ -103,6 +103,7 @@ GtkGisGeometry @geometry: @scale: +@style: @Returns: diff --git a/src/commons.h b/src/commons.h index 21e15c7..5dcd2ee 100644 --- a/src/commons.h +++ b/src/commons.h @@ -39,14 +39,6 @@ typedef struct gdouble xy; } GtkGisScale; -typedef enum - { - LAYER_GEOMETRY_TYPE_POINT, - LAYER_GEOMETRY_TYPE_LINE, - LAYER_GEOMETRY_TYPE_POLYGON, - LAYER_GEOMETRY_TYPE_RASTER - } GtkGisLayerGeometryType; - typedef struct { gdouble min_x; @@ -55,6 +47,21 @@ typedef struct gdouble max_y; } GtkGisLayerExtent; +typedef struct + { + gdouble width; + gchar *stroke_color; + gchar *fill_color; + } GtkGisLayerStyle; + +typedef enum + { + LAYER_GEOMETRY_TYPE_POINT, + LAYER_GEOMETRY_TYPE_LINE, + LAYER_GEOMETRY_TYPE_POLYGON, + LAYER_GEOMETRY_TYPE_RASTER + } GtkGisLayerGeometryType; + G_END_DECLS diff --git a/src/geometry.c b/src/geometry.c index b478024..ec07da4 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -103,13 +103,15 @@ gtk_gis_geometry_set_editable (GtkGisGeometry *geometry, gboolean editable) * */ GooCanvasItem -*gtk_gis_geometry_draw (GtkGisGeometry *geometry, GtkGisScale *scale) +*gtk_gis_geometry_draw (GtkGisGeometry *geometry, + GtkGisScale *scale, + GtkGisLayerStyle style) { GooCanvasItem *item = NULL; if (IS_GTK_GIS_GEOMETRY (geometry) && GTK_GIS_GEOMETRY_GET_CLASS (geometry)->draw != NULL) { - item = GTK_GIS_GEOMETRY_GET_CLASS (geometry)->draw (geometry, scale); + item = GTK_GIS_GEOMETRY_GET_CLASS (geometry)->draw (geometry, scale, style); } return item; diff --git a/src/geometry.h b/src/geometry.h index 30d31a0..8fbf675 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -50,7 +50,9 @@ struct _GtkGisGeometryClass { GObjectClass parent_class; - GooCanvasItem *(*draw) (GtkGisGeometry *geometry, GtkGisScale *scale); + GooCanvasItem *(*draw) (GtkGisGeometry *geometry, + GtkGisScale *scale, + GtkGisLayerStyle style); }; GType gtk_gis_geometry_get_type (void) G_GNUC_CONST; @@ -59,7 +61,9 @@ GType gtk_gis_geometry_get_type (void) G_GNUC_CONST; gboolean gtk_gis_geometry_get_editable (GtkGisGeometry *geometry); void gtk_gis_geometry_set_editable (GtkGisGeometry *geometry, gboolean editable); -GooCanvasItem *gtk_gis_geometry_draw (GtkGisGeometry *geometry, GtkGisScale *scale); +GooCanvasItem *gtk_gis_geometry_draw (GtkGisGeometry *geometry, + GtkGisScale *scale, + GtkGisLayerStyle style); G_END_DECLS diff --git a/src/geometryline.c b/src/geometryline.c index 4341a58..15d1051 100644 --- a/src/geometryline.c +++ b/src/geometryline.c @@ -32,7 +32,9 @@ static void gtk_gis_geometry_line_init (GtkGisGeometryLine *gtk_gis_geometry_lin static gchar *convert_comma_to_dot (gchar *str); -static GooCanvasItem *gtk_gis_geometry_line_draw (GtkGisGeometry *line, GtkGisScale *scale); +static GooCanvasItem *gtk_gis_geometry_line_draw (GtkGisGeometry *line, + GtkGisScale *scale, + GtkGisLayerStyle style); static void gtk_gis_geometry_line_set_property (GObject *object, guint property_id, @@ -183,7 +185,9 @@ static gchar } static GooCanvasItem -*gtk_gis_geometry_line_draw (GtkGisGeometry *line, GtkGisScale *scale) +*gtk_gis_geometry_line_draw (GtkGisGeometry *line, + GtkGisScale *scale, + GtkGisLayerStyle style) { GtkGisGeometryLinePrivate *priv; GooCanvasItem *item = NULL; @@ -216,7 +220,8 @@ static GooCanvasItem path = convert_comma_to_dot (path); item = goo_canvas_path_new (NULL, path, - "line-width", 0.5 / scale->xy, + "line-width", style.width / scale->xy, + "stroke-color", style.stroke_color, NULL); } diff --git a/src/geometrypoint.c b/src/geometrypoint.c index a0c0572..c970abf 100644 --- a/src/geometrypoint.c +++ b/src/geometrypoint.c @@ -30,7 +30,9 @@ static void gtk_gis_geometry_point_class_init (GtkGisGeometryPointClass *klass); static void gtk_gis_geometry_point_init (GtkGisGeometryPoint *gtk_gis_geometry_point); -static GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point, GtkGisScale *scale); +static GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point, + GtkGisScale *scale, + GtkGisLayerStyle style); static void gtk_gis_geometry_point_set_property (GObject *object, guint property_id, @@ -148,7 +150,9 @@ gtk_gis_geometry_point_get_y (GtkGisGeometryPoint *point) } static -GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point, GtkGisScale *scale) +GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point, + GtkGisScale *scale, + GtkGisLayerStyle style) { GooCanvasItem *item = NULL; @@ -159,8 +163,9 @@ GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point, GtkGisScale * item = goo_canvas_ellipse_new (NULL, priv->point->x, priv->point->y, 0.5 / scale->x, 0.5 / scale->y, - "line-width", 0.5 / scale->xy, - "fill-color", "black", + "line-width", style.width / scale->xy, + "stroke-color", style.stroke_color, + "fill-color", style.fill_color, NULL); } diff --git a/src/geometrypolygon.c b/src/geometrypolygon.c index 96e52cf..90aaa50 100644 --- a/src/geometrypolygon.c +++ b/src/geometrypolygon.c @@ -30,7 +30,9 @@ static void gtk_gis_geometry_polygon_class_init (GtkGisGeometryPolygonClass *klass); static void gtk_gis_geometry_polygon_init (GtkGisGeometryPolygon *gtk_gis_geometry_polygon); -static GooCanvasItem *gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon, GtkGisScale *scale); +static GooCanvasItem *gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon, + GtkGisScale *scale, + GtkGisLayerStyle style); static void gtk_gis_geometry_polygon_set_property (GObject *object, guint property_id, @@ -185,7 +187,9 @@ gtk_gis_geometry_polygon_remove_line (GtkGisGeometryPolygon *polygon, } static GooCanvasItem -*gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon, GtkGisScale *scale) +*gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon, + GtkGisScale *scale, + GtkGisLayerStyle style) { GtkGisGeometryPolygonPrivate *priv; GooCanvasItem *polygon_item = NULL; @@ -207,7 +211,7 @@ static GooCanvasItem while (cur != NULL) { line = (GtkGisGeometry *)cur->data; - item = gtk_gis_geometry_draw (line, scale); + item = gtk_gis_geometry_draw (line, scale, style); if (item != NULL) { diff --git a/src/geometryraster.c b/src/geometryraster.c index 630c524..dc97044 100644 --- a/src/geometryraster.c +++ b/src/geometryraster.c @@ -31,7 +31,9 @@ static void gtk_gis_geometry_raster_class_init (GtkGisGeometryRasterClass *klass static void gtk_gis_geometry_raster_init (GtkGisGeometryRaster *gtk_gis_geometry_raster); static -GooCanvasItem *gtk_gis_geometry_raster_draw (GtkGisGeometry *raster, GtkGisScale *scale); +GooCanvasItem *gtk_gis_geometry_raster_draw (GtkGisGeometry *raster, + GtkGisScale *scale, + GtkGisLayerStyle style); static void gtk_gis_geometry_raster_set_property (GObject *object, guint property_id, @@ -114,7 +116,9 @@ GtkGisGeometry } static GooCanvasItem -*gtk_gis_geometry_raster_draw (GtkGisGeometry *raster, GtkGisScale *scale) +*gtk_gis_geometry_raster_draw (GtkGisGeometry *raster, + GtkGisScale *scale, + GtkGisLayerStyle style) { GooCanvasItem *item = NULL; diff --git a/src/gtkgis.c b/src/gtkgis.c index 1708a12..d176a07 100644 --- a/src/gtkgis.c +++ b/src/gtkgis.c @@ -208,6 +208,7 @@ GtkWidget { xmlNode *xml_layer; gchar *layer_name; + GtkGisLayer *layer; xml_layer = cur; @@ -219,6 +220,9 @@ GtkWidget layer_name = g_strdup_printf ("layer"); } + /* TO DO + * don't use a cycle: first find source and then style + */ cur = cur->children; while (cur != NULL) { @@ -238,7 +242,6 @@ GtkWidget || xmlStrcmp (cur->name, "raster") == 0)) { xmlNode *xml_source_spec; - GtkGisLayer *layer = NULL; GtkGisLayerSource *layer_source = NULL; xml_source_spec = cur; @@ -253,7 +256,7 @@ GtkWidget } else if (xmlStrcmp (cur->name, "raster") == 0) { - layer_source = gtk_gis_layer_source_raster_new_from_xml (cur); + layer_source = gtk_gis_layer_source_raster_new_from_xml (cur); } if (layer_source != NULL) @@ -268,6 +271,40 @@ GtkWidget } cur = xml_source; } + else if (cur->type == XML_ELEMENT_NODE + && xmlStrcmp (cur->name, "style") == 0) + { + xmlNode *xml_style; + GtkGisLayerStyle style; + + xml_style = cur; + + cur = cur->children; + while (cur != NULL) + { + if (cur->type == XML_ELEMENT_NODE + && (xmlStrcmp (cur->name, "width") == 0)) + { + style.width = g_strtod ((gchar *)xmlNodeGetContent (cur), NULL); + } + else if (cur->type == XML_ELEMENT_NODE + && (xmlStrcmp (cur->name, "stroke-color") == 0)) + { + style.stroke_color = g_strdup ((gchar *)xmlNodeGetContent (cur)); + } + else if (cur->type == XML_ELEMENT_NODE + && (xmlStrcmp (cur->name, "fill-color") == 0)) + { + style.fill_color = g_strdup ((gchar *)xmlNodeGetContent (cur)); + } + cur = cur->next; + } + + gtk_gis_layer_set_style (layer, style); + + cur = xml_style; + } + cur = cur->next; } cur = xml_layer; diff --git a/src/layer.c b/src/layer.c index c967f28..a7c86c7 100644 --- a/src/layer.c +++ b/src/layer.c @@ -55,6 +55,8 @@ struct _GtkGisLayerPrivate GList *geometries; gboolean editable; + + GtkGisLayerStyle *style; }; G_DEFINE_TYPE (GtkGisLayer, gtk_gis_layer, G_TYPE_OBJECT) @@ -74,6 +76,11 @@ static void gtk_gis_layer_init (GtkGisLayer *gtk_gis_layer) { GtkGisLayerPrivate *priv = GTK_GIS_LAYER_GET_PRIVATE (gtk_gis_layer); + + priv->style = g_malloc (sizeof (GtkGisLayerStyle)); + priv->style->width = 0.5; + priv->style->stroke_color = g_strdup ("black"); + priv->style->fill_color = g_strdup ("white"); } /** @@ -294,6 +301,75 @@ gtk_gis_layer_set_editable (GtkGisLayer *layer, priv->editable = editable; } +/** + * gtk_gis_layer_get_style: + * @layer: a #GtkGisLayer object. + * + * Returns: a pointer to a #GtkGisLayerStyle struct. This is a copy of the struct + * inside the object, so it must be free after use. + */ +GtkGisLayerStyle +*gtk_gis_layer_get_style (GtkGisLayer *layer) +{ + GtkGisLayerPrivate *priv; + GtkGisLayerStyle *style; + + g_return_val_if_fail (IS_GTK_GIS_LAYER (layer), NULL); + + priv = GTK_GIS_LAYER_GET_PRIVATE (layer); + + style = g_malloc (sizeof (GtkGisLayerStyle)); + style = g_memdup (priv->style, sizeof (GtkGisLayerStyle)); + + return style; +} + +/** + * gtk_gis_layer_set_stroke_color: + * @layer: a #GtkGisLayer object. + * @style: a #GtkGisLayerStyle struct. + * + * Valid only for layers that aren't rasters. + */ +void +gtk_gis_layer_set_style (GtkGisLayer *layer, GtkGisLayerStyle style) +{ + GtkGisLayerPrivate *priv; + GtkGisLayerStyle new_style; + + g_return_if_fail (IS_GTK_GIS_LAYER (layer)); + + priv = GTK_GIS_LAYER_GET_PRIVATE (layer); + + if (style.width != 0) + { + new_style.width = style.width; + } + else + { + new_style.width = priv->style->width; + } + if (style.stroke_color != NULL) + { + new_style.stroke_color = g_strdup (style.stroke_color); + } + else + { + new_style.stroke_color = g_strdup (priv->style->stroke_color); + } + if (style.fill_color != NULL) + { + new_style.fill_color = g_strdup (style.fill_color); + } + else + { + new_style.fill_color = g_strdup (priv->style->fill_color); + } + + g_free (priv->style); + priv->style = g_memdup (&new_style, sizeof (GtkGisLayerStyle)); +} + /** * gtk_gis_layer_draw: * @layer: a #GtkGisLayer object. @@ -305,11 +381,12 @@ void gtk_gis_layer_draw (GtkGisLayer *layer, GooCanvasItem *root, GtkGisScale *scale) { GtkGisLayerPrivate *priv; - GList *cur; - GooCanvasItem *item; + g_return_if_fail (IS_GTK_GIS_LAYER (layer)); + g_return_if_fail (GOO_IS_CANVAS_ITEM (root)); + priv = GTK_GIS_LAYER_GET_PRIVATE (layer); gtk_gis_layer_fill_from_source (layer); @@ -319,7 +396,7 @@ gtk_gis_layer_draw (GtkGisLayer *layer, GooCanvasItem *root, GtkGisScale *scale) cur = g_list_first (priv->geometries); while (cur != NULL) { - item = gtk_gis_geometry_draw ((GtkGisGeometry *)cur->data, scale); + item = gtk_gis_geometry_draw ((GtkGisGeometry *)cur->data, scale, *priv->style); if (item != NULL) { diff --git a/src/layer.h b/src/layer.h index 31daa88..13d42f3 100644 --- a/src/layer.h +++ b/src/layer.h @@ -87,6 +87,9 @@ gboolean gtk_gis_layer_get_editable (GtkGisLayer *layer); void gtk_gis_layer_set_editable (GtkGisLayer *layer, gboolean editable); +GtkGisLayerStyle *gtk_gis_layer_get_style (GtkGisLayer *layer); +void gtk_gis_layer_set_style (GtkGisLayer *layer, GtkGisLayerStyle style); + void gtk_gis_layer_draw (GtkGisLayer *layer, GooCanvasItem *root, GtkGisScale *scale); diff --git a/tests/test1.gtkgis b/tests/test1.gtkgis index 090eb87..28baeca 100644 --- a/tests/test1.gtkgis +++ b/tests/test1.gtkgis @@ -16,12 +16,18 @@ + + -- 2.49.0