Rework default bibliography style UI

* Use a combo with all available styles
* Correctly reset default on engine change
* Consider that EngineTypes might have different defaults
This commit is contained in:
Juergen Spitzmueller 2016-12-31 14:57:09 +01:00
parent 59a4f263a4
commit ff93aea509
11 changed files with 256 additions and 72 deletions

View File

@ -14,7 +14,7 @@ Format 62
Requires natbib
CiteEngineType authoryear|numerical
DefaultBiblio plainnat
DefaultBiblio authoryear:plainnat|numerical:plainnat
CiteEngine authoryear
Citet*[][]

View File

@ -1511,7 +1511,7 @@ def checkCiteEnginesConfig():
## It has been automatically generated by configure
## Use "Options/Reconfigure" if you need to update it after a
## configuration change.
## "CiteEngineName" "filename" "CiteEngineType" "Description" "Packages" "Requires" "Excludes"
## "CiteEngineName" "filename" "CiteEngineType" "DefaultBiblio" "Description" "Packages" "Requires" "Excludes"
''')
# build the list of available modules
@ -1550,16 +1550,17 @@ def processCiteEngineFile(file, filename, bool_docbook):
#Excludes: [list of excluded engines]
The last two lines are optional.
We expect output:
"CiteEngineName" "filename" "CiteEngineType" "Description" "Packages" "Requires" "Excludes"
"CiteEngineName" "filename" "CiteEngineType" "DefaultBiblio" "Description" "Packages" "Requires" "Excludes"
'''
remods = re.compile(r'\DeclareLyXCiteEngine\s*(?:\[([^]]*?)\])?{(.*)}')
rereqs = re.compile(r'#+\s*Requires: (.*)')
reexcs = re.compile(r'#+\s*Excludes: (.*)')
rereqs = re.compile(r'#+\s*Requires:\s*(.*)')
reexcs = re.compile(r'#+\s*Excludes:\s*(.*)')
redbeg = re.compile(r'#+\s*DescriptionBegin\s*$')
redend = re.compile(r'#+\s*DescriptionEnd\s*$')
recet = re.compile(r'\s*CiteEngineType (.*)')
recet = re.compile(r'\s*CiteEngineType\s*(.*)')
redb = re.compile(r'\s*DefaultBiblio\s*(.*)')
modname = desc = pkgs = req = excl = cet = ""
modname = desc = pkgs = req = excl = cet = db = ""
readingDescription = False
descLines = []
@ -1603,6 +1604,10 @@ def processCiteEngineFile(file, filename, bool_docbook):
if res != None:
cet = res.group(1)
continue
res = redb.search(line)
if res != None:
db = res.group(1)
continue
if modname == "":
logger.warning("Cite Engine File file without \DeclareLyXCiteEngine line. ")
@ -1625,7 +1630,7 @@ def processCiteEngineFile(file, filename, bool_docbook):
cm.write(line + '\n')
cm.close()
return '"%s" "%s" "%s" "%s" "%s" "%s" "%s"\n' % (modname, filename, cet, desc, pkgs, req, excl)
return '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"\n' % (modname, filename, cet, db, desc, pkgs, req, excl)
def checkTeXAllowSpaces():

View File

@ -3240,7 +3240,8 @@ bool BufferParams::addCiteEngine(vector<string> const & engine)
string const & BufferParams::defaultBiblioStyle() const
{
return documentClass().defaultBiblioStyle();
map<string, string> bs = documentClass().defaultBiblioStyle();
return bs[documentClass().opt_enginetype()];
}

View File

