]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBibtex.cpp
Some improvements to the graphics dialog
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
index 15d84ea45c09c384d6175421edf79339b8aac5dc..4b4dd7e61c7dead146cf2b4ff0a85f7c606a069e 100644 (file)
@@ -4,9 +4,9 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
- * \author Herbert Voß
+ * \author Herbert Voß
  * \author Angus Leeming
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "Buffer.h"
 #include "BufferParams.h"
-#include "ui_BibtexAddUi.h"
+#include "CiteEnginesList.h"
+#include "FuncRequest.h"
+#include "LyXRC.h"
 #include "qt_helpers.h"
 #include "Validator.h"
-#include "LyXRC.h"
+
+#include "ui_BibtexAddUi.h"
 
 #include "ButtonPolicy.h"
 
+#include "frontends/alert.h"
+
+#include "insets/InsetBibtex.h"
+
 #include "support/debug.h"
-#include "support/FileFilterList.h"
+#include "support/ExceptionMessage.h"
+#include "support/FileName.h"
 #include "support/filetools.h" // changeExtension
 #include "support/gettext.h"
 #include "support/lstrings.h"
@@ -43,11 +51,13 @@ namespace frontend {
 
 
 GuiBibtex::GuiBibtex(GuiView & lv)
-       : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
+       : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
+         params_(insetCode("bibtex"))
 {
        setupUi(this);
 
        QDialog::setModal(true);
+       setWindowModality(Qt::WindowModal);
 
        connect(okPB, SIGNAL(clicked()),
                this, SLOT(slotOK()));
@@ -71,6 +81,10 @@ GuiBibtex::GuiBibtex(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(addBibPB, SIGNAL(clicked()),
                this, SLOT(addPressed()));
+       connect(rescanPB, SIGNAL(clicked()),
+               this, SLOT(rescanClicked()));
+       connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
+               this, SLOT(change_adaptor()));
 
        add_ = new GuiBibtexAddDialog(this);
        add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
@@ -84,6 +98,8 @@ GuiBibtex::GuiBibtex(GuiView & lv)
                this, SLOT(addDatabase()));
        connect(add_->addPB, SIGNAL(clicked()),
                add_, SLOT(accept()));
+       connect(add_->rescanPB, SIGNAL(clicked()),
+               this, SLOT(rescanClicked()));
        connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
                this, SLOT(addDatabase()));
        connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
@@ -95,6 +111,10 @@ GuiBibtex::GuiBibtex(GuiView & lv)
        connect(add_->closePB, SIGNAL(clicked()),
                add_, SLOT(reject()));
 
+       add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
+                                   "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
+                                   "If you want to reuse your own database, this is the place you should store it.")));
+
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
        bc().setOK(okPB);
        bc().setCancel(closePB);
@@ -133,8 +153,7 @@ void GuiBibtex::browsePressed()
        if (file.isEmpty())
                return;
 
-       // FIXME UNICODE
-       QString const filen = toqstr(changeExtension(fromqstr(file), ""));
+       QString const filen = changeExtension(file, "");
        bool present = false;
        unsigned int pres = 0;
 
@@ -160,7 +179,7 @@ void GuiBibtex::browseBibPressed()
        if (file.isEmpty())
                return;
 
-       QString const f = toqstr(changeExtension(fromqstr(file), ""));
+       QString const f = changeExtension(file, "");
        bool present = false;
 
        for (int i = 0; i < add_->bibLW->count(); ++i) {
@@ -203,9 +222,7 @@ void GuiBibtex::addDatabase()
                        if (matches.empty()) {
                                QString label = item->text();
                                QListWidgetItem * db = new QListWidgetItem(label);
-                               db->setFlags(db->flags() | Qt::ItemIsSelectable
-                                       | Qt::ItemIsUserCheckable);
-                               db->setCheckState(Qt::Checked);
+                               db->setFlags(db->flags() | Qt::ItemIsSelectable);
                                databaseLW->addItem(db);
                        }
                }
