X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/0df66d57a7784625fcb5f66147c45ed7450f2592..6339640601c48c559aed48a573946fc25785a6e9:/src/vikaggregatelayer.c diff --git a/src/vikaggregatelayer.c b/src/vikaggregatelayer.c index 1487ab61..d86bd0ec 100644 --- a/src/vikaggregatelayer.c +++ b/src/vikaggregatelayer.c @@ -20,9 +20,10 @@ */ #include "viking.h" -#include "vikaggregatelayer_pixmap.h" +#include "icons/icons.h" #include +#include #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val) @@ -31,9 +32,13 @@ static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, Vi static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode ); static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path ); +static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp ); + VikLayerInterface vik_aggregate_layer_interface = { "Aggregate", - &aggregatelayer_pixbuf, + N_("Aggregate"), + "A", + &vikaggregatelayer_pixbuf, NULL, 0, @@ -57,11 +62,14 @@ VikLayerInterface vik_aggregate_layer_interface = { (VikLayerFuncSetMenuItemsSelection) NULL, (VikLayerFuncGetMenuItemsSelection) NULL, - (VikLayerFuncAddMenuItems) NULL, + (VikLayerFuncAddMenuItems) aggregate_layer_add_menu_items, (VikLayerFuncSublayerAddMenuItems) NULL, (VikLayerFuncSublayerRenameRequest) NULL, (VikLayerFuncSublayerToggleVisible) NULL, + (VikLayerFuncSublayerTooltip) NULL, + (VikLayerFuncLayerTooltip) NULL, + (VikLayerFuncLayerSelected) NULL, (VikLayerFuncMarshall) aggregate_layer_marshall, (VikLayerFuncUnmarshall) aggregate_layer_unmarshall, @@ -73,10 +81,16 @@ VikLayerInterface vik_aggregate_layer_interface = { (VikLayerFuncWriteFileData) NULL, (VikLayerFuncDeleteItem) NULL, + (VikLayerFuncCutItem) NULL, (VikLayerFuncCopyItem) NULL, (VikLayerFuncPasteItem) NULL, (VikLayerFuncFreeCopiedItem) NULL, (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request, + + (VikLayerFuncSelectClick) NULL, + (VikLayerFuncSelectMove) NULL, + (VikLayerFuncSelectRelease) NULL, + (VikLayerFuncSelectedViewportMenu) NULL, }; struct _VikAggregateLayer { @@ -178,7 +192,7 @@ static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, Vi VikAggregateLayer *vik_aggregate_layer_new () { VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) ); - vik_layer_init ( VIK_LAYER(val), VIK_LAYER_AGGREGATE ); + vik_layer_set_type ( VIK_LAYER(val), VIK_LAYER_AGGREGATE ); val->children = NULL; return val; } @@ -186,52 +200,89 @@ VikAggregateLayer *vik_aggregate_layer_new () void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter ) { GtkTreeIter iter; + VikLayer *vl = VIK_LAYER(val); + + // By default layers are inserted above the selected layer + gboolean put_above = TRUE; + + // These types are 'base' types in that you what other information on top + if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF ) + put_above = FALSE; - if ( VIK_LAYER(val)->realized ) + if ( vl->realized ) { - vik_treeview_insert_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type, replace_iter ); + vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, replace_iter ); if ( ! l->visible ) - vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); - vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter ); + vik_treeview_item_set_visible ( vl->vt, &iter, FALSE ); + vik_layer_realize ( l, vl->vt, &iter ); if ( val->children == NULL ) - vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) ); + vik_treeview_expand ( vl->vt, &(vl->iter) ); } if (replace_iter) { - GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, replace_iter ) ); - val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 ); + GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) ); + if ( put_above ) + val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 ); + else + // Thus insert 'here' (so don't add 1) + val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone) ); } else { - val->children = g_list_append ( val->children, l ); + // Effectively insert at 'end' of the list to match how displayed in the treeview + // - but since it is drawn from 'bottom first' it is actually the first in the child list + // This ordering is especially important if it is a map or similar type, + // which needs be drawn first for the layering draw method to work properly. + // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer + val->children = g_list_prepend ( val->children, l ); } g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val ); } -void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l ) +/** + * vik_aggregate_layer_add_layer: + * @allow_reordering: should be set for GUI interactions, + * whereas loading from a file needs strict ordering and so should be FALSE + */ +void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l, gboolean allow_reordering ) { GtkTreeIter iter; + VikLayer *vl = VIK_LAYER(val); + + // By default layers go to the top + gboolean put_above = TRUE; - if ( VIK_LAYER(val)->realized ) + if ( allow_reordering ) { + // These types are 'base' types in that you what other information on top + if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF ) + put_above = FALSE; + } + + if ( vl->realized ) { - vik_treeview_add_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type); + vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type); if ( ! l->visible ) - vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); - vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter ); + vik_treeview_item_set_visible ( vl->vt, &iter, FALSE ); + vik_layer_realize ( l, vl->vt, &iter ); if ( val->children == NULL ) - vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) ); + vik_treeview_expand ( vl->vt, &(vl->iter) ); } - val->children = g_list_append ( val->children, l ); + if ( put_above ) + val->children = g_list_append ( val->children, l ); + else + val->children = g_list_prepend ( val->children, l ); + g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val ); } void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up ) { GList *theone, *first, *second; - vik_treeview_move_item ( VIK_LAYER(val)->vt, child_iter, up ); + VikLayer *vl = VIK_LAYER(val); + vik_treeview_move_item ( vl->vt, child_iter, up ); - theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, child_iter ) ); + theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) ); g_assert ( theone != NULL ); @@ -274,23 +325,23 @@ void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child * of the pixmap before drawing the trigger layer so we can use it again * later. */ -void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data ) +void vik_aggregate_layer_draw ( VikAggregateLayer *val, VikViewport *vp ) { GList *iter = val->children; VikLayer *vl; - VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( VIK_VIEWPORT(data) )); + VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( vp )); while ( iter ) { vl = VIK_LAYER(iter->data); if ( vl == trigger ) { - if ( vik_viewport_get_half_drawn ( VIK_VIEWPORT(data) ) ) { - vik_viewport_set_half_drawn ( VIK_VIEWPORT(data), FALSE ); - vik_viewport_snapshot_load( VIK_VIEWPORT(data) ); + if ( vik_viewport_get_half_drawn ( vp ) ) { + vik_viewport_set_half_drawn ( vp, FALSE ); + vik_viewport_snapshot_load( vp ); } else { - vik_viewport_snapshot_save( VIK_VIEWPORT(data) ); + vik_viewport_snapshot_save( vp ); } } - if ( vl->type == VIK_LAYER_AGGREGATE || ! vik_viewport_get_half_drawn( VIK_VIEWPORT(data) ) ) - vik_layer_draw ( vl, data ); + if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( vp ) ) + vik_layer_draw ( vl, vp ); iter = iter->next; } } @@ -305,6 +356,146 @@ static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoord } } +static void aggregate_layer_child_visibile_toggle ( gpointer data[2] ) +{ + // Convert data back to correct types + VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] ); + VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] ); + VikLayer *vl; + + // Loop around all (child) layers applying visibility setting + // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held + GList *iter = val->children; + while ( iter ) { + vl = VIK_LAYER ( iter->data ); + vl->visible = !vl->visible; + // Also set checkbox on/off + vik_treeview_item_toggle_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter) ); + iter = iter->next; + } + // Redraw as view may have changed + vik_layer_emit_update ( VIK_LAYER ( val ) ); +} + +static void aggregate_layer_child_visibile ( gpointer data[2], gboolean on_off) +{ + // Convert data back to correct types + VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] ); + VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] ); + VikLayer *vl; + + // Loop around all (child) layers applying visibility setting + // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held + GList *iter = val->children; + while ( iter ) { + vl = VIK_LAYER ( iter->data ); + vl->visible = on_off; + // Also set checkbox on_off + vik_treeview_item_set_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter), on_off ); + iter = iter->next; + } + // Redraw as view may have changed + vik_layer_emit_update ( VIK_LAYER ( val ) ); +} + +static void aggregate_layer_child_visibile_on ( gpointer data[2] ) +{ + aggregate_layer_child_visibile ( data, TRUE ); +} + +static void aggregate_layer_child_visibile_off ( gpointer data[2] ) +{ + aggregate_layer_child_visibile ( data, FALSE ); +} + +/** + * If order is true sort ascending, otherwise a descending sort + */ +static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order ) +{ + VikLayer *sa = (VikLayer *)a; + VikLayer *sb = (VikLayer *)b; + + // Default ascending order + gint answer = g_strcmp0 ( sa->name, sb->name ); + + if ( GPOINTER_TO_INT(order) ) { + // Invert sort order for ascending order + answer = -answer; + } + + return answer; +} + +static void aggregate_layer_sort_a2z ( gpointer lav[2] ) +{ + VikAggregateLayer *val = VIK_AGGREGATE_LAYER(lav[0]); + vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_ASCENDING ); + val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(TRUE) ); +} + +static void aggregate_layer_sort_z2a ( gpointer lav[2] ) +{ + VikAggregateLayer *val = VIK_AGGREGATE_LAYER(lav[0]); + vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_DESCENDING ); + val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(FALSE) ); +} + +static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp ) +{ + // Data to pass on in menu functions + static gpointer data[2]; + data[0] = val; + data[1] = vlp; + + GtkWidget *item = gtk_menu_item_new(); + gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item ); + gtk_widget_show ( item ); + + GtkWidget *vis_submenu = gtk_menu_new (); + item = gtk_menu_item_new_with_mnemonic ( _("_Visibility") ); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + gtk_widget_show ( item ); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), vis_submenu ); + + item = gtk_image_menu_item_new_with_mnemonic ( _("_Show All") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU) ); + g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visibile_on), data ); + gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item); + gtk_widget_show ( item ); + + item = gtk_image_menu_item_new_with_mnemonic ( _("_Hide All") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU) ); + g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visibile_off), data ); + gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item); + gtk_widget_show ( item ); + + item = gtk_image_menu_item_new_with_mnemonic ( _("_Toggle") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) ); + g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visibile_toggle), data ); + gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item); + gtk_widget_show ( item ); + + GtkWidget *submenu_sort = gtk_menu_new (); + item = gtk_image_menu_item_new_with_mnemonic ( _("_Sort") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) ); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + gtk_widget_show ( item ); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu_sort ); + + item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Ascending") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) ); + g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_a2z), data ); + gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item ); + gtk_widget_show ( item ); + + item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Descending") ); + gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) ); + g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_z2a), data ); + gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item ); + gtk_widget_show ( item ); +} + static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val ) { g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1); @@ -347,7 +538,7 @@ gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter #if 0 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */ -guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp ) +guint vik_aggregate_layer_tool ( VikAggregateLayer *val, VikLayerTypeEnum layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp ) { GList *iter = val->children; gboolean found_rej = FALSE; @@ -382,7 +573,7 @@ guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, Vik } #endif -VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type ) +VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, VikLayerTypeEnum type ) { VikLayer *rv; GList *ls = val->children; @@ -393,11 +584,12 @@ VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer while ( ls ) { - if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == type ) - return VIK_LAYER(ls->data); - else if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == VIK_LAYER_AGGREGATE ) + VikLayer *vl = VIK_LAYER(ls->data); + if ( vl->visible && vl->type == type ) + return vl; + else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE ) { - rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(ls->data), type); + rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type); if ( rv ) return rv; } @@ -406,17 +598,48 @@ VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer return NULL; } -GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, gint type) +GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, VikLayerTypeEnum type, gboolean include_invisible) { GList *l = layers; GList *children = val->children; + VikLayer *vl; if (!children) return layers; + + // Where appropriate *don't* include non-visible layers while (children) { - if (VIK_LAYER(children->data)->type == VIK_LAYER_AGGREGATE) - l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type); - else if (VIK_LAYER(children->data)->type == type) - l = g_list_prepend(l, children->data); /* now in top down order */ + vl = VIK_LAYER(children->data); + if (vl->type == VIK_LAYER_AGGREGATE ) { + // Don't even consider invisible aggregrates, unless told to + if (vl->visible || include_invisible) + l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible); + } + else if (vl->type == type) { + if (vl->visible || include_invisible) + l = g_list_prepend(l, children->data); /* now in top down order */ + } + else if (type == VIK_LAYER_TRW) { + /* GPS layers contain TRW layers. cf with usage in file.c */ + if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) { + if (VIK_LAYER(children->data)->visible || include_invisible) { + if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) { + /* + can not use g_list_concat due to wrong copy method - crashes if used a couple times !! + l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data))); + */ + /* create own copy method instead :( */ + GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)); + int n_layers = g_list_length (gps_trw_layers); + int layer = 0; + for ( layer = 0; layer < n_layers; layer++) { + l = g_list_prepend(l, gps_trw_layers->data); + gps_trw_layers = gps_trw_layers->next; + } + g_list_free(gps_trw_layers); + } + } + } + } children = children->next; } return l; @@ -426,13 +649,16 @@ void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkT { GList *i = val->children; GtkTreeIter iter; + VikLayer *vl = VIK_LAYER(val); + VikLayer *vli; while ( i ) { - vik_treeview_add_layer ( VIK_LAYER(val)->vt, layer_iter, &iter, VIK_LAYER(i->data)->name, val, - VIK_LAYER(i->data), VIK_LAYER(i->data)->type, VIK_LAYER(i->data)->type ); - if ( ! VIK_LAYER(i->data)->visible ) - vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); - vik_layer_realize ( VIK_LAYER(i->data), VIK_LAYER(val)->vt, &iter ); + vli = VIK_LAYER(i->data); + vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val, TRUE, + vli, vli->type, vli->type ); + if ( ! vli->visible ) + vik_treeview_item_set_visible ( vl->vt, &iter, FALSE ); + vik_layer_realize ( vli, vl->vt, &iter ); i = i->next; } }