]> git.street.me.uk Git - andy/viking.git/blob - src/vikmapsourcedefault.c
Refactoring: use a GObject model to implement MapSource hierarchy tree
[andy/viking.git] / src / vikmapsourcedefault.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
20 #include "vikmapsourcedefault.h"
21 #include "vikenumtypes.h"
22
23 static guint8 map_source_get_uniq_id (VikMapSource *self);
24 static const gchar *map_source_get_label (VikMapSource *self);
25 static guint16 map_source_get_tilesize_x (VikMapSource *self);
26 static guint16 map_source_get_tilesize_y (VikMapSource *self);
27 static VikViewportDrawMode map_source_get_drawmode (VikMapSource *self);
28
29 typedef struct _VikMapSourceDefaultPrivate VikMapSourceDefaultPrivate;
30 struct _VikMapSourceDefaultPrivate
31 {
32         guint8 uniq_id;
33         gchar *label;
34         guint16 tilesize_x;
35         guint16 tilesize_y;
36         VikViewportDrawMode drawmode;
37 };
38
39 #define VIK_MAP_SOURCE_DEFAULT_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_MAP_SOURCE_DEFAULT, VikMapSourceDefaultPrivate))
40
41 /* properties */
42 enum
43 {
44   PROP_0,
45
46   PROP_ID,
47   PROP_LABEL,
48   PROP_TILESIZE_X,
49   PROP_TILESIZE_Y,
50   PROP_DRAWMODE,
51 };
52
53 G_DEFINE_TYPE_EXTENDED (VikMapSourceDefault, vik_map_source_default, VIK_TYPE_MAP_SOURCE, (GTypeFlags)G_TYPE_FLAG_ABSTRACT,);
54
55 static void
56 vik_map_source_default_init (VikMapSourceDefault *object)
57 {
58   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
59   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
60
61   priv->label = NULL;
62 }
63
64 static void
65 vik_map_source_default_finalize (GObject *object)
66 {
67   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
68   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
69
70   g_free (priv->label);
71   priv->label = NULL;
72         
73   G_OBJECT_CLASS (vik_map_source_default_parent_class)->finalize (object);
74 }
75
76 static void
77 vik_map_source_default_set_property (GObject      *object,
78                                      guint         property_id,
79                                      const GValue *value,
80                                      GParamSpec   *pspec)
81 {
82   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
83   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
84
85   switch (property_id)
86     {
87     case PROP_ID:
88       priv->uniq_id = g_value_get_uint (value);
89       break;
90
91         case PROP_LABEL:
92       g_free (priv->label);
93       priv->label = g_strdup(g_value_get_string (value));
94       break;
95
96     case PROP_TILESIZE_X:
97       priv->tilesize_x = g_value_get_uint (value);
98       break;
99
100     case PROP_TILESIZE_Y:
101       priv->tilesize_y = g_value_get_uint (value);
102       break;
103
104     case PROP_DRAWMODE:
105       priv->drawmode = g_value_get_enum(value);
106       break;
107
108     default:
109       /* We don't have any other property... */
110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111       break;
112     }
113 }
114
115 static void
116 vik_map_source_default_get_property (GObject    *object,
117                                      guint       property_id,
118                                      GValue     *value,
119                                      GParamSpec *pspec)
120 {
121   VikMapSourceDefault *self = VIK_MAP_SOURCE_DEFAULT (object);
122   VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE (self);
123
124   switch (property_id)
125     {
126     case PROP_ID:
127       g_value_set_uint (value, priv->uniq_id);
128       break;
129
130     case PROP_LABEL:
131       g_value_set_string (value, priv->label);
132       break;
133
134     case PROP_TILESIZE_X:
135       g_value_set_uint (value, priv->tilesize_x);
136       break;
137
138     case PROP_TILESIZE_Y:
139       g_value_set_uint (value, priv->tilesize_y);
140       break;
141
142     case PROP_DRAWMODE:
143       g_value_set_enum (value, priv->drawmode);
144       break;
145
146     default:
147       /* We don't have any other property... */
148       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
149       break;
150     }
151 }
152
153 static void
154 vik_map_source_default_class_init (VikMapSourceDefaultClass *klass)
155 {
156         GObjectClass* object_class = G_OBJECT_CLASS (klass);
157         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
158     GParamSpec *pspec = NULL;
159         
160         object_class->set_property = vik_map_source_default_set_property;
161     object_class->get_property = vik_map_source_default_get_property;
162         
163         /* Overiding methods */
164         parent_class->get_uniq_id =    map_source_get_uniq_id;
165         parent_class->get_label =      map_source_get_label;
166         parent_class->get_tilesize_x = map_source_get_tilesize_x;
167         parent_class->get_tilesize_y = map_source_get_tilesize_y;
168         parent_class->get_drawmode =   map_source_get_drawmode;
169
170         pspec = g_param_spec_uint ("id",
171                                    "Id of the tool",
172                                "Set the id",
173                                0  /* minimum value */,
174                                G_MAXUINT8 /* maximum value */,
175                                0  /* default value */,
176                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
177         g_object_class_install_property (object_class, PROP_ID, pspec);
178
179         pspec = g_param_spec_string ("label",
180                                      "Label",
181                                      "The label of the map source",
182                                      "<no-set>" /* default value */,
183                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
184         g_object_class_install_property (object_class, PROP_LABEL, pspec);
185
186         pspec = g_param_spec_uint ("tilesize-x",
187                                    "TileSizeX",
188                                "Set the size of the tile (x)",
189                                0  /* minimum value */,
190                                G_MAXUINT16 /* maximum value */,
191                                0  /* default value */,
192                                G_PARAM_READWRITE);
193         g_object_class_install_property (object_class, PROP_TILESIZE_X, pspec);
194
195         pspec = g_param_spec_uint ("tilesize-y",
196                                    "TileSizeY",
197                                "Set the size of the tile (y)",
198                                0  /* minimum value */,
199                                G_MAXUINT16 /* maximum value */,
200                                0  /* default value */,
201                                G_PARAM_READWRITE);
202         g_object_class_install_property (object_class, PROP_TILESIZE_Y, pspec);
203
204         pspec = g_param_spec_enum("drawmode",
205                               "Drawmode",
206                               "The mode used to draw map",
207                               VIK_TYPE_VIEWPORT_DRAW_MODE,
208                               VIK_VIEWPORT_DRAWMODE_UTM,
209                               G_PARAM_READWRITE);
210     g_object_class_install_property(object_class, PROP_DRAWMODE, pspec);                                    
211
212         g_type_class_add_private (klass, sizeof (VikMapSourceDefaultPrivate));
213
214         object_class->finalize = vik_map_source_default_finalize;
215 }
216
217 static guint8
218 map_source_get_uniq_id (VikMapSource *self)
219 {
220         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint8)0);
221         
222         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
223
224         return priv->uniq_id;
225 }
226
227 static const gchar *
228 map_source_get_label (VikMapSource *self)
229 {
230         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), NULL);
231         
232         VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
233
234         return priv->label;
235 }
236
237 static guint16
238 map_source_get_tilesize_x (VikMapSource *self)
239 {
240         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
241
242     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
243
244         return priv->tilesize_x;
245 }
246
247 static guint16
248 map_source_get_tilesize_y (VikMapSource *self)
249 {
250         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (guint16)0);
251
252     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
253
254         return priv->tilesize_y;
255 }
256
257 static VikViewportDrawMode
258 map_source_get_drawmode (VikMapSource *self)
259 {
260         g_return_val_if_fail (VIK_IS_MAP_SOURCE_DEFAULT(self), (VikViewportDrawMode)0);
261
262     VikMapSourceDefaultPrivate *priv = VIK_MAP_SOURCE_DEFAULT_PRIVATE(self);
263
264         return priv->drawmode;
265 }