]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBibtex.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
index a1dc48751d2b9dfc060341044735075f0c8a992e..052510b425c245360cd0c8347e4fcda6aec83231 100644 (file)
@@ -5,6 +5,7 @@
  *
  * \author John Levon
  * \author Herbert Voß
+ * \author Angus Leeming
  * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
 
 #include "GuiBibtex.h"
 
+#include "Buffer.h"
+#include "BufferParams.h"
 #include "ui_BibtexAddUi.h"
 #include "qt_helpers.h"
 #include "Validator.h"
 #include "LyXRC.h"
 
-#include "ControlBibtex.h"
 #include "ButtonPolicy.h"
 
+#include "support/debug.h"
+#include "support/FileFilterList.h"
 #include "support/filetools.h" // changeExtension
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
 #include <QPushButton>
 #include <QListWidget>
 #include <QCheckBox>
-#include <QCloseEvent>
 #include <QLineEdit>
 
-#include "debug.h"
-#include "support/filetools.h"
-#include "support/lstrings.h"
-
-using std::vector;
-using std::string;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-using support::changeExtension;
-using support::split;
-using support::trim;
 
-
-GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
-       : GuiDialog(lv, "bibtex")
+GuiBibtex::GuiBibtex(GuiView & lv)
+       : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
 {
        setupUi(this);
 
-       setViewTitle( _("BibTeX Bibliography"));
-       setController(new ControlBibtex(*this));
-
        QDialog::setModal(true);
 
        connect(okPB, SIGNAL(clicked()),
@@ -65,9 +57,13 @@ GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
                this, SLOT(browsePressed()));
        connect(deletePB, SIGNAL(clicked()),
                this, SLOT(deletePressed()));
-       connect(styleCB, SIGNAL(editTextChanged(const QString &)),
+       connect(upPB, SIGNAL(clicked()),
+               this, SLOT(upPressed()));
+       connect(downPB, SIGNAL(clicked()),
+               this, SLOT(downPressed()));
+       connect(styleCB, SIGNAL(editTextChanged(QString)),
                this, SLOT(change_adaptor()));
-       connect(databaseLW, SIGNAL(itemSelectionChanged()),
+       connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
                this, SLOT(databaseChanged()));
        connect(bibtocCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
@@ -82,7 +78,7 @@ GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
        add_bc_.setCancel(add_->closePB);
        add_bc_.addCheckedLineEdit(add_->bibED, 0);
 
-       connect(add_->bibED, SIGNAL(textChanged(const QString &)),
+       connect(add_->bibED, SIGNAL(textChanged(QString)),
                this, SLOT(bibEDChanged()));
        connect(add_->addPB, SIGNAL(clicked()),
                this, SLOT(addDatabase()));
@@ -92,7 +88,7 @@ GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
                this, SLOT(addDatabase()));
        connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
                add_, SLOT(accept()));
-       connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
+       connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
                this, SLOT(availableChanged()));
        connect(add_->browsePB, SIGNAL(clicked()),
                this, SLOT(browseBibPressed()));
@@ -107,17 +103,15 @@ GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
        bc().addReadOnly(styleCB);
        bc().addReadOnly(bibtocCB);
        bc().addReadOnly(addBibPB);
-       bc().addReadOnly(deletePB);
-}
-
+       // Delete/Up/Down are handled with more conditions in
+       // databaseChanged().
 
-ControlBibtex & GuiBibtexDialog::controller() const
-{
-       return static_cast<ControlBibtex &>(GuiDialog::controller());
+       // Make sure the delete/up/down buttons are disabled if necessary.
+       databaseChanged();
 }
 
 
-void GuiBibtexDialog::bibEDChanged()
+void GuiBibtex::bibEDChanged()
 {
        // Indicate to the button controller that the contents have
        // changed. The actual test of validity is carried out by
@@ -126,15 +120,15 @@ void GuiBibtexDialog::bibEDChanged()
 }
 
 
