The Battle for Wesnoth  1.19.7+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>("primary_authors").set_value(pbl["primary_authors"]);
144  find_widget<text_box>("secondary_authors").set_value(pbl["secondary_authors"]);
145  find_widget<text_box>("email").set_visible(gui2::widget::visibility::invisible);
146  find_widget<label>("email_label").set_visible(gui2::widget::visibility::invisible);
147  find_widget<text_box>("password").set_visible(gui2::widget::visibility::invisible);
148  find_widget<label>("password_label").set_visible(gui2::widget::visibility::invisible);
149  find_widget<text_box>("primary_authors").set_visible(gui2::widget::visibility::visible);
150  find_widget<label>("primary_authors_label").set_visible(gui2::widget::visibility::visible);
151  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::visible);
152  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::visible);
153  } else {
154  find_widget<text_box>("email").set_value(pbl["email"]);
155  find_widget<text_box>("password").set_value(pbl["passphrase"]);
156  find_widget<text_box>("primary_authors").set_visible(gui2::widget::visibility::invisible);
157  find_widget<label>("primary_authors_label").set_visible(gui2::widget::visibility::invisible);
158  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::invisible);
159  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::invisible);
160  }
161 
162  if(pbl.has_child("feedback")) {
163  find_widget<text_box>("forum_thread").set_value(pbl.mandatory_child("feedback")["topic_id"]);
164  }
165 
166  unsigned selected = 0;
167  for(unsigned i = 0; i < type_values.size(); i++) {
168  if(type_values[i] == pbl["type"]) {
169  selected = i;
170  break;
171  }
172  }
173 
174  menu_button& types = find_widget<menu_button>("type");
175  std::vector<config> type_list;
176  type_list.emplace_back("label", "");
177  type_list.emplace_back("label", _("Core"));
178  type_list.emplace_back("label", _("Campaign"));
179  type_list.emplace_back("label", _("Hybrid Campaign"));
180  type_list.emplace_back("label", _("Multiplayer Campaign"));
181  type_list.emplace_back("label", _("Scenario"));
182  type_list.emplace_back("label", _("Multiplayer Scenario"));
183  type_list.emplace_back("label", _("Faction"));
184  type_list.emplace_back("label", _("Era"));
185  type_list.emplace_back("label", _("Map Pack"));
186  type_list.emplace_back("label", _("Modification"));
187  type_list.emplace_back("label", _("Media"));
188  type_list.emplace_back("label", _("Theme"));
189  type_list.emplace_back("label", _("Other"));
190  types.set_values(type_list);
191  types.set_selected(selected);
192 
193  multimenu_button& tags = find_widget<multimenu_button>("tags");
194  std::vector<config> tags_list;
195  tags_list.emplace_back("label", _("Cooperative"), "checkbox", false);
196  tags_list.emplace_back("label", _("Cosmetic"), "checkbox", false);
197  tags_list.emplace_back("label", _("Difficulty"), "checkbox", false);
198  tags_list.emplace_back("label", _("RNG"), "checkbox", false);
199  tags_list.emplace_back("label", _("Survival"), "checkbox", false);
200  tags_list.emplace_back("label", _("Terraforming"), "checkbox", false);
201  tags.set_values(tags_list);
202 
203  std::vector<std::string> chosen_tags = utils::split(pbl["tags"].str(), ',');
204  for(unsigned i = 0; i < tag_values.size(); i++) {
205  if(std::find(chosen_tags.begin(), chosen_tags.end(), tag_values[i]) != chosen_tags.end()) {
206  tags.select_option(i);
207  }
208  }
209 
210  listbox& translations = find_widget<listbox>("translations");
211  button& translations_delete = find_widget<button>("translations_delete");
212 
213  for(const config& child : pbl.child_range("translation")) {
214  const widget_data& entry{
215  {"translations_language", widget_item{{"label", child["language"].str()}}},
216  {"translations_title", widget_item{{"label", child["title"].str()}}},
217  {"translations_description", widget_item{{"label", child["description"].str()}}},
218  };
219  translations.add_row(entry);
220  }
221 
222  if(translations.get_item_count() == 0) {
223  translations_delete.set_active(false);
224  }
225 }
226 
228 {
229  if(get_retval() != retval::OK) {
230  return;
231  }
232 
233  std::stringstream wml_stream;
234  config_writer out(wml_stream, false);
235  out.write(create_cfg());
236  filesystem::write_file(pbl_, wml_stream.str());
237 }
238 
240 {
241  config cfg;
242 
243  if(const std::string& name = find_widget<text_box>("name").get_value(); !name.empty()) {
244  cfg["title"] = name;
245  }
246  if(const std::string& description = find_widget<scroll_text>("description").get_value(); !description.empty()) {
247  cfg["description"] = description;
248  }
249  if(const std::string& icon = find_widget<text_box>("icon").get_value(); !icon.empty()) {
250  cfg["icon"] = icon;
251  }
252  if(const std::string& author = find_widget<text_box>("author").get_value(); !author.empty()) {
253  cfg["author"] = author;
254  }
255  if(const std::string& version = find_widget<text_box>("version").get_value(); !version.empty()) {
256  cfg["version"] = version;
257  }
258 
259  multimenu_button& dependencies = find_widget<multimenu_button>("dependencies");
260  boost::dynamic_bitset<> dep_states = dependencies.get_toggle_states();
261  std::vector<std::string> chosen_deps;
262  for(unsigned i = 0; i < dep_states.size(); i++) {
263  if(dep_states[i] == 1) {
264  chosen_deps.emplace_back(dirs_[i]);
265  }
266  }
267  if(chosen_deps.size() > 0) {
268  cfg["dependencies"] = utils::join(chosen_deps, ",");
269  }
270 
271  if(find_widget<toggle_button>("forum_auth").get_value_bool()) {
272  cfg["forum_auth"] = true;
273 
274  if(const std::string& primary_authors = find_widget<text_box>("primary_authors").get_value(); !primary_authors.empty()) {
275  cfg["primary_authors"] = primary_authors;
276  }
277 
278  if(const std::string& secondary_authors = find_widget<text_box>("secondary_authors").get_value(); !secondary_authors.empty()) {
279  cfg["secondary_authors"] = secondary_authors;
280  }
281  } else {
282  if(const std::string& email = find_widget<text_box>("email").get_value(); !email.empty()) {
283  cfg["email"] = email;
284  }
285  if(const std::string& passphrase = find_widget<text_box>("password").get_value(); !passphrase.empty()) {
286  cfg["passphrase"] = passphrase;
287  }
288  }
289 
290  if(const std::string& topic_id = find_widget<text_box>("forum_thread").get_value(); !topic_id.empty()) {
291  config& feedback = cfg.add_child("feedback");
292  feedback["topic_id"] = topic_id;
293  }
294 
295  if(unsigned value = find_widget<menu_button>("type").get_value(); value != 0) {
296  cfg["type"] = type_values[value];
297  }
298 
299  multimenu_button& tags = find_widget<multimenu_button>("tags");
300  boost::dynamic_bitset<> tag_states = tags.get_toggle_states();
301  std::vector<std::string> chosen_tags;
302  for(unsigned i = 0; i < tag_states.size(); i++) {
303  if(tag_states[i] == 1) {
304  chosen_tags.emplace_back(tag_values[i]);
305  }
306  }
307  if(chosen_tags.size() > 0) {
308  cfg["tags"] = utils::join(chosen_tags, ",");
309  }
310 
311  listbox& translations = find_widget<listbox>("translations");
312  for(unsigned i = 0; i < translations.get_item_count(); i++) {
313  grid* row = translations.get_row_grid(i);
314  config& translation = cfg.add_child("translation");
315 
316  translation["language"] = dynamic_cast<label*>(row->find("translations_language", false))->get_label();
317  translation["title"] = dynamic_cast<label*>(row->find("translations_title", false))->get_label();
318  translation["description"] = dynamic_cast<label*>(row->find("translations_description", false))->get_label();
319  }
320 
321  return cfg;
322 }
323 
325 {
326  toggle_button& forum_auth = find_widget<toggle_button>("forum_auth");
327  if(forum_auth.get_value_bool()) {
328  find_widget<text_box>("email").set_visible(gui2::widget::visibility::invisible);
329  find_widget<text_box>("password").set_visible(gui2::widget::visibility::invisible);
330  find_widget<label>("email_label").set_visible(gui2::widget::visibility::invisible);
331  find_widget<label>("password_label").set_visible(gui2::widget::visibility::invisible);
332  find_widget<text_box>("primary_authors").set_visible(gui2::widget::visibility::visible);
333  find_widget<label>("primary_authors_label").set_visible(gui2::widget::visibility::visible);
334  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::visible);
335  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::visible);
336  } else {
337  find_widget<text_box>("email").set_visible(gui2::widget::visibility::visible);
338  find_widget<text_box>("password").set_visible(gui2::widget::visibility::visible);
339  find_widget<label>("email_label").set_visible(gui2::widget::visibility::visible);
340  find_widget<label>("password_label").set_visible(gui2::widget::visibility::visible);
341  find_widget<text_box>("primary_authors").set_visible(gui2::widget::visibility::invisible);
342  find_widget<label>("primary_authors_label").set_visible(gui2::widget::visibility::invisible);
343  find_widget<text_box>("secondary_authors").set_visible(gui2::widget::visibility::invisible);
344  find_widget<label>("secondary_authors_label").set_visible(gui2::widget::visibility::invisible);
345  }
346 }
347 
349 {
350  std::string language;
351  std::string title;
352  std::string description;
353  editor_edit_pbl_translation::execute(language, title, description);
354 
355  if(!language.empty() && !title.empty()) {
356  listbox& translations = find_widget<listbox>("translations");
357  const widget_data& entry{
358  {"translations_language", widget_item{{"label", language}}},
359  {"translations_title", widget_item{{"label", title}}},
360  {"translations_description", widget_item{{"label", description}}},
361  };
362  translations.add_row(entry);
363  find_widget<button>("translations_delete").set_active(true);
364  }
365 }
366 
368 {
369  listbox& translations = find_widget<listbox>("translations");
370  translations.remove_row(translations.get_selected_row());
371 
372  button& translations_delete = find_widget<button>("translations_delete");
373  if(translations.get_item_count() == 0) {
374  translations_delete.set_active(false);
375  }
376 }
377 
379 {
380  std::unique_ptr<schema_validation::schema_validator> validator;
381  validator.reset(new schema_validation::schema_validator(filesystem::get_wml_location("schema/pbl.cfg").value()));
382  validator->set_create_exceptions(false);
383 
384  config temp;
385  std::stringstream ss;
386  ss << create_cfg();
387  read(temp, ss.str(), validator.get());
388  if(!validator->get_errors().empty()) {
389  gui2::show_error_message(utils::join(validator->get_errors(), "\n"));
390  } else {
391  gui2::show_message(_("Success"), _("No validation errors"), gui2::dialogs::message::button_style::auto_close);
392  }
393 }
394 
396 {
397  std::string icon = find_widget<text_box>("icon").get_value();
398  if(icon.find(".png") != std::string::npos || icon.find(".jpg") != std::string::npos || icon.find(".webp") != std::string::npos) {
399  std::string path = filesystem::get_core_images_dir() + icon;
400  drawing& img = find_widget<drawing>("preview");
401 
402  if(filesystem::file_exists(path) || icon.find("data:image") != std::string::npos) {
403  img.set_label(icon);
404  } else {
405  img.set_label("");
406  ERR_ED << "Failed to find icon file: " << path;
407  }
408  }
409 }
410 
412 {
413  std::string topic = find_widget<text_box>("forum_thread").get_value();
414  find_widget<label>("forum_url").set_label("https://r.wesnoth.org/t" + topic);
415 }
416 
418 {
420 
421  dlg.set_title(_("Choose an icon")).set_path(filesystem::get_core_images_dir() + "/icons/");
422 
423  if(dlg.show()) {
424  std::string path = dlg.path();
425  if(path.find(filesystem::get_core_images_dir()) == 0) {
426  std::string icon = path.substr(filesystem::get_core_images_dir().length() + 1);
427  // setting this programmatically doesn't seem to trigger connect_signal_notify_modified()
428  find_widget<text_box>("icon").set_value(icon);
429  find_widget<drawing>("preview").set_label(icon);
430  } else {
431  std::string uri = filesystem::read_file_as_data_uri(path);
432 
433  if(!uri.empty()) {
434  find_widget<text_box>("icon").set_value(uri);
435  find_widget<drawing>("preview").set_label(uri);
436  }
437  }
438  }
439 }
440 
441 } // 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:227
std::vector< std::string > dirs_
Definition: edit_pbl.hpp:46
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_view 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:41
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:92
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:267
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
Definition: listbox.cpp:112
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:305
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:159
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:1029
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:445
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:326
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:91
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:117
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)
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:140
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:629
#define e