layer mapping save function implemented

This commit is contained in:
Mario Hüttel 2018-05-17 21:46:14 +02:00
parent d7d8e6b2dc
commit 9eb861f6d8
6 changed files with 131 additions and 14 deletions

View File

@ -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)

View File

@ -19,7 +19,12 @@
#include "layer-selector.h"
#include "gdsparse.h"
#include <glib.h>
#include <string.h>
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: <Layer>,<R>,<G>,<B>,<Opacity>,<Export?>,<Name> */
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);
}

View File

@ -22,7 +22,7 @@
#include <gtk/gtk.h>
#include <glib.h>
#include "layer-element.h"
#include <layer-element.h>
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);

14
main.c
View File

@ -20,7 +20,7 @@
#include <stdio.h>
#include "gdsparse.h"
#include <gtk/gtk.h>
#include "layer-element.h"
#include <layer-element.h>
#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;