-void GuiBibtexDialog::change_adaptor()
+void GuiBibtex::change_adaptor()
 {
        changed();
 }
 
 
-void GuiBibtexDialog::browsePressed()
+void GuiBibtex::browsePressed()
 {
-       docstring const file = controller().browseBst(docstring());
+       docstring const file = browseBst(docstring());
 
        if (!file.empty()) {
                // FIXME UNICODE
@@ -158,9 +152,9 @@ void GuiBibtexDialog::browsePressed()
 }
 
 
-void GuiBibtexDialog::browseBibPressed()
+void GuiBibtex::browseBibPressed()
 {
-       docstring const file = trim(controller().browseBib(docstring()));
+       docstring const file = trim(browseBib(docstring()));
 
        if (!file.empty()) {
                // FIXME UNICODE
@@ -182,14 +176,14 @@ void GuiBibtexDialog::browseBibPressed()
 }
 
 
-void GuiBibtexDialog::addPressed()
+void GuiBibtex::addPressed()
 {
        add_bc_.setValid(false);
        add_->exec();
 }
 
 
-void GuiBibtexDialog::addDatabase()
+void GuiBibtex::addDatabase()
 {
        int const sel = add_->bibLW->currentRow();
        docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
@@ -205,8 +199,14 @@ void GuiBibtexDialog::addDatabase()
                        add_->bibLW->setItemSelected(item, false);
                        QList<QListWidgetItem *> matches =
                                databaseLW->findItems(item->text(), Qt::MatchExactly);
-                       if (matches.empty())
-                               databaseLW->addItem(item->text());
+                       if (matches.empty()) {
+                               QString label = item->text();
+                               QListWidgetItem * db = new QListWidgetItem(label);
+                               db->setFlags(db->flags() | Qt::ItemIsSelectable
+                                       | Qt::ItemIsUserCheckable);
+                               db->setCheckState(Qt::Checked);
+                               databaseLW->addItem(db);
+                       }
                }
        }
 
@@ -215,88 +215,128 @@ void GuiBibtexDialog::addDatabase()
                QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
                QList<QListWidgetItem *> matches =
                        databaseLW->findItems(f, Qt::MatchExactly);
-               if (matches.empty())
-                       databaseLW->addItem(f);
+               if (matches.empty()) {
+                       QListWidgetItem * db = new QListWidgetItem(f);
+                       db->setFlags(db->flags() | Qt::ItemIsSelectable
+                               | Qt::ItemIsUserCheckable);
+                       db->setCheckState(Qt::Checked);
+                       databaseLW->addItem(db);
+               }
        }
 
+       databaseChanged();
        changed();
 }
 
 
-void GuiBibtexDialog::deletePressed()
+void GuiBibtex::deletePressed()
 {
-       databaseLW->takeItem(databaseLW->currentRow());
-       changed();
+       QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
+       if (cur) {
+               delete cur;
+               databaseChanged();
+               changed();
+       }
 }
 
 
+void GuiBibtex::upPressed()
+{
+       int row = databaseLW->currentRow();
+       QListWidgetItem *cur = databaseLW->takeItem(row);
+       databaseLW->insertItem(row - 1, cur);
+       databaseLW->setCurrentItem(cur);
+       changed();
+}
+
 
-void GuiBibtexDialog::databaseChanged()
+void GuiBibtex::downPressed()
 {
-       deletePB->setEnabled(!readOnly() && databaseLW->currentRow() != -1);
+       int row = databaseLW->currentRow();
+       QListWidgetItem *cur = databaseLW->takeItem(row);
+       databaseLW->insertItem(row + 1, cur);
+       databaseLW->setCurrentItem(cur);
+       changed();
 }
 
 
-void GuiBibtexDialog::availableChanged()
+void GuiBibtex::databaseChanged()
 {
-       add_bc_.setValid(true);
+       bool readOnly = isBufferReadonly();
+       int count = databaseLW->count();
+       int row = databaseLW->currentRow();
+       deletePB->setEnabled(!readOnly && row != -1);
+       upPB->setEnabled(!readOnly && count > 1 && row > 0);
+       downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
 }
 
 
