3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "GuiBibtex.h"
19 #include "BufferParams.h"
20 #include "support/debug.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "support/gettext.h"
27 #include "ButtonPolicy.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
33 #include <QPushButton>
34 #include <QListWidget>
36 #include <QCloseEvent>
40 using namespace lyx::support;
46 GuiBibtex::GuiBibtex(GuiView & lv)
47 : GuiCommand(lv, "bibtex")
51 setViewTitle( _("BibTeX Bibliography"));
53 QDialog::setModal(true);
55 connect(okPB, SIGNAL(clicked()),
56 this, SLOT(slotOK()));
57 connect(closePB, SIGNAL(clicked()),
58 this, SLOT(slotClose()));
59 connect(stylePB, SIGNAL(clicked()),
60 this, SLOT(browsePressed()));
61 connect(deletePB, SIGNAL(clicked()),
62 this, SLOT(deletePressed()));
63 connect(upPB, SIGNAL(clicked()),
64 this, SLOT(upPressed()));
65 connect(downPB, SIGNAL(clicked()),
66 this, SLOT(downPressed()));
67 connect(styleCB, SIGNAL(editTextChanged(QString)),
68 this, SLOT(change_adaptor()));
69 connect(databaseLW, SIGNAL(itemSelectionChanged()),
70 this, SLOT(databaseChanged()));
71 connect(bibtocCB, SIGNAL(clicked()),
72 this, SLOT(change_adaptor()));
73 connect(btPrintCO, SIGNAL(activated(int)),
74 this, SLOT(change_adaptor()));
75 connect(addBibPB, SIGNAL(clicked()),
76 this, SLOT(addPressed()));
78 add_ = new GuiBibtexAddDialog(this);
79 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
80 add_bc_.setOK(add_->addPB);
81 add_bc_.setCancel(add_->closePB);
82 add_bc_.addCheckedLineEdit(add_->bibED, 0);
84 connect(add_->bibED, SIGNAL(textChanged(QString)),
85 this, SLOT(bibEDChanged()));
86 connect(add_->addPB, SIGNAL(clicked()),
87 this, SLOT(addDatabase()));
88 connect(add_->addPB, SIGNAL(clicked()),
89 add_, SLOT(accept()));
90 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
91 this, SLOT(addDatabase()));
92 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
93 add_, SLOT(accept()));
94 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
95 this, SLOT(availableChanged()));
96 connect(add_->browsePB, SIGNAL(clicked()),
97 this, SLOT(browseBibPressed()));
98 connect(add_->closePB, SIGNAL(clicked()),
99 add_, SLOT(reject()));
101 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
103 bc().setCancel(closePB);
104 bc().addReadOnly(databaseLW);
105 bc().addReadOnly(stylePB);
106 bc().addReadOnly(styleCB);
107 bc().addReadOnly(bibtocCB);
108 bc().addReadOnly(addBibPB);
109 bc().addReadOnly(deletePB);
110 bc().addReadOnly(upPB);
111 bc().addReadOnly(downPB);
113 // Make sure the delete/up/down buttons are disabled if necessary.
118 void GuiBibtex::bibEDChanged()
120 // Indicate to the button controller that the contents have
121 // changed. The actual test of validity is carried out by
122 // the checkedLineEdit.
123 add_bc_.setValid(true);
127 void GuiBibtex::change_adaptor()
133 void GuiBibtex::browsePressed()
135 docstring const file = browseBst(docstring());
139 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
140 bool present = false;
141 unsigned int pres = 0;
143 for (int i = 0; i != styleCB->count(); ++i) {
144 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
151 styleCB->insertItem(0, toqstr(filen));
153 styleCB->setCurrentIndex(pres);
159 void GuiBibtex::browseBibPressed()
161 docstring const file = trim(browseBib(docstring()));
165 QString const f = toqstr(changeExtension(to_utf8(file), ""));
166 bool present = false;
168 for (int i = 0; i < add_->bibLW->count(); ++i) {
169 if (add_->bibLW->item(i)->text() == f)
174 add_->bibLW->addItem(f);
178 add_->bibED->setText(f);
183 void GuiBibtex::addPressed()
185 add_bc_.setValid(false);
190 void GuiBibtex::addDatabase()
192 int const sel = add_->bibLW->currentRow();
193 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
195 if (sel < 0 && file.empty())
198 // Add the selected browser_bib keys to browser_database
199 // multiple selections are possible
200 for (int i = 0; i != add_->bibLW->count(); ++i) {
201 QListWidgetItem * const item = add_->bibLW->item(i);
202 if (add_->bibLW->isItemSelected(item)) {
203 add_->bibLW->setItemSelected(item, false);
204 QList<QListWidgetItem *> matches =
205 databaseLW->findItems(item->text(), Qt::MatchExactly);
206 if (matches.empty()) {
207 QString label = item->text();
208 QListWidgetItem * db = new QListWidgetItem(label);
209 db->setFlags(db->flags() | Qt::ItemIsSelectable
210 | Qt::ItemIsUserCheckable);
211 db->setCheckState(Qt::Checked);
212 databaseLW->addItem(db);
218 add_->bibED->clear();
219 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
220 QList<QListWidgetItem *> matches =
221 databaseLW->findItems(f, Qt::MatchExactly);
222 if (matches.empty()) {
223 QListWidgetItem * db = new QListWidgetItem(f);
224 db->setFlags(db->flags() | Qt::ItemIsSelectable
225 | Qt::ItemIsUserCheckable);
226 db->setCheckState(Qt::Checked);
227 databaseLW->addItem(db);
235 void GuiBibtex::deletePressed()
237 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
245 void GuiBibtex::upPressed()
247 int row = databaseLW->currentRow();
248 QListWidgetItem *cur;
249 databaseLW->insertItem(row - 1, cur = databaseLW->takeItem(row));
250 databaseLW->setCurrentItem(cur);
255 void GuiBibtex::downPressed()
257 int row = databaseLW->currentRow();
258 QListWidgetItem *cur;
259 databaseLW->insertItem(row + 1, cur = databaseLW->takeItem(row));
260 databaseLW->setCurrentItem(cur);
265 void GuiBibtex::databaseChanged()
267 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
268 upPB->setEnabled(!isBufferReadonly() && databaseLW->count() > 1 && databaseLW->currentRow() > 0);
269 downPB->setEnabled(!isBufferReadonly() && databaseLW->count() > 1 && databaseLW->currentRow() < databaseLW->count() - 1);
273 void GuiBibtex::availableChanged()
275 add_bc_.setValid(true);
279 void GuiBibtex::closeEvent(QCloseEvent *e)
286 void GuiBibtex::updateContents()
288 bool bibtopic = usingBibtopic();
292 docstring bibs = params_["bibfiles"];
293 docstring embs = params_["embed"];
297 while (!bibs.empty()) {
298 bibs = split(bibs, bib, ',');
299 embs = split(embs, emb, ',');
302 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
303 db->setFlags(db->flags() | Qt::ItemIsSelectable
304 | Qt::ItemIsUserCheckable);
305 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
306 databaseLW->addItem(db);
310 add_->bibLW->clear();
312 vector<string> bib_str;
313 getBibFiles(bib_str);
314 for (vector<string>::const_iterator it = bib_str.begin();
315 it != bib_str.end(); ++it) {
316 string bibItem(changeExtension(*it, ""));
317 add_->bibLW->addItem(toqstr(bibItem));
320 string bibstyle = getStylefile();
322 bibtocCB->setChecked(bibtotoc() && !bibtopic);
323 bibtocCB->setEnabled(!bibtopic);
325 if (!bibtopic && btPrintCO->count() == 3)
326 btPrintCO->removeItem(1);
327 else if (bibtopic && btPrintCO->count() < 3)
328 btPrintCO->insertItem(1, qt_("all uncited references", 0));
330 docstring btprint = params_["btprint"];
332 if ((bibtopic && btprint == "btPrintNotCited") ||
333 (!bibtopic && btprint == "btPrintAll"))
335 else if (bibtopic && btprint == "btPrintAll")
338 btPrintCO->setCurrentIndex(btp);
346 for (vector<string>::const_iterator it = str.begin();
347 it != str.end(); ++it) {
348 string item(changeExtension(*it, ""));
349 if (item == bibstyle)
350 item_nr = int(it - str.begin());
351 styleCB->addItem(toqstr(item));
354 if (item_nr == -1 && !bibstyle.empty()) {
355 styleCB->addItem(toqstr(bibstyle));
356 item_nr = styleCB->count() - 1;
360 styleCB->setCurrentIndex(item_nr);
362 styleCB->clearEditText();
366 void GuiBibtex::applyView()
368 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
369 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
371 unsigned int maxCount = databaseLW->count();
372 for (unsigned int i = 1; i < maxCount; i++) {
374 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
376 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
379 params_["bibfiles"] = dbs;
380 params_["embed"] = emb;
382 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
383 bool const bibtotoc = bibtocCB->isChecked();
385 if (bibtotoc && (!bibstyle.empty())) {
386 // both bibtotoc and style
387 params_["options"] = "bibtotoc," + bibstyle;
388 } else if (bibtotoc) {
389 // bibtotoc and no style
390 params_["options"] = from_ascii("bibtotoc");
392 // only style. An empty one is valid, because some
393 // documentclasses have an own \bibliographystyle{}
395 params_["options"] = bibstyle;
398 int btp = btPrintCO->currentIndex();
400 if (usingBibtopic()) {
401 // bibtopic allows three kinds of sections:
402 // 1. sections that include all cited references of the database(s)
403 // 2. sections that include all uncited references of the database(s)
404 // 3. sections that include all references of the database(s), cited or not
407 params_["btprint"] = from_ascii("btPrintCited");
410 params_["btprint"] = from_ascii("btPrintNotCited");
413 params_["btprint"] = from_ascii("btPrintAll");
419 params_["btprint"] = docstring();
423 params_["btprint"] = from_ascii("btPrintAll");
430 bool GuiBibtex::isValid()
432 return databaseLW->count() != 0;
436 docstring const GuiBibtex::browseBib(docstring const & in_name) const
439 docstring const label1 = _("Documents|#o#O");
440 docstring const dir1 = from_utf8(lyxrc.document_path);
441 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
442 return browseRelFile(in_name, from_utf8(bufferFilepath()),
443 _("Select a BibTeX database to add"), filter, false, label1, dir1);
447 docstring const GuiBibtex::browseBst(docstring const & in_name) const
450 docstring const label1 = _("Documents|#o#O");
451 docstring const dir1 = from_utf8(lyxrc.document_path);
452 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
453 return browseRelFile(in_name, from_utf8(bufferFilepath()),
454 _("Select a BibTeX style"), filter, false, label1, dir1);
458 void GuiBibtex::getBibStyles(vector<string> & data) const
462 getTexFileList("bstFiles.lst", data);
463 // test, if we have a valid list, otherwise run rescan
466 getTexFileList("bstFiles.lst", data);
468 vector<string>::iterator it = data.begin();
469 vector<string>::iterator end = data.end();
470 for (; it != end; ++it) {
471 *it = onlyFilename(*it);
473 // sort on filename only (no path)
474 sort(data.begin(), data.end());
478 void GuiBibtex::getBibFiles(vector<string> & data) const
482 getTexFileList("bibFiles.lst", data);
483 // test, if we have a valid list, otherwise run rescan
486 getTexFileList("bibFiles.lst", data);
488 vector<string>::iterator it = data.begin();
489 vector<string>::iterator end = data.end();
490 for (; it != end; ++it) {
491 *it = onlyFilename(*it);
493 // sort on filename only (no path)
494 sort(data.begin(), data.end());
498 void GuiBibtex::rescanBibStyles() const
504 bool GuiBibtex::usingBibtopic() const
506 return buffer().params().use_bibtopic;
510 bool GuiBibtex::bibtotoc() const
512 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
516 string const GuiBibtex::getStylefile() const
518 // the different bibtex packages have (and need) their
519 // own "plain" stylefiles
520 biblio::CiteEngine const engine = buffer().params().getEngine();
521 docstring defaultstyle;
523 case biblio::ENGINE_BASIC:
524 defaultstyle = from_ascii("plain");
526 case biblio::ENGINE_NATBIB_AUTHORYEAR:
527 defaultstyle = from_ascii("plainnat");
529 case biblio::ENGINE_NATBIB_NUMERICAL:
530 defaultstyle = from_ascii("plainnat");
532 case biblio::ENGINE_JURABIB:
533 defaultstyle = from_ascii("jurabib");
537 docstring bst = params_["options"];
540 if (contains(bst, ',')) {
541 docstring bibtotoc = from_ascii("bibtotoc");
542 bst = split(bst, bibtotoc, ',');
547 // propose default style file for new insets
548 // existing insets might have (legally) no bst files
549 // (if the class already provides a style)
550 if (bst.empty() && params_["bibfiles"].empty())
558 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
561 } // namespace frontend
564 #include "GuiBibtex_moc.cpp"