The Battle for Wesnoth  1.15.1+dev
editor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Tomasz Sniatowski <kailoran@gmail.com>
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 #include "preferences/editor.hpp"
16 #include "config.hpp"
17 #include "preferences/game.hpp"
18 #include "lexical_cast.hpp"
19 
20 namespace preferences {
21 
22 namespace editor {
23 
25  return lexical_cast_default<int>(preferences::get("editor_auto_update_transitions"), TRANSITION_UPDATE_PARTIAL);
26  }
27 
28  void set_auto_update_transitions(int value) {
29  preferences::set("editor_auto_update_transitions", std::to_string(value));
30  }
31 
32  std::string default_dir() {
33  return preferences::get("editor_default_dir");
34  }
35 
37  return preferences::get("editor_draw_terrain_codes", false);
38  }
39 
40  void set_draw_terrain_codes(bool value) {
41  preferences::set("editor_draw_terrain_codes", value);
42  }
43 
45  return preferences::get("editor_draw_hex_coordinates", false);
46  }
47 
48  void set_draw_hex_coordinates(bool value) {
49  preferences::set("editor_draw_hex_coordinates", value);
50  }
51 
53  return preferences::get("editor_draw_num_of_bitmaps", false);
54  }
55 
56  void set_draw_num_of_bitmaps(bool value) {
57  preferences::set("editor_draw_num_of_bitmaps", value);
58  }
59 
60  namespace {
61  std::size_t editor_mru_limit()
62  {
63  return std::max(std::size_t(1), lexical_cast_default<std::size_t>(
64  preferences::get("editor_max_recent_files"), 10));
65  }
66 
67  //
68  // NOTE: The MRU read/save functions enforce the entry count limit in
69  // order to ensure the list on disk doesn't grow forever. Otherwise,
70  // normally this would be the UI's responsibility instead.
71  //
72 
73  std::vector<std::string> do_read_editor_mru()
74  {
75  const config& cfg = preferences::get_child("editor_recent_files");
76 
77  std::vector<std::string> mru;
78  if(!cfg) {
79  return mru;
80  }
81 
82  for(const config& child : cfg.child_range("entry"))
83  {
84  const std::string& entry = child["path"].str();
85  if(!entry.empty()) {
86  mru.push_back(entry);
87  }
88  }
89 
90  mru.resize(std::min(editor_mru_limit(), mru.size()));
91 
92  return mru;
93  }
94 
95  void do_commit_editor_mru(const std::vector<std::string>& mru)
96  {
97  config cfg;
98  unsigned n = 0;
99 
100  for(const std::string& entry : mru)
101  {
102  if(entry.empty()) {
103  continue;
104  }
105 
106  config& child = cfg.add_child("entry");
107  child["path"] = entry;
108 
109  if(++n >= editor_mru_limit()) {
110  break;
111  }
112  }
113 
114  preferences::set_child("editor_recent_files", cfg);
115  }
116  }
117 
118  std::vector<std::string> recent_files()
119  {
120  return do_read_editor_mru();
121  }
122 
123  void add_recent_files_entry(const std::string& path)
124  {
125  if(path.empty()) {
126  return;
127  }
128 
129  std::vector<std::string> mru = do_read_editor_mru();
130 
131  // Enforce uniqueness. Normally shouldn't do a thing unless somebody
132  // has been tampering with the preferences file.
133  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
134 
135  mru.insert(mru.begin(), path);
136  mru.resize(std::min(editor_mru_limit(), mru.size()));
137 
138  do_commit_editor_mru(mru);
139  }
140 
141  void remove_recent_files_entry(const std::string& path)
142  {
143  if(path.empty()) {
144  return;
145  }
146 
147  std::vector<std::string> mru = do_read_editor_mru();
148 
149  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
150 
151  do_commit_editor_mru(mru);
152  }
153 
154 } //end namespace editor
155 
156 } //end namespace preferences
int auto_update_transitions()
Definition: editor.cpp:24
void remove()
Removes a tip.
Definition: tooltip.cpp:189
std::vector< std::string > recent_files()
Retrieves the list of recently opened files.
Definition: editor.cpp:118
bool draw_num_of_bitmaps()
Definition: editor.cpp:52
New lexcical_cast header.
child_itors child_range(config_key_type key)
Definition: config.cpp:362
void set(const std::string &key, bool value)
Definition: general.cpp:180
bool draw_terrain_codes()
Definition: editor.cpp:36
Definitions for the interface to Wesnoth Markup Language (WML).
std::string default_dir()
Definition: editor.cpp:32
std::string get(const std::string &key)
Definition: general.cpp:228
void set_auto_update_transitions(int value)
Definition: editor.cpp:28
std::string path
Definition: game_config.cpp:39
Modify, read and display user preferences.
bool draw_hex_coordinates()
Definition: editor.cpp:44
Manage the empty-palette in the editor.
Definition: action.cpp:29
void set_child(const std::string &key, const config &val)
Definition: general.cpp:210
void remove_recent_files_entry(const std::string &path)
Removes a single entry from the recent files list.
Definition: editor.cpp:141
const config & get_child(const std::string &key)
Definition: general.cpp:215
config & add_child(config_key_type key)
Definition: config.cpp:476
void set_draw_terrain_codes(bool value)
Definition: editor.cpp:40
void set_draw_num_of_bitmaps(bool value)
Definition: editor.cpp:56
void set_draw_hex_coordinates(bool value)
Definition: editor.cpp:48
void add_recent_files_entry(const std::string &path)
Adds an entry to the recent files list.
Definition: editor.cpp:123
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
static map_location::DIRECTION n