]> Pileus Git - grits/blob - src/gis/gis-view.c
Splitting GIS into a shared library, and a lot more
[grits] / src / gis / gis-view.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <glib.h>
19
20 #include "gis-marshal.h"
21 #include "gis-view.h"
22
23 /****************
24  * GObject code *
25  ****************/
26 /* Constants */
27 enum {
28         PROP_0,
29         PROP_TIME,
30         PROP_SITE,
31 };
32 enum {
33         SIG_TIME_CHANGED,
34         SIG_SITE_CHANGED,
35         SIG_LOCATION_CHANGED,
36         SIG_ROTATION_CHANGED,
37         NUM_SIGNALS,
38 };
39 static guint signals[NUM_SIGNALS];
40
41 /* Class/Object init */
42 G_DEFINE_TYPE(GisView, gis_view, G_TYPE_OBJECT);
43 static void gis_view_init(GisView *self)
44 {
45         g_debug("GisView: init");
46         /* Default values */
47         self->time = g_strdup("");
48         self->site = g_strdup("");
49         self->location[0] = 0;
50         self->location[1] = 0;
51         self->location[2] = -300*1000;
52         self->rotation[0] = 0;
53         self->rotation[1] = 0;
54         self->rotation[2] = 0;
55 }
56 static void gis_view_dispose(GObject *gobject)
57 {
58         g_debug("GisView: dispose");
59         /* Drop references to other GObjects */
60         G_OBJECT_CLASS(gis_view_parent_class)->dispose(gobject);
61 }
62 static void gis_view_finalize(GObject *gobject)
63 {
64         g_debug("GisView: finalize");
65         GisView *self = GIS_VIEW(gobject);
66         g_free(self->time);
67         g_free(self->site);
68         G_OBJECT_CLASS(gis_view_parent_class)->finalize(gobject);
69 }
70 static void gis_view_set_property(GObject *object, guint property_id,
71                 const GValue *value, GParamSpec *pspec)
72 {
73         g_debug("GisView: set_property");
74         GisView *self = GIS_VIEW(object);
75         switch (property_id) {
76         case PROP_TIME:     gis_view_set_time(self, g_value_get_string(value)); break;
77         case PROP_SITE:     gis_view_set_site(self, g_value_get_string(value)); break;
78         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
79         }
80 }
81 static void gis_view_get_property(GObject *object, guint property_id,
82                 GValue *value, GParamSpec *pspec)
83 {
84         g_debug("GisView: get_property");
85         GisView *self = GIS_VIEW(object);
86         switch (property_id) {
87         case PROP_TIME:     g_value_set_string(value, gis_view_get_time(self)); break;
88         case PROP_SITE:     g_value_set_string(value, gis_view_get_site(self)); break;
89         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
90         }
91 }
92 static void gis_view_class_init(GisViewClass *klass)
93 {
94         g_debug("GisView: class_init");
95         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
96         gobject_class->dispose      = gis_view_dispose;
97         gobject_class->finalize     = gis_view_finalize;
98         gobject_class->get_property = gis_view_get_property;
99         gobject_class->set_property = gis_view_set_property;
100         g_object_class_install_property(gobject_class, PROP_TIME,
101                 g_param_spec_pointer(
102                         "time",
103                         "time of the current frame",
104                         "(format unknown)", 
105                         G_PARAM_READWRITE));
106         g_object_class_install_property(gobject_class, PROP_SITE,
107                 g_param_spec_pointer(
108                         "site",
109                         "site seen by the viewport",
110                         "Site of the viewport. Currently this is the name of the radar site.", 
111                         G_PARAM_READWRITE));
112         signals[SIG_TIME_CHANGED] = g_signal_new(
113                         "time-changed",
114                         G_TYPE_FROM_CLASS(gobject_class),
115                         G_SIGNAL_RUN_LAST,
116                         0,
117                         NULL,
118                         NULL,
119                         g_cclosure_marshal_VOID__STRING,
120                         G_TYPE_NONE,
121                         1,
122                         G_TYPE_STRING);
123         signals[SIG_SITE_CHANGED] = g_signal_new(
124                         "site-changed",
125                         G_TYPE_FROM_CLASS(gobject_class),
126                         G_SIGNAL_RUN_LAST,
127                         0,
128                         NULL,
129                         NULL,
130                         g_cclosure_marshal_VOID__STRING,
131                         G_TYPE_NONE,
132                         1,
133                         G_TYPE_STRING);
134         signals[SIG_LOCATION_CHANGED] = g_signal_new(
135                         "location-changed",
136                         G_TYPE_FROM_CLASS(gobject_class),
137                         G_SIGNAL_RUN_LAST,
138                         0,
139                         NULL,
140                         NULL,
141                         gis_cclosure_marshal_VOID__DOUBLE_DOUBLE_DOUBLE,
142                         G_TYPE_NONE,
143                         3,
144                         G_TYPE_DOUBLE,
145                         G_TYPE_DOUBLE,
146                         G_TYPE_DOUBLE);
147         signals[SIG_ROTATION_CHANGED] = g_signal_new(
148                         "rotation-changed",
149                         G_TYPE_FROM_CLASS(gobject_class),
150                         G_SIGNAL_RUN_LAST,
151                         0,
152                         NULL,
153                         NULL,
154                         gis_cclosure_marshal_VOID__DOUBLE_DOUBLE_DOUBLE,
155                         G_TYPE_NONE,
156                         3,
157                         G_TYPE_DOUBLE,
158                         G_TYPE_DOUBLE,
159                         G_TYPE_DOUBLE);
160 }
161
162 /* Signal helpers */
163 static void _gis_view_emit_location_changed(GisView *view)
164 {
165         g_signal_emit(view, signals[SIG_LOCATION_CHANGED], 0, 
166                         view->location[0],
167                         view->location[1],
168                         view->location[2]);
169 }
170 static void _gis_view_emit_rotation_changed(GisView *view)
171 {
172         g_signal_emit(view, signals[SIG_ROTATION_CHANGED], 0, 
173                         view->rotation[0],
174                         view->rotation[1],
175                         view->rotation[2]);
176 }
177 static void _gis_view_emit_time_changed(GisView *view)
178 {
179         g_signal_emit(view, signals[SIG_TIME_CHANGED], 0,
180                         view->time);
181 }
182 static void _gis_view_emit_site_changed(GisView *view)
183 {
184         g_signal_emit(view, signals[SIG_SITE_CHANGED], 0,
185                         view->site);
186 }
187
188
189 /***********
190  * Methods *
191  ***********/
192 GisView *gis_view_new()
193 {
194         g_debug("GisView: new");
195         return g_object_new(GIS_TYPE_VIEW, NULL);
196 }
197
198 void gis_view_set_time(GisView *view, const char *time)
199 {
200         g_assert(GIS_IS_VIEW(view));
201         g_debug("GisView: set_time - time=%s", time);
202         g_free(view->time);
203         view->time = g_strdup(time);
204         _gis_view_emit_time_changed(view);
205 }
206
207 gchar *gis_view_get_time(GisView *view)
208 {
209         g_assert(GIS_IS_VIEW(view));
210         g_debug("GisView: get_time");
211         return view->time;
212 }
213
214 void gis_view_set_location(GisView *view, gdouble x, gdouble y, gdouble z)
215 {
216         g_assert(GIS_IS_VIEW(view));
217         g_debug("GisView: set_location");
218         view->location[0] = x;
219         view->location[1] = y;
220         view->location[2] = z;
221         _gis_view_emit_location_changed(view);
222 }
223
224 void gis_view_get_location(GisView *view, gdouble *x, gdouble *y, gdouble *z)
225 {
226         g_assert(GIS_IS_VIEW(view));
227         g_debug("GisView: get_location");
228         *x = view->location[0];
229         *y = view->location[1];
230         *z = view->location[2];
231 }
232
233 void gis_view_pan(GisView *view, gdouble x, gdouble y, gdouble z)
234 {
235         g_assert(GIS_IS_VIEW(view));
236         g_debug("GisView: pan - x=%.0f, y=%.0f, z=%.0f", x, y, z);
237         view->location[0] += x;
238         view->location[1] += y;
239         view->location[2] += z;
240         _gis_view_emit_location_changed(view);
241 }
242
243 void gis_view_zoom(GisView *view, gdouble scale)
244 {
245         g_assert(GIS_IS_VIEW(view));
246         g_debug("GisView: zoom");
247         view->location[2] *= scale;
248         _gis_view_emit_location_changed(view);
249 }
250
251 void gis_view_set_rotation(GisView *view, gdouble x, gdouble y, gdouble z)
252 {
253         g_assert(GIS_IS_VIEW(view));
254         g_debug("GisView: set_rotation");
255         view->rotation[0] = x;
256         view->rotation[1] = y;
257         view->rotation[2] = z;
258         _gis_view_emit_rotation_changed(view);
259 }
260
261 void gis_view_get_rotation(GisView *view, gdouble *x, gdouble *y, gdouble *z)
262 {
263         g_assert(GIS_IS_VIEW(view));
264         g_debug("GisView: get_rotation");
265         *x = view->rotation[0];
266         *y = view->rotation[1];
267         *z = view->rotation[2];
268 }
269
270 void gis_view_rotate(GisView *view, gdouble x, gdouble y, gdouble z)
271 {
272         g_assert(GIS_IS_VIEW(view));
273         g_debug("GisView: rotate - x=%.0f, y=%.0f, z=%.0f", x, y, z);
274         view->rotation[0] += x;
275         view->rotation[1] += y;
276         view->rotation[2] += z;
277         _gis_view_emit_rotation_changed(view);
278 }
279
280 /* To be deprecated, use {get,set}_location */
281 void gis_view_set_site(GisView *view, const gchar *site)
282 {
283         g_assert(GIS_IS_VIEW(view));
284         g_debug("GisView: set_site");
285         g_free(view->site);
286         view->site = g_strdup(site);
287         _gis_view_emit_site_changed(view);
288 }
289
290 gchar *gis_view_get_site(GisView *view)
291 {
292         g_assert(GIS_IS_VIEW(view));
293         g_debug("GisView: get_site - %s", view->site);
294         return view->site;
295 }