]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBibtex.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
index 121053f0a78813d736ec356102940f6d9ce41a11..ff0a03e272222dfcb0b7d2c6eb41d12d9fea0154 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 "debug.h"
-#include "ui_BibtexAddUi.h"
+#include "FuncRequest.h"
+#include "LyXRC.h"
 #include "qt_helpers.h"
 #include "Validator.h"
-#include "LyXRC.h"
-#include "gettext.h"
+
+#include "ui_BibtexAddUi.h"
 
 #include "ButtonPolicy.h"
 
-#include "support/filetools.h" // changeExtension
-#include "support/lstrings.h"
-#include "support/FileFilterList.h"
+#include "frontends/alert.h"
 
-#include "frontend_helpers.h"
+#include "insets/InsetBibtex.h"
 
+#include "support/debug.h"
+#include "support/ExceptionMessage.h"
+#include "support/FileName.h"
+#include "support/filetools.h" // changeExtension
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
 #include <QPushButton>
 #include <QListWidget>
 #include <QCheckBox>
-#include <QCloseEvent>
 #include <QLineEdit>
 
-using std::pair;
-using std::string;
-using std::vector;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-using support::changeExtension;
-using support::contains;
-using support::FileFilterList;
-using support::onlyFilename;
-using support::prefixIs;
-using support::split;
-using support::trim;
-
 
-GuiBibtex::GuiBibtex(LyXView & lv)
-       : GuiCommand(lv, "bibtex")
+GuiBibtex::GuiBibtex(GuiView & lv)
+       : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
+         params_(insetCode("bibtex"))
 {
        setupUi(this);
 
-       setViewTitle( _("BibTeX Bibliography"));
-
        QDialog::setModal(true);
 
        connect(okPB, SIGNAL(clicked()),
@@ -73,9 +65,13 @@ GuiBibtex::GuiBibtex(LyXView & lv)
                this, SLOT(browsePressed()));
        connect(deletePB, SIGNAL(clicked()),
                this, SLOT(deletePressed()));
+       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()));
@@ -83,6 +79,8 @@ GuiBibtex::GuiBibtex(LyXView & lv)
                this, SLOT(change_adaptor()));
        connect(addBibPB, SIGNAL(clicked()),
                this, SLOT(addPressed()));
+       connect(rescanPB, SIGNAL(clicked()),
+               this, SLOT(rescanClicked()));
 
        add_ = new GuiBibtexAddDialog(this);
        add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
@@ -90,17 +88,19 @@ GuiBibtex::GuiBibtex(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()));
        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 *)),
                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()));
@@ -115,7 +115,11 @@ GuiBibtex::GuiBibtex(LyXView & lv)
        bc().addReadOnly(styleCB);
        bc().addReadOnly(bibtocCB);
        bc().addReadOnly(addBibPB);
-       bc().addReadOnly(deletePB);
+       // Delete/Up/Down are handled with more conditions in
+       // databaseChanged().
+
+       // Make sure the delete/up/down buttons are disabled if necessary.
+       databaseChanged();
 }
 
 
