Skip to content

Commit

Permalink
Github #88: Make Waypoint Properties dialog reusable.
Browse files Browse the repository at this point in the history
Make it behave more like the trackpoint properties dialog,
 so that selecting another waypoint is possible without having to close the dialog.

And an apply button so changes can be made without closing the dialog.

Ensure the dialog is removed when the waypoint or layer is deleted.
  • Loading branch information
rnorris committed May 28, 2020
1 parent 86ac4bb commit 49c5660
Show file tree
Hide file tree
Showing 7 changed files with 639 additions and 430 deletions.
13 changes: 13 additions & 0 deletions src/ui_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ GtkWidget *ui_entry_new ( const gchar *str, GtkEntryIconPosition position )
return entry;
}

/**
* Set the entry field text
*
* Handles NULL, unlike gtk_entry_set_text() which complains
*/
void ui_entry_set_text ( GtkWidget *widget, const gchar *str )
{
if ( str )
gtk_entry_set_text ( GTK_ENTRY(widget), str );
else
gtk_entry_set_text ( GTK_ENTRY(widget), "" );
}

/**
* Create a spinbutton with an icon to clear the entry
*
Expand Down
1 change: 1 addition & 0 deletions src/ui_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name);
GtkWidget* ui_label_new_selectable ( const gchar* text );
GtkWidget *ui_entry_new ( const gchar *str, GtkEntryIconPosition position );
void ui_entry_set_text ( GtkWidget *widget, const gchar *str );
GtkWidget *ui_spin_button_new ( GtkAdjustment *adjustment,
gdouble climb_rate,
guint digits );
Expand Down
116 changes: 78 additions & 38 deletions src/viktrwlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ struct _VikTrwLayer {

VikCoordMode coord_mode;

VikTrwLayerWpwin *wpwin;
VikWaypoint *wpwin_wpt; // Similar to current_wp

/* wp editing tool */
VikWaypoint *current_wp;
gpointer current_wp_id;
Expand Down Expand Up @@ -1791,9 +1794,7 @@ static VikTrwLayer *trw_layer_unmarshall ( const guint8 *data_in, guint len, Vik
}
if ( pl == VIK_TRW_LAYER_SUBLAYER_WAYPOINT ) {
VikWaypoint *wp = vik_waypoint_unmarshall ( data + sizeof_len_and_subtype, 0 );
gchar *name = g_strdup ( wp->name );
vik_trw_layer_add_waypoint ( vtl, name, wp );
g_free ( name );
vik_trw_layer_add_waypoint ( vtl, NULL, wp );
waypoint_convert (NULL, wp, &vtl->coord_mode);
}
if ( pl == VIK_TRW_LAYER_SUBLAYER_ROUTE ) {
Expand Down Expand Up @@ -1938,6 +1939,9 @@ static void trw_layer_free ( VikTrwLayer *trwlayer )
if ( trwlayer->tpwin != NULL )
gtk_widget_destroy ( GTK_WIDGET(trwlayer->tpwin) );

if ( trwlayer->wpwin != NULL )
vik_trw_layer_wpwin_destroy ( trwlayer->wpwin );

if ( trwlayer->tracks_analysis_dialog != NULL )
gtk_widget_destroy ( GTK_WIDGET(trwlayer->tracks_analysis_dialog) );

Expand Down Expand Up @@ -4009,21 +4013,17 @@ gboolean vik_trw_layer_new_waypoint ( VikTrwLayer *vtl, GtkWindow *w, const VikC
{
gchar *default_name = highest_wp_number_get(vtl);
VikWaypoint *wp = vik_waypoint_new();
gchar *returned_name;
gboolean updated;
wp->coord = *def_coord;

// Attempt to auto set height if DEM data is available
vik_waypoint_apply_dem_data ( wp, TRUE );

returned_name = a_dialog_waypoint ( w, default_name, vtl, wp, vtl->coord_mode, TRUE, &updated );
gboolean is_created = GPOINTER_TO_UINT(vik_trw_layer_wpwin_show ( w, NULL, default_name, vtl, wp, vtl->coord_mode, TRUE ));

if ( returned_name )
{
if ( is_created ) {
wp->visible = TRUE;
vik_trw_layer_add_waypoint ( vtl, returned_name, wp );
vik_trw_layer_add_waypoint ( vtl, NULL, wp );
g_free (default_name);
g_free (returned_name);
return TRUE;
}
g_free (default_name);
Expand Down Expand Up @@ -4648,11 +4648,17 @@ static void trw_layer_add_menu_items ( VikTrwLayer *vtl, GtkMenu *menu, gpointer
// Fake Waypoint UUIDs vi simple increasing integer
static guint wp_uuid = 0;

/**
* vik_trw_layer_add_waypoint:
* @name: New name for the waypoint, maybe NULL.
* If NULL then the wp must already have a name
*/
void vik_trw_layer_add_waypoint ( VikTrwLayer *vtl, gchar *name, VikWaypoint *wp )
{
wp_uuid++;

vik_waypoint_set_name (wp, name);
if ( name )
vik_waypoint_set_name (wp, name);

if ( VIK_LAYER(vtl)->realized )
{
Expand All @@ -4668,7 +4674,7 @@ void vik_trw_layer_add_waypoint ( VikTrwLayer *vtl, gchar *name, VikWaypoint *wp
timestamp = wp->timestamp;

// Visibility column always needed for waypoints
vik_treeview_add_sublayer ( VIK_LAYER(vtl)->vt, &(vtl->waypoints_iter), iter, name, vtl, GUINT_TO_POINTER(wp_uuid), VIK_TRW_LAYER_SUBLAYER_WAYPOINT, get_wp_sym_small (wp->symbol), TRUE, timestamp, 0 );
vik_treeview_add_sublayer ( VIK_LAYER(vtl)->vt, &(vtl->waypoints_iter), iter, wp->name, vtl, GUINT_TO_POINTER(wp_uuid), VIK_TRW_LAYER_SUBLAYER_WAYPOINT, get_wp_sym_small (wp->symbol), TRUE, timestamp, 0 );

// Actual setting of visibility dependent on the waypoint
vik_treeview_item_set_visible ( VIK_LAYER(vtl)->vt, iter, wp->visible );
Expand All @@ -4679,7 +4685,7 @@ void vik_trw_layer_add_waypoint ( VikTrwLayer *vtl, gchar *name, VikWaypoint *wp
vik_treeview_sort_children ( VIK_LAYER(vtl)->vt, &(vtl->waypoints_iter), vtl->wp_sort_order );
}

highest_wp_number_add_wp(vtl, name);
highest_wp_number_add_wp(vtl, wp->name);
g_hash_table_insert ( vtl->waypoints, GUINT_TO_POINTER(wp_uuid), wp );

}
Expand Down Expand Up @@ -5094,6 +5100,11 @@ gboolean vik_trw_layer_delete_route ( VikTrwLayer *vtl, VikTrack *trk )

static void delete_waypoint_low_level ( VikTrwLayer *vtl, VikWaypoint *wp, gpointer uuid, GtkTreeIter *it )
{
if ( vtl->wpwin && wp == vtl->wpwin_wpt ) {
vik_trw_layer_wpwin_destroy ( vtl->wpwin );
vtl->wpwin = NULL;
}

vik_treeview_item_delete ( VIK_LAYER(vtl)->vt, it );
g_hash_table_remove ( vtl->waypoints_iters, uuid );

Expand All @@ -5107,12 +5118,6 @@ static gboolean trw_layer_delete_waypoint ( VikTrwLayer *vtl, VikWaypoint *wp )

if ( wp && wp->name ) {

if ( wp == vtl->current_wp ) {
vtl->current_wp = NULL;
vtl->current_wp_id = NULL;
vtl->moving_wp = FALSE;
}

was_visible = wp->visible;

wpu_udata udata;
Expand All @@ -5128,6 +5133,12 @@ static gboolean trw_layer_delete_waypoint ( VikTrwLayer *vtl, VikWaypoint *wp )
if ( it ) {
delete_waypoint_low_level ( vtl, wp, udata.uuid, it );

if ( wp == vtl->current_wp ) {
vtl->current_wp = NULL;
vtl->current_wp_id = NULL;
vtl->moving_wp = FALSE;
}

// If last sublayer, then remove sublayer container
if ( g_hash_table_size (vtl->waypoints) == 0 ) {
vik_treeview_item_delete ( VIK_LAYER(vtl)->vt, &(vtl->waypoints_iter) );
Expand Down Expand Up @@ -5268,6 +5279,12 @@ void vik_trw_layer_delete_all_tracks ( VikTrwLayer *vtl )

void vik_trw_layer_delete_all_waypoints ( VikTrwLayer *vtl )
{
if ( vtl->wpwin ) {
vik_trw_layer_wpwin_destroy ( vtl->wpwin );
vtl->wpwin = NULL;
}
vtl->wpwin_wpt = NULL;

vtl->current_wp = NULL;
vtl->current_wp_id = NULL;
vtl->moving_wp = FALSE;
Expand Down Expand Up @@ -5439,31 +5456,48 @@ void trw_layer_waypoint_reset_icon ( VikTrwLayer *vtl, VikWaypoint *wp )
}
}

void trw_layer_waypoint_properties_changed ( VikTrwLayer *vtl, VikWaypoint *wp )
{
// Find in treeview
wpu_udata udataU;
udataU.wp = wp;
udataU.uuid = NULL;
gpointer wpf = g_hash_table_find ( vtl->waypoints, (GHRFunc)trw_layer_waypoint_find_uuid, &udataU );

if ( wpf && udataU.uuid ) {
GtkTreeIter *iter = g_hash_table_lookup ( vtl->waypoints_iters, udataU.uuid );
if ( iter ) {
// Update treeview data
vik_treeview_item_set_name ( VIK_LAYER(vtl)->vt, iter, wp->name );
vik_treeview_item_set_icon ( VIK_LAYER(vtl)->vt, iter, get_wp_sym_small (wp->symbol) );
vik_treeview_item_set_timestamp ( VIK_LAYER(vtl)->vt, iter, wp->timestamp );
vik_treeview_sort_children ( VIK_LAYER(vtl)->vt, &(vtl->waypoints_iter), vtl->wp_sort_order );
}
}
// Position may have changed
trw_layer_calculate_bounds_waypoints ( vtl );

if ( wp->visible && vik_treeview_item_get_visible_tree(VIK_LAYER(vtl)->vt, &(VIK_LAYER(vtl)->iter)) )
vik_layer_emit_update ( VIK_LAYER(vtl) );
}

void trw_layer_wpwin_set ( VikTrwLayer *vtl, VikWaypoint *wp, gpointer wpwin )
{
vtl->wpwin = wpwin;
vtl->wpwin_wpt = wp;
}

static void trw_layer_properties_item ( menu_array_sublayer values )
{
VikTrwLayer *vtl = VIK_TRW_LAYER(values[MA_VTL]);
if ( GPOINTER_TO_INT (values[MA_SUBTYPE]) == VIK_TRW_LAYER_SUBLAYER_WAYPOINT )
{
VikWaypoint *wp = g_hash_table_lookup ( vtl->waypoints, values[MA_SUBLAYER_ID] );

if ( wp && wp->name )
{
gboolean updated = FALSE;
gchar *new_name = a_dialog_waypoint ( VIK_GTK_WINDOW_FROM_LAYER(vtl), wp->name, vtl, wp, vtl->coord_mode, FALSE, &updated );
if ( new_name )
trw_layer_waypoint_rename ( vtl, wp, new_name );

if ( updated && values[MA_TV_ITER] )
vik_treeview_item_set_icon ( VIK_LAYER(vtl)->vt, values[MA_TV_ITER], get_wp_sym_small (wp->symbol) );

if ( updated && VIK_LAYER(vtl)->visible )
vik_layer_emit_update ( VIK_LAYER(vtl) );

// Time & Position could have changed
if ( updated ) {
trw_layer_calculate_bounds_waypoints ( vtl );
trw_layer_treeview_waypoint_align_time ( vtl, wp );
}
if ( wp && wp->name ) {
if ( vtl->wpwin )
vik_trw_layer_wpwin_destroy ( vtl->wpwin );
vtl->wpwin_wpt = wp;
vtl->wpwin = vik_trw_layer_wpwin_show ( VIK_GTK_WINDOW_FROM_LAYER(vtl), NULL, wp->name, vtl, wp, vtl->coord_mode, FALSE );
}
}
else
Expand Down Expand Up @@ -9582,6 +9616,12 @@ static gboolean trw_layer_select_click ( VikTrwLayer *vtl, GdkEventButton *event
}
}

// Change waypoint in dialog
if ( vtl->wpwin ) {
vtl->wpwin_wpt = vtl->current_wp;
vtl->wpwin = vik_trw_layer_wpwin_show ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vtl->wpwin, vtl->wpwin_wpt->name, vtl, vtl->wpwin_wpt, vtl->coord_mode, FALSE );
}

// Selection change only (no change to the layer)
vik_layer_redraw ( VIK_LAYER(vtl) );
return TRUE;
Expand Down
2 changes: 2 additions & 0 deletions src/viktrwlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ void trw_layer_waypoint_rename ( VikTrwLayer *vtl, VikWaypoint *wp, const gchar
void trw_layer_waypoint_reset_icon ( VikTrwLayer *vtl, VikWaypoint *wp );
void trw_layer_calculate_bounds_waypoints ( VikTrwLayer *vtl );
void trw_layer_calculate_bounds_tracks ( VikTrwLayer *vtl );
void trw_layer_waypoint_properties_changed ( VikTrwLayer *vtl, VikWaypoint *wp );
void trw_layer_wpwin_set ( VikTrwLayer *vtl, VikWaypoint *wp, gpointer wpwin );

gboolean vik_trw_layer_get_tracks_visibility ( VikTrwLayer *vtl );
gboolean vik_trw_layer_get_routes_visibility ( VikTrwLayer *vtl );
Expand Down
11 changes: 1 addition & 10 deletions src/viktrwlayer_waypointlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,7 @@ static void trw_layer_waypoint_properties ( menu_array_values values )
GtkWidget *gw = gtk_widget_get_toplevel ( values[MA_TREEVIEW] );
waypoint_close_cb ( gw, 0, values[MA_WPTS_LIST] );

gboolean updated = FALSE;
gchar *new_name = a_dialog_waypoint ( VIK_GTK_WINDOW_FROM_LAYER(vtl), wpt->name, vtl, wpt, vik_trw_layer_get_coord_mode(vtl), FALSE, &updated );
if ( new_name )
trw_layer_waypoint_rename ( vtl, wpt, new_name );

if ( updated )
trw_layer_waypoint_reset_icon ( vtl, wpt );

if ( updated && VIK_LAYER(vtl)->visible )
vik_layer_emit_update ( VIK_LAYER(vtl) );
trw_layer_wpwin_set ( vtl, wpt, vik_trw_layer_wpwin_show ( VIK_GTK_WINDOW_FROM_LAYER(vtl), NULL, wpt->name, vtl, wpt, vik_trw_layer_get_coord_mode(vtl), FALSE ) );
}
}

Expand Down
Loading

0 comments on commit 49c5660

Please sign in to comment.