]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
Center correctly top labels like Abstract.
[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 Branch::Branch()
29         : selected_(false), filenameSuffix_(false)
30 {
31         // no theApp() with command line export
32         if (theApp())
33                 theApp()->getRgbColor(Color_background, color_);
34         else
35                 frontend::Application::getRgbColorUncached(Color_background, color_);
36 }
37
38
39 docstring const & Branch::branch() const
40 {
41         return branch_;
42 }
43
44
45 void Branch::setBranch(docstring const & s)
46 {
47         branch_ = s;
48 }
49
50
51 bool Branch::isSelected() const
52 {
53         return selected_;
54 }
55
56
57 bool Branch::setSelected(bool b)
58 {
59         if (b == selected_)
60                 return false;
61         selected_ = b;
62         return true;
63 }
64
65
66 bool Branch::hasFileNameSuffix() const
67 {
68         return filenameSuffix_;
69 }
70
71
72 void Branch::setFileNameSuffix(bool b)
73 {
74         filenameSuffix_ = b;
75 }
76
77
78 RGBColor const & Branch::color() const
79 {
80         return color_;
81 }
82
83
84 void Branch::setColor(RGBColor const & c)
85 {
86         color_ = c;
87 }
88
89
90 void Branch::setColor(string const & str)
91 {
92         if (str.size() == 7 && str[0] == '#')
93                 color_ = rgbFromHexName(str);
94         else {
95                 // no color set or invalid color - use normal background
96                 // no theApp() with command line export
97                 if (theApp())
98                         theApp()->getRgbColor(Color_background, color_);
99                 else
100                         frontend::Application::getRgbColorUncached(Color_background, color_);
101         }
102 }
103
104
105 namespace {
106
107 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
108 {
109         return [d](Branch const & b){ return b.branch() == d; };
110 }
111
112 } // namespace
113
114
115 Branch * BranchList::find(docstring const & name)
116 {
117         List::iterator it =
118                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
119         return it == list_.end() ? nullptr : &*it;
120 }
121
122
123 Branch const * BranchList::find(docstring const & name) const
124 {
125         List::const_iterator it =
126                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
127         return it == list_.end() ? nullptr : &*it;
128 }
129
130
131 bool BranchList::add(docstring const & s)
132 {
133         bool added = false;
134         size_t i = 0;
135         while (true) {
136                 size_t const j = s.find_first_of(separator_, i);
137                 docstring name;
138                 if (j == docstring::npos)
139                         name = s.substr(i);
140                 else
141                         name = s.substr(i, j - i);
142                 // Is this name already in the list?
143                 bool const already = find(name);
144                 if (!already) {
145                         added = true;
146                         Branch br;
147                         br.setBranch(name);
148                         br.setSelected(false);
149                         br.setFileNameSuffix(false);
150                         list_.push_back(br);
151                 }
152                 if (j == docstring::npos)
153                         break;
154                 i = j + 1;
155         }
156         return added;
157 }
158
159
160 bool BranchList::remove(docstring const & s)
161 {
162         size_t const size = list_.size();
163         list_.remove_if(BranchNameIs(s));
164         return size != list_.size();
165 }
166
167
168 bool BranchList::rename(docstring const & oldname,
169         docstring const & newname, bool const merge)
170 {
171         if (newname.empty())
172                 return false;
173         if (find(newname)) {
174                 // new name already taken
175                 if (merge)
176                       return remove(oldname);
177                 return false;
178         }
179
180         Branch * branch = find(oldname);
181         if (!branch)
182                 return false;
183         branch->setBranch(newname);
184         return true;
185 }
186
187
188 docstring BranchList::getFileNameSuffix() const
189 {
190         docstring result;
191         for (auto const & br : list_) {
192                 if (br.isSelected() && br.hasFileNameSuffix())
193                         result += "-" + br.branch();
194         }
195         return support::subst(result, from_ascii("/"), from_ascii("_"));
196 }
197
198 } // namespace lyx