2003-08-17 11:28:23 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file BranchList.cpp
|
2003-08-17 11:28:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
2003-08-17 11:28:23 +00:00
|
|
|
* \author Martin Vermeer
|
2009-07-13 14:30:08 +00:00
|
|
|
* \author Jürgen Spitzmüller
|
2003-09-09 17:25:35 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-08-17 11:28:23 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-23 00:17:00 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
#include "BranchList.h"
|
2007-11-02 23:42:27 +00:00
|
|
|
#include "Color.h"
|
2021-01-18 08:56:53 +00:00
|
|
|
#include "ColorSet.h"
|
2006-10-12 14:10:13 +00:00
|
|
|
|
|
|
|
#include "frontends/Application.h"
|
|
|
|
|
2021-01-21 07:09:41 +00:00
|
|
|
#include "support/convert.h"
|
2009-07-13 14:30:08 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2005-01-18 14:15:57 +00:00
|
|
|
#include <algorithm>
|
2003-09-09 17:25:35 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
docstring const & Branch::branch() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
return branch_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
void Branch::setBranch(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
branch_ = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
bool Branch::isSelected() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
return selected_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-14 16:33:56 +00:00
|
|
|
bool Branch::setSelected(bool b)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
if (b == selected_)
|
|
|
|
return false;
|
2003-08-17 11:28:23 +00:00
|
|
|
selected_ = b;
|
2003-12-14 16:33:56 +00:00
|
|
|
return true;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
bool Branch::hasFileNameSuffix() const
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
return filenameSuffix_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
void Branch::setFileNameSuffix(bool b)
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
filenameSuffix_ = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-18 08:56:53 +00:00
|
|
|
string const & Branch::color() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2021-01-19 16:04:04 +00:00
|
|
|
return (theApp() && theApp()->isInDarkMode())
|
|
|
|
? dmcolor_ : lmcolor_;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-19 16:04:04 +00:00
|
|
|
string const & Branch::lightModeColor() const
|
2006-03-23 20:04:05 +00:00
|
|
|
{
|
2021-01-19 16:04:04 +00:00
|
|
|
return lmcolor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const & Branch::darkModeColor() const
|
|
|
|
{
|
|
|
|
return dmcolor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Branch::setColor(string const & col)
|
|
|
|
{
|
|
|
|
if (theApp() && theApp()->isInDarkMode())
|
|
|
|
setColors(string(), col);
|
|
|
|
else
|
|
|
|
setColors(col);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Branch::setColors(string const & lmcol, string const & dmcol)
|
|
|
|
{
|
|
|
|
if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#"))
|
|
|
|
lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol)));
|
|
|
|
else if (!lmcol.empty())
|
|
|
|
lmcolor_ = lmcol;
|
|
|
|
if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#"))
|
|
|
|
dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol)));
|
|
|
|
else if (!dmcol.empty())
|
|
|
|
dmcolor_ = dmcol;
|
2021-01-19 08:22:07 +00:00
|
|
|
|
|
|
|
// Update the Color table
|
2021-01-19 16:04:04 +00:00
|
|
|
string lmcolor = lmcolor_;
|
|
|
|
string dmcolor = dmcolor_;
|
|
|
|
if (lmcolor == "none")
|
|
|
|
lmcolor = lcolor.getX11HexName(Color_background);
|
|
|
|
else if (lmcolor.size() != 7 || lmcolor[0] != '#')
|
|
|
|
lmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(lmcolor));
|
|
|
|
if (dmcolor == "none")
|
|
|
|
lmcolor = lcolor.getX11HexName(Color_background, true);
|
|
|
|
else if (dmcolor.size() != 7 || dmcolor[0] != '#')
|
|
|
|
dmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(dmcolor), true);
|
|
|
|
|
2021-01-19 08:22:07 +00:00
|
|
|
// FIXME UNICODE
|
2021-01-21 07:09:41 +00:00
|
|
|
lcolor.setColor("branch" + convert<string>(branch_list_id_)
|
|
|
|
+ to_utf8(branch_), lmcolor, dmcolor);
|
2006-03-23 20:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-04 23:08:12 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-05-05 01:21:42 +00:00
|
|
|
std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
|
2020-05-04 23:08:12 +00:00
|
|
|
{
|
|
|
|
return [d](Branch const & b){ return b.branch() == d; };
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
Branch * BranchList::find(docstring const & name)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
List::iterator it =
|
2020-05-05 01:21:42 +00:00
|
|
|
find_if(list_.begin(), list_.end(), BranchNameIs(name));
|
2020-05-04 23:08:12 +00:00
|
|
|
return it == list_.end() ? nullptr : &*it;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
2004-01-05 15:49:40 +00:00
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
Branch const * BranchList::find(docstring const & name) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
List::const_iterator it =
|
2020-05-05 01:21:42 +00:00
|
|
|
find_if(list_.begin(), list_.end(), BranchNameIs(name));
|
2020-05-04 23:08:12 +00:00
|
|
|
return it == list_.end() ? nullptr : &*it;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
2004-01-05 15:49:40 +00:00
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
bool BranchList::add(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
bool added = false;
|
2008-09-21 19:27:20 +00:00
|
|
|
size_t i = 0;
|
2003-08-17 11:28:23 +00:00
|
|
|
while (true) {
|
2008-09-21 19:27:20 +00:00
|
|
|
size_t const j = s.find_first_of(separator_, i);
|
2006-11-03 15:16:45 +00:00
|
|
|
docstring name;
|
|
|
|
if (j == docstring::npos)
|
2003-08-17 11:28:23 +00:00
|
|
|
name = s.substr(i);
|
|
|
|
else
|
|
|
|
name = s.substr(i, j - i);
|
|
|
|
// Is this name already in the list?
|
2020-05-04 23:08:12 +00:00
|
|
|
bool const already = find(name);
|
2003-08-17 11:28:23 +00:00
|
|
|
if (!already) {
|
2003-12-14 16:33:56 +00:00
|
|
|
added = true;
|
2003-08-17 11:28:23 +00:00
|
|
|
Branch br;
|
|
|
|
br.setBranch(name);
|
|
|
|
br.setSelected(false);
|
2010-04-21 01:19:35 +00:00
|
|
|
br.setFileNameSuffix(false);
|
2021-01-21 07:09:41 +00:00
|
|
|
br.setListID(id_);
|
2020-02-28 06:08:28 +00:00
|
|
|
list_.push_back(br);
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2006-11-03 15:16:45 +00:00
|
|
|
if (j == docstring::npos)
|
2003-08-17 11:28:23 +00:00
|
|
|
break;
|
2003-09-09 17:25:35 +00:00
|
|
|
i = j + 1;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2003-12-14 16:33:56 +00:00
|
|
|
return added;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
bool BranchList::remove(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2020-02-28 06:08:28 +00:00
|
|
|
size_t const size = list_.size();
|
2020-05-05 01:21:42 +00:00
|
|
|
list_.remove_if(BranchNameIs(s));
|
2020-02-28 06:08:28 +00:00
|
|
|
return size != list_.size();
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2009-07-10 06:49:51 +00:00
|
|
|
bool BranchList::rename(docstring const & oldname,
|
|
|
|
docstring const & newname, bool const merge)
|
|
|
|
{
|
|
|
|
if (newname.empty())
|
|
|
|
return false;
|
2020-05-04 23:41:18 +00:00
|
|
|
if (find(newname)) {
|
2009-07-10 06:49:51 +00:00
|
|
|
// new name already taken
|
|
|
|
if (merge)
|
2021-01-21 07:09:41 +00:00
|
|
|
return remove(oldname);
|
2009-07-10 06:49:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Branch * branch = find(oldname);
|
|
|
|
if (!branch)
|
|
|
|
return false;
|
|
|
|
branch->setBranch(newname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
docstring BranchList::getFileNameSuffix() const
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
docstring result;
|
2020-02-28 06:08:28 +00:00
|
|
|
for (auto const & br : list_) {
|
|
|
|
if (br.isSelected() && br.hasFileNameSuffix())
|
|
|
|
result += "-" + br.branch();
|
2009-07-13 14:30:08 +00:00
|
|
|
}
|
|
|
|
return support::subst(result, from_ascii("/"), from_ascii("_"));
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|