From e8fbe25bcbf649c10b319366caaa7f59a18ae878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 18 May 2018 22:47:39 +0200 Subject: [PATCH] Improved Layer Selection * Removed Slider for opacity. Use Color Dialog instead * Implement Layer loading * remove Spinner from header bar --- glade/CMakeLists.txt | 4 +- glade/layer-widget.glade | 33 ++------ glade/main.glade | 12 --- glade/resources.xml | 2 +- layer-selector.c | 155 +++++++++++++++++++++++++++++++++-- layer-widget/layer-element.c | 16 +--- layer-widget/layer-element.h | 3 - 7 files changed, 163 insertions(+), 62 deletions(-) diff --git a/glade/CMakeLists.txt b/glade/CMakeLists.txt index 6570bad..231d42b 100644 --- a/glade/CMakeLists.txt +++ b/glade/CMakeLists.txt @@ -1,6 +1,8 @@ add_custom_target(glib-resources DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/resources.c) add_custom_command(DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/resources.xml + ${CMAKE_CURRENT_SOURCE_DIR}/resources.xml + ${CMAKE_CURRENT_SOURCE_DIR}/main.glade + ${CMAKE_CURRENT_SOURCE_DIR}/layer-widget.glade OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/resources.c COMMAND diff --git a/glade/layer-widget.glade b/glade/layer-widget.glade index 3bf8012..3773863 100644 --- a/glade/layer-widget.glade +++ b/glade/layer-widget.glade @@ -3,10 +3,10 @@ - 100 - 100 - 1 - 10 + 1 + 1 + 0.01 + 0.10000000000000001 True @@ -29,6 +29,9 @@ True True True + True + Select Layer Color and Opacity + rgb(0,0,0) False @@ -36,24 +39,6 @@ 1 - - - 150 - True - True - adjustment1 - False - 100 - 0 - 0 - right - - - False - True - 2 - - Export Layer @@ -65,7 +50,7 @@ False True - 3 + 2 @@ -76,7 +61,7 @@ True True - 4 + 3 diff --git a/glade/main.glade b/glade/main.glade index 63eac62..4f0d1ca 100644 --- a/glade/main.glade +++ b/glade/main.glade @@ -36,18 +36,6 @@ 0 - - - True - False - True - - - False - True - 1 - - Load Mapping diff --git a/glade/resources.xml b/glade/resources.xml index 3603dd0..b41d176 100644 --- a/glade/resources.xml +++ b/glade/resources.xml @@ -1,6 +1,6 @@ - + main.glade layer-widget.glade diff --git a/layer-selector.c b/layer-selector.c index 6029874..f9f3480 100644 --- a/layer-selector.c +++ b/layer-selector.c @@ -21,6 +21,7 @@ #include "gdsparse.h" #include #include +#include static GList *layer_widgets = NULL; static GtkWidget *load_button; @@ -148,8 +149,152 @@ void generate_layer_widgets(GtkListBox *listbox, GList *libs) gtk_widget_set_sensitive(save_button, TRUE); } +/** + * @brief load_csv_line + * @param file + * @param export + * @param name + * @param layer + * @param color + * @param opacity + * @return 0 if succesfull, 1 if line was malformatted or parameters are broken, -1 if file end + */ +static int load_csv_line(GDataInputStream *stream, gboolean *export, char **name, int *layer, GdkRGBA *color) +{ + int ret; + gsize len; + gchar *line; + GRegex *regex; + GMatchInfo *mi; + char *match; + + if ((!export) || (!name) || (!layer) || (!color)) { + ret = 1; + goto ret_direct; + } + + regex = g_regex_new("^(?[0-9]+),(?[0-9\\.]+),(?[0-9\\.]+),(?[0-9\\.]+),(?[0-9\\.]+),(?[01]),(?.*)$", 0, 0, NULL); + + line = g_data_input_stream_read_line(stream, &len, NULL, NULL); + if (!line) { + ret = -1; + goto destroy_regex; + } + + /* Match line in CSV */ + g_regex_match(regex, line, 0, &mi); + if (g_match_info_matches(mi)) { + /* Line is valid */ + match = g_match_info_fetch_named(mi, "layer"); + *layer = g_ascii_strtoll(match, NULL, 10); + g_free(match); + match = g_match_info_fetch_named(mi, "r"); + color->red = g_ascii_strtod(match, NULL); + g_free(match); + match = g_match_info_fetch_named(mi, "g"); + color->green = g_ascii_strtod(match, NULL); + g_free(match); + match = g_match_info_fetch_named(mi, "b"); + color->blue = g_ascii_strtod(match, NULL); + g_free(match); + match = g_match_info_fetch_named(mi, "a"); + color->alpha = g_ascii_strtod(match, NULL); + g_free(match); + match = g_match_info_fetch_named(mi, "export"); + *export = ((!strcmp(match, "1")) ? TRUE : FALSE); + g_free(match); + match = g_match_info_fetch_named(mi, "name"); + *name = match; + + ret = 0; + } else { + /* Line is malformatted */ + printf("Could not recognize line in CSV as valid entry: %s\n", line); + ret = 1; + } + + g_match_info_free(mi); + g_free(line); +destroy_regex: + g_regex_unref(regex); +ret_direct: + return ret; + +} + +static LayerElement *find_layer_in_list(GList *list, int layer) +{ + LayerElement *le_found, *temp; + + for (le_found = NULL; list != NULL; list = list->next) { + temp = LAYER_ELEMENT(list->data); + if (layer_element_get_layer(temp) == layer) { + le_found = temp; + break; + } + } + + return le_found; +} + + +static void load_layer_mapping_from_file(gchar *file_name) +{ + GFile *file; + GFileInputStream *stream; + GDataInputStream *dstream; + + LayerElement *le; + char *name; + gboolean export; + int layer; + GdkRGBA color; + int result; + + file = g_file_new_for_path(file_name); + stream = g_file_read(file, NULL, NULL); + + if (!stream) + goto destroy_file; + + dstream = g_data_input_stream_new(G_INPUT_STREAM(stream)); + + while((result = load_csv_line(dstream, &export, &name, &layer, &color)) >= 0) { + /* skip broken line */ + if (result == 1) + continue; + + /* search for layer widget */ + if (le = find_layer_in_list(layer_widgets, layer)) { + layer_element_set_name(le, name); + layer_element_set_color(le, &color); + layer_element_set_export(le, export); + } + g_free(name); + } + + /* read line */ + g_object_unref(dstream); + g_object_unref(stream); +destroy_file: + g_object_unref(file); +} + static void load_mapping_clicked(GtkWidget *button, gpointer user_data) { + GtkWidget *dialog; + gint res; + gchar *file_name; + + dialog = gtk_file_chooser_dialog_new("Load Mapping File", GTK_WINDOW(user_data), GTK_FILE_CHOOSER_ACTION_OPEN, + "Cancel", GTK_RESPONSE_CANCEL, "Load Mapping", GTK_RESPONSE_ACCEPT, NULL); + res = gtk_dialog_run(GTK_DIALOG(dialog)); + if (res == GTK_RESPONSE_ACCEPT) { + file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + load_layer_mapping_from_file(file_name); + g_free(file_name); + } + gtk_widget_destroy(dialog); } static void create_csv_line(LayerElement *layer_element, char *line_buffer, size_t max_len) @@ -159,7 +304,6 @@ static void create_csv_line(LayerElement *layer_element, char *line_buffer, size const gchar *name; int layer; GdkRGBA color; - int opacity; string = g_string_new_len(NULL, max_len-1); @@ -167,12 +311,11 @@ static void create_csv_line(LayerElement *layer_element, char *line_buffer, size export = layer_element_get_export(layer_element); name = (const gchar*)layer_element_get_name(layer_element); layer = layer_element_get_layer(layer_element); - opacity = layer_element_get_opacity(layer_element); layer_element_get_color(layer_element, &color); /* print values to line */ - g_string_printf(string, "%d,%lf,%lf,%lf,%d,%d,%s\n", layer, color.red, color.green, - color.blue, opacity, (export == TRUE ? 1 : 0), name); + g_string_printf(string, "%d,%lf,%lf,%lf,%lf,%d,%s\n", layer, color.red, color.green, + color.blue, color.alpha, (export == TRUE ? 1 : 0), name); if (string->len > (max_len-1)) { printf("Layer Definition too long. Please shorten Layer Name!!\n"); @@ -197,7 +340,7 @@ static void save_layer_mapping_data(const gchar *file_name) /* Overwrite existing file */ file = fopen((const char *)file_name, "w"); - /* File format is CSV: ,,,,,, */ + /* File format is CSV: ,,,,,, */ for (le_list = layer_widgets; le_list != NULL; le_list = le_list->next) { /* To be sure it is a valid string */ workbuff[0] = 0; @@ -217,7 +360,7 @@ static void save_mapping_clicked(GtkWidget *button, gpointer user_data) gchar *file_name; dialog = gtk_file_chooser_dialog_new("Save Mapping File", GTK_WINDOW(user_data), GTK_FILE_CHOOSER_ACTION_SAVE, - "Cancel", GTK_RESPONSE_CANCEL, "Save Mapping", GTK_RESPONSE_ACCEPT, NULL); + "Cancel", GTK_RESPONSE_CANCEL, "Save Mapping", GTK_RESPONSE_ACCEPT, NULL); res = gtk_dialog_run(GTK_DIALOG(dialog)); if (res == GTK_RESPONSE_ACCEPT) { file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); diff --git a/layer-widget/layer-element.c b/layer-widget/layer-element.c index 9d7ce8a..9e530ba 100644 --- a/layer-widget/layer-element.c +++ b/layer-widget/layer-element.c @@ -32,7 +32,6 @@ static void layer_element_class_init(LayerElementClass *klass) { GObjectClass *oclass = G_OBJECT_CLASS(klass); oclass->dispose = layer_element_dispose; - return; } static void layer_element_init(LayerElement *self) @@ -48,7 +47,6 @@ static void layer_element_init(LayerElement *self) self->priv.export = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "export")); self->priv.layer = GTK_LABEL(gtk_builder_get_object(builder, "layer")); self->priv.name = GTK_ENTRY(gtk_builder_get_object(builder, "entry")); - self->priv.opacity = GTK_SCALE(gtk_builder_get_object(builder, "opacity")); /* Connect signals */ /* None */ @@ -76,7 +74,7 @@ void layer_element_set_layer(LayerElement *elem, int layer) GString *string; string = g_string_new_len(NULL, 100); - g_string_printf(string, "Layer: %d", layer); + g_string_printf(string, "Layer: %3d", layer); gtk_label_set_text(elem->priv.layer, (const gchar *)string->str); elem->priv.layer_num = layer; g_string_free(string, TRUE); @@ -87,18 +85,6 @@ int layer_element_get_layer(LayerElement *elem) return elem->priv.layer_num; } -void layer_element_set_opacity(LayerElement *elem, int opacity) -{ - if (opacity > 100 || opacity < 0) - return; - gtk_range_set_value(GTK_RANGE(elem->priv.opacity), (gdouble)opacity); -} - -int layer_element_get_opacity(LayerElement *elem) -{ - return (int)gtk_range_get_value(GTK_RANGE(elem->priv.opacity)); -} - void layer_element_set_export(LayerElement *elem, gboolean export) { gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(elem->priv.export), export); diff --git a/layer-widget/layer-element.h b/layer-widget/layer-element.h index 56de010..8c3e288 100644 --- a/layer-widget/layer-element.h +++ b/layer-widget/layer-element.h @@ -32,7 +32,6 @@ typedef struct _LayerElementPriv { GtkLabel *layer; int layer_num; GtkColorButton *color; - GtkScale *opacity; GtkCheckButton *export; } LayerElementPriv; @@ -55,8 +54,6 @@ const char *layer_element_get_name(LayerElement *elem); void layer_element_set_name(LayerElement *elem, const char* name); void layer_element_set_layer(LayerElement *elem, int layer); int layer_element_get_layer(LayerElement *elem); -void layer_element_set_opacity(LayerElement *elem, int opacity); -int layer_element_get_opacity(LayerElement *elem); void layer_element_set_export(LayerElement *elem, gboolean export); gboolean layer_element_get_export(LayerElement *elem); void layer_element_get_color(LayerElement *elem, GdkRGBA *rgba);