Blame SOURCES/0004-Conversion-ui-improvements-72.patch

69ba56
From 3edf6664432fd65ad68b7e5b91de65eef7fcdb09 Mon Sep 17 00:00:00 2001
69ba56
From: Robert Roth <robert.roth.off@gmail.com>
69ba56
Date: Thu, 4 Oct 2018 23:41:03 +0300
69ba56
Subject: [PATCH 4/5] Conversion ui improvements (#72)
69ba56
69ba56
---
69ba56
 src/math-converter.ui   |  9 +++++++--
69ba56
 src/math-converter.vala | 45 +++++++++++++++++++++++++++--------------
69ba56
 src/math-display.vala   | 12 ++++-------
69ba56
 3 files changed, 41 insertions(+), 25 deletions(-)
69ba56
69ba56
diff --git a/src/math-converter.ui b/src/math-converter.ui
69ba56
index 8172ede3..9ec03820 100644
69ba56
--- a/src/math-converter.ui
69ba56
+++ b/src/math-converter.ui
69ba56
@@ -1,59 +1,63 @@
69ba56
 
69ba56
 
69ba56
 <interface>
69ba56
   <requires lib="gtk+" version="3.16"/>
69ba56
   <template class="MathConverter" parent="GtkGrid">
69ba56
     <property name="visible">False</property>
69ba56
     <property name="can_focus">False</property>
69ba56
     <property name="row_spacing">6</property>
69ba56
     <property name="column_spacing">6</property>
69ba56
     <child>
69ba56
       <object class="GtkButton" id="swap_button">
69ba56
         <property name="label">⇆</property>
69ba56
         <property name="visible">True</property>
69ba56
         <property name="can_focus">False</property>
69ba56
         <property name="receives_default">False</property>
69ba56
         <property name="tooltip_text" translatable="yes">Switch conversion units</property>
69ba56
         <property name="relief">none</property>
69ba56
         <signal name="clicked" handler="swap_button_clicked_cb" swapped="no"/>
69ba56
       </object>
69ba56
       <packing>
69ba56
         <property name="left_attach">3</property>
69ba56
         <property name="top_attach">0</property>
69ba56
       </packing>
69ba56
     </child>
69ba56
     <child>
69ba56
-      <object class="GtkLabel" id="in_label">
69ba56
+      <object class="GtkButton" id="in_button">
69ba56
         <property name="visible">True</property>
69ba56
         <property name="can_focus">False</property>
69ba56
-        <property name="label" translatable="yes"> in </property>
69ba56
+        <property name="label" translatable="yes"> to </property>
69ba56
+        <signal name="clicked" handler="convert_button_clicked_cb" swapped="no"/>
69ba56
+        <style>
69ba56
+          <class name="flat"/>
69ba56
+        </style>
69ba56
       </object>
69ba56
       <packing>
69ba56
         <property name="left_attach">1</property>
69ba56
         <property name="top_attach">0</property>
69ba56
       </packing>
69ba56
     </child>
69ba56
     <child>
69ba56
       <object class="GtkComboBox" id="from_combo">
69ba56
         <property name="visible">True</property>
69ba56
         <property name="can_focus">False</property>
69ba56
         <signal name="changed" handler="from_combobox_changed_cb" swapped="no"/>
69ba56
         <child>
69ba56
           <object class="GtkCellRendererText" id="from_renderer"/>
69ba56
           <attributes>
69ba56
             <attribute name="text">0</attribute>
69ba56
           </attributes>
69ba56
         </child>
69ba56
       </object>
69ba56
       <packing>
69ba56
         <property name="left_attach">0</property>
69ba56
         <property name="top_attach">0</property>
69ba56
       </packing>
69ba56
     </child>
69ba56
     <child>
69ba56
       <object class="GtkComboBox" id="to_combo">
69ba56
         <property name="visible">True</property>
69ba56
         <property name="can_focus">False</property>
69ba56
         <property name="opacity">0.88</property>
69ba56
         <signal name="changed" handler="to_combobox_changed_cb" swapped="no"/>
69ba56
         <child>
69ba56
@@ -103,30 +107,31 @@
69ba56
             <property name="hexpand">False</property>
69ba56
             <property name="vexpand">False</property>
69ba56
             <property name="justify">center</property>
69ba56
             <property name="xalign">0</property>
69ba56
             <property name="yalign">0</property>
69ba56
             <property name="label" translatable="yes" context="convertion equals label">=</property>
69ba56
           </object>
69ba56
         </child>
69ba56
         <child>
69ba56
           <object class="GtkLabel" id="to_label">
69ba56
             <property name="visible">True</property>
69ba56
             <property name="sensitive">True</property>
69ba56
             <property name="can_focus">False</property>
69ba56
             <property name="halign">end</property>
69ba56
             <property name="valign">center</property>
69ba56
             <property name="hexpand">True</property>
69ba56
             <property name="vexpand">False</property>
69ba56
             <property name="justify">center</property>
69ba56
             <property name="xalign">0</property>
69ba56
             <property name="yalign">0</property>
69ba56
           </object>
69ba56
           <packing>
69ba56
             <property name="expand">false</property>
69ba56
             <property name="fill">true</property>
69ba56
           </packing>
69ba56
         </child>
69ba56
       </object>
69ba56
     </child>
69ba56
   </template>
69ba56
 </interface>
69ba56
+
69ba56
diff --git a/src/math-converter.vala b/src/math-converter.vala
69ba56
index 0cf90ac5..a83dea24 100644
69ba56
--- a/src/math-converter.vala
69ba56
+++ b/src/math-converter.vala
69ba56
@@ -75,66 +75,64 @@ public class MathConverter : Gtk.Grid
69ba56
             {
69ba56
                 Gtk.TreeIter child_iter;
69ba56
                 while (model.iter_children (out child_iter, iter))
69ba56
                     iter = child_iter;
69ba56
                 from_combo.set_active_iter (iter);
69ba56
             }
69ba56
             return;
69ba56
         }
