Properly support the cite engines in the GUI

Instead of hardcoding 3 engines, we now support all engines which are
defined in the *.citeengines files.
This commit is contained in:
Juergen Spitzmueller 2016-12-31 09:28:51 +01:00
parent 0e6a3ac404
commit 8e4bd6a497
9 changed files with 253 additions and 206 deletions

View File

@ -1,6 +1,7 @@
# \DeclareLyXCiteEngine{BibTeX (basic)}
# \DeclareLyXCiteEngine{Basic (BibTeX)}
# DescriptionBegin
# Use the basic citation capabilities provided by plain LaTeX.
# The basic citation capabilities provided by BibTeX.
# Mainly simple numeric styles primarily suitable for science and maths.
# DescriptionEnd
# Excludes: jurabib | natbib

View File

@ -1,8 +1,8 @@
# \DeclareLyXCiteEngine[jurabib.sty]{Jurabib}
# \DeclareLyXCiteEngine[jurabib.sty]{Jurabib (BibTeX)}
# DescriptionBegin
# Loads the LaTeX package jurabib, a citation engine. Jurabib supports annotations,
# author-year style citations and hyphenation patterns for bibliography entries in
# English, German, French, Dutch, Spanish and Italian.
# Jurabib supports a range of author-year styles primarily suitable for law studies
# and the Humanities. It includes localizations for English, German, French, Dutch,
# Spanish and Italian.
# DescriptionEnd
# Excludes: basic | natbib

View File

@ -1,9 +1,9 @@
# \DeclareLyXCiteEngine[natbib.sty]{Natbib}
# \DeclareLyXCiteEngine[natbib.sty]{Natbib (BibTeX)}
# DescriptionBegin
# Loads the LaTeX package natbib, a citation engine. Natbib supports
# both author-year and numerical styles for citations, automatic sorting
# and merging of numerical citations, annotations, capitalization of the
# `van' part of author names, shortened and full author lists, and more.
# Natbib supports a range of both author-year and numerical styles mainly
# aimed at the Humanities. It features automatic sorting and merging of
# numerical citations, annotations, capitalization of the `van' part of
# author names, shortened and full author lists, and more.
# DescriptionEnd
# Excludes: basic | jurabib

View File

@ -23,6 +23,7 @@
#include "Buffer.h"
#include "buffer_funcs.h"
#include "Bullet.h"
#include "CiteEnginesList.h"
#include "Color.h"
#include "ColorSet.h"
#include "Converter.h"
@ -272,27 +273,6 @@ PackageTranslator const & packagetranslator()
}
// Cite engine
typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
CiteEngineTypeTranslator const init_citeenginetypetranslator()
{
CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
translator.addPair("default", ENGINE_TYPE_DEFAULT);
return translator;
}
CiteEngineTypeTranslator const & citeenginetypetranslator()
{
static CiteEngineTypeTranslator const translator =
init_citeenginetypetranslator();
return translator;
}
// Spacing
typedef Translator<string, Spacing::Space> SpaceTranslator;
@ -863,7 +843,7 @@ string BufferParams::readToken(Lexer & lex, string const & token,
} else if (token == "\\cite_engine_type") {
string engine_type;
lex >> engine_type;
cite_engine_type_ = citeenginetypetranslator().find(engine_type);
cite_engine_type_ = theCiteEnginesList.getType(engine_type);
} else if (token == "\\biblio_style") {
lex.eatLine();
biblio_style = lex.getString();
@ -1234,7 +1214,7 @@ void BufferParams::writeFile(ostream & os, Buffer const * buf) const
os << "basic";
}
os << "\n\\cite_engine_type " << citeenginetypetranslator().find(cite_engine_type_)
os << "\n\\cite_engine_type " << theCiteEnginesList.getTypeAsString(cite_engine_type_)
<< "\n\\biblio_style " << biblio_style
<< "\n\\use_bibtopic " << convert<string>(use_bibtopic)
<< "\n\\use_indices " << convert<string>(use_indices)

View File

