]> Pileus Git - aweather/blobdiff - src/plugins/radar.c
Add clickable radar markers
[aweather] / src / plugins / radar.c
index cc2e1c4988b2d47de65d977a39d154a33abbc5e8..2dcea83986dc6a0ad45400998a8c0e85d8789b6b 100644 (file)
@@ -30,7 +30,9 @@
 #include "level2.h"
 #include "../aweather-location.h"
 
-static void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new)
+#include "compat.h"
+
+static void aweather_bin_set_child(GtkBin *bin, GtkWidget *new)
 {
        GtkWidget *old = gtk_bin_get_child(bin);
        if (old)
@@ -100,6 +102,7 @@ struct _RadarSite {
        guint           time_id;     // "time-changed"     callback ID
        guint           refresh_id;  // "refresh"          callback ID
        guint           location_id; // "locaiton-changed" callback ID
+       guint           idle_source; // _site_update_end idle source
 };
 
 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
@@ -122,18 +125,20 @@ gboolean _site_update_end(gpointer _site)
                g_warning("RadarSite: update_end - %s", site->message);
                const char *fmt = "http://forecast.weather.gov/product.php?site=NWS&product=FTM&format=TXT&issuedby=%s";
                char       *uri = g_strdup_printf(fmt, site->city->code+1);
-               GtkWidget  *box = gtk_vbox_new(TRUE, 0);
+               GtkWidget  *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
                GtkWidget  *msg = gtk_label_new(site->message);
                GtkWidget  *btn = gtk_link_button_new_with_label(uri, "View Radar Status");
+               gtk_box_set_homogeneous(GTK_BOX(box), TRUE);
                gtk_box_pack_start(GTK_BOX(box), msg, TRUE, TRUE, 0);
                gtk_box_pack_start(GTK_BOX(box), btn, TRUE, TRUE, 0);
-               _gtk_bin_set_child(GTK_BIN(site->config), box);
+               aweather_bin_set_child(GTK_BIN(site->config), box);
                g_free(uri);
        } else {
-               _gtk_bin_set_child(GTK_BIN(site->config),
+               aweather_bin_set_child(GTK_BIN(site->config),
                                aweather_level2_get_config(site->level2));
        }
        site->status = STATUS_LOADED;
+       site->idle_source = 0;
        return FALSE;
 }
 gpointer _site_update_thread(gpointer _site)
@@ -192,7 +197,8 @@ gpointer _site_update_thread(gpointer _site)
                        GRITS_LEVEL_WORLD+3, TRUE);
 
 out:
-       g_idle_add(_site_update_end, site);
+       if (!site->idle_source)
+               site->idle_source = g_idle_add(_site_update_end, site);
        return NULL;
 }
 void _site_update(RadarSite *site)
@@ -208,7 +214,7 @@ void _site_update(RadarSite *site)
        /* Add a progress bar */
        GtkWidget *progress = gtk_progress_bar_new();
        gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
-       _gtk_bin_set_child(GTK_BIN(site->config), progress);
+       aweather_bin_set_child(GTK_BIN(site->config), progress);
 
        /* Remove old volume */
        g_debug("RadarSite: update - remove - %s", site->city->code);
@@ -219,7 +225,7 @@ void _site_update(RadarSite *site)
 
        /* Fork loading right away so updating the
         * list of times doesn't take too long */
-       g_thread_create(_site_update_thread, site, FALSE, NULL);
+       g_thread_new("site-update-thread", _site_update_thread, site);
 }
 
 /* RadarSite methods */
@@ -234,6 +240,9 @@ void radar_site_unload(RadarSite *site)
                g_signal_handler_disconnect(site->viewer, site->time_id);
        if (site->refresh_id)
                g_signal_handler_disconnect(site->viewer, site->refresh_id);
+       if (site->idle_source)
+               g_source_remove(site->idle_source);
+       site->idle_source = 0;
 
        /* Remove tab */
        if (site->config)
@@ -290,15 +299,24 @@ void _site_on_location_changed(GritsViewer *viewer,
                radar_site_unload(site);
 }
 
