[gnome-calculator] History View



commit e5c307320db02e0f22b6e61e3b40109a1668a506
Author: elitalobo <loboelita gmail com>
Date:   Mon Aug 18 21:04:04 2014 +0530

    History View
    
    Signed-off-by: PioneerAxon <arth svnit gmail com>

 src/Makefile.am        |    1 +
 src/math-display.vala  |   36 +++++++++-
 src/math-equation.vala |   13 +++-
 src/math-history.vala  |  185 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/math-window.vala   |    1 +
 5 files changed, 232 insertions(+), 4 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 28ff7c8..d8a408e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -38,6 +38,7 @@ gnome_calculator_SOURCES = \
        math-function.vala \
        function-manager.vala \
        math-function-popup.vala \
+       math-history.vala \
        $(BUILT_SOURCES)
 
 gnome_calculator_VALAFLAGS = \
diff --git a/src/math-display.vala b/src/math-display.vala
index aca0485..fc10f8b 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -13,6 +13,7 @@ public class MathDisplay : Gtk.Viewport
     /* Equation being displayed */
     private MathEquation _equation;
     public MathEquation equation { get { return _equation; } }
+    private HistoryView history;
 
     /* Display widget */
     Gtk.SourceView source_view;
@@ -26,10 +27,12 @@ public class MathDisplay : Gtk.Viewport
     public MathDisplay (MathEquation equation)
     {
         _equation = equation;
-
+        _equation.history_signal.connect (this.handler);
         var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
         add (main_box);
-
+        history = new HistoryView (this, main_box);
+        var scrolled_window = new Gtk.ScrolledWindow (null, null);
+        scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER);
         source_view = new Gtk.SourceView.with_buffer (equation);
         source_view.set_accepts_tab (false);
         source_view.set_pixels_above_lines (8);
@@ -46,7 +49,9 @@ public class MathDisplay : Gtk.Viewport
         source_view.key_press_event.connect (key_press_cb);
         create_autocompletion ();
 
-        main_box.pack_start (source_view, true, true, 0);
+        main_box.pack_start (scrolled_window, true, true, 0);
+        scrolled_window.add (source_view); /* Adds ScrolledWindow to source_view for displaying long 
equations */
+        scrolled_window.show ();
 
         var info_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
         main_box.pack_start (info_box, false, true, 0);
@@ -75,6 +80,31 @@ public class MathDisplay : Gtk.Viewport
         equation.notify["error-token-end"].connect ((pspec) => { error_status_changed_cb (); });
     }
 
+    public void grabfocus () /* Editbar grabs focus when an instance of gnome-calculator is created */
+    {
+        source_view.grab_focus ();
+    }
+
+    public void handler (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 */
+    }
+
+    public void display_text (string prev_eq)
+    {
+        _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 insert_text (string answer)
+    {
+        _equation.insert_selected (answer);
+    }
+
     private void create_autocompletion ()
     {
         Gtk.SourceCompletion completion = source_view.get_completion ();
diff --git a/src/math-equation.vala b/src/math-equation.vala
index 7169970..8eade41 100644
--- a/src/math-equation.vala
+++ b/src/math-equation.vala
@@ -118,6 +118,7 @@ public class MathEquation : Gtk.SourceBuffer
         }
     }
 
+    public signal void history_signal (string answer, Number number, int number_base, uint 
representation_base); /*signal to be emitted when a new calculation is tp be entered in history-view */
     private AngleUnit _angle_units;  /* Units for trigonometric functions */
     private NumberMode _number_mode;   /* ??? */
     private bool can_super_minus; /* true if entering minus can generate a superscript minus */
@@ -199,6 +200,11 @@ public class MathEquation : Gtk.SourceBuffer
         ans_tag = create_tag (null, "weight", Pango.Weight.BOLD, null);
     }
 
+    public void display_selected (string selected)
+    {
+        set_text (selected, -1);
+    }
+
     private void get_ans_offsets (out int start, out int end)
     {
         if (ans_start_mark == null)
@@ -760,7 +766,7 @@ public class MathEquation : Gtk.SourceBuffer
 
         if (representation_base != 0)
             serializer.set_representation_base (serializer.get_base ());
-
+        this.history_signal (get_current_state ().expression, x, serializer.get_base(), 
representation_base); /*emits signal to enter a new entry into history-view */
         set_text (text, -1);
         state.ans = x;
 
@@ -797,6 +803,11 @@ public class MathEquation : Gtk.SourceBuffer
         insert_at_cursor (text, -1);
     }
 
