The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
label_settings.cpp
Go to the documentation of this file.
1 /*
2  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12  */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
16 #include "label_settings.hpp"
17 
18 #include <vector>
19 #include "utils/functional.hpp"
20 #include "gettext.hpp"
21 #include "game_display.hpp"
22 #include "map/label.hpp"
23 #include "resources.hpp"
25 #include "gui/widgets/control.hpp"
26 #ifdef GUI2_EXPERIMENTAL_LISTBOX
27 #include "gui/widgets/list.hpp"
28 #else
29 #include "gui/widgets/listbox.hpp"
30 #endif
31 #include "gui/widgets/window.hpp"
32 #include "gui/widgets/settings.hpp"
34 #include "gui/widgets/label.hpp"
35 #include "formula/string_utils.hpp"
36 
37 namespace gui2 {
38 REGISTER_DIALOG(label_settings);
39 
41  const std::vector<std::string>& all_categories = resources::screen->labels().all_categories();
42  const std::vector<std::string>& hidden_categories = viewer.hidden_label_categories();
43 
44  for(size_t i = 0; i < all_categories.size(); i++) {
45  all_labels[all_categories[i]] = true;
46  if(all_categories[i].substr(0,4) == "cat:")
47  labels_display[all_categories[i]] = all_categories[i].substr(4);
48  else if(all_categories[i] == "team")
49  labels_display[all_categories[i]] = _("Team Labels");
50  // TODO: Translatable names for categories?
51  }
52  for(size_t i = 0; i < hidden_categories.size(); i++) {
53  all_labels[hidden_categories[i]] = false;
54  }
55  for(size_t i = 0; i < dc.teams().size(); i++) {
56  const team& team = dc.teams()[i];
57  const std::string label_cat_key = "side:" + std::to_string(i + 1);
58  if(team.hidden()) {
59  labels_display[label_cat_key] = "";
60  continue;
61  }
62  std::string team_name = team.side_name();
63  if(team_name.empty()) {
64  team_name = team.user_team_name();
65  }
66  if(team_name.empty()) {
67  team_name = _("Unknown");
68  }
69  string_map subst;
70  subst["side_number"] = std::to_string(i + 1);
71  subst["name"] = team_name;
72  labels_display[label_cat_key] = vgettext("Side $side_number ($name)", subst);
73  }
74 }
75 
77  std::map<std::string, string_map> list_data;
78  tlistbox& cats_listbox = find_widget<tlistbox>(&window, "label_types", false);
79  for(const auto & label_entry : all_labels) {
80  const std::string& category = label_entry.first;
81  const bool& visible = label_entry.second;
82 
83  std::string name = labels_display[category];
84  if(category.substr(0,5) == "side:") {
85  if(name.empty()) {
86  // This means it's a hidden side, so don't show it.
87  continue;
88  }
89  int team = lexical_cast<int>(category.substr(5)) - 1;
90  Uint32 which_color = game_config::tc_info(viewer.teams()[team].color())[0];
91  std::ostringstream sout;
92  sout << "<span color='#" << std::hex << which_color << "'>" << name << "</span>";
93  name = sout.str();
94  }
95 
96  list_data["cat_name"]["label"] = name;
97  cats_listbox.add_row(list_data);
98 
99  tgrid* grid = cats_listbox.get_row_grid(cats_listbox.get_item_count() - 1);
100  ttoggle_button& status = find_widget<ttoggle_button>(grid, "cat_status", false);
101  status.set_value(visible);
102  status.set_callback_state_change(std::bind(&tlabel_settings::toggle_category, this, _1, category));
103 
104  if(category.substr(0,5) == "side:") {
105  tlabel& label = find_widget<tlabel>(grid, "cat_name", false);
106  label.set_use_markup(true);
107  }
108  }
109 }
110 
112  tlabel_settings window(dc);
113  if(!window.show(video)) return false;
114  std::vector<std::string> hidden_categories;
115  for(auto lbl : window.all_labels) {
116  if(lbl.second == false) {
117  hidden_categories.push_back(lbl.first);
118  }
119  }
120  dc.hidden_label_categories_ref().swap(hidden_categories);
121  return true;
122 }
123 
125  all_labels[category] = (static_cast<ttoggle_button&>(box).get_value() != 0);
126 }
127 }
const std::vector< std::string > & all_categories() const
Definition: label.cpp:296
const std::vector< Uint32 > & tc_info(const std::string &name)
std::vector< std::string > & hidden_label_categories_ref()
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
bool hidden() const
Definition: team.hpp:344
Definition: video.hpp:58
game_display * screen
Definition: resources.cpp:27
Base container class.
Definition: grid.hpp:29
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
Label showing a text.
Definition: label.hpp:29
To lexical_cast(From value)
Lexical cast converts one type to another.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
Class for a toggle button.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
std::map< std::string, bool > all_labels
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
std::map< std::string, t_string > labels_display
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
This file contains the settings handling of the widget library.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:138
void add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:74
tlabel_settings(display_context &dc)
const t_string & user_team_name() const
Definition: team.hpp:298
void set_callback_state_change(std::function< void(twidget &)> callback)
Inherited from tselectable_.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
static bool execute(display_context &dc, CVideo &video)
The execute function.
display_context & viewer
virtual const std::vector< team > & teams() const =0
The listbox class.
Definition: listbox.hpp:39
size_t i
Definition: function.cpp:1057
std::string vgettext(const char *msgid, const utils::string_map &symbols)
GLuint const GLchar * name
Definition: glew.h:1782
void pre_show(twindow &window)
Inherited from tdialog.
const std::string & side_name() const
Definition: team.hpp:307
Base class for all widgets.
Definition: widget.hpp:49
map_labels & labels()
Definition: display.cpp:2773
virtual const std::vector< std::string > & hidden_label_categories() const =0
void set_value(const unsigned selected)
Inherited from tselectable_.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void toggle_category(twidget &box, std::string category)
Callback for toggling a checkbox state.
const tgrid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:215