@ -38,11 +38,11 @@ CiteEnginesList theCiteEnginesList;
LyXCiteEngine::LyXCiteEngine(string const & n, string const & i,
vector<string> const & cet, string const & d,
vector<string> const & p,
vector<string> const & cet, vector<string> const & dbs,
string const & d, vector<string> const & p,
vector<string> const & r, vector<string> const & e):
name_(n), id_(i), engine_types_(cet), description_(d), package_list_(p),
required_engines_(r), excluded_engines_(e),
name_(n), id_(i), engine_types_(cet), default_biblios_(dbs), description_(d),
package_list_(p), required_engines_(r), excluded_engines_(e),
checked_(false), available_(false)
{
filename_ = id_ + ".citeengine";
@ -118,6 +118,33 @@ bool LyXCiteEngine::areCompatible(string const & eng1, string const & eng2)
}
string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
{
string res;
string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
//check whether all of the required packages are available
vector<string>::const_iterator it = default_biblios_.begin();
vector<string>::const_iterator end = default_biblios_.end();
for (; it != end; ++it) {
string const s = *it;
if (prefixIs(s, etp))
res = split(s, ':');
else if (!contains(s, ':') && res.empty())
res = s;
}
return res;
}
bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
{
if (find(default_biblios_.begin(), default_biblios_.end(), bf) != default_biblios_.end())
return true;
string const bfp = ":" + bf;
return find(default_biblios_.begin(), default_biblios_.end(), bfp) != default_biblios_.end();
}
// used when sorting the cite engine list.
class EngineSorter {
public:
@ -217,6 +244,16 @@ bool CiteEnginesList::read()
cet = split(cet, p, '|');
cets.push_back(p);
}
if (!lex.next(true))
break;
string db = lex.getString();
LYXERR(Debug::TCLASS, "Default Biblio: " << db);
vector<string> dbs;
while (!db.empty()) {
string p;
db = split(db, p, '|');
dbs.push_back(p);
}
if (!lex.next(true))
break;
string const desc = lex.getString();
@ -254,7 +291,7 @@ bool CiteEnginesList::read()
}
// This code is run when we have
// cename, fname, desc, pkgs, req and exc
addCiteEngine(cename, fname, cets, desc, pkgs, req, exc);
addCiteEngine(cename, fname, cets, dbs, desc, pkgs, req, exc);
} // end switch
} //end while
@ -267,11 +304,12 @@ bool CiteEnginesList::read()
void CiteEnginesList::addCiteEngine(string const & cename,
string const & filename, vector<string> const & cets, string const & description,
string const & filename, vector<string> const & cets,
vector<string> const & dbs, string const & description,
vector<string> const & pkgs, vector<string> const & req,
vector<string> const & exc)
{
LyXCiteEngine ce(cename, filename, cets, description, pkgs, req, exc);
LyXCiteEngine ce(cename, filename, cets, dbs, description, pkgs, req, exc);
englist_.push_back(ce);
}

View File