-void GuiBibtexDialog::closeEvent(QCloseEvent *e)
+void GuiBibtex::availableChanged()
 {
-       slotWMHide();
-       e->accept();
+       add_bc_.setValid(true);
 }
 
 
-void GuiBibtexDialog::update_contents()
+void GuiBibtex::updateContents()
 {
-       bool bibtopic = controller().usingBibtopic();
+       bool bibtopic = usingBibtopic();
 
        databaseLW->clear();
 
-       docstring bibs(controller().params()["bibfiles"]);
+       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())
-                       databaseLW->addItem(toqstr(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);
+                       databaseLW->addItem(db);
+               }
        }
 
        add_->bibLW->clear();
 
        vector<string> bib_str;
-       controller().getBibFiles(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));
        }
 
-       string bibstyle(controller().getStylefile());
+       string bibstyle = getStylefile();
 
-       bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
+       bibtocCB->setChecked(bibtotoc() && !bibtopic);
        bibtocCB->setEnabled(!bibtopic);
 
-       docstring btprint(controller().params()["btprint"]);
+       if (!bibtopic && btPrintCO->count() == 3)
+               btPrintCO->removeItem(1);
+       else if (bibtopic && btPrintCO->count() < 3)
+               btPrintCO->insertItem(1, qt_("all uncited references", 0));
+
+       docstring btprint = params_["btprint"];
        int btp = 0;
-       if (btprint == "btPrintNotCited")
+       if ((bibtopic && btprint == "btPrintNotCited") ||
+          (!bibtopic && btprint == "btPrintAll"))
                btp = 1;
-       else if (btprint == "btPrintAll")
+       else if (bibtopic && btprint == "btPrintAll")
                btp = 2;
 
        btPrintCO->setCurrentIndex(btp);
-       btPrintCO->setEnabled(bibtopic);
 
        styleCB->clear();
 
        int item_nr(-1);
 
        vector<string> str;
-       controller().getBibStyles(str);
+       getBibStyles(str);
        for (vector<string>::const_iterator it = str.begin();
                it != str.end(); ++it) {
                string item(changeExtension(*it, ""));
@@ -317,62 +357,201 @@ void GuiBibtexDialog::update_contents()
 }
 
 
