From dc008bf3ccf874cd88a90575f0c4415deadb2e29 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Fri, 25 Aug 2017 16:36:42 +1100 Subject: [PATCH] GUI2/Listbox: cleaned up set/get_active_sorting_option functions * Cleaner implementation of get_active_sorting_option. * Added some comments. --- src/gui/widgets/listbox.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp index 5f4ce4020013..1375b5cfc175 100644 --- a/src/gui/widgets/listbox.cpp +++ b/src/gui/widgets/listbox.cpp @@ -615,10 +615,14 @@ void listbox::set_active_sorting_option(const order_pair& sort_by, const bool se // TODO: should this be moved to a public header_grid() getter function? grid& header_grid = find_widget(this, "_header_grid", false); - selectable_item& wgt = find_widget(&header_grid, "sort_" + std::to_string(sort_by.first), false); - wgt.set_value(static_cast(sort_by.second)); + selectable_item& w = find_widget(&header_grid, "sort_" + std::to_string(sort_by.first), false); - order_by_column(sort_by.first, dynamic_cast(wgt)); + // Set the sorting toggle widgets' value (in this case, its state) to the given sorting + // order. This is necessary since the widget's value is used to determine the order in + // @ref order_by_column in lieu of a direction being passed directly. + w.set_value(static_cast(sort_by.second)); + + order_by_column(sort_by.first, dynamic_cast(w)); if(select_first && generator_->get_item_count() > 0) { select_row_at(0); @@ -627,17 +631,15 @@ void listbox::set_active_sorting_option(const order_pair& sort_by, const bool se const listbox::order_pair listbox::get_active_sorting_option() { - const auto iter = std::find_if(orders_.begin(), orders_.end(), - [](const std::pair& option) { - return option.first != nullptr && option.first->get_value() != SORT_NONE; - } - ); + for(unsigned int column = 0; column < orders_.size(); ++column) { + selectable_item* w = orders_[column].first; - if(iter != orders_.end()) { - return {iter - orders_.begin(), static_cast(iter->first->get_value())}; + if(w && w->get_value() != SORT_NONE) { + return std::make_pair(column, static_cast(w->get_value())); + } } - return {-1, SORT_NONE}; + return std::make_pair(-1, SORT_NONE); } void listbox::set_content_size(const point& origin, const point& size)