@ -14,6 +14,7 @@
#include "CiteEnginesList.h"
#include "Citation.h"
#include "LaTeXFeatures.h"
#include "Lexer.h"
@ -22,6 +23,7 @@
#include "support/gettext.h"
#include "support/filetools.h"
#include "support/lstrings.h"
#include "support/Translator.h"
#include <algorithm>
@ -76,6 +78,13 @@ bool LyXCiteEngine::isAvailable() const
}
bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
{
return std::find(engine_types_.begin(), engine_types_.end(),
theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
}
bool LyXCiteEngine::isCompatible(string const & cename) const
{
// do we exclude it?
@ -119,6 +128,43 @@ public:
};
// Local translators
namespace {
typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
CiteEngineTypeTranslator const init_citeenginetypetranslator()
{
CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
translator.addPair("default", ENGINE_TYPE_DEFAULT);
return translator;
}
CiteEngineTypeTranslator const & citeenginetypetranslator()
{
static CiteEngineTypeTranslator const translator =
init_citeenginetypetranslator();
return translator;
}
} // namespace anon
string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
{
return citeenginetypetranslator().find(et);
}
CiteEngineType CiteEnginesList::getType(string const & et) const
{
return citeenginetypetranslator().find(et);
}
// Much of this is borrowed from LayoutFileList::read()
bool CiteEnginesList::read()
{

View File

@ -13,6 +13,8 @@
#ifndef CITEENGINESLIST_H
#define CITEENGINESLIST_H
#include "Citation.h"
#include <string>
#include <vector>
@ -29,13 +31,13 @@ namespace lyx {
* which must begin roughly so:
* # \DeclareLyXCiteEngine[natbib.sty]{Natbib}
* # DescriptionBegin
* # Loads the LaTeX package natbib, a citation engine. Natbib supports
* # both author-year and numerical styles for citations, automatic sorting
* # and merging of numerical citations, annotations, capitalization of the
* # `van' part of author names, shortened and full author lists, and more.
* # Natbib supports a range of both author-year and numerical styles mainly
* # aimed at the Humanities. It features automatic sorting and merging of
* # numerical citations, annotations, capitalization of the `van' part of
* # author names, shortened and full author lists, and more.
* # DescriptionEnd
* # Excludes: basic | jurabib
* The description might be used in the gui to give information to the user. The
* The description will be used in the gui to give information to the user. The
* Requires and Excludes lines are read by the configuration script
* and written to a file citeengines.lst in the user configuration directory.
* That file is then read on startup to populate the CiteEnginesList, below.
@ -66,6 +68,8 @@ public:
///
std::vector<std::string> const & getEngineType() const { return engine_types_; }
///
bool hasEngineType(CiteEngineType const &) const;
///
std::string const & getDescription() const { return description_; }
///
std::vector<std::string> const & getPackageList() const
@ -121,6 +125,10 @@ class CiteEnginesList {
public:
///
CiteEnginesList() {}
///
std::string getTypeAsString(CiteEngineType const &) const;
///
CiteEngineType getType(std::string const &) const;
/// reads the engines from a file generated by configure.py
bool read();
///

View File

@ -29,6 +29,7 @@
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "CiteEnginesList.h"
#include "Color.h"
#include "ColorCache.h"
#include "Converter.h"
@ -1126,22 +1127,10 @@ GuiDocument::GuiDocument(GuiView & lv)
// biblio
biblioModule = new UiWidget<Ui::BiblioUi>;
connect(biblioModule->citeDefaultRB, SIGNAL(toggled(bool)),
this, SLOT(setNumerical(bool)));
connect(biblioModule->citeJurabibRB, SIGNAL(toggled(bool)),
this, SLOT(setAuthorYear(bool)));
connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
biblioModule->citationStyleL, SLOT(setEnabled(bool)));
connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
biblioModule->citeStyleCO, SLOT(setEnabled(bool)));
connect(biblioModule->citeDefaultRB, SIGNAL(clicked()),
this, SLOT(biblioChanged()));
connect(biblioModule->citeNatbibRB, SIGNAL(clicked()),
this, SLOT(biblioChanged()));
connect(biblioModule->citeEngineCO, SIGNAL(activated(int)),
this, SLOT(citeEngineChanged(int)));
connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
this, SLOT(biblioChanged()));
connect(biblioModule->citeJurabibRB, SIGNAL(clicked()),
this, SLOT(biblioChanged()));
connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
this, SLOT(biblioChanged()));
connect(biblioModule->bibtexCO, SIGNAL(activated(int)),
@ -1151,15 +1140,19 @@ GuiDocument::GuiDocument(GuiView & lv)
connect(biblioModule->bibtexStyleLE, SIGNAL(textChanged(QString)),
this, SLOT(biblioChanged()));
biblioModule->citeEngineCO->clear();
for (LyXCiteEngine const & cet : theCiteEnginesList) {
biblioModule->citeEngineCO->addItem(qt_(cet.getName()), toqstr(cet.getID()));
int const i = biblioModule->citeEngineCO->findData(toqstr(cet.getID()));
biblioModule->citeEngineCO->setItemData(i, qt_(cet.getDescription()),
Qt::ToolTipRole);
}
biblioModule->bibtexOptionsLE->setValidator(new NoNewLineValidator(
biblioModule->bibtexOptionsLE));
biblioModule->bibtexStyleLE->setValidator(new NoNewLineValidator(
biblioModule->bibtexStyleLE));
biblioModule->citeStyleCO->addItem(qt_("Author-year"));
biblioModule->citeStyleCO->addItem(qt_("Numerical"));
biblioModule->citeStyleCO->setCurrentIndex(0);
// NOTE: we do not provide "custom" here for security reasons!
biblioModule->bibtexCO->clear();
biblioModule->bibtexCO->addItem(qt_("Default"), QString("default"));
@ -2278,6 +2271,19 @@ void GuiDocument::biblioChanged()
}
void GuiDocument::citeEngineChanged(int n)
{
QString const engine = biblioModule->citeEngineCO->itemData(n).toString();
vector<string> const engs =
theCiteEnginesList[fromqstr(engine)]->getEngineType();
updateCiteStyles(engs);
biblioChanged();
}
void GuiDocument::bibtexChanged(int n)
{
biblioModule->bibtexOptionsLE->setEnabled(
@ -2289,7 +2295,8 @@ void GuiDocument::bibtexChanged(int n)
void GuiDocument::setAuthorYear(bool authoryear)
{
if (authoryear)
biblioModule->citeStyleCO->setCurrentIndex(0);
biblioModule->citeStyleCO->setCurrentIndex(
biblioModule->citeStyleCO->findData(ENGINE_TYPE_AUTHORYEAR));
biblioChanged();
}
@ -2297,11 +2304,39 @@ void GuiDocument::setAuthorYear(bool authoryear)
void GuiDocument::setNumerical(bool numerical)
{
if (numerical)
biblioModule->citeStyleCO->setCurrentIndex(1);
biblioModule->citeStyleCO->setCurrentIndex(
biblioModule->citeStyleCO->findData(ENGINE_TYPE_NUMERICAL));
biblioChanged();
}
void GuiDocument::updateCiteStyles(vector<string> const & engs, CiteEngineType const & sel)
{
biblioModule->citeStyleCO->clear();
vector<string>::const_iterator it = engs.begin();
vector<string>::const_iterator end = engs.end();
for (; it != end; ++it) {
if (*it == "default")
biblioModule->citeStyleCO->addItem(qt_("Basic numerical"),
ENGINE_TYPE_DEFAULT);
else if (*it == "authoryear")
biblioModule->citeStyleCO->addItem(qt_("Author-year"),
ENGINE_TYPE_AUTHORYEAR);
else if (*it == "numerical")
biblioModule->citeStyleCO->addItem(qt_("Author-number"),
ENGINE_TYPE_NUMERICAL);
}
int i = biblioModule->citeStyleCO->findData(sel);
if (biblioModule->citeStyleCO->findData(sel) == -1)
i = 0;
biblioModule->citeStyleCO->setCurrentIndex(i);
biblioModule->citationStyleL->setEnabled(engs.size() > 1);
biblioModule->citeStyleCO->setEnabled(engs.size() > 1);
}
void GuiDocument::updateEngineType(string const & items, CiteEngineType const & sel)
{
engine_types_.clear();
@ -2314,28 +2349,7 @@ void GuiDocument::updateEngineType(string const & items, CiteEngineType const &
engine_types_.push_back(style);
}
switch (sel) {
case ENGINE_TYPE_AUTHORYEAR:
biblioModule->citeStyleCO->setCurrentIndex(0);
break;
case ENGINE_TYPE_NUMERICAL:
case ENGINE_TYPE_DEFAULT:
biblioModule->citeStyleCO->setCurrentIndex(1);
break;
}
biblioModule->citationStyleL->setEnabled(nn > 1);
biblioModule->citeStyleCO->setEnabled(nn > 1);
if (nn != 1)
return;
// If the textclass allows only one of authoryear or numerical,
// we have no choice but to force that engine type.
if (engine_types_[0] == "authoryear")
biblioModule->citeStyleCO->setCurrentIndex(0);
else
biblioModule->citeStyleCO->setCurrentIndex(1);
updateCiteStyles(engine_types_, sel);
}
@ -2588,19 +2602,17 @@ void GuiDocument::applyView()
bp_.use_refstyle = latexModule->refstyleCB->isChecked();
// biblio
if (biblioModule->citeNatbibRB->isChecked())
bp_.setCiteEngine("natbib");
else if (biblioModule->citeJurabibRB->isChecked())
bp_.setCiteEngine("jurabib");
if (biblioModule->citeDefaultRB->isChecked()) {
bp_.setCiteEngine("basic");
string const engine =
fromqstr(biblioModule->citeEngineCO->itemData(
biblioModule->citeEngineCO->currentIndex()).toString());
bp_.setCiteEngine(engine);
CiteEngineType const style = CiteEngineType(biblioModule->citeStyleCO->itemData(
biblioModule->citeStyleCO->currentIndex()).toInt());
if (theCiteEnginesList[engine]->hasEngineType(style))
bp_.setCiteEngineType(style);
else
bp_.setCiteEngineType(ENGINE_TYPE_DEFAULT);
}
else
if (biblioModule->citeStyleCO->currentIndex())
bp_.setCiteEngineType(ENGINE_TYPE_NUMERICAL);
else
bp_.setCiteEngineType(ENGINE_TYPE_AUTHORYEAR);
bp_.use_bibtopic =
biblioModule->bibtopicCB->isChecked();
@ -3016,21 +3028,15 @@ void GuiDocument::paramsToDialog()
// biblio
string const cite_engine = bp_.citeEngine().list().front();
biblioModule->citeDefaultRB->setChecked(
cite_engine == "basic");
biblioModule->citeJurabibRB->setChecked(
cite_engine == "jurabib");
biblioModule->citeNatbibRB->setChecked(
cite_engine == "natbib");
biblioModule->citeStyleCO->setCurrentIndex(
bp_.citeEngineType() & ENGINE_TYPE_NUMERICAL);
biblioModule->citeEngineCO->setCurrentIndex(
biblioModule->citeEngineCO->findData(toqstr(cite_engine)));
updateEngineType(documentClass().opt_enginetype(),
bp_.citeEngineType());
biblioModule->citeStyleCO->setCurrentIndex(
biblioModule->citeStyleCO->findData(bp_.citeEngineType()));
biblioModule->bibtopicCB->setChecked(
bp_.use_bibtopic);

View File

@ -75,6 +75,8 @@ public:
void updateFontsize(std::string const &, std::string const &);
void updateFontlist();
void updateDefaultFormat();
void updateCiteStyles(std::vector<std::string> const &,
CiteEngineType const & sel = ENGINE_TYPE_AUTHORYEAR);
void updateEngineType(std::string const &, CiteEngineType const &);
void updatePagestyle(std::string const &, std::string const &);
bool isChildIncluded(std::string const &);
@ -113,6 +115,7 @@ private Q_SLOTS:
void classChanged_adaptor();
void languagePackageChanged(int);
void biblioChanged();
void citeEngineChanged(int);
void bibtexChanged(int);
void setAuthorYear(bool);
void setNumerical(bool);

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>BiblioUi</class>
<widget class="QWidget" name="BiblioUi">
@ -23,40 +24,49 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QRadioButton" name="citeDefaultRB" >
<property name="toolTip" >
<string>Use BibTeX's default numerical styles</string>
</property>
<property name="text" >
<string>&amp;Default (numerical)</string>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QRadioButton" name="citeNatbibRB" >
<property name="toolTip" >
<string>Use the natbib styles for natural sciences and arts. Set additional parameters in document class options.</string>
</property>
<property name="text" >
<string>&amp;Natbib</string>
</property>
</widget>
</item>
<item row="1" column="1" >
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="margin" >
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="citeEngineLA">
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>Sty&amp;le Engine:</string>
</property>
<property name="buddy">
<cstring>citeEngineCO</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="citeEngineCO">
<property name="toolTip">
<string>A selection of different style engines (such as natbib) that respectively provide support for specific citytion and bibliography styles. Expand to get more information.</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="citationStyleL">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Natbib &amp;style:</string>
<string>&amp;Variant:</string>
</property>
<property name="buddy">
<cstring>citeStyleCO</cstring>
@ -69,16 +79,19 @@
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy vsizetype="Fixed" hsizetype="Fixed" >
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Provides available cite style variants.</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="2" >
<item row="0" column="1">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -91,16 +104,6 @@
</property>
</spacer>
</item>
<item row="2" column="0" >
<widget class="QRadioButton" name="citeJurabibRB" >
<property name="toolTip" >
<string>Use the jurabib styles for law and humanities</string>
</property>
<property name="text" >
<string>&amp;Jurabib</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -180,7 +183,7 @@
</item>
</layout>
</item>
<item rowspan="2" row="0" column="1" >
<item row="0" column="1" rowspan="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -198,7 +201,7 @@
<item>
<widget class="QLabel" name="bibtexOptionsLA">
<property name="text">
<string>&amp;Options:</string>
<string>Op&amp;tions:</string>
</property>
<property name="buddy">
<cstring>bibtexOptionsLE</cstring>