[gcalctool/vala] Use more properties



commit fe0ef8bf06d8f0e6b4fc6683e13c06b7cf577e3d
Author: Robert Ancell <robert ancell canonical com>
Date:   Sat Oct 13 21:02:58 2012 +1300

    Use more properties

 src/math-buttons.vala        |    9 +-
 src/math-converter.vala      |    4 +-
 src/math-display.vala        |    4 +-
 src/math-equation.vala       |  168 +++++++++++++++++++++--------------------
 src/math-variable-popup.vala |   26 +++---
 5 files changed, 107 insertions(+), 104 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index ed539c8..102eedf 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -141,7 +141,7 @@ public class MathButtons : Gtk.Box
         if (bit_panel == null)
             return;
 
-        var x = equation.get_number ();
+        var x = equation.number;
 
         uint64 bits = 0;
         var enabled = x != null;
@@ -465,8 +465,7 @@ public class MathButtons : Gtk.Box
         button = builder.get_object ("calc_numeric_point_button") as Gtk.Button;
         if (button != null)
         {
-            var serializer = equation.get_serializer ();
-            button.set_label (serializer.get_radix ().to_string ());
+            button.set_label (equation.serializer.get_radix ().to_string ());
             button.clicked.connect (() => { equation.insert_numeric_point (); });
         }
 
@@ -972,7 +971,7 @@ public class MathButtons : Gtk.Box
         if (button.get_active ())
         {
             equation.number_mode = NumberMode.SUPERSCRIPT;
-            if (!equation.get_has_selection ())
+            if (!equation.has_selection)
                 remove_trailing_spaces ();
         }
         else if (equation.number_mode == NumberMode.SUPERSCRIPT)
@@ -986,7 +985,7 @@ public class MathButtons : Gtk.Box
         if (button.get_active ())
         {
             equation.number_mode = NumberMode.SUBSCRIPT;
-            if (!equation.get_has_selection ())
+            if (!equation.has_selection)
                 remove_trailing_spaces ();
         }
         else if (equation.number_mode == NumberMode.SUBSCRIPT)