-void GuiBibtexDialog::applyView()
+void GuiBibtex::applyView()
 {
        docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
+       docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
 
        unsigned int maxCount = databaseLW->count();
        for (unsigned int i = 1; i < maxCount; i++) {
                dbs += ',';
                dbs += qstring_to_ucs4(databaseLW->item(i)->text());
+               emb += ',';
+               emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
        }
 
-       controller().params()["bibfiles"] = dbs;
+       params_["bibfiles"] = dbs;
+       params_["embed"] = emb;
 
-       docstring const bibstyle(qstring_to_ucs4(styleCB->currentText()));
-       bool const bibtotoc(bibtocCB->isChecked());
+       docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
+       bool const bibtotoc = bibtocCB->isChecked();
 
        if (bibtotoc && (!bibstyle.empty())) {
                // both bibtotoc and style
-               controller().params()["options"] = "bibtotoc," + bibstyle;
+               params_["options"] = "bibtotoc," + bibstyle;
        } else if (bibtotoc) {
                // bibtotoc and no style
-               controller().params()["options"] = from_ascii("bibtotoc");
+               params_["options"] = from_ascii("bibtotoc");
        } else {
                // only style. An empty one is valid, because some
                // documentclasses have an own \bibliographystyle{}
                // command!
-               controller().params()["options"] = bibstyle;
+               params_["options"] = bibstyle;
        }
 
-       // 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
        int btp = btPrintCO->currentIndex();
 
-       switch (btp) {
-       case 0:
-               controller().params()["btprint"] = from_ascii("btPrintCited");
+       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;
+               }               
+       }
+}
+
+
+bool GuiBibtex::isValid()
+{
+       return databaseLW->count() != 0;
+}
+
+
+docstring const GuiBibtex::browseBib(docstring const & in_name) const
+{
+       // FIXME UNICODE
+       docstring const label1 = _("Documents|#o#O");
+       docstring const dir1 = from_utf8(lyxrc.document_path);
+       FileFilterList const filter(_("BibTeX Databases (*.bib)"));
+       return browseRelFile(in_name, from_utf8(bufferFilepath()),
+               _("Select a BibTeX database to add"), filter, false, label1, dir1);
+}
+
+
+docstring const GuiBibtex::browseBst(docstring const & in_name) const
+{
+       // FIXME UNICODE
+       docstring const label1 = _("Documents|#o#O");
+       docstring const dir1 = from_utf8(lyxrc.document_path);
+       FileFilterList const filter(_("BibTeX Styles (*.bst)"));
+       return browseRelFile(in_name, from_utf8(bufferFilepath()),
+               _("Select a BibTeX style"), filter, false, label1, dir1);
+}
+
+
+void GuiBibtex::getBibStyles(vector<string> & data) const
+{
+       data.clear();
+
+       getTexFileList("bstFiles.lst", data);
+       // test, if we have a valid list, otherwise run rescan
+       if (data.empty()) {
+               rescanBibStyles();
+               getTexFileList("bstFiles.lst", data);
+       }
+       vector<string>::iterator it  = data.begin();
+       vector<string>::iterator end = data.end();
+       for (; it != end; ++it) {
+               *it = onlyFilename(*it);
+       }
+       // sort on filename only (no path)
+       sort(data.begin(), data.end());
+}
+
+
+void GuiBibtex::getBibFiles(vector<string> & data) const
+{
+       data.clear();
+
+       getTexFileList("bibFiles.lst", data);
+       // test, if we have a valid list, otherwise run rescan
+       if (data.empty()) {
+               rescanBibStyles();
+               getTexFileList("bibFiles.lst", data);
+       }
+       vector<string>::iterator it  = data.begin();
+       vector<string>::iterator end = data.end();
+       for (; it != end; ++it) {
+               *it = onlyFilename(*it);
+       }
+       // sort on filename only (no path)
+       sort(data.begin(), data.end());
+}
+
+
+void GuiBibtex::rescanBibStyles() const
+{
+       rescanTexStyles();
+}
+
+
+bool GuiBibtex::usingBibtopic() const
+{
+       return buffer().params().use_bibtopic;
+}
+
+
+bool GuiBibtex::bibtotoc() const
+{
+       return prefixIs(to_utf8(params_["options"]), "bibtotoc");
+}
+
+
+string const GuiBibtex::getStylefile() 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 1:
-               controller().params()["btprint"] = from_ascii("btPrintNotCited");
+       case biblio::ENGINE_NATBIB_NUMERICAL:
+               defaultstyle = from_ascii("plainnat");
                break;
-       case 2:
-               controller().params()["btprint"] = from_ascii("btPrintAll");
+       case biblio::ENGINE_JURABIB:
+               defaultstyle = from_ascii("jurabib");
                break;
        }
 
-       if (!controller().usingBibtopic())
-               controller().params()["btprint"] = docstring();
-}
+       docstring bst = params_["options"];
+       if (bibtotoc()){
+               // bibstyle exists?
+               if (contains(bst, ',')) {
+                       docstring bibtotoc = from_ascii("bibtotoc");
+                       bst = split(bst, bibtotoc, ',');
+               } else
+                       bst.erase();
+       }
 
+       // 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())
+               bst = defaultstyle;
 
-bool GuiBibtexDialog::isValid()
-{
-       return databaseLW->count() != 0;
+       // FIXME UNICODE
+       return to_utf8(bst);
 }
 
+
+Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
+
+
 } // namespace frontend
 } // namespace lyx