[gtk/constraint-list-model: 6/7] constraints editor: Stop naming constraints



commit e4e846c2b20f742cdc19046830b567191e8f4601
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jul 2 04:35:16 2019 +0000

    constraints editor: Stop naming constraints
    
    We don't have a field for the name in ui files,
    so stop naming constraints.

 demos/constraint-editor/constraint-editor-window.c |  7 ++-
 demos/constraint-editor/constraint-editor.c        | 66 +++++++++++++++-------
 demos/constraint-editor/constraint-editor.h        |  1 +
 demos/constraint-editor/constraint-editor.ui       | 17 ------
 4 files changed, 54 insertions(+), 37 deletions(-)
---
diff --git a/demos/constraint-editor/constraint-editor-window.c 
b/demos/constraint-editor/constraint-editor-window.c
index 971450e5fa..6b19eb0e8a 100644
--- a/demos/constraint-editor/constraint-editor-window.c
+++ b/demos/constraint-editor/constraint-editor-window.c
@@ -441,14 +441,17 @@ create_widget_func (gpointer item,
 {
   ConstraintEditorWindow *win = user_data;
   const char *name;
+  char *freeme = NULL;
   GtkWidget *row, *box, *label, *button;
 
   if (GTK_IS_WIDGET (item))
     name = gtk_widget_get_name (GTK_WIDGET (item));
   else if (GTK_IS_CONSTRAINT_GUIDE (item))
     name = gtk_constraint_guide_get_name (GTK_CONSTRAINT_GUIDE (item));
+  else if (GTK_IS_CONSTRAINT (item))
+    name = freeme = constraint_editor_constraint_to_string (GTK_CONSTRAINT (item));
   else
-    name = (const char *)g_object_get_data (G_OBJECT (item), "name");
+    name = "";
 
   row = gtk_list_box_row_new ();
   g_object_set_data_full (G_OBJECT (row), "item", g_object_ref (item), g_object_unref);
@@ -484,6 +487,8 @@ create_widget_func (gpointer item,
       gtk_container_add (GTK_CONTAINER (box), button);
     }
 
+  g_free (freeme);
+
   return row;
 }
 
diff --git a/demos/constraint-editor/constraint-editor.c b/demos/constraint-editor/constraint-editor.c
index 8346be7d42..62af6e356f 100644
--- a/demos/constraint-editor/constraint-editor.c
+++ b/demos/constraint-editor/constraint-editor.c
@@ -26,7 +26,6 @@ struct _ConstraintEditor
   GtkWidget parent_instance;
 
   GtkWidget *grid;
-  GtkWidget *name;
   GtkWidget *target;
   GtkWidget *target_attr;
   GtkWidget *relation;
@@ -284,7 +283,6 @@ create_constraint (GtkButton        *button,
   double constant;
   int strength;
   GtkConstraint *constraint;
-  const char *name;
 
   id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->target));
   target = get_target (editor->model, id);
@@ -306,15 +304,12 @@ create_constraint (GtkButton        *button,
   id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->strength));
   strength = get_strength (id);
 
-  name = gtk_editable_get_text (GTK_EDITABLE (editor->name));
-
   constraint = gtk_constraint_new (target, target_attr,
                                    relation,
                                    source, source_attr,
                                    multiplier,
                                    constant,
                                    strength);
-  g_object_set_data_full (G_OBJECT (constraint), "name", g_strdup (name), g_free);
   g_signal_emit (editor, signals[DONE], 0, constraint);
   g_object_unref (constraint);
 }
@@ -339,6 +334,53 @@ source_attr_changed (ConstraintEditor *editor)
     }
 }
 
