]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
Rename a variable
[lyx.git] / src / BranchList.cpp
1 /**
2  * \file BranchList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BranchList.h"
15 #include "Color.h"
16
17 #include "frontends/Application.h"
18
19 #include "support/lstrings.h"
20
21 #include <algorithm>
22
23 using namespace std;
24
25
26 namespace lyx {
27
28 namespace {
29
30 class BranchNamesEqual : public std::unary_function<Branch, bool>
31 {
32 public:
33         BranchNamesEqual(docstring const & name)
34                 : name_(name)
35         {}
36
37         bool operator()(Branch const & branch) const
38         {
39                 return branch.branch() == name_;
40         }
41 private:
42         docstring name_;
43 };
44
45 } // namespace
46
47
48 Branch::Branch()
49         : selected_(false), filenameSuffix_(false)
50 {
51         // no theApp() with command line export
52         if (theApp())
53                 theApp()->getRgbColor(Color_background, color_);
54         else
55                 frontend::Application::getRgbColorUncached(Color_background, color_);
56 }
57
58
59 docstring const & Branch::branch() const
60 {
61         return branch_;
62 }
63
64
65 void Branch::setBranch(docstring const & s)
66 {
67         branch_ = s;
68 }
69
70
71 bool Branch::isSelected() const
72 {
73         return selected_;
74 }
75
76
77 bool Branch::setSelected(bool b)
78 {
79         if (b == selected_)
80                 return false;
81         selected_ = b;
82         return true;
83 }
84
85
86 bool Branch::hasFileNameSuffix() const
87 {
88         return filenameSuffix_;
89 }
90
91
92 void Branch::setFileNameSuffix(bool b)
93 {
94         filenameSuffix_ = b;
95 }
96
97
98 RGBColor const & Branch::color() const
99 {
100         return color_;
101 }
102
103
104 void Branch::setColor(RGBColor const & c)
105 {
106         color_ = c;
107 }
108
109
110 void Branch::setColor(string const & str)
111 {
112         if (str.size() == 7 && str[0] == '#')
113                 color_ = rgbFromHexName(str);
114         else {
115                 // no color set or invalid color - use normal background
116                 // no theApp() with command line export
117                 if (theApp())
118                         theApp()->getRgbColor(Color_background, color_);
119                 else
120                         frontend::Application::getRgbColorUncached(Color_background, color_);
121         }
122 }
123
124
125 Branch * BranchList::find(docstring const & name)
126 {
127         List::iterator it =
128                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
129         return it == list.end() ? 0 : &*it;
130 }
131
132
133 Branch const * BranchList::find(docstring const & name) const
134 {
135         List::const_iterator it =
136                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
137         return it == list.end() ? 0 : &*it;
138 }
139
140
141 bool BranchList::add(docstring const & s)
142 {
143         bool added = false;
144         size_t i = 0;
145         while (true) {
146                 size_t const j = s.find_first_of(separator_, i);
147                 docstring name;
148                 if (j == docstring::npos)
149                         name = s.substr(i);
150                 else
151                         name = s.substr(i, j - i);
152                 // Is this name already in the list?
153                 bool const already =
154                         find_if(list.begin(), list.end(),
155                                      BranchNamesEqual(name)) != list.end();
156                 if (!already) {
157                         added = true;
158                         Branch br;
159                         br.setBranch(name);
160                         br.setSelected(false);
161                         br.setFileNameSuffix(false);
162                         list.push_back(br);
163                 }
164                 if (j == docstring::npos)
165                         break;
166                 i = j + 1;
167         }
168         return added;
169 }
170
171
172 bool BranchList::remove(docstring const & s)
173 {
174         size_t const size = list.size();
175         list.remove_if(BranchNamesEqual(s));
176         return size != list.size();
177 }
178
179
180 bool BranchList::rename(docstring const & oldname,
181         docstring const & newname, bool const merge)
182 {
183         if (newname.empty())
184                 return false;
185         if (find_if(list.begin(), list.end(),
186                     BranchNamesEqual(newname)) != list.end()) {
187                 // new name already taken
188                 if (merge)
189                       return remove(oldname);
190                 return false;
191         }
192
193         Branch * branch = find(oldname);
194         if (!branch)
195                 return false;
196         branch->setBranch(newname);
197         return true;
198 }
199
200
201 docstring BranchList::getFileNameSuffix() const
202 {
203         docstring result;
204         List::const_iterator it = list.begin();
205         for (; it != list.end(); ++it) {
206                 if (it->isSelected() && it->hasFileNameSuffix())
207                         result += "-" + it->branch();
208         }
209         return support::subst(result, from_ascii("/"), from_ascii("_"));
210 }
211
212 } // namespace lyx