The Battle for Wesnoth  1.19.5+dev
edit_pbl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 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 "editor/editor_common.hpp"
20 #include "filesystem.hpp"
21 #include "gettext.hpp"
24 #include "gui/dialogs/message.hpp"
25 #include "gui/widgets/button.hpp"
26 #include "gui/widgets/drawing.hpp"
27 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/listbox.hpp"
31 #include "gui/widgets/text_box.hpp"
35 #include "serialization/parser.hpp"
38 
39 
40 namespace gui2::dialogs
41 {
42 
43 REGISTER_DIALOG(editor_edit_pbl)
44 
45 const std::array type_values = {
46  "",
47  "core",
48  "campaign",
49  "campaign_sp_mp",
50  "campaign_mp",
51  "scenario",
52  "scenario_mp",
53  "faction",
54  "era",
55  "map_pack",
56  "mod_mp",
57  "media",
58  "theme",
59  "other",
60 };
61 
62 const std::array tag_values = {
63  "cooperative",
64  "cosmetic",
65  "difficulty",
66  "rng",
67  "survival",
68  "terraforming",
69 };
70 
71 editor_edit_pbl::editor_edit_pbl(const std::string& pbl, const std::string& current_addon)
72  : modal_dialog(window_id())
73  , pbl_(pbl)
74  , current_addon_(current_addon)
75  , dirs_()
76 {
78  find_widget<toggle_button>("forum_auth"), std::bind(&editor_edit_pbl::toggle_auth, this));
79  connect_signal_mouse_left_click(find_widget<button>("translations_add"),
80  std::bind(&editor_edit_pbl::add_translation, this));
81  connect_signal_mouse_left_click(find_widget<button>("translations_delete"),
82  std::bind(&editor_edit_pbl::delete_translation, this));
84  find_widget<button>("validate"), std::bind(&editor_edit_pbl::validate, this));
86  find_widget<button>("select_icon"), std::bind(&editor_edit_pbl::select_icon_file, this));
88  find_widget<text_box>("icon"), std::bind(&editor_edit_pbl::update_icon_preview, this));
89  connect_signal_notify_modified(find_widget<text_box>("forum_thread"),
90  std::bind(&editor_edit_pbl::update_url_preview, this));
91  label& url = find_widget<label>("forum_url");
92  url.set_link_aware(true);
93  url.set_use_markup(true);
94  // not setting this to some value causes the modified signal to not update the label text
95  url.set_label("https://r.wesnoth.org/t");
96 }
97 
99 {
100  config pbl;
102  try {
103  read(pbl, *preprocess_file(pbl_));
104  } catch(const config::error& e) {
105  ERR_ED << "Caught a config error while parsing file " << pbl_ << "\n" << e.message;
106  }
107  }
108 
109  text_box* name = find_widget<text_box>("name", false, true);
110  name->set_value(pbl["title"]);
111  keyboard_capture(name);
112 
113  find_widget<scroll_text>("description").set_value(pbl["description"]);
114  find_widget<text_box>("icon").set_value(pbl["icon"]);
115  if(!pbl["icon"].empty()) {
116  drawing& img = find_widget<drawing>("preview");
117  img.set_label(pbl["icon"]);
118  }
119  find_widget<text_box>("author").set_value(pbl["author"]);
120  find_widget<text_box>("version").set_value(pbl["version"]);
121 
122  multimenu_button& dependencies = find_widget<multimenu_button>("dependencies");
123  std::vector<config> addons_list;
125  if(dirs_.size() > 0 && std::find(dirs_.begin(), dirs_.end(), current_addon_) != dirs_.end()) {
127  }
128 
129  for(const std::string& dir : dirs_) {
130  addons_list.emplace_back("label", dir, "checkbox", false);
131  }
132  dependencies.set_values(addons_list);
133 
134  std::vector<std::string> existing_dependencies = utils::split(pbl["dependencies"].str(), ',');
135  for(unsigned i = 0; i < dirs_.size(); i++) {
136  if(std::find(existing_dependencies.begin(), existing_dependencies.end(), dirs_[i]) != existing_dependencies.end()) {
137  dependencies.select_option(i);
138  }
139  }
140 
141  if(pbl["forum_auth"].to_bool()) {
142  find_widget<toggle_button>("forum_auth").set_value(true);
143  find_widget<text_box>("email").set_visible(gui2::widget::visibility::invisible);
144  find_widget<label>("email_label").set_visible(gui2::widget::visibility::invisible);
145  find_widget<text_box>("password").set_visible(gui2::widget::visibility::invisible);
146  find_widget<label>("password_label").set_visible(gui2::widget::visibility::invisible);
147  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::visible);
148  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::visible);
149  } else {
150  find_widget<text_box>("email").set_value(pbl["email"]);
151  find_widget<text_box>("password").set_value(pbl["passphrase"]);
152  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::invisible);
153  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::invisible);
154  }
155 
156  if(pbl.has_child("feedback")) {
157  find_widget<text_box>("forum_thread").set_value(pbl.mandatory_child("feedback")["topic_id"]);
158  }
159 
160  unsigned selected = 0;
161  for(unsigned i = 0; i < type_values.size(); i++) {
162  if(type_values[i] == pbl["type"]) {
163  selected = i;
164  break;
165  }
166  }
167 
168  menu_button& types = find_widget<menu_button>("type");
169  std::vector<config> type_list;
170  type_list.emplace_back("label", "");
171  type_list.emplace_back("label", _("Core"));
172  type_list.emplace_back("label", _("Campaign"));
173  type_list.emplace_back("label", _("Hybrid Campaign"));
174  type_list.emplace_back("label", _("Multiplayer Campaign"));
175  type_list.emplace_back("label", _("Scenario"));
176  type_list.emplace_back("label", _("Multiplayer Scenario"));
177  type_list.emplace_back("label", _("Faction"));
178  type_list.emplace_back("label", _("Era"));
179  type_list.emplace_back("label", _("Map Pack"));
180  type_list.emplace_back("label", _("Modification"));
181  type_list.emplace_back("label", _("Media"));
182  type_list.emplace_back("label", _("Theme"));
183  type_list.emplace_back("label", _("Other"));
184  types.set_values(type_list);
185  types.set_selected(selected);
186 
187  multimenu_button& tags = find_widget<multimenu_button>("tags");
188  std::vector<config> tags_list;
189  tags_list.emplace_back("label", _("Cooperative"), "checkbox", false);
190  tags_list.emplace_back("label", _("Cosmetic"), "checkbox", false);
191  tags_list.emplace_back("label", _("Difficulty"), "checkbox", false);
192  tags_list.emplace_back("label", _("RNG"), "checkbox", false);
193  tags_list.emplace_back("label", _("Survival"), "checkbox", false);
194  tags_list.emplace_back("label", _("Terraforming"), "checkbox", false);
195  tags.set_values(tags_list);
196 
197  std::vector<std::string> chosen_tags = utils::split(pbl["tags"].str(), ',');
198  for(unsigned i = 0; i < tag_values.size(); i++) {
199  if(std::find(chosen_tags.begin(), chosen_tags.end(), tag_values[i]) != chosen_tags.end()) {
200  tags.select_option(i);
201  }
202  }
203 
204  listbox& translations = find_widget<listbox>("translations");
205  button& translations_delete = find_widget<button>("translations_delete");
206 
207  for(const config& child : pbl.child_range("translation")) {
208  const widget_data& entry{
209  {"translations_language", widget_item{{"label", child["language"].str()}}},
210  {"translations_title", widget_item{{"label", child["title"].str()}}},
211  {"translations_description", widget_item{{"label", child["description"].str()}}},
212  };
213  translations.add_row(entry);
214  }
215 
216  if(translations.get_item_count() == 0) {
217  translations_delete.set_active(false);
218  }
219 }
220 
222 {
223  if(get_retval() != retval::OK) {
224  return;
225  }
226 
227  std::stringstream wml_stream;
228  config_writer out(wml_stream, false);
229  out.write(create_cfg());
230  filesystem::write_file(pbl_, wml_stream.str());
231 }
232 
234 {
235  config cfg;
236 
237  if(const std::string& name = find_widget<text_box>("name").get_value(); !name.empty()) {
238  cfg["title"] = name;
239  }
240  if(const std::string& description = find_widget<scroll_text>("description").get_value(); !description.empty()) {
241  cfg["description"] = description;
242  }
243  if(const std::string& icon = find_widget<text_box>("icon").get_value(); !icon.empty()) {
244  cfg["icon"] = icon;
245  }
246  if(const std::string& author = find_widget<text_box>("author").get_value(); !author.empty()) {
247  cfg["author"] = author;
248  }
249  if(const std::string& version = find_widget<text_box>("version").get_value(); !version.empty()) {
250  cfg["version"] = version;
251  }
252 
253  multimenu_button& dependencies = find_widget<multimenu_button>("dependencies");
254  boost::dynamic_bitset<> dep_states = dependencies.get_toggle_states();
255  std::vector<std::string> chosen_deps;
256  for(unsigned i = 0; i < dep_states.size(); i++) {
257  if(dep_states[i] == 1) {
258  chosen_deps.emplace_back(dirs_[i]);
259  }
260  }
261  if(chosen_deps.size() > 0) {
262  cfg["dependencies"] = utils::join(chosen_deps, ",");
263  }
264 
265  if(find_widget<toggle_button>("forum_auth").get_value_bool()) {
266  cfg["forum_auth"] = true;
267 
268  if(const std::string& secondary_authors = find_widget<text_box>("secondary_authors").get_value(); !secondary_authors.empty()) {
269  cfg["secondary_authors"] = secondary_authors;
270  }
271  } else {
272  if(const std::string& email = find_widget<text_box>("email").get_value(); !email.empty()) {
273  cfg["email"] = email;
274  }
275  if(const std::string& passphrase = find_widget<text_box>("password").get_value(); !passphrase.empty()) {
276  cfg["passphrase"] = passphrase;
277  }
278  }
279 
280  if(const std::string& topic_id = find_widget<text_box>("forum_thread").get_value(); !topic_id.empty()) {
281  config& feedback = cfg.add_child("feedback");
282  feedback["topic_id"] = topic_id;
283  }
284 
285  if(unsigned value = find_widget<menu_button>("type").get_value(); value != 0) {
286  cfg["type"] = type_values[value];
287  }
288 
289  multimenu_button& tags = find_widget<multimenu_button>("tags");
290  boost::dynamic_bitset<> tag_states = tags.get_toggle_states();
291  std::vector<std::string> chosen_tags;
292  for(unsigned i = 0; i < tag_states.size(); i++) {
293  if(tag_states[i] == 1) {
294  chosen_tags.emplace_back(tag_values[i]);
295  }
296  }
297  if(chosen_tags.size() > 0) {
298  cfg["tags"] = utils::join(chosen_tags, ",");
299  }
300 
301  listbox& translations = find_widget<listbox>("translations");
302  for(unsigned i = 0; i < translations.get_item_count(); i++) {
303  grid* row = translations.get_row_grid(i);
304  config& translation = cfg.add_child("translation");
305 
306  translation["language"] = dynamic_cast<label*>(row->find("translations_language", false))->get_label();
307  translation["title"] = dynamic_cast<label*>(row->find("translations_title", false))->get_label();
308  translation["description"] = dynamic_cast<label*>(row->find("translations_description", false))->get_label();
309  }
310 
311  return cfg;
312 }
313 
315 {
316  toggle_button& forum_auth = find_widget<toggle_button>("forum_auth");
317  if(forum_auth.get_value_bool()) {
318  find_widget<text_box>("email").set_visible(gui2::widget::visibility::invisible);
319  find_widget<text_box>("password").set_visible(gui2::widget::visibility::invisible);
320  find_widget<label>("email_label").set_visible(gui2::widget::visibility::invisible);
321  find_widget<label>("password_label").set_visible(gui2::widget::visibility::invisible);
322  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::visible);
323  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::visible);
324  } else {
325  find_widget<text_box>("email").set_visible(gui2::widget::visibility::visible);
326  find_widget<text_box>("password").set_visible(gui2::widget::visibility::visible);
327  find_widget<label>("email_label").set_visible(gui2::widget::visibility::visible);
328  find_widget<label>("password_label").set_visible(gui2::widget::visibility::visible);
329  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::invisible);
330  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::invisible);
331  }
332 }
333 
335 {
336  std::string language;
337  std::string title;
338  std::string description;
339  editor_edit_pbl_translation::execute(language, title, description);
340 
341  if(!language.empty() && !title.empty()) {
342  listbox& translations = find_widget<listbox>("translations");
343  const widget_data& entry{
344  {"translations_language", widget_item{{"label", language}}},
345  {"translations_title", widget_item{{"label", title}}},
346  {"translations_description", widget_item{{"label", description}}},
347  };
348  translations.add_row(entry);
349  find_widget<button>("translations_delete").set_active(true);
350  }
351 }
352 
354 {
355  listbox& translations = find_widget<listbox>("translations");
356  translations.remove_row(translations.get_selected_row());
357 
358  button& translations_delete = find_widget<button>("translations_delete");
359  if(translations.get_item_count() == 0) {
360  translations_delete.set_active(false);
361  }
362 }
363 
365 {
366  std::unique_ptr<schema_validation::schema_validator> validator;
367  validator.reset(new schema_validation::schema_validator(filesystem::get_wml_location("schema/pbl.cfg").value()));
368  validator->set_create_exceptions(false);
369 
370  config temp;
371  std::stringstream ss;
372  ss << create_cfg();
373  read(temp, ss.str(), validator.get());
374  if(!validator->get_errors().empty()) {
375  gui2::show_error_message(utils::join(validator->get_errors(), "\n"));
376  } else {
377  gui2::show_message(_("Success"), _("No validation errors"), gui2::dialogs::message::button_style::auto_close);
378  }
379 }
380 
382 {
383  std::string icon = find_widget<text_box>("icon").get_value();
384  if(icon.find(".png") != std::string::npos || icon.find(".jpg") != std::string::npos || icon.find(".webp") != std::string::npos) {
385  std::string path = filesystem::get_core_images_dir() + icon;
386  drawing& img = find_widget<drawing>("preview");
387 
388  if(filesystem::file_exists(path) || icon.find("data:image") != std::string::npos) {
389  img.set_label(icon);
390  } else {
391  img.set_label("");
392  ERR_ED << "Failed to find icon file: " << path;
393  }
394  }
395 }
396 
398 {
399  std::string topic = find_widget<text_box>("forum_thread").get_value();
400  find_widget<label>("forum_url").set_label("https://r.wesnoth.org/t" + topic);
401 }
402 
404 {
406 
407  dlg.set_title(_("Choose an icon")).set_path(filesystem::get_core_images_dir() + "/icons/");
408 
409  if(dlg.show()) {
410  std::string path = dlg.path();
411  if(path.find(filesystem::get_core_images_dir()) == 0) {
412  std::string icon = path.substr(filesystem::get_core_images_dir().length() + 1);
413  // setting this programmatically doesn't seem to trigger connect_signal_notify_modified()
414  find_widget<text_box>("icon").set_value(icon);
415  find_widget<drawing>("preview").set_label(icon);
416  } else {
417  std::string uri = filesystem::read_file_as_data_uri(path);
418 
419  if(!uri.empty()) {
420  find_widget<text_box>("icon").set_value(uri);
421  find_widget<drawing>("preview").set_label(uri);
422  }
423  }
424  }
425 }
426 
427 } // namespace gui2::dialogs
Class for writing a config out to a file in pieces.
void write(const config &cfg)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:366
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:316
child_itors child_range(config_key_type key)
Definition: config.cpp:272
config & add_child(config_key_type key)
Definition: config.cpp:440
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
editor_edit_pbl(const std::string &pbl, const std::string &current_addon)
Definition: edit_pbl.cpp:71
virtual void pre_show() override
The execute function.
Definition: edit_pbl.cpp:98
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition: edit_pbl.cpp:221
std::vector< std::string > dirs_
Definition: edit_pbl.hpp:50
file_dialog & set_path(const std::string &value)
Sets the initial file selection.
file_dialog & set_title(const std::string &value)
Sets the current dialog title text.
Definition: file_dialog.hpp:59
std::string path() const
Gets the current file selection.
Abstract base class for all modal dialogs.
bool show(const unsigned auto_close_time=0)
Shows the window.
int get_retval() const
Returns the cached window exit code.
Base container class.
Definition: grid.hpp:32
void set_active(const bool active)
Activates all children.
Definition: grid.cpp:167
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
void set_link_aware(bool l)
Definition: label.cpp:88
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:58
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:229
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
Definition: listbox.cpp:78
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:267
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:123
void set_selected(unsigned selected, bool fire_event=true)
void set_values(const std::vector<::config > &values, unsigned selected=0)
void set_values(const std::vector<::config > &values)
Set the available menu options.
void select_option(const unsigned option, const bool selected=true)
Select an option in the menu.
boost::dynamic_bitset get_toggle_states() const
Get the current state of the menu options.
const t_string & get_label() const
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
Realization of serialization/validator.hpp abstract validator.
Main (common) editor header.
#define ERR_ED
Declarations for File-IO.
std::size_t i
Definition: function.cpp:1028
static std::string _(const char *str)
Definition: gettext.hpp:93
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
Definition: filesystem.cpp:444
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:325
utils::optional< std::string > get_wml_location(const std::string &path, const utils::optional< std::string > &current_dir)
Returns a translated path to the actual file or directory, if it exists.
std::string read_file_as_data_uri(const std::string &fname)
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode)
Throws io_exception if an error occurs.
std::string get_addons_dir()
std::string get_core_images_dir()
std::string selected
std::string path
Definition: filesystem.cpp:90
const std::array type_values
Definition: edit_pbl.cpp:45
REGISTER_DIALOG(editor_edit_unit)
const std::array tag_values
Definition: edit_pbl.cpp:62
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
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:201
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:148
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string img(const std::string &src, const std::string &align, const bool floating)
Definition: markup.cpp:29
std::size_t erase(Container &container, const Value &value)
Convenience wrapper for using std::remove on a container.
Definition: general.hpp:111
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
One of the realizations of serialization/validator.hpp abstract validator.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:622
#define e