+char *
+constraint_editor_constraint_to_string (GtkConstraint *constraint)
+{
+  GString *str;
+  const char *name;
+  const char *attr;
+  const char *relation;
+  double c, m;
+
+  str = g_string_new ("");
+
+  name = get_target_name (gtk_constraint_get_target (constraint));
+  attr = get_attr_nick (gtk_constraint_get_target_attribute (constraint));
+  relation = get_relation_nick (gtk_constraint_get_relation (constraint));
+
+  if (name == NULL)
+    name = "[ ]";
+
+  g_string_append_printf (str, "%s.%s %s ", name, attr, relation);
+
+  c = gtk_constraint_get_constant (constraint);
+
+  attr = get_attr_nick (gtk_constraint_get_source_attribute (constraint));
+  if (strcmp (attr, "none") != 0)
+    {
+      name = get_target_name (gtk_constraint_get_source (constraint));
+      m = gtk_constraint_get_multiplier (constraint);
+
+      if (name == NULL)
+        name = "[ ]";
+
+      g_string_append_printf (str, "%s.%s", name, attr);
+
+      if (m != 1.0)
+        g_string_append_printf (str, " × %g", m);
+
+      if (c > 0.0)
+        g_string_append_printf (str, " + %g", c);
+      else if (c < 0.0)
+        g_string_append_printf (str, " - %g", -c);
+    }
+  else
+    g_string_append_printf (str, "%g", c);
+
+  return g_string_free (str, FALSE);
+}
+
 static void
 update_preview (ConstraintEditor *editor)
 {
@@ -412,8 +454,6 @@ constraint_editor_init (ConstraintEditor *editor)
   gtk_widget_init_template (GTK_WIDGET (editor));
 }
 
-static int constraint_counter;
-
 static void
 constraint_editor_constructed (GObject *object)
 {
@@ -438,10 +478,6 @@ constraint_editor_constructed (GObject *object)
       double multiplier;
       double constant;
 
-      nick = (char *)g_object_get_data (G_OBJECT (editor->constraint), "name");
-      if (nick)
-        gtk_editable_set_text (GTK_EDITABLE (editor->name), nick);
-
       target = gtk_constraint_get_target (editor->constraint);
       nick = get_target_name (target);
       gtk_combo_box_set_active_id (GTK_COMBO_BOX (editor->target), nick);
@@ -480,13 +516,6 @@ constraint_editor_constructed (GObject *object)
     }
   else
     {
-      char *name;
-
-      constraint_counter++;
-      name = g_strdup_printf ("Constraint %d", constraint_counter);
-      gtk_editable_set_text (GTK_EDITABLE (editor->name), name);
-      g_free (name);
-
       gtk_combo_box_set_active_id (GTK_COMBO_BOX (editor->target_attr), "left");
       gtk_combo_box_set_active_id (GTK_COMBO_BOX (editor->source_attr), "left");
       gtk_combo_box_set_active_id (GTK_COMBO_BOX (editor->relation), "eq");
@@ -599,7 +628,6 @@ constraint_editor_class_init (ConstraintEditorClass *class)
                                                "/org/gtk/gtk4/constraint-editor/constraint-editor.ui");
 
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditor, grid);
-  gtk_widget_class_bind_template_child (widget_class, ConstraintEditor, name);
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditor, target);
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditor, target_attr);
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditor, relation);
diff --git a/demos/constraint-editor/constraint-editor.h b/demos/constraint-editor/constraint-editor.h
index b2b5613856..1b6125bfad 100644
--- a/demos/constraint-editor/constraint-editor.h
+++ b/demos/constraint-editor/constraint-editor.h
@@ -31,3 +31,4 @@ ConstraintEditor * constraint_editor_new (GListModel    *model,
 void constraint_editor_serialize_constraint (GString       *str,
                                              int            indent,
                                              GtkConstraint *constraint);
+char *constraint_editor_constraint_to_string (GtkConstraint *constraint);
diff --git a/demos/constraint-editor/constraint-editor.ui b/demos/constraint-editor/constraint-editor.ui
index 604757659a..8d12a37758 100644
--- a/demos/constraint-editor/constraint-editor.ui
+++ b/demos/constraint-editor/constraint-editor.ui
@@ -6,23 +6,6 @@
         <property name="margin">20</property>
         <property name="row-spacing">10</property>
         <property name="column-spacing">10</property>
-        <child>
-          <object class="GtkLabel">
-            <property name="label">Name</property>
-            <layout>
-              <property name="left-attach">0</property>
-              <property name="top-attach">0</property>
-            </layout>
-          </object>
-        </child>
-        <child>
-          <object class="GtkEntry" id="name">
-            <layout>
-              <property name="left-attach">1</property>
-              <property name="top-attach">0</property>
-            </layout>
-          </object>
-        </child>
         <child>
           <object class="GtkLabel">
             <property name="label">Target</property>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]