[gnome-calculator/wip/conversion-ui] Conversion ui improvements (#72)



commit 7cf0c88f31af000406487c854d7bbaac3317c662
Author: Robert Roth <robert roth off gmail com>
Date:   Thu Oct 4 23:41:03 2018 +0300

    Conversion ui improvements (#72)

 src/math-converter.ui   |  9 +++++++--
 src/math-converter.vala | 45 ++++++++++++++++++++++++++++++---------------
 src/math-display.vala   | 12 ++++--------
 3 files changed, 41 insertions(+), 25 deletions(-)
---
diff --git a/src/math-converter.ui b/src/math-converter.ui
index 8172ede3..9ec03820 100644
--- a/src/math-converter.ui
+++ b/src/math-converter.ui
@@ -23,10 +23,14 @@
       </packing>
     </child>
     <child>
-      <object class="GtkLabel" id="in_label">
+      <object class="GtkButton" id="in_button">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="label" translatable="yes"> in </property>
+        <property name="label" translatable="yes"> to </property>
+        <signal name="clicked" handler="convert_button_clicked_cb" swapped="no"/>
+        <style>
+          <class name="flat"/>
+        </style>
       </object>
       <packing>
         <property name="left_attach">1</property>
@@ -130,3 +134,4 @@
     </child>
   </template>
 </interface>
+
diff --git a/src/math-converter.vala b/src/math-converter.vala
index 0cf90ac5..a83dea24 100644
--- a/src/math-converter.vala
+++ b/src/math-converter.vala
@@ -102,12 +102,10 @@ public class MathConverter : Gtk.Grid
         if (x == null)
             return;
 
-        var z = convert_equation (x);
+        Unit source_unit, target_unit;
+        var z = convert_equation (x, out source_unit, out target_unit);
         if (z != null)
         {
-            Unit source_unit, target_unit;
-            get_conversion (out source_unit, out target_unit);
-
             var source_text = source_unit.format (x);
             var target_text = target_unit.format (z);
             from_label.set_text (source_text);
@@ -228,35 +226,52 @@ public class MathConverter : Gtk.Grid
     [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);
+
+        do_convert(out from_unit, out to_unit);
+
+        update_result_label ();
+    }
+
+    private void do_convert (out Unit? from_unit, out Unit? to_unit) {
         var x = equation.number;
         if (x != null)
         {
-            var z = convert_equation (x);
-            if (z != null)
-                equation.set_number (z);
+            var z = convert_equation (x, out from_unit, out to_unit);
+            if (z != null && from_unit != null && to_unit != null)
+            {
+                equation.set ("%s %s %s %s".printf(equation.serializer.to_string (x), 
from_unit.display_name, _("in"), to_unit.display_name));
+                equation.solve ();
+            }
         }
+    }
 
-        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);
+    [GtkCallback]
+    private void convert_button_clicked_cb ()
+    {
+        Unit? from_unit, to_unit;
+        do_convert (out from_unit, out to_unit);
 
         update_result_label ();
     }
 
-    private Number? convert_equation (Number x)
+    private Number?  convert_equation (Number x,
+                                       out Unit? source_unit,
+                                       out Unit? target_unit)
     {
         Gtk.TreeIter from_iter, to_iter;
         if (!from_combo.get_active_iter (out from_iter))
             return null;
         if (!to_combo.get_active_iter (out to_iter))
             return null;
-
         UnitCategory category;
-        Unit source_unit, target_unit;
         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);
 
         return category.convert (x, source_unit, target_unit);
-    }
+  }
 }
diff --git a/src/math-display.vala b/src/math-display.vala
index 21d8cd70..87e31400 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -27,7 +27,7 @@ public class MathDisplay : Gtk.Viewport
     public MathDisplay (MathEquation equation)
     {
         _equation = equation;
-        _equation.history_signal.connect (this.handler);
+        _equation.history_signal.connect (this.update_history);
         var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
         add (main_box);
 
@@ -94,9 +94,10 @@ public class MathDisplay : Gtk.Viewport
         source_view.grab_focus ();
     }
 
-    public void handler (string answer, Number number, int number_base, uint representation_base)
+    public void update_history (string answer, Number number, int number_base, uint representation_base)
     {
-        this.update_history (answer, number, number_base, representation_base); /* Recieves signal emitted 
by a MathEquation object for updating history-view */
+        /* Recieves signal emitted by a MathEquation object for updating history-view */
+        history.insert_entry (answer, number, number_base, representation_base); /* Sends current equation 
and answer for updating History-View */
     }
 
     public void display_text (string prev_eq)
@@ -104,11 +105,6 @@ public class MathDisplay : Gtk.Viewport
         _equation.display_selected (prev_eq);
     }
 
-    public void update_history (string answer, Number number, int number_base, uint representation_base)
-    {
-        history.insert_entry (answer, number, number_base, representation_base); /* Sends current equation 
and answer for updating History-View */
-    }
-
     public void clear_history ()
     {
         history.clear ();


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