69ba56
 
69ba56
         set_active_unit (from_combo, null, ua);
69ba56
         set_active_unit (to_combo, null, ub);
69ba56
     }
69ba56
 
69ba56
     public void get_conversion (out Unit from_unit, out Unit to_unit)
69ba56
     {
69ba56
         Gtk.TreeIter from_iter, to_iter;
69ba56
 
69ba56
         from_combo.get_active_iter (out from_iter);
69ba56
         to_combo.get_active_iter (out to_iter);
69ba56
 
69ba56
         from_combo.get_model ().get (from_iter, 2, out from_unit, -1);
69ba56
         to_combo.get_model ().get (to_iter, 2, out to_unit, -1);
69ba56
     }
69ba56
 
69ba56
     private void update_result_label ()
69ba56
     {
69ba56
         var x = equation.number;
69ba56
         if (x == null)
69ba56
             return;
69ba56
 
69ba56
-        var z = convert_equation (x);
69ba56
+        Unit source_unit, target_unit;
69ba56
+        var z = convert_equation (x, out source_unit, out target_unit);
69ba56
         if (z != null)
69ba56
         {
69ba56
-            Unit source_unit, target_unit;
69ba56
-            get_conversion (out source_unit, out target_unit);
69ba56
-
69ba56
             var source_text = source_unit.format (x);
69ba56
             var target_text = target_unit.format (z);
69ba56
             from_label.set_text (source_text);
69ba56
             to_label.set_text (target_text);
69ba56
         }
69ba56
     }
69ba56
 
69ba56
     private void update_from_model ()
