[gtk/constraint-list-model: 2/4] constraint editor: Implement saving
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/constraint-list-model: 2/4] constraint editor: Implement saving
- Date: Tue, 2 Jul 2019 11:42:27 +0000 (UTC)
commit 9a5ea0a713b7040500a88ac696e5fd4604780145
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Jul 2 04:11:46 2019 +0000
constraint editor: Implement saving
Save to a ui file.
demos/constraint-editor/constraint-editor-window.c | 60 ++++++++++++++++++++--
demos/constraint-editor/constraint-editor.c | 34 ++++++++++++
demos/constraint-editor/constraint-editor.h | 4 ++
demos/constraint-editor/guide-editor.c | 23 +++++++++
demos/constraint-editor/guide-editor.h | 4 ++
5 files changed, 120 insertions(+), 5 deletions(-)
---
diff --git a/demos/constraint-editor/constraint-editor-window.c
b/demos/constraint-editor/constraint-editor-window.c
index c6e4fa42e7..971450e5fa 100644
--- a/demos/constraint-editor/constraint-editor-window.c
+++ b/demos/constraint-editor/constraint-editor-window.c
@@ -102,6 +102,58 @@ open_cb (GtkWidget *button,
gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
}
+static void
+serialize_child (GString *str,
+ int indent,
+ GtkWidget *child)
+{
+ const char *name;
+
+ name = gtk_widget_get_name (child);
+ g_string_append_printf (str, "%*s<child>\n", indent, "");
+ g_string_append_printf (str, "%*s <object class=\"GtkLabel\" id=\"%s\">\n", indent, "", name);
+ g_string_append_printf (str, "%*s <property name=\"label\">%s</property>\n", indent, "", name);
+ g_string_append_printf (str, "%*s </object>\n", indent, "");
+ g_string_append_printf (str, "%*s</child>\n", indent, "");
+}
+
+static char *
+serialize_model (GListModel *list)
+{
+ GString *str = g_string_new ("");
+ int i;
+
+ g_string_append (str, "<interface>\n");
+ g_string_append (str, " <object class=\"GtkBox\" id=\"view\">\n");
+ g_string_append (str, " <property name=\"layout-manager\">\n");
+ g_string_append (str, " <object class=\"GtkConstraintLayout\">\n");
+ g_string_append (str, " <constraints>\n");
+ for (i = 0; i < g_list_model_get_n_items (list); i++)
+ {
+ gpointer item = g_list_model_get_item (list, i);
+ g_object_unref (item);
+ if (GTK_IS_CONSTRAINT (item))
+ constraint_editor_serialize_constraint (str, 10, GTK_CONSTRAINT (item));
+ else if (GTK_IS_CONSTRAINT_GUIDE (item))
+ guide_editor_serialize_guide (str, 10, GTK_CONSTRAINT_GUIDE (item));
+ }
+ g_string_append (str, " </constraints>\n");
+ g_string_append (str, " </object>\n");
+ g_string_append (str, " </property>\n");
+ for (i = 0; i < g_list_model_get_n_items (list); i++)
+ {
+ gpointer item = g_list_model_get_item (list, i);
+ g_object_unref (item);
+ if (GTK_IS_WIDGET (item))
+ serialize_child (str, 4, GTK_WIDGET (item));
+ }
+ g_string_append (str, " </object>\n");
+ g_string_append (str, "</interface>\n");
+
+ return g_string_free (str, FALSE);
+}
+
+
static void
save_response_cb (GtkNativeDialog *dialog,
gint response,
@@ -111,14 +163,12 @@ save_response_cb (GtkNativeDialog *dialog,
if (response == GTK_RESPONSE_ACCEPT)
{
+ GListModel *model;
char *text, *filename;
GError *error = NULL;
-#if 0
- text = get_current_text (self->text_buffer);
-#else
- text = "Sorry, Dave";
-#endif
+ model = constraint_view_get_model (CONSTRAINT_VIEW (self->view));
+ text = serialize_model (model);
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (!g_file_set_contents (filename, text, -1, &error))
diff --git a/demos/constraint-editor/constraint-editor.c b/demos/constraint-editor/constraint-editor.c
index e871b6a6c8..8346be7d42 100644
--- a/demos/constraint-editor/constraint-editor.c
+++ b/demos/constraint-editor/constraint-editor.c
@@ -236,6 +236,40 @@ get_strength_nick (GtkConstraintStrength strength)
return nick;
}
+void
+constraint_editor_serialize_constraint (GString *str,
+ int indent,
+ GtkConstraint *constraint)
+{
+ const char *target;
+ const char *target_attr;
+ const char *relation;
+ const char *source;
+ const char *source_attr;
+ double multiplier;
+ double constant;
+ const char *strength;
+
+ target = get_target_name (gtk_constraint_get_target (constraint));
+ target_attr = get_attr_nick (gtk_constraint_get_target_attribute (constraint));
+ relation = get_relation_nick (gtk_constraint_get_relation (constraint));
+ source = get_target_name (gtk_constraint_get_source (constraint));
+ source_attr = get_attr_nick (gtk_constraint_get_source_attribute (constraint));
+ multiplier = gtk_constraint_get_multiplier (constraint);
+ constant = gtk_constraint_get_constant (constraint);
+ strength = get_strength_nick (gtk_constraint_get_strength (constraint));
+
+ g_string_append_printf (str, "%*s<constraint target=\"%s\" target-attribute=\"%s\"\n", indent, "", target,
target_attr);
+ g_string_append_printf (str, "%*s relation=\"%s\"\n", indent, "", relation);
+ if (strcmp (source_attr, "none") != 0)
+ {
+ g_string_append_printf (str, "%*s source=\"%s\" source-attribute=\"%s\"\n", indent, "",
source, source_attr);
+ g_string_append_printf (str, "%*s multiplier=\"%g\"\n", indent, "", multiplier);
+ }
+ g_string_append_printf (str, "%*s constant=\"%g\"\n", indent, "", constant);
+ g_string_append_printf (str, "%*s strength=\"%s\" />\n", indent, "", strength);
+}
+
static void
create_constraint (GtkButton *button,
ConstraintEditor *editor)
diff --git a/demos/constraint-editor/constraint-editor.h b/demos/constraint-editor/constraint-editor.h
index c5940e254b..b2b5613856 100644
--- a/demos/constraint-editor/constraint-editor.h
+++ b/demos/constraint-editor/constraint-editor.h
@@ -27,3 +27,7 @@ G_DECLARE_FINAL_TYPE (ConstraintEditor, constraint_editor, CONSTRAINT, EDITOR, G
ConstraintEditor * constraint_editor_new (GListModel *model,
GtkConstraint *constraint);
+
+void constraint_editor_serialize_constraint (GString *str,
+ int indent,
+ GtkConstraint *constraint);
diff --git a/demos/constraint-editor/guide-editor.c b/demos/constraint-editor/guide-editor.c
index 663697337c..a7ea6b5da0 100644
--- a/demos/constraint-editor/guide-editor.c
+++ b/demos/constraint-editor/guide-editor.c
@@ -89,6 +89,29 @@ get_strength_nick (GtkConstraintStrength strength)
return nick;
}
+void
+guide_editor_serialize_guide (GString *str,
+ int indent,
+ GtkConstraintGuide *guide)
+{
+ int min_width, min_height;
+ int nat_width, nat_height;
+ int max_width, max_height;
+ const char *name;
+ const char *strength;
+
+ gtk_constraint_guide_get_min_size (guide, &min_width, &min_height);
+ gtk_constraint_guide_get_nat_size (guide, &nat_width, &nat_height);
+ gtk_constraint_guide_get_max_size (guide, &max_width, &max_height);
+ name = gtk_constraint_guide_get_name (guide);
+ strength = get_strength_nick (gtk_constraint_guide_get_strength (guide));
+
+ g_string_append_printf (str, "%*s<guide min-width=\"%d\" min-height=\"%d\"\n", indent, "", min_width,
min_height);
+ g_string_append_printf (str, "%*s nat-width=\"%d\" nat-height=\"%d\"\n", indent, "", nat_width,
nat_height);
+ g_string_append_printf (str, "%*s max-width=\"%d\" max-height=\"%d\"\n", indent, "", max_width,
max_height);
+ g_string_append_printf (str, "%*s name=\"%s\" strength=\"%s\" />\n", indent, "", name, strength);
+}
+
static void
create_guide (GtkButton *button,
GuideEditor *editor)
diff --git a/demos/constraint-editor/guide-editor.h b/demos/constraint-editor/guide-editor.h
index d11cb4f3db..56ccbfd5d3 100644
--- a/demos/constraint-editor/guide-editor.h
+++ b/demos/constraint-editor/guide-editor.h
@@ -26,3 +26,7 @@
G_DECLARE_FINAL_TYPE (GuideEditor, guide_editor, GUIDE, EDITOR, GtkWidget)
GuideEditor * guide_editor_new (GtkConstraintGuide *guide);
+
+void guide_editor_serialize_guide (GString *str,
+ int indent,
+ GtkConstraintGuide *guide);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]