Contents of /trunk/src/info.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Tue Dec 9 20:06:06 2008 UTC (15 years, 5 months ago) by harbaum
Original Path: src/info.c
File MIME type: text/plain
File size: 18575 byte(s)
Initial import
1 harbaum 1 /*
2     * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3     *
4     * This file is part of OSM2Go.
5     *
6     * OSM2Go 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 3 of the License, or
9     * (at your option) any later version.
10     *
11     * OSM2Go 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 OSM2Go. If not, see <http://www.gnu.org/licenses/>.
18     */
19    
20     #include "appdata.h"
21    
22     enum {
23     TAG_COL_KEY = 0,
24     TAG_COL_VALUE,
25     TAG_COL_COLLISION,
26     TAG_COL_DATA,
27     TAG_NUM_COLS
28     };
29    
30     gboolean info_tag_key_collision(tag_t *tags, tag_t *tag) {
31     while(tags) {
32     if((tags != tag) && (strcasecmp(tags->key, tag->key) == 0))
33     return TRUE;
34    
35     tags = tags->next;
36     }
37     return FALSE;
38     }
39    
40     static gboolean
41     view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model,
42     GtkTreePath *path, gboolean path_currently_selected,
43     gpointer userdata) {
44     tag_context_t *context = (tag_context_t*)userdata;
45     GtkTreeIter iter;
46    
47     if(gtk_tree_model_get_iter(model, &iter, path)) {
48     g_assert(gtk_tree_path_get_depth(path) == 1);
49    
50     tag_t *tag;
51     gtk_tree_model_get(model, &iter, TAG_COL_DATA, &tag, -1);
52    
53     if(context->but_remove && context->but_edit) {
54    
55     /* you just cannot delete or edit the "created_by" tag */
56     if(context->but_remove && context->but_edit) {
57     if(strcasecmp(tag->key, "created_by") == 0) {
58     gtk_widget_set_sensitive(context->but_remove, FALSE);
59     gtk_widget_set_sensitive(context->but_edit, FALSE);
60     } else {
61     gtk_widget_set_sensitive(context->but_remove, TRUE);
62     gtk_widget_set_sensitive(context->but_edit, TRUE);
63     }
64     }
65     }
66     }
67    
68     return TRUE; /* allow selection state to change */
69     }
70    
71     static void update_collisions(GtkListStore *store, tag_t *tags) {
72     GtkTreeIter iter;
73     tag_t *tag = NULL;
74    
75     /* walk the entire store to get all values */
76     if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
77     gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, TAG_COL_DATA, &tag, -1);
78     g_assert(tag);
79     gtk_list_store_set(store, &iter,
80     TAG_COL_COLLISION, info_tag_key_collision(tags, tag), -1);
81    
82     while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)) {
83     gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
84     TAG_COL_DATA, &tag, -1);
85     g_assert(tag);
86     gtk_list_store_set(store, &iter,
87     TAG_COL_COLLISION, info_tag_key_collision(tags, tag), -1);
88     }
89     }
90     }
91    
92     static void on_tag_remove(GtkWidget *but, tag_context_t *context) {
93     GtkTreeSelection *selection;
94     GtkTreeModel *model;
95     GtkTreeIter iter;
96    
97     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->view));
98     if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
99     tag_t *tag;
100     gtk_tree_model_get(model, &iter, TAG_COL_DATA, &tag, -1);
101    
102     g_assert(tag);
103    
104     /* de-chain */
105     printf("de-chaining tag %s/%s\n", tag->key, tag->value);
106     tag_t **prev = context->tag;
107     while(*prev != tag) prev = &((*prev)->next);
108     *prev = tag->next;
109    
110     /* free tag itself */
111     osm_tag_free(tag);
112    
113     /* and remove from store */
114     gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
115    
116     update_collisions(context->store, *context->tag);
117     }
118    
119     /* disable remove and edit buttons */
120     gtk_widget_set_sensitive(context->but_remove, FALSE);
121     gtk_widget_set_sensitive(context->but_edit, FALSE);
122     }
123    
124     static gboolean tag_edit(tag_context_t *context) {
125    
126     GtkTreeModel *model;
127     GtkTreeIter iter;
128     tag_t *tag;
129    
130     GtkTreeSelection *sel = gtk_tree_view_get_selection(
131     GTK_TREE_VIEW(context->view));
132     if(!sel) {
133     printf("got no selection object\n");
134     return FALSE;
135     }
136    
137     if(!gtk_tree_selection_get_selected(sel, &model, &iter)) {
138     printf("nothing selected\n");
139     return FALSE;
140     }
141    
142     gtk_tree_model_get(model, &iter, TAG_COL_DATA, &tag, -1);
143     printf("got %s/%s\n", tag->key, tag->value);
144    
145     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Edit Tag"),
146     GTK_WINDOW(context->dialog), GTK_DIALOG_MODAL,
147     GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
148     GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
149     NULL);
150    
151     gtk_dialog_set_default_response(GTK_DIALOG(dialog),
152     GTK_RESPONSE_ACCEPT);
153    
154     GtkWidget *label, *key, *value;
155     GtkWidget *table = gtk_table_new(2, 2, FALSE);
156    
157     gtk_table_attach_defaults(GTK_TABLE(table),
158     label = gtk_label_new(_("Key:")), 0, 1, 0, 1);
159     gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
160     gtk_table_attach_defaults(GTK_TABLE(table),
161     key = gtk_entry_new(), 1, 2, 0, 1);
162     gtk_entry_set_activates_default(GTK_ENTRY(key), TRUE);
163     HILDON_ENTRY_NO_AUTOCAP(key);
164    
165     gtk_table_attach_defaults(GTK_TABLE(table),
166     label = gtk_label_new(_("Value:")), 0, 1, 1, 2);
167     gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
168     gtk_table_attach_defaults(GTK_TABLE(table),
169     value = gtk_entry_new(), 1, 2, 1, 2);
170     gtk_entry_set_activates_default(GTK_ENTRY(value), TRUE);
171     HILDON_ENTRY_NO_AUTOCAP(value);
172    
173     gtk_entry_set_text(GTK_ENTRY(key), tag->key);
174     gtk_entry_set_text(GTK_ENTRY(value), tag->value);
175    
176     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
177    
178     gtk_widget_show_all(dialog);
179    
180     if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
181     free(tag->key); free(tag->value);
182     tag->key = strdup((char*)gtk_entry_get_text(GTK_ENTRY(key)));
183     tag->value = strdup((char*)gtk_entry_get_text(GTK_ENTRY(value)));
184     printf("setting %s/%s\n", tag->key, tag->value);
185    
186     gtk_list_store_set(context->store, &iter,
187     TAG_COL_KEY, tag->key,
188     TAG_COL_VALUE, tag->value,
189     -1);
190    
191     gtk_widget_destroy(dialog);
192    
193     /* update collisions for all entries */
194     update_collisions(context->store, *context->tag);
195     return TRUE;
196     }
197    
198     gtk_widget_destroy(dialog);
199     return FALSE;
200     }
201    
202     static void on_tag_edit(GtkWidget *button, tag_context_t *context) {
203     tag_edit(context);
204     }
205    
206     static void on_tag_last(GtkWidget *button, tag_context_t *context) {
207     static const char *type_name[] = { "illegal", "node", "way", "relation" };
208    
209     if(yes_no_f(context->dialog,
210     context->appdata, MISC_AGAIN_ID_OVERWRITE_TAGS, 0,
211     _("Overwrite tags?"),
212     _("This will overwrite all tags of this %s with the "
213     "ones from the %s selected last.\n\n"
214     "Do you really want this?"),
215     type_name[context->type], type_name[context->type])) {
216    
217     osm_tags_free(*context->tag);
218    
219     if(context->type == NODE)
220     *context->tag = osm_tags_copy(context->appdata->map->last_node_tags, TRUE);
221     else
222     *context->tag = osm_tags_copy(context->appdata->map->last_way_tags, TRUE);
223    
224     info_tags_replace(context);
225     }
226     }
227    
228     static void on_tag_add(GtkWidget *button, tag_context_t *context) {
229     /* search end of tag chain */
230     tag_t **tag = context->tag;
231     while(*tag)
232     tag = &(*tag)->next;
233    
234     /* create and append a new tag */
235     *tag = g_new0(tag_t, 1);
236     if(!*tag) {
237     errorf(GTK_WIDGET(context->appdata->window), _("Out of memory"));
238     return;
239     }
240    
241     /* fill with some empty strings */
242     (*tag)->key = strdup("");
243     (*tag)->value = strdup("");
244    
245     /* append a row for the new data */
246     GtkTreeIter iter;
247     gtk_list_store_append(context->store, &iter);
248     gtk_list_store_set(context->store, &iter,
249     TAG_COL_KEY, (*tag)->key,
250     TAG_COL_VALUE, (*tag)->value,
251     TAG_COL_COLLISION, FALSE,
252     TAG_COL_DATA, *tag,
253     -1);
254    
255     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(
256     GTK_TREE_VIEW(context->view)), &iter);
257    
258     if(!tag_edit(context)) {
259     printf("cancelled\n");
260     on_tag_remove(NULL, context);
261     }
262     }
263    
264     void info_tags_replace(tag_context_t *context) {
265     gtk_list_store_clear(context->store);
266    
267     GtkTreeIter iter;
268     tag_t *tag = *context->tag;
269     while(tag) {
270     gtk_list_store_append(context->store, &iter);
271     gtk_list_store_set(context->store, &iter,
272     TAG_COL_KEY, tag->key,
273     TAG_COL_VALUE, tag->value,
274     TAG_COL_COLLISION, info_tag_key_collision(*context->tag, tag),
275     TAG_COL_DATA, tag,
276     -1);
277     tag = tag->next;
278     }
279     }
280    
281     static GtkWidget *tag_widget(tag_context_t *context) {
282     GtkWidget *vbox = gtk_vbox_new(FALSE,3);
283     context->view = gtk_tree_view_new();
284    
285     gtk_tree_selection_set_select_function(
286     gtk_tree_view_get_selection(GTK_TREE_VIEW(context->view)),
287     view_selection_func,
288     context, NULL);
289    
290     /* --- "Key" column --- */
291     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
292     g_object_set(renderer,
293     "ellipsize", PANGO_ELLIPSIZE_END,
294     "background", "red",
295     NULL );
296     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
297     _("Key"), renderer,
298     "text", TAG_COL_KEY,
299     "background-set", TAG_COL_COLLISION,
300     NULL);
301     gtk_tree_view_column_set_expand(column, TRUE);
302     gtk_tree_view_insert_column(GTK_TREE_VIEW(context->view), column, -1);
303    
304     /* --- "Value" column --- */
305     renderer = gtk_cell_renderer_text_new();
306     g_object_set(renderer,
307     "ellipsize", PANGO_ELLIPSIZE_END,
308     NULL );
309     column = gtk_tree_view_column_new_with_attributes(
310     _("Value"), renderer,
311     "text", TAG_COL_VALUE,
312     NULL);
313     gtk_tree_view_column_set_expand(column, TRUE);
314     gtk_tree_view_insert_column(GTK_TREE_VIEW(context->view), column, -1);
315    
316     /* build and fill the store */
317     context->store = gtk_list_store_new(TAG_NUM_COLS,
318     G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER);
319    
320     gtk_tree_view_set_model(GTK_TREE_VIEW(context->view),
321     GTK_TREE_MODEL(context->store));
322    
323     GtkTreeIter iter;
324     tag_t *tag = *context->tag;
325     while(tag) {
326     /* Append a row and fill in some data */
327     gtk_list_store_append(context->store, &iter);
328     gtk_list_store_set(context->store, &iter,
329     TAG_COL_KEY, tag->key,
330     TAG_COL_VALUE, tag->value,
331     TAG_COL_COLLISION, info_tag_key_collision(*context->tag, tag),
332     TAG_COL_DATA, tag,
333     -1);
334     tag = tag->next;
335     }
336    
337     g_object_unref(context->store);
338    
339     /* put it into a scrolled window */
340     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
341     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
342     GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
343     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window),
344     GTK_SHADOW_ETCHED_IN);
345     // gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 3);
346     gtk_container_add(GTK_CONTAINER(scrolled_window), context->view);
347     gtk_box_pack_start_defaults(GTK_BOX(vbox), scrolled_window);
348    
349     /* ------- button box ------------ */
350    
351     GtkWidget *hbox = gtk_hbox_new(TRUE,3);
352    
353     GtkWidget *but_last = gtk_button_new_with_label(_("Last..."));
354     gtk_box_pack_start_defaults(GTK_BOX(hbox), but_last);
355    
356     /* disable if no appropriate "last" tags have been stored or if the */
357     /* selected item isn't a node or way */
358     if(((context->type == NODE) &&
359     (!context->appdata->map->last_node_tags)) ||
360     ((context->type == WAY) &&
361     (!context->appdata->map->last_way_tags)) ||
362     ((context->type != NODE) && (context->type != WAY)))
363     gtk_widget_set_sensitive(but_last, FALSE);
364    
365     gtk_signal_connect(GTK_OBJECT(but_last), "clicked",
366     GTK_SIGNAL_FUNC(on_tag_last), context);
367    
368     GtkWidget *presets = josm_presets_select(context->appdata, context);
369     if(presets) gtk_box_pack_start_defaults(GTK_BOX(hbox), presets);
370    
371     context->but_add = gtk_button_new_with_label(_("Add..."));
372     gtk_box_pack_start_defaults(GTK_BOX(hbox), context->but_add);
373     gtk_signal_connect(GTK_OBJECT(context->but_add), "clicked",
374     GTK_SIGNAL_FUNC(on_tag_add), context);
375    
376     context->but_edit = gtk_button_new_with_label(_("Edit..."));
377     gtk_widget_set_sensitive(context->but_edit, FALSE);
378     gtk_box_pack_start_defaults(GTK_BOX(hbox), context->but_edit);
379     gtk_signal_connect(GTK_OBJECT(context->but_edit), "clicked",
380     GTK_SIGNAL_FUNC(on_tag_edit), context);
381    
382     context->but_remove = gtk_button_new_with_label(_("Remove"));
383     gtk_widget_set_sensitive(context->but_remove, FALSE);
384     gtk_box_pack_start_defaults(GTK_BOX(hbox), context->but_remove);
385     gtk_signal_connect(GTK_OBJECT(context->but_remove), "clicked",
386     GTK_SIGNAL_FUNC(on_tag_remove), context);
387    
388    
389     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
390     return vbox;
391     }
392    
393     /* edit tags of currently selected node or way or of the relation */
394     /* given */
395     void info_dialog(GtkWidget *parent, appdata_t *appdata, relation_t *relation) {
396     if(!relation)
397     g_assert(appdata->map->selected.type != MAP_TYPE_ILLEGAL);
398    
399     tag_context_t *context = g_new0(tag_context_t, 1);
400     user_t *user = NULL;
401     char *str = NULL;
402     time_t stime = 0;
403     tag_t *work_copy = NULL;
404    
405     context->appdata = appdata;
406     context->tag = &work_copy;
407    
408     if(!relation) {
409     switch(appdata->map->selected.type) {
410     case MAP_TYPE_NODE:
411     str = g_strdup_printf(_("Node #%ld"), appdata->map->selected.node->id);
412     user = appdata->map->selected.node->user;
413     work_copy = osm_tags_copy(appdata->map->selected.node->tag, FALSE);
414     stime = appdata->map->selected.node->time;
415     context->type = NODE;
416     break;
417     case MAP_TYPE_WAY:
418     str = g_strdup_printf(_("Way #%ld"), appdata->map->selected.way->id);
419     user = appdata->map->selected.way->user;
420     work_copy = osm_tags_copy(appdata->map->selected.way->tag, FALSE);
421     stime = appdata->map->selected.way->time;
422     context->type = WAY;
423     break;
424     default:
425     g_assert((appdata->map->selected.type == MAP_TYPE_NODE) ||
426     (appdata->map->selected.type == MAP_TYPE_WAY));
427     break;
428     }
429     } else {
430     str = g_strdup_printf(_("Relation #%ld"), relation->id);
431     user = relation->user;
432     work_copy = osm_tags_copy(relation->tag, FALSE);
433     stime = relation->time;
434     context->type = RELATION;
435     }
436    
437     context->dialog = gtk_dialog_new_with_buttons(str,
438     GTK_WINDOW(parent), GTK_DIALOG_MODAL,
439     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
440     GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
441     NULL);
442     g_free(str);
443    
444     gtk_dialog_set_default_response(GTK_DIALOG(context->dialog),
445     GTK_RESPONSE_ACCEPT);
446    
447     /* making the dialog a little wider makes it less "crowded" */
448     #ifdef USE_HILDON
449     gtk_window_set_default_size(GTK_WINDOW(context->dialog), 500, 300);
450     #else
451     gtk_window_set_default_size(GTK_WINDOW(context->dialog), 400, 200);
452     #endif
453    
454     GtkWidget *label;
455     GtkWidget *table = gtk_table_new(2, 2, FALSE); // x, y
456    
457     /* ------------ user ----------------- */
458     char *u_str = NULL;
459     if(user) u_str = g_strdup_printf(_("User: %s"), user->name);
460     else u_str = g_strdup_printf(_("User: ---"));
461     label = gtk_label_new(u_str);
462     gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
463     g_free(u_str);
464    
465     /* ------------ time ----------------- */
466    
467     struct tm *loctime = localtime(&stime);
468     char time_str[32];
469     strftime(time_str, sizeof(time_str), "%x %X", loctime);
470     char *t_str = g_strdup_printf(_("Time: %s"), time_str);
471     label = gtk_label_new(t_str);
472     g_free(t_str);
473     gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
474    
475     /* ------------ coordinate (only for nodes) ----------------- */
476     if(!relation) {
477     if(appdata->map->selected.type == MAP_TYPE_NODE) {
478     char pos_str[32];
479     pos_lat_str(pos_str, sizeof(pos_str),appdata->map->selected.node->pos.lat);
480     label = gtk_label_new(pos_str);
481     gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
482     pos_lat_str(pos_str, sizeof(pos_str),appdata->map->selected.node->pos.lon);
483     label = gtk_label_new(pos_str);
484     gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
485     } else {
486     char *nodes_str = g_strdup_printf(_("Length: %u nodes"),
487     osm_way_number_of_nodes(appdata->map->selected.way));
488     label = gtk_label_new(nodes_str);
489     gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
490     g_free(nodes_str);
491    
492     char *type_str = g_strdup_printf("%s (%s)",
493     (osm_way_get_last_node(appdata->map->selected.way) ==
494     osm_way_get_first_node(appdata->map->selected.way))?
495     "closed way":"open way",
496     (appdata->map->selected.way->draw.flags & OSM_DRAW_FLAG_AREA)?
497     "area":"line");
498    
499     label = gtk_label_new(type_str);
500     gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
501     g_free(type_str);
502     }
503     } else {
504     /* relations tell something about their members */
505     gint nodes = 0, ways = 0, relations = 0;
506     member_t *member = relation->member;
507     while(member) {
508     switch(member->type) {
509     case NODE:
510     case NODE_ID:
511     nodes++;
512     break;
513     case WAY:
514     case WAY_ID:
515     ways++;
516     break;
517     case RELATION:
518     case RELATION_ID:
519     relations++;
520     break;
521    
522     default:
523     break;
524     }
525    
526     member = member->next;
527     }
528    
529     char *str = g_strdup_printf(_("Members: %d nodes, %d ways, %d relations"),
530     nodes, ways, relations);
531    
532     gtk_table_attach_defaults(GTK_TABLE(table), gtk_label_new(str), 0, 2, 1, 2);
533     g_free(str);
534     }
535    
536     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), table,
537     FALSE, FALSE, 0);
538    
539    
540     /* ------------ tags ----------------- */
541    
542     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox),
543     tag_widget(context), TRUE, TRUE, 0);
544    
545     /* ----------------------------------- */
546    
547     gtk_widget_show_all(context->dialog);
548     if(gtk_dialog_run(GTK_DIALOG(context->dialog)) == GTK_RESPONSE_ACCEPT) {
549     gtk_widget_destroy(context->dialog);
550    
551     if(!relation) {
552     /* replace original tags with work copy */
553     switch(appdata->map->selected.type) {
554    
555     case MAP_TYPE_NODE:
556     osm_tags_free(appdata->map->selected.node->tag);
557     appdata->map->selected.node->tag = osm_tags_copy(work_copy, TRUE);
558     break;
559    
560     case MAP_TYPE_WAY:
561     osm_tags_free(appdata->map->selected.way->tag);
562     appdata->map->selected.way->tag = osm_tags_copy(work_copy, TRUE);
563     break;
564    
565     default:
566     break;
567     }
568    
569     /* since nodes being parts of ways but with no tags are invisible, */
570     /* the result of editing them may have changed their visibility */
571     map_item_redraw(appdata, &appdata->map->selected);
572     map_item_set_flags(&context->appdata->map->selected, OSM_FLAG_DIRTY, 0);
573     } else {
574     osm_tags_free(relation->tag);
575     relation->tag = osm_tags_copy(work_copy, TRUE);
576     relation->flags |= OSM_FLAG_DIRTY;
577     }
578     } else {
579     gtk_widget_destroy(context->dialog);
580     osm_tags_free(work_copy);
581     }
582    
583     g_free(context);
584     }