* qt_helpers.cpp:
[lyx.git] / src / BranchList.cpp
blob2afc765a3d53aba2b4d04a14f03c57a3d07f0015
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.
6 * \author Martin Vermeer
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "BranchList.h"
15 #include "Color.h"
17 #include "frontends/Application.h"
19 #include "support/lstrings.h"
21 #include <algorithm>
23 using namespace std;
26 namespace lyx {
28 namespace {
30 class BranchNamesEqual : public std::unary_function<Branch, bool>
32 public:
33 BranchNamesEqual(docstring const & name) : name_(name) {}
35 bool operator()(Branch const & branch) const
37 return branch.branch() == name_;
39 private:
40 docstring name_;
45 Branch::Branch()
46 : selected_(false), filenameSuffix_(false)
48 // no theApp() with command line export
49 if (theApp())
50 theApp()->getRgbColor(Color_background, color_);
54 docstring const & Branch::branch() const
56 return branch_;
60 void Branch::setBranch(docstring const & s)
62 branch_ = s;
66 bool Branch::isSelected() const
68 return selected_;
72 bool Branch::setSelected(bool b)
74 if (b == selected_)
75 return false;
76 selected_ = b;
77 return true;
81 bool Branch::hasFilenameSuffix() const
83 return filenameSuffix_;
87 void Branch::setFilenameSuffix(bool b)
89 filenameSuffix_ = b;
93 RGBColor const & Branch::color() const
95 return color_;
99 void Branch::setColor(RGBColor const & c)
101 color_ = c;
105 void Branch::setColor(string const & str)
107 if (str.size() == 7 && str[0] == '#')
108 color_ = rgbFromHexName(str);
109 else
110 // no color set or invalid color - use normal background
111 theApp()->getRgbColor(Color_background, color_);
115 Branch * BranchList::find(docstring const & name)
117 List::iterator it =
118 find_if(list.begin(), list.end(), BranchNamesEqual(name));
119 return it == list.end() ? 0 : &*it;
123 Branch const * BranchList::find(docstring const & name) const
125 List::const_iterator it =
126 find_if(list.begin(), list.end(), BranchNamesEqual(name));
127 return it == list.end() ? 0 : &*it;
131 bool BranchList::add(docstring const & s)
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 =
144 find_if(list.begin(), list.end(),
145 BranchNamesEqual(name)) != list.end();
146 if (!already) {
147 added = true;
148 Branch br;
149 br.setBranch(name);
150 br.setSelected(false);
151 br.setFilenameSuffix(false);
152 list.push_back(br);
154 if (j == docstring::npos)
155 break;
156 i = j + 1;
158 return added;
162 bool BranchList::remove(docstring const & s)
164 size_t const size = list.size();
165 list.remove_if(BranchNamesEqual(s));
166 return size != list.size();
170 bool BranchList::rename(docstring const & oldname,
171 docstring const & newname, bool const merge)
173 if (newname.empty())
174 return false;
175 if (find_if(list.begin(), list.end(),
176 BranchNamesEqual(newname)) != list.end()) {
177 // new name already taken
178 if (merge)
179 return remove(oldname);
180 return false;
183 Branch * branch = find(oldname);
184 if (!branch)
185 return false;
186 branch->setBranch(newname);
187 return true;
191 docstring BranchList::getFilenameSuffix() const
193 docstring result;
194 List::const_iterator it = list.begin();
195 for (; it != list.end(); ++it) {
196 if (it->isSelected() && it->hasFilenameSuffix())
197 result += "-" + it->branch();
199 return support::subst(result, from_ascii("/"), from_ascii("_"));
202 } // namespace lyx