]> Pileus Git - grits/blob - src/gis/gis-view.c
Lots of work on libGIS
[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 #include "gis-world.h"
23
24 /* Constants */
25 enum {
26         PROP_0,
27         PROP_TIME,
28         PROP_SITE,
29 };
30 enum {
31         SIG_TIME_CHANGED,
32         SIG_SITE_CHANGED,
33         SIG_LOCATION_CHANGED,
34         SIG_ROTATION_CHANGED,
35         NUM_SIGNALS,
36 };
37 static guint signals[NUM_SIGNALS];
38
39
40 /* Signal helpers */
41 static void _gis_view_emit_location_changed(GisView *view)
42 {
43         g_signal_emit(view, signals[SIG_LOCATION_CHANGED], 0, 
44                         view->location[0],
45                         view->location[1],
46                         view->location[2]);
47 }
48 static void _gis_view_emit_rotation_changed(GisView *view)
49 {
50         g_signal_emit(view, signals[SIG_ROTATION_CHANGED], 0, 
51                         view->rotation[0],
52                         view->rotation[1],
53                         view->rotation[2]);
54 }
55 static void _gis_view_emit_time_changed(GisView *view)
56 {
57         g_signal_emit(view, signals[SIG_TIME_CHANGED], 0,
58                         view->time);
59 }
60 static void _gis_view_emit_site_changed(GisView *view)
61 {
62         g_signal_emit(view, signals[SIG_SITE_CHANGED], 0,
63                         view->site);
64 }
65
66
67 /***********
68  * Methods *
69  ***********/
70 GisView *gis_view_new()
71 {
72         g_debug("GisView: new");
73         return g_object_new(GIS_TYPE_VIEW, NULL);
74 }
75
76 void gis_view_set_time(GisView *view, const char *time)
77 {
78         g_assert(GIS_IS_VIEW(view));
79         g_debug("GisView: set_time - time=%s", time);
80         g_free(view->time);
81         view->time = g_strdup(time);
82         _gis_view_emit_time_changed(view);
83 }
84
85 gchar *gis_view_get_time(GisView *view)
86 {
87         g_assert(GIS_IS_VIEW(view));
88         g_debug("GisView: get_time");
89         return view->time;
90 }
91
92 void gis_view_set_location(GisView *view, gdouble lat, gdouble lon, gdouble elev)
93 {
94         g_assert(GIS_IS_VIEW(view));
95         g_debug("GisView: set_location");
96         view->location[0] = lat;
97         view->location[1] = lon;
98         view->location[2] = elev;
99         _gis_view_emit_location_changed(view);
100 }
101
102 void gis_view_get_location(GisView *view, gdouble *lat, gdouble *lon, gdouble *elev)
103 {
104         g_assert(GIS_IS_VIEW(view));
105         //g_debug("GisView: get_location");
106         *lat  = view->location[0];
107         *lon  = view->location[1];
108         *elev = view->location[2];
109 }
110
111 void gis_view_pan(GisView *view, gdouble lat, gdouble lon, gdouble elev)
112 {
113         g_assert(GIS_IS_VIEW(view));
114         g_debug("GisView: pan - lat=%8.3f, lon=%8.3f, elev=%8.3f", lat, lon, elev);
115         view->location[0] += lat;
116         view->location[1] += lon;
117         view->location[2] += elev;
118         _gis_view_emit_location_changed(view);
119 }
120
121 void gis_view_zoom(GisView *view, gdouble scale)
122 {
123         g_assert(GIS_IS_VIEW(view));
124         g_debug("GisView: zoom");
125         view->location[2] *= scale;
126         _gis_view_emit_location_changed(view);
127 }
128
129 void gis_view_set_rotation(GisView *view, gdouble x, gdouble y, gdouble z)
130 {
131         g_assert(GIS_IS_VIEW(view));
132         g_debug("GisView: set_rotation");
133         view->rotation[0] = x;
134         view->rotation[1] = y;
135         view->rotation[2] = z;
136         _gis_view_emit_rotation_changed(view);
137 }
138
139 void gis_view_get_rotation(GisView *view, gdouble *x, gdouble *y, gdouble *z)
140 {
141         g_assert(GIS_IS_VIEW(view));
142         g_debug("GisView: get_rotation");
143         *x = view->rotation[0];
144         *y = view->rotation[1];
145         *z = view->rotation[2];
146 }
147
148 void gis_view_rotate(GisView *view, gdouble x, gdouble y, gdouble z)
149 {
150         g_assert(GIS_IS_VIEW(view));
151         g_debug("GisView: rotate - x=%.0f, y=%.0f, z=%.0f", x, y, z);
152         view->rotation[0] += x;
153         view->rotation[1] += y;
154         view->rotation[2] += z;
155         _gis_view_emit_rotation_changed(view);
156 }
157
158 /* To be deprecated, use {get,set}_location */
159 void gis_view_set_site(GisView *view, const gchar *site)
160 {
161         g_assert(GIS_IS_VIEW(view));
162         g_debug("GisView: set_site");
163         g_free(view->site);
164         view->site = g_strdup(site);
165         _gis_view_emit_site_changed(view);
166 }
167
168 gchar *gis_view_get_site(GisView *view)
169 {
170         g_assert(GIS_IS_VIEW(view));
171         g_debug("GisView: get_site - %s", view->site);
172         return view->site;
173 }
174
175
176 /****************
177  * GObject code *
178  ****************/
179 G_DEFINE_TYPE(GisView, gis_view, G_TYPE_OBJECT);
180 static void gis_view_init(GisView *self)
181 {
182         g_debug("GisView: init");
183         /* Default values */
184         self->time = g_strdup("");
185         self->site = g_strdup("");
186         self->location[0] = 40;
187         self->location[1] = -100;
188         self->location[2] = 1.5*EARTH_R;
189         self->rotation[0] = 0;
190         self->rotation[1] = 0;
191         self->rotation[2] = 0;
192 }
193 static void gis_view_dispose(GObject *gobject)
194 {
195         g_debug("GisView: dispose");
196         /* Drop references to other GObjects */
197         G_OBJECT_CLASS(gis_view_parent_class)->dispose(gobject);
198 }
199 static void gis_view_finalize(GObject *gobject)
200 {
201         g_debug("GisView: finalize");
202         GisView *self = GIS_VIEW(gobject);
203         g_free(self->time);
204         g_free(self->site);
205         G_OBJECT_CLASS(gis_view_parent_class)->finalize(gobject);
206 }
207 static void gis_view_set_property(GObject *object, guint property_id,
208                 const GValue *value, GParamSpec *pspec)
209 {
210         g_debug("GisView: set_property");
211         GisView *self = GIS_VIEW(object);
212         switch (property_id) {
213         case PROP_TIME:     gis_view_set_time(self, g_value_get_string(value)); break;
214         case PROP_SITE:     gis_view_set_site(self, g_value_get_string(value)); break;
215         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
216         }
217 }
218 static void gis_view_get_property(GObject *object, guint property_id,
219                 GValue *value, GParamSpec *pspec)
220 {
221         g_debug("GisView: get_property");
222         GisView *self = GIS_VIEW(object);
223         switch (property_id) {
224         case PROP_TIME:     g_value_set_string(value, gis_view_get_time(self)); break;
225         case PROP_SITE:     g_value_set_string(value, gis_view_get_site(self)); break;
226         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
227         }
228 }
229 static void gis_view_class_init(GisViewClass *klass)
230 {
231         g_debug("GisView: class_init");
232         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
233         gobject_class->dispose      = gis_view_dispose;
234         gobject_class->finalize     = gis_view_finalize;
235         gobject_class->get_property = gis_view_get_property;
236         gobject_class->set_property = gis_view_set_property;
237         g_object_class_install_property(gobject_class, PROP_TIME,
238                 g_param_spec_pointer(
239                         "time",
240                         "time of the current frame",
241                         "(format unknown)", 
242                         G_PARAM_READWRITE));
243         g_object_class_install_property(gobject_class, PROP_SITE,
244                 g_param_spec_pointer(
245                         "site",
246                         "site seen by the viewport",
247                         "Site of the viewport. Currently this is the name of the radar site.", 
248                         G_PARAM_READWRITE));
249         signals[SIG_TIME_CHANGED] = g_signal_new(
250                         "time-changed",
251                         G_TYPE_FROM_CLASS(gobject_class),
252                         G_SIGNAL_RUN_LAST,
253                         0,
254                         NULL,
255                         NULL,
256                         g_cclosure_marshal_VOID__STRING,
257                         G_TYPE_NONE,
258                         1,
259                         G_TYPE_STRING);
260         signals[SIG_SITE_CHANGED] = g_signal_new(
261                         "site-changed",
262                         G_TYPE_FROM_CLASS(gobject_class),
263                         G_SIGNAL_RUN_LAST,
264                         0,
265                         NULL,
266                         NULL,
267                         g_cclosure_marshal_VOID__STRING,
268                         G_TYPE_NONE,
269                         1,
270                         G_TYPE_STRING);
271         signals[SIG_LOCATION_CHANGED] = g_signal_new(
272                         "location-changed",
273                         G_TYPE_FROM_CLASS(gobject_class),
274                         G_SIGNAL_RUN_LAST,
275                         0,
276                         NULL,
277                         NULL,
278                         gis_cclosure_marshal_VOID__DOUBLE_DOUBLE_DOUBLE,
279                         G_TYPE_NONE,
280                         3,
281                         G_TYPE_DOUBLE,
282                         G_TYPE_DOUBLE,
283                         G_TYPE_DOUBLE);
284         signals[SIG_ROTATION_CHANGED] = g_signal_new(
285                         "rotation-changed",
286                         G_TYPE_FROM_CLASS(gobject_class),
287                         G_SIGNAL_RUN_LAST,
288                         0,
289                         NULL,
290                         NULL,
291                         gis_cclosure_marshal_VOID__DOUBLE_DOUBLE_DOUBLE,
292                         G_TYPE_NONE,
293                         3,
294                         G_TYPE_DOUBLE,
295                         G_TYPE_DOUBLE,
296                         G_TYPE_DOUBLE);
297 }