The Battle for Wesnoth  1.19.0-dev
label_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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"
28 #include "gui/widgets/window.hpp"
29 #include "map/label.hpp"
30 #include "team.hpp"
31 
32 #include <vector>
33 
34 namespace gui2::dialogs
35 {
36 REGISTER_DIALOG(label_settings)
37 
39  : modal_dialog(window_id())
40  , viewer_(dc)
41 {
42  const std::vector<std::string>& all_categories = display::get_singleton()->labels().all_categories();
43  const std::vector<std::string>& hidden_categories = viewer_.hidden_label_categories();
44 
45  for(const std::string& cat : all_categories) {
46  all_labels_[cat] = true;
47 
48  // TODO: Translatable names for categories?
49  if(cat.substr(0, 4) == "cat:") {
50  labels_display_[cat] = cat.substr(4);
51  } else if(cat == "team") {
52  labels_display_[cat] = _("Team Labels");
53  }
54  }
55 
56  for(const std::string& hidden_cat : hidden_categories) {
57  all_labels_[hidden_cat] = false;
58  }
59 
60  for(std::size_t i = 0; i < viewer_.teams().size(); i++) {
61  const team& team = viewer_.teams()[i];
62  const std::string label_cat_key = "side:" + std::to_string(i + 1);
63 
64  if(team.hidden()) {
65  labels_display_[label_cat_key] = "";
66  continue;
67 
68  }
69 
70  std::string team_name = team.side_name();
71  if(team_name.empty()) {
72  team_name = team.user_team_name();
73  }
74 
75  if(team_name.empty()) {
76  team_name = _("Unknown");
77  }
78 
79  utils::string_map subst;
80  subst["side_number"] = std::to_string(i + 1);
81  subst["name"] = team_name;
82  labels_display_[label_cat_key] = VGETTEXT("Side $side_number ($name)", subst);
83  }
84 }
85 
87 {
88  listbox& cats_listbox = find_widget<listbox>(&window, "label_types", false);
89  widget_data list_data;
90 
91  for(const auto& label_entry : all_labels_) {
92  const std::string& category = label_entry.first;
93  const bool visible = label_entry.second;
94 
95  std::string name = labels_display_[category];
96  if(category.substr(0, 5) == "side:") {
97  // This means it's a hidden side, so don't show it.
98  if(name.empty()) {
99  continue;
100  }
101 
102  const int team = std::stoi(category.substr(5)) - 1;
103  const color_t tc = game_config::tc_info(viewer_.teams()[team].color())[0];
104 
105  name = (formatter() << font::span_color(tc) << name << "</span>").str();
106  }
107 
108  list_data["cat_name"]["label"] = name;
109  grid* grid = &cats_listbox.add_row(list_data);
110 
111  toggle_button& status = find_widget<toggle_button>(grid, "cat_status", false);
112  status.set_value(visible);
113 
114  connect_signal_notify_modified(status, std::bind(&label_settings::toggle_category, this, std::placeholders::_1, category));
115 
116  if(category.substr(0, 5) == "side:") {
117  label& cat_name = find_widget<label>(grid, "cat_name", false);
118  cat_name.set_use_markup(true);
119  }
120  }
121 }
122 
124 {
125  if(get_retval() == retval::OK) {
126  std::vector<std::string> hidden_categories;
127 
128  for(const auto& lbl : all_labels_) {
129  if(lbl.second == false) {
130  hidden_categories.push_back(lbl.first);
131  }
132  }
133 
134  viewer_.hidden_label_categories().swap(hidden_categories);
135  }
136 }
137 
138 void label_settings::toggle_category(widget& box, const std::string& category)
139 {
140  all_labels_[category] = static_cast<toggle_button&>(box).get_value_bool();
141 }
142 
143 } // namespace dialogs
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
virtual const std::vector< std::string > & hidden_label_categories() const =0
virtual const std::vector< team > & teams() const =0
map_labels & labels()
Definition: display.cpp:2619
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:102
std::ostringstream wrapper.
Definition: formatter.hpp:40
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
std::map< std::string, t_string > labels_display_
void toggle_category(widget &box, const std::string &category)
Callback for toggling a checkbox state.
std::map< std::string, bool > all_labels_
The execute function.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
Base container class.
Definition: grid.hpp:32
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
The listbox class.
Definition: listbox.hpp:43
grid & add_row(const widget_item &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:59
virtual void set_use_markup(bool use_markup)
Class for a toggle button.
Base class for all widgets.
Definition: widget.hpp:53
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
status
The status of the window.
Definition: window.hpp:210
const std::vector< std::string > & all_categories() const
Definition: label.cpp:287
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
const std::string & side_name() const
Definition: team.hpp:293
bool hidden() const
Definition: team.hpp:333
const t_string & user_team_name() const
Definition: team.hpp:283
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::size_t i
Definition: function.cpp:968
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
const std::vector< color_t > & tc_info(std::string_view name)
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::map< std::string, t_string > string_map
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59