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 "CiteEnginesList.h"
21 #include "FuncRequest.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
26 #include "ui_BibtexAddUi.h"
28 #include "ButtonPolicy.h"
30 #include "frontends/alert.h"
32 #include "insets/InsetBibtex.h"
34 #include "support/debug.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h" // changeExtension
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include <QPushButton>
42 #include <QListWidget>
47 using namespace lyx::support;
53 GuiBibtex::GuiBibtex(GuiView & lv)
54 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
55 params_(insetCode("bibtex"))
59 QDialog::setModal(true);
60 setWindowModality(Qt::WindowModal);
62 connect(okPB, SIGNAL(clicked()),
63 this, SLOT(slotOK()));
64 connect(closePB, SIGNAL(clicked()),
65 this, SLOT(slotClose()));
66 connect(stylePB, SIGNAL(clicked()),
67 this, SLOT(browsePressed()));
68 connect(deletePB, SIGNAL(clicked()),
69 this, SLOT(deletePressed()));
70 connect(upPB, SIGNAL(clicked()),
71 this, SLOT(upPressed()));
72 connect(downPB, SIGNAL(clicked()),
73 this, SLOT(downPressed()));
74 connect(styleCB, SIGNAL(editTextChanged(QString)),
75 this, SLOT(change_adaptor()));
76 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
77 this, SLOT(databaseChanged()));
78 connect(bibtocCB, SIGNAL(clicked()),
79 this, SLOT(change_adaptor()));
80 connect(btPrintCO, SIGNAL(activated(int)),
81 this, SLOT(change_adaptor()));
82 connect(addBibPB, SIGNAL(clicked()),
83 this, SLOT(addPressed()));
84 connect(rescanPB, SIGNAL(clicked()),
85 this, SLOT(rescanClicked()));
86 connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
87 this, SLOT(change_adaptor()));
89 add_ = new GuiBibtexAddDialog(this);
90 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
91 add_bc_.setOK(add_->addPB);
92 add_bc_.setCancel(add_->closePB);
93 add_bc_.addCheckedLineEdit(add_->bibED, 0);
95 connect(add_->bibED, SIGNAL(textChanged(QString)),
96 this, SLOT(bibEDChanged()));
97 connect(add_->addPB, SIGNAL(clicked()),
98 this, SLOT(addDatabase()));
99 connect(add_->addPB, SIGNAL(clicked()),
100 add_, SLOT(accept()));
101 connect(add_->rescanPB, SIGNAL(clicked()),
102 this, SLOT(rescanClicked()));
103 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
104 this, SLOT(addDatabase()));
105 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
106 add_, SLOT(accept()));
107 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
108 this, SLOT(availableChanged()));
109 connect(add_->browsePB, SIGNAL(clicked()),
110 this, SLOT(browseBibPressed()));
111 connect(add_->closePB, SIGNAL(clicked()),
112 add_, SLOT(reject()));
114 add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
115 "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
116 "If you want to reuse your own database, this is the place you should store it.")));
118 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
120 bc().setCancel(closePB);
121 bc().addReadOnly(databaseLW);
122 bc().addReadOnly(stylePB);
123 bc().addReadOnly(styleCB);
124 bc().addReadOnly(bibtocCB);
125 bc().addReadOnly(addBibPB);
126 // Delete/Up/Down are handled with more conditions in
127 // databaseChanged().
129 // Make sure the delete/up/down buttons are disabled if necessary.
134 void GuiBibtex::bibEDChanged()
136 // Indicate to the button controller that the contents have
137 // changed. The actual test of validity is carried out by
138 // the checkedLineEdit.
139 add_bc_.setValid(true);
143 void GuiBibtex::change_adaptor()
149 void GuiBibtex::browsePressed()
151 QString const file = browseBst(QString());
156 QString const filen = changeExtension(file, "");
157 bool present = false;
158 unsigned int pres = 0;
160 for (int i = 0; i != styleCB->count(); ++i) {
161 if (styleCB->itemText(i) == filen) {
168 styleCB->insertItem(0, filen);
170 styleCB->setCurrentIndex(pres);
175 void GuiBibtex::browseBibPressed()
177 QString const file = browseBib(QString()).trimmed();
182 QString const f = changeExtension(file, "");
183 bool present = false;
185 for (int i = 0; i < add_->bibLW->count(); ++i) {
186 if (add_->bibLW->item(i)->text() == f)
191 add_->bibLW->addItem(f);
195 add_->bibED->setText(f);
199 void GuiBibtex::addPressed()
201 add_bc_.setValid(false);
206 void GuiBibtex::addDatabase()
208 int const sel = add_->bibLW->currentRow();
209 QString const file = add_->bibED->text().trimmed();
211 if (sel < 0 && file.isEmpty())
214 // Add the selected browser_bib keys to browser_database
215 // multiple selections are possible
216 for (int i = 0; i != add_->bibLW->count(); ++i) {
217 QListWidgetItem * const item = add_->bibLW->item(i);
218 if (add_->bibLW->isItemSelected(item)) {
219 add_->bibLW->setItemSelected(item, false);
220 QList<QListWidgetItem *> matches =
221 databaseLW->findItems(item->text(), Qt::MatchExactly);
222 if (matches.empty()) {
223 QString label = item->text();
224 QListWidgetItem * db = new QListWidgetItem(label);
225 db->setFlags(db->flags() | Qt::ItemIsSelectable);
226 databaseLW->addItem(db);
231 if (!file.isEmpty()) {
232 add_->bibED->clear();
233 QString const f = changeExtension(file, "");
234 QList<QListWidgetItem *> matches =
235 databaseLW->findItems(f, Qt::MatchExactly);
236 if (matches.empty()) {
237 QListWidgetItem * db = new QListWidgetItem(f);
238 db->setFlags(db->flags() | Qt::ItemIsSelectable);
239 databaseLW->addItem(db);
248 void GuiBibtex::deletePressed()
250 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
259 void GuiBibtex::upPressed()
261 int row = databaseLW->currentRow();
262 QListWidgetItem *cur = databaseLW->takeItem(row);
263 databaseLW->insertItem(row - 1, cur);
264 databaseLW->setCurrentItem(cur);
269 void GuiBibtex::downPressed()
271 int row = databaseLW->currentRow();
272 QListWidgetItem *cur = databaseLW->takeItem(row);
273 databaseLW->insertItem(row + 1, cur);
274 databaseLW->setCurrentItem(cur);
279 void GuiBibtex::rescanClicked()
286 void GuiBibtex::databaseChanged()
288 bool readOnly = isBufferReadonly();
289 int count = databaseLW->count();
290 int row = databaseLW->currentRow();
291 deletePB->setEnabled(!readOnly && row != -1);
292 upPB->setEnabled(!readOnly && count > 1 && row > 0);
293 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
297 void GuiBibtex::availableChanged()
299 add_bc_.setValid(true);
303 void GuiBibtex::updateContents()
305 bool bibtopic = usingBibtopic();
306 bool biblatex = usingBiblatex();
309 setTitle(qt_("Biblatex Bibliography"));
311 setTitle(qt_("BibTeX Bibliography"));
315 docstring bibs = params_["bibfiles"];
318 while (!bibs.empty()) {
319 bibs = split(bibs, bib, ',');
322 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
323 db->setFlags(db->flags() | Qt::ItemIsSelectable);
324 databaseLW->addItem(db);
328 add_->bibLW->clear();
330 QStringList bibfiles = bibFiles();
331 for (int i = 0; i != bibfiles.count(); ++i)
332 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
334 QString const bibstyle = styleFile();
336 bibtocCB->setChecked(bibtotoc() && !bibtopic);
337 bibtocCB->setEnabled(!bibtopic);
340 btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
342 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
343 btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
344 if (usingBiblatex() && !buffer().masterParams().multibib.empty())
345 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
347 docstring btprint = params_["btprint"];
350 btprint = from_ascii("btPrintCited");
351 btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
353 // Only useful for biblatex
354 biblatexOptsLA->setVisible(biblatex);
355 biblatexOptsLE->setVisible(biblatex);
357 // only useful for BibTeX
358 styleCB->setVisible(!biblatex);
359 styleLA->setVisible(!biblatex);
360 stylePB->setVisible(!biblatex);
367 QStringList const str = bibStyles();
368 for (int i = 0; i != str.count(); ++i) {
369 QString item = changeExtension(str[i], "");
370 if (item == bibstyle)
372 styleCB->addItem(item);
375 if (item_nr == -1 && !bibstyle.isEmpty()) {
376 styleCB->addItem(bibstyle);
377 item_nr = styleCB->count() - 1;
382 styleCB->setCurrentIndex(item_nr);
384 styleCB->clearEditText();
386 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
390 void GuiBibtex::applyView()
394 unsigned int maxCount = databaseLW->count();
395 for (unsigned int i = 0; i < maxCount; i++) {
398 QString item = databaseLW->item(i)->text();
399 docstring bibfile = qstring_to_ucs4(item);
403 params_["bibfiles"] = dbs;
405 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
406 bool const bibtotoc = bibtocCB->isChecked();
408 if (bibtotoc && !bibstyle.empty()) {
409 // both bibtotoc and style
410 params_["options"] = "bibtotoc," + bibstyle;
411 } else if (bibtotoc) {
412 // bibtotoc and no style
413 params_["options"] = from_ascii("bibtotoc");
415 // only style. An empty one is valid, because some
416 // documentclasses have an own \bibliographystyle{}
418 params_["options"] = bibstyle;
421 params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
423 params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
427 bool GuiBibtex::isValid()
429 return databaseLW->count() != 0;
433 QString GuiBibtex::browseBib(QString const & in_name) const
435 QString const label1 = qt_("Documents|#o#O");
436 QString const dir1 = toqstr(lyxrc.document_path);
437 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
438 return browseRelToParent(in_name, bufferFilePath(),
439 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
443 QString GuiBibtex::browseBst(QString const & in_name) const
445 QString const label1 = qt_("Documents|#o#O");
446 QString const dir1 = toqstr(lyxrc.document_path);
447 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
448 return browseRelToParent(in_name, bufferFilePath(),
449 qt_("Select a BibTeX style"), filter, false, label1, dir1);
453 QStringList GuiBibtex::bibStyles() const
455 QStringList data = texFileList("bstFiles.lst");
456 // test whether we have a valid list, otherwise run rescan
457 if (data.isEmpty()) {
459 data = texFileList("bstFiles.lst");
461 for (int i = 0; i != data.size(); ++i)
462 data[i] = onlyFileName(data[i]);
463 // sort on filename only (no path)
469 QStringList GuiBibtex::bibFiles() const
471 QStringList data = texFileList("bibFiles.lst");
472 // test whether we have a valid list, otherwise run rescan
473 if (data.isEmpty()) {
475 data = texFileList("bibFiles.lst");
477 for (int i = 0; i != data.size(); ++i)
478 data[i] = onlyFileName(data[i]);
479 // sort on filename only (no path)
485 void GuiBibtex::rescanBibStyles() const
488 rescanTexStyles("bib");
490 rescanTexStyles("bst bib");
494 bool GuiBibtex::usingBibtopic() const
496 return buffer().params().useBibtopic();
500 bool GuiBibtex::bibtotoc() const
502 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
506 bool GuiBibtex::usingBiblatex() const
508 return buffer().masterBuffer()->params().useBiblatex();
512 QString GuiBibtex::styleFile() const
514 // the different bibtex packages have (and need) their
515 // own "plain" stylefiles
516 QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
518 QString bst = toqstr(params_["options"]);
521 int pos = bst.indexOf(',');
524 // docstring bibtotoc = from_ascii("bibtotoc");
525 // bst = split(bst, bibtotoc, ',');
526 bst = bst.mid(pos + 1);
532 // propose default style file for new insets
533 // existing insets might have (legally) no bst files
534 // (if the class already provides a style)
535 if (bst.isEmpty() && params_["bibfiles"].empty())
542 bool GuiBibtex::initialiseParams(std::string const & data)
544 InsetCommand::string2params(data, params_);
549 void GuiBibtex::dispatchParams()
551 std::string const lfun = InsetCommand::params2string(params_);
552 dispatch(FuncRequest(getLfun(), lfun));
557 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
560 } // namespace frontend
563 #include "moc_GuiBibtex.cpp"