From 9f7e1e169644708941f18b7f462ffc1bb362f21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Thu, 29 Aug 2019 18:19:28 +0200 Subject: [PATCH] Issue #26: Add layer_selector_auto_name_layers() function --- include/gds-render/layer/layer-selector.h | 11 +++++++++ layer/layer-selector.c | 30 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/gds-render/layer/layer-selector.h b/include/gds-render/layer/layer-selector.h index a5dfa9b..2a8811d 100644 --- a/include/gds-render/layer/layer-selector.h +++ b/include/gds-render/layer/layer-selector.h @@ -107,6 +107,17 @@ void layer_selector_select_all_layers(LayerSelector *layer_selector, gboolean se */ void layer_selector_auto_color_layers(LayerSelector *layer_selector, ColorPalette *palette, double global_alpha); +/** + * @brief Auto name all layers in the layer selector. + * + * This functions sets the name of the layer equal to its number. + * The \p overwrite parameter specifies if already set layer names are overwritten. + * + * @param layer_selector LayerSelector + * @param overwrite Overwrite existing layer names + */ +void layer_selector_auto_name_layers(LayerSelector *layer_selector, gboolean overwrite); + G_END_DECLS #endif /* __LAYER_SELECTOR_H__ */ diff --git a/layer/layer-selector.c b/layer/layer-selector.c index 372338d..1ad0976 100644 --- a/layer/layer-selector.c +++ b/layer/layer-selector.c @@ -857,4 +857,34 @@ ret_free_le_list: g_list_free(le_list); } +void layer_selector_auto_name_layers(LayerSelector *layer_selector, gboolean overwrite) +{ + GList *le_list; + GList *le_list_ptr; + LayerElement *le; + const char *old_layer_name; + GString *new_layer_name; + + g_return_if_fail(LAYER_IS_SELECTOR(layer_selector)); + + new_layer_name = g_string_new_len(NULL, 10); + le_list = gtk_container_get_children(GTK_CONTAINER(layer_selector->list_box)); + + for (le_list_ptr = le_list; le_list_ptr != NULL; le_list_ptr = g_list_next(le_list_ptr)) { + le = LAYER_ELEMENT(le_list_ptr->data); + if (!le) + continue; + old_layer_name = layer_element_get_name(le); + + /* Check if layer name is empty or may be overwritten */ + if (!old_layer_name || *old_layer_name == '\0' || overwrite) { + g_string_printf(new_layer_name, "Layer %d", layer_element_get_layer(le)); + layer_element_set_name(le, new_layer_name->str); + } + } + + g_string_free(new_layer_name, TRUE); + g_list_free(le_list); +} + /** @} */