Skip to content

Commit

Permalink
Generalize listbox sorting option setup (thanks to gfgtdf)
Browse files Browse the repository at this point in the history
Previously, an init_sorting_option class member had to be added to every dialog that
wanted to use sorting. This removes that need and also removes the need for individual
generator_sort_array objects.
  • Loading branch information
Vultraz committed Aug 9, 2016
1 parent fba34c1 commit a30d816
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 177 deletions.
33 changes: 5 additions & 28 deletions src/gui/dialogs/addon/list.cpp
Expand Up @@ -314,18 +314,6 @@ static std::string describe_status_verbose(const addon_tracking_info& state)
return colorify_addon_state_string(s, state);
}

template<typename Fcn>
void taddon_list::init_sorting_option(generator_sort_array& order_funcs, Fcn 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);
Expand Down Expand Up @@ -382,22 +370,11 @@ void taddon_list::pre_show(twindow& window)
find_widget<tbutton>(row_grid, "single_uninstall", false).set_active(is_installed);
}

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);
list.register_sorting_option(0, [this](const int i) { return addon_at(ids_[i], addons_).title; });
list.register_sorting_option(1, [this](const int i) { return addon_at(ids_[i], addons_).author; });
list.register_sorting_option(2, [this](const int i) { return addon_at(ids_[i], addons_).size; });
list.register_sorting_option(3, [this](const int i) { return addon_at(ids_[i], addons_).downloads; });
list.register_sorting_option(4, [this](const int i) { return addon_at(ids_[i], addons_).type; });

find_widget<ttext_box>(&window, "filter", false).set_text_changed_callback(
std::bind(&taddon_list::on_filtertext_changed, this, _1, _2));
Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/addon/list.hpp
Expand Up @@ -19,7 +19,6 @@
#include "addon/state.hpp"

#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "gui/widgets/pane.hpp"

#include "config.hpp" // needed for config::const_child_itors
Expand All @@ -38,9 +37,6 @@ class taddon_list : public tdialog
explicit taddon_list(const config& cfg);

private:
template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

void on_filtertext_changed(ttext_* textbox, const std::string& text);

std::vector<tselectable_*> orders_;
Expand Down
21 changes: 2 additions & 19 deletions src/gui/dialogs/game_load.cpp
Expand Up @@ -145,13 +145,8 @@ void tgame_load::pre_show(twindow& window)
list.add_row(data);
}

generator_sort_array order_funcs;

init_sorting_option(order_funcs, [](savegame::save_info s) { return s.name(); });
list.set_column_order(0, order_funcs);

init_sorting_option(order_funcs, [](savegame::save_info s) { return s.modified(); });
list.set_column_order(1, order_funcs);
list.register_sorting_option(0, [this](const int i) { return games_[i].name(); });
list.register_sorting_option(1, [this](const int i) { return games_[i].modified(); });

connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "delete", false),
Expand All @@ -161,18 +156,6 @@ void tgame_load::pre_show(twindow& window)
display_savegame(window);
}

template<typename Fcn>
void tgame_load::init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on)
{
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::display_savegame(twindow& window)
{
const int selected_row =
Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/game_load.hpp
Expand Up @@ -16,7 +16,6 @@
#define GUI_DIALOGS_LOAD_GAME_HPP_INCLUDED

#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "save_index.hpp"
#include "tstring.hpp"

Expand Down Expand Up @@ -70,9 +69,6 @@ class tgame_load : public tdialog
void evaluate_summary_string(std::stringstream& str,
const config& cfg_summary);

template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

tfield_text* txtFilter_;
tfield_bool* chk_change_difficulty_;
tfield_bool* chk_show_replay_;
Expand Down
31 changes: 5 additions & 26 deletions src/gui/dialogs/preferences_dialog.cpp
Expand Up @@ -401,18 +401,6 @@ static tgrid* get_advanced_row_grid(tlistbox& list, const int selected_row)
list.get_row_grid(selected_row)->find("pref_main_grid", false));
}

template<typename Fcn>
void tpreferences::init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on)
{
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(visible_hotkeys_[i1]) < filter_on(visible_hotkeys_[i2]);
};

order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on(visible_hotkeys_[i1]) > filter_on(visible_hotkeys_[i2]);
};
}

/**
* Sets up states and callbacks for each of the widgets
*/
Expand Down Expand Up @@ -863,25 +851,16 @@ void tpreferences::initialize_members(twindow& window)

tlistbox& hotkey_list = find_widget<tlistbox>(&window, "list_hotkeys", false);

