commit 3615a6a75bfe7bc3f51051fa19c46a4fd4540974
Author: Juergen Spitzmueller <sp...@lyx.org>
Date:   Thu Apr 18 14:48:01 2019 +0200

    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 b29f8a8..a41bfe6 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<InsetCommand const *>(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 559a199..01c2b79 100644
--- a/src/frontends/qt4/ui/BibitemUi.ui
+++ b/src/frontends/qt4/ui/BibitemUi.ui
@@ -6,17 +6,24 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>218</width>
-    <height>121</height>
+    <width>400</width>
+    <height>123</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string/>
   </property>
-  <layout class="QGridLayout" name="gridLayout">
+  <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
-    <layout class="QVBoxLayout" name="verticalLayout_2">
-     <item>
+    <widget class="QLabel" name="label">
+     <property name="text">
+      <string/>
+     </property>
+    </widget>
+   </item>
+   <item row="0" column="1">
+    <layout class="QGridLayout" name="gridLayout">
+     <item row="0" column="0">
       <widget class="QLabel" name="keyLA">
        <property name="toolTip">
         <string>The bibliography key</string>
@@ -29,31 +36,7 @@
        </property>
       </widget>
      </item>
-     <item>
-      <widget class="QLabel" name="labelLA">
-       <property name="toolTip">
-        <string>The label as it appears in the document</string>
-       </property>
-       <property name="text">
-        <string>&amp;Label:</string>
-       </property>
-       <property name="buddy">
-        <cstring>labelED</cstring>
-       </property>
-      </widget>
-     </item>
-     <item>
-      <widget class="QLabel" name="label">
-       <property name="text">
-        <string/>
-       </property>
-      </widget>
-     </item>
-    </layout>
-   </item>
-   <item row="0" column="1">
-    <layout class="QVBoxLayout" name="verticalLayout">
-     <item>
+     <item row="0" column="1">
       <widget class="QLineEdit" name="keyED">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -66,31 +49,71 @@
        </property>
       </widget>
      </item>
-     <item>
-      <widget class="QLineEdit" name="labelED">
-       <property name="sizePolicy">
-        <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
-         <horstretch>0</horstretch>
-         <verstretch>0</verstretch>
-        </sizepolicy>
-       </property>
+     <item row="1" column="0">
+      <widget class="QLabel" name="labelLA">
        <property name="toolTip">
         <string>The label as it appears in the document</string>
        </property>
-      </widget>
-     </item>
-     <item>
-      <widget class="QCheckBox" name="literalCB">
-       <property name="toolTip">
-        <string>Pass content of the `Label' field literally to LaTeX. Check 
this if you want to enter LaTeX code.</string>
-       </property>
        <property name="text">
-        <string>Li&amp;teral</string>
+        <string>&amp;Label:</string>
+       </property>
+       <property name="buddy">
+        <cstring>labelED</cstring>
        </property>
       </widget>
      </item>
+     <item row="1" column="1">
+      <layout class="QHBoxLayout" name="horizontalLayout">
+       <property name="spacing">
+        <number>0</number>
+       </property>
+       <item>
+        <widget class="QLineEdit" name="labelED">
+         <property name="sizePolicy">
+          <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+           <horstretch>0</horstretch>
+           <verstretch>0</verstretch>
+          </sizepolicy>
+         </property>
+         <property name="toolTip">
+          <string>The label as it appears in the document</string>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QLabel" name="yearLA">
+         <property name="text">
+          <string>&amp;Year:</string>
+         </property>
+         <property name="margin">
+          <number>6</number>
+         </property>
+         <property name="buddy">
+          <cstring>yearED</cstring>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QLineEdit" name="yearED">
+         <property name="toolTip">
+          <string>The year with &quot;Author (Year)&quot; citations (without 
parentheses).</string>
+         </property>
+        </widget>
+       </item>
+      </layout>
+     </item>
     </layout>
    </item>
+   <item row="1" column="1">
+    <widget class="QCheckBox" name="literalCB">
+     <property name="toolTip">
+      <string>Pass content of the `Label' field literally to LaTeX. Check this 
if you want to enter LaTeX code.</string>
+     </property>
+     <property name="text">
+      <string>Li&amp;teral</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <includes>
diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp
index f9b244c..2060b1a 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;
 }
 

Reply via email to