@ -51,6 +51,7 @@ public:
///
LyXCiteEngine(std::string const & name, std::string const & id,
std::vector<std::string> const & enginetypes,
std::vector<std::string> const & defaultbiblios,
std::string const & description,
std::vector<std::string> const & packagelist,
std::vector<std::string> const & requires,
@ -70,6 +71,10 @@ public:
///
bool hasEngineType(CiteEngineType const &) const;
///
std::string getDefaultBiblio(CiteEngineType const &) const;
///
bool isDefaultBiblio(std::string const &) const;
///
std::string const & getDescription() const { return description_; }
///
std::vector<std::string> const & getPackageList() const
@ -97,6 +102,8 @@ private:
std::string filename_;
/// the engine type(s)
std::vector<std::string> engine_types_;
/// default bibliography styles
std::vector<std::string> default_biblios_;
/// a short description for use in the ui
std::string description_;
/// the LaTeX packages on which this depends, if any
@ -153,7 +160,8 @@ public:
void operator=(CiteEnginesList const &);
/// add an engine to the list
void addCiteEngine(std::string const &, std::string const &,
std::vector<std::string> const &, std::string const &, std::vector<std::string> const &,
std::vector<std::string> const &, std::vector<std::string> const &,
std::string const &, std::vector<std::string> const &,
std::vector<std::string> const &, std::vector<std::string> const &);
///
std::vector<LyXCiteEngine> englist_;

View File

@ -759,8 +759,21 @@ TextClass::ReturnValues TextClass::read(Lexer & lexrc, ReadType rt)
break;
case TC_DEFAULTBIBLIO:
if (lexrc.next())
cite_default_biblio_style_ = rtrim(lexrc.getString());
if (lexrc.next()) {
vector<string> const dbs =
getVectorFromString(rtrim(lexrc.getString()), "|");
vector<string>::const_iterator it = dbs.begin();
vector<string>::const_iterator end = dbs.end();
for (; it != end; ++it) {
if (!contains(*it, ':'))
cite_default_biblio_style_[opt_enginetype_] = *it;
else {
string eng;
string const db = split(*it, eng, ':');
cite_default_biblio_style_[eng] = db;
}
}
}
break;
case TC_FULLAUTHORLIST:

View File

@ -330,7 +330,7 @@ protected:
/// Citation macros
std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
/// The default BibTeX bibliography style file
std::string cite_default_biblio_style_;
std::map<std::string, std::string> cite_default_biblio_style_;
/// Whether full author lists are supported
bool cite_full_author_list_;
/// The possible citation styles
@ -487,7 +487,8 @@ public:
///
std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
///
std::string const & defaultBiblioStyle() const { return cite_default_biblio_style_; }
std::map<std::string, std::string> const & defaultBiblioStyle() const
{ return cite_default_biblio_style_; }
///
bool const & fullAuthorList() const { return cite_full_author_list_; }
protected:

View File

@ -1130,15 +1130,23 @@ GuiDocument::GuiDocument(GuiView & lv)
connect(biblioModule->citeEngineCO, SIGNAL(activated(int)),
this, SLOT(citeEngineChanged(int)));
connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
this, SLOT(biblioChanged()));
this, SLOT(citeStyleChanged()));
connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
this, SLOT(biblioChanged()));
connect(biblioModule->bibtexCO, SIGNAL(activated(int)),
this, SLOT(bibtexChanged(int)));
connect(biblioModule->bibtexOptionsLE, SIGNAL(textChanged(QString)),
this, SLOT(biblioChanged()));
connect(biblioModule->bibtexStyleLE, SIGNAL(textChanged(QString)),
connect(biblioModule->defaultBiblioCO, SIGNAL(activated(int)),
this, SLOT(biblioChanged()));
connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
this, SLOT(biblioChanged()));
connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
this, SLOT(updateResetDefaultBiblio()));
connect(biblioModule->rescanBibliosPB, SIGNAL(clicked()),
this, SLOT(rescanBibFiles()));
connect(biblioModule->resetDefaultBiblioPB, SIGNAL(clicked()),
this, SLOT(resetDefaultBibfile()));
biblioModule->citeEngineCO->clear();
for (LyXCiteEngine const & cet : theCiteEnginesList) {
@ -1150,8 +1158,8 @@ GuiDocument::GuiDocument(GuiView & lv)
biblioModule->bibtexOptionsLE->setValidator(new NoNewLineValidator(
biblioModule->bibtexOptionsLE));
biblioModule->bibtexStyleLE->setValidator(new NoNewLineValidator(
biblioModule->bibtexStyleLE));
biblioModule->defaultBiblioCO->lineEdit()->setValidator(new NoNewLineValidator(
biblioModule->defaultBiblioCO->lineEdit()));
// NOTE: we do not provide "custom" here for security reasons!
biblioModule->bibtexCO->clear();
@ -2271,14 +2279,49 @@ void GuiDocument::biblioChanged()
}
void GuiDocument::rescanBibFiles()
{
rescanTexStyles("bst");
}
void GuiDocument::resetDefaultBibfile()
{
QString const engine =
biblioModule->citeEngineCO->itemData(
biblioModule->citeEngineCO->currentIndex()).toString();
CiteEngineType const cet =
CiteEngineType(biblioModule->citeStyleCO->itemData(
biblioModule->citeStyleCO->currentIndex()).toInt());
updateDefaultBiblio(theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet));
}
void GuiDocument::citeEngineChanged(int n)
{
QString const engine = biblioModule->citeEngineCO->itemData(n).toString();
QString const engine =
biblioModule->citeEngineCO->itemData(n).toString();
vector<string> const engs =
theCiteEnginesList[fromqstr(engine)]->getEngineType();
updateCiteStyles(engs);
resetDefaultBibfile();
biblioChanged();
}
void GuiDocument::citeStyleChanged()
{
QString const engine =
biblioModule->citeEngineCO->itemData(
biblioModule->citeEngineCO->currentIndex()).toString();
if (theCiteEnginesList[fromqstr(engine)]->isDefaultBiblio(
fromqstr(biblioModule->defaultBiblioCO->currentText())))
resetDefaultBibfile();
biblioChanged();
}
@ -2292,24 +2335,6 @@ void GuiDocument::bibtexChanged(int n)
}
void GuiDocument::setAuthorYear(bool authoryear)
{
if (authoryear)
biblioModule->citeStyleCO->setCurrentIndex(
biblioModule->citeStyleCO->findData(ENGINE_TYPE_AUTHORYEAR));
biblioChanged();
}
void GuiDocument::setNumerical(bool numerical)
{
if (numerical)
biblioModule->citeStyleCO->setCurrentIndex(
biblioModule->citeStyleCO->findData(ENGINE_TYPE_NUMERICAL));
biblioChanged();
}
void GuiDocument::updateCiteStyles(vector<string> const & engs, CiteEngineType const & sel)
{
biblioModule->citeStyleCO->clear();
@ -2617,7 +2642,7 @@ void GuiDocument::applyView()
bp_.use_bibtopic =
biblioModule->bibtopicCB->isChecked();
bp_.biblio_style = fromqstr(biblioModule->bibtexStyleLE->text());
bp_.biblio_style = fromqstr(biblioModule->defaultBiblioCO->currentText());
string const bibtex_command =
fromqstr(biblioModule->bibtexCO->itemData(
@ -3040,7 +3065,7 @@ void GuiDocument::paramsToDialog()
biblioModule->bibtopicCB->setChecked(
bp_.use_bibtopic);
biblioModule->bibtexStyleLE->setText(toqstr(bp_.biblio_style));
updateDefaultBiblio(bp_.defaultBiblioStyle());
string command;
string options =
@ -3626,6 +3651,59 @@ void GuiDocument::updateIncludeonlys()
}
void GuiDocument::updateDefaultBiblio(string const & style)
{
QString const bibstyle = toqstr(style);
biblioModule->defaultBiblioCO->clear();
int item_nr = -1;
QStringList str = texFileList("bstFiles.lst");
// test whether we have a valid list, otherwise run rescan
if (str.isEmpty()) {
rescanTexStyles("bst");
str = texFileList("bstFiles.lst");
}
for (int i = 0; i != str.size(); ++i)
str[i] = onlyFileName(str[i]);
// sort on filename only (no path)
str.sort();
for (int i = 0; i != str.count(); ++i) {
QString item = changeExtension(str[i], "");
if (item == bibstyle)
item_nr = i;
biblioModule->defaultBiblioCO->addItem(item);
}
if (item_nr == -1 && !bibstyle.isEmpty()) {
biblioModule->defaultBiblioCO->addItem(bibstyle);
item_nr = biblioModule->defaultBiblioCO->count() - 1;
}
if (item_nr != -1)
biblioModule->defaultBiblioCO->setCurrentIndex(item_nr);
else
biblioModule->defaultBiblioCO->clearEditText();
updateResetDefaultBiblio();
}
void GuiDocument::updateResetDefaultBiblio()
{
QString const engine =
biblioModule->citeEngineCO->itemData(
biblioModule->citeEngineCO->currentIndex()).toString();
CiteEngineType const cet =
CiteEngineType(biblioModule->citeStyleCO->itemData(
biblioModule->citeStyleCO->currentIndex()).toInt());
biblioModule->resetDefaultBiblioPB->setEnabled(
theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet)
!= fromqstr(biblioModule->defaultBiblioCO->currentText()));
}
void GuiDocument::updateContents()
{
// Nothing to do here as the document settings is not cursor dependant.

View File

@ -115,10 +115,12 @@ private Q_SLOTS:
void classChanged_adaptor();
void languagePackageChanged(int);
void biblioChanged();
void rescanBibFiles();
void resetDefaultBibfile();
void citeEngineChanged(int);
void citeStyleChanged();
void bibtexChanged(int);
void setAuthorYear(bool);
void setNumerical(bool);
void updateResetDefaultBiblio();
void updateModuleInfo();
void modulesChanged();
void changeBackgroundColor();
@ -181,6 +183,8 @@ private:
void updateSelectedModules();
///
void updateIncludeonlys();
///
void updateDefaultBiblio(std::string const & style);
/// save as default template
void saveDocDefault();
/// reset to default params

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>418</width>
<width>475</width>
<height>394</height>
</rect>
</property>
@ -46,7 +46,7 @@
<string/>
</property>
<property name="text">
<string>Sty&amp;le Engine:</string>
<string>Sty&amp;le engine:</string>
</property>
<property name="buddy">
<cstring>citeEngineCO</cstring>
@ -116,27 +116,6 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_1">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_1">
<item>
<widget class="QLabel" name="bibtexStyleLA">
<property name="text">
<string>Default st&amp;yle:</string>
</property>
<property name="buddy">
<cstring>bibtexStyleLE</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="bibtexStyleLE">
<property name="toolTip">
<string>Define the default BibTeX style</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="bibtopicCB">
<property name="toolTip">
@ -147,6 +126,63 @@
</property>
</widget>
</item>
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_1">
<item>
<widget class="QLabel" name="bibtexStyleLA">
<property name="text">
<string>Default BibTeX st&amp;yle:</string>
</property>
<property name="buddy">
<cstring>defaultBiblioCO</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="defaultBiblioCO">
<property name="toolTip">
<string>Here, you can define a BibTeX style that is suggested in the BibTeX dialog by default</string>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="rescanBibliosPB">
<property name="toolTip">
<string>Rescan style files</string>
</property>
<property name="text">
<string>Re&amp;scan</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="resetDefaultBiblioPB">
<property name="toolTip">
<string>Reset to the preset default</string>
</property>
<property name="text">
<string>&amp;Reset</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>

View File

@ -328,7 +328,7 @@ void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
}
if (style == "default")
style = buffer().params().biblio_style;
style = buffer().params().defaultBiblioStyle();
if (!style.empty() && !buffer().params().use_bibtopic) {
string base = normalizeName(buffer(), runparams, style, ".bst");