Convert several GUI2 dialogs to use the new sorting code

This commit is contained in:
Charles Dang 2016-08-03 11:42:16 +11:00
parent df84646026
commit 12601cc6e2
8 changed files with 75 additions and 136 deletions

View file

@ -43,7 +43,7 @@
horizontal_grow = "true"
[toggle_button]
id = "sort_name"
id = "sort_0"
definition = "listbox_header"
linked_group = "name"
@ -72,7 +72,7 @@
horizontal_grow = "true"
[toggle_button]
id = "sort_author"
id = "sort_1"
definition = "listbox_header"
linked_group = "author"
@ -86,7 +86,7 @@
horizontal_grow = "true"
[toggle_button]
id = "sort_size"
id = "sort_2"
definition = "listbox_header"
linked_group = "size"
@ -100,7 +100,7 @@
horizontal_grow = "true"
[toggle_button]
id = "sort_downloads"
id = "sort_3"
definition = "listbox_header"
linked_group = "downloads"
@ -114,7 +114,7 @@
horizontal_grow = "true"
[toggle_button]
id = "sort_type"
id = "sort_4"
definition = "listbox_header"
linked_group = "type"

View file

@ -1012,7 +1012,7 @@ bool addons_manager_ui(CVideo& v, const std::string& remote_address)
addons_list addons;
if(gui2::new_widgets) {
//if(gui2::new_widgets) {
config cfg;
client.request_addons_list(cfg);
if(!cfg) {
@ -1025,7 +1025,7 @@ bool addons_manager_ui(CVideo& v, const std::string& remote_address)
gui2::taddon_list dlg(cfg);
dlg.show(v);
return need_wml_cache_refresh;
}
//}
if(!get_addons_list(client, addons)) {
gui2::show_error_message(v, _("An error occurred while downloading the add-ons list from the server."));

View file

@ -94,24 +94,6 @@ namespace gui2
*/
namespace {
// TODO: it would be better if we didnt have to parse the config every time.
bool str_up(const config* cfg, const std::string& prop_id, unsigned i1, unsigned i2)
{
return cfg->child("campaign", i1)[prop_id].str() < cfg->child("campaign", i2)[prop_id].str();
}
bool str_down(const config* cfg, const std::string& prop_id, unsigned i1, unsigned i2)
{
return cfg->child("campaign", i1)[prop_id].str() > cfg->child("campaign", i2)[prop_id].str();
}
bool num_up(const config* cfg, const std::string& prop_id, unsigned i1, unsigned i2)
{
return cfg->child("campaign", i1)[prop_id].to_int() < cfg->child("campaign", i2)[prop_id].to_int();
}
bool num_down(const config* cfg, const std::string& prop_id, unsigned i1, unsigned i2)
{
return cfg->child("campaign", i1)[prop_id].to_int() > cfg->child("campaign", i2)[prop_id].to_int();
}
struct filter_transform
{
filter_transform(const std::vector<std::string>& filtertext) : filtertext_(filtertext) {}
@ -142,7 +124,7 @@ namespace {
}
const std::vector<std::string> filtertext_;
};
/**
* Retrieves an element from the given associative container or dies in some
* way.
@ -200,53 +182,6 @@ void taddon_list::on_filtertext_changed(ttext_* textbox, const std::string& text
listbox.set_row_shown(res);
}
void taddon_list::on_order_button_click(twindow& window, const tgenerator_::torder_func& up, const tgenerator_::torder_func& down, twidget& w)
{
tselectable_& selectable = dynamic_cast<tselectable_&>(w);
for(auto& other : orders_)
{
if(other != &selectable) {
other->set_value(0);
}
}
tlistbox& listbox = find_widget<tlistbox>(&window, "addons", true);
switch(selectable.get_value())
{
case 0:
listbox.order_by(std::less<unsigned>());
break;
case 1:
listbox.order_by(up);
break;
case 2:
listbox.order_by(down);
break;
}
}
void taddon_list::register_sort_button(twindow& window, const std::string& id, const tgenerator_::torder_func& up, const tgenerator_::torder_func& down)
{
tselectable_& selectable = find_widget<tselectable_>(&window, id, true);
orders_.push_back(&selectable);
selectable.set_callback_state_change(std::bind(&taddon_list::on_order_button_click, this, std::ref(window), up, down, _1));
}
void taddon_list::register_sort_button_alphabetical(twindow& window, const std::string& id, const std::string& prop_id)
{
register_sort_button(window, id,
std::bind(&str_up, &cfg_, prop_id, _1, _2),
std::bind(&str_down, &cfg_, prop_id, _1, _2)
);
}
void taddon_list::register_sort_button_numeric(twindow& window, const std::string& id, const std::string& prop_id)
{
register_sort_button(window, id,
std::bind(&num_up, &cfg_, prop_id, _1, _2),
std::bind(&num_down, &cfg_, prop_id, _1, _2)
);
}
static std::string colorify_addon_state_string(const std::string& str,
const addon_tracking_info& state, bool verbose = false)
{
@ -379,6 +314,18 @@ static std::string describe_status_verbose(const addon_tracking_info& state)
return colorify_addon_state_string(s, state);
}
template<typename Fnc>
void taddon_list::init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on)
{
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(addon_at(ids_[i1], addons_)) < filter_on(addon_at(ids_[i2], addons_));
};
order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(addon_at(ids_[i1], addons_)) > filter_on(addon_at(ids_[i2], addons_));
};
}
void taddon_list::pre_show(twindow& window)
{
tlistbox& list = find_widget<tlistbox>(&window, "addons", false);
@ -435,11 +382,22 @@ void taddon_list::pre_show(twindow& window)
find_widget<tbutton>(row_grid, "single_uninstall", false).set_active(is_installed);
}
register_sort_button_alphabetical(window, "sort_name", "name");
register_sort_button_alphabetical(window, "sort_author", "author");
register_sort_button_alphabetical(window, "sort_type", "type");
register_sort_button_numeric(window, "sort_downloads", "downloads");
register_sort_button_numeric(window, "sort_size", "size");
generator_sort_array order_funcs;
init_sorting_option(order_funcs, [](addon_info info) { return info.title; });
list.set_column_order(0, order_funcs);
init_sorting_option(order_funcs, [](addon_info info) { return info.author; });
list.set_column_order(1, order_funcs);
init_sorting_option(order_funcs, [](addon_info info) { return info.size; });
list.set_column_order(2, order_funcs);
init_sorting_option(order_funcs, [](addon_info info) { return info.downloads; });
list.set_column_order(3, order_funcs);
init_sorting_option(order_funcs, [](addon_info info) { return info.type; });
list.set_column_order(4, order_funcs);
find_widget<ttext_box>(&window, "filter", false).set_text_changed_callback(
std::bind(&taddon_list::on_filtertext_changed, this, _1, _2));

View file

@ -38,11 +38,9 @@ public:
explicit taddon_list(const config& cfg);
private:
void register_sort_button(twindow& window, const std::string& id, const tgenerator_::torder_func& up, const tgenerator_::torder_func& down);
void register_sort_button_alphabetical(twindow& window, const std::string& id, const std::string& prop_id);
void register_sort_button_numeric(twindow& window, const std::string& id, const std::string& prop_id);
template<typename Fnc>
void init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on);
void on_order_button_click(twindow& window, const tgenerator_::torder_func& up, const tgenerator_::torder_func& down, twidget& w);
void on_filtertext_changed(ttext_* textbox, const std::string& text);
std::vector<tselectable_*> orders_;
@ -67,7 +65,7 @@ private:
addons_tracking_list tracking_info_;
std::vector<std::string> ids_;
void browse_url_callback(ttext_box& url_box);
void copy_url_callback(ttext_box& url_box);
void options_button_callback(twindow& window);

View file

@ -145,24 +145,16 @@ void tgame_load::pre_show(twindow& window)
display_savegame(window);
}
bool tgame_load::compare_name(unsigned i1, unsigned i2) const
template<typename Fnc>
void tgame_load::init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on)
{
return games_[i1].name() < games_[i2].name();
}
bool tgame_load::compare_date(unsigned i1, unsigned i2) const
{
return games_[i1].modified() < games_[i2].modified();
}
bool tgame_load::compare_name_rev(unsigned i1, unsigned i2) const
{
return games_[i1].name() > games_[i2].name();
}
bool tgame_load::compare_date_rev(unsigned i1, unsigned i2) const
{
return games_[i1].modified() > games_[i2].modified();
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(games_[i1]) < filter_on(games_[i2]);
};
order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(games_[i1]) > filter_on(games_[i2]);
};
}
void tgame_load::fill_game_list(twindow& window,
@ -186,12 +178,13 @@ void tgame_load::fill_game_list(twindow& window,
list.add_row(data);
}
generator_sort_array order_funcs;
order_funcs[0] = std::bind(&tgame_load::compare_name, this, _1, _2);
order_funcs[1] = std::bind(&tgame_load::compare_name_rev, this, _1, _2);
init_sorting_option(order_funcs, [](savegame::save_info s) { return s.name(); });
list.set_column_order(0, order_funcs);
order_funcs[0] = std::bind(&tgame_load::compare_date, this, _1, _2);
order_funcs[1] = std::bind(&tgame_load::compare_date_rev, this, _1, _2);
init_sorting_option(order_funcs, [](savegame::save_info s) { return s.modified(); });
list.set_column_order(1, order_funcs);
}

View file

@ -16,6 +16,7 @@
#define GUI_DIALOGS_LOAD_GAME_HPP_INCLUDED
#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "save_index.hpp"
#include "tstring.hpp"
@ -70,10 +71,8 @@ private:
void evaluate_summary_string(std::stringstream& str,
const config& cfg_summary);
bool compare_name(unsigned i1, unsigned i2) const;
bool compare_date(unsigned i1, unsigned i2) const;
bool compare_name_rev(unsigned i1, unsigned i2) const;
bool compare_date_rev(unsigned i1, unsigned i2) const;
template<typename Fnc>
void init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on);
void fill_game_list(twindow& window,
std::vector<savegame::save_info>& games);

View file

@ -87,6 +87,18 @@ tunit_create::tunit_create()
{
}
template<typename Fnc>
void tunit_create::init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on)
{
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*units_[i1])) < filter_on((*units_[i2]));
};
order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*units_[i1])) > filter_on((*units_[i2]));
};
}
void tunit_create::pre_show(twindow& window)
{
ttoggle_button& male_toggle
@ -160,36 +172,16 @@ void tunit_create::pre_show(twindow& window)
}
generator_sort_array order_funcs;
order_funcs[0] = std::bind(&tunit_create::compare_race, this, _1, _2);
order_funcs[1] = std::bind(&tunit_create::compare_race_rev, this, _1, _2);
init_sorting_option(order_funcs, [](unit_type u) { return u.race()->plural_name().str(); });
list.set_column_order(0, order_funcs);
order_funcs[0] = std::bind(&tunit_create::compare_type, this, _1, _2);
order_funcs[1] = std::bind(&tunit_create::compare_type_rev, this, _1, _2);
init_sorting_option(order_funcs, [](unit_type u) { return u.type_name().str(); });
list.set_column_order(1, order_funcs);
list_item_clicked(window);
}
bool tunit_create::compare_type(unsigned i1, unsigned i2) const
{
return units_[i1]->type_name().str() < units_[i2]->type_name().str();
}
bool tunit_create::compare_race(unsigned i1, unsigned i2) const
{
return units_[i1]->race()->plural_name().str() < units_[i2]->race()->plural_name().str();
}
bool tunit_create::compare_type_rev(unsigned i1, unsigned i2) const
{
return units_[i1]->type_name().str() > units_[i2]->type_name().str();
}
bool tunit_create::compare_race_rev(unsigned i1, unsigned i2) const
{
return units_[i1]->race()->plural_name().str() > units_[i2]->race()->plural_name().str();
}
void tunit_create::post_show(twindow& window)
{
tlistbox& list = find_widget<tlistbox>(&window, "unit_type_list", false);

View file

@ -16,6 +16,7 @@
#define GUI_DIALOGS_UNIT_CREATE_HPP_INCLUDED
#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "gui/widgets/group.hpp"
#include "units/race.hpp"
@ -68,10 +69,8 @@ private:
/** Inherited from tdialog. */
void pre_show(twindow& window);
bool compare_type(unsigned i1, unsigned i2) const;
bool compare_race(unsigned i1, unsigned i2) const;
bool compare_type_rev(unsigned i1, unsigned i2) const;
bool compare_race_rev(unsigned i1, unsigned i2) const;
template<typename Fnc>
void init_sorting_option(generator_sort_array& order_funcs, Fnc filter_on);
/** Inherited from tdialog. */
void post_show(twindow& window);