]> git.street.me.uk Git - andy/viking.git/blob - src/vikmaptype.c
Refactoring: use a GObject model to implement MapSource hierarchy tree
[andy/viking.git] / src / vikmaptype.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking
4  * Copyright (C) Guilhem Bonnefille 2009 <guilhem.bonnefille@gmail.com>
5  * 
6  * viking is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  * 
11  * viking is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along
17  * with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "vikmaptype.h"
24 #include "vikmapslayer_compat.h"
25
26 static guint8 map_type_get_uniq_id (VikMapSource *self);
27 static const gchar *map_type_get_label (VikMapSource *self);
28 static guint16 map_type_get_tilesize_x (VikMapSource *self);
29 static guint16 map_type_get_tilesize_y (VikMapSource *self);
30 static VikViewportDrawMode map_type_get_drawmode (VikMapSource *self);
31 static gboolean map_type_coord_to_mapcoord (VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
32 static void map_type_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikCoord *dest);
33 static int map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn);
34
35 typedef struct _VikMapTypePrivate VikMapTypePrivate;
36 struct _VikMapTypePrivate
37 {
38         gchar *label;
39         VikMapsLayer_MapType map_type;
40 };
41
42 #define VIK_MAP_TYPE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_TYPE, VikMapTypePrivate))
43
44
45 G_DEFINE_TYPE (VikMapType, vik_map_type, VIK_TYPE_MAP_SOURCE);
46
47 static void
48 vik_map_type_init (VikMapType *object)
49 {
50         VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(object);
51         priv->label = NULL;
52 }
53
54 VikMapType *
55 vik_map_type_new_with_id (VikMapsLayer_MapType map_type, const char *label)
56 {
57         VikMapType *ret = (VikMapType *)g_object_new(vik_map_type_get_type(), NULL);
58         VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(ret);
59         priv->map_type = map_type;
60         priv->label = g_strdup (label);
61         return ret;
62 }
63
64 static void
65 vik_map_type_finalize (GObject *object)
66 {
67         VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(object);
68         g_free (priv->label);
69         priv->label = NULL;
70
71         G_OBJECT_CLASS (vik_map_type_parent_class)->finalize (object);
72 }
73
74 static void
75 vik_map_type_class_init (VikMapTypeClass *klass)
76 {
77         GObjectClass* object_class = G_OBJECT_CLASS (klass);
78         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
79
80         /* Overiding methods */
81         parent_class->get_uniq_id =              map_type_get_uniq_id;
82         parent_class->get_label =                map_type_get_label;
83         parent_class->get_tilesize_x =           map_type_get_tilesize_x;
84         parent_class->get_tilesize_y =           map_type_get_tilesize_y;
85         parent_class->get_drawmode =             map_type_get_drawmode;
86         parent_class->coord_to_mapcoord =        map_type_coord_to_mapcoord;
87         parent_class->mapcoord_to_center_coord = map_type_mapcoord_to_center_coord;
88         parent_class->download =                 map_type_download;
89
90         g_type_class_add_private (klass, sizeof (VikMapTypePrivate));
91
92         object_class->finalize = vik_map_type_finalize;
93 }
94
95 static guint8
96 map_type_get_uniq_id (VikMapSource *self)
97 {
98     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
99         g_return_val_if_fail (priv != NULL, (guint8)0);
100
101         return priv->map_type.uniq_id;
102 }
103
104 static const gchar *
105 map_type_get_label (VikMapSource *self)
106 {
107     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
108         g_return_val_if_fail (priv != NULL, FALSE);
109
110         return priv->label;
111 }
112
113 static guint16
114 map_type_get_tilesize_x (VikMapSource *self)
115 {
116     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
117         g_return_val_if_fail (priv != NULL, (guint16)0);
118
119         return priv->map_type.tilesize_x;
120 }
121
122 static guint16
123 map_type_get_tilesize_y (VikMapSource *self)
124 {
125     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
126         g_return_val_if_fail (priv != NULL, (guint16)0);
127
128         return priv->map_type.tilesize_y;
129 }
130
131 static VikViewportDrawMode
132 map_type_get_drawmode (VikMapSource *self)
133 {
134     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
135         g_return_val_if_fail (priv != NULL, (VikViewportDrawMode)0);
136
137         return priv->map_type.drawmode;
138 }
139
140 static gboolean
141 map_type_coord_to_mapcoord (VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
142 {
143     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
144         g_return_val_if_fail (priv != NULL, FALSE);
145
146         return (priv->map_type.coord_to_mapcoord)(src, xzoom, yzoom, dest);
147 }
148
149 static void
150 map_type_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikCoord *dest)
151 {
152     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
153         g_return_if_fail (self != NULL);
154
155         return (priv->map_type.mapcoord_to_center_coord)(src, dest);
156 }
157
158 static int
159 map_type_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn)
160 {
161     VikMapTypePrivate *priv = VIK_MAP_TYPE_PRIVATE(self);
162         g_return_val_if_fail (priv != NULL, 0);
163
164         return (priv->map_type.download)(src, dest_fn);
165 }
166