69ba56
     {
69ba56
         var from_model = new Gtk.TreeStore (3, typeof (string), typeof (UnitCategory), typeof (Unit));
69ba56
 
69ba56
         if (category == null)
69ba56
         {
69ba56
             var categories = UnitManager.get_default ().get_categories ();
69ba56
             foreach (var category in categories)
69ba56
             {
69ba56
                 Gtk.TreeIter parent;
69ba56
                 from_model.append (out parent, null);
69ba56
                 from_model.set (parent, 0, category.display_name, 1, category, -1);
69ba56
 
69ba56
                 foreach (var unit in category.get_units ())
69ba56
                 {
69ba56
                     Gtk.TreeIter iter;
69ba56
                     from_model.append (out iter, parent);
69ba56
                     from_model.set (iter, 0, unit.display_name, 1, category, 2, unit, -1);
69ba56
                 }
69ba56
             }
69ba56
         }
69ba56
         else
69ba56
         {
69ba56
@@ -201,62 +199,79 @@ public class MathConverter : Gtk.Grid
69ba56
             /* Set the to combobox to be the list of units can be converted to */
69ba56
             to_model = new Gtk.ListStore (3, typeof (string), typeof (UnitCategory), typeof (Unit));
69ba56
             foreach (var u in category.get_units ())
69ba56
             {
69ba56
                 to_model.append (out iter);
69ba56
                 to_model.set (iter, 0, u.display_name, 1, category, 2, u, -1);
69ba56
             }
69ba56
             to_combo.model = to_model;
69ba56
 
69ba56
             /* Select the first possible unit */
69ba56
             to_combo.set_active (0);
69ba56
         }
69ba56
     }
69ba56
 
69ba56
     [GtkCallback]
69ba56
     private void to_combobox_changed_cb ()
69ba56
     {
69ba56
         /* Conversion must have changed */
69ba56
         update_result_label ();
69ba56
         changed ();
69ba56
     }
69ba56
 
69ba56
     private void from_cell_data_func (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel tree_model, Gtk.TreeIter iter)
69ba56
     {
69ba56
         cell.set ("sensitive", !tree_model.iter_has_child (iter));
69ba56
     }
69ba56
 
69ba56
     [GtkCallback]
69ba56
     private void swap_button_clicked_cb ()
69ba56
     {
69ba56
+        Unit? from_unit, to_unit;
69ba56
+        get_conversion (out from_unit, out to_unit);
69ba56
+
69ba56
+        set_active_unit (from_combo, null, to_unit);
69ba56
+        set_active_unit (to_combo, null, from_unit);
69ba56
+
69ba56
+        do_convert(out from_unit, out to_unit);
69ba56
+
69ba56
+        update_result_label ();
69ba56
+    }
69ba56
+
69ba56
+    private void do_convert (out Unit? from_unit, out Unit? to_unit) {
69ba56
         var x = equation.number;
69ba56
         if (x != null)
69ba56
         {
69ba56
-            var z = convert_equation (x);
69ba56
-            if (z != null)
69ba56
-                equation.set_number (z);
69ba56
+            var z = convert_equation (x, out from_unit, out to_unit);
69ba56
+            if (z != null && from_unit != null && to_unit != null)
69ba56
+            {
69ba56
+                equation.set ("%s %s %s %s".printf(equation.serializer.to_string (x), from_unit.display_name, _("in"), to_unit.display_name));
69ba56
+                equation.solve ();
69ba56
+            }
69ba56
         }
69ba56
+    }
69ba56
 
69ba56
-        Unit from_unit, to_unit;
69ba56
-        get_conversion (out from_unit, out to_unit);
69ba56
-        set_active_unit (from_combo, null, to_unit);
69ba56
-        set_active_unit (to_combo, null, from_unit);
69ba56
+    [GtkCallback]
69ba56
+    private void convert_button_clicked_cb ()
69ba56
+    {
69ba56
+        Unit? from_unit, to_unit;
69ba56
+        do_convert (out from_unit, out to_unit);
69ba56
 
69ba56
         update_result_label ();
69ba56
     }
69ba56
 