@@ -136,51 +140,51 @@ void GuiBibtex::change_adaptor()
 
 void GuiBibtex::browsePressed()
 {
-       docstring const file = browseBst(docstring());
-
-       if (!file.empty()) {
-               // FIXME UNICODE
-               docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
-               bool present = false;
-               unsigned int pres = 0;
-
-               for (int i = 0; i != styleCB->count(); ++i) {
-                       if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
-                               present = true;
-                               pres = i;
-                       }
-               }
+       QString const file = browseBst(QString());
+
+       if (file.isEmpty())
+               return;
 
-               if (!present)
-                       styleCB->insertItem(0, toqstr(filen));
+       QString const filen = changeExtension(file, "");
+       bool present = false;
+       unsigned int pres = 0;
 
-               styleCB->setCurrentIndex(pres);
-               changed();
+       for (int i = 0; i != styleCB->count(); ++i) {
+               if (styleCB->itemText(i) == filen) {
+                       present = true;
+                       pres = i;
+               }
        }
+
+       if (!present)
+               styleCB->insertItem(0, filen);
+
+       styleCB->setCurrentIndex(pres);
+       changed();
 }
 
 
 void GuiBibtex::browseBibPressed()
 {
-       docstring const file = trim(browseBib(docstring()));
+       QString const file = browseBib(QString()).trimmed();
 
-       if (!file.empty()) {
-               // FIXME UNICODE
-               QString const f = toqstr(changeExtension(to_utf8(file), ""));
-               bool present = false;
+       if (file.isEmpty())
+               return;
 
-               for (int i = 0; i < add_->bibLW->count(); ++i) {
-                       if (add_->bibLW->item(i)->text() == f)
-                               present = true;
-               }
+       QString const f = changeExtension(file, "");
+       bool present = false;
 
-               if (!present) {
-                       add_->bibLW->addItem(f);
-                       changed();
-               }
+       for (int i = 0; i < add_->bibLW->count(); ++i) {
+               if (add_->bibLW->item(i)->text() == f)
+                       present = true;
+       }
 
-               add_->bibED->setText(f);
+       if (!present) {
+               add_->bibLW->addItem(f);
+               changed();
        }
+
+       add_->bibED->setText(f);
 }
 
 
@@ -194,9 +198,9 @@ void GuiBibtex::addPressed()
 void GuiBibtex::addDatabase()
 {
        int const sel = add_->bibLW->currentRow();
-       docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
+       QString const file = add_->bibED->text().trimmed();
 
-       if (sel < 0 && file.empty())
+       if (sel < 0 && file.isEmpty())
                return;
 
        // Add the selected browser_bib keys to browser_database
@@ -207,48 +211,84 @@ void GuiBibtex::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);
+                               databaseLW->addItem(db);
+                       }
                }
        }
 
-       if (!file.empty()) {
+       if (!file.isEmpty()) {
                add_->bibED->clear();
-               QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
+               QString const f = changeExtension(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);
+                       databaseLW->addItem(db);
+               }
        }
 
+       databaseChanged();
        changed();
 }
 
 
 void GuiBibtex::deletePressed()
 {
-       databaseLW->takeItem(databaseLW->currentRow());
+       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 GuiBibtex::downPressed()
+{
+       int row = databaseLW->currentRow();
+       QListWidgetItem *cur = databaseLW->takeItem(row);
+       databaseLW->insertItem(row + 1, cur);
+       databaseLW->setCurrentItem(cur);
+       changed();
+}
 
-void GuiBibtex::databaseChanged()
+
+void GuiBibtex::rescanClicked()
 {
-       deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
+       rescanBibStyles();
+       updateContents();
 }
 
 
-void GuiBibtex::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 GuiBibtex::closeEvent(QCloseEvent *e)
+void GuiBibtex::availableChanged()
 {
-       slotClose();
-       e->accept();
+       add_bc_.setValid(true);
 }
 
 
@@ -264,51 +304,53 @@ void GuiBibtex::updateContents()
        while (!bibs.empty()) {
                bibs = split(bibs, bib, ',');
                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);
+                       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 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));
+
        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);
+       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, ""));
+       QStringList str = bibStyles();
+       for (int i = 0; i != str.count(); ++i) {
+               QString item = changeExtension(str[i], "");
                if (item == bibstyle)
-                       item_nr = int(it - str.begin());
-               styleCB->addItem(toqstr(item));
+                       item_nr = i;
+               styleCB->addItem(item);
        }
 
