[gnome-calculator/wip/glade: 4/4] Migrate HistoryEntry to use a GtkBuilder template
- From: Alberto Ruiz <aruiz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/wip/glade: 4/4] Migrate HistoryEntry to use a GtkBuilder template
- Date: Sun, 17 Jan 2016 03:51:48 +0000 (UTC)
commit 237b1dcd1d1863be910a649e3b6c55c93e1ed249
Author: Alberto Ruiz <aruiz gnome org>
Date: Sun Jan 17 03:09:44 2016 +0000
Migrate HistoryEntry to use a GtkBuilder template
data/gnome-calculator.gresource.xml | 1 +
data/history-entry.ui | 48 ++++++++++
src/math-history.vala | 173 +++++++++++------------------------
3 files changed, 104 insertions(+), 118 deletions(-)
---
diff --git a/data/gnome-calculator.gresource.xml b/data/gnome-calculator.gresource.xml
index 8d33a98..15b681c 100644
--- a/data/gnome-calculator.gresource.xml
+++ b/data/gnome-calculator.gresource.xml
@@ -6,6 +6,7 @@
<file preprocess="xml-stripblanks">buttons-financial.ui</file>
<file preprocess="xml-stripblanks">buttons-programming.ui</file>
<file preprocess="xml-stripblanks">history-view.ui</file>
+ <file preprocess="xml-stripblanks">history-entry.ui</file>
<file preprocess="xml-stripblanks">math-converter.ui</file>
<file preprocess="xml-stripblanks">math-window.ui</file>
<file preprocess="xml-stripblanks">menu.ui</file>
diff --git a/data/history-entry.ui b/data/history-entry.ui
new file mode 100644
index 0000000..4070bd5
--- /dev/null
+++ b/data/history-entry.ui
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.19.0 -->
+<interface>
+ <requires lib="gtk+" version="3.16"/>
+ <template class="HistoryEntry" parent="GtkListBoxRow">
+ <child>
+ <object class="GtkGrid" id="grid">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="column_homogeneous">True</property>
+ <child>
+ <object class="GtkLabel" id="equation_label">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="events">GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK</property>
+ <property name="xalign">0.0</property>
+ <property name="yalign">0.0</property>
+ <property name="ellipsize">end</property>
+ <signal name="button-press-event" handler="equation_clicked_cb" swapped="no"/>
+ </object>
+ <packing>
+ <property name="left_attach">0</property>
+ <property name="top_attach">0</property>
+ <property name="width">3</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkLabel" id="answer_label">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="events">GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK</property>
+ <property name="xalign">0.0</property>
+ <property name="yalign">0.0</property>
+ <property name="ellipsize">end</property>
+ <attributes>
+ <attribute name="weight" value="bold"/>
+ </attributes>
+ <signal name="button-press-event" handler="answer_clicked_cb" swapped="no"/>
+ </object>
+ <packing>
+ <property name="left_attach">3</property>
+ <property name="top_attach">0</property>
+ </packing>
+ </child>
+ </object>
+ </child>
+ </template>
+</interface>
diff --git a/src/math-history.vala b/src/math-history.vala
index 8fbdb23..c0900b0 100644
--- a/src/math-history.vala
+++ b/src/math-history.vala
@@ -13,12 +13,19 @@ public class HistoryView : Gtk.ScrolledWindow
{
int no_ofitems = 0; /* No of entries in history-view listbox */
+ string? last_answer = null;
+ string? last_equation = null;
+
+ Serializer serializer_four = new Serializer (DisplayFormat.AUTOMATIC, 10, 4);
+ Serializer serializer_nine = new Serializer (DisplayFormat.AUTOMATIC, 10, 9);
+
[GtkChild]
Gtk.ListBox listbox;
public signal void answer_clicked (string ans);
public signal void equation_clicked (string equation);
+
[GtkCallback]
public void scroll_bottom ()
{
@@ -27,141 +34,71 @@ public class HistoryView : Gtk.ScrolledWindow
}
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, number_base, representation_base);
- if (entry != null)
- {
- listbox.add (entry);
- entry.show ();
- no_ofitems = no_ofitems + 1;
-
- entry.answer_clicked.connect ((ans) => { this.answer_clicked (ans); });
- entry.equation_clicked.connect ((eq) => { this.equation_clicked (eq); });
- }
- }
- }
+ {
+
+ serializer_four.set_base (number_base);
+ serializer_nine.set_base (number_base);
+
+ serializer_four.set_representation_base (representation_base);
+ serializer_nine.set_representation_base (representation_base);
+
+ var answer_nine_digits = serializer_nine.to_string (answer);
+ var answer_four_digits = serializer_four.to_string (answer);
+
+ if (last_answer == answer_nine_digits && last_equation == equation)
+ return;
+
+ var entry = new HistoryEntry (equation, answer_four_digits, answer_nine_digits);
- 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.Grid grid = (row.get_child () as Gtk.Grid);
- if (grid != null)
- {
- Gtk.EventBox ans_eventbox = grid.get_children ().nth_data (0) as Gtk.EventBox;
- string prev_ans = (ans_eventbox.get_child () as Gtk.Label).get_tooltip_text (); /* retrieves
previous equation */
- Gtk.EventBox eq_eventbox = grid.get_children ().nth_data (1) 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;
+ listbox.add (entry);
+ entry.show ();
+ no_ofitems++;
+
+ entry.answer_clicked.connect ((ans) => { this.answer_clicked (ans); });
+ entry.equation_clicked.connect ((eq) => { this.equation_clicked (eq); });
+
+ last_answer = answer_nine_digits;
+ last_equation = equation;
}
}
-public class HistoryEntryView : Gtk.ListBoxRow
+[GtkTemplate (ui = "/org/gnome/calculator/history-entry.ui")]
+public class HistoryEntry : Gtk.ListBoxRow
{
- 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 */
+ [GtkChild]
Gtk.Label equation_label;
+ [GtkChild]
Gtk.Label answer_label;
- Gtk.EventBox eq_eventbox;
- Gtk.EventBox ans_eventbox;
public signal void answer_clicked (string ans);
public signal void equation_clicked (string equation);
- public HistoryEntryView (string equation, Number number, int number_base, uint representation_base)
+ public HistoryEntry (string equation,
+ string answer_four_digits,
+ string answer_nine_digits)
{
- 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 ();
+ equation_label.set_tooltip_text (equation);
+ answer_label.set_tooltip_text (answer_nine_digits);
+
+ equation_label.set_text (equation);
+ answer_label.set_text ("= " + answer_four_digits);
}
- 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)
- answer_clicked (prev_ans);
- }
+ [GtkCallback]
+ public bool answer_clicked_cb (Gtk.Widget widget, Gdk.EventButton eventbutton)
+ {
+ var answer = answer_label.get_tooltip_text ();
+ if (answer != null)
+ answer_clicked (answer);
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)
- equation_clicked (prev_equation);
- }
+ [GtkCallback]
+ private bool equation_clicked_cb (Gtk.Widget widget, Gdk.EventButton eventbutton)
+ {
+ var equation = equation_label.get_text ();
+ if (equation != null)
+ equation_clicked (equation);
return true;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]