From 9eb861f6d8364fd8ddcf6f54970e9de48ff0bc57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Thu, 17 May 2018 21:46:14 +0200 Subject: [PATCH] layer mapping save function implemented --- CMakeLists.txt | 13 +- layer-selector.c | 112 +++++++++++++++++- layer-selector.h | 6 +- .../layer-element.c | 0 .../layer-element.h | 0 main.c | 14 ++- 6 files changed, 131 insertions(+), 14 deletions(-) rename layer-element.c => layer-widget/layer-element.c (100%) rename layer-element.h => layer-widget/layer-element.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47a38d5..3701bc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,24 @@ cmake_minimum_required(VERSION 2.8) +aux_source_directory("layer-widget" SUB_SOURCES) +set(SOURCE "main.c" "gdsparse.c" "layer-selector.c") + + +set(SOURCE + ${SOURCE} + ${SUB_SOURCES} +) + find_package(PkgConfig REQUIRED) pkg_search_module(GLIB REQUIRED glib-2.0) pkg_check_modules(GTK3 REQUIRED gtk+-3.0) -include_directories(${GLIB_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS}) +include_directories(${GLIB_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS} "layer-widget") link_directories(${GLIB_LINK_DIRS} ${GTK3_LINK_DIRS}) configure_file(glade/main.glade glade/main.glade COPYONLY) configure_file(glade/layer-widget.glade glade/layer-widget.glade COPYONLY) project(gds-render) -add_executable(${PROJECT_NAME} "main.c" "gdsparse.c" "layer-element.c" "layer-selector.c") +add_executable(${PROJECT_NAME} ${SOURCE}) target_link_libraries(${PROJECT_NAME} ${GLIB_LDFLAGS} ${GTK3_LDFLAGS} m) diff --git a/layer-selector.c b/layer-selector.c index 1fd7aaf..05bdd07 100644 --- a/layer-selector.c +++ b/layer-selector.c @@ -19,7 +19,12 @@ #include "layer-selector.h" #include "gdsparse.h" +#include +#include +static GList *layer_widgets = NULL; +static GtkWidget *load_button; +static GtkWidget *save_button; static void layer_list_remove_element(struct layer_info *inf) { @@ -42,8 +47,6 @@ void get_layer_info(GList **info_list, GtkListBox *box) *info_list = local_list; } -static GList *layer_widgets = NULL; - static void delete_layer_widget(GtkWidget *widget) { @@ -58,9 +61,13 @@ void clear_list_box_widgets(GtkListBox *box) for (;list != NULL; list = list->next) { gtk_container_remove(GTK_CONTAINER(box), GTK_WIDGET(list->data)); } - /* Widgets are already destroyed when removed from box */ + /* Widgets are already destroyed when removed from box because they are only referenced inside the container */ g_list_free(layer_widgets); layer_widgets = NULL; + + /* Deactivate buttons */ + gtk_widget_set_sensitive(load_button, FALSE); + gtk_widget_set_sensitive(save_button, FALSE); } static gboolean check_if_layer_widget_exists(int layer) { @@ -94,7 +101,6 @@ static void analyze_cell_layers(GtkListBox *listbox, struct gds_cell *cell) gtk_list_box_insert(listbox, le, -1); gtk_widget_show(le); layer_widgets = g_list_append(layer_widgets, le); - printf("added new layer\n"); } } } @@ -136,4 +142,102 @@ void generate_layer_widgets(GtkListBox *listbox, GList *libs) /* Force sort */ gtk_list_box_invalidate_sort(listbox); + + /* Activate Buttons */ + gtk_widget_set_sensitive(load_button, TRUE); + gtk_widget_set_sensitive(save_button, TRUE); +} + +static void load_mapping_clicked(GtkWidget *button, gpointer user_data) +{ + printf("Clicked\n"); +} + +static void create_csv_line(LayerElement *layer_element, char *line_buffer, size_t max_len) +{ + GString *string; + gboolean export; + const gchar *name; + int layer; + GdkRGBA color; + int opacity; + + string = g_string_new_len(NULL, max_len-1); + + /* Extract values */ + 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); + + if (string->len > (max_len-1)) { + printf("Layer Definition too long. Please shorten Layer Name!!\n"); + line_buffer[0] = 0x0; + return; + } + + /* copy max_len bytes of string */ + strncpy(line_buffer, (char *)string->str, max_len-1); + line_buffer[max_len-1] = 0; + + /* Completely remove string */ + g_string_free(string, TRUE); +} + +static void save_layer_mapping_data(const gchar *file_name) +{ + FILE *file; + char workbuff[512]; + GList *le_list; + + /* Overwrite existing file */ + file = fopen((const char *)file_name, "w"); + + /* 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; + create_csv_line((LayerElement *)le_list->data, workbuff, sizeof(workbuff)); + fwrite(workbuff, sizeof(char), strlen(workbuff), file); + } + + /* Save File */ + fflush(file); + fclose(file); +} + +static void save_mapping_clicked(GtkWidget *button, gpointer user_data) +{ + GtkWidget *dialog; + gint res; + 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); + res = gtk_dialog_run(GTK_DIALOG(dialog)); + if (res == GTK_RESPONSE_ACCEPT) { + file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + save_layer_mapping_data(file_name); + g_free(file_name); + } + gtk_widget_destroy(dialog); +} + +void setup_load_mapping_callback(GtkWidget *button, GtkWindow *main_window) +{ + g_object_ref(G_OBJECT(button)); + load_button = button; + g_signal_connect(button, "clicked", G_CALLBACK(load_mapping_clicked), main_window); +} + +void setup_save_mapping_callback(GtkWidget *button, GtkWindow *main_window) +{ + g_object_ref(G_OBJECT(button)); + save_button = button; + g_signal_connect(button, "clicked", G_CALLBACK(save_mapping_clicked), main_window); } diff --git a/layer-selector.h b/layer-selector.h index 4e9eb79..c2015cb 100644 --- a/layer-selector.h +++ b/layer-selector.h @@ -22,7 +22,7 @@ #include #include -#include "layer-element.h" +#include struct layer_info { int id; @@ -33,8 +33,8 @@ struct layer_info { void generate_layer_widgets(GtkListBox *listbox, GList *libs); void clear_list_box_widgets(); -void setup_load_mapping_callback(GtkWidget *button); -void setup_save_mapping_callback(GtkWidget *button); +void setup_load_mapping_callback(GtkWidget *button, GtkWindow *main_window); +void setup_save_mapping_callback(GtkWidget *button, GtkWindow *main_window); void get_layer_info(GList **info_list, GtkListBox *box); diff --git a/layer-element.c b/layer-widget/layer-element.c similarity index 100% rename from layer-element.c rename to layer-widget/layer-element.c diff --git a/layer-element.h b/layer-widget/layer-element.h similarity index 100% rename from layer-element.h rename to layer-widget/layer-element.h diff --git a/main.c b/main.c index ba9d6b8..6fdcc35 100644 --- a/main.c +++ b/main.c @@ -20,7 +20,7 @@ #include #include "gdsparse.h" #include -#include "layer-element.h" +#include #include "layer-selector.h" @@ -38,9 +38,6 @@ struct open_button_data { GtkListBox *layer_box; }; - - - gboolean on_window_close(gpointer window, gpointer user) { gtk_widget_destroy(GTK_WIDGET(window)); @@ -116,6 +113,7 @@ void on_load_gds(gpointer button, gpointer user) } end_destroy: + /* Destroy dialog and filter */ gtk_widget_destroy(open_dialog); } @@ -178,10 +176,16 @@ int main(int argc, char **argv) widget_generic = GTK_WIDGET(gtk_builder_get_object(main_builder, "convert-button")); g_signal_connect(widget_generic, "clicked", G_CALLBACK(on_convert_clicked), layer); - /* test widget list */ listbox = GTK_WIDGET(gtk_builder_get_object(main_builder, "layer-list")); open_data.layer_box = GTK_LIST_BOX(listbox); + /* Set buttons fpr layer mapping GUI */ + setup_load_mapping_callback(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-load-mapping")), + open_data.main_window); + setup_save_mapping_callback(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-save-mapping")), + open_data.main_window); + + gtk_main(); return 0;