generator_sort_array order_funcs;

// Action column
init_sorting_option(order_funcs, [](const hotkey::hotkey_command* key) { return key->description.str(); });
hotkey_list.set_column_order(0, order_funcs);
hotkey_list.register_sorting_option(0, [this](const int i) { return visible_hotkeys_[i]->description.str(); });

// Hotkey column
init_sorting_option(order_funcs, [](const hotkey::hotkey_command* key) { return hotkey::get_names(key->command); });
hotkey_list.set_column_order(1, order_funcs);
hotkey_list.register_sorting_option(1, [this](const int i) { return hotkey::get_names(visible_hotkeys_[i]->command); });

// Scope columns
init_sorting_option(order_funcs, [](const hotkey::hotkey_command* key) { return !key->scope[hotkey::SCOPE_GAME]; });
hotkey_list.set_column_order(2, order_funcs);

init_sorting_option(order_funcs, [](const hotkey::hotkey_command* key) { return !key->scope[hotkey::SCOPE_EDITOR]; });
hotkey_list.set_column_order(3, order_funcs);

init_sorting_option(order_funcs, [](const hotkey::hotkey_command* key) { return !key->scope[hotkey::SCOPE_MAIN_MENU]; });
hotkey_list.set_column_order(4, order_funcs);
hotkey_list.register_sorting_option(2, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_GAME]; });
hotkey_list.register_sorting_option(3, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_EDITOR]; });
hotkey_list.register_sorting_option(4, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_MAIN_MENU]; });

connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "btn_add_hotkey", false), std::bind(
Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/preferences_dialog.hpp
Expand Up @@ -20,7 +20,6 @@
#include "game_preferences.hpp"
#include "utils/make_enum.hpp"
#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "gui/widgets/group.hpp"

// This file is not named preferences.hpp in order -I conflicts with
Expand Down Expand Up @@ -75,9 +74,6 @@ class tpreferences : public tdialog
void setup_friends_list(twindow& window);
void setup_hotkey_list(twindow& window);

template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

void add_friend_list_entry(const bool is_friend,
ttext_box& textbox, twindow& window);

Expand Down
21 changes: 2 additions & 19 deletions src/gui/dialogs/unit_create.cpp
Expand Up @@ -87,18 +87,6 @@ tunit_create::tunit_create()
{
}

template<typename Fcn>
void tunit_create::init_sorting_option(generator_sort_array& order_funcs, Fcn 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
Expand Down Expand Up @@ -171,13 +159,8 @@ void tunit_create::pre_show(twindow& window)
<< std::endl;
}

generator_sort_array order_funcs;

init_sorting_option(order_funcs, [](unit_type u) { return u.race()->plural_name().str(); });
list.set_column_order(0, order_funcs);

init_sorting_option(order_funcs, [](unit_type u) { return u.type_name().str(); });
list.set_column_order(1, order_funcs);
list.register_sorting_option(0, [this](const int i) { return (*units_[i]).race()->plural_name().str(); });
list.register_sorting_option(1, [this](const int i) { return (*units_[i]).type_name().str(); });

list_item_clicked(window);
}
Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/unit_create.hpp
Expand Up @@ -16,7 +16,6 @@
#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"

Expand Down Expand Up @@ -69,9 +68,6 @@ class tunit_create : public tdialog
/** Inherited from tdialog. */
void pre_show(twindow& window);

template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

/** Inherited from tdialog. */
void post_show(twindow& window);

Expand Down
37 changes: 6 additions & 31 deletions src/gui/dialogs/unit_list.cpp
Expand Up @@ -104,18 +104,6 @@ static std::string format_movement_string(unit_const_ptr u)
return formatter() << "<span color='" << color << "'>" << moves_left << "/" << moves_max << "</span>";
}

template<typename Fcn>
void tunit_list::init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on)
{
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*unit_list_)[i1]) < filter_on((*unit_list_)[i2]);
};

order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*unit_list_)[i1]) > filter_on((*unit_list_)[i2]);
};
}

void tunit_list::pre_show(twindow& window)
{
tlistbox& list = find_widget<tlistbox>(&window, "units_list", false);
Expand Down Expand Up @@ -183,25 +171,12 @@ void tunit_list::pre_show(twindow& window)
list.add_row(row_data);
}

generator_sort_array order_funcs;

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->type_name().str(); });
list.set_column_order(0, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->name().str(); });
list.set_column_order(1, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->level(); });
list.set_column_order(2, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->experience(); });
list.set_column_order(3, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) {
return !u.get()->trait_names().empty() ? u.get()->trait_names().front().str() : "";
});

