From 3615a6a75bfe7bc3f51051fa19c46a4fd4540974 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Thu, 18 Apr 2019 14:48:01 +0200 Subject: [PATCH] Make usage of natbib without BibTeX more obvious Fixes: #5549 --- src/frontends/qt4/GuiBibitem.cpp | 39 +++++++++- src/frontends/qt4/ui/BibitemUi.ui | 115 ++++++++++++++++++------------ src/insets/InsetBibitem.cpp | 5 +- 3 files changed, 109 insertions(+), 50 deletions(-) diff --git a/src/frontends/qt4/GuiBibitem.cpp b/src/frontends/qt4/GuiBibitem.cpp index b29f8a8704..a41bfe6682 100644 --- a/src/frontends/qt4/GuiBibitem.cpp +++ b/src/frontends/qt4/GuiBibitem.cpp @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author John Levon + * \¸author Jürgen Spitzmüller * * Full author contact details are available in file CREDITS. */ @@ -12,6 +13,9 @@ #include "GuiBibitem.h" +#include "Buffer.h" +#include "BufferParams.h" + #include "qt_helpers.h" #include "insets/InsetCommand.h" @@ -32,6 +36,8 @@ GuiBibitem::GuiBibitem(QWidget * parent) : InsetParamsWidget(parent) this, SIGNAL(changed())); connect(labelED, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); + connect(yearED, SIGNAL(textChanged(QString)), + this, SIGNAL(changed())); connect(literalCB, SIGNAL(clicked()), this, SIGNAL(changed())); } @@ -42,16 +48,42 @@ void GuiBibitem::paramsToDialog(Inset const * inset) InsetCommand const * ic = static_cast(inset); InsetCommandParams const & params = ic->params(); keyED->setText(toqstr(params["key"])); - labelED->setText(toqstr(params["label"])); literalCB->setChecked(params["literal"] == "true"); + QString const label = toqstr(params["label"]); + BufferParams const bp = inset->buffer().masterParams(); + if (bp.citeEngine() == "natbib" && bp.citeEngineType() == ENGINE_TYPE_AUTHORYEAR) { + yearED->setHidden(false); + yearLA->setHidden(false); + labelLA->setText(qt_("Author &Name:")); + labelED->setToolTip(qt_("Insert the author name(s) here. The year goes to the separate field.")); + int const i = label.lastIndexOf("("); + int const j = label.lastIndexOf(")"); + if (i != -1 && j != -1 && i < j) { + // Split Author(Year) to Author and Year + QString const year = label.left(j).mid(i + 1); + QString const author = label.left(i); + labelED->setText(author); + yearED->setText(year); + } else + labelED->setText(label); + } else { + yearED->setHidden(true); + yearLA->setHidden(true); + labelLA->setText(qt_("&Label:")); + labelED->setToolTip(qt_("The label as it appears in the document")); + labelED->setText(label); + } } docstring GuiBibitem::dialogToParams() const { InsetCommandParams params(insetCode()); + QString label = labelED->text(); + if (!yearED->isHidden()) + label += "(" + yearED->text() + ")"; params["key"] = qstring_to_ucs4(keyED->text()); - params["label"] = qstring_to_ucs4(labelED->text()); + params["label"] = qstring_to_ucs4(label); params["literal"] = literalCB->isChecked() ? from_ascii("true") : from_ascii("false"); return from_utf8(InsetCommand::params2string(params)); @@ -64,7 +96,8 @@ bool GuiBibitem::checkWidgets(bool readonly) const labelED->setReadOnly(readonly); if (!InsetParamsWidget::checkWidgets()) return false; - return !keyED->text().isEmpty(); + return !keyED->text().isEmpty() + && (yearED->isHidden() || !yearED->text().isEmpty()); } } // namespace frontend diff --git a/src/frontends/qt4/ui/BibitemUi.ui b/src/frontends/qt4/ui/BibitemUi.ui index 559a199887..01c2b7928b 100644 --- a/src/frontends/qt4/ui/BibitemUi.ui +++ b/src/frontends/qt4/ui/BibitemUi.ui @@ -6,17 +6,24 @@ 0 0 - 218 - 121 + 400 + 123 - + - - + + + + + + + + + The bibliography key @@ -29,31 +36,7 @@ - - - - The label as it appears in the document - - - &Label: - - - labelED - - - - - - - - - - - - - - - + @@ -66,31 +49,71 @@ - - - - - 0 - 0 - - + + The label as it appears in the document + + &Label: + + + labelED + - - - - Pass content of the `Label' field literally to LaTeX. Check this if you want to enter LaTeX code. + + + + 0 - - Li&teral - - + + + + + 0 + 0 + + + + The label as it appears in the document + + + + + + + &Year: + + + 6 + + + yearED + + + + + + + The year with "Author (Year)" citations (without parentheses). + + + + + + + + Pass content of the `Label' field literally to LaTeX. Check this if you want to enter LaTeX code. + + + Li&teral + + + diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index f9b244c460..2060b1a1c9 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -187,7 +187,10 @@ docstring InsetBibitem::bibLabel() const BufferParams const & bp = buffer().masterBuffer()->params(); if (bp.citeEngineType() == ENGINE_TYPE_NUMERICAL) return autolabel_; - docstring const & label = getParam("label"); + docstring label = getParam("label"); + if (!label.empty() && bp.citeEngine() == "natbib") + // Add a space before opening paren + label = subst(label, from_ascii("("), from_ascii(" (")); return label.empty() ? autolabel_ : label; }