-gboolean _site_add_marker(RadarSite *site)
+static gboolean on_marker_clicked(GritsObject *marker, GdkEvent *event, RadarSite *site)
 {
-       site->marker = grits_marker_new(site->city->name);
-       GRITS_OBJECT(site->marker)->center = site->city->pos;
-       GRITS_OBJECT(site->marker)->lod    = EARTH_R*0.75*site->city->lod;
-       grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
-                       GRITS_LEVEL_OVERLAY, FALSE);
-       return FALSE;
+       GritsViewer *viewer = site->viewer;
+       GritsPoint center = marker->center;
+       grits_viewer_set_location(viewer, center.lat, center.lon, EARTH_R/35);
+       grits_viewer_set_rotation(viewer, 0, 0, 0);
+       /* Recursivly set notebook tabs */
+       GtkWidget *widget, *parent;
+       for (widget = site->config; widget; widget = parent) {
+               parent = gtk_widget_get_parent(widget);
+               if (GTK_IS_NOTEBOOK(parent)) {
+                       gint i = gtk_notebook_page_num(GTK_NOTEBOOK(parent), widget);
+                       gtk_notebook_set_current_page(GTK_NOTEBOOK(parent), i);
+               }
+       }
+       return TRUE;
 }
+
 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
                GritsViewer *viewer, GritsPrefs *prefs, GritsHttp *http)
 {
@@ -319,7 +337,14 @@ RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
        _site_on_location_changed(viewer, lat, lon, elev, site);
 
        /* Add marker */
-       _site_add_marker(site);
+       site->marker = grits_marker_new(site->city->name);
+       GRITS_OBJECT(site->marker)->center = site->city->pos;
+       GRITS_OBJECT(site->marker)->lod    = EARTH_R*0.75*site->city->lod;
+       grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
+                       GRITS_LEVEL_HUD, FALSE);
+       g_signal_connect(site->marker, "clicked",
+                       G_CALLBACK(on_marker_clicked), site);
+       grits_object_set_cursor(GRITS_OBJECT(site->marker), GDK_HAND2);
 
        /* Connect signals */
        site->location_id  = g_signal_connect(viewer, "location-changed",
@@ -355,13 +380,14 @@ struct _RadarConus {
        GtkWidget   *config;
        time_t       time;
        const gchar *message;
-       GStaticMutex loading;
+       GMutex       loading;
 
        gchar       *path;
        GritsTile   *tile[2];
 
        guint        time_id;     // "time-changed"     callback ID
        guint        refresh_id;  // "refresh"          callback ID
+       guint        idle_source; // _conus_update_end idle source
 };
 
 void _conus_update_loading(gchar *file, goffset cur,
@@ -380,14 +406,11 @@ void _conus_update_loading(gchar *file, goffset cur,
 /* Copy images to graphics memory */
 static void _conus_update_end_copy(GritsTile *tile, guchar *pixels)
 {
-       if (!tile->data) {
-               tile->data = g_new0(guint, 1);
-               glGenTextures(1, tile->data);
-       }
+       if (!tile->tex)
+               glGenTextures(1, &tile->tex);
 
        gchar *clear = g_malloc0(2048*2048*4);
-       guint *tex = tile->data;
-       glBindTexture(GL_TEXTURE_2D, *tex);
+       glBindTexture(GL_TEXTURE_2D, tile->tex);
 
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
@@ -451,7 +474,7 @@ gboolean _conus_update_end(gpointer _conus)
        /* Check error status */
        if (conus->message) {
                g_warning("Conus: update_end - %s", conus->message);
-               _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
+               aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
                goto out;
        }
 
@@ -460,7 +483,7 @@ gboolean _conus_update_end(gpointer _conus)
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
        if (!pixbuf || error) {
                g_warning("Conus: update_end - error loading pixbuf: %s", conus->path);
-               _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
+               aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
                g_remove(conus->path);
                goto out;
        }
@@ -484,13 +507,14 @@ gboolean _conus_update_end(gpointer _conus)
 
        /* Update GUI */
        gchar *label = g_path_get_basename(conus->path);
-       _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
-       gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
+       aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
+       grits_viewer_queue_draw(conus->viewer);
        g_free(label);
 
 out:
+       conus->idle_source = 0;
        g_free(conus->path);
-       g_static_mutex_unlock(&conus->loading);
+       g_mutex_unlock(&conus->loading);
        return FALSE;
 }
 
@@ -542,13 +566,14 @@ gpointer _conus_update_thread(gpointer _conus)
 
 out:
        g_debug("Conus: update_thread - done");
-       g_idle_add(_conus_update_end, conus);
+       if (!conus->idle_source)
+               conus->idle_source = g_idle_add(_conus_update_end, conus);
        return NULL;
 }
 
 void _conus_update(RadarConus *conus)
 {
-       if (!g_static_mutex_trylock(&conus->loading))
+       if (!g_mutex_trylock(&conus->loading))
                return;
        conus->time = grits_viewer_get_time(conus->viewer);
        g_debug("Conus: update - %d",
@@ -557,9 +582,9 @@ void _conus_update(RadarConus *conus)
        /* Add a progress bar */
        GtkWidget *progress = gtk_progress_bar_new();
        gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
-       _gtk_bin_set_child(GTK_BIN(conus->config), progress);
+       aweather_bin_set_child(GTK_BIN(conus->config), progress);
 
-       g_thread_create(_conus_update_thread, conus, FALSE, NULL);
+       g_thread_new("conus-update-thread", _conus_update_thread, conus);
 }
 
 RadarConus *radar_conus_new(GtkWidget *pconfig,
@@ -569,7 +594,7 @@ RadarConus *radar_conus_new(GtkWidget *pconfig,
        conus->viewer  = g_object_ref(viewer);
        conus->http    = http;
        conus->config  = gtk_alignment_new(0, 0, 1, 1);
-       g_static_mutex_init(&conus->loading);
+       g_mutex_init(&conus->loading);
 
        gdouble south =  CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT;
        gdouble east  =  CONUS_WEST  + CONUS_DEG_PER_PX*CONUS_WIDTH;
@@ -598,14 +623,13 @@ void radar_conus_free(RadarConus *conus)
 {
        g_signal_handler_disconnect(conus->viewer, conus->time_id);
        g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
+       if (conus->idle_source)
+               g_source_remove(conus->idle_source);
 
        for (int i = 0; i < 2; i++) {
                GritsTile *tile = conus->tile[i];
-               if (tile->data) {
-                       glDeleteTextures(1, tile->data);
-                       g_free(tile->data);
-               }
                grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile));
+               g_object_unref(tile);
        }
 
        g_object_unref(conus->viewer);
@@ -680,7 +704,7 @@ out:
 }
 
 static void _update_hidden(GtkNotebook *notebook,
-               GtkNotebookPage *page, guint page_num, gpointer viewer)
+               gpointer _, guint page_num, gpointer viewer)
 {
        g_debug("GritsPluginRadar: _update_hidden - 0..%d = %d",
                        gtk_notebook_get_n_pages(notebook), page_num);
@@ -703,7 +727,7 @@ static void _update_hidden(GtkNotebook *notebook,
                        g_warning("GritsPluginRadar: _update_hidden - no site or counus found");
                }
        }
-       gtk_widget_queue_draw(GTK_WIDGET(viewer));
+       grits_viewer_queue_draw(viewer);
 }
 
 /* Methods */
@@ -712,8 +736,8 @@ GritsPluginRadar *grits_plugin_radar_new(GritsViewer *viewer, GritsPrefs *prefs)
        /* TODO: move to constructor if possible */
        g_debug("GritsPluginRadar: new");
        GritsPluginRadar *self = g_object_new(GRITS_TYPE_PLUGIN_RADAR, NULL);
-       self->viewer = viewer;
-       self->prefs  = prefs;
+       self->viewer = g_object_ref(viewer);
+       self->prefs  = g_object_ref(prefs);
 
        /* Setup page switching */
        self->tab_id = g_signal_connect(self->config, "switch-page",
@@ -767,7 +791,7 @@ static void grits_plugin_radar_init(GritsPluginRadar *self)
                        "conus"  G_DIR_SEPARATOR_S);
        self->sites      = g_hash_table_new_full(g_str_hash, g_str_equal,
                                NULL, (GDestroyNotify)radar_site_free);
-       self->config     = gtk_notebook_new();
+       self->config     = g_object_ref(gtk_notebook_new());
 
        /* Load colormaps */
        for (int i = 0; colormaps[i].file; i++) {
@@ -790,6 +814,11 @@ static void grits_plugin_radar_dispose(GObject *gobject)
                g_signal_handler_disconnect(self->config, self->tab_id);
                grits_viewer_remove(viewer, GRITS_OBJECT(self->hud));
                radar_conus_free(self->conus);
+               g_hash_table_destroy(self->sites);
+               g_object_unref(self->config);
+               g_object_unref(self->hud);
+               g_object_unref(self->prefs);
+               g_object_unref(viewer);
        }
        /* Drop references */
        G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject);
@@ -801,7 +830,6 @@ static void grits_plugin_radar_finalize(GObject *gobject)
        /* Free data */
        grits_http_free(self->conus_http);
        grits_http_free(self->sites_http);
-       g_hash_table_destroy(self->sites);
        gtk_widget_destroy(self->config);
        G_OBJECT_CLASS(grits_plugin_radar_parent_class)->finalize(gobject);