X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/108889308bdc1f0a1e3329f68ce89fbe14112cbd..ba9d0a00bb9c614423562381e6edca2a33d28b54:/src/vikgeoreflayer.c?ds=inline diff --git a/src/vikgeoreflayer.c b/src/vikgeoreflayer.c index 815ae716..da8a1c61 100644 --- a/src/vikgeoreflayer.c +++ b/src/vikgeoreflayer.c @@ -19,10 +19,19 @@ * */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "viking.h" -#include "vikgeoreflayer_pixmap.h" -#include +#include +#include +#include #include +#include +#include + +#include "icons/icons.h" VikLayerParam georef_layer_params[] = { { "image", VIK_LAYER_PARAM_STRING, VIK_LAYER_NOT_IN_PROPERTIES }, @@ -34,11 +43,11 @@ VikLayerParam georef_layer_params[] = { enum { PARAM_IMAGE = 0, PARAM_CE, PARAM_CN, PARAM_ME, PARAM_MN, NUM_PARAMS }; +static const gchar* georef_layer_tooltip ( VikGeorefLayer *vgl ); static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len ); static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ); -static VikGeorefLayer *georef_layer_copy ( VikGeorefLayer *vgl, gpointer vp ); -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 ); @@ -57,16 +66,18 @@ static gpointer georef_layer_zoom_create ( VikWindow *vw, VikViewport *vvp); static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ); static VikToolInterface georef_tools[] = { - { "Georef Move Map", (VikToolConstructorFunc) georef_layer_move_create, NULL, NULL, NULL, - (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release }, + { N_("Georef Move Map"), (VikToolConstructorFunc) georef_layer_move_create, NULL, NULL, NULL, + (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release, + (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geomove_pixbuf }, - { "Georef Zoom Tool", (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL, - (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL }, + { N_("Georef Zoom Tool"), (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL, + (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL, + (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geozoom_pixbuf }, }; VikLayerInterface vik_georef_layer_interface = { "GeoRef Map", - &georeflayer_pixbuf, /*icon */ + &vikgeoreflayer_pixbuf, /*icon */ georef_tools, sizeof(georef_tools) / sizeof(VikToolInterface), @@ -95,8 +106,10 @@ VikLayerInterface vik_georef_layer_interface = { (VikLayerFuncSublayerRenameRequest) NULL, (VikLayerFuncSublayerToggleVisible) NULL, + (VikLayerFuncSublayerTooltip) NULL, + (VikLayerFuncLayerTooltip) georef_layer_tooltip, + (VikLayerFuncLayerSelected) NULL, - (VikLayerFuncCopy) georef_layer_copy, (VikLayerFuncMarshall) georef_layer_marshall, (VikLayerFuncUnmarshall) georef_layer_unmarshall, @@ -107,10 +120,16 @@ VikLayerInterface vik_georef_layer_interface = { (VikLayerFuncWriteFileData) NULL, (VikLayerFuncDeleteItem) NULL, + (VikLayerFuncCutItem) NULL, (VikLayerFuncCopyItem) NULL, (VikLayerFuncPasteItem) NULL, (VikLayerFuncFreeCopiedItem) NULL, (VikLayerFuncDragDropRequest) NULL, + + (VikLayerFuncSelectClick) NULL, + (VikLayerFuncSelectMove) NULL, + (VikLayerFuncSelectRelease) NULL, + (VikLayerFuncSelectedViewportMenu) NULL, }; struct _VikGeorefLayer { @@ -121,6 +140,9 @@ struct _VikGeorefLayer { gdouble mpp_easting, mpp_northing; guint width, height; + GdkPixbuf *scaled; + guint32 scaled_width, scaled_height; + gint click_x, click_y; }; @@ -150,21 +172,9 @@ GType vik_georef_layer_get_type () return vgl_type; } -static VikGeorefLayer *georef_layer_copy ( VikGeorefLayer *vgl, gpointer vp ) +static const gchar* georef_layer_tooltip ( VikGeorefLayer *vgl ) { - VikGeorefLayer *rv = georef_layer_new (); - rv->corner = vgl->corner; - rv->mpp_easting = vgl->mpp_easting; - rv->mpp_northing = vgl->mpp_northing; - rv->width = vgl->width; - rv->height = vgl->height; - - if ( vgl->image ) - { - rv->image = g_strdup ( vgl->image ); - georef_layer_load_image ( rv ); - } - return rv; + return vgl->image; } static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len ) @@ -174,7 +184,7 @@ static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ) { - VikGeorefLayer *rv = georef_layer_new ( vvp ); + VikGeorefLayer *rv = georef_layer_new (); vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp ); if (rv->image) { georef_layer_load_image ( rv ); @@ -182,7 +192,7 @@ static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewp 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 ) { @@ -195,7 +205,7 @@ static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLay 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 ) @@ -218,6 +228,9 @@ VikGeorefLayer *georef_layer_new ( ) vgl->pixbuf = NULL; vgl->click_x = -1; vgl->click_y = -1; + vgl->scaled = NULL; + vgl->scaled_width = 0; + vgl->scaled_height = 0; return vgl; } @@ -229,20 +242,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); + 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 ); - 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. */ } } @@ -250,6 +291,8 @@ static void georef_layer_free ( VikGeorefLayer *vgl ) { if ( vgl->image != NULL ) g_free ( vgl->image ); + if ( vgl->scaled != NULL ) + g_object_unref ( vgl->scaled ); } VikGeorefLayer *georef_layer_create ( VikViewport *vp ) @@ -270,12 +313,17 @@ static void georef_layer_load_image ( VikGeorefLayer *vgl ) 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 ); if (gx) { - g_warning ( "Couldn't open image file: %s", gx->message ); + g_warning ( _("Couldn't open image file: %s"), gx->message ); g_error_free ( gx ); } else @@ -291,6 +339,11 @@ static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *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 ); @@ -300,14 +353,15 @@ static gboolean world_file_read_line ( gchar *buffer, gint size, FILE *f, GtkWid { if (!fgets ( buffer, 1024, f )) { - a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(widget), "Unexpected end of file reading World file." ); + a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(widget), _("Unexpected end of file reading World file.") ); g_free ( buffer ); fclose ( f ); + f = NULL; return FALSE; } if ( use_value ) { - gdouble val = strtod ( buffer, NULL ); + gdouble val = g_strtod ( buffer, NULL ); gtk_spin_button_set_value ( GTK_SPIN_BUTTON(widget), val > 0 ? val : -val ); } return TRUE; @@ -315,26 +369,32 @@ static gboolean world_file_read_line ( gchar *buffer, gint size, FILE *f, GtkWid static void georef_layer_dialog_load ( GtkWidget *pass_along[4] ) { - GtkWidget *file_selector = gtk_file_selection_new ("Choose World file"); - - if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_OK ) + GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"), + NULL, + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + + if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT ) { - FILE *f = fopen ( gtk_file_selection_get_filename ( GTK_FILE_SELECTION(file_selector) ), "r" ); + FILE *f = g_fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "r" ); gtk_widget_destroy ( file_selector ); if ( !f ) { - a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), "The World file you requested could not be opened for reading." ); + a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The World file you requested could not be opened for reading.") ); return; } else { gchar *buffer = g_malloc ( 1024 * sizeof(gchar) ); - if ( world_file_read_line ( buffer, 1024, f, pass_along[0], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[1], TRUE ) - && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) + if ( world_file_read_line ( buffer, 1024, f, pass_along[0], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE) + && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) && world_file_read_line ( buffer, 1024, f, pass_along[1], TRUE) && world_file_read_line ( buffer, 1024, f, pass_along[2], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[3], TRUE ) ) { g_free ( buffer ); fclose ( f ); + f = NULL; } } } @@ -354,21 +414,27 @@ We need a static void georef_layer_export_params ( gpointer *pass_along[2] ) { VikGeorefLayer *vgl = VIK_GEOREF_LAYER(pass_along[0]); - GtkWidget *file_selector = gtk_file_selection_new ("Choose World file"); - - if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_OK ) + GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"), + NULL, + GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT ) { - FILE *f = fopen ( gtk_file_selection_get_filename ( GTK_FILE_SELECTION(file_selector) ), "w" ); + FILE *f = g_fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "w" ); + gtk_widget_destroy ( file_selector ); if ( !f ) { - a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), "The file you requested could not be opened for writing." ); + a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The file you requested could not be opened for writing.") ); return; } else { fprintf ( f, "%f\n%f\n%f\n%f\n%f\n%f\n", vgl->mpp_easting, vgl->mpp_northing, 0.0, 0.0, vgl->corner.easting, vgl->corner.northing ); fclose ( f ); + f = NULL; } } else @@ -378,7 +444,7 @@ static void georef_layer_export_params ( gpointer *pass_along[2] ) /* returns TRUE if OK was pressed. */ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w ) { - GtkWidget *dialog = gtk_dialog_new_with_buttons ("Layer Properties", + GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Layer Properties"), w, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, @@ -386,6 +452,12 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL ); + /* Default to reject as user really needs to specify map file first */ + gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT ); + GtkWidget *response_w = NULL; +#if GTK_CHECK_VERSION (2, 20, 0) + response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT ); +#endif GtkWidget *table, *wfp_hbox, *wfp_label, *wfp_button, *ce_label, *ce_spin, *cn_label, *cn_spin, *xlabel, *xspin, *ylabel, *yspin, *imagelabel, *imageentry; GtkWidget *pass_along[4]; @@ -394,26 +466,30 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0 ); wfp_hbox = gtk_hbox_new ( FALSE, 0 ); - wfp_label = gtk_label_new ( "World File Parameters:" ); - wfp_button = gtk_button_new_with_label ( "Load From File..." ); + wfp_label = gtk_label_new ( _("World File Parameters:") ); + wfp_button = gtk_button_new_with_label ( _("Load From File...") ); gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_label, TRUE, TRUE, 0 ); 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_label = gtk_label_new ( _("Corner pixel easting:") ); + 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_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 5 ), 1, 4 ); + 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, 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 (); + imagelabel = gtk_label_new ( _("Map Image:") ); + imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN); if (*vgl) { @@ -454,6 +530,9 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind pass_along[3] = cn_spin; g_signal_connect_swapped ( G_OBJECT(wfp_button), "clicked", G_CALLBACK(georef_layer_dialog_load), pass_along ); + if ( response_w ) + gtk_widget_grab_focus ( response_w ); + gtk_widget_show_all ( table ); if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) @@ -516,17 +595,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 ); - 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 ); - 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 ); - 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 );