diff --git a/src/math-converter.vala b/src/math-converter.vala
index 864fa76..15eba6f 100644
--- a/src/math-converter.vala
+++ b/src/math-converter.vala
@@ -125,7 +125,7 @@ public class MathConverter : Gtk.Box
 
     private void update_result_label ()
     {
-        var x = equation.get_number ();
+        var x = equation.number;
         if (x == null)
             return;
 
@@ -245,7 +245,7 @@ public class MathConverter : Gtk.Box
 
     private void swap_button_clicked_cb ()
     {
-        var x = equation.get_number ();
+        var x = equation.number;
         if (x != null)
         {
             var z = convert_equation (x);
diff --git a/src/math-display.vala b/src/math-display.vala
index 256cd73..5c2884d 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -323,12 +323,12 @@ public class MathDisplay : Gtk.Viewport
     private void status_changed_cb ()
     {
         info_buffer.set_text (equation.status, -1);
-        if (equation.get_in_solve () && !spinner.get_visible ())
+        if (equation.in_solve && !spinner.get_visible ())
         {
             spinner.show ();
             spinner.start ();
         }
-        else if (!equation.get_in_solve () && spinner.get_visible ())
+        else if (!equation.in_solve && spinner.get_visible ())
         {
             spinner.hide ();
             spinner.stop ();
diff --git a/src/math-equation.vala b/src/math-equation.vala
index 5f26972..6a94b0f 100644
--- a/src/math-equation.vala
+++ b/src/math-equation.vala
@@ -131,10 +131,23 @@ public class MathEquation : Gtk.TextBuffer
 
     private bool in_delete;
 
-    private bool in_solve;
+    private bool _in_solve;
+    public bool in_solve
+    {
+        get { return _in_solve; }
+    }
 
-    private MathVariables variables;
-    private Serializer serializer;
+    private MathVariables _variables;
+    public MathVariables variables
+    {
+        get { return _variables; }
+    }
+
+    private Serializer _serializer;
+    public Serializer serializer
+    {
+        get { return _serializer; }
+    }
 
     private AsyncQueue<SolveData> queue;
 
@@ -161,7 +174,7 @@ public class MathEquation : Gtk.TextBuffer
                 digits[i] = ds[i].get_char (0);
         }
 
-        variables = new MathVariables ();
+        _variables = new MathVariables ();
 
         state = new MathEquationState ();
         state.status = "";
@@ -172,7 +185,7 @@ public class MathEquation : Gtk.TextBuffer
         target_currency = "";
         source_units = "";
         target_units = "";
-        serializer = new Serializer (DisplayFormat.AUTOMATIC, 10, 9);
+        _serializer = new Serializer (DisplayFormat.AUTOMATIC, 10, 9);
         queue = new AsyncQueue<SolveData> ();
 
         state.ans = new Number.integer (0);
@@ -180,11 +193,6 @@ public class MathEquation : Gtk.TextBuffer
         ans_tag = create_tag (null, "weight", Pango.Weight.BOLD, null);
     }
 
-    public MathVariables get_variables ()
-    {
-        return variables;
-    }
-
     private void get_ans_offsets (out int start, out int end)
     {
         if (ans_start_mark == null)
@@ -594,65 +602,66 @@ public class MathEquation : Gtk.TextBuffer
         }
     }
 
-    public bool is_empty ()
+    public bool is_empty
     {
-        return get_char_count () == 0;
+        get { return get_char_count () == 0; }
     }
 
-    public bool is_result ()
+    public bool is_result
     {
-        return get_equation () == "ans";
+        get { return equation == "ans"; }
     }
 
-    public string get_equation ()
+    public string equation
     {
-        var text = display;
-        var eq_text = "";
-
-        var ans_start = -1, ans_end = -1;
-        if (ans_start_mark != null)
-            get_ans_offsets (out ans_start, out ans_end);
-        if (ans_start >= 0)
-            text = text.splice (text.index_of_nth_char (ans_start), text.index_of_nth_char (ans_end), "ans");
-
-        var last_is_digit = false;
-        var index = 0;
-        unichar c;
-        while (text.get_next_char (ref index, out c))
+        owned get
         {
-            var is_digit = c.isdigit ();
-            var next_is_digit = false;
-            unichar next_char;
-            var i = index;
-            if (text.get_next_char (ref i, out next_char))
-                next_is_digit = next_char.isdigit ();
-
-            /* Ignore thousands separators */
-            if (c == serializer.get_thousands_separator () && last_is_digit && next_is_digit)
-                ;
-            /* Substitute radix character */
-            else if (c == serializer.get_radix () && (last_is_digit || next_is_digit))
-                eq_text += ".";
-            else
-                eq_text += c.to_string ();
-
-            last_is_digit = is_digit;
-        }
+            var text = display;
+            var eq_text = "";
+
+            var ans_start = -1, ans_end = -1;
+            if (ans_start_mark != null)
+                get_ans_offsets (out ans_start, out ans_end);
+            if (ans_start >= 0)
+                text = text.splice (text.index_of_nth_char (ans_start), text.index_of_nth_char (ans_end), "ans");
+
+            var last_is_digit = false;
+            var index = 0;
+            unichar c;
+            while (text.get_next_char (ref index, out c))
+            {
+                var is_digit = c.isdigit ();
+                var next_is_digit = false;
+                unichar next_char;
+                var i = index;
+                if (text.get_next_char (ref i, out next_char))
+                    next_is_digit = next_char.isdigit ();
+
+                /* Ignore thousands separators */
+                if (c == serializer.get_thousands_separator () && last_is_digit && next_is_digit)
+                    ;
+                /* Substitute radix character */
+                else if (c == serializer.get_radix () && (last_is_digit || next_is_digit))
+                    eq_text += ".";
+                else
+                    eq_text += c.to_string ();
 
-        return eq_text;
-    }
+                last_is_digit = is_digit;
+            }
 
-    public Number? get_number ()
-    {
-        if (is_result ())
-            return get_answer ();
-        else
-            return serializer.from_string (get_equation ());
+            return eq_text;
+        }
     }
 
-    public Serializer get_serializer ()
+    public Number? number
     {
-        return serializer;
+        owned get
+        {
+            if (is_result)
+                return answer;
+            else
+                return serializer.from_string (equation);
+        }
     }
 
     public NumberMode number_mode
@@ -670,19 +679,14 @@ public class MathEquation : Gtk.TextBuffer
         }
     }
 
