Contents of /trunk/src/info.c

Parent Directory Parent Directory | Revision Log Revision Log


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