[gnome-calculator/convert-categories: 21/21] converter: Replaced from and to combos with dropdown




commit 95cc20201f2127ec0a67521d3a12ff09cb24e992
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Nov 12 14:28:52 2021 +0200

    converter: Replaced from and to combos with dropdown

 src/math-converter.vala  | 174 ++++++++++++-----------------------------------
 src/ui/math-converter.ui |  25 ++-----
 2 files changed, 47 insertions(+), 152 deletions(-)
---
diff --git a/src/math-converter.vala b/src/math-converter.vala
index da51acc2..3d1f1e21 100644
--- a/src/math-converter.vala
+++ b/src/math-converter.vala
@@ -17,15 +17,12 @@ public class MathConverter : Gtk.Grid
 
     private bool single_category = false;
 
-    [GtkChild]
-    private unowned Gtk.CellRendererText from_renderer;
-
     [GtkChild]
     private unowned Gtk.DropDown category_combo;
     [GtkChild]
-    private unowned Gtk.ComboBox from_combo;
+    private unowned Gtk.DropDown from_combo;
     [GtkChild]
-    private unowned Gtk.ComboBox to_combo;
+    private unowned Gtk.DropDown to_combo;
     [GtkChild]
     private unowned Gtk.Label from_label;
     [GtkChild]
@@ -43,7 +40,6 @@ public class MathConverter : Gtk.Grid
 
     construct
     {
-        from_combo.set_cell_data_func (from_renderer, from_cell_data_func);
         CurrencyManager.get_default ().updated.connect (() => {
             update_visibility ();
             update_result_label ();
@@ -51,7 +47,7 @@ public class MathConverter : Gtk.Grid
 
         build_category_model ();
         update_visibility ();
-        update_from_model ();
+        build_units_model ();
     }
 
     public MathConverter (MathEquation equation)
@@ -111,32 +107,19 @@ public class MathConverter : Gtk.Grid
         var ub = UnitManager.get_default ().get_unit_by_name (unit_b);
         if (ua == null || ub == null)
         {
-            /* Select the first unit */
-            var model = from_combo.get_model ();
-            Gtk.TreeIter iter;
-            if (model.get_iter_first (out iter))
-            {
-                Gtk.TreeIter child_iter;
-                while (model.iter_children (out child_iter, iter))
-                    iter = child_iter;
-                from_combo.set_active_iter (iter);
-            }
+            from_combo.selected = 0;
             return;
         }
 
-        set_active_unit (from_combo, null, ua);
-        set_active_unit (to_combo, null, ub);
+        set_active_unit (from_combo, ua);
+        set_active_unit (to_combo, ub);
     }
 
     public void get_conversion (out Unit from_unit, out Unit to_unit)
     {
-        Gtk.TreeIter from_iter, to_iter;
-
-        from_combo.get_active_iter (out from_iter);
-        to_combo.get_active_iter (out to_iter);
 
-        from_combo.get_model ().get (from_iter, 2, out from_unit, -1);
-        to_combo.get_model ().get (to_iter, 2, out to_unit, -1);
+        from_unit = from_combo.selected_item as Unit;
+        to_unit = to_combo.selected_item as Unit;
     }
 
     private void update_visibility ()
@@ -153,6 +136,8 @@ public class MathConverter : Gtk.Grid
 
     private void update_result_label ()
     {
+        if (equation == null)
+            return;
         var x = equation.number;
         if (x == null)
             return;
@@ -168,115 +153,50 @@ public class MathConverter : Gtk.Grid
         }
     }
 
-    private void update_from_model ()
+    private void build_units_model ()
     {
-        var from_model = new Gtk.TreeStore (3, typeof (string), typeof (UnitCategory), typeof (Unit));
+        var unit_model = new ListStore (typeof (Unit));
 
-        if (category == null)
-        {
-            var categories = UnitManager.get_default ().get_categories ();
-            foreach (var category in categories)
-            {
-                Gtk.TreeIter parent;
-                from_model.append (out parent, null);
-                from_model.set (parent, 0, category.display_name, 1, category, -1);
-
-                foreach (var unit in category.get_units ())
-                {
-                    Gtk.TreeIter iter;
-                    from_model.append (out iter, parent);
-                    from_model.set (iter, 0, unit.display_name, 1, category, 2, unit, -1);
-                }
-            }
-        }
-        else
+        var expression = new Gtk.PropertyExpression (typeof (Unit),
+                                                     null,
+                                                     "display_name");
+        from_combo.expression = expression;
+        to_combo.expression = expression;
+
+        var c = UnitManager.get_default ().get_category (category);
+        foreach (var unit in c.get_units ())
         {
-            var c = UnitManager.get_default ().get_category (category);
-            foreach (var unit in c.get_units ())
-            {
-                Gtk.TreeIter iter;
-                from_model.append (out iter, null);
-                from_model.set (iter, 0, unit.display_name, 1, c, 2, unit, -1);
-            }
+            unit_model.append (unit);
         }
 
-        from_combo.model = from_model;
-    }
-
-    private bool iter_is_unit (Gtk.TreeModel model, Gtk.TreeIter iter, Unit unit)
-    {
-        Unit u;
-        model.get (iter, 2, out u, -1);
-        return u == unit;
+        uint model_size = unit_model.get_n_items ();
+        to_combo.model = unit_model;
+        to_combo.enable_search = model_size > 10;
+        from_combo.model = unit_model;
+        from_combo.enable_search = model_size > 10;
     }
 
