]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
4c77d5e0 GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
50a14534 EB |
26 | #include "viking.h" |
27 | #include "vikgeoreflayer_pixmap.h" | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
4c77d5e0 | 30 | #include <glib/gi18n.h> |
50a14534 | 31 | |
bce3a7b0 EB |
32 | #include "icons/icons.h" |
33 | ||
50a14534 EB |
34 | VikLayerParam georef_layer_params[] = { |
35 | { "image", VIK_LAYER_PARAM_STRING, VIK_LAYER_NOT_IN_PROPERTIES }, | |
36 | { "corner_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES }, | |
37 | { "corner_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES }, | |
38 | { "mpp_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES }, | |
39 | { "mpp_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES }, | |
40 | }; | |
41 | ||
42 | enum { PARAM_IMAGE = 0, PARAM_CE, PARAM_CN, PARAM_ME, PARAM_MN, NUM_PARAMS }; | |
43 | ||
0a6cab71 AF |
44 | static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len ); |
45 | static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ); | |
50a14534 EB |
46 | static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp ); |
47 | static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id ); | |
48 | VikGeorefLayer *georef_layer_new ( ); | |
49 | VikGeorefLayer *georef_layer_create ( VikViewport *vp ); | |
50 | static void georef_layer_free ( VikGeorefLayer *vgl ); | |
51 | gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ); | |
52 | static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ); | |
53 | static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gpointer vlp ); | |
54 | static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image ); | |
55 | static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w ); | |
56 | static void georef_layer_load_image ( VikGeorefLayer *vgl ); | |
941aa6e9 AF |
57 | |
58 | /* tools */ | |
59 | static gpointer georef_layer_move_create ( VikWindow *vw, VikViewport *vvp); | |
50a14534 EB |
60 | static gboolean georef_layer_move_release ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ); |
61 | static gboolean georef_layer_move_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ); | |
941aa6e9 | 62 | static gpointer georef_layer_zoom_create ( VikWindow *vw, VikViewport *vvp); |
50a14534 EB |
63 | static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ); |
64 | ||
65 | static VikToolInterface georef_tools[] = { | |
4c77d5e0 | 66 | { N_("Georef Move Map"), (VikToolConstructorFunc) georef_layer_move_create, NULL, NULL, NULL, |
bce3a7b0 | 67 | (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release, |
f2f2f7bf | 68 | (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geomove }, |
941aa6e9 | 69 | |
4c77d5e0 | 70 | { N_("Georef Zoom Tool"), (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL, |
bce3a7b0 | 71 | (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL, |
f2f2f7bf | 72 | (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geozoom }, |
50a14534 EB |
73 | }; |
74 | ||
75 | VikLayerInterface vik_georef_layer_interface = { | |
76 | "GeoRef Map", | |
77 | &georeflayer_pixbuf, /*icon */ | |
78 | ||
79 | georef_tools, | |
80 | sizeof(georef_tools) / sizeof(VikToolInterface), | |
81 | ||
82 | georef_layer_params, | |
83 | NUM_PARAMS, | |
84 | NULL, | |
85 | 0, | |
86 | ||
5a4a28bf QT |
87 | VIK_MENU_ITEM_ALL, |
88 | ||
50a14534 EB |
89 | (VikLayerFuncCreate) georef_layer_create, |
90 | (VikLayerFuncRealize) NULL, | |
91 | (VikLayerFuncPostRead) georef_layer_load_image, | |
92 | (VikLayerFuncFree) georef_layer_free, | |
93 | ||
94 | (VikLayerFuncProperties) georef_layer_properties, | |
95 | (VikLayerFuncDraw) georef_layer_draw, | |
96 | (VikLayerFuncChangeCoordMode) NULL, | |
97 | ||
20c7a3a0 QT |
98 | (VikLayerFuncSetMenuItemsSelection) NULL, |
99 | (VikLayerFuncGetMenuItemsSelection) NULL, | |
100 | ||
50a14534 EB |
101 | (VikLayerFuncAddMenuItems) georef_layer_add_menu_items, |
102 | (VikLayerFuncSublayerAddMenuItems) NULL, | |
103 | ||
104 | (VikLayerFuncSublayerRenameRequest) NULL, | |
105 | (VikLayerFuncSublayerToggleVisible) NULL, | |
106 | ||
0a6cab71 AF |
107 | (VikLayerFuncMarshall) georef_layer_marshall, |
108 | (VikLayerFuncUnmarshall) georef_layer_unmarshall, | |
50a14534 EB |
109 | |
110 | (VikLayerFuncSetParam) georef_layer_set_param, | |
111 | (VikLayerFuncGetParam) georef_layer_get_param, | |
112 | ||
113 | (VikLayerFuncReadFileData) NULL, | |
114 | (VikLayerFuncWriteFileData) NULL, | |
115 | ||
33534cd8 | 116 | (VikLayerFuncDeleteItem) NULL, |
50a14534 EB |
117 | (VikLayerFuncCopyItem) NULL, |
118 | (VikLayerFuncPasteItem) NULL, | |
119 | (VikLayerFuncFreeCopiedItem) NULL, | |
70a23263 | 120 | (VikLayerFuncDragDropRequest) NULL, |
50a14534 EB |
121 | }; |
122 | ||
123 | struct _VikGeorefLayer { | |
124 | VikLayer vl; | |
125 | gchar *image; | |
126 | GdkPixbuf *pixbuf; | |
127 | struct UTM corner; | |
128 | gdouble mpp_easting, mpp_northing; | |
129 | guint width, height; | |
130 | ||
131 | gint click_x, click_y; | |
132 | }; | |
133 | ||
134 | ||
135 | ||
136 | GType vik_georef_layer_get_type () | |
137 | { | |
138 | static GType vgl_type = 0; | |
139 | ||
140 | if (!vgl_type) | |
141 | { | |
142 | static const GTypeInfo vgl_info = | |
143 | { | |
144 | sizeof (VikGeorefLayerClass), | |
145 | NULL, /* base_init */ | |
146 | NULL, /* base_finalize */ | |
147 | NULL, /* class init */ | |
148 | NULL, /* class_finalize */ | |
149 | NULL, /* class_data */ | |
150 | sizeof (VikGeorefLayer), | |
151 | 0, | |
152 | NULL /* instance init */ | |
153 | }; | |
154 | vgl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikGeorefLayer", &vgl_info, 0 ); | |
155 | } | |
156 | ||
157 | return vgl_type; | |
158 | } | |
159 | ||
0a6cab71 AF |
160 | static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len ) |
161 | { | |
162 | vik_layer_marshall_params ( VIK_LAYER(vgl), data, len ); | |
163 | } | |
164 | ||
165 | static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ) | |
166 | { | |
167 | VikGeorefLayer *rv = georef_layer_new ( vvp ); | |
168 | vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp ); | |
169 | if (rv->image) { | |
170 | georef_layer_load_image ( rv ); | |
171 | } | |
172 | return rv; | |
173 | } | |
174 | ||
50a14534 EB |
175 | static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp ) |
176 | { | |
177 | switch ( id ) | |
178 | { | |
179 | case PARAM_IMAGE: georef_layer_set_image ( vgl, data.s ); break; | |
180 | case PARAM_CN: vgl->corner.northing = data.d; break; | |
181 | case PARAM_CE: vgl->corner.easting = data.d; break; | |
182 | case PARAM_MN: vgl->mpp_northing = data.d; break; | |
183 | case PARAM_ME: vgl->mpp_easting = data.d; break; | |
184 | } | |
185 | return TRUE; | |
186 | } | |
187 | ||
188 | static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id ) | |
189 | { | |
190 | VikLayerParamData rv; | |
191 | switch ( id ) | |
192 | { | |
193 | case PARAM_IMAGE: rv.s = vgl->image ? vgl->image : ""; break; | |
194 | case PARAM_CN: rv.d = vgl->corner.northing; break; | |
195 | case PARAM_CE: rv.d = vgl->corner.easting; break; | |
196 | case PARAM_MN: rv.d = vgl->mpp_northing; break; | |
197 | case PARAM_ME: rv.d = vgl->mpp_easting; break; | |
198 | } | |
199 | return rv; | |
200 | } | |
201 | ||
202 | VikGeorefLayer *georef_layer_new ( ) | |
203 | { | |
204 | VikGeorefLayer *vgl = VIK_GEOREF_LAYER ( g_object_new ( VIK_GEOREF_LAYER_TYPE, NULL ) ); | |
205 | vik_layer_init ( VIK_LAYER(vgl), VIK_LAYER_GEOREF ); | |
206 | ||
207 | vgl->image = NULL; | |
208 | vgl->pixbuf = NULL; | |
209 | vgl->click_x = -1; | |
210 | vgl->click_y = -1; | |
211 | return vgl; | |
212 | } | |
213 | ||
214 | static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ) | |
215 | { | |
216 | /* bla, bla */ | |
217 | if ( vgl->pixbuf ) | |
218 | { | |
219 | VikViewport *vp = VIK_VIEWPORT(data); | |
220 | struct UTM utm_middle; | |
221 | gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp); | |
222 | vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle ); | |
223 | ||
224 | if ( xmpp == vgl->mpp_easting && ympp == vgl->mpp_northing ) | |
225 | { | |
226 | guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp); | |
227 | gint32 x, y; | |
228 | vgl->corner.zone = utm_middle.zone; | |
229 | vgl->corner.letter = utm_middle.letter; | |
230 | VikCoord corner_coord; | |
231 | vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) ); | |
232 | vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y ); | |
233 | if ( (x < 0 || x < width) && (y < 0 || y < height) && x+vgl->width > 0 && y+vgl->height > 0 ) | |
234 | vik_viewport_draw_pixbuf ( vp, vgl->pixbuf, 0, 0, x, y, vgl->width, vgl->height ); /* todo: draw only what we need to. */ | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
239 | static void georef_layer_free ( VikGeorefLayer *vgl ) | |
240 | { | |
241 | if ( vgl->image != NULL ) | |
242 | g_free ( vgl->image ); | |
243 | } | |
244 | ||
245 | VikGeorefLayer *georef_layer_create ( VikViewport *vp ) | |
246 | { | |
247 | return georef_layer_new (); | |
248 | } | |
249 | ||
250 | gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ) | |
251 | { | |
252 | return georef_layer_dialog ( &vgl, vp, VIK_GTK_WINDOW_FROM_WIDGET(vp) ); | |
253 | } | |
254 | ||
255 | static void georef_layer_load_image ( VikGeorefLayer *vgl ) | |
256 | { | |
257 | GError *gx = NULL; | |
258 | if ( vgl->image == NULL ) | |
259 | return; | |
260 | ||
261 | if ( vgl->pixbuf ) | |
262 | g_object_unref ( G_OBJECT(vgl->pixbuf) ); | |
263 | ||
264 | vgl->pixbuf = gdk_pixbuf_new_from_file ( vgl->image, &gx ); | |
265 | ||
266 | if (gx) | |
267 | { | |
4c77d5e0 | 268 | g_warning ( _("Couldn't open image file: %s"), gx->message ); |
50a14534 EB |
269 | g_error_free ( gx ); |
270 | } | |
271 | else | |
272 | { | |
273 | vgl->width = gdk_pixbuf_get_width ( vgl->pixbuf ); | |
274 | vgl->height = gdk_pixbuf_get_height ( vgl->pixbuf ); | |
275 | } | |
276 | ||
277 | /* should find length and width here too */ | |
278 | } | |
279 | ||
280 | static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image ) | |
281 | { | |
282 | if ( vgl->image ) | |
283 | g_free ( vgl->image ); | |
284 | if ( image == NULL ) | |
285 | vgl->image = NULL; | |
286 | vgl->image = g_strdup ( image ); | |
287 | } | |
288 | ||
289 | static gboolean world_file_read_line ( gchar *buffer, gint size, FILE *f, GtkWidget *widget, gboolean use_value ) | |
290 | { | |
291 | if (!fgets ( buffer, 1024, f )) | |
292 | { | |
4c77d5e0 | 293 | a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(widget), _("Unexpected end of file reading World file.") ); |
50a14534 EB |
294 | g_free ( buffer ); |
295 | fclose ( f ); | |
296 | return FALSE; | |
297 | } | |
298 | if ( use_value ) | |
299 | { | |
300 | gdouble val = strtod ( buffer, NULL ); | |
301 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(widget), val > 0 ? val : -val ); | |
302 | } | |
303 | return TRUE; | |
304 | } | |
305 | ||
306 | static void georef_layer_dialog_load ( GtkWidget *pass_along[4] ) | |
307 | { | |
6e4a49aa MA |
308 | GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"), |
309 | NULL, | |
310 | GTK_FILE_CHOOSER_ACTION_OPEN, | |
311 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
312 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, | |
313 | NULL); | |
314 | ||
315 | if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT ) | |
50a14534 | 316 | { |
6e4a49aa | 317 | FILE *f = fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "r" ); |
50a14534 EB |
318 | gtk_widget_destroy ( file_selector ); |
319 | if ( !f ) | |
320 | { | |
4c77d5e0 | 321 | a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The World file you requested could not be opened for reading.") ); |
50a14534 EB |
322 | return; |
323 | } | |
324 | else | |
325 | { | |
326 | gchar *buffer = g_malloc ( 1024 * sizeof(gchar) ); | |
327 | if ( world_file_read_line ( buffer, 1024, f, pass_along[0], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[1], TRUE ) | |
328 | && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) | |
329 | && world_file_read_line ( buffer, 1024, f, pass_along[2], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[3], TRUE ) ) | |
330 | { | |
331 | g_free ( buffer ); | |
332 | fclose ( f ); | |
333 | } | |
334 | } | |
335 | } | |
336 | else | |
337 | gtk_widget_destroy ( file_selector ); | |
338 | /* do your jazz | |
339 | We need a | |
340 | file selection dialog | |
341 | file opener for reading, if NULL, send error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]) ) | |
342 | does that catch directories too? | |
343 | read lines -- if not enough lines, give error. | |
344 | if anything outside, give error. define range with #define CONSTANTS | |
345 | put 'em in thar widgets, and that's it. | |
346 | */ | |
347 | } | |
348 | ||
349 | static void georef_layer_export_params ( gpointer *pass_along[2] ) | |
350 | { | |
351 | VikGeorefLayer *vgl = VIK_GEOREF_LAYER(pass_along[0]); | |
6e4a49aa MA |
352 | GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"), |
353 | NULL, | |
354 | GTK_FILE_CHOOSER_ACTION_SAVE, | |
355 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
356 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, | |
357 | NULL); | |
358 | if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT ) | |
50a14534 | 359 | { |
6e4a49aa MA |
360 | FILE *f = fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "w" ); |
361 | ||
50a14534 EB |
362 | gtk_widget_destroy ( file_selector ); |
363 | if ( !f ) | |
364 | { | |
4c77d5e0 | 365 | a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The file you requested could not be opened for writing.") ); |
50a14534 EB |
366 | return; |
367 | } | |
368 | else | |
369 | { | |
370 | 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 ); | |
371 | fclose ( f ); | |
372 | } | |
373 | } | |
374 | else | |
375 | gtk_widget_destroy ( file_selector ); | |
376 | } | |
377 | ||
378 | /* returns TRUE if OK was pressed. */ | |
379 | static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w ) | |
380 | { | |
4c77d5e0 | 381 | GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Layer Properties"), |
50a14534 EB |
382 | w, |
383 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | |
384 | GTK_STOCK_CANCEL, | |
385 | GTK_RESPONSE_REJECT, | |
386 | GTK_STOCK_OK, | |
387 | GTK_RESPONSE_ACCEPT, | |
10888930 | 388 | NULL ); |
50a14534 EB |
389 | GtkWidget *table, *wfp_hbox, *wfp_label, *wfp_button, *ce_label, *ce_spin, *cn_label, *cn_spin, *xlabel, *xspin, *ylabel, *yspin, *imagelabel, *imageentry; |
390 | ||
391 | GtkWidget *pass_along[4]; | |
392 | ||
393 | table = gtk_table_new ( 6, 2, FALSE ); | |
394 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0 ); | |
395 | ||
396 | wfp_hbox = gtk_hbox_new ( FALSE, 0 ); | |
4c77d5e0 GB |
397 | wfp_label = gtk_label_new ( _("World File Parameters:") ); |
398 | wfp_button = gtk_button_new_with_label ( _("Load From File...") ); | |
50a14534 EB |
399 | |
400 | gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_label, TRUE, TRUE, 0 ); | |
401 | gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_button, FALSE, FALSE, 3 ); | |
402 | ||
4c77d5e0 | 403 | ce_label = gtk_label_new ( _("Corner pixel easting:") ); |
50a14534 EB |
404 | ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 5 ), 1, 4 ); |
405 | ||
4c77d5e0 | 406 | cn_label = gtk_label_new ( _("Corner pixel northing:") ); |
50a14534 EB |
407 | cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 5 ), 1, 4 ); |
408 | ||
4c77d5e0 GB |
409 | xlabel = gtk_label_new ( _("X (easting) scale (mpp): ")); |
410 | ylabel = gtk_label_new ( _("Y (northing) scale (mpp): ")); | |
50a14534 EB |
411 | |
412 | xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 ); | |
413 | yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 ); | |
414 | ||
4c77d5e0 | 415 | imagelabel = gtk_label_new ( _("Map Image:") ); |
50a14534 EB |
416 | imageentry = vik_file_entry_new (); |
417 | ||
418 | if (*vgl) | |
419 | { | |
420 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(ce_spin), (*vgl)->corner.easting ); | |
421 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(cn_spin), (*vgl)->corner.northing ); | |
422 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(xspin), (*vgl)->mpp_easting ); | |
423 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(yspin), (*vgl)->mpp_northing ); | |
424 | if ( (*vgl)->image ) | |
425 | vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), (*vgl)->image ); | |
426 | } | |
427 | else | |
428 | { | |
429 | VikCoord corner_coord; | |
430 | struct UTM utm; | |
431 | vik_viewport_screen_to_coord ( VIK_VIEWPORT(vp), 0, 0, &corner_coord ); | |
432 | vik_coord_to_utm ( &corner_coord, &utm ); | |
433 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(ce_spin), utm.easting ); | |
434 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(cn_spin), utm.northing ); | |
435 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(xspin), vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ) ); | |
436 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(yspin), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ) ); | |
437 | } | |
438 | ||
439 | gtk_table_attach_defaults ( GTK_TABLE(table), imagelabel, 0, 1, 0, 1 ); | |
440 | gtk_table_attach_defaults ( GTK_TABLE(table), imageentry, 1, 2, 0, 1 ); | |
441 | gtk_table_attach_defaults ( GTK_TABLE(table), wfp_hbox, 0, 2, 1, 2 ); | |
442 | gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 2, 3 ); | |
443 | gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 2, 3 ); | |
444 | gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 3, 4 ); | |
445 | gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 3, 4 ); | |
446 | gtk_table_attach_defaults ( GTK_TABLE(table), ce_label, 0, 1, 4, 5 ); | |
447 | gtk_table_attach_defaults ( GTK_TABLE(table), ce_spin, 1, 2, 4, 5 ); | |
448 | gtk_table_attach_defaults ( GTK_TABLE(table), cn_label, 0, 1, 5, 6 ); | |
449 | gtk_table_attach_defaults ( GTK_TABLE(table), cn_spin, 1, 2, 5, 6 ); | |
450 | ||
451 | pass_along[0] = xspin; | |
452 | pass_along[1] = yspin; | |
453 | pass_along[2] = ce_spin; | |
454 | pass_along[3] = cn_spin; | |
455 | g_signal_connect_swapped ( G_OBJECT(wfp_button), "clicked", G_CALLBACK(georef_layer_dialog_load), pass_along ); | |
456 | ||
457 | gtk_widget_show_all ( table ); | |
458 | ||
459 | if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) | |
460 | { | |
461 | if (! *vgl) | |
462 | { | |
463 | *vgl = georef_layer_new (); | |
464 | vik_layer_rename ( VIK_LAYER(*vgl), vik_georef_layer_interface.name ); | |
465 | } | |
466 | (*vgl)->corner.easting = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(ce_spin) ); | |
467 | (*vgl)->corner.northing = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(cn_spin) ); | |
468 | (*vgl)->mpp_easting = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) ); | |
469 | (*vgl)->mpp_northing = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) ); | |
470 | if ( (!(*vgl)->image) || strcmp( (*vgl)->image, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) ) != 0 ) | |
471 | { | |
472 | georef_layer_set_image ( *vgl, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) ); | |
473 | georef_layer_load_image ( *vgl ); | |
474 | } | |
475 | ||
476 | gtk_widget_destroy ( GTK_WIDGET(dialog) ); | |
477 | return TRUE; | |
478 | } | |
479 | gtk_widget_destroy ( GTK_WIDGET(dialog) ); | |
480 | return FALSE; | |
481 | } | |
482 | ||
483 | static void georef_layer_zoom_to_fit ( gpointer vgl_vlp[2] ) | |
484 | { | |
485 | vik_viewport_set_xmpp ( vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1])), VIK_GEOREF_LAYER(vgl_vlp[0])->mpp_easting ); | |
486 | vik_viewport_set_ympp ( vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1])), VIK_GEOREF_LAYER(vgl_vlp[0])->mpp_northing ); | |
487 | vik_layers_panel_emit_update ( VIK_LAYERS_PANEL(vgl_vlp[1]) ); | |
488 | } | |
489 | ||
490 | static void georef_layer_goto_center ( gpointer vgl_vlp[2] ) | |
491 | { | |
492 | VikGeorefLayer *vgl = VIK_GEOREF_LAYER ( vgl_vlp[0] ); | |
493 | VikViewport *vp = vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1])); | |
494 | struct UTM utm; | |
495 | VikCoord coord; | |
496 | ||
497 | vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm ); | |
498 | ||
499 | utm.easting = vgl->corner.easting + (vgl->width * vgl->mpp_easting / 2); /* only an approximation */ | |
500 | utm.northing = vgl->corner.northing - (vgl->height * vgl->mpp_northing / 2); | |
501 | ||
502 | vik_coord_load_from_utm ( &coord, vik_viewport_get_coord_mode ( vp ), &utm ); | |
503 | vik_viewport_set_center_coord ( vp, &coord ); | |
504 | ||
505 | vik_layers_panel_emit_update ( VIK_LAYERS_PANEL(vgl_vlp[1]) ); | |
506 | } | |
507 | ||
508 | static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gpointer vlp ) | |
509 | { | |
510 | static gpointer pass_along[2]; | |
511 | GtkWidget *item; | |
512 | pass_along[0] = vgl; | |
513 | pass_along[1] = vlp; | |
514 | ||
515 | item = gtk_menu_item_new(); | |
516 | gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item ); | |
517 | gtk_widget_show ( item ); | |
518 | ||
4c77d5e0 | 519 | item = gtk_menu_item_new_with_label ( _("Zoom to Fit Map") ); |
50a14534 EB |
520 | g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_zoom_to_fit), pass_along ); |
521 | gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); | |
522 | gtk_widget_show ( item ); | |
523 | ||
4c77d5e0 | 524 | item = gtk_menu_item_new_with_label ( _("Goto Map Center") ); |
50a14534 EB |
525 | g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_goto_center), pass_along ); |
526 | gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); | |
527 | gtk_widget_show ( item ); | |
528 | ||
4c77d5e0 | 529 | item = gtk_menu_item_new_with_label ( _("Export to World File") ); |
50a14534 EB |
530 | g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_export_params), pass_along ); |
531 | gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); | |
532 | gtk_widget_show ( item ); | |
533 | } | |
534 | ||
941aa6e9 AF |
535 | |
536 | static gpointer georef_layer_move_create ( VikWindow *vw, VikViewport *vvp) | |
537 | { | |
538 | return vvp; | |
539 | } | |
540 | ||
50a14534 EB |
541 | static gboolean georef_layer_move_release ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ) |
542 | { | |
941aa6e9 AF |
543 | if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF) |
544 | return FALSE; | |
545 | ||
50a14534 EB |
546 | if ( vgl->click_x != -1 ) |
547 | { | |
548 | vgl->corner.easting += (event->x - vgl->click_x) * vik_viewport_get_xmpp (vvp); | |
549 | vgl->corner.northing -= (event->y - vgl->click_y) * vik_viewport_get_ympp (vvp); | |
550 | vik_layer_emit_update ( VIK_LAYER(vgl) ); | |
551 | return TRUE; | |
552 | } | |
553 | return FALSE; /* I didn't move anything on this layer! */ | |
554 | } | |
555 | ||
941aa6e9 AF |
556 | static gpointer georef_layer_zoom_create ( VikWindow *vw, VikViewport *vvp) |
557 | { | |
558 | return vvp; | |
559 | } | |
560 | ||
50a14534 EB |
561 | static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ) |
562 | { | |
941aa6e9 AF |
563 | if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF) |
564 | return FALSE; | |
50a14534 EB |
565 | if ( event->button == 1 ) |
566 | { | |
567 | if ( vgl->mpp_easting < (VIK_VIEWPORT_MAX_ZOOM / 1.05) && vgl->mpp_northing < (VIK_VIEWPORT_MAX_ZOOM / 1.05) ) | |
568 | { | |
569 | vgl->mpp_easting *= 1.01; | |
570 | vgl->mpp_northing *= 1.01; | |
571 | } | |
572 | } | |
573 | else | |
574 | { | |
575 | if ( vgl->mpp_easting > (VIK_VIEWPORT_MIN_ZOOM * 1.05) && vgl->mpp_northing > (VIK_VIEWPORT_MIN_ZOOM * 1.05) ) | |
576 | { | |
577 | vgl->mpp_easting /= 1.01; | |
578 | vgl->mpp_northing /= 1.01; | |
579 | } | |
580 | } | |
581 | vik_viewport_set_xmpp ( vvp, vgl->mpp_easting ); | |
582 | vik_viewport_set_ympp ( vvp, vgl->mpp_northing ); | |
583 | vik_layer_emit_update ( VIK_LAYER(vgl) ); | |
584 | return TRUE; | |
585 | } | |
586 | ||
587 | static gboolean georef_layer_move_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp ) | |
588 | { | |
941aa6e9 AF |
589 | if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF) |
590 | return FALSE; | |
50a14534 EB |
591 | vgl->click_x = event->x; |
592 | vgl->click_y = event->y; | |
593 | return TRUE; | |
594 | } |