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