The Battle for Wesnoth  1.17.0-dev
label_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "display.hpp"
20 #include "font/text_formatting.hpp"
21 #include "formatter.hpp"
22 #include "formula/string_utils.hpp"
23 #include "gettext.hpp"
25 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/listbox.hpp"
27 #include "gui/widgets/settings.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "map/label.hpp"
32 #include "team.hpp"
33 
34 #include <vector>
35 
36 namespace gui2::dialogs
37 {
38 REGISTER_DIALOG(label_settings)
39 
41  : viewer_(dc)
42 {
43  const std::vector<std::string>& all_categories = display::get_singleton()->labels().all_categories();
44  const std::vector<std::string>& hidden_categories = viewer_.hidden_label_categories();
45 
46  for(const std::string& cat : all_categories) {
47  all_labels_[cat] = true;
48 
49  // TODO: Translatable names for categories?
50  if(cat.substr(0, 4) == "cat:") {
51  labels_display_[cat] = cat.substr(4);
52  } else if(cat == "team") {
53  labels_display_[cat] = _("Team Labels");
54  }
55  }
56 
57  for(const std::string& hidden_cat : hidden_categories) {
58  all_labels_[hidden_cat] = false;
59  }
60 
61  for(std::size_t i = 0; i < viewer_.teams().size(); i++) {
62  const team& team = viewer_.teams()[i];
63  const std::string label_cat_key = "side:" + std::to_string(i + 1);
64 
65  if(team.hidden()) {
66  labels_display_[label_cat_key] = "";
67  continue;
68 
69  }
70 
71  std::string team_name = team.side_name();
72  if(team_name.empty()) {
73  team_name = team.user_team_name();
74  }
75 
76  if(team_name.empty()) {
77  team_name = _("Unknown");
78  }
79 
80  string_map subst;
81  subst["side_number"] = std::to_string(i + 1);
82  subst["name"] = team_name;
83  labels_display_[label_cat_key] = VGETTEXT("Side $side_number ($name)", subst);
84  }
85 }
86 
88 {
89  listbox& cats_listbox = find_widget<listbox>(&window, "label_types", false);
90  std::map<std::string, string_map> list_data;
91 
92  for(const auto& label_entry : all_labels_) {
93  const std::string& category = label_entry.first;
94  const bool visible = label_entry.second;
95 
96  std::string name = labels_display_[category];
97  if(category.substr(0, 5) == "side:") {
98  // This means it's a hidden side, so don't show it.
99  if(name.empty()) {
100  continue;
101  }
102 
103  const int team = std::stoi(category.substr(5)) - 1;
104  const color_t tc = game_config::tc_info(viewer_.teams()[team].color())[0];
105 
106  name = (formatter() << font::span_color(tc) << name << "</span>").str();
107  }
108 
109  list_data["cat_name"]["label"] = name;
110  grid* grid = &cats_listbox.add_row(list_data);
111 
112  toggle_button& status = find_widget<toggle_button>(grid, "cat_status", false);
113  status.set_value(visible);
114 
115  connect_signal_notify_modified(status, std::bind(&label_settings::toggle_category, this, std::placeholders::_1, category));
116 
117  if(category.substr(0, 5) == "side:") {
118  label& cat_name = find_widget<label>(grid, "cat_name", false);
119  cat_name.set_use_markup(true);
120  }
121  }
122 }
123 
125 {
126  if(get_retval() == retval::OK) {
127  std::vector<std::string> hidden_categories;
128 
129  for(const auto& lbl : all_labels_) {
130  if(lbl.second == false) {
131  hidden_categories.push_back(lbl.first);
132  }
133  }
134 
135  viewer_.hidden_label_categories().swap(hidden_categories);
136  }
137 }
138 
139 void label_settings::toggle_category(widget& box, const std::string& category)
140 {
141  all_labels_[category] = static_cast<toggle_button&>(box).get_value_bool();
142 }
143 
144 } // namespace dialogs
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:91
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
void toggle_category(widget &box, const std::string &category)
Callback for toggling a checkbox state.
const std::vector< std::string > & all_categories() const
Definition: label.cpp:286
const std::string & side_name() const
Definition: team.hpp:319
This file contains the window object, this object is a top level container which has the event manage...
Base class for all widgets.
Definition: widget.hpp:48
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
static std::string _(const char *str)
Definition: gettext.hpp:92
The listbox class.
Definition: listbox.hpp:42
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
Base container class.
Definition: grid.hpp:30
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:71
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:186
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void set_use_markup(bool use_markup)
map_display and display: classes which take care of displaying the map and game-data on the screen...
bool hidden() const
Definition: team.hpp:359
std::map< std::string, bool > all_labels_
The execute function.
std::size_t i
Definition: function.cpp:940
virtual const std::vector< team > & teams() const =0
std::map< std::string, t_string > string_map
Definition: widget.hpp:25
grid & 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:67
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool grid()
Definition: general.cpp:524
int get_retval() const
Returns the cached window exit code.
const std::vector< color_t > & tc_info(const std::string &name)
std::map< std::string, t_string > labels_display_
map_labels & labels()
Definition: display.cpp:2535
Dialog was closed with the OK button.
Definition: retval.hpp:34
virtual const std::vector< std::string > & hidden_label_categories() const =0
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
Class for a toggle button.
const t_string & user_team_name() const
Definition: team.hpp:309