]> Pileus Git - grits/blob - src/aweather-view.c
Example plugin in GObject plugin format
[grits] / src / aweather-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 "marshal.h"
21 #include "aweather-view.h"
22
23 /****************
24  * GObject code *
25  ****************/
26 G_DEFINE_TYPE(AWeatherView, aweather_view, G_TYPE_OBJECT);
27
28 enum {
29         PROP_0,
30         PROP_TIME,
31         PROP_SITE,
32 };
33
34 enum {
35         SIG_TIME_CHANGED,
36         SIG_SITE_CHANGED,
37         SIG_LOCATION_CHANGED,
38         SIG_REFRESH,
39         NUM_SIGNALS,
40 };
41
42 static guint signals[NUM_SIGNALS];
43
44 static void aweather_view_init(AWeatherView *self)
45 {
46         //g_message("aweather_view_init");
47         /* Default values */
48         self->time = g_strdup("");
49         self->site = g_strdup("");
50 }
51
52 static GObject *aweather_view_constructor(GType gtype, guint n_properties,
53                 GObjectConstructParam *properties)
54 {
55         //g_message("aweather_view_constructor");
56         GObjectClass *parent_class = G_OBJECT_CLASS(aweather_view_parent_class);
57         return  parent_class->constructor(gtype, n_properties, properties);
58 }
59
60 static void aweather_view_dispose (GObject *gobject)
61 {
62         //g_message("aweather_view_dispose");
63         /* Drop references to other GObjects */
64         G_OBJECT_CLASS(aweather_view_parent_class)->dispose(gobject);
65 }
66
67 static void aweather_view_finalize(GObject *gobject)
68 {
69         //g_message("aweather_view_finalize");
70         AWeatherView *self = AWEATHER_VIEW(gobject);
71         g_free(self->site);
72         G_OBJECT_CLASS(aweather_view_parent_class)->finalize(gobject);
73 }
74
75 static void aweather_view_set_property(GObject *object, guint property_id,
76                 const GValue *value, GParamSpec *pspec)
77 {
78         //g_message("aweather_view_set_property");
79         AWeatherView *self = AWEATHER_VIEW(object);
80         switch (property_id) {
81         case PROP_TIME:     aweather_view_set_time(self, g_value_get_string(value)); break;
82         case PROP_SITE:     aweather_view_set_site(self, g_value_get_string(value)); break;
83         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
84         }
85 }
86
87 static void aweather_view_get_property(GObject *object, guint property_id,
88                 GValue *value, GParamSpec *pspec)
89 {
90         //g_message("aweather_view_get_property");
91         AWeatherView *self = AWEATHER_VIEW(object);
92         switch (property_id) {
93         case PROP_TIME:     g_value_set_string(value, aweather_view_get_time(self)); break;
94         case PROP_SITE:     g_value_set_string(value, aweather_view_get_site(self)); break;
95         default:            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
96         }
97 }
98
99 static void aweather_view_class_init(AWeatherViewClass *klass)
100 {
101         //g_message("aweather_view_class_init");
102         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
103         gobject_class->constructor  = aweather_view_constructor;
104         gobject_class->dispose      = aweather_view_dispose;
105         gobject_class->finalize     = aweather_view_finalize;
106         gobject_class->get_property = aweather_view_get_property;
107         gobject_class->set_property = aweather_view_set_property;
108         g_object_class_install_property(gobject_class, PROP_TIME,
109                 g_param_spec_pointer(
110                         "time",
111                         "time of the current frame",
112                         "(format unknown)", 
113                         G_PARAM_READWRITE));
114         g_object_class_install_property(gobject_class, PROP_SITE,
115                 g_param_spec_pointer(
116                         "site",
117                         "site seen by the viewport",
118                         "Site of the viewport. Currently this is the name of the radar site.", 
119                         G_PARAM_READWRITE));
120         signals[SIG_TIME_CHANGED] = g_signal_new(
121                         "time-changed",
122                         G_TYPE_FROM_CLASS(gobject_class),
123                         G_SIGNAL_RUN_LAST,
124                         0,
125                         NULL,
126                         NULL,
127                         g_cclosure_marshal_VOID__INT,
128                         G_TYPE_NONE,
129                         1,
130                         G_TYPE_STRING);
131         signals[SIG_SITE_CHANGED] = g_signal_new(
132                         "site-changed",
133                         G_TYPE_FROM_CLASS(gobject_class),
134                         G_SIGNAL_RUN_LAST,
135                         0,
136                         NULL,
137                         NULL,
138                         g_cclosure_marshal_VOID__STRING,
139                         G_TYPE_NONE,
140                         1,
141                         G_TYPE_STRING);
142         signals[SIG_LOCATION_CHANGED] = g_signal_new(
143                         "location-changed",
144                         G_TYPE_FROM_CLASS(gobject_class),
145                         G_SIGNAL_RUN_LAST,
146                         0,
147                         NULL,
148                         NULL,
149                         aweather_cclosure_marshal_VOID__DOUBLE_DOUBLE_DOUBLE,
150                         G_TYPE_NONE,
151                         3,
152                         G_TYPE_DOUBLE,
153                         G_TYPE_DOUBLE,
154                         G_TYPE_DOUBLE);
155         signals[SIG_REFRESH] = g_signal_new(
156                         "refresh",
157                         G_TYPE_FROM_CLASS(gobject_class),
158                         G_SIGNAL_RUN_LAST,
159                         0,
160                         NULL,
161                         NULL,
162                         g_cclosure_marshal_VOID__VOID,
163                         G_TYPE_NONE,
164                         0);
165
166 }
167
168 /* Signal helpers */
169 static void _aweather_view_emit_location_changed(AWeatherView *view)
170 {
171         g_signal_emit(view, signals[SIG_LOCATION_CHANGED], 0, 
172                         view->location[0],
173                         view->location[1],
174                         view->location[2]);
175 }
176 static void _aweather_view_emit_time_changed(AWeatherView *view)
177 {
178         g_signal_emit(view, signals[SIG_TIME_CHANGED], 0,
179                         view->time);
180 }
181 static void _aweather_view_emit_site_changed(AWeatherView *view)
182 {
183         g_signal_emit(view, signals[SIG_SITE_CHANGED], 0,
184                         view->site);
185 }
186 static void _aweather_view_emit_refresh(AWeatherView *view)
187 {
188         g_signal_emit(view, signals[SIG_REFRESH], 0);
189 }
190
191
192 /***********
193  * Methods *
194  ***********/
195 AWeatherView *aweather_view_new()
196 {
197         //g_message("aweather_view_new");
198         return g_object_new(AWEATHER_TYPE_VIEW, NULL);
199 }
200
201 void aweather_view_set_time(AWeatherView *view, const char *time)
202 {
203         g_assert(AWEATHER_IS_VIEW(view));
204         //g_message("aweather_view:set_time: setting time to %s", time);
205         g_free(view->time);
206         view->time = g_strdup(time);
207         _aweather_view_emit_time_changed(view);
208 }
209
210 gchar *aweather_view_get_time(AWeatherView *view)
211 {
212         g_assert(AWEATHER_IS_VIEW(view));
213         //g_message("aweather_view_get_time");
214         return view->time;
215 }
216
217 void aweather_view_get_location(AWeatherView *view, gdouble *x, gdouble *y, gdouble *z)
218 {
219         g_assert(AWEATHER_IS_VIEW(view));
220         //g_message("aweather_view_get_location");
221         *x = view->location[0];
222         *y = view->location[1];
223         *z = view->location[2];
224 }
225
226 void aweather_view_set_location(AWeatherView *view, gdouble x, gdouble y, gdouble z)
227 {
228         g_assert(AWEATHER_IS_VIEW(view));
229         //g_message("aweather_view_set_location");
230         view->location[0] = x;
231         view->location[1] = y;
232         view->location[2] = z;
233         _aweather_view_emit_location_changed(view);
234 }
235
236 void aweather_view_pan(AWeatherView *view, gdouble x, gdouble y, gdouble z)
237 {
238         g_assert(AWEATHER_IS_VIEW(view));
239         g_message("aweather_view_pan: %f, %f, %f", x, y, z);
240         view->location[0] += x;
241         view->location[1] += y;
242         view->location[2] += z;
243         _aweather_view_emit_location_changed(view);
244 }
245
246 void aweather_view_zoom(AWeatherView *view, gdouble scale)
247 {
248         g_assert(AWEATHER_IS_VIEW(view));
249         view->location[2] *= scale;
250         _aweather_view_emit_location_changed(view);
251 }
252
253 void aweather_view_refresh(AWeatherView *view)
254 {
255         g_message("aweather_view_refresh: ..");
256         _aweather_view_emit_refresh(view);
257 }
258
259 void aweather_view_set_site(AWeatherView *view, const gchar *site)
260 {
261         g_assert(AWEATHER_IS_VIEW(view));
262         //g_message("aweather_view_set_site");
263         g_free(view->site);
264         view->site = g_strdup(site);
265         _aweather_view_emit_site_changed(view);
266 }
267
268 gchar *aweather_view_get_site(AWeatherView *view)
269 {
270         g_assert(AWEATHER_IS_VIEW(view));
271         //g_message("aweather_view_get_site");
272         return view->site;
273 }
274