]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikgeoreflayer.c
Various improvements and tidy ups.
[andy/viking.git] / src / vikgeoreflayer.c
index 9134e9f96aa659ab92f6ec01ecdd90b529d132d9..7220168085f9be5f58193e2244496a2bb8fdcbd2 100644 (file)
@@ -28,6 +28,8 @@
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <string.h>
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <string.h>
+#include <math.h>
+#include <stdlib.h>
 
 #include "icons/icons.h"
 
 
 #include "icons/icons.h"
 
@@ -43,8 +45,8 @@ enum { PARAM_IMAGE = 0, PARAM_CE, PARAM_CN, PARAM_ME, PARAM_MN, NUM_PARAMS };
 
 static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len );
 static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
 
 static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len );
 static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
-static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp );
-static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id );
+static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp, gboolean is_file_operation );
+static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id, gboolean is_file_operation );
 VikGeorefLayer *georef_layer_new ( );
 VikGeorefLayer *georef_layer_create ( VikViewport *vp );
 static void georef_layer_free ( VikGeorefLayer *vgl );
 VikGeorefLayer *georef_layer_new ( );
 VikGeorefLayer *georef_layer_create ( VikViewport *vp );
 static void georef_layer_free ( VikGeorefLayer *vgl );
@@ -128,6 +130,9 @@ struct _VikGeorefLayer {
   gdouble mpp_easting, mpp_northing;
   guint width, height;
 
   gdouble mpp_easting, mpp_northing;
   guint width, height;
 
+  GdkPixbuf *scaled;
+  guint32 scaled_width, scaled_height;
+
   gint click_x, click_y;
 };
 
   gint click_x, click_y;
 };
 
@@ -172,7 +177,7 @@ static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewp
   return rv;
 }
 
   return rv;
 }
 
-static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp )
+static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp, gboolean is_file_operation )
 {
   switch ( id )
   {
 {
   switch ( id )
   {
@@ -185,7 +190,7 @@ static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLay
   return TRUE;
 }
 
   return TRUE;
 }
 
-static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id )
+static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id, gboolean is_file_operation )
 {
   VikLayerParamData rv;
   switch ( id )
 {
   VikLayerParamData rv;
   switch ( id )
@@ -208,6 +213,9 @@ VikGeorefLayer *georef_layer_new ( )
   vgl->pixbuf = NULL;
   vgl->click_x = -1;
   vgl->click_y = -1;
   vgl->pixbuf = NULL;
   vgl->click_x = -1;
   vgl->click_y = -1;
+  vgl->scaled = NULL;
+  vgl->scaled_width = 0;
+  vgl->scaled_height = 0;
   return vgl;
 }
 
   return vgl;
 }
 
@@ -219,20 +227,48 @@ static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data )
     VikViewport *vp = VIK_VIEWPORT(data);
     struct UTM utm_middle;
     gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp);
     VikViewport *vp = VIK_VIEWPORT(data);
     struct UTM utm_middle;
     gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp);
+    GdkPixbuf *pixbuf = vgl->pixbuf;
+    guint layer_width = vgl->width;
+    guint layer_height = vgl->height;
+
     vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle );
 
     vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle );
 
-    if ( xmpp == vgl->mpp_easting && ympp == vgl->mpp_northing )
+    /* scale the pixbuf if it doesn't match our dimensions */
+    if ( xmpp != vgl->mpp_easting || ympp != vgl->mpp_northing )
     {
     {
-      guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp);
-      gint32 x, y;
-      vgl->corner.zone = utm_middle.zone;
-      vgl->corner.letter = utm_middle.letter;
-      VikCoord corner_coord;
-      vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) );
-      vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y );
-      if ( (x < 0 || x < width) && (y < 0 || y < height) && x+vgl->width > 0 && y+vgl->height > 0 )
-        vik_viewport_draw_pixbuf ( vp, vgl->pixbuf, 0, 0, x, y, vgl->width, vgl->height ); /* todo: draw only what we need to. */
+      layer_width = round(vgl->width * vgl->mpp_easting / xmpp);
+      layer_height = round(vgl->height * vgl->mpp_northing / ympp);
+
+      /* rescale if necessary */
+      if (layer_width == vgl->scaled_width && layer_height == vgl->scaled_height && vgl->scaled != NULL)
+        pixbuf = vgl->scaled;
+      else
+      {
+        pixbuf = gdk_pixbuf_scale_simple(
+          vgl->pixbuf, 
+          layer_width,
+          layer_height,
+          GDK_INTERP_BILINEAR
+        );
+
+        if (vgl->scaled != NULL)
+          g_object_unref(vgl->scaled);
+
+        vgl->scaled = pixbuf;
+        vgl->scaled_width = layer_width;
+        vgl->scaled_height = layer_height;
+      }
     }
     }
