power: Correctly lookup or insert new items into combobox

The code to lookup or insert items into the combobox had a few issues.
It would assume that the items are sorted, causing existing items to not
be found and be inserted instead. It also would simply forget to insert
an item if it was larger than all existing items.

This code is now changed to iterate over all items, finding the best
insertion point in the process (next item has a larger value, or the
values are not increasing anymore). The item will only be inserted if it
has not been found.

Fixes #261
This commit is contained in:
Benjamin Berg 2018-11-05 13:27:12 +01:00 committed by Georges Basile Stavracas Neto
parent 13bfd725cd
commit b5711c59ec

View file

@ -1223,9 +1223,12 @@ static void
set_value_for_combo (GtkComboBox *combo_box, gint value)
{
GtkTreeIter iter;
GtkTreeIter last;
g_autoptr(GtkTreeIter) insert = NULL;
GtkTreeIter new;
GtkTreeModel *model;
gint value_tmp;
gint value_last = 0;
g_autofree gchar *text = NULL;
gboolean ret;
/* get entry */
@ -1245,25 +1248,24 @@ set_value_for_combo (GtkComboBox *combo_box, gint value)
gtk_combo_box_set_active_iter (combo_box, &iter);
return;
}
else if (value_tmp > value)
{
GtkTreeIter new;
g_autofree gchar *text = NULL;
/* This is an unlisted value, add it to the drop-down */
gtk_list_store_insert_before (GTK_LIST_STORE (model), &new, &iter);
text = time_to_string_text (value * 1000);
gtk_list_store_set (GTK_LIST_STORE (model), &new,
ACTION_MODEL_TEXT, text,
ACTION_MODEL_VALUE, value,
-1);
gtk_combo_box_set_active_iter (combo_box, &new);
return;
}
last = iter;
/* Insert before if the next value is larger or the value is lower
* again (i.e. "Never" is zero and last). */
if (!insert && (value_tmp > value || value_last > value_tmp))
insert = gtk_tree_iter_copy (&iter);
value_last = value_tmp;
} while (gtk_tree_model_iter_next (model, &iter));
gtk_combo_box_set_active_iter (combo_box, &last);
/* The value is not listed, so add it at the best point (or the end). */
gtk_list_store_insert_before (GTK_LIST_STORE (model), &new, insert);
text = time_to_string_text (value * 1000);
gtk_list_store_set (GTK_LIST_STORE (model), &new,
ACTION_MODEL_TEXT, text,
ACTION_MODEL_VALUE, value,
-1);
gtk_combo_box_set_active_iter (combo_box, &new);
}
static void