69ba56
-    private Number? convert_equation (Number x)
69ba56
+    private Number?  convert_equation (Number x,
69ba56
+                                       out Unit? source_unit,
69ba56
+                                       out Unit? target_unit)
69ba56
     {
69ba56
         Gtk.TreeIter from_iter, to_iter;
69ba56
         if (!from_combo.get_active_iter (out from_iter))
69ba56
             return null;
69ba56
         if (!to_combo.get_active_iter (out to_iter))
69ba56
             return null;
69ba56
-
69ba56
         UnitCategory category;
69ba56
-        Unit source_unit, target_unit;
69ba56
         from_combo.model.get (from_iter, 1, out category, 2, out source_unit, -1);
69ba56
         to_combo.model.get (to_iter, 2, out target_unit, -1);
69ba56
 
69ba56
         return category.convert (x, source_unit, target_unit);
69ba56
-    }
69ba56
+  }
69ba56
 }
69ba56
diff --git a/src/math-display.vala b/src/math-display.vala
69ba56
index 4b1a17c8..01156b0c 100644
69ba56
--- a/src/math-display.vala
69ba56
+++ b/src/math-display.vala
69ba56
@@ -1,60 +1,60 @@
69ba56
 /*
69ba56
  * Copyright (C) 2008-2012 Robert Ancell
69ba56
  *
69ba56
  * This program is free software: you can redistribute it and/or modify it under
69ba56
  * the terms of the GNU General Public License as published by the Free Software
69ba56
  * Foundation, either version 3 of the License, or (at your option) any later
69ba56
  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
69ba56
  * license.
69ba56
  */
69ba56
 
69ba56
 public class MathDisplay : Gtk.Viewport
69ba56
 {
69ba56
     /* Equation being displayed */
69ba56
     private MathEquation _equation;
69ba56
     public MathEquation equation { get { return _equation; } }
69ba56
     private HistoryView history;
69ba56
 
69ba56
     /* Display widget */
69ba56
     Gtk.SourceView source_view;
69ba56
 
69ba56
     /* Buffer that shows errors etc */
69ba56
     Gtk.TextBuffer info_buffer;
69ba56
 
69ba56
     /* Spinner widget that shows if we're calculating a response */
69ba56
     Gtk.Spinner spinner;
69ba56
 
69ba56
     public MathDisplay (MathEquation equation)
69ba56
     {
69ba56
         _equation = equation;
69ba56
-        _equation.history_signal.connect (this.handler);
69ba56
+        _equation.history_signal.connect (this.update_history);
69ba56
         var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
69ba56
         add (main_box);
69ba56
 
69ba56
         history = new HistoryView ();
69ba56
         history.answer_clicked.connect ((ans) => { insert_text (ans); });
69ba56
         history.equation_clicked.connect ((eq) => { display_text (eq); });
69ba56
         main_box.add (history);
69ba56
         main_box.show_all ();
69ba56
 
69ba56
         var scrolled_window = new Gtk.ScrolledWindow (null, null);
69ba56
         var style_context = scrolled_window.get_style_context ();
69ba56
         style_context.add_class ("display-scrolled");
69ba56
 
69ba56
         scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER);
69ba56
         source_view = new Gtk.SourceView.with_buffer (equation);
69ba56
         source_view.set_accepts_tab (false);
69ba56
         source_view.set_left_margin (14);
69ba56
         source_view.set_pixels_above_lines (8);
69ba56
         source_view.set_pixels_below_lines (2);
69ba56
         source_view.set_justification (Gtk.Justification.LEFT);
69ba56
 
69ba56
         source_view.set_name ("displayitem");
69ba56
         source_view.set_size_request (20, 20);
69ba56
         source_view.get_accessible ().set_role (Atk.Role.EDITBAR);
69ba56
         //FIXME:<property name="AtkObject::accessible-description" translatable="yes" comments="Accessible description for the area in which results are displayed">Result Region</property>
69ba56
         source_view.key_press_event.connect (key_press_cb);
69ba56
         create_autocompletion ();
69ba56
 
69ba56
         main_box.pack_start (scrolled_window, false, false, 0);
69ba56
         scrolled_window.add (source_view); /* Adds ScrolledWindow to source_view for displaying long equations */
69ba56
@@ -67,75 +67,71 @@ public class MathDisplay : Gtk.Viewport
69ba56
         info_view.set_wrap_mode (Gtk.WrapMode.WORD);
69ba56
         info_view.set_can_focus (false);
69ba56
         info_view.set_editable (false);
69ba56
         info_view.set_left_margin (12);
69ba56
         info_view.set_right_margin (12);
69ba56
         info_box.pack_start (info_view, true, true, 0);
69ba56
         info_buffer = info_view.get_buffer ();
69ba56
 
69ba56
         style_context = info_view.get_style_context ();
69ba56
         style_context.add_class ("info-view");
69ba56
 
69ba56
         spinner = new Gtk.Spinner ();
69ba56
         info_box.pack_end (spinner, false, false, 0);
69ba56
 
69ba56
         info_box.show ();
69ba56
         info_view.show ();
69ba56
         source_view.show ();
69ba56
         main_box.show ();
69ba56
 
69ba56
         equation.notify["status"].connect ((pspec) => { status_changed_cb (); });
69ba56
         status_changed_cb ();
69ba56
 
69ba56
         equation.notify["error-token-end"].connect ((pspec) => { error_status_changed_cb (); });
69ba56
     }