+    public void insert_selected (string answer)
+    {
+        insert (answer);
+    }
+
     public new void insert_square ()
     {
         var space_required = false;
diff --git a/src/math-history.vala b/src/math-history.vala
new file mode 100644
index 0000000..3d24680
--- /dev/null
+++ b/src/math-history.vala
@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) 2014 ELITA ASTRID ANGELINA LOBO
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+*/
+
+public class HistoryView : Gtk.Box
+{
+    int no_ofitems = 0; /* No of entries in history-view listbox */
+    Gtk.ScrolledWindow scroll_window;
+    Gtk.ListBox listbox;
+    Gtk.Box main_box;
+    private MathDisplay _display;
+    public MathDisplay display { get { return _display; } }
+    /* Creates a History-View box */
+    public HistoryView (MathDisplay display, Gtk.Box box)
+    {
+        _display = display;
+        main_box = box;
+        listbox = new Gtk.ListBox ();
+        listbox.set_selection_mode (Gtk.SelectionMode.NONE);
+        listbox.set_border_width (5);
+        scroll_window = new Gtk.ScrolledWindow (null, null);
+        scroll_window.set_shadow_type (Gtk.ShadowType.ETCHED_OUT);
+        scroll_window.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+        scroll_window.set_placement (Gtk.CornerType.TOP_LEFT);
+        scroll_window.add (listbox);
+        scroll_window.set_size_request (100, 100);
+        scroll_window.size_allocate.connect (scroll_bottom);
+        main_box.add (scroll_window);
+        main_box.show_all ();
+    }
+
+    public void scroll_bottom ()
+    {   /* Scrolls to the last entered history-view entry */
+        Gtk.Adjustment adjustment = scroll_window.get_vadjustment ();
+        adjustment.set_value (adjustment.get_upper () - adjustment.get_page_size ());
+    }
+
+    public void insert_entry (string equation, Number answer, int number_base, uint representation_base)
+    {   /* Inserts a new entry into the history-view listbox */
+        string prev_eq = equation;
+        Serializer _serializer = new Serializer (DisplayFormat.AUTOMATIC, number_base, 9);
+        _serializer.set_representation_base (representation_base);
+        string ans = _serializer.to_string (answer);
+        bool check = check_history (prev_eq, ans);
+        if (check == false)
+        {
+            var entry = new HistoryEntryView (prev_eq, answer, _display, number_base, representation_base);
+            if (entry != null)
+            {
+                listbox.add (entry);
+                entry.show ();
+                no_ofitems = no_ofitems + 1;
+            }
+        }
+    }
+
+    public bool check_history (string equation, string answer)
+    {   /* Checks if the last inserted calculation is the same as the current calculation to be inserted in 
history-view */
+        if (no_ofitems == 0)
+        {
+            return false; /* returns false for the first entry */
+        }
+        string current_equation = equation;
+        string ans = answer;
+        Gtk.ListBoxRow row = (listbox.get_row_at_index (no_ofitems - 1) as Gtk.ListBoxRow);
+        Gtk.Box box = (row.get_child () as Gtk.Box);
+        if (box != null)
+        {
+            Gtk.EventBox ans_eventbox = box.get_children ().nth_data (1) as Gtk.EventBox;
+            string prev_ans = (ans_eventbox.get_child () as Gtk.Label).get_tooltip_text (); /* retrieves 
previous equation */
+            Gtk.EventBox eq_eventbox = box.get_children ().nth_data (0) as Gtk.EventBox;
+            string prev_equation = (eq_eventbox.get_child () as Gtk.Label).get_tooltip_text (); /* retrieves 
previous answer */
+
+            if ((no_ofitems >= 1) && (prev_ans == ans) && (current_equation == prev_equation))
+                return true; /* returns true if last entered equation and answer is the same as the current 
equation and answer */
+        }
+        return false;
+    }
+}
+
+public class HistoryEntryView : Gtk.ListBoxRow
+{   /* Creates a new history-view entry */
+    private MathDisplay _display;
+    public MathDisplay display { get { return _display; } }
+    private Number answer; /* Stores answer in Number object */
+    private string prev_equation; /* Stores equation to be entered in history-view */
+    private string prev_answer; /* Stores answer to be entered in history-view */
+    Gtk.Label equation_label;
+    Gtk.Label answer_label;
+    Gtk.EventBox eq_eventbox;
+    Gtk.EventBox ans_eventbox;
+
+    public HistoryEntryView (string equation, Number number, MathDisplay display, int number_base, uint 
representation_base)
+    {
+        _display = display;
+        answer = number;
+        prev_equation = equation;
+        Serializer _serializer = new Serializer (DisplayFormat.AUTOMATIC, number_base, 9);
+        Serializer ans_serializer = new Serializer (DisplayFormat.AUTOMATIC, number_base, 4);
+        ans_serializer.set_representation_base (representation_base);
+        _serializer.set_representation_base (representation_base);
+        prev_answer = _serializer.to_string (answer);
+        string answer_text = ans_serializer.to_string (answer);
+        Gtk.Grid grid = new Gtk.Grid ();
+        grid.insert_column (0);
+        grid.insert_column (1);
+        grid.insert_column (2);
+        grid.insert_column (3);
+        grid.insert_row (0);
+        grid.set_column_homogeneous (true);
+        add (grid);
+        equation_label = new Gtk.Label ("");
+        answer_label = new Gtk.Label ("");
+        eq_eventbox = new Gtk.EventBox ();
+        ans_eventbox = new Gtk.EventBox ();
+        eq_eventbox.add (equation_label);
+        ans_eventbox.add (answer_label);
+        eq_eventbox.set_events (Gdk.EventMask.BUTTON_PRESS_MASK);
+        ans_eventbox.set_events (Gdk.EventMask.BUTTON_PRESS_MASK);
+        eq_eventbox.button_press_event.connect (onclick_equation); /* Calls onclick_equation on clicking on 
equation_label */
+        ans_eventbox.button_press_event.connect (onclick_answer); /* Calls onclick_answer on clicking on 
answer_label */
+        equation_label.set_size_request (10, 10);
+        answer_label.set_size_request (10, 10);
+        equation_label.set_selectable (true);
+        answer_label.set_selectable (true);
+        eq_eventbox.set_above_child (true);
+        ans_eventbox.set_above_child (true);
+        equation_label.set_tooltip_text (prev_equation); /* Sets tooltip for equation_label */
+        answer_label.set_tooltip_text (prev_answer); /* Sets tooltip for answer_label */
+        equation_label.set_ellipsize (Pango.EllipsizeMode.END); /* Ellipsizes the equation when its size is 
greater than the size of the             equation_label */
+        answer_label.set_ellipsize (Pango.EllipsizeMode.END); /* Elipsizes the answer when its size is 
greater the than size of answer_label */
+        equation_label.set_text (prev_equation);
+        string final_answer = "= " + answer_text;
+        answer_label.set_text (final_answer);
+        grid.attach (eq_eventbox, 0, 0, 3, 1);
+        grid.attach (ans_eventbox, 3, 0, 1, 1);
+        equation_label.set_alignment (0, 0); /* Aligns equation on equation_label to the left */
+        answer_label.set_alignment (0, 0); /* Aligns answer on answer_label to the left */
+        Pango.AttrList list = new Pango.AttrList ();
+        Pango.FontDescription font = new Pango.FontDescription ();
+        font.set_weight (Pango.Weight.BOLD);
+        Pango.Attribute weight = Pango.attr_weight_new (Pango.Weight.BOLD);
+        list.insert ((owned) weight);
+        answer_label.set_attributes (list); /* Sets font weight of the text on answer_label to BOLD */
+        grid.show_all ();
+    }
+
+    public bool onclick_answer (Gtk.Widget widget, Gdk.EventButton eventbutton)
+    {   /* Callback function for button-press-event on ans_eventbox */
+        Gtk.EventBox event = (Gtk.EventBox) widget;
+        if (event != null)
+        {
+            Gtk.Label ans_label = (event.get_child () as Gtk.Label);
+            string prev_ans = ans_label.get_tooltip_text ();
+            if (prev_ans  != null)
+            {
+                _display.insert_text (prev_ans); /* Replaces current equation by selected equation from 
history-view */
+            }
+        }
+        return true;
+    }
+
+    private bool onclick_equation (Gtk.Widget widget, Gdk.EventButton eventbutton)
+    {   /* Callback function for button-press-event on eq_eventbox */
+        Gtk.EventBox event = (Gtk.EventBox) widget;
+        if (event != null)
+        {
+            Gtk.Label equation_label = (event.get_child () as Gtk.Label);
+            string prev_equation = equation_label.get_text ();
+            if (prev_equation != null)
+            {
+                _display.display_text (prev_equation); /* Appends selected answer from history-view to 
current equation in editbar */
+            }
+        }
+        return true;
+    }
+}
+
+
diff --git a/src/math-window.vala b/src/math-window.vala
index 9b07786..9690807 100644
--- a/src/math-window.vala
+++ b/src/math-window.vala
@@ -103,6 +103,7 @@ public class MathWindow : Gtk.ApplicationWindow
         scrolled_window.show ();
 
         _display = new MathDisplay (equation);
+        _display.grabfocus ();
         scrolled_window.add (display);
         display.show ();
 


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