From 3edf6664432fd65ad68b7e5b91de65eef7fcdb09 Mon Sep 17 00:00:00 2001 From: Robert Roth Date: Thu, 4 Oct 2018 23:41:03 +0300 Subject: [PATCH 4/5] 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 @@ -1,59 +1,63 @@ + 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 @@ -75,66 +75,64 @@ public class MathConverter : Gtk.Grid { Gtk.TreeIter child_iter; while (model.iter_children (out child_iter, iter)) iter = child_iter; from_combo.set_active_iter (iter); } return; } set_active_unit (from_combo, null, ua); set_active_unit (to_combo, null, ub); } public void get_conversion (out Unit from_unit, out Unit to_unit) { Gtk.TreeIter from_iter, to_iter; from_combo.get_active_iter (out from_iter); to_combo.get_active_iter (out to_iter); from_combo.get_model ().get (from_iter, 2, out from_unit, -1); to_combo.get_model ().get (to_iter, 2, out to_unit, -1); } private void update_result_label () { var x = equation.number; 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); to_label.set_text (target_text); } } private void update_from_model () { var from_model = new Gtk.TreeStore (3, typeof (string), typeof (UnitCategory), typeof (Unit)); if (category == null) { var categories = UnitManager.get_default ().get_categories (); foreach (var category in categories) { Gtk.TreeIter parent; from_model.append (out parent, null); from_model.set (parent, 0, category.display_name, 1, category, -1); foreach (var unit in category.get_units ()) { Gtk.TreeIter iter; from_model.append (out iter, parent); from_model.set (iter, 0, unit.display_name, 1, category, 2, unit, -1); } } } else { @@ -201,62 +199,79 @@ public class MathConverter : Gtk.Grid /* Set the to combobox to be the list of units can be converted to */ to_model = new Gtk.ListStore (3, typeof (string), typeof (UnitCategory), typeof (Unit)); foreach (var u in category.get_units ()) { to_model.append (out iter); to_model.set (iter, 0, u.display_name, 1, category, 2, u, -1); } to_combo.model = to_model; /* Select the first possible unit */ to_combo.set_active (0); } } [GtkCallback] private void to_combobox_changed_cb () { /* Conversion must have changed */ update_result_label (); changed (); } private void from_cell_data_func (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel tree_model, Gtk.TreeIter iter) { cell.set ("sensitive", !tree_model.iter_has_child (iter)); } [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 4b1a17c8..01156b0c 100644 --- a/src/math-display.vala +++ b/src/math-display.vala @@ -1,60 +1,60 @@ /* * Copyright (C) 2008-2012 Robert Ancell * * 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 3 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 MathDisplay : Gtk.Viewport { /* Equation being displayed */ private MathEquation _equation; public MathEquation equation { get { return _equation; } } private HistoryView history; /* Display widget */ Gtk.SourceView source_view; /* Buffer that shows errors etc */ Gtk.TextBuffer info_buffer; /* Spinner widget that shows if we're calculating a response */ Gtk.Spinner spinner; 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); history = new HistoryView (); history.answer_clicked.connect ((ans) => { insert_text (ans); }); history.equation_clicked.connect ((eq) => { display_text (eq); }); main_box.add (history); main_box.show_all (); var scrolled_window = new Gtk.ScrolledWindow (null, null); var style_context = scrolled_window.get_style_context (); style_context.add_class ("display-scrolled"); 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_left_margin (14); source_view.set_pixels_above_lines (8); source_view.set_pixels_below_lines (2); source_view.set_justification (Gtk.Justification.LEFT); source_view.set_name ("displayitem"); source_view.set_size_request (20, 20); source_view.get_accessible ().set_role (Atk.Role.EDITBAR); //FIXME:Result Region source_view.key_press_event.connect (key_press_cb); create_autocompletion (); main_box.pack_start (scrolled_window, false, false, 0); scrolled_window.add (source_view); /* Adds ScrolledWindow to source_view for displaying long equations */ @@ -67,75 +67,71 @@ public class MathDisplay : Gtk.Viewport info_view.set_wrap_mode (Gtk.WrapMode.WORD); info_view.set_can_focus (false); info_view.set_editable (false); info_view.set_left_margin (12); info_view.set_right_margin (12); info_box.pack_start (info_view, true, true, 0); info_buffer = info_view.get_buffer (); style_context = info_view.get_style_context (); style_context.add_class ("info-view"); spinner = new Gtk.Spinner (); info_box.pack_end (spinner, false, false, 0); info_box.show (); info_view.show (); source_view.show (); main_box.show (); equation.notify["status"].connect ((pspec) => { status_changed_cb (); }); status_changed_cb (); 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) + 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) { _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 (); } public void insert_text (string answer) { _equation.insert_selected (answer); } private void create_autocompletion () { Gtk.SourceCompletion completion = source_view.get_completion (); try { completion.add_provider (new FunctionCompletionProvider ()); completion.add_provider (new VariableCompletionProvider (equation)); } catch (Error e) { warning ("Could not add CompletionProvider to source-view"); } } private bool function_completion_window_visible () { unowned List providers_list = source_view.get_completion ().get_providers (); if (providers_list.length () > 0) { MathFunction[] functions = FunctionCompletionProvider.get_matches_for_completion_at_cursor (equation); -- 2.31.1