]> 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 121053f0a78813d736ec356102940f6d9ce41a11..052510b425c245360cd0c8347e4fcda6aec83231 100644 (file)
 
 #include "Buffer.h"
 #include "BufferParams.h"
-#include "debug.h"
 #include "ui_BibtexAddUi.h"
 #include "qt_helpers.h"
 #include "Validator.h"
 #include "LyXRC.h"
-#include "gettext.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 "support/FileFilterList.h"
-
-#include "frontend_helpers.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)
+       : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
 {
        setupUi(this);
 
-       setViewTitle( _("BibTeX Bibliography"));
-
        QDialog::setModal(true);
 
        connect(okPB, SIGNAL(clicked()),
@@ -73,9 +57,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()));
@@ -90,7 +78,7 @@ 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()));
@@ -100,7 +88,7 @@ GuiBibtex::GuiBibtex(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()));
@@ -115,7 +103,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();
 }
 
 
@@ -207,8 +199,14 @@ 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
+                                       | Qt::ItemIsUserCheckable);
+                               db->setCheckState(Qt::Checked);
+                               databaseLW->addItem(db);
+                       }
                }
        }
 
@@ -217,38 +215,65 @@ void GuiBibtex::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 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 GuiBibtex::databaseChanged()
+void GuiBibtex::downPressed()
 {
-       deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
+       int row = databaseLW->currentRow();
+       QListWidgetItem *cur = databaseLW->takeItem(row);
+       databaseLW->insertItem(row + 1, cur);
+       databaseLW->setCurrentItem(cur);
+       changed();
 }
 
 
-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);
 }
 
 
@@ -259,13 +284,21 @@ void GuiBibtex::updateContents()
        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())
-                       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();
@@ -283,15 +316,20 @@ void GuiBibtex::updateContents()
        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();
 
@@ -322,14 +360,18 @@ void GuiBibtex::updateContents()
 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");
        }
 
        params_["bibfiles"] = dbs;
+       params_["embed"] = emb;
 
        docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
        bool const bibtotoc = bibtocCB->isChecked();
@@ -347,26 +389,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();
 }
 
 
@@ -379,23 +430,22 @@ bool GuiBibtex::isValid()
 docstring const GuiBibtex::browseBib(docstring const & in_name) const
 {
        // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 from_utf8(lyxrc.document_path));
+       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, dir1);
+               _("Select a BibTeX database to add"), filter, false, label1, dir1);
 }
 
 
 docstring const GuiBibtex::browseBst(docstring const & in_name) const
 {
        // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 from_utf8(lyxrc.document_path));
+       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, dir1);
+               _("Select a BibTeX style"), filter, false, label1, dir1);
 }
 
 
@@ -415,7 +465,7 @@ void GuiBibtex::getBibStyles(vector<string> & data) const
                *it = onlyFilename(*it);
        }
        // sort on filename only (no path)
-       std::sort(data.begin(), data.end());
+       sort(data.begin(), data.end());
 }
 
 
@@ -435,7 +485,7 @@ void GuiBibtex::getBibFiles(vector<string> & data) const
                *it = onlyFilename(*it);
        }
        // sort on filename only (no path)
-       std::sort(data.begin(), data.end());
+       sort(data.begin(), data.end());
 }
 
 
@@ -499,7 +549,7 @@ string const GuiBibtex::getStylefile() const
 }
 
 
-Dialog * createGuiBibtex(LyXView & lv) { return new GuiBibtex(lv); }
+Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
 
 
 } // namespace frontend