list.set_column_order(4, order_funcs);
list.register_sorting_option(0, [this](const int i) { return (*unit_list_)[i]->type_name().str(); });
list.register_sorting_option(1, [this](const int i) { return (*unit_list_)[i]->name().str(); });
list.register_sorting_option(2, [this](const int i) { return (*unit_list_)[i]->level(); });
list.register_sorting_option(3, [this](const int i) { return (*unit_list_)[i]->experience(); });
list.register_sorting_option(4, [this](const int i) {
return (*unit_list_)[i]->trait_names().empty() ? (*unit_list_)[i]->trait_names().front().str() : ""; });

list_item_clicked(window);
}
Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/unit_list.hpp
Expand Up @@ -15,7 +15,6 @@
#define GUI_DIALOGS_UNIT_LIST_HPP_INCLUDED

#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/generator.hpp"
#include "units/ptr.hpp"

#include <memory>
Expand Down Expand Up @@ -46,9 +45,6 @@ class tunit_list : public tdialog

int selected_index_;

template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

/** Callbacks */
void list_item_clicked(twindow& window);

Expand Down
36 changes: 6 additions & 30 deletions src/gui/dialogs/unit_recall.cpp
Expand Up @@ -111,18 +111,6 @@ static std::string format_cost_string(int unit_recall_cost, const int team_recal
return str.str();
}

template<typename Fcn>
void tunit_recall::init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on)
{
order_funcs[0] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*recall_list_)[i1]) < filter_on((*recall_list_)[i2]);
};

order_funcs[1] = [this, filter_on](unsigned i1, unsigned i2) {
return filter_on((*recall_list_)[i1]) > filter_on((*recall_list_)[i2]);
};
}

static std::string get_title_suffix(int side_num)
{
if(!resources::teams || !resources::units) {
Expand Down Expand Up @@ -239,26 +227,14 @@ void tunit_recall::pre_show(twindow& window)
filter_options_.push_back(filter_text);
}

generator_sort_array order_funcs;

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->type_name().str(); });
list.set_column_order(0, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->name().str(); });
list.set_column_order(1, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->level(); });
list.set_column_order(2, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) { return u.get()->experience(); });
list.set_column_order(3, order_funcs);

init_sorting_option(order_funcs, [](unit_const_ptr u) {
return !u.get()->trait_names().empty() ? u.get()->trait_names().front().str() : "";
list.register_sorting_option(0, [this](const int i) { return (*recall_list_)[i]->type_name().str(); });
list.register_sorting_option(1, [this](const int i) { return (*recall_list_)[i]->name().str(); });
list.register_sorting_option(2, [this](const int i) { return (*recall_list_)[i]->level(); });
list.register_sorting_option(3, [this](const int i) { return (*recall_list_)[i]->experience(); });
list.register_sorting_option(4, [this](const int i) {
return !(*recall_list_)[i]->trait_names().empty() ? (*recall_list_)[i]->trait_names().front().str() : "";
});

list.set_column_order(4, order_funcs);

list_item_clicked(window);
}

Expand Down
4 changes: 0 additions & 4 deletions src/gui/dialogs/unit_recall.hpp
Expand Up @@ -16,7 +16,6 @@

#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/group.hpp"
#include "gui/widgets/generator.hpp"
#include "units/race.hpp"
#include "units/ptr.hpp"

Expand Down Expand Up @@ -54,9 +53,6 @@ class tunit_recall : public tdialog
std::vector<std::string> filter_options_;
std::vector<std::string> last_words_;

template<typename Fcn>
void init_sorting_option(generator_sort_array& order_funcs, Fcn filter_on);

/** Callbacks */
void list_item_clicked(twindow& window);
void filter_text_changed(ttext_* textbox, const std::string& text);
Expand Down
10 changes: 10 additions & 0 deletions src/gui/widgets/listbox.hpp
Expand Up @@ -235,6 +235,16 @@ class tlistbox : public tscrollbar_container
void order_by(const tgenerator_::torder_func& func);

void set_column_order(unsigned col, const generator_sort_array& func);

template<typename Func>
void register_sorting_option(const int col, const Func& f)
{
set_column_order(col, {
[f](int lhs, int rhs) { return f(lhs) < f(rhs); },
[f](int lhs, int rhs) { return f(lhs) > f(rhs); }
});
};

protected:
/***** ***** ***** ***** keyboard functions ***** ***** ***** *****/

Expand Down

0 comments on commit a30d816

Please sign in to comment.