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 "ui_BibtexAddUi.h"
21 #include "qt_helpers.h"
22 #include "Validator.h"
25 #include "ButtonPolicy.h"
27 #include "support/debug.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include <QPushButton>
34 #include <QListWidget>
39 using namespace lyx::support;
45 GuiBibtex::GuiBibtex(GuiView & lv)
46 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
50 QDialog::setModal(true);
52 connect(okPB, SIGNAL(clicked()),
53 this, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()),
55 this, SLOT(slotClose()));
56 connect(stylePB, SIGNAL(clicked()),
57 this, SLOT(browsePressed()));
58 connect(deletePB, SIGNAL(clicked()),
59 this, SLOT(deletePressed()));
60 connect(upPB, SIGNAL(clicked()),
61 this, SLOT(upPressed()));
62 connect(downPB, SIGNAL(clicked()),
63 this, SLOT(downPressed()));
64 connect(styleCB, SIGNAL(editTextChanged(QString)),
65 this, SLOT(change_adaptor()));
66 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
67 this, SLOT(databaseChanged()));
68 connect(bibtocCB, SIGNAL(clicked()),
69 this, SLOT(change_adaptor()));
70 connect(btPrintCO, SIGNAL(activated(int)),
71 this, SLOT(change_adaptor()));
72 connect(addBibPB, SIGNAL(clicked()),
73 this, SLOT(addPressed()));
75 add_ = new GuiBibtexAddDialog(this);
76 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
77 add_bc_.setOK(add_->addPB);
78 add_bc_.setCancel(add_->closePB);
79 add_bc_.addCheckedLineEdit(add_->bibED, 0);
81 connect(add_->bibED, SIGNAL(textChanged(QString)),
82 this, SLOT(bibEDChanged()));
83 connect(add_->addPB, SIGNAL(clicked()),
84 this, SLOT(addDatabase()));
85 connect(add_->addPB, SIGNAL(clicked()),
86 add_, SLOT(accept()));
87 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
88 this, SLOT(addDatabase()));
89 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
90 add_, SLOT(accept()));
91 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
92 this, SLOT(availableChanged()));
93 connect(add_->browsePB, SIGNAL(clicked()),
94 this, SLOT(browseBibPressed()));
95 connect(add_->closePB, SIGNAL(clicked()),
96 add_, SLOT(reject()));
98 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
100 bc().setCancel(closePB);
101 bc().addReadOnly(databaseLW);
102 bc().addReadOnly(stylePB);
103 bc().addReadOnly(styleCB);
104 bc().addReadOnly(bibtocCB);
105 bc().addReadOnly(addBibPB);
106 // Delete/Up/Down are handled with more conditions in
107 // databaseChanged().
109 // Make sure the delete/up/down buttons are disabled if necessary.
114 void GuiBibtex::bibEDChanged()
116 // Indicate to the button controller that the contents have
117 // changed. The actual test of validity is carried out by
118 // the checkedLineEdit.
119 add_bc_.setValid(true);
123 void GuiBibtex::change_adaptor()
129 void GuiBibtex::browsePressed()
131 QString const file = browseBst(QString());
137 QString const filen = toqstr(changeExtension(fromqstr(file), ""));
138 bool present = false;
139 unsigned int pres = 0;
141 for (int i = 0; i != styleCB->count(); ++i) {
142 if (styleCB->itemText(i) == filen) {
149 styleCB->insertItem(0, filen);
151 styleCB->setCurrentIndex(pres);
156 void GuiBibtex::browseBibPressed()
158 QString const file = browseBib(QString()).trimmed();
163 QString const f = toqstr(changeExtension(fromqstr(file), ""));
164 bool present = false;
166 for (int i = 0; i < add_->bibLW->count(); ++i) {
167 if (add_->bibLW->item(i)->text() == f)
172 add_->bibLW->addItem(f);
176 add_->bibED->setText(f);
180 void GuiBibtex::addPressed()
182 add_bc_.setValid(false);
187 void GuiBibtex::addDatabase()
189 int const sel = add_->bibLW->currentRow();
190 QString const file = add_->bibED->text().trimmed();
192 if (sel < 0 && file.isEmpty())
195 // Add the selected browser_bib keys to browser_database
196 // multiple selections are possible
197 for (int i = 0; i != add_->bibLW->count(); ++i) {
198 QListWidgetItem * const item = add_->bibLW->item(i);
199 if (add_->bibLW->isItemSelected(item)) {
200 add_->bibLW->setItemSelected(item, false);
201 QList<QListWidgetItem *> matches =
202 databaseLW->findItems(item->text(), Qt::MatchExactly);
203 if (matches.empty()) {
204 QString label = item->text();
205 QListWidgetItem * db = new QListWidgetItem(label);
206 db->setFlags(db->flags() | Qt::ItemIsSelectable
207 | Qt::ItemIsUserCheckable);
208 db->setCheckState(Qt::Checked);
209 databaseLW->addItem(db);
214 if (!file.isEmpty()) {
215 add_->bibED->clear();
216 QString const f = toqstr(changeExtension(fromqstr(file), ""));
217 QList<QListWidgetItem *> matches =
218 databaseLW->findItems(f, Qt::MatchExactly);
219 if (matches.empty()) {
220 QListWidgetItem * db = new QListWidgetItem(f);
221 db->setFlags(db->flags() | Qt::ItemIsSelectable
222 | Qt::ItemIsUserCheckable);
223 db->setCheckState(Qt::Checked);
224 databaseLW->addItem(db);
233 void GuiBibtex::deletePressed()
235 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
244 void GuiBibtex::upPressed()
246 int row = databaseLW->currentRow();
247 QListWidgetItem *cur = databaseLW->takeItem(row);
248 databaseLW->insertItem(row - 1, cur);
249 databaseLW->setCurrentItem(cur);
254 void GuiBibtex::downPressed()
256 int row = databaseLW->currentRow();
257 QListWidgetItem *cur = databaseLW->takeItem(row);
258 databaseLW->insertItem(row + 1, cur);
259 databaseLW->setCurrentItem(cur);
264 void GuiBibtex::databaseChanged()
266 bool readOnly = isBufferReadonly();
267 int count = databaseLW->count();
268 int row = databaseLW->currentRow();
269 deletePB->setEnabled(!readOnly && row != -1);
270 upPB->setEnabled(!readOnly && count > 1 && row > 0);
271 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
275 void GuiBibtex::availableChanged()
277 add_bc_.setValid(true);
281 void GuiBibtex::updateContents()
283 bool bibtopic = usingBibtopic();
287 docstring bibs = params_["bibfiles"];
288 docstring embs = params_["embed"];
292 while (!bibs.empty()) {
293 bibs = split(bibs, bib, ',');
294 embs = split(embs, emb, ',');
297 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
298 db->setFlags(db->flags() | Qt::ItemIsSelectable
299 | Qt::ItemIsUserCheckable);
300 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
301 databaseLW->addItem(db);
305 add_->bibLW->clear();
307 vector<string> bib_str;
308 getBibFiles(bib_str);
309 for (vector<string>::const_iterator it = bib_str.begin();
310 it != bib_str.end(); ++it) {
311 string bibItem(changeExtension(*it, ""));
312 add_->bibLW->addItem(toqstr(bibItem));
315 string bibstyle = getStylefile();
317 bibtocCB->setChecked(bibtotoc() && !bibtopic);
318 bibtocCB->setEnabled(!bibtopic);
320 if (!bibtopic && btPrintCO->count() == 3)
321 btPrintCO->removeItem(1);
322 else if (bibtopic && btPrintCO->count() < 3)
323 btPrintCO->insertItem(1, qt_("all uncited references", 0));
325 docstring btprint = params_["btprint"];
327 if ((bibtopic && btprint == "btPrintNotCited") ||
328 (!bibtopic && btprint == "btPrintAll"))
330 else if (bibtopic && btprint == "btPrintAll")
333 btPrintCO->setCurrentIndex(btp);
341 for (vector<string>::const_iterator it = str.begin();
342 it != str.end(); ++it) {
343 string item(changeExtension(*it, ""));
344 if (item == bibstyle)
345 item_nr = int(it - str.begin());
346 styleCB->addItem(toqstr(item));
349 if (item_nr == -1 && !bibstyle.empty()) {
350 styleCB->addItem(toqstr(bibstyle));
351 item_nr = styleCB->count() - 1;
355 styleCB->setCurrentIndex(item_nr);
357 styleCB->clearEditText();
361 void GuiBibtex::applyView()
363 QString dbs = databaseLW->item(0)->text();
364 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
366 unsigned int maxCount = databaseLW->count();
367 for (unsigned int i = 1; i < maxCount; i++) {
369 dbs += databaseLW->item(i)->text();
371 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
374 params_["bibfiles"] = qstring_to_ucs4(dbs);
375 params_["embed"] = emb;
377 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
378 bool const bibtotoc = bibtocCB->isChecked();
380 if (bibtotoc && !bibstyle.empty()) {
381 // both bibtotoc and style
382 params_["options"] = "bibtotoc," + bibstyle;
383 } else if (bibtotoc) {
384 // bibtotoc and no style
385 params_["options"] = from_ascii("bibtotoc");
387 // only style. An empty one is valid, because some
388 // documentclasses have an own \bibliographystyle{}
390 params_["options"] = bibstyle;
393 int btp = btPrintCO->currentIndex();
395 if (usingBibtopic()) {
396 // bibtopic allows three kinds of sections:
397 // 1. sections that include all cited references of the database(s)
398 // 2. sections that include all uncited references of the database(s)
399 // 3. sections that include all references of the database(s), cited or not
402 params_["btprint"] = from_ascii("btPrintCited");
405 params_["btprint"] = from_ascii("btPrintNotCited");
408 params_["btprint"] = from_ascii("btPrintAll");
414 params_["btprint"] = docstring();
418 params_["btprint"] = from_ascii("btPrintAll");
425 bool GuiBibtex::isValid()
427 return databaseLW->count() != 0;
431 QString GuiBibtex::browseBib(QString const & in_name) const
433 QString const label1 = qt_("Documents|#o#O");
434 QString const dir1 = toqstr(lyxrc.document_path);
435 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
436 return browseRelFile(in_name, bufferFilepath(),
437 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
441 QString GuiBibtex::browseBst(QString const & in_name) const
443 QString const label1 = qt_("Documents|#o#O");
444 QString const dir1 = toqstr(lyxrc.document_path);
445 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
446 return browseRelFile(in_name, bufferFilepath(),
447 qt_("Select a BibTeX style"), filter, false, label1, dir1);
451 void GuiBibtex::getBibStyles(vector<string> & data) const
455 getTexFileList("bstFiles.lst", data);
456 // test, if we have a valid list, otherwise run rescan
459 getTexFileList("bstFiles.lst", data);
461 vector<string>::iterator it = data.begin();
462 vector<string>::iterator end = data.end();
463 for (; it != end; ++it)
464 *it = support::onlyFilename(*it);
465 // sort on filename only (no path)
466 sort(data.begin(), data.end());
470 void GuiBibtex::getBibFiles(vector<string> & data) const
474 getTexFileList("bibFiles.lst", data);
475 // test, if we have a valid list, otherwise run rescan
478 getTexFileList("bibFiles.lst", data);
480 vector<string>::iterator it = data.begin();
481 vector<string>::iterator end = data.end();
482 for (; it != end; ++it)
483 *it = support::onlyFilename(*it);
484 // sort on filename only (no path)
485 sort(data.begin(), data.end());
489 void GuiBibtex::rescanBibStyles() const
495 bool GuiBibtex::usingBibtopic() const
497 return buffer().params().use_bibtopic;
501 bool GuiBibtex::bibtotoc() const
503 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
507 string const GuiBibtex::getStylefile() const
509 // the different bibtex packages have (and need) their
510 // own "plain" stylefiles
511 biblio::CiteEngine const engine = buffer().params().getEngine();
512 docstring defaultstyle;
514 case biblio::ENGINE_BASIC:
515 defaultstyle = from_ascii("plain");
517 case biblio::ENGINE_NATBIB_AUTHORYEAR:
518 defaultstyle = from_ascii("plainnat");
520 case biblio::ENGINE_NATBIB_NUMERICAL:
521 defaultstyle = from_ascii("plainnat");
523 case biblio::ENGINE_JURABIB:
524 defaultstyle = from_ascii("jurabib");
528 docstring bst = params_["options"];
531 if (contains(bst, ',')) {
532 docstring bibtotoc = from_ascii("bibtotoc");
533 bst = split(bst, bibtotoc, ',');
538 // propose default style file for new insets
539 // existing insets might have (legally) no bst files
540 // (if the class already provides a style)
541 if (bst.empty() && params_["bibfiles"].empty())
549 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
552 } // namespace frontend
555 #include "GuiBibtex_moc.cpp"