69ba56
 
69ba56
     public void grabfocus () /* Editbar grabs focus when an instance of gnome-calculator is created */
69ba56
     {
69ba56
         source_view.grab_focus ();
69ba56
     }
69ba56
 
69ba56
-    public void handler (string answer, Number number, int number_base, uint representation_base)
69ba56
+    public void update_history (string answer, Number number, int number_base, uint representation_base)
69ba56
     {
69ba56
-        this.update_history (answer, number, number_base, representation_base); /* Recieves signal emitted by a MathEquation object for updating history-view */
69ba56
+        /* Recieves signal emitted by a MathEquation object for updating history-view */
69ba56
+        history.insert_entry (answer, number, number_base, representation_base); /* Sends current equation and answer for updating History-View */
69ba56
     }
69ba56
 
69ba56
     public void display_text (string prev_eq)
69ba56
     {
69ba56
         _equation.display_selected (prev_eq);
69ba56
     }
69ba56
 
69ba56
-    public void update_history (string answer, Number number, int number_base, uint representation_base)
69ba56
-    {
69ba56
-        history.insert_entry (answer, number, number_base, representation_base); /* Sends current equation and answer for updating History-View */
69ba56
-    }
69ba56
-
69ba56
     public void clear_history ()
69ba56
     {
69ba56
         history.clear ();
69ba56
     }
69ba56
 
69ba56
     public void insert_text (string answer)
69ba56
     {
69ba56
         _equation.insert_selected (answer);
69ba56
     }
69ba56
 
69ba56
     private void create_autocompletion ()
69ba56
     {
69ba56
         Gtk.SourceCompletion completion = source_view.get_completion ();
69ba56
         try
69ba56
         {
69ba56
             completion.add_provider (new FunctionCompletionProvider ());
69ba56
             completion.add_provider (new VariableCompletionProvider (equation));
69ba56
         }
69ba56
         catch (Error e)
69ba56
         {
69ba56
             warning ("Could not add CompletionProvider to source-view");
69ba56
         }
69ba56
     }
69ba56
 
69ba56
     private bool function_completion_window_visible ()
69ba56
     {
69ba56
         unowned List<Gtk.SourceCompletionProvider> providers_list = source_view.get_completion ().get_providers ();
69ba56
         if (providers_list.length () > 0)
69ba56
         {
69ba56
             MathFunction[] functions = FunctionCompletionProvider.get_matches_for_completion_at_cursor (equation);
69ba56
-- 
69ba56
2.31.1
69ba56