-       if (item_nr == -1 && !bibstyle.empty()) {
-               styleCB->addItem(toqstr(bibstyle));
+       if (item_nr == -1 && !bibstyle.isEmpty()) {
+               styleCB->addItem(bibstyle);
                item_nr = styleCB->count() - 1;
        }
 
@@ -321,12 +363,15 @@ void GuiBibtex::updateContents()
 
 void GuiBibtex::applyView()
 {
-       docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
+       docstring dbs;
 
        unsigned int maxCount = databaseLW->count();
-       for (unsigned int i = 1; i < maxCount; i++) {
-               dbs += ',';
-               dbs += qstring_to_ucs4(databaseLW->item(i)->text());
+       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"] = dbs;
@@ -334,7 +379,7 @@ void GuiBibtex::applyView()
        docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
        bool const bibtotoc = bibtocCB->isChecked();
 
-       if (bibtotoc && (!bibstyle.empty())) {
+       if (bibtotoc && !bibstyle.empty()) {
                // both bibtotoc and style
                params_["options"] = "bibtotoc," + bibstyle;
        } else if (bibtotoc) {
@@ -347,26 +392,35 @@ void GuiBibtex::applyView()
                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:
-               params_["btprint"] = from_ascii("btPrintCited");
-               break;
-       case 1:
-               params_["btprint"] = from_ascii("btPrintNotCited");
-               break;
-       case 2:
-               params_["btprint"] = from_ascii("btPrintAll");
-               break;
+       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;
+               }               
        }
-
-       if (!usingBibtopic())
-               params_["btprint"] = docstring();
 }
 
 
@@ -376,66 +430,55 @@ bool GuiBibtex::isValid()
 }
 
 
-docstring const GuiBibtex::browseBib(docstring const & in_name) const
+QString GuiBibtex::browseBib(QString const & in_name) const
 {
-       // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 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, dir1);
+       QString const label1 = qt_("Documents|#o#O");
+       QString const dir1 = toqstr(lyxrc.document_path);
+       QStringList const filter(qt_("BibTeX Databases (*.bib)"));
+       return browseRelFile(in_name, bufferFilepath(),
+               qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
 }
 
 
-docstring const GuiBibtex::browseBst(docstring const & in_name) const
+QString GuiBibtex::browseBst(QString const & in_name) const
 {
-       // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 from_utf8(lyxrc.document_path));
-       FileFilterList const filter(_("BibTeX Styles (*.bst)"));
-       return browseRelFile(in_name, from_utf8(bufferFilepath()),
-                            _("Select a BibTeX style"), filter, false, dir1);
+       QString const label1 = qt_("Documents|#o#O");
+       QString const dir1 = toqstr(lyxrc.document_path);
+       QStringList const filter(qt_("BibTeX Styles (*.bst)"));
+       return browseRelFile(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);
-       }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it) {
-               *it = onlyFilename(*it);
+               data = texFileList("bstFiles.lst");
        }
+       for (int i = 0; i != data.size(); ++i)
+               data[i] = onlyFilename(data[i]);
        // sort on filename only (no path)
-       std::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);
-       }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it) {
-               *it = onlyFilename(*it);
+               data = texFileList("bibFiles.lst");
        }
+       for (int i = 0; i != data.size(); ++i)
+               data[i] = onlyFilename(data[i]);
        // sort on filename only (no path)
-       std::sort(data.begin(), data.end());
+       data.sort();
+       return data;
 }
 
 
@@ -457,52 +500,70 @@ bool GuiBibtex::bibtotoc() const
 }
 
 
-string const GuiBibtex::getStylefile() const
+QString GuiBibtex::styleFile() const
 {
        // the different bibtex packages have (and need) their
        // own "plain" stylefiles
-       biblio::CiteEngine const engine = buffer().params().getEngine();
-       docstring defaultstyle;
+       CiteEngine const engine = buffer().params().citeEngine();
+       QString defaultstyle;
        switch (engine) {
-       case biblio::ENGINE_BASIC:
-               defaultstyle = from_ascii("plain");
+       case ENGINE_BASIC:
+               defaultstyle = "plain";
                break;
-       case biblio::ENGINE_NATBIB_AUTHORYEAR:
-               defaultstyle = from_ascii("plainnat");
+       case ENGINE_NATBIB_AUTHORYEAR:
+               defaultstyle = "plainnat";
                break;
-       case biblio::ENGINE_NATBIB_NUMERICAL:
-               defaultstyle = from_ascii("plainnat");
+       case ENGINE_NATBIB_NUMERICAL:
+               defaultstyle = "plainnat";
                break;
-       case biblio::ENGINE_JURABIB:
-               defaultstyle = from_ascii("jurabib");
+       case ENGINE_JURABIB:
+               defaultstyle = "jurabib";
                break;
        }
 
-       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;
 }
 
 
-Dialog * createGuiBibtex(LyXView & lv) { return new GuiBibtex(lv); }
+bool GuiBibtex::initialiseParams(std::string const & data)
+{
+       InsetCommand::string2params("bibtex", data, params_);
+       return true;
+}
+
+
+void GuiBibtex::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string("bibtex", 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"