-    public bool get_in_solve ()
-    {
-        return in_solve;
-    }
-
-    public Number get_answer ()
+    public Number answer
     {
-        return state.ans;
+        get { return state.ans; }
     }
 
     public void store (string name)
     {
-        var t = get_number ();
+        var t = number;
         if (t == null)
             status = _("No sane value to store");
         else
@@ -719,7 +723,7 @@ public class MathEquation : Gtk.TextBuffer
     public new void insert (string text)
     {
         /* Replace ** with ^ (not on all keyboards) */
-        if (!get_has_selection () && text == "Ã" && state.entered_multiply)
+        if (!has_selection && text == "Ã" && state.entered_multiply)
         {
             Gtk.TextIter iter;
             get_iter_at_mark (out iter, get_insert ());
@@ -804,7 +808,7 @@ public class MathEquation : Gtk.TextBuffer
     {
         var solvedata = new SolveData ();
 
-        var text = get_equation ();
+        var text = equation;
         /* Count the number of brackets and automatically add missing closing brackets */
         var n_brackets = 0;
         for (var i = 0; text[i] != '\0'; i++)
@@ -882,7 +886,7 @@ public class MathEquation : Gtk.TextBuffer
         if (result == null)
             return true;
 
-        in_solve = false;
+        _in_solve = false;
 
         if (result.error == null)
             status = "";
@@ -903,18 +907,18 @@ public class MathEquation : Gtk.TextBuffer
         if (in_solve)
             return;
 
-        if (is_empty ())
+        if (is_empty)
             return;
 
         /* If showing a result return to the equation that caused it */
         // FIXME: Result may not be here due to solve (i.e. the user may have entered "ans")
-        if (is_result ())
+        if (is_result)
         {
             undo ();
             return;
         }
 
-        in_solve = true;
+        _in_solve = true;
 
         number_mode = NumberMode.NORMAL;
 
@@ -926,7 +930,7 @@ public class MathEquation : Gtk.TextBuffer
 
     private void* factorize_real ()
     {
-        var x = get_number ();
+        var x = number;
         var factors = x.factorize ();
 
         var text = "";
@@ -952,7 +956,7 @@ public class MathEquation : Gtk.TextBuffer
         if (in_solve)
             return;
 
-        var x = get_number ();
+        var x = number;
         if (x == null || !x.is_integer ())
         {
             /* Error displayed when trying to factorize a non-integer value */
@@ -960,7 +964,7 @@ public class MathEquation : Gtk.TextBuffer
             return;
         }
 
-        in_solve = true;
+        _in_solve = true;
 
         new Thread<void*> ("", factorize_real);
 
@@ -984,10 +988,10 @@ public class MathEquation : Gtk.TextBuffer
     public new void backspace ()
     {
         /* Can't delete empty display */
-        if (is_empty ())
+        if (is_empty)
             return;
 
-        if (get_has_selection ())
+        if (has_selection)
             delete_selection (false, false);
         else
         {
@@ -1006,7 +1010,7 @@ public class MathEquation : Gtk.TextBuffer
 
     public void shift (int count)
     {
-        var z = get_number ();
+        var z = number;
         if (z == null)
         {
             /* This message is displayed in the status bar when a bit shift operation is performed and the display does not contain a number */        
@@ -1019,7 +1023,7 @@ public class MathEquation : Gtk.TextBuffer
 
     public void toggle_bit (uint bit)
     {
-        var x = get_number ();
+        var x = number;
         var max = new Number.unsigned_integer (uint64.MAX);
         if (x == null || x.is_negative () || x.compare (max) > 0)
         {
@@ -1052,7 +1056,7 @@ public class MathEquation : Gtk.TextBuffer
         var c = text.get_char (0);
         int cursor;
         get ("cursor-position", out cursor, null);
-        if ((c.isdigit () || c == serializer.get_radix ()) && is_result () && cursor >= get_char_count ())
+        if ((c.isdigit () || c == serializer.get_radix ()) && is_result && cursor >= get_char_count ())
         {
             set_text ("", -1);
             clear_ans (false);
@@ -1134,7 +1138,7 @@ private class MEquation : Equation
         if (lower_name == "rand" || lower_name == "ans")
             return true;
 
-        return m_equation.get_variables ().get (name) != null;
+        return m_equation.variables.get (name) != null;
     }
 
     public override Number? get_variable (string name)
@@ -1144,15 +1148,15 @@ private class MEquation : Equation
         if (lower_name == "rand")
             return new Number.random ();
         else if (lower_name == "ans")
-            return m_equation.get_answer ();
+            return m_equation.answer;
         else
-            return m_equation.get_variables ().get (name);
+            return m_equation.variables.get (name);
     }
 
     public override void set_variable (string name, Number x)
     {
         /* FIXME: Don't allow writing to built-in variables, e.g. ans, rand, sin, ... */
-        m_equation.get_variables ().set (name, x);
+        m_equation.variables.set (name, x);
     }
 
     public override Number? convert (Number x, string x_units, string z_units)
diff --git a/src/math-variable-popup.vala b/src/math-variable-popup.vala
index 794fed7..05849a1 100644
--- a/src/math-variable-popup.vala
+++ b/src/math-variable-popup.vala
@@ -32,10 +32,10 @@ public class MathVariablePopup : Gtk.Window
         add (vbox);
         vbox.show ();
 
-        var names = equation.get_variables ().get_names ();
+        var names = equation.variables.get_names ();
         for (var i = 0; names[i] != null; i++)
         {
-            var value = equation.get_variables ().get (names[i]);
+            var value = equation.variables.get (names[i]);
             var entry = make_variable_entry (names[i], value, true);
             entry.show ();
             vbox.pack_start (entry, false, true, 0);
@@ -66,7 +66,7 @@ public class MathVariablePopup : Gtk.Window
         entry.show ();
         vbox.pack_end (entry, false, true, 0);
 
-        entry = make_variable_entry ("ans", equation.get_answer (), false);
+        entry = make_variable_entry ("ans", equation.answer, false);
         entry.show ();
         vbox.pack_end (entry, false, true, 0);
     }
@@ -99,11 +99,11 @@ public class MathVariablePopup : Gtk.Window
         if (name == "")
             return;
 
-        var z = equation.get_number ();
+        var z = equation.number;
         if (z != null)
-            equation.get_variables ().set (name, z);
-        else if (equation.is_result ())
-            equation.get_variables ().set (name, equation.get_answer ());
+            equation.variables.set (name, z);
+        else if (equation.is_result)
+            equation.variables.set (name, equation.answer);
         else
             warning ("Can't add variable %s, the display is not a number", name);
 
@@ -113,11 +113,11 @@ public class MathVariablePopup : Gtk.Window
     private void save_variable_cb (Gtk.Widget widget)
     {
         var name = widget.get_data<string> ("variable_name");
-        var z = equation.get_number ();
+        var z = equation.number;
         if (z != null)
-            equation.get_variables ().set (name, z);
-        else if (equation.is_result ())
-            equation.get_variables ().set (name, equation.get_answer ());
+            equation.variables.set (name, z);
+        else if (equation.is_result)
+            equation.variables.set (name, equation.answer);
         else
             warning ("Can't save variable %s, the display is not a number", name);
 
@@ -127,7 +127,7 @@ public class MathVariablePopup : Gtk.Window
     private void delete_variable_cb (Gtk.Widget widget)
     {
         var name = widget.get_data<string> ("variable_name");
-        equation.get_variables ().delete (name);
+        equation.variables.delete (name);
 
         widget.get_toplevel ().destroy ();
     }
@@ -139,7 +139,7 @@ public class MathVariablePopup : Gtk.Window
         string text;
         if (value != null)
         {
-            var value_text = equation.get_serializer ().to_string (value);
+            var value_text = equation.serializer.to_string (value);
             text = "<b>%s</b> = %s".printf (name, value_text);
         }
         else



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