The Battle for Wesnoth  1.19.0-dev
editor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Tomasz Sniatowski <kailoran@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "preferences/editor.hpp"
17 #include "config.hpp"
18 #include "preferences/game.hpp"
19 #include "lexical_cast.hpp"
20 
21 namespace preferences {
22 
23 namespace editor {
24 
26  return lexical_cast_default<int>(preferences::get("editor_auto_update_transitions"), TRANSITION_UPDATE_PARTIAL);
27  }
28 
29  void set_auto_update_transitions(int value) {
30  preferences::set("editor_auto_update_transitions", std::to_string(value));
31  }
32 
33  std::string default_dir() {
34  return preferences::get("editor_default_dir");
35  }
36 
38  return preferences::get("editor_draw_terrain_codes", false);
39  }
40 
41  void set_draw_terrain_codes(bool value) {
42  preferences::set("editor_draw_terrain_codes", value);
43  }
44 
46  return preferences::get("editor_draw_hex_coordinates", false);
47  }
48 
49  void set_draw_hex_coordinates(bool value) {
50  preferences::set("editor_draw_hex_coordinates", value);
51  }
52 
54  return preferences::get("editor_draw_num_of_bitmaps", false);
55  }
56 
57  void set_draw_num_of_bitmaps(bool value) {
58  preferences::set("editor_draw_num_of_bitmaps", value);
59  }
60 
61  namespace {
62  std::size_t editor_mru_limit()
63  {
64  return std::max(std::size_t(1), lexical_cast_default<std::size_t>(
65  preferences::get("editor_max_recent_files"), 10));
66  }
67 
68  //
69  // NOTE: The MRU read/save functions enforce the entry count limit in
70  // order to ensure the list on disk doesn't grow forever. Otherwise,
71  // normally this would be the UI's responsibility instead.
72  //
73 
74  std::vector<std::string> do_read_editor_mru()
75  {
76  auto cfg = preferences::get_child("editor_recent_files");
77 
78  std::vector<std::string> mru;
79  if(!cfg) {
80  return mru;
81  }
82 
83  for(const config& child : cfg->child_range("entry"))
84  {
85  const std::string& entry = child["path"].str();
86  if(!entry.empty()) {
87  mru.push_back(entry);
88  }
89  }
90 
91  mru.resize(std::min(editor_mru_limit(), mru.size()));
92 
93  return mru;
94  }
95 
96  void do_commit_editor_mru(const std::vector<std::string>& mru)
97  {
98  config cfg;
99  unsigned n = 0;
100 
101  for(const std::string& entry : mru)
102  {
103  if(entry.empty()) {
104  continue;
105  }
106 
107  config& child = cfg.add_child("entry");
108  child["path"] = entry;
109 
110  if(++n >= editor_mru_limit()) {
111  break;
112  }
113  }
114 
115  preferences::set_child("editor_recent_files", cfg);
116  }
117  }
118 
119  std::vector<std::string> recent_files()
120  {
121  return do_read_editor_mru();
122  }
123 
124  void add_recent_files_entry(const std::string& path)
125  {
126  if(path.empty()) {
127  return;
128  }
129 
130  std::vector<std::string> mru = do_read_editor_mru();
131 
132  // Enforce uniqueness. Normally shouldn't do a thing unless somebody
133  // has been tampering with the preferences file.
134  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
135 
136  mru.insert(mru.begin(), path);
137  mru.resize(std::min(editor_mru_limit(), mru.size()));
138 
139  do_commit_editor_mru(mru);
140  }
141 
142  void remove_recent_files_entry(const std::string& path)
143  {
144  if(path.empty()) {
145  return;
146  }
147 
148  std::vector<std::string> mru = do_read_editor_mru();
149 
150  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
151 
152  do_commit_editor_mru(mru);
153  }
154 
155 } //end namespace editor
156 
157 } //end namespace preferences
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
child_itors child_range(config_key_type key)
Definition: config.cpp:273
config & add_child(config_key_type key)
Definition: config.cpp:441
New lexcical_cast header.
Manage the empty-palette in the editor.
Definition: action.cpp:31
std::string path
Definition: filesystem.cpp:83
void remove()
Removes a tip.
Definition: tooltip.cpp:109
void set_draw_num_of_bitmaps(bool value)
Definition: editor.cpp:57
std::string default_dir()
Definition: editor.cpp:33
bool draw_terrain_codes()
Definition: editor.cpp:37
void set_auto_update_transitions(int value)
Definition: editor.cpp:29
void add_recent_files_entry(const std::string &path)
Adds an entry to the recent files list.
Definition: editor.cpp:124
bool draw_hex_coordinates()
Definition: editor.cpp:45
int auto_update_transitions()
Definition: editor.cpp:25
void remove_recent_files_entry(const std::string &path)
Removes a single entry from the recent files list.
Definition: editor.cpp:142
bool draw_num_of_bitmaps()
Definition: editor.cpp:53
void set_draw_hex_coordinates(bool value)
Definition: editor.cpp:49
std::vector< std::string > recent_files()
Retrieves the list of recently opened files.
Definition: editor.cpp:119
void set_draw_terrain_codes(bool value)
Definition: editor.cpp:41
Modify, read and display user preferences.
void set(const std::string &key, bool value)
Definition: general.cpp:165
optional_const_config get_child(const std::string &key)
Definition: general.cpp:200
void set_child(const std::string &key, const config &val)
Definition: general.cpp:195
std::string get(const std::string &key)
Definition: general.cpp:213
static map_location::DIRECTION n