@@ -213,14 +230,12 @@ void GuiBibtex::addDatabase()
 
        if (!file.isEmpty()) {
                add_->bibED->clear();
-               QString const f = toqstr(changeExtension(fromqstr(file), ""));
+               QString const f = changeExtension(file, "");
                QList<QListWidgetItem *> matches =
                        databaseLW->findItems(f, Qt::MatchExactly);
                if (matches.empty()) {
                        QListWidgetItem * db = new QListWidgetItem(f);
-                       db->setFlags(db->flags() | Qt::ItemIsSelectable
-                               | Qt::ItemIsUserCheckable);
-                       db->setCheckState(Qt::Checked);
+                       db->setFlags(db->flags() | Qt::ItemIsSelectable);
                        databaseLW->addItem(db);
                }
        }
@@ -261,6 +276,13 @@ void GuiBibtex::downPressed()
 }
 
 
+void GuiBibtex::rescanClicked()
+{
+       rescanBibStyles();
+       updateContents();
+}
+
+
 void GuiBibtex::databaseChanged()
 {
        bool readOnly = isBufferReadonly();
@@ -281,98 +303,104 @@ void GuiBibtex::availableChanged()
 void GuiBibtex::updateContents()
 {
        bool bibtopic = usingBibtopic();
+       bool biblatex = usingBiblatex();
+
+       if (biblatex)
+               setTitle(qt_("Biblatex Bibliography"));
+       else
+               setTitle(qt_("BibTeX Bibliography"));
 
        databaseLW->clear();
 
        docstring bibs = params_["bibfiles"];
-       docstring embs = params_["embed"];
        docstring bib;
-       docstring emb;
 
        while (!bibs.empty()) {
                bibs = split(bibs, bib, ',');
-               embs = split(embs, emb, ',');
                bib = trim(bib);
                if (!bib.empty()) {
                        QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
-                       db->setFlags(db->flags() | Qt::ItemIsSelectable
-                               | Qt::ItemIsUserCheckable);
-                       db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
+                       db->setFlags(db->flags() | Qt::ItemIsSelectable);
                        databaseLW->addItem(db);
                }
        }
 
        add_->bibLW->clear();
 
-       vector<string> bib_str;
-       getBibFiles(bib_str);
-       for (vector<string>::const_iterator it = bib_str.begin();
-               it != bib_str.end(); ++it) {
-               string bibItem(changeExtension(*it, ""));
-               add_->bibLW->addItem(toqstr(bibItem));
-       }
+       QStringList bibfiles = bibFiles();
+       for (int i = 0; i != bibfiles.count(); ++i)
+               add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
 
-       string bibstyle = getStylefile();
+       QString const bibstyle = styleFile();
 
        bibtocCB->setChecked(bibtotoc() && !bibtopic);
        bibtocCB->setEnabled(!bibtopic);
 
-       if (!bibtopic && btPrintCO->count() == 3)
-               btPrintCO->removeItem(1);
-       else if (bibtopic && btPrintCO->count() < 3)
-               btPrintCO->insertItem(1, qt_("all uncited references", 0));
+       btPrintCO->clear();
+       btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
+       if (bibtopic)
+               btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
+       btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
+       if (usingBiblatex() && !buffer().masterParams().multibib.empty())
+               btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
 
        docstring btprint = params_["btprint"];
-       int btp = 0;
-       if ((bibtopic && btprint == "btPrintNotCited") ||
-          (!bibtopic && btprint == "btPrintAll"))
-               btp = 1;
-       else if (bibtopic && btprint == "btPrintAll")
-               btp = 2;
-
-       btPrintCO->setCurrentIndex(btp);
-
-       styleCB->clear();
-
-       int item_nr(-1);
-
-       vector<string> str;
-       getBibStyles(str);
-       for (vector<string>::const_iterator it = str.begin();
-               it != str.end(); ++it) {
-               string item(changeExtension(*it, ""));
-               if (item == bibstyle)
-                       item_nr = int(it - str.begin());
-               styleCB->addItem(toqstr(item));
-       }
+       if (btprint.empty())
+               // default
+               btprint = from_ascii("btPrintCited");
+       btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
+
+       // Only useful for biblatex
+       biblatexOptsLA->setVisible(biblatex);
+       biblatexOptsLE->setVisible(biblatex);
+
+       // only useful for BibTeX
+       styleCB->setVisible(!biblatex);
+       styleLA->setVisible(!biblatex);
+       stylePB->setVisible(!biblatex);
+
+       if (!biblatex) {
+               styleCB->clear();
+
+               int item_nr = -1;
+
+               QStringList const str = bibStyles();
+               for (int i = 0; i != str.count(); ++i) {
+                       QString item = changeExtension(str[i], "");
+                       if (item == bibstyle)
+                               item_nr = i;
+                       styleCB->addItem(item);
+               }
 
-       if (item_nr == -1 && !bibstyle.empty()) {
-               styleCB->addItem(toqstr(bibstyle));
-               item_nr = styleCB->count() - 1;
-       }
+               if (item_nr == -1 && !bibstyle.isEmpty()) {
+                       styleCB->addItem(bibstyle);
+                       item_nr = styleCB->count() - 1;
+               }
 
-       if (item_nr != -1)
-               styleCB->setCurrentIndex(item_nr);
-       else
-               styleCB->clearEditText();
+
+               if (item_nr != -1)
+                       styleCB->setCurrentIndex(item_nr);
+               else
+                       styleCB->clearEditText();
+       } else
+               biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
 }
 
 
 void GuiBibtex::applyView()
 {
-       QString dbs = databaseLW->item(0)->text();
-       docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
+       docstring dbs;
 
        unsigned int maxCount = databaseLW->count();
-       for (unsigned int i = 1; i < maxCount; i++) {
-               dbs += ',';
-               dbs += databaseLW->item(i)->text();
-               emb += ',';
-               emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
+       for (unsigned int i = 0; i < maxCount; i++) {
+               if (i != 0)
+                       dbs += ',';
+               QString item = databaseLW->item(i)->text();
+               docstring bibfile = qstring_to_ucs4(item);
+               dbs += bibfile;
        }
 
-       params_["bibfiles"] = qstring_to_ucs4(dbs);
-       params_["embed"] = emb;
+       params_["bibfiles"] = dbs;
 
        docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
        bool const bibtotoc = bibtocCB->isChecked();
@@ -390,35 +418,9 @@ void GuiBibtex::applyView()
                params_["options"] = bibstyle;
        }
 
-       int btp = btPrintCO->currentIndex();
-
-       if (usingBibtopic()) {
-               // bibtopic allows three kinds of sections:
-               // 1. sections that include all cited references of the database(s)
-               // 2. sections that include all uncited references of the database(s)
-               // 3. sections that include all references of the database(s), cited or not
-               switch (btp) {
-               case 0:
-                       params_["btprint"] = from_ascii("btPrintCited");
-                       break;
-               case 1:
-                       params_["btprint"] = from_ascii("btPrintNotCited");
-                       break;
-               case 2:
-                       params_["btprint"] = from_ascii("btPrintAll");
-                       break;
-               }
-       } else {
-               switch (btp) {
-               case 0:
-                       params_["btprint"] = docstring();
-                       break;
-               case 1:
-                       // use \nocite{*}
-                       params_["btprint"] = from_ascii("btPrintAll");
-                       break;
-               }               
-       }
+       params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
+
+       params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
 }
 
 
@@ -432,8 +434,8 @@ QString GuiBibtex::browseBib(QString const & in_name) const
 {
        QString const label1 = qt_("Documents|#o#O");
        QString const dir1 = toqstr(lyxrc.document_path);
-       FileFilterList const filter(_("BibTeX Databases (*.bib)"));
-       return browseRelFile(in_name, bufferFilepath(),
+       QStringList const filter(qt_("BibTeX Databases (*.bib)"));
+       return browseRelToParent(in_name, bufferFilePath(),
                qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
 }
 
@@ -442,59 +444,56 @@ QString GuiBibtex::browseBst(QString const & in_name) const
 {
        QString const label1 = qt_("Documents|#o#O");
        QString const dir1 = toqstr(lyxrc.document_path);
-       FileFilterList const filter(_("BibTeX Styles (*.bst)"));
-       return browseRelFile(in_name, bufferFilepath(),
+       QStringList const filter(qt_("BibTeX Styles (*.bst)"));
+       return browseRelToParent(in_name, bufferFilePath(),
                qt_("Select a BibTeX style"), filter, false, label1, dir1);
 }
 
 
-void GuiBibtex::getBibStyles(vector<string> & data) const
+QStringList GuiBibtex::bibStyles() const
 {
-       data.clear();
-
-       getTexFileList("bstFiles.lst", data);
-       // test, if we have a valid list, otherwise run rescan
-       if (data.empty()) {
+       QStringList data = texFileList("bstFiles.lst");
+       // test whether we have a valid list, otherwise run rescan
+       if (data.isEmpty()) {
                rescanBibStyles();
-               getTexFileList("bstFiles.lst", data);
+               data = texFileList("bstFiles.lst");
        }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it)
-               *it = support::onlyFilename(*it);
+       for (int i = 0; i != data.size(); ++i)
+               data[i] = onlyFileName(data[i]);
        // sort on filename only (no path)
-       sort(data.begin(), data.end());
+       data.sort();
+       return data;
 }
 
 
-void GuiBibtex::getBibFiles(vector<string> & data) const
+QStringList GuiBibtex::bibFiles() const
 {
-       data.clear();
-
-       getTexFileList("bibFiles.lst", data);
-       // test, if we have a valid list, otherwise run rescan
-       if (data.empty()) {
+       QStringList data = texFileList("bibFiles.lst");
+       // test whether we have a valid list, otherwise run rescan
+       if (data.isEmpty()) {
                rescanBibStyles();
-               getTexFileList("bibFiles.lst", data);
+               data = texFileList("bibFiles.lst");
        }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it)
-               *it = support::onlyFilename(*it);
+       for (int i = 0; i != data.size(); ++i)
+               data[i] = onlyFileName(data[i]);
        // sort on filename only (no path)
-       sort(data.begin(), data.end());
+       data.sort();
+       return data;
 }
 
 
 void GuiBibtex::rescanBibStyles() const
 {
-       rescanTexStyles();
+       if (usingBiblatex())
+               rescanTexStyles("bib");
+       else
+               rescanTexStyles("bst bib");
 }
 
 
 bool GuiBibtex::usingBibtopic() const
 {
-       return buffer().params().use_bibtopic;
+       return buffer().params().useBibtopic();
 }
 
 
@@ -504,52 +503,61 @@ bool GuiBibtex::bibtotoc() const
 }
 
 
-string const GuiBibtex::getStylefile() const
+bool GuiBibtex::usingBiblatex() const
+{
+       return buffer().masterBuffer()->params().useBiblatex();
+}
+
+
+QString GuiBibtex::styleFile() const
 {
        // the different bibtex packages have (and need) their
        // own "plain" stylefiles
-       biblio::CiteEngine const engine = buffer().params().getEngine();
-       docstring defaultstyle;
-       switch (engine) {
-       case biblio::ENGINE_BASIC:
-               defaultstyle = from_ascii("plain");
-               break;
-       case biblio::ENGINE_NATBIB_AUTHORYEAR:
-               defaultstyle = from_ascii("plainnat");
-               break;
-       case biblio::ENGINE_NATBIB_NUMERICAL:
-               defaultstyle = from_ascii("plainnat");
-               break;
-       case biblio::ENGINE_JURABIB:
-               defaultstyle = from_ascii("jurabib");
-               break;
-       }
+       QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
 
-       docstring bst = params_["options"];
+       QString bst = toqstr(params_["options"]);
        if (bibtotoc()){
                // bibstyle exists?
-               if (contains(bst, ',')) {
-                       docstring bibtotoc = from_ascii("bibtotoc");
-                       bst = split(bst, bibtotoc, ',');
-               } else
-                       bst.erase();
+               int pos = bst.indexOf(',');
+               if (pos != -1) {
+                       // FIXME: check
+                       // docstring bibtotoc = from_ascii("bibtotoc");
+                       // bst = split(bst, bibtotoc, ',');
+                       bst = bst.mid(pos + 1);
+               } else {
+                       bst.clear();
+               }
        }
 
        // propose default style file for new insets
        // existing insets might have (legally) no bst files
        // (if the class already provides a style)
-       if (bst.empty() && params_["bibfiles"].empty())
+       if (bst.isEmpty() && params_["bibfiles"].empty())
                bst = defaultstyle;
 
-       // FIXME UNICODE
-       return to_utf8(bst);
+       return bst;
+}
+
+
+bool GuiBibtex::initialiseParams(std::string const & data)
+{
+       InsetCommand::string2params(data, params_);
+       return true;
 }
 
 
+void GuiBibtex::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string(params_);
+       dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+
 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
 
 
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiBibtex_moc.cpp"
+#include "moc_GuiBibtex.cpp"