Parent Directory | Revision Log
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 | /* UI sizes */ |
23 | /* TH: All this dialog size stuff should imho go into one central place */ |
24 | |
25 | #ifdef USE_HILDON |
26 | // Making the dialog a little wider makes it less "crowded" |
27 | static const guint LIST_OF_RELATIONS_DIALOG_WIDTH = 500; |
28 | static const guint LIST_OF_RELATIONS_DIALOG_HEIGHT = 300; |
29 | static const guint LIST_OF_MEMBERS_DIALOG_WIDTH = 500; |
30 | static const guint LIST_OF_MEMBERS_DIALOG_HEIGHT = 300; |
31 | #else |
32 | // Desktop mode dialogs should be narrower and taller |
33 | static const guint LIST_OF_RELATIONS_DIALOG_WIDTH = 475; |
34 | static const guint LIST_OF_RELATIONS_DIALOG_HEIGHT = 350; |
35 | static const guint LIST_OF_MEMBERS_DIALOG_WIDTH = 450; |
36 | static const guint LIST_OF_MEMBERS_DIALOG_HEIGHT = 350; |
37 | #endif |
38 | |
39 | |
40 | /* --------------- relation dialog for an item (node or way) ----------- */ |
41 | |
42 | typedef struct { |
43 | relation_item_t *item; |
44 | appdata_t *appdata; |
45 | GtkWidget *dialog, *list; |
46 | GtkListStore *store; |
47 | } relitem_context_t; |
48 | |
49 | enum { |
50 | RELITEM_COL_SELECTED = 0, |
51 | RELITEM_COL_TYPE, |
52 | RELITEM_COL_ROLE, |
53 | RELITEM_COL_NAME, |
54 | RELITEM_COL_DATA, |
55 | RELITEM_NUM_COLS |
56 | }; |
57 | |
58 | typedef struct role_chain_s { |
59 | char *role; |
60 | struct role_chain_s *next; |
61 | } role_chain_t; |
62 | |
63 | static gboolean relation_add_item(GtkWidget *parent, |
64 | relation_t *relation, relation_item_t *item) { |
65 | role_chain_t *chain = NULL, **chainP = &chain; |
66 | |
67 | printf("add item of type %d to relation #%ld\n", |
68 | item->type, relation->id); |
69 | |
70 | /* ask the user for the role of the new item in this relation */ |
71 | |
72 | /* collect roles first */ |
73 | member_t *member = relation->member; |
74 | while(member) { |
75 | if(member->role) { |
76 | /* check if this role has already been saved */ |
77 | gboolean already_stored = FALSE; |
78 | role_chain_t *crole = chain; |
79 | while(crole) { |
80 | if(strcasecmp(crole->role, member->role) == 0) already_stored = TRUE; |
81 | crole = crole->next; |
82 | } |
83 | |
84 | /* not stored yet: attach it */ |
85 | if(!already_stored) { |
86 | *chainP = g_new0(role_chain_t, 1); |
87 | (*chainP)->role = g_strdup(member->role); |
88 | chainP = &(*chainP)->next; |
89 | } |
90 | } |
91 | member = member->next; |
92 | } |
93 | |
94 | /* ------------------ role dialog ---------------- */ |
95 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Select role"), |
96 | GTK_WINDOW(parent), GTK_DIALOG_MODAL, |
97 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
98 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
99 | NULL); |
100 | |
101 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); |
102 | |
103 | char *type = osm_tag_get_by_key(relation->tag, "type"); |
104 | |
105 | char *info_str = NULL; |
106 | if(type) info_str = g_strdup_printf(_("In relation of type: %s"), type); |
107 | else info_str = g_strdup_printf(_("In relation #%ld"), relation->id); |
108 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
109 | gtk_label_new(info_str)); |
110 | g_free(info_str); |
111 | |
112 | char *name = osm_tag_get_by_key(relation->tag, "name"); |
113 | if(name) |
114 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
115 | gtk_label_new(name)); |
116 | |
117 | GtkWidget *hbox = gtk_hbox_new(FALSE, 8); |
118 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("Role:"))); |
119 | |
120 | GtkWidget *entry = NULL; |
121 | if(chain) { |
122 | entry = gtk_combo_box_entry_new_text(); |
123 | |
124 | /* fill combo box with presets */ |
125 | while(chain) { |
126 | role_chain_t *next = chain->next; |
127 | gtk_combo_box_append_text(GTK_COMBO_BOX(entry), chain->role); |
128 | g_free(chain); |
129 | chain = next; |
130 | } |
131 | } else |
132 | entry = gtk_entry_new(); |
133 | |
134 | gtk_box_pack_start_defaults(GTK_BOX(hbox), entry); |
135 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
136 | |
137 | gtk_widget_show_all(dialog); |
138 | if(GTK_RESPONSE_ACCEPT != gtk_dialog_run(GTK_DIALOG(dialog))) { |
139 | printf("user clicked cancel\n"); |
140 | gtk_widget_destroy(dialog); |
141 | return FALSE; |
142 | } |
143 | |
144 | printf("user clicked ok\n"); |
145 | |
146 | /* get role from dialog */ |
147 | char *ptr = NULL; |
148 | |
149 | if(GTK_IS_COMBO_BOX(entry)) |
150 | ptr = gtk_combo_box_get_active_text(GTK_COMBO_BOX(entry)); |
151 | else |
152 | ptr = (char*)gtk_entry_get_text(GTK_ENTRY(entry)); |
153 | |
154 | char *role = NULL; |
155 | if(ptr && strlen(ptr)) role = g_strdup(ptr); |
156 | |
157 | gtk_widget_destroy(dialog); |
158 | |
159 | /* search end of member chain */ |
160 | member_t **memberP = &relation->member; |
161 | while(*memberP) memberP = &(*memberP)->next; |
162 | |
163 | /* create new member */ |
164 | *memberP = g_new0(member_t, 1); |
165 | (*memberP)->type = item->type; |
166 | (*memberP)->role = role; |
167 | switch(item->type) { |
168 | case NODE: |
169 | (*memberP)->node = item->node; |
170 | break; |
171 | case WAY: |
172 | (*memberP)->way = item->way; |
173 | break; |
174 | case RELATION: |
175 | (*memberP)->relation = item->relation; |
176 | break; |
177 | default: |
178 | g_assert((item->type == NODE)||(item->type == WAY)|| |
179 | (item->type == RELATION)); |
180 | break; |
181 | } |
182 | |
183 | relation->flags |= OSM_FLAG_DIRTY; |
184 | return TRUE; |
185 | } |
186 | |
187 | static void relation_remove_item(relation_t *relation, relation_item_t *item) { |
188 | |
189 | printf("remove item of type %d from relation #%ld\n", |
190 | item->type, relation->id); |
191 | |
192 | member_t **member = &relation->member; |
193 | while(*member) { |
194 | if(((*member)->type == item->type) && |
195 | (((item->type == NODE) && (item->node == (*member)->node)) || |
196 | ((item->type == WAY) && (item->way == (*member)->way)) || |
197 | ((item->type == RELATION) && (item->relation == (*member)->relation)))) { |
198 | |
199 | member_t *next = (*member)->next; |
200 | osm_member_free(*member); |
201 | *member = next; |
202 | |
203 | relation->flags |= OSM_FLAG_DIRTY; |
204 | |
205 | return; |
206 | } else |
207 | member = &(*member)->next; |
208 | } |
209 | g_assert(0); |
210 | } |
211 | |
212 | static void relation_item_list_selected(relitem_context_t *context, |
213 | gboolean selected) { |
214 | |
215 | list_button_enable(context->list, LIST_BUTTON_REMOVE, selected); |
216 | list_button_enable(context->list, LIST_BUTTON_EDIT, selected); |
217 | } |
218 | |
219 | /* try to find something descriptive */ |
220 | static char *relation_get_descriptive_name(relation_t *relation) { |
221 | char *name = osm_tag_get_by_key(relation->tag, "ref"); |
222 | if (!name) |
223 | name = osm_tag_get_by_key(relation->tag, "name"); |
224 | if (!name) |
225 | name = osm_tag_get_by_key(relation->tag, "note"); |
226 | if (!name) |
227 | name = osm_tag_get_by_key(relation->tag, "fix" "me"); |
228 | return name; |
229 | } |
230 | |
231 | static gboolean relation_info_dialog(GtkWidget *parent, appdata_t *appdata, |
232 | relation_t *relation) { |
233 | |
234 | object_t object = { .type = RELATION }; |
235 | object.relation = relation; |
236 | return info_dialog(parent, appdata, &object); |
237 | } |
238 | |
239 | static void on_relation_item_add(GtkWidget *but, relitem_context_t *context) { |
240 | /* create a new relation */ |
241 | |
242 | relation_t *relation = osm_relation_new(); |
243 | if(!relation_info_dialog(context->dialog, context->appdata, relation)) { |
244 | printf("tag edit cancelled\n"); |
245 | osm_relation_free(relation); |
246 | } else { |
247 | osm_relation_attach(context->appdata->osm, relation); |
248 | |
249 | /* add to list */ |
250 | |
251 | /* append a row for the new data */ |
252 | char *name = relation_get_descriptive_name(relation); |
253 | |
254 | GtkTreeIter iter; |
255 | gtk_list_store_append(context->store, &iter); |
256 | gtk_list_store_set(context->store, &iter, |
257 | RELITEM_COL_SELECTED, FALSE, |
258 | RELITEM_COL_TYPE, |
259 | osm_tag_get_by_key(relation->tag, "type"), |
260 | RELITEM_COL_NAME, name, |
261 | RELITEM_COL_DATA, relation, |
262 | -1); |
263 | |
264 | gtk_tree_selection_select_iter(list_get_selection(context->list), &iter); |
265 | } |
266 | } |
267 | |
268 | static relation_t *get_selection(relitem_context_t *context) { |
269 | GtkTreeSelection *selection; |
270 | GtkTreeModel *model; |
271 | GtkTreeIter iter; |
272 | |
273 | selection = list_get_selection(context->list); |
274 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
275 | relation_t *relation; |
276 | gtk_tree_model_get(model, &iter, RELITEM_COL_DATA, &relation, -1); |
277 | return(relation); |
278 | } |
279 | return NULL; |
280 | } |
281 | |
282 | static void on_relation_item_edit(GtkWidget *but, relitem_context_t *context) { |
283 | relation_t *sel = get_selection(context); |
284 | if(!sel) return; |
285 | |
286 | printf("edit relation item #%ld\n", sel->id); |
287 | |
288 | if (!relation_info_dialog(context->dialog, context->appdata, sel)) |
289 | return; |
290 | |
291 | // Locate the changed item |
292 | GtkTreeIter iter; |
293 | gboolean valid = gtk_tree_model_get_iter_first( |
294 | GTK_TREE_MODEL(context->store), &iter); |
295 | while (valid) { |
296 | relation_t *row_rel; |
297 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, |
298 | RELITEM_COL_DATA, &row_rel, |
299 | -1); |
300 | if (row_rel == sel) |
301 | break; |
302 | valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(context->store), &iter); |
303 | } |
304 | if (!valid) |
305 | return; |
306 | |
307 | // Found it. Update all visible fields that belong to the relation iself. |
308 | gtk_list_store_set(context->store, &iter, |
309 | RELITEM_COL_TYPE, osm_tag_get_by_key(sel->tag, "type"), |
310 | RELITEM_COL_NAME, relation_get_descriptive_name(sel), |
311 | -1); |
312 | |
313 | // Order will probably have changed, so refocus |
314 | list_focus_on(context->list, &iter, TRUE); |
315 | } |
316 | |
317 | /* remove the selected relation */ |
318 | static void on_relation_item_remove(GtkWidget *but, relitem_context_t *context) { |
319 | relation_t *sel = get_selection(context); |
320 | if(!sel) return; |
321 | |
322 | printf("remove relation #%ld\n", sel->id); |
323 | |
324 | gint members = osm_relation_members_num(sel); |
325 | |
326 | if(members) |
327 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
328 | _("Delete non-empty relation?"), |
329 | _("This relation still has %d members. " |
330 | "Delete it anyway?"), members)) |
331 | return; |
332 | |
333 | /* first remove selected row from list */ |
334 | GtkTreeIter iter; |
335 | GtkTreeSelection *selection = list_get_selection(context->list); |
336 | if(gtk_tree_selection_get_selected(selection, NULL, &iter)) |
337 | gtk_list_store_remove(context->store, &iter); |
338 | |
339 | /* then really delete it */ |
340 | osm_relation_delete(context->appdata->osm, sel, FALSE); |
341 | |
342 | relation_item_list_selected(context, FALSE); |
343 | } |
344 | |
345 | static char *relitem_get_role_in_relation(relation_item_t *item, relation_t *relation) { |
346 | member_t *member = relation->member; |
347 | while(member) { |
348 | switch(member->type) { |
349 | |
350 | case NODE: |
351 | if((item->type == NODE) && (item->node == member->node)) |
352 | return member->role; |
353 | break; |
354 | |
355 | case WAY: |
356 | if((item->type == WAY) && (item->way == member->way)) |
357 | return member->role; |
358 | break; |
359 | |
360 | default: |
361 | break; |
362 | } |
363 | member = member->next; |
364 | } |
365 | return NULL; |
366 | } |
367 | |
368 | static void |
369 | relitem_toggled(GtkCellRendererToggle *cell, const gchar *path_str, |
370 | relitem_context_t *context) { |
371 | GtkTreePath *path; |
372 | GtkTreeIter iter; |
373 | |
374 | path = gtk_tree_path_new_from_string(path_str); |
375 | gtk_tree_model_get_iter(GTK_TREE_MODEL(context->store), &iter, path); |
376 | gtk_tree_path_free(path); |
377 | |
378 | /* get current enabled flag */ |
379 | gboolean enabled; |
380 | relation_t *relation = NULL; |
381 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, |
382 | RELITEM_COL_SELECTED, &enabled, |
383 | RELITEM_COL_DATA, &relation, |
384 | -1); |
385 | |
386 | list_pre_inplace_edit_tweak(GTK_TREE_MODEL(context->store)); |
387 | |
388 | if(!enabled) { |
389 | printf("will now become be part of this relation\n"); |
390 | if(relation_add_item(context->dialog, relation, context->item)) |
391 | gtk_list_store_set(context->store, &iter, |
392 | RELITEM_COL_SELECTED, TRUE, |
393 | RELITEM_COL_ROLE, |
394 | relitem_get_role_in_relation(context->item, relation), |
395 | -1); |
396 | } else { |
397 | printf("item will not be part of this relation anymore\n"); |
398 | relation_remove_item(relation, context->item); |
399 | gtk_list_store_set(context->store, &iter, |
400 | RELITEM_COL_SELECTED, FALSE, |
401 | RELITEM_COL_ROLE, NULL, |
402 | -1); |
403 | } |
404 | |
405 | } |
406 | |
407 | static gboolean relitem_is_in_relation(relation_item_t *item, relation_t *relation) { |
408 | member_t *member = relation->member; |
409 | while(member) { |
410 | switch(member->type) { |
411 | |
412 | case NODE: |
413 | if((item->type == NODE) && (item->node == member->node)) |
414 | return TRUE; |
415 | break; |
416 | |
417 | case WAY: |
418 | if((item->type == WAY) && (item->way == member->way)) |
419 | return TRUE; |
420 | break; |
421 | |
422 | default: |
423 | break; |
424 | } |
425 | member = member->next; |
426 | } |
427 | return FALSE; |
428 | } |
429 | |
430 | static GtkWidget *relation_item_list_widget(relitem_context_t *context) { |
431 | context->list = list_new(LIST_HILDON_WITH_HEADERS); |
432 | |
433 | list_set_columns(context->list, |
434 | _(""), RELITEM_COL_SELECTED, LIST_FLAG_TOGGLE, |
435 | G_CALLBACK(relitem_toggled), context, |
436 | _("Type"), RELITEM_COL_TYPE, 0, |
437 | _("Role"), RELITEM_COL_ROLE, 0, |
438 | _("Name"), RELITEM_COL_NAME, LIST_FLAG_ELLIPSIZE, |
439 | NULL); |
440 | |
441 | /* build and fill the store */ |
442 | context->store = gtk_list_store_new(RELITEM_NUM_COLS, |
443 | G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, |
444 | G_TYPE_STRING, G_TYPE_POINTER); |
445 | |
446 | list_set_store(context->list, context->store); |
447 | |
448 | // Debatable whether to sort by the "selected" or the "Name" column by |
449 | // default. Both are be useful, in different ways. |
450 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(context->store), |
451 | RELITEM_COL_NAME, GTK_SORT_ASCENDING); |
452 | |
453 | GtkTreeIter iter; |
454 | relation_t *relation = context->appdata->osm->relation; |
455 | while(relation) { |
456 | /* try to find something descriptive */ |
457 | char *name = relation_get_descriptive_name(relation); |
458 | |
459 | /* Append a row and fill in some data */ |
460 | gtk_list_store_append(context->store, &iter); |
461 | gtk_list_store_set(context->store, &iter, |
462 | RELITEM_COL_SELECTED, relitem_is_in_relation(context->item, relation), |
463 | RELITEM_COL_TYPE, osm_tag_get_by_key(relation->tag, "type"), |
464 | RELITEM_COL_ROLE, relitem_get_role_in_relation(context->item, relation), |
465 | RELITEM_COL_NAME, name, |
466 | RELITEM_COL_DATA, relation, |
467 | -1); |
468 | |
469 | relation = relation->next; |
470 | } |
471 | |
472 | g_object_unref(context->store); |
473 | |
474 | list_set_static_buttons(context->list, G_CALLBACK(on_relation_item_add), |
475 | G_CALLBACK(on_relation_item_edit),G_CALLBACK(on_relation_item_remove), |
476 | context); |
477 | |
478 | relation_item_list_selected(context, FALSE); |
479 | |
480 | return context->list; |
481 | } |
482 | |
483 | void relation_add_dialog(appdata_t *appdata, relation_item_t *relitem) { |
484 | relitem_context_t *context = g_new0(relitem_context_t, 1); |
485 | map_t *map = appdata->map; |
486 | g_assert(map); |
487 | |
488 | context->appdata = appdata; |
489 | context->item = relitem; |
490 | |
491 | char *str = NULL; |
492 | switch(relitem->type) { |
493 | case NODE: |
494 | str = g_strdup_printf(_("Relations for node #%ld"), relitem->node->id); |
495 | break; |
496 | case WAY: |
497 | str = g_strdup_printf(_("Relations for way #%ld"), relitem->way->id); |
498 | break; |
499 | default: |
500 | g_assert((relitem->type == NODE) || (relitem->type == WAY)); |
501 | break; |
502 | } |
503 | |
504 | context->dialog = gtk_dialog_new_with_buttons(str, |
505 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
506 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
507 | NULL); |
508 | g_free(str); |
509 | |
510 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
511 | GTK_RESPONSE_CLOSE); |
512 | |
513 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), |
514 | LIST_OF_RELATIONS_DIALOG_WIDTH, |
515 | LIST_OF_RELATIONS_DIALOG_HEIGHT); |
516 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
517 | relation_item_list_widget(context), TRUE, TRUE, 0); |
518 | |
519 | /* ----------------------------------- */ |
520 | |
521 | gtk_widget_show_all(context->dialog); |
522 | gtk_dialog_run(GTK_DIALOG(context->dialog)); |
523 | gtk_widget_destroy(context->dialog); |
524 | |
525 | g_free(context); |
526 | } |
527 | |
528 | /* -------------------- global relation list ----------------- */ |
529 | |
530 | typedef struct { |
531 | appdata_t *appdata; |
532 | GtkWidget *dialog, *list, *show_btn; |
533 | GtkListStore *store; |
534 | } relation_context_t; |
535 | |
536 | enum { |
537 | RELATION_COL_ID = 0, |
538 | RELATION_COL_TYPE, |
539 | RELATION_COL_NAME, |
540 | RELATION_COL_MEMBERS, |
541 | RELATION_COL_DATA, |
542 | RELATION_NUM_COLS |
543 | }; |
544 | |
545 | static relation_t *get_selected_relation(relation_context_t *context) { |
546 | GtkTreeSelection *selection; |
547 | GtkTreeModel *model; |
548 | GtkTreeIter iter; |
549 | |
550 | selection = list_get_selection(context->list); |
551 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
552 | relation_t *relation; |
553 | gtk_tree_model_get(model, &iter, RELATION_COL_DATA, &relation, -1); |
554 | return(relation); |
555 | } |
556 | return NULL; |
557 | } |
558 | |
559 | static void relation_list_selected(relation_context_t *context, |
560 | relation_t *selected) { |
561 | |
562 | list_button_enable(context->list, LIST_BUTTON_USER0, |
563 | (selected != NULL) && (selected->member != NULL)); |
564 | gtk_widget_set_sensitive(context->show_btn, |
565 | (selected != NULL) && (selected->member != NULL)); |
566 | |
567 | list_button_enable(context->list, LIST_BUTTON_REMOVE, selected != NULL); |
568 | list_button_enable(context->list, LIST_BUTTON_EDIT, selected != NULL); |
569 | } |
570 | |
571 | static gboolean |
572 | relation_list_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
573 | GtkTreePath *path, gboolean path_currently_selected, |
574 | gpointer userdata) { |
575 | relation_context_t *context = (relation_context_t*)userdata; |
576 | GtkTreeIter iter; |
577 | |
578 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
579 | g_assert(gtk_tree_path_get_depth(path) == 1); |
580 | |
581 | relation_t *relation = NULL; |
582 | gtk_tree_model_get(model, &iter, RELATION_COL_DATA, &relation, -1); |
583 | relation_list_selected(context, relation); |
584 | } |
585 | |
586 | return TRUE; /* allow selection state to change */ |
587 | } |
588 | |
589 | typedef struct { |
590 | relation_t *relation; |
591 | GtkWidget *dialog, *view; |
592 | GtkListStore *store; |
593 | } member_context_t; |
594 | |
595 | enum { |
596 | MEMBER_COL_TYPE = 0, |
597 | MEMBER_COL_ID, |
598 | MEMBER_COL_NAME, |
599 | MEMBER_COL_ROLE, |
600 | MEMBER_COL_REF_ONLY, |
601 | MEMBER_COL_DATA, |
602 | MEMBER_NUM_COLS |
603 | }; |
604 | |
605 | static gboolean |
606 | member_list_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
607 | GtkTreePath *path, gboolean path_currently_selected, |
608 | gpointer userdata) { |
609 | GtkTreeIter iter; |
610 | |
611 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
612 | g_assert(gtk_tree_path_get_depth(path) == 1); |
613 | |
614 | member_t *member = NULL; |
615 | gtk_tree_model_get(model, &iter, MEMBER_COL_DATA, &member, -1); |
616 | if(member && member->type < NODE_ID) |
617 | return TRUE; |
618 | } |
619 | |
620 | return FALSE; |
621 | } |
622 | |
623 | |
624 | static GtkWidget *member_list_widget(member_context_t *context) { |
625 | GtkWidget *vbox = gtk_vbox_new(FALSE,3); |
626 | context->view = gtk_tree_view_new(); |
627 | |
628 | gtk_tree_selection_set_select_function( |
629 | gtk_tree_view_get_selection(GTK_TREE_VIEW(context->view)), |
630 | member_list_selection_func, |
631 | context, NULL); |
632 | |
633 | /* --- "type" column --- */ |
634 | GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
635 | g_object_set(renderer, "foreground", "grey", NULL); |
636 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), |
637 | -1, _("Type"), renderer, "text", MEMBER_COL_TYPE, |
638 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); |
639 | |
640 | |
641 | /* --- "id" column --- */ |
642 | renderer = gtk_cell_renderer_text_new(); |
643 | g_object_set(renderer, "foreground", "grey", NULL); |
644 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), |
645 | -1, _("Id"), renderer, "text", MEMBER_COL_ID, |
646 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); |
647 | |
648 | /* --- "Name" column --- */ |
649 | renderer = gtk_cell_renderer_text_new(); |
650 | g_object_set(renderer, "foreground", "grey", NULL); |
651 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
652 | GtkTreeViewColumn *column = |
653 | gtk_tree_view_column_new_with_attributes(_("Name"), renderer, |
654 | "text", MEMBER_COL_NAME, |
655 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); |
656 | gtk_tree_view_column_set_expand(column, TRUE); |
657 | gtk_tree_view_insert_column(GTK_TREE_VIEW(context->view), column, -1); |
658 | |
659 | /* --- "role" column --- */ |
660 | renderer = gtk_cell_renderer_text_new(); |
661 | g_object_set(renderer, "foreground", "grey", NULL); |
662 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), |
663 | -1, _("Role"), renderer, "text", MEMBER_COL_ROLE, |
664 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); |
665 | |
666 | /* build and fill the store */ |
667 | context->store = gtk_list_store_new(MEMBER_NUM_COLS, |
668 | G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, |
669 | G_TYPE_BOOLEAN, G_TYPE_POINTER); |
670 | |
671 | gtk_tree_view_set_model(GTK_TREE_VIEW(context->view), |
672 | GTK_TREE_MODEL(context->store)); |
673 | |
674 | GtkTreeIter iter; |
675 | member_t *member = context->relation->member; |
676 | while(member) { |
677 | tag_t *tags = osm_object_get_tags(member->type, member->ptr); |
678 | char *id = osm_id_string(member->type, member->ptr); |
679 | |
680 | /* try to find something descriptive */ |
681 | char *name = NULL; |
682 | if(tags) |
683 | name = osm_tag_get_by_key(tags, "name"); |
684 | |
685 | /* Append a row and fill in some data */ |
686 | gtk_list_store_append(context->store, &iter); |
687 | gtk_list_store_set(context->store, &iter, |
688 | MEMBER_COL_TYPE, osm_type_string(member->type), |
689 | MEMBER_COL_ID, id, |
690 | MEMBER_COL_NAME, name, |
691 | MEMBER_COL_ROLE, member->role, |
692 | MEMBER_COL_REF_ONLY, member->type >= NODE_ID, |
693 | MEMBER_COL_DATA, member, |
694 | -1); |
695 | |
696 | g_free(id); |
697 | member = member->next; |
698 | } |
699 | |
700 | g_object_unref(context->store); |
701 | |
702 | /* put it into a scrolled window */ |
703 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
704 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
705 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
706 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), |
707 | GTK_SHADOW_ETCHED_IN); |
708 | gtk_container_add(GTK_CONTAINER(scrolled_window), context->view); |
709 | |
710 | gtk_box_pack_start_defaults(GTK_BOX(vbox), scrolled_window); |
711 | |
712 | return vbox; |
713 | } |
714 | |
715 | /* user clicked "members..." button in relation list */ |
716 | static void on_relation_members(GtkWidget *but, relation_context_t *context) { |
717 | member_context_t *mcontext = g_new0(member_context_t, 1); |
718 | |
719 | /* display members list */ |
720 | mcontext->relation = get_selected_relation(context); |
721 | if(!mcontext->relation) return; |
722 | |
723 | char *str = osm_tag_get_by_key(mcontext->relation->tag, "name"); |
724 | if(!str) str = osm_tag_get_by_key(mcontext->relation->tag, "ref"); |
725 | if(!str) |
726 | str = g_strdup_printf(_("Members of relation #%ld"), |
727 | mcontext->relation->id); |
728 | else |
729 | str = g_strdup_printf(_("Members of relation \"%s\""), str); |
730 | |
731 | mcontext->dialog = |
732 | gtk_dialog_new_with_buttons(str, |
733 | GTK_WINDOW(context->dialog), GTK_DIALOG_MODAL, |
734 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
735 | NULL); |
736 | g_free(str); |
737 | |
738 | gtk_dialog_set_default_response(GTK_DIALOG(mcontext->dialog), |
739 | GTK_RESPONSE_CLOSE); |
740 | |
741 | gtk_window_set_default_size(GTK_WINDOW(mcontext->dialog), |
742 | LIST_OF_MEMBERS_DIALOG_WIDTH, |
743 | LIST_OF_MEMBERS_DIALOG_HEIGHT); |
744 | |
745 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(mcontext->dialog)->vbox), |
746 | member_list_widget(mcontext), TRUE, TRUE, 0); |
747 | |
748 | /* ----------------------------------- */ |
749 | |
750 | gtk_widget_show_all(mcontext->dialog); |
751 | gtk_dialog_run(GTK_DIALOG(mcontext->dialog)); |
752 | gtk_widget_destroy(mcontext->dialog); |
753 | |
754 | g_free(mcontext); |
755 | } |
756 | |
757 | static void on_relation_add(GtkWidget *but, relation_context_t *context) { |
758 | /* create a new relation */ |
759 | |
760 | relation_t *relation = osm_relation_new(); |
761 | if(!relation_info_dialog(context->dialog, context->appdata, relation)) { |
762 | printf("tag edit cancelled\n"); |
763 | osm_relation_free(relation); |
764 | } else { |
765 | osm_relation_attach(context->appdata->osm, relation); |
766 | |
767 | /* append a row for the new data */ |
768 | |
769 | char *name = relation_get_descriptive_name(relation); |
770 | |
771 | guint num = osm_relation_members_num(relation); |
772 | |
773 | /* Append a row and fill in some data */ |
774 | GtkTreeIter iter; |
775 | gtk_list_store_append(context->store, &iter); |
776 | gtk_list_store_set(context->store, &iter, |
777 | RELATION_COL_ID, relation->id, |
778 | RELATION_COL_TYPE, |
779 | osm_tag_get_by_key(relation->tag, "type"), |
780 | RELATION_COL_NAME, name, |
781 | RELATION_COL_MEMBERS, num, |
782 | RELATION_COL_DATA, relation, |
783 | -1); |
784 | |
785 | gtk_tree_selection_select_iter(list_get_selection(context->list), &iter); |
786 | |
787 | /* scroll to end */ |
788 | // GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(); |
789 | /* xyz */ |
790 | } |
791 | } |
792 | |
793 | /* user clicked "edit..." button in relation list */ |
794 | static void on_relation_edit(GtkWidget *but, relation_context_t *context) { |
795 | relation_t *sel = get_selected_relation(context); |
796 | if(!sel) return; |
797 | |
798 | printf("edit relation #%ld\n", sel->id); |
799 | |
800 | if (!relation_info_dialog(context->dialog, context->appdata, sel)) |
801 | return; |
802 | |
803 | // Locate the changed item |
804 | GtkTreeIter iter; |
805 | gboolean valid = gtk_tree_model_get_iter_first( |
806 | GTK_TREE_MODEL(context->store), &iter); |
807 | while (valid) { |
808 | relation_t *row_rel; |
809 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, |
810 | RELATION_COL_DATA, &row_rel, |
811 | -1); |
812 | if (row_rel == sel) |
813 | break; |
814 | valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(context->store), &iter); |
815 | } |
816 | if (!valid) |
817 | return; |
818 | |
819 | // Found it. Update all visible fields. |
820 | gtk_list_store_set(context->store, &iter, |
821 | RELATION_COL_ID, sel->id, |
822 | RELATION_COL_TYPE, osm_tag_get_by_key(sel->tag, "type"), |
823 | RELATION_COL_NAME, relation_get_descriptive_name(sel), |
824 | RELATION_COL_MEMBERS, osm_relation_members_num(sel), |
825 | -1); |
826 | |
827 | // Order will probably have changed, so refocus |
828 | list_focus_on(context->list, &iter, TRUE); |
829 | } |
830 | |
831 | |
832 | /* remove the selected relation */ |
833 | static void on_relation_remove(GtkWidget *but, relation_context_t *context) { |
834 | relation_t *sel = get_selected_relation(context); |
835 | if(!sel) return; |
836 | |
837 | printf("remove relation #%ld\n", sel->id); |
838 | |
839 | gint members = osm_relation_members_num(sel); |
840 | |
841 | if(members) |
842 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
843 | _("Delete non-empty relation?"), |
844 | _("This relation still has %d members. " |
845 | "Delete it anyway?"), members)) |
846 | return; |
847 | |
848 | /* first remove selected row from list */ |
849 | GtkTreeIter iter; |
850 | GtkTreeSelection *selection = list_get_selection(context->list); |
851 | if(gtk_tree_selection_get_selected(selection, NULL, &iter)) |
852 | gtk_list_store_remove(context->store, &iter); |
853 | |
854 | /* then really delete it */ |
855 | osm_relation_delete(context->appdata->osm, sel, FALSE); |
856 | |
857 | relation_list_selected(context, NULL); |
858 | } |
859 | |
860 | static GtkWidget *relation_list_widget(relation_context_t *context) { |
861 | context->list = list_new(LIST_HILDON_WITH_HEADERS); |
862 | |
863 | list_set_selection_function(context->list, relation_list_selection_func, |
864 | context); |
865 | |
866 | list_set_columns(context->list, |
867 | _("Id"), RELATION_COL_ID, 0, |
868 | _("Type"), RELATION_COL_TYPE, 0, |
869 | _("Name"), RELATION_COL_NAME, LIST_FLAG_ELLIPSIZE, |
870 | _("Members"), RELATION_COL_MEMBERS, 0, |
871 | NULL); |
872 | |
873 | /* build and fill the store */ |
874 | context->store = gtk_list_store_new(RELATION_NUM_COLS, |
875 | G_TYPE_ITEM_ID_T, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, |
876 | G_TYPE_POINTER); |
877 | |
878 | list_set_store(context->list, context->store); |
879 | |
880 | // Sorting by ref/name by default is useful for places with lots of numbered |
881 | // bus routes. Especially for small screens. |
882 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(context->store), |
883 | RELATION_COL_NAME, GTK_SORT_ASCENDING); |
884 | |
885 | GtkTreeIter iter; |
886 | relation_t *relation = context->appdata->osm->relation; |
887 | while(relation) { |
888 | char *name = relation_get_descriptive_name(relation); |
889 | |
890 | guint num = osm_relation_members_num(relation); |
891 | |
892 | /* Append a row and fill in some data */ |
893 | gtk_list_store_append(context->store, &iter); |
894 | gtk_list_store_set(context->store, &iter, |
895 | RELATION_COL_ID, relation->id, |
896 | RELATION_COL_TYPE, |
897 | osm_tag_get_by_key(relation->tag, "type"), |
898 | RELATION_COL_NAME, name, |
899 | RELATION_COL_MEMBERS, num, |
900 | RELATION_COL_DATA, relation, |
901 | -1); |
902 | |
903 | relation = relation->next; |
904 | } |
905 | |
906 | g_object_unref(context->store); |
907 | |
908 | list_set_static_buttons(context->list, G_CALLBACK(on_relation_add), |
909 | G_CALLBACK(on_relation_edit), G_CALLBACK(on_relation_remove), context); |
910 | |
911 | list_set_user_buttons(context->list, |
912 | LIST_BUTTON_USER0, _("Members..."), G_CALLBACK(on_relation_members), |
913 | 0); |
914 | |
915 | relation_list_selected(context, NULL); |
916 | |
917 | return context->list; |
918 | } |
919 | |
920 | /* a global view on all relations */ |
921 | void relation_list(appdata_t *appdata) { |
922 | relation_context_t *context = g_new0(relation_context_t, 1); |
923 | context->appdata = appdata; |
924 | |
925 | printf("relation list\n"); |
926 | |
927 | context->dialog = |
928 | gtk_dialog_new_with_buttons(_("All relations"), |
929 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
930 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
931 | NULL); |
932 | |
933 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
934 | GTK_RESPONSE_CLOSE); |
935 | |
936 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), |
937 | LIST_OF_RELATIONS_DIALOG_WIDTH, |
938 | LIST_OF_RELATIONS_DIALOG_HEIGHT); |
939 | |
940 | context->show_btn = gtk_dialog_add_button(GTK_DIALOG(context->dialog), |
941 | _("Select"), GTK_RESPONSE_HELP); |
942 | |
943 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
944 | relation_list_widget(context), TRUE, TRUE, 0); |
945 | |
946 | /* ----------------------------------- */ |
947 | |
948 | |
949 | gtk_widget_show_all(context->dialog); |
950 | if(gtk_dialog_run(GTK_DIALOG(context->dialog)) == GTK_RESPONSE_HELP) { |
951 | map_item_deselect(appdata); |
952 | |
953 | relation_t *sel = get_selected_relation(context); |
954 | if(sel) map_relation_select(appdata, sel); |
955 | } |
956 | |
957 | gtk_widget_destroy(context->dialog); |
958 | g_free(context); |
959 | } |
960 | |
961 | |
962 | // vim:et:ts=8:sw=2:sts=2:ai |