-    private bool set_active_unit (Gtk.ComboBox combo, Gtk.TreeIter? iter, Unit unit)
+    private bool set_active_unit (Gtk.DropDown combo, Unit unit)
     {
-        var model = combo.get_model ();
-        if (iter != null && iter_is_unit (model, iter, unit))
-        {
-            combo.set_active_iter (iter);
-            return true;
-        }
-
-        Gtk.TreeIter child_iter;
-        if (!model.iter_children (out child_iter, iter))
+        uint position = 0;
+        var model = combo.get_model () as ListStore;
+        model.find (unit, out position);
+        if (position == -1)
             return false;
-
-        do
-        {
-            if (set_active_unit (combo, child_iter, unit))
-                return true;
-        } while (model.iter_next (ref child_iter));
-
-        return false;
+        combo.selected = position;
+        return true;
     }
 
     [GtkCallback]
     private void category_combobox_changed_cb ()
     {
-
         UnitCategory? category  = category_combo.selected_item as UnitCategory;
 
         this.category = category.name;
 
         update_visibility ();
-        update_from_model ();
-        from_combo.set_active (0);
-
-    }
-
-    [GtkCallback]
-    private void from_combobox_changed_cb ()
-    {
-        UnitCategory? category = null, to_category = null;
-        Unit unit;
-        Gtk.TreeIter iter, to_iter;
-
-        var model = from_combo.get_model ();
-        var to_model = to_combo.get_model () as Gtk.ListStore;
-
-        if (!from_combo.get_active_iter (out iter))
-            return;
-
-        model.get (iter, 1, out category, 2, out unit, -1);
-        if (to_combo.get_active_iter (out to_iter))
-            to_model.get (to_iter, 1, out to_category);
-
-        if (category != to_category)
-        {
-            /* Set the to combobox to be the list of units can be converted to */
-            to_model = new Gtk.ListStore (3, typeof (string), typeof (UnitCategory), typeof (Unit));
-            foreach (var u in category.get_units ())
-            {
-                to_model.append (out iter);
-                to_model.set (iter, 0, u.display_name, 1, category, 2, u, -1);
-            }
-            to_combo.model = to_model;
-
-            /* Select the first possible unit */
-            to_combo.set_active (0);
-        }
+        build_units_model ();
+        from_combo.selected = 0;
     }
 
     [GtkCallback]
@@ -287,19 +207,14 @@ public class MathConverter : Gtk.Grid
         changed ();
     }
 
-    private void from_cell_data_func (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel 
tree_model, Gtk.TreeIter iter)
-    {
-        cell.set ("sensitive", !tree_model.iter_has_child (iter));
-    }
-
     [GtkCallback]
     private void swap_button_clicked_cb ()
     {
         Unit? from_unit, to_unit;
         get_conversion (out from_unit, out to_unit);
 
-        set_active_unit (from_combo, null, to_unit);
-        set_active_unit (to_combo, null, from_unit);
+        set_active_unit (from_combo, to_unit);
+        set_active_unit (to_combo, from_unit);
 
         update_result_label ();
     }
@@ -332,17 +247,14 @@ public class MathConverter : Gtk.Grid
                                        out Unit? source_unit,
                                        out Unit? target_unit)
     {
-        Gtk.TreeIter from_iter, to_iter;
-        source_unit = null;
-        target_unit = null;
-        if (!from_combo.get_active_iter (out from_iter))
+        if (category_combo == null || from_combo == null || to_combo == null)
             return null;
-        if (!to_combo.get_active_iter (out to_iter))
-            return null;
-        UnitCategory category;
-        from_combo.model.get (from_iter, 1, out category, 2, out source_unit, -1);
-        to_combo.model.get (to_iter, 2, out target_unit, -1);
+        UnitCategory? category = category_combo.selected_item as UnitCategory;
+        source_unit = from_combo.selected_item as Unit;
+        target_unit = to_combo.selected_item as Unit;
 
+        if (category == null || source_unit == null || target_unit == null)
+            return null;
         return category.convert (x, source_unit, target_unit);
   }
 }
diff --git a/src/ui/math-converter.ui b/src/ui/math-converter.ui
index 052e9020..fbbd150e 100644
--- a/src/ui/math-converter.ui
+++ b/src/ui/math-converter.ui
@@ -31,17 +31,9 @@
             <property name="vexpand">False</property>
 
             <child>
-              <object class="GtkComboBox" id="from_combo">
+              <object class="GtkDropDown" id="from_combo">
                 <property name="hexpand">True</property>
-                <signal name="changed" handler="from_combobox_changed_cb" swapped="no"/>
-                <child>
-                  <object class="GtkCellRendererText" id="from_renderer">
-                    <property name="ellipsize">end</property>
-                  </object>
-                  <attributes>
-                    <attribute name="text">0</attribute>
-                  </attributes>
-                </child>
+                <signal name="notify::selected" handler="to_combobox_changed_cb" swapped="no"/>
               </object>
             </child>
             <child>
@@ -54,18 +46,9 @@
               </object>
             </child>
             <child>
-              <object class="GtkComboBox" id="to_combo">
-                <property name="opacity">0.88</property>
+              <object class="GtkDropDown" id="to_combo">
                 <property name="hexpand">True</property>
-                <signal name="changed" handler="to_combobox_changed_cb" swapped="no"/>
-                <child>
-                  <object class="GtkCellRendererText" id="to_renderer">
-                    <property name="ellipsize">end</property>
-                  </object>
-                  <attributes>
-                    <attribute name="text">0</attribute>
-                  </attributes>
-                </child>
+                <signal name="notify::selected" handler="to_combobox_changed_cb" swapped="no"/>
               </object>
             </child>
             <child>


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