[gnome-calculator/wip-gtk4-port] VariablePopover uses the MathPopover class
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/wip-gtk4-port] VariablePopover uses the MathPopover class
- Date: Fri, 1 Oct 2021 11:57:54 +0000 (UTC)
commit 9614b82077b4ed07cf4c61cfae616c3da5786286
Author: Robert Roth <robert roth off gmail com>
Date: Fri Oct 1 14:57:42 2021 +0300
VariablePopover uses the MathPopover class
src/math-buttons.vala | 29 +++++++++--
src/math-function-popover.vala | 2 +-
src/math-popover.vala | 6 +--
src/math-variable-popover.vala | 111 +++++++++++++++++-----------------------
src/ui/math-variable-popover.ui | 2 +-
5 files changed, 76 insertions(+), 74 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 1648510d..97d86f9d 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -425,7 +425,12 @@ public class MathButtons : Gtk.Box
menu_button.menu_model = create_word_size_menu ();
menu_button = builder.get_object ("calc_memory_button") as Gtk.MenuButton;
if (menu_button != null)
- menu_button.popover = new MathVariablePopover (equation);
+ {
+ var model = new ListStore(typeof(MathVariable));
+ MathVariablePopover math_popover = new MathVariablePopover (equation, model, (a,b) =>
MathVariable.name_compare_func(a as MathVariable, b as MathVariable));
+ fill_variables_model (model, math_popover, equation);
+ menu_button.popover = math_popover;
+ }
menu_button = builder.get_object ("calc_function_button") as Gtk.MenuButton;
FunctionManager function_manager = FunctionManager.get_default_function_manager ();
@@ -433,7 +438,7 @@ public class MathButtons : Gtk.Box
{
var model = new ListStore(typeof(MathFunction));
MathFunctionPopover math_popover = new MathFunctionPopover (equation, model);
- build_functions_model (model, math_popover, function_manager);
+ fill_functions_model (model, math_popover, function_manager);
menu_button.popover = math_popover;
}
@@ -479,7 +484,7 @@ public class MathButtons : Gtk.Box
return panel;
}
- private ListStore build_functions_model (ListStore model, MathPopover<MathFunction> math_popover,
FunctionManager function_manager)
+ private void fill_functions_model (ListStore model, MathPopover<MathFunction> math_popover,
FunctionManager function_manager)
{
var names = function_manager.get_names ();
@@ -492,7 +497,23 @@ public class MathButtons : Gtk.Box
function_manager.function_added.connect (f=>math_popover.item_added_cb(f as MathFunction));
function_manager.function_edited.connect (f=>math_popover.item_edited_cb(f as MathFunction));
function_manager.function_deleted.connect (f=>math_popover.item_deleted_cb(f as MathFunction));
- return model;
+ }
+
+ private void fill_variables_model (ListStore model, MathPopover<MathVariable> math_popover, MathEquation
equation)
+ {
+ // Fill variable list
+ var names = equation.variables.get_names ();
+ for (var i = 0; names[i] != null; i++)
+ {
+ var value = equation.variables[names[i]];
+ math_popover.item_added_cb (new MathVariable(names[i], value));
+ }
+
+ math_popover.item_added_cb (new MathVariable ("rand", null));
+ // Listen for variable changes
+ equation.variables.variable_added.connect ((name, value) => math_popover.item_added_cb (new
MathVariable (name, value)));
+ equation.variables.variable_edited.connect ((name, value) => math_popover.item_edited_cb (new
MathVariable (name, value)));
+ equation.variables.variable_deleted.connect ((name) => math_popover.item_deleted_cb (new
MathVariable (name, null)));
}
private void converter_changed_cb ()
diff --git a/src/math-function-popover.vala b/src/math-function-popover.vala
index cea097a9..678a19e2 100644
--- a/src/math-function-popover.vala
+++ b/src/math-function-popover.vala
@@ -143,7 +143,7 @@ public class MathFunctionPopover : MathPopover<MathFunction>
}
- public override int get_item_index (MathFunction item)
+ protected override int get_item_index (MathFunction item)
{
uint position;
if (model.find_with_equal_func (item as Object, (a, b) => (MathFunction.equal_func(a as
MathFunction, b as MathFunction)), out position))
diff --git a/src/math-popover.vala b/src/math-popover.vala
index 11f9b44b..1fc3a5f9 100644
--- a/src/math-popover.vala
+++ b/src/math-popover.vala
@@ -36,13 +36,13 @@ public abstract class MathPopover<T> : Gtk.Popover
public void item_deleted_cb (T item)
{
- uint position = get_item_index (item);
+ int position = get_item_index (item);
if (position >= 0)
model.remove (position);
}
- public abstract int get_item_index (T item);
+ protected abstract int get_item_index (T item);
- protected abstract Gtk.Widget make_item_row (MathFunction function);
+ protected abstract Gtk.Widget make_item_row (T item);
}
diff --git a/src/math-variable-popover.vala b/src/math-variable-popover.vala
index 187fcdad..e7359958 100644
--- a/src/math-variable-popover.vala
+++ b/src/math-variable-popover.vala
@@ -8,13 +8,33 @@
* license.
*/
+public class MathVariable : Object
+{
+ public string name;
+ public Number? value;
+
+ public MathVariable (string name, Number? value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ public static int name_compare_func (MathVariable var1, MathVariable var2)
+ {
+ return strcmp (var1.name, var2.name);
+ }
+
+ public static bool name_equal_func (MathVariable var1, MathVariable var2)
+ {
+ return var1.name == var2.name;
+ }
+}
+
[GtkTemplate (ui = "/org/gnome/calculator/math-variable-popover.ui")]
-public class MathVariablePopover : Gtk.Popover
+public class MathVariablePopover : MathPopover<MathVariable>
{
private static string[] RESERVED_VARIABLE_NAMES = {"_", "rand"};
- private MathEquation equation;
-
[GtkChild]
private unowned Gtk.ListBox variable_list;
[GtkChild]
@@ -22,60 +42,34 @@ public class MathVariablePopover : Gtk.Popover
[GtkChild]
private unowned Gtk.Button store_variable_button;
- public MathVariablePopover (MathEquation equation)
+ public MathVariablePopover (MathEquation equation, ListStore model, CompareDataFunc compare_func)
{
- this.equation = equation;
- equation.history_signal.connect (this.handler);
+ base(equation, model, (a,b) => MathVariable.name_compare_func(a as MathVariable,b as MathVariable));
- // Fill variable list
- var names = equation.variables.get_names ();
- for (var i = 0; names[i] != null; i++)
- {
- var value = equation.variables[names[i]];
- variable_list.add (make_variable_row (names[i], value));
- }
+ variable_list.bind_model (model, (variable) => make_item_row (variable as MathVariable));
+ equation.history_signal.connect (this.handler);
- variable_list.add (make_variable_row ("rand", null));
-
- // Sort list
- variable_list.set_sort_func (variable_list_sort);
-
- // Listen for variable changes
- equation.variables.variable_added.connect ((name, value) => {
- variable_list.add (make_variable_row (name, value));
- });
- equation.variables.variable_edited.connect ((name, value) => {
- variable_list.remove (find_row_for_variable (name));
- variable_list.add (make_variable_row (name, value));
- });
- equation.variables.variable_deleted.connect ((name) => {
- variable_list.remove (find_row_for_variable (name));
- });
}
- private void handler (string answer, Number number, int number_base, uint representation_base)
+ protected override int get_item_index (MathVariable item)
{
- var row = find_row_for_variable ("_");
- if (row != null)
- variable_list.remove (row);
- variable_list.add (make_variable_row ("_", number));
+ uint position;
+ if (model.find_with_equal_func (item as Object, (a, b) => (MathVariable.name_equal_func(a as
MathVariable, b as MathVariable)), out position))
+ return (int)position;
+ else
+ return -1;
}
- private Gtk.ListBoxRow? find_row_for_variable (string name)
+ private void handler (string answer, Number number, int number_base, uint representation_base)
{
- weak Gtk.ListBoxRow? row = null;
- variable_list.foreach ((child) => {
- if (name == child.get_data<string> ("variable_name"))
- row = child as Gtk.ListBoxRow;
- });
- return row;
+ item_edited_cb (new MathVariable("_", number));
}
[GtkCallback]
private void insert_variable_cb (Gtk.ListBoxRow row)
{
- var name = row.get_data<string> ("variable_name");
- equation.insert (name);
+ var variable = model.get_item (row.get_index ()) as MathVariable;
+ equation.insert (variable.name);
}
[GtkCallback]
@@ -110,22 +104,18 @@ public class MathVariablePopover : Gtk.Popover
equation.variables.delete (name);
}
- private Gtk.ListBoxRow make_variable_row (string name, Number? value)
+ protected override Gtk.Widget make_item_row (MathVariable variable)
{
- var row = new Gtk.ListBoxRow ();
- row.get_style_context ().add_class ("popover-row");
- row.set_data<string> ("variable_name", name);
-
var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
string text;
- if (value != null)
+ if (variable.value != null)
{
- var value_text = equation.serializer.to_string (value);
- text = "<b>%s</b> = %s".printf (name, value_text);
+ var value_text = equation.serializer.to_string (variable.value);
+ text = "<b>%s</b> = %s".printf (variable.name, value_text);
}
else
- text = "<b>%s</b>".printf (name);
+ text = "<b>%s</b>".printf (variable.name);
var label = new Gtk.Label (text);
label.set_margin_start (6);
@@ -133,26 +123,17 @@ public class MathVariablePopover : Gtk.Popover
label.halign = Gtk.Align.START;
hbox.pack_start (label, true, true, 0);
- if (!(name in RESERVED_VARIABLE_NAMES))
+ if (!(variable.name in RESERVED_VARIABLE_NAMES))
{
var button = new Gtk.Button.from_icon_name ("list-remove-symbolic");
button.get_style_context ().add_class ("flat");
- button.set_data<string> ("variable_name", name);
+ button.set_data<string> ("variable_name", variable.name);
button.clicked.connect (delete_variable_cb);
hbox.pack_start (button, false, true, 0);
}
- row.add (hbox);
- row.show_all ();
-
- return row;
+ hbox.show_all ();
+ return hbox;
}
- private int variable_list_sort (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2)
- {
- string name1 = row1.get_data<string> ("variable_name");
- string name2 = row2.get_data<string> ("variable_name");
-
- return strcmp (name1, name2);
- }
}
diff --git a/src/ui/math-variable-popover.ui b/src/ui/math-variable-popover.ui
index bf1a43ae..1d375d29 100644
--- a/src/ui/math-variable-popover.ui
+++ b/src/ui/math-variable-popover.ui
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.16"/>
- <template class="MathVariablePopover" parent="GtkPopover">
+ <template class="MathVariablePopover" parent="MathPopover">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="content">
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]