+
+    guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp);
+    gint32 x, y;
+    vgl->corner.zone = utm_middle.zone;
+    vgl->corner.letter = utm_middle.letter;
+    VikCoord corner_coord;
+    vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) );
+    vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y );
+    if ( (x < 0 || x < width) && (y < 0 || y < height) && x+layer_width > 0 && y+layer_height > 0 )
+      vik_viewport_draw_pixbuf ( vp, pixbuf, 0, 0, x, y, layer_width, layer_height ); /* todo: draw only what we need to. */
   }
 }
 
   }
 }
 
@@ -240,6 +276,8 @@ static void georef_layer_free ( VikGeorefLayer *vgl )
 {
   if ( vgl->image != NULL )
     g_free ( vgl->image );
 {
   if ( vgl->image != NULL )
     g_free ( vgl->image );
+  if ( vgl->scaled != NULL )
+    g_object_unref ( vgl->scaled );
 }
 
 VikGeorefLayer *georef_layer_create ( VikViewport *vp )
 }
 
 VikGeorefLayer *georef_layer_create ( VikViewport *vp )
@@ -260,6 +298,11 @@ static void georef_layer_load_image ( VikGeorefLayer *vgl )
 
   if ( vgl->pixbuf )
     g_object_unref ( G_OBJECT(vgl->pixbuf) );
 
   if ( vgl->pixbuf )
     g_object_unref ( G_OBJECT(vgl->pixbuf) );
+  if ( vgl->scaled )
+  {
+    g_object_unref ( G_OBJECT(vgl->scaled) );
+    vgl->scaled = NULL;
+  }
 
   vgl->pixbuf = gdk_pixbuf_new_from_file ( vgl->image, &gx );
 
 
   vgl->pixbuf = gdk_pixbuf_new_from_file ( vgl->image, &gx );
 
@@ -281,6 +324,11 @@ static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image )
 {
   if ( vgl->image )
     g_free ( vgl->image );
 {
   if ( vgl->image )
     g_free ( vgl->image );
+  if ( vgl->scaled )
+  {
+    g_object_unref ( vgl->scaled );
+    vgl->scaled = NULL;
+  }
   if ( image == NULL )
     vgl->image = NULL;
   vgl->image = g_strdup ( image );
   if ( image == NULL )
     vgl->image = NULL;
   vgl->image = g_strdup ( image );
@@ -404,16 +452,20 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind
   gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_button, FALSE, FALSE, 3 );
 
   ce_label = gtk_label_new ( _("Corner pixel easting:") );
   gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_button, FALSE, FALSE, 3 );
 
   ce_label = gtk_label_new ( _("Corner pixel easting:") );
-  ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 5 ), 1, 4 );
+  ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 0 ), 1, 4 );
+  gtk_widget_set_tooltip_text ( GTK_WIDGET(ce_spin), _("the UTM \"easting\" value of the upper-right corner pixel of the map") );
 
   cn_label = gtk_label_new ( _("Corner pixel northing:") );
 
   cn_label = gtk_label_new ( _("Corner pixel northing:") );
-  cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 5 ), 1, 4 );
+  cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 0 ), 1, 4 );
+  gtk_widget_set_tooltip_text ( GTK_WIDGET(cn_spin), _("the UTM \"northing\" value of the upper-right corner pixel of the map") );
 
   xlabel = gtk_label_new ( _("X (easting) scale (mpp): "));
   ylabel = gtk_label_new ( _("Y (northing) scale (mpp): "));
 
 
   xlabel = gtk_label_new ( _("X (easting) scale (mpp): "));
   ylabel = gtk_label_new ( _("Y (northing) scale (mpp): "));
 
-  xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 );
-  yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 );
+  xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 );
+  gtk_widget_set_tooltip_text ( GTK_WIDGET(xspin), _("the scale of the map in the X direction (meters per pixel)") );
+  yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 0 ), 1, 8 );
+  gtk_widget_set_tooltip_text ( GTK_WIDGET(yspin), _("the scale of the map in the Y direction (meters per pixel)") );
 
   imagelabel = gtk_label_new ( _("Map Image:") );
   imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
 
   imagelabel = gtk_label_new ( _("Map Image:") );
   imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
@@ -519,17 +571,17 @@ static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gp
   gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
   gtk_widget_show ( item );
 
   gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
   gtk_widget_show ( item );
 
-  item = gtk_menu_item_new_with_label ( _("Zoom to Fit Map") );
+  item = gtk_menu_item_new_with_mnemonic ( _("_Zoom to Fit Map") );
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_zoom_to_fit), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );
 
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_zoom_to_fit), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );
 
-  item = gtk_menu_item_new_with_label ( _("Goto Map Center") );
+  item = gtk_menu_item_new_with_mnemonic ( _("_Goto Map Center") );
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_goto_center), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );
 
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_goto_center), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );
 
-  item = gtk_menu_item_new_with_label ( _("Export to World File") );
+  item = gtk_menu_item_new_with_mnemonic ( _("_Export to World File") );
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_export_params), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );
   g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_export_params), pass_along );
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
   gtk_widget_show ( item );