The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
editor_preferences.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Tomasz Sniatowski <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
16 #include "config.hpp"
17 #include "game_preferences.hpp"
19 #include "util.hpp"
20 
21 namespace preferences {
22 
23 namespace editor {
24 
26  return lexical_cast_default<int>(preferences::get("editor_auto_update_transitions"), TransitionUpdateMode::partial);
27  }
28 
30  preferences::set("editor_auto_update_transitions", std::to_string(value));
31  }
32 
34  return preferences::get("editor_default_dir");
35  }
36 
38  return preferences::get("editor_draw_terrain_codes", false);
39  }
40 
42  preferences::set("editor_draw_terrain_codes", value);
43  }
44 
46  return preferences::get("editor_draw_hex_coordinates", false);
47  }
48 
50  preferences::set("editor_draw_hex_coordinates", value);
51  }
52 
53  namespace {
54  size_t editor_mru_limit()
55  {
56  return std::max(size_t(1), lexical_cast_default<size_t>(
57  preferences::get("editor_max_recent_files"), 10));
58  }
59 
60  //
61  // NOTE: The MRU read/save functions enforce the entry count limit in
62  // order to ensure the list on disk doesn't grow forever. Otherwise,
63  // normally this would be the UI's responsibility instead.
64  //
65 
66  std::vector<std::string> do_read_editor_mru()
67  {
68  const config& cfg = preferences::get_child("editor_recent_files");
69 
70  std::vector<std::string> mru;
71  if(!cfg) {
72  return mru;
73  }
74 
75  for(const config& child : cfg.child_range("entry"))
76  {
77  const std::string& entry = child["path"].str();
78  if(!entry.empty()) {
79  mru.push_back(entry);
80  }
81  }
82 
83  mru.resize(std::min(editor_mru_limit(), mru.size()));
84 
85  return mru;
86  }
87 
88  void do_commit_editor_mru(const std::vector<std::string>& mru)
89  {
90  config cfg;
91  unsigned n = 0;
92 
93  for(const std::string& entry : mru)
94  {
95  if(entry.empty()) {
96  continue;
97  }
98 
99  config& child = cfg.add_child("entry");
100  child["path"] = entry;
101 
102  if(++n >= editor_mru_limit()) {
103  break;
104  }
105  }
106 
107  preferences::set_child("editor_recent_files", cfg);
108  }
109  }
110 
111  std::vector<std::string> recent_files()
112  {
113  return do_read_editor_mru();
114  }
115 
117  {
118  if(path.empty()) {
119  return;
120  }
121 
122  std::vector<std::string> mru = do_read_editor_mru();
123 
124  // Enforce uniqueness. Normally shouldn't do a thing unless somebody
125  // has been tampering with the preferences file.
126  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
127 
128  mru.insert(mru.begin(), path);
129  mru.resize(std::min(editor_mru_limit(), mru.size()));
130 
131  do_commit_editor_mru(mru);
132  }
133 
135  {
136  if(path.empty()) {
137  return;
138  }
139 
140  std::vector<std::string> mru = do_read_editor_mru();
141 
142  mru.erase(std::remove(mru.begin(), mru.end(), path), mru.end());
143 
144  do_commit_editor_mru(mru);
145  }
146 
147 } //end namespace editor
148 
149 } //end namespace preferences
150 
child_itors child_range(const std::string &key)
Definition: config.cpp:613
std::vector< std::string > recent_files()
Retrieves the list of recently opened files.
void set(const std::string &key, bool value)
Definitions for the interface to Wesnoth Markup Language (WML).
std::string get(const std::string &key)
GLsizei const char ** path
Definition: glew.h:4654
GLsizei const GLfloat * value
Definition: glew.h:1817
void set_auto_update_transitions(int value)
config & add_child(const std::string &key)
Definition: config.cpp:743
Modify, read and display user preferences.
Manage the empty-palette in the editor.
Definition: action.cpp:28
Templates and utility-routines for strings and numbers.
void set_child(const std::string &key, const config &val)
void remove_recent_files_entry(const std::string &path)
Removes a single entry from the recent files list.
const config & get_child(const std::string &key)
GLclampd n
Definition: glew.h:5903
void set_draw_terrain_codes(bool value)
const std::string remove
remove directive
void set_draw_hex_coordinates(bool value)
void add_recent_files_entry(const std::string &path)
Adds an entry to the recent files list.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503