LayerSelector: Make import of layer information from CSV independed from mapping-parser code. Use LayerSettings class instead.

This commit is contained in:
Mario Hüttel 2019-09-25 17:55:59 +02:00
parent f20826ccf7
commit c365c89908

View File

@ -577,6 +577,7 @@ void layer_selector_generate_layer_widgets(LayerSelector *selector, GList *libs)
static LayerElement *layer_selector_find_layer_element_in_list(GList *el_list, int layer) static LayerElement *layer_selector_find_layer_element_in_list(GList *el_list, int layer)
{ {
LayerElement *ret = NULL; LayerElement *ret = NULL;
for (; el_list != NULL; el_list = el_list->next) { for (; el_list != NULL; el_list = el_list->next) {
if (layer_element_get_layer(LAYER_ELEMENT(el_list->data)) == layer) { if (layer_element_get_layer(LAYER_ELEMENT(el_list->data)) == layer) {
ret = LAYER_ELEMENT(el_list->data); ret = LAYER_ELEMENT(el_list->data);
@ -597,19 +598,18 @@ static LayerElement *layer_selector_find_layer_element_in_list(GList *el_list, i
* @param self LayerSelector instance * @param self LayerSelector instance
* @param file_name File name to load from * @param file_name File name to load from
*/ */
static void layer_selector_load_layer_mapping_from_file(LayerSelector *self, gchar *file_name) static void layer_selector_load_layer_mapping_from_file(LayerSelector *self, const gchar *file_name)
{ {
GFile *file; GFile *file;
GFileInputStream *stream; GFileInputStream *stream;
GDataInputStream *dstream; GDataInputStream *dstream;
LayerElement *le; LayerElement *le;
char *name;
gboolean export;
int layer;
GdkRGBA color;
int result;
GList *rows; GList *rows;
GList *temp; GList *temp;
GList *layer_infos;
int status;
LayerSettings *layer_settings;
struct layer_info *linfo;
file = g_file_new_for_path(file_name); file = g_file_new_for_path(file_name);
stream = g_file_read(file, NULL, NULL); stream = g_file_read(file, NULL, NULL);
@ -624,31 +624,40 @@ static void layer_selector_load_layer_mapping_from_file(LayerSelector *self, gch
/* Reference and remove all rows from box */ /* Reference and remove all rows from box */
for (temp = rows; temp != NULL; temp = temp->next) { for (temp = rows; temp != NULL; temp = temp->next) {
le = LAYER_ELEMENT(temp->data); le = LAYER_ELEMENT(temp->data);
/* Referencing protets the widget from being deleted when removed */ /* Referencing protects the widget from being deleted when removed */
g_object_ref(G_OBJECT(le)); g_object_ref(G_OBJECT(le));
gtk_container_remove(GTK_CONTAINER(self->list_box), GTK_WIDGET(le)); gtk_container_remove(GTK_CONTAINER(self->list_box), GTK_WIDGET(le));
} }
while((result = mapping_parser_load_line(dstream, &export, &name, &layer, &color)) >= 0) { /* Load Layer settings. No need to check pointer, will be checked by load csv func. */
/* skip broken line */ layer_settings = layer_settings_new();
if (result == 1)
status = layer_settings_load_from_csv(layer_settings, file_name);
if (status)
goto abort_layer_settings;
layer_infos = layer_settings_get_layer_info_list(layer_settings);
if (!layer_infos)
goto abort_layer_settings;
/* Loop over all layer infos read from the CSV file */
for (; layer_infos; layer_infos = g_list_next(layer_infos)) {
linfo = (struct layer_info *)layer_infos->data;
le = layer_selector_find_layer_element_in_list(rows, linfo->layer);
if (!le)
continue; continue;
/* Add rows in the same order as in file */ layer_element_set_name(le, linfo->name);
if ((le = layer_selector_find_layer_element_in_list(rows, layer))) { layer_element_set_export(le, (linfo->render ? TRUE : FALSE));
gtk_list_box_insert(self->list_box, GTK_WIDGET(le), -1); layer_element_set_color(le, &linfo->color);
gtk_container_add(GTK_CONTAINER(self->list_box), GTK_WIDGET(le));
layer_element_set_color(le, &color); rows = g_list_remove(rows, le);
layer_element_set_export(le, export);
layer_element_set_name(le, name);
g_free(name);
/* Dereference and remove from list */
g_object_unref(G_OBJECT(le));
rows = g_list_remove(rows, le);
}
} }
abort_layer_settings:
/* Destroy layer settings. Not needed for adding remaining elements */
g_object_unref(layer_settings);
/* Add remaining elements */ /* Add remaining elements */
for (temp = rows; temp != NULL; temp = temp->next) { for (temp = rows; temp != NULL; temp = temp->next) {
le = LAYER_ELEMENT(temp->data); le = LAYER_ELEMENT(temp->data);